diff --git a/src/Controls/src/Core/TitleBar/TitleBar.cs b/src/Controls/src/Core/TitleBar/TitleBar.cs index 51e0a7cbc375..e851f76da6a0 100644 --- a/src/Controls/src/Core/TitleBar/TitleBar.cs +++ b/src/Controls/src/Core/TitleBar/TitleBar.cs @@ -54,6 +54,40 @@ public partial class TitleBar : TemplatedView, ITitleBar, ISafeAreaView #if MACCATALYST static int GetMacCatalystLeadingMargin() => OperatingSystem.IsMacCatalystVersionAtLeast(26) ? MacCatalystMarginLiquidGlass : MacCatalystMargin; + + bool IsMacCatalystFullScreen() + { + if (OperatingSystem.IsMacCatalystVersionAtLeast(16) + && Window?.Handler?.PlatformView is UIKit.UIWindow uiwindow) + { + return uiwindow.WindowScene?.FullScreen ?? false; + } + + return false; + } + + void ApplyMacCatalystMargin() + { + if (!_isDefaultControlTemplate) + { + return; + } + + if (_templateRoot is not Grid contentGrid) + { + return; + } + + if (IsMacCatalystFullScreen()) + { + contentGrid.Margin = new Thickness(0); + return; + } + + contentGrid.Margin = FlowDirection == FlowDirection.RightToLeft + ? new Thickness(0, 0, GetMacCatalystLeadingMargin(), 0) + : new Thickness(GetMacCatalystLeadingMargin(), 0, 0, 0); + } #endif // Margin space (150px) required for Windows title bar system buttons @@ -313,21 +347,40 @@ public Color ForegroundColor static ControlTemplate? _defaultTemplate; View? _templateRoot; +#if MACCATALYST + bool _isDefaultControlTemplate; +#endif public TitleBar() { PassthroughElements = new List(); PropertyChanged += TitleBar_PropertyChanged; +#if MACCATALYST + SizeChanged += OnSizeChanged; +#endif + if (ControlTemplate is null) { ControlTemplate = DefaultTemplate; } } +#if MACCATALYST + void OnSizeChanged(object? sender, EventArgs e) + { + ApplyMacCatalystMargin(); + } +#endif + internal void Cleanup() { PropertyChanged -= TitleBar_PropertyChanged; + +#if MACCATALYST + SizeChanged -= OnSizeChanged; +#endif + if (Window is not null) { Window.Activated -= Window_Activated; @@ -358,6 +411,11 @@ void UpdateFlowDirectionState() : TitleBarLTRState; ApplyVisibleState(flowDirectionState); + +#if MACCATALYST + ApplyMacCatalystMargin(); +#endif + } internal void ApplyVisibleState(string stateGroup) @@ -391,6 +449,10 @@ protected override void OnApplyTemplate() _templateRoot = controlTemplate?.TemplateRoot as View; +#if MACCATALYST + _isDefaultControlTemplate = ReferenceEquals(ControlTemplate, DefaultTemplate); +#endif + if (controlTemplate?.GetTemplateChild(TitleBarLeading) is IView leadingContent) { PassthroughElements.Add(leadingContent); @@ -428,7 +490,7 @@ static View BuildDefaultTemplate() var contentGrid = new Grid() { #if MACCATALYST - Margin = new Thickness(GetMacCatalystLeadingMargin(), 0, 0, 0), + Margin = new Thickness(0), #endif HorizontalOptions = LayoutOptions.Fill, ColumnDefinitions = @@ -630,30 +692,30 @@ static View BuildDefaultTemplate() // Left-to-Right state (default) var ltrState = new VisualState() { Name = TitleBarLTRState }; + +#if !MACCATALYST ltrState.Setters.Add(new Setter() { Property = MarginProperty, TargetName = TemplateRootName, -#if MACCATALYST - Value = new Thickness(GetMacCatalystLeadingMargin(), 0, 0, 0) // System buttons on left in macOS -#else Value = new Thickness(0, 0, WindowsMargin, 0) // System buttons on right in Windows -#endif }); +#endif + flowDirectionGroup.States.Add(ltrState); // Right-to-Left state var rtlState = new VisualState() { Name = TitleBarRTLState }; + +#if !MACCATALYST rtlState.Setters.Add(new Setter() { Property = MarginProperty, TargetName = TemplateRootName, -#if MACCATALYST - Value = new Thickness(0, 0, GetMacCatalystLeadingMargin(), 0) // System buttons on right in macOS RTL -#else Value = new Thickness(WindowsMargin, 0, 0, 0) // System buttons on left in Windows RTL -#endif }); +#endif + flowDirectionGroup.States.Add(rtlState); visualStateGroups.Add(flowDirectionGroup); diff --git a/src/Controls/tests/TestCases.HostApp/Issues/Issue30248.cs b/src/Controls/tests/TestCases.HostApp/Issues/Issue30248.cs new file mode 100644 index 000000000000..fa2376317792 --- /dev/null +++ b/src/Controls/tests/TestCases.HostApp/Issues/Issue30248.cs @@ -0,0 +1,56 @@ +namespace Maui.Controls.Sample.Issues; + +[Issue(IssueTracker.Github, 30248, "TitleBar, MacCatalyst - content is not aligned to left on fullscreen", PlatformAffected.macOS)] + +public class Issue30248 : ContentPage +{ + public Issue30248() + { + Title = "Issue 30248"; + + // Create TitleBar + var titleBar = new TitleBar + { + Title = "Maui App", + Subtitle = "Hello, World!", + ForegroundColor = Colors.Red, + HeightRequest = 48 + }; + + titleBar.LeadingContent = new Image {Source = "dotnet_bot.png", HeightRequest = 24}; + + // Set the TitleBar on the current Window when this page appears + this.Loaded += (sender, e) => + { + if (Window != null) + { + Window.TitleBar = titleBar; + } + }; + + // Create the page content with a Label + Content = new VerticalStackLayout + { + Spacing = 25, + Padding = new Thickness(30), + VerticalOptions = LayoutOptions.Center, + Children = + { + new Label + { + Text = "TitleBar should be aligned to the left in fullscreen mode", + AutomationId = "TitleBarAlignmentLabel", + FontSize = 32, + HorizontalOptions = LayoutOptions.Center + }, + new Button + { + Text = "Empty Button", + AutomationId = "EmptyButton", + HorizontalOptions = LayoutOptions.Center + } + } + }; + } +} + diff --git a/src/Controls/tests/TestCases.Mac.Tests/snapshots/mac/VerifyTitleBarContentinFullScreenmode.png b/src/Controls/tests/TestCases.Mac.Tests/snapshots/mac/VerifyTitleBarContentinFullScreenmode.png new file mode 100644 index 000000000000..904c43285e23 Binary files /dev/null and b/src/Controls/tests/TestCases.Mac.Tests/snapshots/mac/VerifyTitleBarContentinFullScreenmode.png differ diff --git a/src/Controls/tests/TestCases.Shared.Tests/Tests/Issues/Issue30248.cs b/src/Controls/tests/TestCases.Shared.Tests/Tests/Issues/Issue30248.cs new file mode 100644 index 000000000000..7813cdad6382 --- /dev/null +++ b/src/Controls/tests/TestCases.Shared.Tests/Tests/Issues/Issue30248.cs @@ -0,0 +1,34 @@ +#if MACCATALYST //This is the Mac Specific issue, so restricting other platforms +using NUnit.Framework; +using UITest.Appium; +using UITest.Core; + +namespace Microsoft.Maui.TestCases.Tests.Issues; + +public class Issue30248 : _IssuesUITest +{ + public override string Issue => "TitleBar, MacCatalyst - content is not aligned to left on fullscreen"; + + public Issue30248(TestDevice device) + : base(device) + { } + + [Test] + [Category(UITestCategories.Window)] + public void VerifyTitleBarContentinFullScreenmode() + { + App.WaitForElement("TitleBarAlignmentLabel"); + try + { + App.EnterFullScreen(); + App.WaitForElement("TitleBarAlignmentLabel"); + App.Tap("EmptyButton"); + VerifyScreenshot(includeTitleBar: true); + } + finally + { + App.ExitFullScreen(); + } + } +} +#endif \ No newline at end of file