Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixes #3342. Why ViewMouse doesn't handle the movable procedure instead of the Adornment? #3343

Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 @@
/// <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 Expand Up @@ -1182,7 +1182,7 @@
&& top != OverlappedTop
&& top != Current
&& Current?.Running == false
&& !top.Running)

Check warning on line 1185 in Terminal.Gui/Application.cs

View workflow job for this annotation

GitHub Actions / build_and_test

Dereference of a possibly null reference.
{
lock (_topLevels)
{
Expand Down Expand Up @@ -1381,7 +1381,7 @@
/// </para>
/// <para>The <see cref="MouseEvent.View"/> will contain the <see cref="View"/> that contains the mouse coordinates.</para>
/// </remarks>
public static event EventHandler<MouseEventEventArgs> MouseEvent;

Check warning on line 1384 in Terminal.Gui/Application.cs

View workflow job for this annotation

GitHub Actions / build_and_test

Non-nullable event 'MouseEvent' must contain a non-null value when exiting constructor. Consider declaring the event as nullable.

/// <summary>Called when a mouse event occurs. Raises the <see cref="MouseEvent"/> event.</summary>
/// <remarks>This method can be used to simulate a mouse event, e.g. in unit tests.</remarks>
Expand Down Expand Up @@ -1463,7 +1463,7 @@

if (view is { } && view != OverlappedTop && top != Current)
{
MoveCurrent ((Toplevel)top);

Check warning on line 1466 in Terminal.Gui/Application.cs

View workflow job for this annotation

GitHub Actions / build_and_test

Converting null literal or possible null value to non-nullable type.
}
}
}
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 @@ -8,7 +8,7 @@
/// <remarsk>
/// <para>
/// Each of <see cref="Margin"/>, <see cref="Border"/>, and <see cref="Padding"/> has slightly different
/// behavior relative to <see cref="ColorScheme"/>, <see cref="View.SetFocus"/>, keyboard input, and

Check warning on line 11 in Terminal.Gui/View/Adornment/Adornment.cs

View workflow job for this annotation

GitHub Actions / build_and_test

Ambiguous reference in cref attribute: 'View.SetFocus'. Assuming 'View.SetFocus(View)', but could have also matched other overloads including 'View.SetFocus()'.
/// mouse input. Each can be customized by manipulating their Subviews.
/// </para>
/// </remarsk>
Expand Down Expand Up @@ -259,7 +259,7 @@

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 @@

// 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 @@
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);
}
}
}
Loading