Skip to content

Commit

Permalink
Merge pull request #1054 from unoplatform/dev/ramr/issueUno14746-addH…
Browse files Browse the repository at this point in the history
…ashRedirect

Add fragment or args redirect to samples page
  • Loading branch information
jeromelaban authored Jan 30, 2024
2 parents 6014393 + c11b53e commit c645a47
Show file tree
Hide file tree
Showing 10 changed files with 381 additions and 22 deletions.
71 changes: 49 additions & 22 deletions Uno.Gallery/Uno.Gallery.Shared/App.xaml.cs
Original file line number Diff line number Diff line change
@@ -1,26 +1,22 @@
using Microsoft.Extensions.Logging;
using Microsoft.UI.Dispatching;
using Microsoft.UI.Xaml;
using Microsoft.UI.Xaml.Automation;
using Microsoft.UI.Xaml.Controls;
using ShowMeTheXAML;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using Uno.Extensions;
using Uno.Gallery.Entities;
using Uno.Gallery.Helpers;
using Uno.Gallery.Views.GeneralPages;
using Uno.Logging;
using Uno.UI;
using Windows.ApplicationModel;
using Windows.ApplicationModel.Activation;
using Windows.Foundation;
using Windows.UI.ViewManagement;
using Microsoft.UI.Xaml;
using Microsoft.UI.Xaml.Automation;
using Microsoft.UI.Xaml.Controls;
using LaunchActivatedEventArgs = Microsoft.UI.Xaml.LaunchActivatedEventArgs;
using MUXC = Microsoft.UI.Xaml.Controls;
using MUXCP = Microsoft.UI.Xaml.Controls.Primitives;
using LaunchActivatedEventArgs = Microsoft.UI.Xaml.LaunchActivatedEventArgs;
using Microsoft.UI.Dispatching;
using Uno.UI;

namespace Uno.Gallery
{
Expand All @@ -33,8 +29,6 @@ public partial class App : Application

public Window MainWindow { get; private set; }

private static Sample[] _samples;

private Shell _shell;

/// <summary>
Expand Down Expand Up @@ -184,15 +178,15 @@ private void ShellNavigateTo(Sample sample, bool trySynchronizeCurrentItem)
public void SearchShellNavigateTo(Sample sample)
{
var nv = _shell.NavigationView;
if(nv.Content?.GetType() == sample.ViewType)
if (nv.Content?.GetType() == sample.ViewType)
{
return;
}

MUXC.NavigationViewItem selectedItem = null;
MUXC.NavigationViewItem selectedCategory = null;

foreach(MUXC.NavigationViewItem category in nv.MenuItems)
foreach (MUXC.NavigationViewItem category in nv.MenuItems)
{
selectedItem = category.MenuItems.OfType<MUXC.NavigationViewItem>()
.FirstOrDefault(item => item.DataContext is Sample s && s.ViewType == sample.ViewType);
Expand All @@ -204,7 +198,7 @@ public void SearchShellNavigateTo(Sample sample)
}
}

if(selectedItem is null)
if (selectedItem is null)
{
nv.SelectedItem = nv.MenuItems[0];
}
Expand Down Expand Up @@ -233,20 +227,53 @@ private Shell BuildShell()
_shell.RegisterPropertyChangedCallback(Shell.CurrentSampleBackdoorProperty, OnCurrentSampleBackdoorChanged);
var nv = _shell.NavigationView;
AddNavigationItems(nv);

// landing navigation
ShellNavigateTo<OverviewPage>(
#if __WASM__
if (!IsThereSampleFilteredByArgs(nv))
#endif
{
// landing navigation
ShellNavigateTo<OverviewPage>(
#if !WINDOWS
// workaround for uno#5069: setting NavView.SelectedItem at launch bricks it
trySynchronizeCurrentItem: false
// workaround for uno#5069: setting NavView.SelectedItem at launch bricks it
trySynchronizeCurrentItem: false
#endif
);
);
}

// navigation + setting handler
nv.ItemInvoked += OnNavigationItemInvoked;

return _shell;
}
#if __WASM__
private bool IsThereSampleFilteredByArgs(MUXC.NavigationView nv)
{
var argumentsHash = Wasm.FragmentNavigationHandler.CurrentFragment;
if (argumentsHash.Contains("#"))
{
string searchTerm = (argumentsHash + string.Empty).Replace("#", string.Empty);

foreach (MUXC.NavigationViewItem item in nv.MenuItems)
{
MUXC.NavigationViewItem sampleItem = item.MenuItems
.Cast<MUXC.NavigationViewItem>()
.FirstOrDefault(i => i.Content.ToString().Contains(searchTerm, StringComparison.InvariantCultureIgnoreCase));

if (sampleItem != null)
{
ShellNavigateTo(
(Uno.Gallery.Sample)sampleItem.DataContext
, trySynchronizeCurrentItem: false
);
return true;
}
}
//If there is a Hash that is not valid, redirect it to the root of the site.
Wasm.LocationHrefNavigationHandler.CurrentLocationHref = "/";
}
return false;
}
#endif

