ApplicationImpl.Screen.cs 5.7 KB

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