123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401 |
- using System.ComponentModel;
- using System.Diagnostics;
- 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;
- /// <summary>
- /// Processes a <see cref="MouseEvent"/>. This method is called by <see cref="Application.OnMouseEvent"/> when a mouse
- /// event occurs.
- /// </summary>
- /// <remarks>
- /// <para>
- /// A view must be both enabled and visible to receive mouse events.
- /// </para>
- /// <para>
- /// This method calls <see cref="OnMouseEvent"/> to process the event. If the event is not handled, and one of the
- /// mouse buttons was clicked, it calls <see cref="OnMouseClick"/> to process the click.
- /// </para>
- /// <para>
- /// If <see cref="HighlightOnPress"/> is <see langword="true"/>, the view will be highlighted when the mouse is
- /// pressed.
- /// See <see cref="EnableHighlight"/> and <see cref="DisableHighlight"/> for more information.
- /// </para>
- /// <para>
- /// If <see cref="WantContinuousButtonPressed"/> is <see langword="true"/>, the <see cref="OnMouseClick"/> event
- /// will be invoked repeatedly while the button is pressed.
- /// </para>
- /// </remarks>
- /// <param name="mouseEvent"></param>
- /// <returns><see langword="true"/> if the event was handled, <see langword="false"/> otherwise.</returns>
- public bool? NewMouseEvent (MouseEvent mouseEvent)
- {
- if (!Enabled)
- {
- // A disabled view should not eat mouse events
- return false;
- }
- if (!CanBeVisible (this))
- {
- return false;
- }
- if (OnMouseEvent (mouseEvent))
- {
- // Technically mouseEvent.Handled should already be true if implementers of OnMouseEvent
- // follow the rules. But we'll update it just in case.
- return mouseEvent.Handled = true;
- }
- if ((HighlightOnPress || WantContinuousButtonPressed) && Highlight (mouseEvent))
- {
- Debug.Assert (mouseEvent.Handled);
- return mouseEvent.Handled;
- }
- if (mouseEvent.Flags.HasFlag (MouseFlags.Button1Clicked)
- || mouseEvent.Flags.HasFlag (MouseFlags.Button2Clicked)
- || mouseEvent.Flags.HasFlag (MouseFlags.Button3Clicked)
- || mouseEvent.Flags.HasFlag (MouseFlags.Button4Clicked))
- {
- return OnMouseClick (new (mouseEvent));
- }
- return false;
- }
- /// <summary>
- /// Highlight the view when the mouse is pressed.
- /// </summary>
- /// <remarks>
- /// <para>
- /// Set <see cref="HighlightOnPress"/> to <see langword="true"/> to have the view highlighted when the mouse is
- /// pressed.
- /// </para>
- /// <para>
- /// Calls <see cref="OnEnablingHighlight"/> which fires the <see cref="EnablingHighlight"/> event.
- /// </para>
- /// <para>
- /// Calls <see cref="OnDisablingHighlight"/> which fires the <see cref="DisablingHighlight"/> event.
- /// </para>
- /// </remarks>
- /// <param name="mouseEvent"></param>
- /// <returns><see langword="true"/>, if the event was handled, <see langword="false"/> otherwise.</returns>
- private bool Highlight (MouseEvent mouseEvent)
- {
- if (Application.MouseGrabView == this
- && (mouseEvent.Flags.HasFlag (MouseFlags.Button1Clicked)
- || mouseEvent.Flags.HasFlag (MouseFlags.Button2Clicked)
- || mouseEvent.Flags.HasFlag (MouseFlags.Button3Clicked)
- || mouseEvent.Flags.HasFlag (MouseFlags.Button4Clicked)))
- {
- // We're grabbed. Clicked event comes after the last Release. This is our signal to ungrab
- Application.UngrabMouse ();
- DisableHighlight ();
- // If mouse is still in bounds, click
- if (!WantContinuousButtonPressed && Bounds.Contains (mouseEvent.X, mouseEvent.Y))
- {
- return OnMouseClick (new (mouseEvent));
- }
- return mouseEvent.Handled = true;
- }
- if (mouseEvent.Flags.HasFlag (MouseFlags.Button1Pressed)
- || mouseEvent.Flags.HasFlag (MouseFlags.Button2Pressed)
- || mouseEvent.Flags.HasFlag (MouseFlags.Button3Pressed)
- || mouseEvent.Flags.HasFlag (MouseFlags.Button4Pressed))
- {
- // The first time we get pressed event, grab the mouse and set focus
- if (Application.MouseGrabView != this)
- {
- Application.GrabMouse (this);
- if (CanFocus)
- {
- // Set the focus, but don't invoke Accept
- SetFocus ();
- }
- }
- if (Bounds.Contains (mouseEvent.X, mouseEvent.Y))
- {
- EnableHighlight ();
- }
- else
- {
- DisableHighlight ();
- }
- if (WantContinuousButtonPressed && Application.MouseGrabView == this)
- {
- // If this is not the first pressed event, click
- return OnMouseClick (new (mouseEvent));
- }
- return mouseEvent.Handled = true;
- }
- if (mouseEvent.Flags.HasFlag (MouseFlags.Button1Released)
- || mouseEvent.Flags.HasFlag (MouseFlags.Button2Released)
- || mouseEvent.Flags.HasFlag (MouseFlags.Button3Released)
- || mouseEvent.Flags.HasFlag (MouseFlags.Button4Released))
- {
- if (Application.MouseGrabView == this)
- {
- DisableHighlight ();
- }
- return mouseEvent.Handled = true;
- }
- return mouseEvent.Handled;
- }
- [CanBeNull]
- private ColorScheme _savedHighlightColorScheme;
- /// <summary>
- /// Enables the highlight for the view. Called from OnMouseEvent.
- /// </summary>
- public void EnableHighlight ()
- {
- if (OnEnablingHighlight () == true)
- {
- return;
- }
- if (_savedHighlightColorScheme is null && ColorScheme is { })
- {
- _savedHighlightColorScheme ??= ColorScheme;
- if (CanFocus)
- {
- // TODO: Make the inverted color configurable
- var cs = new ColorScheme (ColorScheme)
- {
- // For Buttons etc...
- Focus = new (ColorScheme.Normal.Foreground, ColorScheme.Focus.Background),
- // For Adornments
- Normal = new (ColorScheme.Focus.Foreground, ColorScheme.Normal.Background)
- };
- ColorScheme = cs;
- }
- else
- {
- var cs = new ColorScheme (ColorScheme)
- {
- // For Buttons etc... that can't focus (like up/down).
- Normal = new (ColorScheme.Focus.Background, ColorScheme.Normal.Foreground)
- };
- ColorScheme = cs;
- }
- }
- }
- /// <summary>
- /// Fired when the view is highlighted. Set <see cref="CancelEventArgs.Cancel"/> to <see langword="true"/>
- /// to implement a custom highlight scheme or prevent the view from being highlighted.
- /// </summary>
- public event EventHandler<CancelEventArgs> EnablingHighlight;
- /// <summary>
- /// Called when the view is to be highlighted.
- /// </summary>
- /// <returns><see langword="true"/>, if the event was handled, <see langword="false"/> otherwise.</returns>
- protected virtual bool? OnEnablingHighlight ()
- {
- CancelEventArgs args = new ();
- EnablingHighlight?.Invoke (this, args);
- return args.Cancel;
- }
- /// <summary>
- /// Disables the highlight for the view. Called from OnMouseEvent.
- /// </summary>
- public void DisableHighlight ()
- {
- if (OnDisablingHighlight () == true)
- {
- return;
- }
- // Unhighlight
- if (_savedHighlightColorScheme is { })
- {
- ColorScheme = _savedHighlightColorScheme;
- _savedHighlightColorScheme = null;
- }
- }
- /// <summary>
- /// Fired when the view is no longer to be highlighted. Set <see cref="CancelEventArgs.Cancel"/> to
- /// <see langword="true"/>
- /// to implement a custom highlight scheme or prevent the view from being highlighted.
- /// </summary>
- public event EventHandler<CancelEventArgs> DisablingHighlight;
- /// <summary>
- /// Called when the view is no longer to be highlighted.
- /// </summary>
- /// <returns><see langword="true"/>, if the event was handled, <see langword="false"/> otherwise.</returns>
- protected virtual bool? OnDisablingHighlight ()
- {
- CancelEventArgs args = new ();
- DisablingHighlight?.Invoke (this, args);
- return args.Cancel;
- }
- /// <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)
- {
- var args = new MouseEventEventArgs (mouseEvent);
- 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>
- /// <returns><see langword="true"/>, if the event was handled, <see langword="false"/> otherwise.</returns>
- 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;
- }
|