ApplicationImpl.Screen.cs 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  1. namespace Terminal.Gui.App;
  2. internal partial class ApplicationImpl
  3. {
  4. /// <inheritdoc/>
  5. public event EventHandler<EventArgs<Rectangle>>? ScreenChanged;
  6. /// <inheritdoc/>
  7. public Rectangle Screen
  8. {
  9. get => Driver?.Screen ?? new (new (0, 0), new (2048, 2048));
  10. set
  11. {
  12. if (value is { } && (value.X != 0 || value.Y != 0))
  13. {
  14. throw new NotImplementedException ("Screen locations other than 0, 0 are not yet supported");
  15. }
  16. Driver?.SetScreenSize (value.Width, value.Height);
  17. }
  18. }
  19. /// <inheritdoc/>
  20. public bool ClearScreenNextIteration { get; set; }
  21. /// <inheritdoc/>
  22. public bool PositionCursor ()
  23. {
  24. if (Driver is null)
  25. {
  26. return false;
  27. }
  28. // Find the most focused view and position the cursor there.
  29. View? mostFocused = Navigation?.GetFocused ();
  30. // If the view is not visible or enabled, don't position the cursor
  31. if (mostFocused is null || !mostFocused.Visible || !mostFocused.Enabled)
  32. {
  33. var current = CursorVisibility.Invisible;
  34. Driver?.GetCursorVisibility (out current);
  35. if (current != CursorVisibility.Invisible)
  36. {
  37. Driver?.SetCursorVisibility (CursorVisibility.Invisible);
  38. }
  39. return false;
  40. }
  41. // If the view is not visible within it's superview, don't position the cursor
  42. Rectangle mostFocusedViewport = mostFocused.ViewportToScreen (mostFocused.Viewport with { Location = Point.Empty });
  43. Rectangle superViewViewport =
  44. mostFocused.SuperView?.ViewportToScreen (mostFocused.SuperView.Viewport with { Location = Point.Empty }) ?? Driver.Screen;
  45. if (!superViewViewport.IntersectsWith (mostFocusedViewport))
  46. {
  47. return false;
  48. }
  49. Point? cursor = mostFocused.PositionCursor ();
  50. Driver!.GetCursorVisibility (out CursorVisibility currentCursorVisibility);
  51. if (cursor is { })
  52. {
  53. // Convert cursor to screen coords
  54. cursor = mostFocused.ViewportToScreen (mostFocused.Viewport with { Location = cursor.Value }).Location;
  55. // If the cursor is not in a visible location in the SuperView, hide it
  56. if (!superViewViewport.Contains (cursor.Value))
  57. {
  58. if (currentCursorVisibility != CursorVisibility.Invisible)
  59. {
  60. Driver.SetCursorVisibility (CursorVisibility.Invisible);
  61. }
  62. return false;
  63. }
  64. // Show it
  65. if (currentCursorVisibility == CursorVisibility.Invisible)
  66. {
  67. Driver.SetCursorVisibility (mostFocused.CursorVisibility);
  68. }
  69. return true;
  70. }
  71. if (currentCursorVisibility != CursorVisibility.Invisible)
  72. {
  73. Driver.SetCursorVisibility (CursorVisibility.Invisible);
  74. }
  75. return false;
  76. }
  77. /// <summary>
  78. /// INTERNAL: Called when the application's screen has changed.
  79. /// Raises the <see cref="ScreenChanged"/> event.
  80. /// </summary>
  81. /// <param name="screen">The new screen size and position.</param>
  82. private void RaiseScreenChangedEvent (Rectangle screen)
  83. {
  84. //Screen = new (Point.Empty, screen.Size);
  85. ScreenChanged?.Invoke (this, new (screen));
  86. foreach (SessionToken t in SessionStack!)
  87. {
  88. if (t.Runnable is View runnableView)
  89. {
  90. runnableView.SetNeedsLayout ();
  91. }
  92. }
  93. }
  94. private void Driver_SizeChanged (object? sender, SizeChangedEventArgs e) { RaiseScreenChangedEvent (new (new (0, 0), e.Size!.Value)); }
  95. /// <inheritdoc/>
  96. public void LayoutAndDraw (bool forceRedraw = false)
  97. {
  98. if (ClearScreenNextIteration)
  99. {
  100. forceRedraw = true;
  101. ClearScreenNextIteration = false;
  102. }
  103. if (forceRedraw)
  104. {
  105. Driver?.ClearContents ();
  106. }
  107. List<View?> views = [.. SessionStack!.Select (r => r.Runnable! as View)!];
  108. if (Popover?.GetActivePopover () as View is { Visible: true } visiblePopover)
  109. {
  110. visiblePopover.SetNeedsDraw ();
  111. visiblePopover.SetNeedsLayout ();
  112. views.Insert (0, visiblePopover);
  113. }
  114. // Layout
  115. bool neededLayout = View.Layout (views.ToArray ().Reverse ()!, Screen.Size);
  116. // Draw
  117. bool needsDraw = forceRedraw || views.Any (v => v is { NeedsDraw: true } or { SubViewNeedsDraw: true });
  118. if (Driver is { } && (neededLayout || needsDraw))
  119. {
  120. Logging.Redraws.Add (1);
  121. Driver.Clip = new (Screen);
  122. // Only force a complete redraw if needed (needsLayout or forceRedraw).
  123. // Otherwise, just redraw views that need it.
  124. View.Draw (views: views.ToArray ().Cast<View> (), neededLayout || forceRedraw);
  125. Driver.Clip = new (Screen);
  126. // Cause the driver to flush any pending updates to the terminal
  127. Driver?.Refresh ();
  128. }
  129. }
  130. }