Skip to content

Commit

Permalink
Fixes gui-cs#3342. Why ViewMouse doesn't handle the movable procedure…
Browse files Browse the repository at this point in the history
… instead of the Adornment?
  • Loading branch information
BDisp committed Mar 20, 2024
1 parent 6badd0d commit 8b9400c
Show file tree
Hide file tree
Showing 6 changed files with 72 additions and 37 deletions.
2 changes: 1 addition & 1 deletion Terminal.Gui/Application.cs
Original file line number Diff line number Diff line change
Expand Up @@ -179,7 +179,7 @@ internal static void ResetState ()
/// <see cref="ConsoleDriver"/> to use. If neither <paramref name="driver"/> or <paramref name="driverName"/> are
/// specified the default driver for the platform will be used.
/// </param>
public static void Init (ConsoleDriver driver = null, string driverName = null) { InternalInit (() => new Toplevel (), driver, driverName); }
public static void Init (ConsoleDriver driver = null, string driverName = null) { InternalInit (() => new Toplevel { Arrangement = ViewArrangement.Fixed }, driver, driverName); }

internal static bool _initialized;
internal static int _mainThreadId = -1;
Expand Down
20 changes: 18 additions & 2 deletions Terminal.Gui/View/Adornment/Adornment.cs
Original file line number Diff line number Diff line change
Expand Up @@ -259,7 +259,7 @@ protected internal override bool OnMouseEvent (MouseEvent mouseEvent)

if (!Parent.CanFocus || !Parent.Arrangement.HasFlag (ViewArrangement.Movable))
{
return true;
return false;
}

// BUGBUG: See https://github.com/gui-cs/Terminal.Gui/issues/3312
Expand All @@ -270,7 +270,8 @@ protected internal override bool OnMouseEvent (MouseEvent mouseEvent)

// Only start grabbing if the user clicks in the Thickness area
// Adornment.Contains takes Parent SuperView=relative coords.
if (Contains (mouseEvent.X + Parent.Frame.X + Frame.X, mouseEvent.Y+ Parent.Frame.Y + Frame.Y))
if (mouseEvent.View is Adornment && Contains (mouseEvent.X + Parent.Frame.X + Frame.X, mouseEvent.Y + Parent.Frame.Y + Frame.Y)
|| (mouseEvent.View == Parent && IsParentLocationOnTheBoundaries(Parent.Frame, mouseEvent.X + Parent.Frame.X + Parent.GetBoundsOffset().X, mouseEvent.Y + Parent.Frame.Y + Parent.GetBoundsOffset().Y)))
{
// Set the start grab point to the Frame coords
_startGrabPoint = new (mouseEvent.X + Frame.X, mouseEvent.Y + Frame.Y);
Expand Down Expand Up @@ -324,6 +325,21 @@ out _
return false;
}

private static bool IsParentLocationOnTheBoundaries (Rectangle rect, int x, int y)
{
if (x == rect.Left || x == rect.Right - 1)
{
return y >= rect.Top && y <= rect.Bottom;
}

if (y == rect.Top || y == rect.Bottom - 1)
{
return x >= rect.Left && x <= rect.Right;
}

return false;
}

/// <inheritdoc/>
protected internal override bool OnMouseLeave (MouseEvent mouseEvent)
{
Expand Down
49 changes: 24 additions & 25 deletions Terminal.Gui/View/Adornment/Padding.cs
Original file line number Diff line number Diff line change
Expand Up @@ -39,30 +39,29 @@ public override ColorScheme ColorScheme
}
}

/// <summary>Called when a mouse event occurs within the Padding.</summary>
/// <remarks>
/// <para>
/// The coordinates are relative to <see cref="View.Bounds"/>.
/// </para>
/// <para>
/// A mouse click on the Padding will cause the Parent to focus.
/// </para>
/// </remarks>
/// <param name="mouseEvent"></param>
/// <returns><see langword="true"/>, if the event was handled, <see langword="false"/> otherwise.</returns>
protected internal override bool OnMouseEvent (MouseEvent mouseEvent)
{
if (mouseEvent.Flags.HasFlag (MouseFlags.Button1Clicked))
{
if (Parent.CanFocus && !Parent.HasFocus)
{
Parent.SetFocus ();
Parent.SetNeedsDisplay ();
return true;
}
}

return false;
}
///// <summary>Called when a mouse event occurs within the Padding.</summary>
///// <remarks>
///// <para>
///// The coordinates are relative to <see cref="View.Bounds"/>.
///// </para>
///// <para>
///// A mouse click on the Padding will cause the Parent to focus.
///// </para>
///// </remarks>
///// <param name="mouseEvent"></param>
///// <returns><see langword="true"/>, if the event was handled, <see langword="false"/> otherwise.</returns>
//protected internal override bool OnMouseEvent (MouseEvent mouseEvent)
//{
// if (mouseEvent.Flags.HasFlag (MouseFlags.Button1Clicked))
// {
// if (Parent.CanFocus && !Parent.HasFocus)
// {
// Parent.SetFocus ();
// Parent.SetNeedsDisplay ();
// return true;
// }
// }

