Skip to content

Commit

Permalink
Use PrependToMapping method to replace Window MapTitle method (#15041)
Browse files Browse the repository at this point in the history
* Use new PrependToMapping method

* this

* The entire method is replaced

* Added a test
  • Loading branch information
mattleibow committed May 23, 2023
1 parent ecd03a3 commit c7cc10b
Show file tree
Hide file tree
Showing 5 changed files with 61 additions and 24 deletions.
4 changes: 2 additions & 2 deletions eng/pipelines/common/device-tests-steps.yml
Expand Up @@ -62,13 +62,13 @@ steps:
inputs:
testResultsFormat: xUnit
testResultsFiles: '$(TestResultsDirectory)/**/TestResults.xml'
testRunTitle: '$(System.PhaseName)'
testRunTitle: '$(System.PhaseName) (attempt: $(System.JobAttempt))'

- task: PublishBuildArtifacts@1
displayName: Publish Artifacts
condition: always()
inputs:
artifactName: $(System.PhaseName)
artifactName: $(System.PhaseName)_attempt_$(System.JobAttempt)

# This must always be placed as the last step in the job
- template: agent-rebooter/mac.v1.yml@yaml-templates
Expand Down
1 change: 1 addition & 0 deletions src/Controls/src/Core/HandlerImpl/Window/Window.Android.cs
Expand Up @@ -17,6 +17,7 @@ public static void MapContent(WindowHandler handler, IWindow view)
{
}

[Obsolete]
public static void MapContent(IWindowHandler handler, IWindow view)
{
}
Expand Down
13 changes: 4 additions & 9 deletions src/Controls/src/Core/HandlerImpl/Window/Window.Windows.cs
Expand Up @@ -17,16 +17,11 @@ void UpdateTitleBarDragRectangles(Rect[] titleBarDragRectangles)
Handler?.UpdateValue(nameof(IWindow.TitleBarDragRectangles));
}

static void MapWindowTitle(IWindowHandler handler, IWindow window)
static void MapTitle(IWindowHandler handler, Window window)
{
if (window is Window controlsWindow)
{
handler
.PlatformView
.UpdateTitle(window, controlsWindow.GetCurrentlyPresentedMauiContext());
}

WindowHandler.MapTitle(handler, window);
handler
.PlatformView
.UpdateTitle(window, window.GetCurrentlyPresentedMauiContext());
}

Rect[] IWindow.TitleBarDragRectangles
Expand Down
20 changes: 7 additions & 13 deletions src/Controls/src/Core/HandlerImpl/Window/Window.cs
Expand Up @@ -6,26 +6,20 @@ namespace Microsoft.Maui.Controls
{
public partial class Window
{
[Obsolete("Use WindowHandler.Mapper instead.")]
public static IPropertyMapper<IWindow, WindowHandler> ControlsWindowMapper =
new PropertyMapper<IWindow, WindowHandler>(WindowHandler.Mapper);
new PropertyMapper<Window, WindowHandler>(WindowHandler.Mapper);

// ControlsWindowMapper incorrectly typed to WindowHandler
static IPropertyMapper<IWindow, IWindowHandler> Mapper =
new PropertyMapper<IWindow, IWindowHandler>(ControlsWindowMapper)
{
internal static void RemapForControls()
{
#if ANDROID
// This property is also on the Application Mapper since that's where the attached property exists
[PlatformConfiguration.AndroidSpecific.Application.WindowSoftInputModeAdjustProperty.PropertyName] = MapWindowSoftInputModeAdjust,
// This property is also on the Application Mapper since that's where the attached property exists
WindowHandler.Mapper.ReplaceMapping<IWindow, IWindowHandler>(PlatformConfiguration.AndroidSpecific.Application.WindowSoftInputModeAdjustProperty.PropertyName, MapWindowSoftInputModeAdjust);
#endif

#if WINDOWS
[nameof(ITitledElement.Title)] = MapWindowTitle
WindowHandler.Mapper.PrependToMapping<Window, IWindowHandler>(nameof(ITitledElement.Title), MapTitle);
#endif
};

internal static void RemapForControls()
{
WindowHandler.Mapper = Mapper;
}
}
}
Expand Up @@ -85,5 +85,52 @@ public async Task WindowTitleSetToModalTitleContainer()
Assert.Equal(modalNavigationRootManager.WindowTitle, mauiWindow.Title);
});
}

[Fact]
public async Task WindowTitleIsCorrectAfterPushAndPop()
{
const string OriginalTitle = "Original Title";
const string UpdatedTitle = "Updated Title";

SetupBuilder();

var navPage = new NavigationPage(new ContentPage());
var window = new Window(navPage) { Title = OriginalTitle };

await CreateHandlerAndAddToWindow(window,
(Func<IWindowHandler, Task>)(async (handler) =>
{
var mauiWindow = handler.PlatformView;
var currentPage = navPage.CurrentPage;
var modalPage = new ContentPage();
await currentPage.Navigation.PushModalAsync(modalPage);
await OnLoadedAsync(modalPage);
var currentNavigationRootManager = currentPage
.FindMauiContext()
.GetNavigationRootManager();
var modalNavigationRootManager = modalPage
.FindMauiContext()
.GetNavigationRootManager();
Assert.Equal(OriginalTitle, mauiWindow.Title);
Assert.Equal(OriginalTitle, currentNavigationRootManager.WindowTitle);
Assert.Equal(OriginalTitle, modalNavigationRootManager.WindowTitle);
window.Title = UpdatedTitle;
Assert.Equal(UpdatedTitle, mauiWindow.Title);
Assert.Equal(UpdatedTitle, currentNavigationRootManager.WindowTitle);
Assert.Equal(UpdatedTitle, modalNavigationRootManager.WindowTitle);
await currentPage.Navigation.PopModalAsync();
await OnUnloadedAsync(modalPage);
Assert.Equal(UpdatedTitle, mauiWindow.Title);
Assert.Equal(UpdatedTitle, currentNavigationRootManager.WindowTitle);
Assert.Equal(UpdatedTitle, modalNavigationRootManager.WindowTitle);
}));
}
}
}

0 comments on commit c7cc10b

Please sign in to comment.