Merge pull request #191 from irihitech/issue/184
Make NavMenu work on touch screen.
This commit is contained in:
@@ -31,6 +31,9 @@ public class NavMenuItem: HeaderedItemsControl
|
|||||||
private Popup? _popup;
|
private Popup? _popup;
|
||||||
private Panel? _overflowPanel;
|
private Panel? _overflowPanel;
|
||||||
|
|
||||||
|
private static readonly Point s_invalidPoint = new (double.NaN, double.NaN);
|
||||||
|
private Point _pointerDownPoint = s_invalidPoint;
|
||||||
|
|
||||||
public static readonly StyledProperty<object?> IconProperty = AvaloniaProperty.Register<NavMenuItem, object?>(
|
public static readonly StyledProperty<object?> IconProperty = AvaloniaProperty.Register<NavMenuItem, object?>(
|
||||||
nameof(Icon));
|
nameof(Icon));
|
||||||
|
|
||||||
@@ -224,20 +227,29 @@ public class NavMenuItem: HeaderedItemsControl
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
base.OnPointerPressed(e);
|
base.OnPointerPressed(e);
|
||||||
if (this.ItemCount == 0)
|
if (e.Handled) return;
|
||||||
|
|
||||||
|
var p = e.GetCurrentPoint(this);
|
||||||
|
if (p.Properties.PointerUpdateKind is not (PointerUpdateKind.LeftButtonPressed
|
||||||
|
or PointerUpdateKind.RightButtonPressed)) return;
|
||||||
|
if (p.Pointer.Type == PointerType.Mouse)
|
||||||
{
|
{
|
||||||
SelectItem(this);
|
if (this.ItemCount == 0)
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
if (!IsHorizontalCollapsed)
|
|
||||||
{
|
{
|
||||||
SetCurrentValue(IsVerticalCollapsedProperty, !IsVerticalCollapsed);
|
SelectItem(this);
|
||||||
|
Command?.Execute(CommandParameter);
|
||||||
|
e.Handled = true;
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
if (_popup is not null)
|
if (!IsHorizontalCollapsed)
|
||||||
{
|
{
|
||||||
|
SetCurrentValue(IsVerticalCollapsedProperty, !IsVerticalCollapsed);
|
||||||
|
e.Handled = true;
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
if (_popup is null || e.Source is not Visual v || _popup.IsInsidePopup(v)) return;
|
||||||
if (_popup.IsOpen)
|
if (_popup.IsOpen)
|
||||||
{
|
{
|
||||||
_popup.Close();
|
_popup.Close();
|
||||||
@@ -249,10 +261,49 @@ public class NavMenuItem: HeaderedItemsControl
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
Command?.Execute(CommandParameter);
|
else
|
||||||
e.Handled = true;
|
{
|
||||||
|
_pointerDownPoint = p.Position;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
protected override void OnPointerReleased(PointerReleasedEventArgs e)
|
||||||
|
{
|
||||||
|
base.OnPointerReleased(e);
|
||||||
|
if (!e.Handled && !double.IsNaN(_pointerDownPoint.X) &&
|
||||||
|
e.InitialPressMouseButton is MouseButton.Left or MouseButton.Right)
|
||||||
|
{
|
||||||
|
var point = e.GetCurrentPoint(this);
|
||||||
|
if (!new Rect(Bounds.Size).ContainsExclusive(point.Position) || e.Pointer.Type != PointerType.Touch) return;
|
||||||
|
if (this.ItemCount == 0)
|
||||||
|
{
|
||||||
|
SelectItem(this);
|
||||||
|
Command?.Execute(CommandParameter);
|
||||||
|
e.Handled = true;
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
if (!IsHorizontalCollapsed)
|
||||||
|
{
|
||||||
|
SetCurrentValue(IsVerticalCollapsedProperty, !IsVerticalCollapsed);
|
||||||
|
e.Handled = true;
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
if (_popup is null || e.Source is not Visual v || _popup.IsInsidePopup(v)) return;
|
||||||
|
if (_popup.IsOpen)
|
||||||
|
{
|
||||||
|
_popup.Close();
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
_popup.Open();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
internal void SelectItem(NavMenuItem item)
|
internal void SelectItem(NavMenuItem item)
|
||||||
{
|
{
|
||||||
if (item == this)
|
if (item == this)
|
||||||
@@ -281,6 +332,10 @@ public class NavMenuItem: HeaderedItemsControl
|
|||||||
{
|
{
|
||||||
menu.SelectItem(item, this);
|
menu.SelectItem(item, this);
|
||||||
}
|
}
|
||||||
|
if(_popup is not null)
|
||||||
|
{
|
||||||
|
_popup.Close();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
internal void ClearSelection()
|
internal void ClearSelection()
|
||||||
|
|||||||
Reference in New Issue
Block a user