Skip to content
Draft
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
27 changes: 27 additions & 0 deletions src/Controls/src/Core/NavigationPage/NavigationPageToolbar.cs
Original file line number Diff line number Diff line change
Expand Up @@ -34,13 +34,33 @@ public NavigationPageToolbar(Maui.IElement parent, Page rootPage) : base(parent)
RootPage = rootPage;
_toolbarTracker.PageAppearing += OnPageAppearing;
_toolbarTracker.Target = RootPage;

#if ANDROID || WINDOWS
// Subscribe to orientation changes to update FlyoutPage toolbar button visibility
// Android/Windows need manual orientation detection, iOS/Mac handle this automatically
if (parent is FlyoutPage)
{
Microsoft.Maui.Devices.DeviceDisplay.MainDisplayInfoChanged += OnOrientationChanged;
}
#endif
}

void OnToolbarItemsChanged(object sender, EventArgs e)
{
ToolbarItems = _toolbarTracker.ToolbarItems;
}

#if ANDROID || WINDOWS
void OnOrientationChanged(object sender, Microsoft.Maui.Devices.DisplayInfoChangedEventArgs e)
{
// Re-evaluate toolbar button visibility when orientation changes
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Where is re-evaluated on iOS? Why is not required in that case?

if (_currentNavigationPage is not null)
{
ApplyChanges(_currentNavigationPage);
}
}
#endif

void OnPagePropertyChanged(object sender, System.ComponentModel.PropertyChangedEventArgs e)
{
if (e.IsOneOf(NavigationPage.HasNavigationBarProperty,
Expand Down Expand Up @@ -328,6 +348,13 @@ internal void Disconnect()
navPage.ChildRemoved -= NavigationPageChildrenChanged;
}
}

#if ANDROID || WINDOWS
if (Parent is FlyoutPage)
{
Microsoft.Maui.Devices.DeviceDisplay.MainDisplayInfoChanged -= OnOrientationChanged;
}
#endif
}
}
}
55 changes: 55 additions & 0 deletions src/Controls/tests/TestCases.HostApp/Issues/Issue24468.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
namespace Maui.Controls.Sample.Issues;

[Issue(IssueTracker.Github, 24468, "FlyoutPage toolbar button not updating on orientation change on Android",
PlatformAffected.Android)]
public class Issue24468 : TestFlyoutPage
{
private Label _statusLabel;
private int _callCount = 0;

protected override void Init()
{
Title = "Issue 24468";

_statusLabel = new Label
{
Text = "ShouldShowToolbarButton is not called",
AutomationId = "StatusLabel"
};

Flyout = new ContentPage
{
Title = "Menu",
Content = new Label { Text = "Flyout Menu" }
};

Detail = new NavigationPage(new ContentPage
{
Title = "Detail",
Content = new StackLayout
{
Children =
{
new Label { Text = "Rotate device to test toolbar button updates" },
_statusLabel
}
},
AutomationId = "ContentPage"
});

FlyoutLayoutBehavior = FlyoutLayoutBehavior.SplitOnLandscape;
}

public override bool ShouldShowToolbarButton()
{
_callCount++;
var shouldShow = base.ShouldShowToolbarButton();

if (_callCount > 1)
{
_statusLabel.Text = "ShouldShowToolbarButton is called";
}

return shouldShow;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
#if ANDROID || IOS //The test fails on Windows and MacCatalyst because the SetOrientation method, which is intended to change the device orientation, is only supported on mobile platforms iOS and Android.
using UITest.Core;

namespace Microsoft.Maui.TestCases.Tests.Issues;
public class Issue24468 : _IssuesUITest
{
public Issue24468(TestDevice device) : base(device)
{
}

public override string Issue => "FlyoutPage toolbar button not updating on orientation change on Android";
[Test]
[Category(UITestCategories.Navigation)]
public void FlyoutPageToolbarButtonUpdatesOnOrientationChange()
{
App.WaitForElement("ContentPage");

App.SetOrientationLandscape();

var text = App.FindElement("StatusLabel").GetText();
Assert.That(text, Contains.Substring("ShouldShowToolbarButton is called"));
App.SetOrientationPortrait();
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can use try-finally to always restore the state even if there is any error.

}
}
#endif