Merge pull request #2500 from unoplatform/mergify/bp/release/stable/4.2/pr-2462

fix: Check if navigating back is possible to update state (backport #2462)
This commit is contained in:
Steve Bilogan 2024-08-08 08:55:43 -04:00 коммит произвёл GitHub
Родитель 3b862403c4 f9df35e8b7
Коммит f2869926b3
Не найден ключ, соответствующий данной подписи
Идентификатор ключа GPG: B5690EEEBB952194
7 изменённых файлов: 101 добавлений и 52 удалений

Просмотреть файл

@ -16,15 +16,14 @@ internal class AppHostingEnvironment : HostingEnvironment, IAppHostEnvironment,
public Assembly? HostAssembly { get; init; }
#if __WASM__
public async Task UpdateAddressBar(Uri applicationUri)
public async Task UpdateAddressBar(Uri applicationUri, bool canGoBack)
{
CoreApplication.MainView?.DispatcherQueue.TryEnqueue(() =>
{
var state = 1;
if (PlatformHelper.IsWebAssembly)
{
var currentView = SystemNavigationManager.GetForCurrentView();
state = currentView?.AppViewBackButtonVisibility == AppViewBackButtonVisibility.Visible ? 1 : 0;
state = canGoBack ? 1 : 0;
}
var href = Imports.GetLocation();

Просмотреть файл

@ -12,8 +12,11 @@ public interface IHasAddressBar
/// <param name="applicationUri">
/// The URI to update the address bar with.
/// </param>
/// <param name="canGoBack">
/// Whether it is possible to navigate back or not.
/// </param>
/// <returns>
/// A task that completes when the address bar has been updated.
/// </returns>
Task UpdateAddressBar(Uri applicationUri);
Task UpdateAddressBar(Uri applicationUri, bool canGoBack);
}

Просмотреть файл

@ -72,12 +72,14 @@ internal class BrowserAddressBarService : IHostedService
return;
}
var canGoBack = rootRegion.Navigator() is { } navigator && await navigator.CanGoBack();
var url = new UriBuilder
{
Query = route.Query(),
Path = route.FullPath()?.Replace("+", "/")
};
await _addressbarHost!.UpdateAddressBar(url.Uri);
await _addressbarHost!.UpdateAddressBar(url.Uri, canGoBack);
}
catch (Exception ex)
{

Просмотреть файл

@ -1,14 +1,14 @@
<Page x:Class="TestHarness.Ext.Navigation.PageNavigation.PageNavigationOnePage"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="using:TestHarness.Ext.Navigation.PageNavigation"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:local="using:TestHarness.Ext.Navigation.PageNavigation"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable="d"
xmlns:muxc="using:Microsoft.UI.Xaml.Controls"
xmlns:uen="using:Uno.Extensions.Navigation.UI"
xmlns:utu="using:Uno.Toolkit.UI"
xmlns:muxc="using:Microsoft.UI.Xaml.Controls"
Background="{ThemeResource ApplicationPageBackgroundThemeBrush}">
Background="{ThemeResource ApplicationPageBackgroundThemeBrush}"
mc:Ignorable="d">
<Grid>
<Grid.RowDefinitions>
@ -16,60 +16,61 @@
<RowDefinition Height="Auto" />
<RowDefinition />
</Grid.RowDefinitions>
<utu:NavigationBar Content="Page Navigation - One"
AutomationProperties.AutomationId="PageNavigationOne" />
<StackPanel HorizontalAlignment="Center"
VerticalAlignment="Center"
Grid.Row="1">
<utu:NavigationBar AutomationProperties.AutomationId="PageNavigationOne" Content="Page Navigation - One" />
<StackPanel Grid.Row="1"
HorizontalAlignment="Center"
VerticalAlignment="Center">
<TextBlock>
<Run Text="Created on UI Thread: " /><Run Text="{Binding CreatedOnUIThread}" />
</TextBlock>
<Button AutomationProperties.AutomationId="OnePageToTwoPageButton"
Content="Two"
uen:Navigation.Request="PageNavigationTwo" />
<Button uen:Navigation.Request="PageNavigationTwo"
AutomationProperties.AutomationId="OnePageToTwoPageButton"
Content="Two" />
<Button AutomationProperties.AutomationId="OnePageToTwoPageCodebehindButton"
Content="Two (Codebehind)"
Click="OnePageToTwoPageCodebehindClick" />
Click="OnePageToTwoPageCodebehindClick"
Content="Two (Codebehind)" />
<Button AutomationProperties.AutomationId="OnePageToTwoPageViewModelButton"
Content="Two (ViewModel)"
Click="{x:Bind ViewModel.GoToTwo}" />
Click="{x:Bind ViewModel.GoToTwo}"
Content="Two (ViewModel)" />
<Button AutomationProperties.AutomationId="RapidSettingsWriteTestButton"
Content="Settings Write Test"
Click="{x:Bind ViewModel.SettingsWriteTest}" />
Click="{x:Bind ViewModel.SettingsWriteTest}"
Content="Settings Write Test" />
<TextBlock x:Name="TxtUrl" AutomationProperties.AutomationId="OnePageTxtUrlFromBrowser" />
<Button AutomationProperties.AutomationId="OnePageGetUrlFromBrowser"
Click="GetUrlFromBrowser"
Content="Get URL" />
</StackPanel>
<ScrollViewer Grid.Row="2">
<muxc:ItemsRepeater ItemsSource="{Binding Items}"
uen:Navigation.Request="PageNavigationTwo">
<muxc:ItemsRepeater uen:Navigation.Request="PageNavigationTwo" ItemsSource="{Binding Items}">
<muxc:ItemsRepeater.ItemTemplate>
<DataTemplate>
<!--<UserControl>-->
<Grid Height="200"
Background="Red">
<Border Background="Pink"
VerticalAlignment="Top"
HorizontalAlignment="Center">
<TextBlock Text="{Binding}" />
</Border>
<utu:CardContentControl Height="50"
Width="200"
Style="{StaticResource FilledCardContentControlStyle}"
HorizontalAlignment="Stretch"
HorizontalContentAlignment="Stretch"
VerticalContentAlignment="Stretch">
<utu:CardContentControl.ContentTemplate>
<DataTemplate>
<Border Background="Green"
<Grid Height="200" Background="Red">
<Border HorizontalAlignment="Center"
VerticalAlignment="Top"
Background="Pink">
<TextBlock Text="{Binding}" />
</Border>
<utu:CardContentControl Width="200"
Height="50"
Width="200">
<TextBlock Text="Inner card" />
</Border>
</DataTemplate>
</utu:CardContentControl.ContentTemplate>
</utu:CardContentControl>
<Button VerticalAlignment="Bottom"
Content="Go to Three"
uen:Navigation.Request="PageNavigationThree" />
</Grid>
HorizontalAlignment="Stretch"
HorizontalContentAlignment="Stretch"
VerticalContentAlignment="Stretch"
Style="{StaticResource FilledCardContentControlStyle}">
<utu:CardContentControl.ContentTemplate>
<DataTemplate>
<Border Width="200"
Height="50"
Background="Green">
<TextBlock Text="Inner card" />
</Border>
</DataTemplate>
</utu:CardContentControl.ContentTemplate>
</utu:CardContentControl>
<Button VerticalAlignment="Bottom"
uen:Navigation.Request="PageNavigationThree"
Content="Go to Three" />
</Grid>
<!--</UserControl>-->
</DataTemplate>
</muxc:ItemsRepeater.ItemTemplate>

Просмотреть файл

@ -14,4 +14,22 @@ public sealed partial class PageNavigationOnePage : Page
await this.Navigator()!.NavigateViewAsync<PageNavigationTwoPage>(this);
}
public async void GetUrlFromBrowser(object sender, RoutedEventArgs e)
{
#if __WASM__
var url = Imports.GetLocation();
TxtUrl.Text = url;
#else
TxtUrl.Text = "Not supported";
#endif
}
}
#if __WASM__
internal static partial class Imports
{
[System.Runtime.InteropServices.JavaScript.JSImport("globalThis.Uno.Extensions.Hosting.getLocation")]
public static partial string GetLocation();
}
#endif

