ApplicationImpl.Screen.cs 5.1 KB

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