Skip to content

Commit

Permalink
Added Is<flag> helpers
Browse files Browse the repository at this point in the history
  • Loading branch information
tig committed Oct 15, 2024
1 parent 5590c28 commit 6a90328
Show file tree
Hide file tree
Showing 7 changed files with 83 additions and 68 deletions.
3 changes: 3 additions & 0 deletions Terminal.Gui/Application/Application.Mouse.cs
Original file line number Diff line number Diff line change
Expand Up @@ -251,6 +251,8 @@ internal static void RaiseMouseEvent (MouseEventArgs mouseEvent)
}
}


#pragma warning disable CS1574 // XML comment has cref attribute that could not be resolved
/// <summary>
/// Raised when a mouse event occurs. Can be cancelled by setting <see cref="MouseEventArgs.Handled"/> to <see langword="true"/>.
/// </summary>
Expand All @@ -269,6 +271,7 @@ internal static void RaiseMouseEvent (MouseEventArgs mouseEvent)
/// </para>
/// </remarks>
public static event EventHandler<MouseEventArgs>? MouseEvent;
#pragma warning restore CS1574 // XML comment has cref attribute that could not be resolved

internal static bool HandleMouseGrab (View? deepestViewUnderMouse, MouseEventArgs mouseEvent)
{
Expand Down
90 changes: 70 additions & 20 deletions Terminal.Gui/Input/MouseEventArgs.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@ namespace Terminal.Gui;

/// <summary>
/// Specifies the event arguments for <see cref="Terminal.Gui.MouseEventArgs"/>. This is a higher-level construct than
/// the wrapped <see cref="Terminal.Gui.MouseEventArgs"/> class and is used for the events defined on <see cref="View"/> and subclasses
/// the wrapped <see cref="Terminal.Gui.MouseEventArgs"/> class and is used for the events defined on
/// <see cref="View"/> and subclasses
/// of View (e.g. <see cref="View.MouseEnter"/> and <see cref="View.MouseClick"/>).
/// </summary>
public class MouseEventArgs : HandledEventArgs
Expand All @@ -23,27 +24,76 @@ public class MouseEventArgs : HandledEventArgs
/// <summary>The deepest View who's <see cref="View.Frame"/> contains <see cref="ScreenPosition"/>.</summary>
public View? View { get; set; }

/// <summary>The position of the mouse in <see cref="View"/>'s Viewport-relative coordinates. Only valid if <see cref="View"/> is set.</summary>
/// <summary>
/// The position of the mouse in <see cref="View"/>'s Viewport-relative coordinates. Only valid if <see cref="View"/>
/// is set.
/// </summary>
public Point Position { get; set; }

public bool IsButtonEvent
{
get
{
return Flags.HasFlag (MouseFlags.Button1Clicked)
|| Flags.HasFlag (MouseFlags.Button2Clicked)
|| Flags.HasFlag (MouseFlags.Button3Clicked)
|| Flags.HasFlag (MouseFlags.Button4Clicked)
|| Flags.HasFlag (MouseFlags.Button1DoubleClicked)
|| Flags.HasFlag (MouseFlags.Button2DoubleClicked)
|| Flags.HasFlag (MouseFlags.Button3DoubleClicked)
|| Flags.HasFlag (MouseFlags.Button4DoubleClicked)
|| Flags.HasFlag (MouseFlags.Button1TripleClicked)
|| Flags.HasFlag (MouseFlags.Button2TripleClicked)
|| Flags.HasFlag (MouseFlags.Button3TripleClicked)
|| Flags.HasFlag (MouseFlags.Button4TripleClicked);
}
}
/// <summary>
/// Gets whether <see cref="Flags"/> contains any of the button pressed related flags.
/// </summary>
public bool IsPressed => Flags.HasFlag (MouseFlags.Button1Pressed)
|| Flags.HasFlag (MouseFlags.Button2Pressed)
|| Flags.HasFlag (MouseFlags.Button3Pressed)
|| Flags.HasFlag (MouseFlags.Button4Pressed);

/// <summary>
/// Gets whether <see cref="Flags"/> contains any of the button released related flags.
/// </summary>
public bool IsReleased => Flags.HasFlag (MouseFlags.Button1Released)
|| Flags.HasFlag (MouseFlags.Button2Released)
|| Flags.HasFlag (MouseFlags.Button3Released)
|| Flags.HasFlag (MouseFlags.Button4Released);

/// <summary>
/// Gets whether <see cref="Flags"/> contains any of the single-clicked related flags.
/// </summary>
public bool IsSingleClicked => Flags.HasFlag (MouseFlags.Button1Clicked)
|| Flags.HasFlag (MouseFlags.Button2Clicked)
|| Flags.HasFlag (MouseFlags.Button3Clicked)
|| Flags.HasFlag (MouseFlags.Button4Clicked);

/// <summary>
/// Gets whether <see cref="Flags"/> contains any of the double-clicked related flags.
/// </summary>
public bool IsDoubleClicked => Flags.HasFlag (MouseFlags.Button1DoubleClicked)
|| Flags.HasFlag (MouseFlags.Button2DoubleClicked)
|| Flags.HasFlag (MouseFlags.Button3DoubleClicked)
|| Flags.HasFlag (MouseFlags.Button4DoubleClicked);

/// <summary>
/// Gets whether <see cref="Flags"/> contains any of the triple-clicked related flags.
/// </summary>
public bool IsTripleClicked => Flags.HasFlag (MouseFlags.Button1TripleClicked)
|| Flags.HasFlag (MouseFlags.Button2TripleClicked)
|| Flags.HasFlag (MouseFlags.Button3TripleClicked)
|| Flags.HasFlag (MouseFlags.Button4TripleClicked);

/// <summary>
/// Gets whether <see cref="Flags"/> contains any of the mouse button clicked related flags.
/// </summary>
public bool IsSingleDoubleOrTripleClicked =>
Flags.HasFlag (MouseFlags.Button1Clicked)
|| Flags.HasFlag (MouseFlags.Button2Clicked)
|| Flags.HasFlag (MouseFlags.Button3Clicked)
|| Flags.HasFlag (MouseFlags.Button4Clicked)
|| Flags.HasFlag (MouseFlags.Button1DoubleClicked)
|| Flags.HasFlag (MouseFlags.Button2DoubleClicked)
|| Flags.HasFlag (MouseFlags.Button3DoubleClicked)
|| Flags.HasFlag (MouseFlags.Button4DoubleClicked)
|| Flags.HasFlag (MouseFlags.Button1TripleClicked)
|| Flags.HasFlag (MouseFlags.Button2TripleClicked)
|| Flags.HasFlag (MouseFlags.Button3TripleClicked)
|| Flags.HasFlag (MouseFlags.Button4TripleClicked);

/// <summary>
/// Gets whether <see cref="Flags"/> contains any of the mouse wheel related flags.
/// </summary>
public bool IsWheel => Flags.HasFlag (MouseFlags.WheeledDown)
|| Flags.HasFlag (MouseFlags.WheeledUp)
|| Flags.HasFlag (MouseFlags.WheeledLeft)
|| Flags.HasFlag (MouseFlags.WheeledRight);

/// <summary>Returns a <see cref="T:System.String"/> that represents the current <see cref="Terminal.Gui.MouseEventArgs"/>.</summary>
/// <returns>A <see cref="T:System.String"/> that represents the current <see cref="Terminal.Gui.MouseEventArgs"/>.</returns>
Expand Down
25 changes: 3 additions & 22 deletions Terminal.Gui/View/View.Mouse.cs
Original file line number Diff line number Diff line change
Expand Up @@ -269,19 +269,7 @@ protected virtual void OnMouseLeave () { }
}
}