Просмотреть файл

@ -4,6 +4,9 @@
<ProjectGuid>6279c845-92f8-4333-ab99-3d213163593c</ProjectGuid>
<MinimumVisualStudioVersion>14.0</MinimumVisualStudioVersion>
</PropertyGroup>
<PropertyGroup>
<AllowUnsafeBlocks>true</AllowUnsafeBlocks>
</PropertyGroup>
<Import Project="$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props" Condition="Exists('$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props')" />
<Import Project="$(MSBuildExtensionsPath32)\Microsoft\VisualStudio\v$(VisualStudioVersion)\CodeSharing\Microsoft.CodeSharing.Common.Default.props" />
<Import Project="$(MSBuildExtensionsPath32)\Microsoft\VisualStudio\v$(VisualStudioVersion)\CodeSharing\Microsoft.CodeSharing.Common.props" />
@ -37,4 +40,4 @@
<_Globbled_Page Remove="Ext\Navigation\NavigationView\NavigationViewDataRecipeDetailsPage.xaml" />
<_Globbled_Page Remove="Ext\Navigation\NavigationView\NavigationViewDataRecipesPage.xaml" />
</ItemGroup>
</Project>
</Project>

Просмотреть файл

@ -176,4 +176,27 @@ public class Given_PageNavigation : NavigationTestBase
App.WaitThenTap("FourPageToFivePageButton");
}
[Test]
[ActivePlatforms(Platform.Browser)]
public void When_PageNavigationNavigateRootUpdateUrl()
{
InitTestSection(TestSections.Navigation_PageNavigation);
App.WaitThenTap("ShowOnePageButton");
App.WaitThenTap("OnePageGetUrlFromBrowser");
App.WaitElement("OnePageTxtUrlFromBrowser");
var urlBefore = App.GetText("OnePageTxtUrlFromBrowser");
App.WaitThenTap("OnePageToTwoPageButton");
App.WaitThenTap("TwoPageBackButton");
App.WaitThenTap("OnePageGetUrlFromBrowser");
App.WaitElement("OnePageTxtUrlFromBrowser");
var urlAfter = App.GetText("OnePageTxtUrlFromBrowser");
Assert.AreEqual(urlBefore, urlAfter);
}
}