Skip to content

Commit

Permalink
Invalidate shell tab title on Windows (#16593)
Browse files Browse the repository at this point in the history
Co-authored-by: Rui Marinho <[email protected]>
  • Loading branch information
jsuarezruiz and rmarinho authored Aug 25, 2023
1 parent 5ac1e80 commit d933b72
Show file tree
Hide file tree
Showing 4 changed files with 57 additions and 1 deletion.
12 changes: 12 additions & 0 deletions src/Controls/src/Core/Handlers/Shell/ShellItemHandler.Windows.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ public partial class ShellItemHandler : ElementHandler<ShellItem, FrameworkEleme
public static PropertyMapper<ShellItem, ShellItemHandler> Mapper =
new PropertyMapper<ShellItem, ShellItemHandler>(ElementMapper)
{
[nameof(ShellItem.Title)] = MapTitle,
[nameof(ShellItem.CurrentItem)] = MapCurrentItem,
[Shell.TabBarIsVisibleProperty.PropertyName] = MapTabBarIsVisible
};
Expand Down Expand Up @@ -352,6 +353,12 @@ void UpdateQueryIcon()
}

ShellSection? _currentShellSection;

internal void UpdateTitle()
{
MapMenuItems();
}

void UpdateCurrentItem()
{
if (_currentShellSection == VirtualView.CurrentItem)
Expand Down Expand Up @@ -433,6 +440,11 @@ NavigationViewPaneDisplayMode GetNavigationViewPaneDisplayMode(IShellItemControl
NavigationViewPaneDisplayMode.LeftMinimal;
}

public static void MapTitle(ShellItemHandler handler, ShellItem item)
{
handler.UpdateTitle();
}

public static void MapCurrentItem(ShellItemHandler handler, ShellItem item)
{
handler.UpdateCurrentItem();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ public partial class ShellSectionHandler : ElementHandler<ShellSection, WFrame>,
public static PropertyMapper<ShellSection, ShellSectionHandler> Mapper =
new PropertyMapper<ShellSection, ShellSectionHandler>(ElementMapper)
{
[nameof(ShellSection.Title)] = MapTitle,
[nameof(ShellSection.CurrentItem)] = MapCurrentItem,
};

Expand All @@ -34,7 +35,13 @@ protected override WFrame CreatePlatformElement()
_navigationManager = CreateNavigationManager();
return new WFrame();
}

public static void MapTitle(ShellSectionHandler handler, ShellSection item)
{
var shellItem = item.Parent as ShellItem;
var shellItemHandler = shellItem?.Handler as ShellItemHandler;
shellItemHandler?.UpdateTitle();
}

public static void MapCurrentItem(ShellSectionHandler handler, ShellSection item)
{
handler.SyncNavigationStack(false, null);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
#nullable enable
*REMOVED*~Microsoft.Maui.Controls.Accelerator.Modifiers.set -> void
*REMOVED*~Microsoft.Maui.Controls.Accelerator.Keys.set -> void
static Microsoft.Maui.Controls.Handlers.ShellItemHandler.MapTitle(Microsoft.Maui.Controls.Handlers.ShellItemHandler! handler, Microsoft.Maui.Controls.ShellItem! item) -> void
static Microsoft.Maui.Controls.Handlers.ShellSectionHandler.MapTitle(Microsoft.Maui.Controls.Handlers.ShellSectionHandler! handler, Microsoft.Maui.Controls.ShellSection! item) -> void
Microsoft.Maui.Controls.Handlers.Compatibility.ViewRenderer<TElement, TNativeView>.ViewRenderer(Microsoft.Maui.IPropertyMapper! mapper, Microsoft.Maui.CommandMapper? commandMapper = null) -> void
Microsoft.Maui.Controls.Handlers.Compatibility.VisualElementRenderer<TElement, TPlatformElement>.VisualElementRenderer(Microsoft.Maui.IPropertyMapper! mapper, Microsoft.Maui.CommandMapper? commandMapper = null) -> void
Microsoft.Maui.Controls.PointerGestureRecognizer.PointerPressed -> System.EventHandler<Microsoft.Maui.Controls.PointerEventArgs!>?
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
using Microsoft.Maui.Platform;
using Microsoft.UI.Xaml;
using Microsoft.UI.Xaml.Controls;
using Microsoft.Maui.DeviceTests.Stubs;
using Xunit;
using NavigationView = Microsoft.UI.Xaml.Controls.NavigationView;
using WFrameworkElement = Microsoft.UI.Xaml.FrameworkElement;
Expand All @@ -28,6 +29,40 @@ protected Task CheckFlyoutState(ShellHandler handler, bool desiredState)
return Task.CompletedTask;
}

[Fact(DisplayName = "Shell Title Updates Correctly")]
public async Task ShellTitleUpdatesCorrectly()
{
SetupBuilder();

var page1 = new ContentPage()
{ Content = new Label() { Text = "Page 1" }, Title = "Page 1" };
var page2 = new ContentPage()
{ Content = new Label() { Text = "Page 2" }, Title = "Page 2" };

var shell = await CreateShellAsync((shell) =>
{
var tabBar = new TabBar()
{
Items =
{
new ShellContent(){ Content = page1 },
new ShellContent(){ Content = page2 },
}
};

shell.Items.Add(tabBar);
});

await CreateHandlerAndAddToWindow<WindowHandlerStub>(new Controls.Window(shell), (handler) =>
{
Assert.Equal("Page 1", GetToolbarTitle(handler));

string newTitle = "New Page 1";
page1.Title = newTitle;
Assert.Equal(newTitle, GetToolbarTitle(handler));
});
}

[Fact(DisplayName = "Shell FlyoutIcon Initializes Correctly")]
public async Task ShellFlyoutIconInitializesCorrectly()
{
Expand Down

0 comments on commit d933b72

Please sign in to comment.