// return false;
//}
}
7 changes: 6 additions & 1 deletion Terminal.Gui/View/ViewMouse.cs
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,12 @@ protected internal virtual bool OnMouseEvent (MouseEvent mouseEvent)

MouseEvent?.Invoke (this, args);

return args.Handled == true;
if (args.Handled)
{
return true;
}

return Margin?.OnMouseEvent (mouseEvent) == true;
}

/// <summary>Invokes the MouseClick event.</summary>
Expand Down
10 changes: 6 additions & 4 deletions UICatalog/Scenarios/BackgroundWorkerCollection.cs
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@ public OverlappedMain ()

IsOverlappedContainer = true;

Arrangement = ViewArrangement.Fixed;

_workerApp = new WorkerApp { Visible = false };

_menu = new MenuBar
Expand Down Expand Up @@ -328,15 +330,15 @@ public WorkerApp ()

ColorScheme = Colors.ColorSchemes ["Base"];

var label = new Label { X = Pos.Center (), Y = 0, Text = "Worker collection Log" };
var label = new Label { X = Pos.Center (), Y = 1, Text = "Worker collection Log" };
Add (label);

_listLog = new ListView
{
X = 0,
X = 1,
Y = Pos.Bottom (label),
Width = Dim.Fill (),
Height = Dim.Fill (),
Width = Dim.Fill (1),
Height = Dim.Fill (1),
Source = new ListWrapper (_log)
};
Add (_listLog);
Expand Down
21 changes: 17 additions & 4 deletions UnitTests/View/MouseTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -37,20 +37,21 @@ public void MouseClick_SetsFocus_If_CanFocus (bool canFocus, bool setFocus, bool
// Test drag to move
[Theory]
[InlineData (0, 0, 0, 0, false)]
[InlineData (0, 0, 0, 4, false)]
[InlineData (0, 0, 0, 4, true)]
[InlineData (1, 0, 0, 4, true)]
[InlineData (0, 1, 0, 4, true)]
[InlineData (0, 0, 1, 4, false)]
[InlineData (0, 0, 1, 4, true)]

[InlineData (1, 1, 0, 3, false)]
[InlineData (1, 1, 0, 4, true)]
[InlineData (1, 1, 0, 5, true)]
[InlineData (1, 1, 0, 6, false)]


[InlineData (1, 1, 0, 11, false)]
[InlineData (1, 1, 0, 12, true)]
[InlineData (0, 0, 0, 13, true)]
[InlineData (1, 1, 0, 13, true)]
[InlineData (0, 0, 1, 13, true)]
[InlineData (1, 1, 0, 14, false)]
[AutoInitShutdown]
public void ButtonPressed_In_Margin_Or_Border_Starts_Drag (int marginThickness, int borderThickness, int paddingThickness, int xy, bool expectedMoved)
Expand All @@ -73,10 +74,22 @@ public void ButtonPressed_In_Margin_Or_Border_Starts_Drag (int marginThickness,
Assert.Equal (new Point (4, 4), testView.Frame.Location);
Application.OnMouseEvent (new (new () { X = xy, Y = xy, Flags = MouseFlags.Button1Pressed }));

Assert.False (Application.MouseGrabView is { } && (Application.MouseGrabView != testView.Margin && Application.MouseGrabView != testView.Border));
if (expectedMoved)
{
Assert.False (Application.MouseGrabView is { } && (Application.MouseGrabView != testView.Margin && Application.MouseGrabView != testView.Border && Application.MouseGrabView != testView.Padding));
}
else
{
Assert.Null (Application.MouseGrabView);
}

Application.OnMouseEvent (new (new () { X = xy + 1, Y = xy + 1, Flags = MouseFlags.Button1Pressed | MouseFlags.ReportMousePosition }));

Assert.Equal (expectedMoved, new Point (5, 5) == testView.Frame.Location);

if (!expectedMoved)
{
Assert.Equal (new Point (4, 4), testView.Frame.Location);
}
}
}

0 comments on commit 8b9400c

Please sign in to comment.