Skip to content
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

Propagate Title to android and ios window #14413

Merged
merged 3 commits into from
Apr 11, 2023
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
3 changes: 2 additions & 1 deletion src/Core/src/Handlers/Window/WindowHandler.Android.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,8 @@ protected override void ConnectHandler(Activity platformView)
UpdateVirtualViewFrame(platformView);
}

public static void MapTitle(IWindowHandler handler, IWindow window) { }
public static void MapTitle(IWindowHandler handler, IWindow window) =>
handler.PlatformView.UpdateTitle(window);

public static void MapContent(IWindowHandler handler, IWindow window)
{
Expand Down
10 changes: 2 additions & 8 deletions src/Core/src/Handlers/Window/WindowHandler.iOS.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,14 +12,8 @@ protected override void ConnectHandler(UIWindow platformView)

UpdateVirtualViewFrame(platformView);
}

public static void MapTitle(IWindowHandler handler, IWindow window)
{
#if MACCATALYST
if (handler.PlatformView.WindowScene is not null)
handler.PlatformView.WindowScene.Title = window.Title ?? String.Empty;
#endif
}
public static void MapTitle(IWindowHandler handler, IWindow window) =>
handler.PlatformView.UpdateTitle(window);

public static void MapContent(IWindowHandler handler, IWindow window)
{
Expand Down
8 changes: 8 additions & 0 deletions src/Core/src/Platform/Android/WindowExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,14 @@ namespace Microsoft.Maui
{
public static partial class WindowExtensions
{
internal static void UpdateTitle(this Activity platformWindow, IWindow window)
{
if (string.IsNullOrEmpty(window.Title))
platformWindow.Title = ApplicationModel.AppInfo.Current.Name;
else
platformWindow.Title = window.Title;
}

internal static DisplayOrientation GetOrientation(this IWindow? window)
{
if (window == null)
Expand Down
9 changes: 9 additions & 0 deletions src/Core/src/Platform/iOS/WindowExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,15 @@ namespace Microsoft.Maui.Platform
{
public static partial class WindowExtensions
{
internal static void UpdateTitle(this UIWindow platformWindow, IWindow window)
{
// If you set the title to null the app will crash
// If you set it to an empty string the title reverts back to the
// default app title.
if (OperatingSystem.IsIOSVersionAtLeast(13) && platformWindow.WindowScene is not null)
platformWindow.WindowScene.Title = window.Title ?? String.Empty;
}

internal static void UpdateX(this UIWindow platformWindow, IWindow window) =>
platformWindow.UpdateUnsupportedCoordinate(window);

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
using System.Threading.Tasks;
using AndroidX.AppCompat.App;
using Microsoft.Maui.Controls;
using Xunit;

namespace Microsoft.Maui.DeviceTests
{
public partial class WindowHandlerTests : CoreHandlerTestBase
{
[Fact]
public async Task TitleSetsOnWindow()
{
await InvokeOnMainThreadAsync(() =>
{
var activity = (AppCompatActivity)MauiProgramDefaults.DefaultContext;
var testWindow = new Window();

Assert.True(activity is not null, "Activity is Null");

testWindow.Title = "Test Title";
WindowExtensions.UpdateTitle(activity, testWindow);

Assert.Equal("Test Title", activity.Title);
testWindow.Title = null;

WindowExtensions.UpdateTitle(activity, testWindow);
Assert.Equal(activity.Title, ApplicationModel.AppInfo.Current.Name);
});
}
}
}