| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184 |
- namespace Terminal.Gui.App;
- public partial class ApplicationImpl
- {
- /// <inheritdoc/>
- public event EventHandler<EventArgs<Rectangle>>? ScreenChanged;
- private readonly object _lockScreen = new ();
- private Rectangle? _screen;
- /// <inheritdoc/>
- public Rectangle Screen
- {
- get
- {
- lock (_lockScreen)
- {
- if (_screen == null)
- {
- _screen = Driver?.Screen ?? new (new (0, 0), new (2048, 2048));
- }
- return _screen.Value;
- }
- }
- set
- {
- if (value is { } && (value.X != 0 || value.Y != 0))
- {
- throw new NotImplementedException ("Screen locations other than 0, 0 are not yet supported");
- }
- lock (_lockScreen)
- {
- _screen = value;
- }
- }
- }
- /// <inheritdoc/>
- public bool ClearScreenNextIteration { get; set; }
- /// <inheritdoc/>
- public bool PositionCursor ()
- {
- if (Driver is null)
- {
- return false;
- }
- // Find the most focused view and position the cursor there.
- View? mostFocused = Navigation?.GetFocused ();
- // If the view is not visible or enabled, don't position the cursor
- if (mostFocused is null || !mostFocused.Visible || !mostFocused.Enabled)
- {
- var current = CursorVisibility.Invisible;
- Driver?.GetCursorVisibility (out current);
- if (current != CursorVisibility.Invisible)
- {
- Driver?.SetCursorVisibility (CursorVisibility.Invisible);
- }
- return false;
- }
- // If the view is not visible within it's superview, don't position the cursor
- Rectangle mostFocusedViewport = mostFocused.ViewportToScreen (mostFocused.Viewport with { Location = Point.Empty });
- Rectangle superViewViewport =
- mostFocused.SuperView?.ViewportToScreen (mostFocused.SuperView.Viewport with { Location = Point.Empty }) ?? Driver.Screen;
- if (!superViewViewport.IntersectsWith (mostFocusedViewport))
- {
- return false;
- }
- Point? cursor = mostFocused.PositionCursor ();
- Driver!.GetCursorVisibility (out CursorVisibility currentCursorVisibility);
- if (cursor is { })
- {
- // Convert cursor to screen coords
- cursor = mostFocused.ViewportToScreen (mostFocused.Viewport with { Location = cursor.Value }).Location;
- // If the cursor is not in a visible location in the SuperView, hide it
- if (!superViewViewport.Contains (cursor.Value))
- {
- if (currentCursorVisibility != CursorVisibility.Invisible)
- {
- Driver.SetCursorVisibility (CursorVisibility.Invisible);
- }
- return false;
- }
- // Show it
- if (currentCursorVisibility == CursorVisibility.Invisible)
- {
- Driver.SetCursorVisibility (mostFocused.CursorVisibility);
- }
- return true;
- }
- if (currentCursorVisibility != CursorVisibility.Invisible)
- {
- Driver.SetCursorVisibility (CursorVisibility.Invisible);
- }
- return false;
- }
- /// <summary>
- /// INTERNAL: Resets the Screen field to null so it will be recalculated on next access.
- /// </summary>
- private void ResetScreen ()
- {
- lock (_lockScreen)
- {
- _screen = null;
- }
- }
- /// <summary>
- /// INTERNAL: Called when the application's size has changed. Sets the size of all <see cref="Toplevel"/>s and fires
- /// the
- /// <see cref="ScreenChanged"/> event.
- /// </summary>
- /// <param name="screen">The new screen size and position.</param>
- private void RaiseScreenChangedEvent (Rectangle screen)
- {
- Screen = new (Point.Empty, screen.Size);
- ScreenChanged?.Invoke (this, new (screen));
- foreach (Toplevel t in SessionStack)
- {
- t.OnSizeChanging (new (screen.Size));
- t.SetNeedsLayout ();
- }
- LayoutAndDraw (true);
- }
- private void Driver_SizeChanged (object? sender, SizeChangedEventArgs e) { RaiseScreenChangedEvent (new (new (0, 0), e.Size!.Value)); }
- /// <inheritdoc/>
- public void LayoutAndDraw (bool forceRedraw = false)
- {
- List<View> tops = [.. SessionStack];
- if (Popover?.GetActivePopover () as View is { Visible: true } visiblePopover)
- {
- visiblePopover.SetNeedsDraw ();
- visiblePopover.SetNeedsLayout ();
- tops.Insert (0, visiblePopover);
- }
- bool neededLayout = View.Layout (tops.ToArray ().Reverse (), Screen.Size);
- if (ClearScreenNextIteration)
- {
- forceRedraw = true;
- ClearScreenNextIteration = false;
- }
- if (forceRedraw)
- {
- Driver?.ClearContents ();
- }
- if (Driver is { })
- {
- Driver.Clip = new (Screen);
- View.Draw (tops, neededLayout || forceRedraw);
- Driver.Clip = new (Screen);
- Driver?.Refresh ();
- }
- }
- }
|