ApplicationImpl.Screen.cs 5.2 KB

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