private void OnCurrentSampleBackdoorChanged(DependencyObject sender, DependencyProperty dp)
{
Expand Down Expand Up @@ -417,7 +444,7 @@ private void ConfigureXamlDisplay()
private void ConfigureFeatureFlags()
{
#if !WINDOWS
FeatureConfiguration.ApiInformation.NotImplementedLogLevel = Foundation.Logging.LogLevel.Debug; // Raise not implemented usages as Debug messages
FeatureConfiguration.ApiInformation.NotImplementedLogLevel = Foundation.Logging.LogLevel.Debug; // Raise not implemented usages as Debug messages
FeatureConfiguration.ToolTip.UseToolTips = true;
#endif
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
using System.Runtime.InteropServices.JavaScript;

namespace Uno.Gallery.Wasm
{
internal sealed partial class FragmentNavigationHandler
{
internal static partial class NativeMethods
{
private const string JsType = "globalThis.Uno.Gallery.Wasm.FragmentNavigation";

[JSImport($"{JsType}.getCurrentFragment")]
internal static partial string GetCurrentFragment();

[JSImport($"{JsType}.setCurrentFragment")]
internal static partial string SetCurrentFragment(string fragment);
}
}
}
21 changes: 21 additions & 0 deletions Uno.Gallery/Uno.Gallery.Wasm/FragmentNavigationHandler.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
using Uno.Foundation;

namespace Uno.Gallery.Wasm
{
internal partial class FragmentNavigationHandler
{
public static string CurrentFragment
{
get
{
return NativeMethods.GetCurrentFragment();
}

set
{
var escaped = WebAssemblyRuntime.EscapeJs(value);
NativeMethods.SetCurrentFragment(escaped);
}
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
using System.Runtime.InteropServices.JavaScript;

namespace Uno.Gallery.Wasm
{
internal sealed partial class LocationHrefNavigationHandler
{
internal static partial class NativeMethods
{
private const string JsType = "globalThis.Uno.Gallery.Wasm.LocationHrefNavigation";

[JSImport($"{JsType}.getCurrentLocationHref")]
internal static partial string GetCurrentLocationHref();

[JSImport($"{JsType}.setCurrentLocationHref")]
internal static partial string SetCurrentLocationHref(string locationHref);
}
}
}
21 changes: 21 additions & 0 deletions Uno.Gallery/Uno.Gallery.Wasm/LocationHrefNavigationHandler.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
using Uno.Foundation;

namespace Uno.Gallery.Wasm
{
internal partial class LocationHrefNavigationHandler
{
public static string CurrentLocationHref
{
get
{
return NativeMethods.GetCurrentLocationHref();
}

set
{
var escaped = WebAssemblyRuntime.EscapeJs(value);
NativeMethods.SetCurrentLocationHref(escaped);
}
}
}
}
6 changes: 6 additions & 0 deletions Uno.Gallery/Uno.Gallery.Wasm/Uno.Gallery.Wasm.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -146,4 +146,10 @@
<Folder Include="Properties\" />
</ItemGroup>
<Import Project="..\Uno.Gallery.Shared\Uno.Gallery.Shared.projitems" Label="Shared" Condition="Exists('..\Uno.Gallery.Shared\Uno.Gallery.Shared.projitems')" />
<ItemGroup>
<_Globbed_Compile Remove="FragmentNavigationHandler.cs" />
<_Globbed_Compile Remove="FragmentNavigationHandler.Interop.wasm.cs" />
<_Globbed_Compile Remove="LocationHrefNavigationHandler.cs" />
<_Globbed_Compile Remove="LocationHrefNavigationHandler.Interop.wasm.cs" />
</ItemGroup>
</Project>
24 changes: 24 additions & 0 deletions Uno.Gallery/Uno.Gallery.Wasm/WasmScripts/UnoGallery.d.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,31 @@
declare namespace Uno.Gallery.Wasm {
class FragmentNavigation {
private static currentFragment;
static getCurrentFragment(): string;
static setCurrentFragment(fragment: string): string;
private static subscribed;
static subscribeToFragmentChanged(): string;
private static notifyFragmentChangedMethod;
private static notifyFragmentChanged;
private static initializeMethods;
}
}
declare namespace Uno.UI.Demo {
class Analytics {
private static isLoaded;
static reportPageView(screenName: string, appName?: string): string;
private static init;
}
}
declare namespace Uno.Gallery.Wasm {
class LocationHrefNavigation {
private static currentLocationHref;
static getCurrentLocationHref(): string;
static setCurrentLocationHref(locationHref: string): string;
private static subscribed;
static subscribeToLocationHrefChanged(): string;
private static notifyLocationHrefChangedMethod;
private static notifyLocationHrefChanged;
private static initializeMethods;
}
}
96 changes: 96 additions & 0 deletions Uno.Gallery/Uno.Gallery.Wasm/WasmScripts/UnoGallery.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,52 @@
var Uno;
(function (Uno) {
var Gallery;
(function (Gallery) {
var Wasm;
(function (Wasm) {
class FragmentNavigation {
static getCurrentFragment() {
return window.location.hash;
}
static setCurrentFragment(fragment) {
window.location.hash = fragment;
this.currentFragment = window.location.hash;
return "ok";
}
static subscribeToFragmentChanged() {
if (this.subscribed) {
return "already subscribed";
}
this.subscribed = true;
this.currentFragment = this.getCurrentFragment();
window.addEventListener("hashchange", _ => this.notifyFragmentChanged(), false);
return "ok";
}
static notifyFragmentChanged() {
const newFragment = this.getCurrentFragment();
if (newFragment === this.currentFragment) {
return; // nothing to do
}
this.currentFragment = newFragment;
this.initializeMethods();
const newFragmentStr = MonoRuntime.mono_string(newFragment);
MonoRuntime.call_method(this.notifyFragmentChangedMethod, null, [newFragmentStr]);
}
static initializeMethods() {
if (this.notifyFragmentChangedMethod) {
return; // already initialized.
}
const asm = MonoRuntime.assembly_load("Uno.Gallery.WASM");
const handlerClass = MonoRuntime.find_class(asm, "Uno.Gallery.Wasm", "FragmentNavigation");
this.notifyFragmentChangedMethod = MonoRuntime.find_method(handlerClass, "NotifyFragmentChanged", -1);
}
}
FragmentNavigation.subscribed = false;
Wasm.FragmentNavigation = FragmentNavigation;
})(Wasm = Gallery.Wasm || (Gallery.Wasm = {}));
})(Gallery = Uno.Gallery || (Uno.Gallery = {}));
})(Uno || (Uno = {}));
var Uno;
(function (Uno) {
var UI;
(function (UI) {
Expand Down Expand Up @@ -48,3 +96,51 @@ var Uno;
})(Demo = UI.Demo || (UI.Demo = {}));
})(UI = Uno.UI || (Uno.UI = {}));
})(Uno || (Uno = {}));
var Uno;
(function (Uno) {
var Gallery;
(function (Gallery) {
var Wasm;
(function (Wasm) {
class LocationHrefNavigation {
static getCurrentLocationHref() {
return window.location.href;
}
static setCurrentLocationHref(locationHref) {
window.location.href = locationHref;
this.currentLocationHref = window.location.href;
return "ok";
}
static subscribeToLocationHrefChanged() {
if (this.subscribed) {
return "already subscribed";
}
this.subscribed = true;
this.currentLocationHref = this.getCurrentLocationHref();
window.addEventListener("hashchange", _ => this.notifyLocationHrefChanged(), false);
return "ok";
}
static notifyLocationHrefChanged() {
const newLocationHref = this.getCurrentLocationHref();
if (newLocationHref === this.currentLocationHref) {
return; // nothing to do
}
this.currentLocationHref = newLocationHref;
this.initializeMethods();
const newLocationHrefStr = MonoRuntime.mono_string(newLocationHref);
MonoRuntime.call_method(this.notifyLocationHrefChangedMethod, null, [newLocationHrefStr]);
}
static initializeMethods() {
if (this.notifyLocationHrefChangedMethod) {
return; // already initialized.
}
const asm = MonoRuntime.assembly_load("Uno.Gallery.WASM");
const handlerClass = MonoRuntime.find_class(asm, "Uno.Gallery.Wasm", "LocationHrefHavigation");
this.notifyLocationHrefChangedMethod = MonoRuntime.find_method(handlerClass, "NotifyLocationHrefChanged", -1);
}
}
LocationHrefNavigation.subscribed = false;
Wasm.LocationHrefNavigation = LocationHrefNavigation;
})(Wasm = Gallery.Wasm || (Gallery.Wasm = {}));
})(Gallery = Uno.Gallery || (Uno.Gallery = {}));
})(Uno || (Uno = {}));
Loading

0 comments on commit c645a47

Please sign in to comment.