| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618 |
- using System.ComponentModel;
- namespace Terminal.Gui.ViewBase;
- public partial class View // Mouse APIs
- {
- /// <summary>
- /// Handles <see cref="WantContinuousButtonPressed"/>, we have detected a button
- /// down in the view and have grabbed the mouse.
- /// </summary>
- public IMouseHeldDown? MouseHeldDown { get; set; }
- /// <summary>Gets the mouse bindings for this view.</summary>
- public MouseBindings MouseBindings { get; internal set; } = null!;
- private void SetupMouse ()
- {
- MouseHeldDown = new MouseHeldDown (this, App?.TimedEvents, App?.Mouse);
- MouseBindings = new ();
- // TODO: Should the default really work with any button or just button1?
- MouseBindings.Add (MouseFlags.Button1Clicked, Command.Select);
- MouseBindings.Add (MouseFlags.Button2Clicked, Command.Select);
- MouseBindings.Add (MouseFlags.Button3Clicked, Command.Select);
- MouseBindings.Add (MouseFlags.Button4Clicked, Command.Select);
- MouseBindings.Add (MouseFlags.Button1Clicked | MouseFlags.ButtonCtrl, Command.Select);
- }
- /// <summary>
- /// Invokes the Commands bound to the MouseFlags specified by <paramref name="mouseEventArgs"/>.
- /// <para>See <see href="../docs/mouse.md">for an overview of Terminal.Gui mouse APIs.</see></para>
- /// </summary>
- /// <param name="mouseEventArgs">The mouse event passed.</param>
- /// <returns>
- /// <see langword="null"/> if no command was invoked; input processing should continue.
- /// <see langword="false"/> if at least one command was invoked and was not handled (or cancelled); input processing
- /// should continue.
- /// <see langword="true"/> if at least one command was invoked and handled (or cancelled); input processing should
- /// stop.
- /// </returns>
- protected bool? InvokeCommandsBoundToMouse (MouseEventArgs mouseEventArgs)
- {
- if (!MouseBindings.TryGet (mouseEventArgs.Flags, out MouseBinding binding))
- {
- return null;
- }
- binding.MouseEventArgs = mouseEventArgs;
- return InvokeCommands (binding.Commands, binding);
- }
- #region MouseEnterLeave
- /// <summary>
- /// INTERNAL Called by <see cref="IMouse.RaiseMouseEvent"/> when the mouse moves over the View's
- /// <see cref="Frame"/>.
- /// <see cref="MouseLeave"/> will
- /// be raised when the mouse is no longer over the <see cref="Frame"/>. If another View occludes this View, the
- /// that View will also receive MouseEnter/Leave events.
- /// </summary>
- /// <param name="eventArgs"></param>
- /// <returns>
- /// <see langword="true"/> if the event was canceled, <see langword="false"/> if not, <see langword="null"/> if the
- /// view is not visible. Cancelling the event
- /// prevents Views higher in the visible hierarchy from receiving Enter/Leave events.
- /// </returns>
- internal bool? NewMouseEnterEvent (CancelEventArgs eventArgs)
- {
- // Pre-conditions
- if (!CanBeVisible (this))
- {
- return null;
- }
- // Cancellable event
- if (OnMouseEnter (eventArgs))
- {
- return true;
- }
- MouseEnter?.Invoke (this, eventArgs);
- if (eventArgs.Cancel)
- {
- return true;
- }
- MouseState |= MouseState.In;
- if (HighlightStates != MouseState.None)
- {
- SetNeedsDraw ();
- }
- return false;
- }
- /// <summary>
- /// Called when the mouse moves over the View's <see cref="Frame"/> and no other non-SubView occludes it.
- /// <see cref="MouseLeave"/> will
- /// be raised when the mouse is no longer over the <see cref="Frame"/>.
- /// </summary>
- /// <remarks>
- /// <para>
- /// A view must be visible to receive Enter events (Leave events are always received).
- /// </para>
- /// <para>
- /// If the event is cancelled, the mouse event will not be propagated to other views and <see cref="MouseEnter"/>
- /// will not be raised.
- /// </para>
- /// <para>
- /// Adornments receive MouseEnter/Leave events when the mouse is over the Adornment's <see cref="Thickness"/>.
- /// </para>
- /// <para>
- /// See <see cref="MouseState"/> for more information.
- /// </para>
- /// </remarks>
- /// <param name="eventArgs"></param>
- /// <returns>
- /// <see langword="true"/> if the event was canceled, <see langword="false"/> if not. Cancelling the event
- /// prevents Views higher in the visible hierarchy from receiving Enter/Leave events.
- /// </returns>
- protected virtual bool OnMouseEnter (CancelEventArgs eventArgs) { return false; }
- /// <summary>
- /// Raised when the mouse moves over the View's <see cref="Frame"/>. <see cref="MouseLeave"/> will
- /// be raised when the mouse is no longer over the <see cref="Frame"/>. If another View occludes this View, the
- /// that View will also receive MouseEnter/Leave events.
- /// </summary>
- /// <remarks>
- /// <para>
- /// A view must be visible to receive Enter events (Leave events are always received).
- /// </para>
- /// <para>
- /// If the event is cancelled, the mouse event will not be propagated to other views.
- /// </para>
- /// <para>
- /// Adornments receive MouseEnter/Leave events when the mouse is over the Adornment's <see cref="Thickness"/>.
- /// </para>
- /// <para>
- /// Set <see cref="CancelEventArgs.Cancel"/> to <see langword="true"/> if the event was canceled,
- /// <see langword="false"/> if not. Cancelling the event
- /// prevents Views higher in the visible hierarchy from receiving Enter/Leave events.
- /// </para>
- /// <para>
- /// See <see cref="MouseState"/> for more information.
- /// </para>
- /// </remarks>
- public event EventHandler<CancelEventArgs>? MouseEnter;
- /// <summary>
- /// INTERNAL Called by <see cref="IMouse.RaiseMouseEvent"/> when the mouse leaves <see cref="Frame"/>, or is
- /// occluded
- /// by another non-SubView.
- /// </summary>
- /// <remarks>
- /// <para>
- /// This method calls <see cref="OnMouseLeave"/> and raises the <see cref="MouseLeave"/> event.
- /// </para>
- /// <para>
- /// Adornments receive MouseEnter/Leave events when the mouse is over the Adornment's <see cref="Thickness"/>.
- /// </para>
- /// <para>
- /// See <see cref="MouseState"/> for more information.
- /// </para>
- /// </remarks>
- internal void NewMouseLeaveEvent ()
- {
- // Pre-conditions
- // Non-cancellable event
- OnMouseLeave ();
- MouseLeave?.Invoke (this, EventArgs.Empty);
- MouseState &= ~MouseState.In;
- // TODO: Should we also MouseState &= ~MouseState.Pressed; ??
- if (HighlightStates != MouseState.None)
- {
- SetNeedsDraw ();
- }
- }
- /// <summary>
- /// Called when the mouse moves outside View's <see cref="Frame"/>, or is occluded by another non-SubView.
- /// </summary>
- /// <remarks>
- /// <para>
- /// Adornments receive MouseEnter/Leave events when the mouse is over the Adornment's <see cref="Thickness"/>.
- /// </para>
- /// <para>
- /// See <see cref="MouseState"/> for more information.
- /// </para>
- /// </remarks>
- protected virtual void OnMouseLeave () { }
- /// <summary>
- /// Raised when the mouse moves outside View's <see cref="Frame"/>, or is occluded by another non-SubView.
- /// </summary>
- /// <remarks>
- /// <para>
- /// Adornments receive MouseEnter/Leave events when the mouse is over the Adornment's <see cref="Thickness"/>.
- /// </para>
- /// <para>
- /// See <see cref="MouseState"/> for more information.
- /// </para>
- /// </remarks>
- public event EventHandler? MouseLeave;
- #endregion MouseEnterLeave
- #region Low Level Mouse Events
- /// <summary>
- /// Gets or sets whether the <see cref="View"/> wants continuous button pressed events. When set to
- /// <see langword="true"/>,
- /// and the user presses and holds the mouse button, <see cref="NewMouseEvent"/> will be
- /// repeatedly called with the same <see cref="MouseFlags"/> for as long as the mouse button remains pressed.
- /// </summary>
- public 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 bool WantMousePositionReports { get; set; }
- /// <summary>
- /// Processes a new <see cref="MouseEvent"/>. This method is called by <see cref="IMouse.RaiseMouseEvent"/> when a
- /// mouse
- /// event occurs.
- /// </summary>
- /// <remarks>
- /// <para>
- /// A view must be both enabled and visible to receive mouse events.
- /// </para>
- /// <para>
- /// If <see cref="WantContinuousButtonPressed"/> is <see langword="true"/>, and the user presses and holds the
- /// mouse button, <see cref="NewMouseEvent"/> will be repeatedly called with the same <see cref="MouseFlags"/> for
- /// as long as the mouse button remains pressed.
- /// </para>
- /// </remarks>
- /// <param name="mouseEvent"></param>
- /// <returns><see langword="true"/> if the event was handled, <see langword="false"/> otherwise.</returns>
- public bool? NewMouseEvent (MouseEventArgs mouseEvent)
- {
- // Pre-conditions
- if (!Enabled)
- {
- // A disabled view should not eat mouse events
- return false;
- }
- if (!CanBeVisible (this))
- {
- return false;
- }
- if (!WantMousePositionReports && mouseEvent.Flags == MouseFlags.ReportMousePosition)
- {
- return false;
- }
- // Cancellable event
- if (RaiseMouseEvent (mouseEvent) || mouseEvent.Handled)
- {
- return true;
- }
- // Post-Conditions
- if (HighlightStates != MouseState.None || WantContinuousButtonPressed)
- {
- if (WhenGrabbedHandlePressed (mouseEvent))
- {
- return mouseEvent.Handled;
- }
- if (WhenGrabbedHandleReleased (mouseEvent))
- {
- return mouseEvent.Handled;
- }
- if (WhenGrabbedHandleClicked (mouseEvent))
- {
- return mouseEvent.Handled;
- }
- }
- if (mouseEvent.IsWheel)
- {
- return RaiseMouseWheelEvent (mouseEvent);
- }
- return false;
- }
- /// <summary>
- /// Raises the <see cref="RaiseMouseEvent"/>/<see cref="MouseEvent"/> event.
- /// </summary>
- /// <param name="mouseEvent"></param>
- /// <returns><see langword="true"/>, if the event was handled, <see langword="false"/> otherwise.</returns>
- public bool RaiseMouseEvent (MouseEventArgs mouseEvent)
- {
- // TODO: probably this should be moved elsewhere, please advise
- if (WantContinuousButtonPressed && MouseHeldDown != null)
- {
- if (mouseEvent.IsPressed)
- {
- MouseHeldDown.Start ();
- }
- else
- {
- MouseHeldDown.Stop ();
- }
- }
- if (OnMouseEvent (mouseEvent) || mouseEvent.Handled)
- {
- return true;
- }
- MouseEvent?.Invoke (this, mouseEvent);
- if (!mouseEvent.Handled)
- {
- mouseEvent.Handled = InvokeCommandsBoundToMouse (mouseEvent) == true;
- }
- return mouseEvent.Handled;
- }
- /// <summary>Called when a mouse event occurs within the view's <see cref="Viewport"/>.</summary>
- /// <remarks>
- /// <para>
- /// The coordinates are relative to <see cref="View.Viewport"/>.
- /// </para>
- /// </remarks>
- /// <param name="mouseEvent"></param>
- /// <returns><see langword="true"/>, if the event was handled, <see langword="false"/> otherwise.</returns>
- protected virtual bool OnMouseEvent (MouseEventArgs mouseEvent) { return false; }
- /// <summary>Raised when a mouse event occurs.</summary>
- /// <remarks>
- /// <para>
- /// The coordinates are relative to <see cref="View.Viewport"/>.
- /// </para>
- /// </remarks>
- public event EventHandler<MouseEventArgs>? MouseEvent;
- #endregion Low Level Mouse Events
- #region WhenGrabbed Handlers
- /// <summary>
- /// INTERNAL For cases where the view is grabbed and the mouse is clicked, this method handles the released event
- /// (typically
- /// when <see cref="WantContinuousButtonPressed"/> or <see cref="HighlightStates"/> are set).
- /// </summary>
- /// <remarks>
- /// Marked internal just to support unit tests
- /// </remarks>
- /// <param name="mouseEvent"></param>
- /// <returns><see langword="true"/>, if the event was handled, <see langword="false"/> otherwise.</returns>
- internal bool WhenGrabbedHandleReleased (MouseEventArgs mouseEvent)
- {
- mouseEvent.Handled = false;
- if (mouseEvent.IsReleased)
- {
- if (App?.Mouse.MouseGrabView == this)
- {
- //Logging.Debug ($"{Id} - {MouseState}");
- MouseState &= ~MouseState.Pressed;
- MouseState &= ~MouseState.PressedOutside;
- }
- return mouseEvent.Handled = true;
- }
- return false;
- }
- /// <summary>
- /// INTERNAL For cases where the view is grabbed and the mouse is clicked, this method handles the released event
- /// (typically
- /// when <see cref="WantContinuousButtonPressed"/> or <see cref="HighlightStates"/> are set).
- /// </summary>
- /// <remarks>
- /// <para>
- /// Marked internal just to support unit tests
- /// </para>
- /// </remarks>
- /// <param name="mouseEvent"></param>
- /// <returns><see langword="true"/>, if the event was handled, <see langword="false"/> otherwise.</returns>
- private bool WhenGrabbedHandlePressed (MouseEventArgs mouseEvent)
- {
- mouseEvent.Handled = false;
- if (mouseEvent.IsPressed)
- {
- // The first time we get pressed event, grab the mouse and set focus
- if (App?.Mouse.MouseGrabView != this)
- {
- App?.Mouse.GrabMouse (this);
- if (!HasFocus && CanFocus)
- {
- // Set the focus, but don't invoke Accept
- SetFocus ();
- }
- mouseEvent.Handled = true;
- }
- if (Viewport.Contains (mouseEvent.Position))
- {
- //Logging.Debug ($"{Id} - Inside Viewport: {MouseState}");
- // The mouse is inside.
- if (HighlightStates.HasFlag (MouseState.Pressed))
- {
- MouseState |= MouseState.Pressed;
- }
- // Always clear PressedOutside when the mouse is pressed inside the Viewport
- MouseState &= ~MouseState.PressedOutside;
- }
- if (!Viewport.Contains (mouseEvent.Position))
- {
- // Logging.Debug ($"{Id} - Outside Viewport: {MouseState}");
- // The mouse is outside.
- // When WantContinuousButtonPressed is set we want to keep the mouse state as pressed (e.g. a repeating button).
- // This shows the user that the button is doing something, even if the mouse is outside the Viewport.
- if (HighlightStates.HasFlag (MouseState.PressedOutside) && !WantContinuousButtonPressed)
- {
- MouseState |= MouseState.PressedOutside;
- }
- }
- return mouseEvent.Handled = true;
- }
- return false;
- }
- /// <summary>
- /// INTERNAL For cases where the view is grabbed and the mouse is clicked, this method handles the click event
- /// (typically
- /// when <see cref="WantContinuousButtonPressed"/> or <see cref="HighlightStates"/> are set).
- /// </summary>
- /// <remarks>
- /// Marked internal just to support unit tests
- /// </remarks>
- /// <param name="mouseEvent"></param>
- /// <returns><see langword="true"/>, if the event was handled, <see langword="false"/> otherwise.</returns>
- internal bool WhenGrabbedHandleClicked (MouseEventArgs mouseEvent)
- {
- mouseEvent.Handled = false;
- if (App?.Mouse.MouseGrabView == this && mouseEvent.IsSingleClicked)
- {
- // We're grabbed. Clicked event comes after the last Release. This is our signal to ungrab
- App?.Mouse.UngrabMouse ();
- // TODO: Prove we need to unset MouseState.Pressed and MouseState.PressedOutside here
- // TODO: There may be perf gains if we don't unset these flags here
- MouseState &= ~MouseState.Pressed;
- MouseState &= ~MouseState.PressedOutside;
- // If mouse is still in bounds, generate a click
- if (!WantMousePositionReports && Viewport.Contains (mouseEvent.Position))
- {
- // By default, this will raise Selecting/OnSelecting - Subclasses can override this via AddCommand (Command.Select ...).
- mouseEvent.Handled = InvokeCommandsBoundToMouse (mouseEvent) == true;
- }
- return mouseEvent.Handled = true;
- }
- return false;
- }
- #endregion WhenGrabbed Handlers
- #region Mouse Wheel Events
- /// <summary>Raises the <see cref="OnMouseWheel"/>/<see cref="MouseWheel"/> event.</summary>
- /// <remarks>
- /// </remarks>
- /// <returns><see langword="true"/>, if the event was handled, <see langword="false"/> otherwise.</returns>
- protected bool RaiseMouseWheelEvent (MouseEventArgs args)
- {
- // Pre-conditions
- if (!Enabled)
- {
- // QUESTION: Is this right? Should a disabled view eat mouse?
- return args.Handled = false;
- }
- // Cancellable event
- if (OnMouseWheel (args) || args.Handled)
- {
- return args.Handled;
- }
- MouseWheel?.Invoke (this, args);
- if (args.Handled)
- {
- return true;
- }
- args.Handled = InvokeCommandsBoundToMouse (args) == true;
- return args.Handled;
- }
- /// <summary>
- /// Called when a mouse wheel event occurs. Check <see cref="MouseEventArgs.Flags"/> to see which wheel was moved was
- /// clicked.
- /// </summary>
- /// <remarks>
- /// </remarks>
- /// <param name="args"></param>
- /// <returns><see langword="true"/>, if the event was handled, <see langword="false"/> otherwise.</returns>
- protected virtual bool OnMouseWheel (MouseEventArgs args) { return false; }
- /// <summary>Raised when a mouse wheel event occurs.</summary>
- /// <remarks>
- /// </remarks>
- public event EventHandler<MouseEventArgs>? MouseWheel;
- #endregion Mouse Wheel Events
- #region MouseState Handling
- private MouseState _mouseState;
- /// <summary>
- /// Gets the state of the mouse relative to the View. When changed, the <see cref="MouseStateChanged"/>/
- /// <see cref="OnMouseStateChanged"/>
- /// event will be raised.
- /// </summary>
- public MouseState MouseState
- {
- get => _mouseState;
- internal set
- {
- if (_mouseState == value)
- {
- return;
- }
- EventArgs<MouseState> args = new (value);
- RaiseMouseStateChanged (args);
- _mouseState = value;
- }
- }
- /// <summary>
- /// Gets or sets which <see cref="MouseState"/> changes should cause the View to change its appearance.
- /// </summary>
- /// <remarks>
- /// <para>
- /// <see cref="MouseState.In"/> is set by default, which means the View will be highlighted when the
- /// mouse is over it. The default behavior of <see cref="SetAttributeForRole"/>
- /// is to use the <see cref="Drawing.VisualRole.Highlight"/> role for the highlight Attribute.
- /// </para>
- /// <para>
- /// <see cref="MouseState.Pressed"/> means the View will be highlighted when the mouse is pressed over it.
- /// <see cref="Border"/>'s default behavior is to use
- /// the <see cref="VisualRole.Highlight"/> role when the Border is pressed for Arrangement.
- /// <see cref="Margin"/>'s default behavior, when shadows are enabled, is to move the shadow providing
- /// a pressed effect.
- /// </para>
- /// <para>
- /// <see cref="MouseState.PressedOutside"/> means the View will be highlighted when the mouse was pressed
- /// inside it and then moved outside of it, unless <see cref="WantContinuousButtonPressed"/> is set to
- /// <see langword="true"/>, in which case the flag has no effect.
- /// </para>
- /// </remarks>
- public MouseState HighlightStates { get; set; }
- /// <summary>
- /// INTERNAL Raises the <see cref="MouseStateChanged"/> event.
- /// </summary>
- /// <param name="args"></param>
- private void RaiseMouseStateChanged (EventArgs<MouseState> args)
- {
- //Logging.Debug ($"{Id} - {args.Value} -> {args.Value}");
- OnMouseStateChanged (args);
- MouseStateChanged?.Invoke (this, args);
- }
- /// <summary>
- /// Called when <see cref="MouseState"/> has changed, indicating the View should be highlighted or not. The <see cref="MouseState"/> passed in the event
- /// indicates the highlight style that will be applied.
- /// </summary>
- protected virtual void OnMouseStateChanged (EventArgs<MouseState> args) { }
- /// <summary>
- /// RaisedCalled when <see cref="MouseState"/> has changed, indicating the View should be highlighted or not. The <see cref="MouseState"/> passed in the event
- /// indicates the highlight style that will be applied.
- /// </summary>
- public event EventHandler<EventArgs<MouseState>>? MouseStateChanged;
- #endregion MouseState Handling
- private void DisposeMouse () { }
- }
|