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

[Windows] Notify Shell tab title changes #16593

Merged
merged 4 commits into from
Aug 25, 2023
Merged
Show file tree
Hide file tree
Changes from 3 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
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.PointerGestureRecognizer.PointerPressed -> System.EventHandler<Microsoft.Maui.Controls.PointerEventArgs!>?
Microsoft.Maui.Controls.PointerGestureRecognizer.PointerPressedCommand.get -> System.Windows.Input.ICommand!
Microsoft.Maui.Controls.PointerGestureRecognizer.PointerPressedCommand.set -> void
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