-
Notifications
You must be signed in to change notification settings - Fork 2k
[Net11] [iOS/MacCatalyst] Migrate FlyoutPage to handler architecture #36676
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
8829ed5
Implemented the iOS FlyoutViewHandler.
Vignesh-SF3580 05ce88f
Updated changes.
Vignesh-SF3580 d3ed6d6
Resolved UI test failures.
Vignesh-SF3580 303cad2
Moved the PR-34831 RTL fix to the handler.
Vignesh-SF3580 f45e462
Update FlyoutContainerManager.cs
Vignesh-SF3580 812ae59
Update FlyoutContainerManager.cs
Vignesh-SF3580 526ddf9
Addressed concerns.
Vignesh-SF3580 06b5f09
Addressed AI summary.
Vignesh-SF3580 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,267 @@ | ||
| using System; | ||
| using System.ComponentModel; | ||
| using System.Linq; | ||
| using Microsoft.Maui.Graphics; | ||
| using Microsoft.Maui.Graphics.Platform; | ||
| using Microsoft.Maui.Handlers; | ||
| using Microsoft.Maui.Platform; | ||
| using UIKit; | ||
|
|
||
| namespace Microsoft.Maui.Controls | ||
| { | ||
| public partial class FlyoutPage | ||
| { | ||
| // Track the flyout page this specific FlyoutPage instance is subscribed to | ||
| // for icon/title property changes. Instance-scoped (not static) so multiple | ||
| // FlyoutPage instances (multi-window, modal FlyoutPage, etc.) don't clobber | ||
| // each other's subscriptions. | ||
| WeakReference<Page>? _subscribedFlyout; | ||
|
|
||
| // Cached delegate instance so we can unsubscribe the exact same handler | ||
| // we subscribed with. | ||
| PropertyChangedEventHandler? _flyoutPropertyChangedHandler; | ||
|
|
||
|
|
||
| internal static void OnPresentedChangedByGesture(IFlyoutView view, bool isPresented) | ||
| { | ||
| if (view is FlyoutPage fp) | ||
| { | ||
| // Guard: during rotation, ShouldShowSplitMode may still return true while | ||
| // orientation hasn't settled. Writing false triggers InvalidOperationException | ||
| // in OnIsPresentedPropertyChanging validation. | ||
| if (!isPresented && ((IFlyoutPageController)fp).ShouldShowSplitMode) | ||
| { | ||
| return; | ||
| } | ||
|
|
||
| fp.IsPresented = isPresented; | ||
| } | ||
| else | ||
| { | ||
| view.IsPresented = isPresented; | ||
| } | ||
| } | ||
|
|
||
| internal static void OnLayoutBoundsChanged(IFlyoutView view, Rect flyoutBounds, Rect detailBounds) | ||
| { | ||
| if (view is IFlyoutPageController controller) | ||
| { | ||
| controller.FlyoutBounds = flyoutBounds; | ||
| controller.DetailBounds = detailBounds; | ||
| } | ||
| } | ||
|
|
||
| internal static void OnLeftBarButtonNeedsUpdate(IFlyoutView view) | ||
| { | ||
| if (view is not FlyoutPage fp) | ||
| { | ||
| return; | ||
| } | ||
|
|
||
| fp.SubscribeToFlyoutPropertyChanges(); | ||
|
|
||
| if (fp.Detail?.Handler is not IPlatformViewHandler detailHandler) | ||
| { | ||
| return; | ||
| } | ||
|
|
||
| var detailVC = detailHandler.ViewController; | ||
| if (detailVC is null) | ||
| { | ||
| return; | ||
| } | ||
|
|
||
| // If detail VC is a UINavigationController, use its root VC | ||
| var targetVC = detailVC is UINavigationController nav | ||
| ? nav.ViewControllers?.FirstOrDefault() ?? detailVC | ||
| : detailVC; | ||
|
|
||
| UpdateFlyoutLeftBarButton(targetVC, fp); | ||
| } | ||
|
|
||
| /// <summary> | ||
| /// Called when this FlyoutPage's handler is disconnected, so its flyout | ||
| /// icon/title subscription doesn't outlive the handler. | ||
| /// </summary> | ||
| internal static void OnHandlerDisconnected(IFlyoutView view) | ||
| { | ||
| if (view is FlyoutPage fp) | ||
| { | ||
| fp.UnsubscribeFlyoutPropertyChanges(); | ||
| } | ||
| } | ||
|
|
||
| void SubscribeToFlyoutPropertyChanges() | ||
| { | ||
| var flyout = Flyout; | ||
| if (flyout is null) | ||
| { | ||
| return; | ||
| } | ||
|
|
||
| // Unsubscribe from this instance's previous flyout if it changed | ||
| if (_subscribedFlyout is not null && _subscribedFlyout.TryGetTarget(out var oldFlyout)) | ||
| { | ||
| if (ReferenceEquals(oldFlyout, flyout)) | ||
| { | ||
| return; // Already subscribed to this flyout | ||
| } | ||
|
|
||
| if (_flyoutPropertyChangedHandler is not null) | ||
| { | ||
| oldFlyout.PropertyChanged -= _flyoutPropertyChangedHandler; | ||
| } | ||
| } | ||
|
|
||
| _flyoutPropertyChangedHandler = OnFlyoutPagePropertyChanged; | ||
| flyout.PropertyChanged += _flyoutPropertyChangedHandler; | ||
| _subscribedFlyout = new WeakReference<Page>(flyout); | ||
| } | ||
|
|
||
| /// <summary> | ||
| /// Unsubscribes from the currently-tracked flyout's property changes. | ||
| /// Called when this FlyoutPage's handler is disconnected. | ||
| /// </summary> | ||
| void UnsubscribeFlyoutPropertyChanges() | ||
| { | ||
| if (_subscribedFlyout is not null && | ||
| _subscribedFlyout.TryGetTarget(out var flyout) && | ||
| _flyoutPropertyChangedHandler is not null) | ||
| { | ||
| flyout.PropertyChanged -= _flyoutPropertyChangedHandler; | ||
| } | ||
|
|
||
| _flyoutPropertyChangedHandler = null; | ||
| _subscribedFlyout = null; | ||
| } | ||
|
|
||
| void OnFlyoutPagePropertyChanged(object? sender, PropertyChangedEventArgs e) | ||
| { | ||
| if (e.PropertyName == Page.IconImageSourceProperty.PropertyName || | ||
| e.PropertyName == Page.TitleProperty.PropertyName) | ||
| { | ||
| if (sender is Page flyoutPage && flyoutPage.Parent is FlyoutPage fp) | ||
| { | ||
| OnLeftBarButtonNeedsUpdate(fp); | ||
| } | ||
| } | ||
| } | ||
|
|
||
| static void UpdateFlyoutLeftBarButton(UIViewController targetVC, FlyoutPage flyoutPage) | ||
| { | ||
| if (!flyoutPage.ShouldShowToolbarButton()) | ||
| { | ||
| targetVC.NavigationItem.LeftBarButtonItem = null; | ||
| return; | ||
| } | ||
|
|
||
| var mauiContext = flyoutPage.FindMauiContext(); | ||
| if (mauiContext is null) | ||
| { | ||
| return; | ||
| } | ||
|
|
||
| // Weak reference prevents a pending async callback from keeping | ||
| // the page alive after the handler is disconnected (memory leak fix). | ||
| var weakPage = new WeakReference<FlyoutPage>(flyoutPage); | ||
|
|
||
| EventHandler onItemTapped = (sender, e) => | ||
| { | ||
| if (weakPage.TryGetTarget(out var p)) | ||
| { | ||
| p.IsPresented = !p.IsPresented; | ||
| } | ||
| }; | ||
|
|
||
| flyoutPage.Flyout.IconImageSource.LoadImage(mauiContext, result => | ||
| { | ||
| if (!weakPage.TryGetTarget(out var fp)) | ||
| { | ||
| return; | ||
|
Vignesh-SF3580 marked this conversation as resolved.
|
||
| } | ||
|
|
||
| var icon = result?.Value; | ||
|
|
||
| if (icon is not null) | ||
| { | ||
| // Scale icon to fit nav bar (max 44pt height) | ||
| var originalSize = icon.Size; | ||
| if (originalSize.Height > 44) | ||
| { | ||
| if (fp.Flyout.IconImageSource is not FontImageSource fontImageSource || | ||
| !fontImageSource.IsSet(FontImageSource.SizeProperty)) | ||
| { | ||
| icon = icon.ResizeImageSource(originalSize.Width, 44f, originalSize); | ||
| } | ||
| } | ||
|
|
||
| try | ||
| { | ||
| targetVC.NavigationItem.LeftBarButtonItem = | ||
| new UIBarButtonItem(icon, UIBarButtonItemStyle.Plain, onItemTapped); | ||
| } | ||
| catch (Exception) | ||
| { | ||
| // UIBarButtonItem creation can throw | ||
| } | ||
| } | ||
|
|
||
| if (icon is null || targetVC.NavigationItem.LeftBarButtonItem is null) | ||
| { | ||
| // Fallback: use Flyout.Title as text button | ||
| targetVC.NavigationItem.LeftBarButtonItem = | ||
| new UIBarButtonItem(fp.Flyout?.Title ?? string.Empty, UIBarButtonItemStyle.Plain, onItemTapped); | ||
| } | ||
|
|
||
| // Set AutomationId and VoiceOver label/hint on the hamburger button. | ||
| if (!string.IsNullOrEmpty(fp.AutomationId)) | ||
| { | ||
| targetVC.NavigationItem.LeftBarButtonItem.AccessibilityIdentifier = $"btn_{fp.AutomationId}"; | ||
| } | ||
|
|
||
| // Apply FlyoutPage's SemanticProperties (Description/Hint), if set. | ||
| var semantics = SemanticProperties.UpdateSemantics(fp, null); | ||
| if (semantics is not null) | ||
| { | ||
| targetVC.NavigationItem.LeftBarButtonItem.UpdateSemantics(semantics); | ||
| } | ||
| }); | ||
| } | ||
|
|
||
|
|
||
| internal static void MapApplyShadow(IFlyoutViewHandler handler, IFlyoutView view) | ||
| { | ||
| if (handler is FlyoutViewHandler h && h._manager is { } manager && view is BindableObject bo) | ||
| { | ||
| var applyShadow = PlatformConfiguration.iOSSpecific.FlyoutPage.GetApplyShadow(bo); | ||
| manager.UpdateApplyShadow(applyShadow); | ||
| } | ||
| } | ||
|
|
||
| internal static void MapFlowDirection(IFlyoutViewHandler handler, IFlyoutView view) | ||
| { | ||
| if (handler is FlyoutViewHandler h && h._manager is { } manager && view is IView v) | ||
| { | ||
| // Use the effective/inherited flow direction rather than the raw | ||
| // FlowDirection. A FlyoutPage left at the default MatchParent should | ||
| // follow an RTL app/window, not be treated as LTR. | ||
| var flowDirection = (view as IVisualElementController)?.EffectiveFlowDirection.ToFlowDirection() | ||
| ?? v.FlowDirection; | ||
| manager.UpdateFlowDirection(flowDirection); | ||
|
|
||
| // NavigationPage isn't auto-walked by Core's FlowDirection propagation, so | ||
| // manually re-trigger it on each page in the navigation stack. | ||
| if (view is FlyoutPage fp && fp.Detail is NavigationPage detailNavPage) | ||
| { | ||
| foreach (var page in detailNavPage.Navigation.NavigationStack) | ||
| { | ||
| if (page?.Handler is IElementHandler pageHandler) | ||
| { | ||
| pageHandler.UpdateValue(nameof(IView.FlowDirection)); | ||
| } | ||
| } | ||
| } | ||
| } | ||
| } | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.