Skip to content
Merged
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
Original file line number Diff line number Diff line change
Expand Up @@ -1969,26 +1969,58 @@ void TrackerOnCollectionChanged(object sender, EventArgs eventArgs)
UpdateToolbarItems();
}

void UpdateSecondaryMenuOnly()
{
var menuButton = NavigationItem.RightBarButtonItems?.FirstOrDefault(
item => item.AccessibilityIdentifier == "SecondaryToolbarMenuButton");

if (menuButton is null)
return;

List<UIMenuElement> secondaries = null;

foreach (var item in _tracker.ToolbarItems)
{
if (item.Order == ToolbarItemOrder.Secondary)
(secondaries ??= []).Add(CreateSecondaryPlatformAction(item));
}

if (secondaries is not null)
menuButton.Menu = UIMenu.Create(string.Empty, null, UIMenuIdentifier.Edit, UIMenuOptions.DisplayInline, secondaries.ToArray());
}

void OnToolbarItemPropertyChanged(object sender, PropertyChangedEventArgs e)
{
// Only rebuild toolbar items if a relevant property changed
if (e.PropertyName == MenuItem.IsEnabledProperty.PropertyName ||
var item = sender as ToolbarItem;
var isRelevantProperty =
e.PropertyName == MenuItem.IsEnabledProperty.PropertyName ||
e.PropertyName == MenuItem.TextProperty.PropertyName ||
e.PropertyName == MenuItem.IconImageSourceProperty.PropertyName)
e.PropertyName == MenuItem.IconImageSourceProperty.PropertyName;

if (item is null || !isRelevantProperty)
return;

if (item.Order != ToolbarItemOrder.Secondary &&
e.PropertyName == MenuItem.IsEnabledProperty.PropertyName)
{
return;
}

// Throttle updates to prevent excessive rebuilding when multiple properties change rapidly
if (!_toolbarUpdatePending)
{
// Throttle updates to prevent excessive rebuilding when multiple properties change rapidly
if (!_toolbarUpdatePending)
_toolbarUpdatePending = true;
BeginInvokeOnMainThread(() =>
{
_toolbarUpdatePending = true;
BeginInvokeOnMainThread(() =>
_toolbarUpdatePending = false;
if (!_disposed)
{
_toolbarUpdatePending = false;
if (!_disposed)
{
if (item.Order == ToolbarItemOrder.Secondary)
UpdateSecondaryMenuOnly();
else
UpdateToolbarItems();
}
});
}
}
});
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,11 @@ public static UIBarButtonItem ToUIBarButtonItem(this ToolbarItem item, bool forc
}

internal static SecondarySubToolbarItem ToSecondarySubToolbarItem(this ToolbarItem item)
{
return new SecondarySubToolbarItem(item, CreateSecondaryPlatformAction(item));
}

internal static UIAction CreateSecondaryPlatformAction(ToolbarItem item)
{
var weakItem = new WeakReference<ToolbarItem>(item);

Expand All @@ -56,7 +61,12 @@ internal static SecondarySubToolbarItem ToSecondarySubToolbarItem(this ToolbarIt
});
}

return new SecondarySubToolbarItem(item, action);
action.UpdateIsEnabled(item.IsEnabled);

if (!string.IsNullOrEmpty(item.AutomationId))
action.AccessibilityIdentifier = item.AutomationId;

return action;
}

static UIImage ScaleImageToSystemDefaults(ImageSource imageSource, UIImage uIImage)
Expand Down
96 changes: 96 additions & 0 deletions src/Controls/tests/TestCases.HostApp/Issues/Issue39061.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
#if IOS || MACCATALYST
using Microsoft.Maui.Controls.Handlers.Compatibility;
using UIKit;
#endif

namespace Maui.Controls.Sample.Issues
{
[Issue(IssueTracker.Github, 39061, "ToolbarItem custom TintColor is lost when IsEnabled changes on iOS", PlatformAffected.iOS | PlatformAffected.macOS)]
public class Issue39061 : NavigationPage
{
public Issue39061() : base(new MainPage())
{
}

class MainPage : ContentPage
{
readonly ToolbarItem _saveItem;
readonly ToolbarItem _refreshItem;

public MainPage()
{
Title = "Issue 39061";

_saveItem = new ToolbarItem
{
Text = "Save",
Order = ToolbarItemOrder.Primary,
Priority = 0,
AutomationId = "SaveItem"
};

_refreshItem = new ToolbarItem
{
Text = "Refresh",
Order = ToolbarItemOrder.Primary,
Priority = 1,
AutomationId = "RefreshItem"
};

ToolbarItems.Add(_saveItem);
ToolbarItems.Add(_refreshItem);

var toggleButton = new Button
{
Text = "Simulate Dialog (Disable -> Enable)",
AutomationId = "ToggleIsEnabledButton"
};

toggleButton.Clicked += async (s, e) =>
{
_saveItem.IsEnabled = false;
_refreshItem.IsEnabled = false;

await DisplayAlertAsync("Popup", "Toolbar is disabled", "Dismiss");

_saveItem.IsEnabled = true;
_refreshItem.IsEnabled = true;
};

Content = new VerticalStackLayout
{
Padding = 20,
Spacing = 16,
Children =
{
new Label
{
Text = "Toggling IsEnabled should preserve custom native TintColor on toolbar items.",
AutomationId = "IssueDescriptionLabel"
},
toggleButton
}
};
}
}
}

#if IOS || MACCATALYST
public sealed class Issue39061NavigationRenderer : NavigationRenderer
{
public override void ViewDidAppear(bool animated)
{
base.ViewDidAppear(animated);

NavigationBar.TintColor = UIColor.FromRGB(0, 190, 180);

var rightBarButtonItems = VisibleViewController?.NavigationItem.RightBarButtonItems;
if (rightBarButtonItems is null)
return;

foreach (var item in rightBarButtonItems)
item.TintColor = UIColor.FromRGB(255, 45, 146);
}
}
#endif
}
1 change: 1 addition & 0 deletions src/Controls/tests/TestCases.HostApp/MauiProgram.cs
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ public static MauiApp CreateMauiApp()
#if IOS || MACCATALYST
handlers.AddHandler(typeof(Issue11132Control), typeof(Issue11132ControlHandler));
handlers.AddHandler(typeof(Issue36749Button), typeof(Issue36749ButtonHandler));
handlers.AddHandler(typeof(Issue39061), typeof(Issue39061NavigationRenderer));
#endif
#if IOS || MACCATALYST || ANDROID
handlers.AddHandler(typeof(UITestEditor), typeof(UITestEditorHandler));
Expand Down
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
#if IOS || MACCATALYST // Regression is specific to the iOS/Mac Catalyst compatibility NavigationRenderer and UIKit toolbar items.
using NUnit.Framework;
using UITest.Appium;
using UITest.Core;

namespace Microsoft.Maui.TestCases.Tests.Issues;

public class Issue39061 : _IssuesUITest
{
public Issue39061(TestDevice device) : base(device) { }

public override string Issue => "ToolbarItem custom TintColor is lost when IsEnabled changes on iOS";

[Test]
[Category(UITestCategories.ToolbarItem)]
public void ToolbarItemPreservesCustomTintColorOnStateToggle()
{
App.WaitForElement("ToggleIsEnabledButton");

App.Tap("ToggleIsEnabledButton");

App.TapDisplayAlertButton("Dismiss");

App.WaitForElement("SaveItem");

VerifyScreenshot(retryTimeout: TimeSpan.FromSeconds(2));
}
}
#endif
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading