123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249 |
- namespace Terminal.Gui;
- public partial class View
- {
- /// <summary>
- /// Gets or sets whether the <see cref="View"/> will be highlighted visually while the mouse button is
- /// pressed.
- /// </summary>
- public bool HighlightOnPress { get; set; }
- /// <summary>Gets or sets whether the <see cref="View"/> wants continuous button pressed events.</summary>
- public virtual bool WantContinuousButtonPressed { get; set; }
- /// <summary>Gets or sets whether the <see cref="View"/> wants mouse position reports.</summary>
- /// <value><see langword="true"/> if mouse position reports are wanted; otherwise, <see langword="false"/>.</value>
- public virtual bool WantMousePositionReports { get; set; }
-
- /// <summary>
- /// Called when the mouse enters the View's <see cref="Bounds"/>. The view will now receive mouse events until the
- /// mouse leaves
- /// the view. At which time, <see cref="OnMouseLeave(Gui.MouseEvent)"/> will be called.
- /// </summary>
- /// <remarks>
- /// The coordinates are relative to <see cref="View.Bounds"/>.
- /// </remarks>
- /// <param name="mouseEvent"></param>
- /// <returns><see langword="true"/>, if the event was handled, <see langword="false"/> otherwise.</returns>
- protected internal virtual bool OnMouseEnter (MouseEvent mouseEvent)
- {
- if (!Enabled)
- {
- return true;
- }
- if (!CanBeVisible (this))
- {
- return false;
- }
- var args = new MouseEventEventArgs (mouseEvent);
- MouseEnter?.Invoke (this, args);
- return args.Handled;
- }
- /// <summary>Event fired when the mouse moves into the View's <see cref="Bounds"/>.</summary>
- public event EventHandler<MouseEventEventArgs> MouseEnter;
- /// <summary>
- /// Called when the mouse has moved out of the View's <see cref="Bounds"/>. The view will no longer receive mouse
- /// events (until the
- /// mouse moves within the view again and <see cref="OnMouseEnter(Gui.MouseEvent)"/> is called).
- /// </summary>
- /// <remarks>
- /// The coordinates are relative to <see cref="View.Bounds"/>.
- /// </remarks>
- /// <param name="mouseEvent"></param>
- /// <returns><see langword="true"/>, if the event was handled, <see langword="false"/> otherwise.</returns>
- protected internal virtual bool OnMouseLeave (MouseEvent mouseEvent)
- {
- if (!Enabled)
- {
- return true;
- }
- if (!CanBeVisible (this))
- {
- return false;
- }
- var args = new MouseEventEventArgs (mouseEvent);
- MouseLeave?.Invoke (this, args);
- return args.Handled;
- }
- /// <summary>Event fired when the mouse leaves the View's <see cref="Bounds"/>.</summary>
- public event EventHandler<MouseEventEventArgs> MouseLeave;
- [CanBeNull]
- private ColorScheme _savedColorScheme;
- /// <summary>Called when a mouse event occurs within the view's <see cref="Bounds"/>.</summary>
- /// <remarks>
- /// <para>
- /// The coordinates are relative to <see cref="View.Bounds"/>.
- /// </para>
- /// </remarks>
- /// <param name="mouseEvent"></param>
- /// <returns><see langword="true"/>, if the event was handled, <see langword="false"/> otherwise.</returns>
- protected internal virtual bool OnMouseEvent (MouseEvent mouseEvent)
- {
- if (!Enabled)
- {
- // A disabled view should not eat mouse events
- return false;
- }
- if (!CanBeVisible (this))
- {
- return false;
- }
- var args = new MouseEventEventArgs (mouseEvent);
- // Default behavior is to invoke Accept (via HotKey) on clicked.
- if (!WantContinuousButtonPressed
- && Application.MouseGrabView != this
- && (mouseEvent.Flags.HasFlag (MouseFlags.Button1Clicked)
- || mouseEvent.Flags.HasFlag (MouseFlags.Button2Clicked)
- || mouseEvent.Flags.HasFlag (MouseFlags.Button3Clicked)
- || mouseEvent.Flags.HasFlag (MouseFlags.Button4Clicked)))
- {
- return OnMouseClick (args);
- }
- if (mouseEvent.Flags.HasFlag (MouseFlags.Button1Pressed)
- || mouseEvent.Flags.HasFlag (MouseFlags.Button2Pressed)
- || mouseEvent.Flags.HasFlag (MouseFlags.Button3Pressed)
- || mouseEvent.Flags.HasFlag (MouseFlags.Button4Pressed))
- {
- // If WantContinuousButtonPressed is true, and this is not the first pressed event,
- // invoke Accept (via HotKey)
- if (WantContinuousButtonPressed && Application.MouseGrabView == this)
- {
- return OnMouseClick (args);
- }
- // The first time we get pressed event, grab the mouse and invert the colors
- if (Application.MouseGrabView != this)
- {
- Application.GrabMouse (this);
- _savedColorScheme = ColorScheme;
- if (HighlightOnPress && ColorScheme is { })
- {
- if (CanFocus)
- {
- // TODO: Make the inverted color configurable
- var cs = new ColorScheme (ColorScheme)
- {
- Focus = new (ColorScheme.Normal.Foreground, ColorScheme.Focus.Background)
- };
- ColorScheme = cs;
- }
- else
- {
- var cs = new ColorScheme (ColorScheme)
- {
- Normal = new (ColorScheme.Focus.Background, ColorScheme.Normal.Foreground)
- };
- ColorScheme = cs;
- }
- }
- if (CanFocus)
- {
- // Set the focus, but don't invoke Accept
- SetFocus ();
- }
- args.Handled = true;
- }
- }
- if (mouseEvent.Flags.HasFlag (MouseFlags.Button1Released)
- || mouseEvent.Flags.HasFlag (MouseFlags.Button2Released)
- || mouseEvent.Flags.HasFlag (MouseFlags.Button3Released)
- || mouseEvent.Flags.HasFlag (MouseFlags.Button4Released))
- {
- // When the mouse is released, if WantContinuousButtonPressed is set, invoke Accept one last time.
- if (WantContinuousButtonPressed)
- {
- OnMouseClick (args);
- }
- if (Application.MouseGrabView == this)
- {
- Application.UngrabMouse ();
- if (HighlightOnPress && _savedColorScheme is { })
- {
- ColorScheme = _savedColorScheme;
- _savedColorScheme = null;
- }
- args.Handled = true;
- }
- }
- if (args.Handled != true)
- {
- MouseEvent?.Invoke (this, args);
- }
- return args.Handled;
- }
- /// <summary>Event fired when a mouse event occurs.</summary>
- /// <remarks>
- /// <para>
- /// The coordinates are relative to <see cref="View.Bounds"/>.
- /// </para>
- /// </remarks>
- public event EventHandler<MouseEventEventArgs> MouseEvent;
- /// <summary>Invokes the MouseClick event.</summary>
- /// <remarks>
- /// <para>
- /// Called when the mouse is either clicked or double-clicked. Check
- /// <see cref="MouseEvent.Flags"/> to see which button was clicked.
- /// </para>
- /// </remarks>
- protected bool OnMouseClick (MouseEventEventArgs args)
- {
- if (!Enabled)
- {
- // QUESTION: Is this right? Should a disabled view eat mouse clicks?
- args.Handled = true;
- return true;
- }
- MouseClick?.Invoke (this, args);
- if (args.Handled)
- {
- return true;
- }
- if (!HasFocus && CanFocus)
- {
- args.Handled = true;
- SetFocus ();
- }
- return args.Handled;
- }
- /// <summary>Event fired when a mouse click occurs.</summary>
- /// <remarks>
- /// <para>
- /// Fired when the mouse is either clicked or double-clicked. Check
- /// <see cref="MouseEvent.Flags"/> to see which button was clicked.
- /// </para>
- /// <para>
- /// The coordinates are relative to <see cref="View.Bounds"/>.
- /// </para>
- /// </remarks>
- public event EventHandler<MouseEventEventArgs> MouseClick;
- }
|