ApplicationImpl.Screen.cs 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  1. namespace Terminal.Gui.App;
  2. public 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. lock (_lockScreen)
  29. {
  30. _screen = value;
  31. }
  32. }
  33. }
  34. /// <inheritdoc/>
  35. public bool ClearScreenNextIteration { get; set; }
  36. /// <inheritdoc/>
  37. public bool PositionCursor ()
  38. {
  39. // Find the most focused view and position the cursor there.
  40. View? mostFocused = Navigation?.GetFocused ();
  41. // If the view is not visible or enabled, don't position the cursor
  42. if (mostFocused is null || !mostFocused.Visible || !mostFocused.Enabled)
  43. {
  44. var current = CursorVisibility.Invisible;
  45. Driver?.GetCursorVisibility (out current);
  46. if (current != CursorVisibility.Invisible)
  47. {
  48. Driver?.SetCursorVisibility (CursorVisibility.Invisible);
  49. }
  50. return false;
  51. }
  52. // If the view is not visible within it's superview, don't position the cursor
  53. Rectangle mostFocusedViewport = mostFocused.ViewportToScreen (mostFocused.Viewport with { Location = Point.Empty });
  54. Rectangle superViewViewport =
  55. mostFocused.SuperView?.ViewportToScreen (mostFocused.SuperView.Viewport with { Location = Point.Empty }) ?? Driver!.Screen;
  56. if (!superViewViewport.IntersectsWith (mostFocusedViewport))
  57. {
  58. return false;
  59. }
  60. Point? cursor = mostFocused.PositionCursor ();
  61. Driver!.GetCursorVisibility (out CursorVisibility currentCursorVisibility);
  62. if (cursor is { })
  63. {
  64. // Convert cursor to screen coords
  65. cursor = mostFocused.ViewportToScreen (mostFocused.Viewport with { Location = cursor.Value }).Location;
  66. // If the cursor is not in a visible location in the SuperView, hide it
  67. if (!superViewViewport.Contains (cursor.Value))
  68. {
  69. if (currentCursorVisibility != CursorVisibility.Invisible)
  70. {
  71. Driver.SetCursorVisibility (CursorVisibility.Invisible);
  72. }
  73. return false;
  74. }
  75. // Show it
  76. if (currentCursorVisibility == CursorVisibility.Invisible)
  77. {
  78. Driver.SetCursorVisibility (mostFocused.CursorVisibility);
  79. }
  80. return true;
  81. }
  82. if (currentCursorVisibility != CursorVisibility.Invisible)
  83. {
  84. Driver.SetCursorVisibility (CursorVisibility.Invisible);
  85. }
  86. return false;
  87. }
  88. /// <summary>
  89. /// INTERNAL: Resets the Screen field to null so it will be recalculated on next access.
  90. /// </summary>
  91. private void ResetScreen ()
  92. {
  93. lock (_lockScreen)
  94. {
  95. _screen = null;
  96. }
  97. }
  98. /// <summary>
  99. /// INTERNAL: Called when the application's size has changed. Sets the size of all <see cref="Toplevel"/>s and fires
  100. /// the
  101. /// <see cref="ScreenChanged"/> event.
  102. /// </summary>
  103. /// <param name="screen">The new screen size and position.</param>
  104. private void RaiseScreenChangedEvent (Rectangle screen)
  105. {
  106. Screen = new (Point.Empty, screen.Size);
  107. ScreenChanged?.Invoke (this, new (screen));
  108. foreach (Toplevel t in SessionStack)
  109. {
  110. t.OnSizeChanging (new (screen.Size));
  111. t.SetNeedsLayout ();
  112. }
  113. LayoutAndDraw (true);
  114. }
  115. private void Driver_SizeChanged (object? sender, SizeChangedEventArgs e) { RaiseScreenChangedEvent (new (new (0, 0), e.Size!.Value)); }
  116. /// <inheritdoc/>
  117. public void LayoutAndDraw (bool forceRedraw = false)
  118. {
  119. List<View> tops = [.. SessionStack];
  120. if (Popover?.GetActivePopover () as View is { Visible: true } visiblePopover)
  121. {
  122. visiblePopover.SetNeedsDraw ();
  123. visiblePopover.SetNeedsLayout ();
  124. tops.Insert (0, visiblePopover);
  125. }
  126. bool neededLayout = View.Layout (tops.ToArray ().Reverse (), Screen.Size);
  127. if (ClearScreenNextIteration)
  128. {
  129. forceRedraw = true;
  130. ClearScreenNextIteration = false;
  131. }
  132. if (forceRedraw)
  133. {
  134. Driver?.ClearContents ();
  135. }
  136. View.SetClipToScreen ();
  137. View.Draw (tops, neededLayout || forceRedraw);
  138. View.SetClipToScreen ();
  139. Driver?.Refresh ();
  140. }
  141. }