diff --git a/src/Droid/ContextMenuContainerRenderer.cs b/src/Droid/ContextMenuContainerRenderer.cs index f857025..f0f7418 100644 --- a/src/Droid/ContextMenuContainerRenderer.cs +++ b/src/Droid/ContextMenuContainerRenderer.cs @@ -15,7 +15,7 @@ using Java.Lang.Reflect; using AColor = Android.Graphics.Color; using Path = System.IO.Path; -using DrawableWrapperX = Android.Graphics.Drawables.InsetDrawable; +using DrawableWrapperX = Android.Graphics.Drawables.InsetDrawable; using Android.Runtime; using Android.Util; using Microsoft.Maui.Dispatching; @@ -161,7 +161,7 @@ public void SetupMenu(ContextMenuContainer? container) DeconstructInteraction(); Element = container; } - + #pragma warning disable SA1201 private PopupMenu? _contextMenu; #pragma warning restore SA1201 @@ -169,7 +169,7 @@ public void SetupMenu(ContextMenuContainer? container) private bool _timerFired; // ReSharper disable once RedundantTypeCheckInPattern - private bool ContextMenuIsNotEmpty => Element is ContextMenuContainer {MenuItems.Count: > 0}; + private bool ContextMenuIsNotEmpty => Element is ContextMenuContainer { MenuItems.Count: > 0 }; public override bool DispatchTouchEvent(MotionEvent? e) { @@ -184,8 +184,9 @@ public override bool DispatchTouchEvent(MotionEvent? e) { // You can change the timespan of the long press _timerFired = false; + var delayMs = Element?.LongPressDelay ?? 1500; _timer = new MyTimer( - TimeSpan.FromMilliseconds(1500), + TimeSpan.FromMilliseconds(delayMs), () => { _timerFired = true; @@ -289,7 +290,7 @@ private void AddMenuItem(ContextMenuItem item) } contextAction.SetEnabled(item.IsEnabled); - + if (item.Icon != null) { @@ -302,7 +303,7 @@ private void AddMenuItem(ContextMenuItem item) Drawable? drawable = Context?.GetDrawable(id); if (drawable != null) { - var wrapper = new DrawableWrapperX(drawable,0); + var wrapper = new DrawableWrapperX(drawable, 0); if (item.IsDestructive) { wrapper.SetTint(AColor.Red); @@ -316,7 +317,7 @@ private void AddMenuItem(ContextMenuItem item) private void FillMenuItems() { // ReSharper disable once RedundantTypeCheckInPattern - if (Element is ContextMenuContainer {MenuItems.Count: > 0} element) + if (Element is ContextMenuContainer { MenuItems.Count: > 0 } element) { foreach (var item in element.MenuItems) { @@ -335,7 +336,7 @@ private void FillMenuItems() if (NeedToRefillMenu || element.MenuItems?.Count != _contextMenu.Menu.Size()) { DeconstructNativeMenu(); - + } } @@ -349,9 +350,9 @@ private void ContextMenu_MenuItemClick(object? sender, PopupMenu.MenuItemClickEv x => x.Text == e.Item.TitleFormatted?.ToString()); item?.OnItemTapped(); } - + public bool NeedToRefillMenu { get; set; } = false; - + } @@ -373,7 +374,7 @@ public MyTimer(TimeSpan timespan, Action callback) public void Start() { CancellationTokenSource cts = _cancellation; // safe copy - DispatcherProvider.Current.GetForCurrentThread() !.StartTimer( + DispatcherProvider.Current.GetForCurrentThread()!.StartTimer( #pragma warning disable SA1114 interval: _timespan, diff --git a/src/Shared/ContextMenuContainer.cs b/src/Shared/ContextMenuContainer.cs index dbd7c04..a368392 100644 --- a/src/Shared/ContextMenuContainer.cs +++ b/src/Shared/ContextMenuContainer.cs @@ -1,6 +1,7 @@ // MIT License // Copyright (c) 2021 Pavel Anpin +using System; using System.Collections.Generic; using Microsoft.Maui.Controls; @@ -22,6 +23,21 @@ public ContextMenuItems? MenuItems set => SetValue(MenuItemsProperty, value); } + // delay(ms) + public static readonly BindableProperty LongPressDelayProperty = + BindableProperty.Create( + nameof(LongPressDelay), + typeof(int), + typeof(ContextMenuContainer), + 1500, + validateValue: (_, v) => v is int i && i >= 0); + + public int LongPressDelay + { + get => (int)GetValue(LongPressDelayProperty); + set => SetValue(LongPressDelayProperty, value); + } + protected override void OnBindingContextChanged() { base.OnBindingContextChanged();