if (mouseEvent.Flags.HasFlag (MouseFlags.Button1Clicked)
|| mouseEvent.Flags.HasFlag (MouseFlags.Button2Clicked)
|| mouseEvent.Flags.HasFlag (MouseFlags.Button3Clicked)
|| mouseEvent.Flags.HasFlag (MouseFlags.Button4Clicked)
|| mouseEvent.Flags.HasFlag (MouseFlags.Button1DoubleClicked)
|| mouseEvent.Flags.HasFlag (MouseFlags.Button2DoubleClicked)
|| mouseEvent.Flags.HasFlag (MouseFlags.Button3DoubleClicked)
|| mouseEvent.Flags.HasFlag (MouseFlags.Button4DoubleClicked)
|| mouseEvent.Flags.HasFlag (MouseFlags.Button1TripleClicked)
|| mouseEvent.Flags.HasFlag (MouseFlags.Button2TripleClicked)
|| mouseEvent.Flags.HasFlag (MouseFlags.Button3TripleClicked)
|| mouseEvent.Flags.HasFlag (MouseFlags.Button4TripleClicked)
)
if (mouseEvent.IsSingleDoubleOrTripleClicked)
{
// If it's a click, and we didn't handle it, then we need to generate a click event
// We get here if the view did not handle the mouse event via OnMouseEvent/MouseEvent and
Expand Down Expand Up @@ -418,11 +406,7 @@ internal bool WhenGrabbedHandleClicked (MouseEventArgs mouseEvent)
{
mouseEvent.Handled = false;

if (Application.MouseGrabView == this
&& (mouseEvent.Flags.HasFlag (MouseFlags.Button1Clicked)
|| mouseEvent.Flags.HasFlag (MouseFlags.Button2Clicked)
|| mouseEvent.Flags.HasFlag (MouseFlags.Button3Clicked)
|| mouseEvent.Flags.HasFlag (MouseFlags.Button4Clicked)))
if (Application.MouseGrabView == this && mouseEvent.IsSingleClicked)
{
// We're grabbed. Clicked event comes after the last Release. This is our signal to ungrab
Application.UngrabMouse ();
Expand Down Expand Up @@ -457,10 +441,7 @@ internal bool WhenGrabbedHandleReleased (MouseEventArgs mouseEvent)
{
mouseEvent.Handled = false;

if (mouseEvent.Flags.HasFlag (MouseFlags.Button1Released)
|| mouseEvent.Flags.HasFlag (MouseFlags.Button2Released)
|| mouseEvent.Flags.HasFlag (MouseFlags.Button3Released)
|| mouseEvent.Flags.HasFlag (MouseFlags.Button4Released))
if (mouseEvent.IsReleased)
{
if (Application.MouseGrabView == this)
{
Expand Down
14 changes: 3 additions & 11 deletions Terminal.Gui/Views/TabView.cs
Original file line number Diff line number Diff line change
Expand Up @@ -573,11 +573,7 @@ protected override bool OnMouseEvent (MouseEventArgs me)
{
Tab hit = me.View is Tab ? (Tab)me.View : null;

bool isClick = me.Flags.HasFlag (MouseFlags.Button1Clicked)
|| me.Flags.HasFlag (MouseFlags.Button2Clicked)
|| me.Flags.HasFlag (MouseFlags.Button3Clicked);

if (isClick)
if (me.IsSingleClicked)
{
_host.OnTabClicked (new TabMouseEventArgs (hit, me));

Expand All @@ -588,9 +584,7 @@ protected override bool OnMouseEvent (MouseEventArgs me)
}
}

if (!me.Flags.HasFlag (MouseFlags.Button1Clicked)
&& !me.Flags.HasFlag (MouseFlags.Button1DoubleClicked)
&& !me.Flags.HasFlag (MouseFlags.Button1TripleClicked))
if (!me.IsSingleDoubleOrTripleClicked)
{
return false;
}
Expand All @@ -600,9 +594,7 @@ protected override bool OnMouseEvent (MouseEventArgs me)
SetFocus ();
}

if (me.Flags.HasFlag (MouseFlags.Button1Clicked)
|| me.Flags.HasFlag (MouseFlags.Button1DoubleClicked)
|| me.Flags.HasFlag (MouseFlags.Button1TripleClicked))
if (me.IsSingleDoubleOrTripleClicked)
{
var scrollIndicatorHit = 0;

Expand Down
3 changes: 1 addition & 2 deletions Terminal.Gui/Views/TextField.cs
Original file line number Diff line number Diff line change
Expand Up @@ -800,9 +800,8 @@ public virtual void KillWordForwards ()
/// <inheritdoc/>
protected override bool OnMouseEvent (MouseEventArgs ev)
{
if (!ev.Flags.HasFlag (MouseFlags.Button1Pressed)
if (ev is { IsPressed: false, IsReleased: false }
&& !ev.Flags.HasFlag (MouseFlags.ReportMousePosition)
&& !ev.Flags.HasFlag (MouseFlags.Button1Released)
&& !ev.Flags.HasFlag (MouseFlags.Button1DoubleClicked)
&& !ev.Flags.HasFlag (MouseFlags.Button1TripleClicked)
&& !ev.Flags.HasFlag (ContextMenu.MouseFlags))
Expand Down
8 changes: 1 addition & 7 deletions Terminal.Gui/Views/TextView.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3276,16 +3276,10 @@ public void Load (List<List<Cell>> cellsList)
/// <inheritdoc/>
protected override bool OnMouseEvent (MouseEventArgs ev)
{
if (!ev.Flags.HasFlag (MouseFlags.Button1Clicked)
&& !ev.Flags.HasFlag (MouseFlags.Button1Pressed)
if (ev is { IsSingleDoubleOrTripleClicked: false, IsPressed: false, IsReleased: false, IsWheel: false }
&& !ev.Flags.HasFlag (MouseFlags.Button1Pressed | MouseFlags.ReportMousePosition)
&& !ev.Flags.HasFlag (MouseFlags.Button1Released)
&& !ev.Flags.HasFlag (MouseFlags.Button1Pressed | MouseFlags.ButtonShift)
&& !ev.Flags.HasFlag (MouseFlags.WheeledDown)
&& !ev.Flags.HasFlag (MouseFlags.WheeledUp)
&& !ev.Flags.HasFlag (MouseFlags.Button1DoubleClicked)
&& !ev.Flags.HasFlag (MouseFlags.Button1DoubleClicked | MouseFlags.ButtonShift)
&& !ev.Flags.HasFlag (MouseFlags.Button1TripleClicked)
&& !ev.Flags.HasFlag (ContextMenu!.MouseFlags))
{
return false;
Expand Down
8 changes: 2 additions & 6 deletions Terminal.Gui/Views/TreeView/TreeView.cs
Original file line number Diff line number Diff line change
Expand Up @@ -993,12 +993,8 @@ public void GoToFirst ()
protected override bool OnMouseEvent (MouseEventArgs me)
{
// If it is not an event we care about
if (!me.Flags.HasFlag (MouseFlags.Button1Clicked)
&& !me.Flags.HasFlag (ObjectActivationButton ?? MouseFlags.Button1DoubleClicked)
&& !me.Flags.HasFlag (MouseFlags.WheeledDown)
&& !me.Flags.HasFlag (MouseFlags.WheeledUp)
&& !me.Flags.HasFlag (MouseFlags.WheeledRight)
&& !me.Flags.HasFlag (MouseFlags.WheeledLeft))
if (me is { IsSingleClicked: false, IsPressed: false, IsReleased: false, IsWheel: false }
&& !me.Flags.HasFlag (ObjectActivationButton ?? MouseFlags.Button1DoubleClicked))
{
// do nothing
return false;
Expand Down

0 comments on commit 6a90328

Please sign in to comment.