Skip to content

Commit

Permalink
Made sure single checkable items are not unchecked if the user tries …
Browse files Browse the repository at this point in the history
…to uncheck them

made sure button in context menu control is transparent
  • Loading branch information
haavamoa committed Jan 6, 2023
1 parent 8fa65e4 commit 783806e
Show file tree
Hide file tree
Showing 5 changed files with 51 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,11 @@ public bool OnMenuItemClick(IMenuItem theTappedNativeItem)
{
if (theTappedNativeItem.IsCheckable) //check the item
{
if (contextMenuItem.Parent is ContextMenuGroup && theTappedNativeItem.IsChecked) //You are unchecking a grouped item, which means its single mode and it should not be able to uncheck
{
return true;
}

m_menuItems.ForEach(pair =>
{
if (pair.Value.GroupId == theTappedNativeItem.GroupId) //Uncheck previous items
Expand Down
14 changes: 12 additions & 2 deletions src/DIPS.Xamarin.UI.Android/ContextMenu/ContextMenuHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ internal static Dictionary<ContextMenuItem, IMenuItem> CreateMenuItems(
var index = items.IndexOf(contextMenuItem);
if (contextMenuItem is ContextMenuGroup contextMenuGroup)
{
contextMenuGroup.Parent = contextMenuButton;
groupIndex += 1;

if (items.Count(i => i is ContextMenuGroup) >
Expand All @@ -40,6 +41,7 @@ internal static Dictionary<ContextMenuItem, IMenuItem> CreateMenuItems(
var contextMenuItemInGroupIndex = contextMenuGroup.ItemsSource.IndexOf(contextMenuItemInGroup);
var menuItem = groupMenu.Add(groupIndex, contextMenuItemInGroupIndex, Menu.None, contextMenuItemInGroup.Title);
TrySetChecked(contextMenuButton, menuItem, contextMenuItemInGroup);
contextMenuItemInGroup.Parent = contextMenuGroup;
dict.Add(contextMenuItemInGroup, menuItem);
}

Expand All @@ -53,7 +55,12 @@ internal static Dictionary<ContextMenuItem, IMenuItem> CreateMenuItems(
{
var newDict = CreateMenuItems(contextMenuGroup.ItemsSource, contextMenuButton, popupMenu,
groupIndex);
newDict.ForEach(pair => dict.Add(pair.Key, pair.Value));
newDict.ForEach(pair =>
{
var contextMenuItemInGroup = pair.Key;
contextMenuItemInGroup.Parent = contextMenuGroup;
dict.Add(contextMenuItemInGroup, pair.Value);
});
if (contextMenuGroup.IsCheckable)
{
popupMenu.Menu.SetGroupCheckable(groupIndex, contextMenuGroup.IsCheckable, false);
Expand All @@ -64,6 +71,10 @@ internal static Dictionary<ContextMenuItem, IMenuItem> CreateMenuItems(
{
var menuItem = popupMenu.Menu.Add(groupIndex, index, Menu.None, contextMenuItem.Title);
TrySetChecked(contextMenuButton, menuItem, contextMenuItem);
if (groupIndex == 0) //Not in a group
{
contextMenuItem.Parent = contextMenuButton;
}
dict.Add(contextMenuItem, menuItem);
}
}
Expand All @@ -81,7 +92,6 @@ private static void TrySetChecked(ContextMenuButton contextMenuButton, IMenuItem
{
contextMenuButton.ResetIsCheckedForTheRest(contextMenuItem);
}

menuItem?.SetChecked(contextMenuItem.IsChecked);
}
}
Expand Down
35 changes: 28 additions & 7 deletions src/DIPS.Xamarin.UI.iOS/ContextMenu/ContextMenuHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ internal static Dictionary<ContextMenuItem, UIMenuElement> CreateMenuItems(
UIMenuElement uiMenuElement;
if (contextMenuItem is ContextMenuGroup contextMenuGroup) //Recursively add menu items from a group
{
contextMenuGroup.Parent = contextMenuButton;
//Inherit isCheckable context menu group group to all menu items in the group
contextMenuGroup.ItemsSource.ForEach(c => c.IsCheckable = contextMenuGroup.IsCheckable);

Expand All @@ -33,15 +34,19 @@ internal static Dictionary<ContextMenuItem, UIMenuElement> CreateMenuItems(
}
else //Only one group, add this to the root of the menu so the user does not have to tap an extra time to get to the items.
{
newDict.ForEach(newD => dict.Add(newD.Key, newD.Value));
newDict.ForEach(newD =>
{
dict.Add(newD.Key, newD.Value);
});
continue;
}
}

else
{
var uiAction = UIAction.Create(contextMenuItem.Title, null, null, uiAction => OnMenuItemClick(uiAction, contextMenuItem, contextMenuButton));

var uiAction = UIAction.Create(contextMenuItem.Title, null, null,
uiAction => OnMenuItemClick(uiAction, contextMenuItem, contextMenuButton));

if (contextMenuItem.IsChecked)
{
contextMenuButton.ResetIsCheckedForTheRest(contextMenuItem);
Expand All @@ -50,6 +55,15 @@ internal static Dictionary<ContextMenuItem, UIMenuElement> CreateMenuItems(
SetCorrectUiActionState(contextMenuItem, uiAction); //Setting the correct check mark if it can

uiMenuElement = uiAction;

if (menuGroup != null)
{
contextMenuItem.Parent = menuGroup;
}
else
{
contextMenuItem.Parent = contextMenuButton;
}
}

dict.Add(contextMenuItem, uiMenuElement);
Expand All @@ -58,13 +72,20 @@ internal static Dictionary<ContextMenuItem, UIMenuElement> CreateMenuItems(
return dict;
}

private static void OnMenuItemClick(UIAction action, ContextMenuItem contextMenuItem, ContextMenuButton contextMenuButton)
private static void OnMenuItemClick(UIAction action, ContextMenuItem contextMenuItem,
ContextMenuButton contextMenuButton)
{

if (contextMenuItem.IsCheckable)
{
contextMenuButton.ResetIsCheckedForTheRest(contextMenuItem);
if (contextMenuItem.Parent is not ContextMenuGroup || !contextMenuItem.IsChecked)
{ //Only check if if unchecked if its a part of a group
contextMenuButton.ResetIsCheckedForTheRest(contextMenuItem);

contextMenuItem.IsChecked = !contextMenuItem.IsChecked; //Can not change the visuals when the menu is showing as the items are immutable when they are showing
contextMenuItem.IsChecked =
!contextMenuItem
.IsChecked; //Can not change the visuals when the menu is showing as the items are immutable when they are showing
}
}

contextMenuItem.SendClicked(contextMenuButton);
Expand All @@ -74,7 +95,7 @@ private static void SetCorrectUiActionState(ContextMenuItem contextMenuItem, UIA
{
if (contextMenuItem.IsCheckable)
{
uiAction.State = contextMenuItem.IsChecked ? UIMenuElementState.On : UIMenuElementState.Off;
uiAction.State = contextMenuItem.IsChecked ? UIMenuElementState.On : UIMenuElementState.Off;
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ public ContextMenuControl()
new Binding(nameof(ItemClickedCommand), source: this));
m_contextMenuButton.ContextMenuOpened += ContextMenuButton_OnContextMenuOpened;
m_contextMenuButton.ItemClicked += ContextMenuButton_OnItemClicked;
m_contextMenuButton.BackgroundColor = Color.Transparent;

m_theContentView = new ContentView();
m_theContentView.SetBinding(ContentProperty, new Binding(nameof(TheContent), source: this));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,5 +59,10 @@ public object CommandParameter
/// Determines if the item should be default checked when its opened for the first time
/// </summary>
public bool IsChecked { get; set; }

/// <summary>
/// The parent of the context menu item
/// </summary>
public object Parent { get; internal set; }
}
}

0 comments on commit 783806e

Please sign in to comment.