From e0e163d0a7617cf6548052a6ae91a59f67e07a4a Mon Sep 17 00:00:00 2001 From: Rafael Rosa Date: Mon, 11 Dec 2023 14:17:03 -0300 Subject: [PATCH 1/6] chore: add hash or args redirect to samples page --- Uno.Gallery/Uno.Gallery.Shared/App.xaml.cs | 79 +++++++++++++------ .../Properties/launchSettings.json | 10 +++ .../FragmentHavigationHandler.cs | 26 ++++++ .../Uno.Gallery.Wasm/Uno.Gallery.Wasm.csproj | 3 + .../WasmScripts/UnoGallery.d.ts | 12 +++ .../WasmScripts/UnoGallery.js | 45 +++++++++++ .../Uno.Gallery.Wasm/ts/FragmentNavigation.ts | 64 +++++++++++++++ 7 files changed, 216 insertions(+), 23 deletions(-) create mode 100644 Uno.Gallery/Uno.Gallery.Skia.Gtk/Properties/launchSettings.json create mode 100644 Uno.Gallery/Uno.Gallery.Wasm/FragmentHavigationHandler.cs create mode 100644 Uno.Gallery/Uno.Gallery.Wasm/ts/FragmentNavigation.ts diff --git a/Uno.Gallery/Uno.Gallery.Shared/App.xaml.cs b/Uno.Gallery/Uno.Gallery.Shared/App.xaml.cs index 2b609d10d..68dee6956 100644 --- a/Uno.Gallery/Uno.Gallery.Shared/App.xaml.cs +++ b/Uno.Gallery/Uno.Gallery.Shared/App.xaml.cs @@ -1,7 +1,10 @@ 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; @@ -9,18 +12,11 @@ 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 { @@ -71,10 +67,11 @@ protected override void OnLaunched(LaunchActivatedEventArgs e) base.OnLaunched(e); this.Log().Debug("Launched app."); - OnLaunchedOrActivated(); + this.Log().Debug(e.Arguments.ToString()); + OnLaunchedOrActivated(e); } - private void OnLaunchedOrActivated() + private void OnLaunchedOrActivated(LaunchActivatedEventArgs e) { #if WINDOWS && !HAS_UNO MainWindow = new Window(); @@ -90,7 +87,7 @@ private void OnLaunchedOrActivated() Xamarin.Calabash.Start(); #endif - MainWindow.Content = _shell = BuildShell(); + MainWindow.Content = _shell = BuildShell(e); } // Ensure the current window is active @@ -184,7 +181,7 @@ 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; } @@ -192,7 +189,7 @@ public void SearchShellNavigateTo(Sample sample) 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() .FirstOrDefault(item => item.DataContext is Sample s && s.ViewType == sample.ViewType); @@ -204,7 +201,7 @@ public void SearchShellNavigateTo(Sample sample) } } - if(selectedItem is null) + if (selectedItem is null) { nv.SelectedItem = nv.MenuItems[0]; } @@ -226,7 +223,7 @@ public void SearchShellNavigateTo(Sample sample) _shell.NavigationView.Content = page; } - private Shell BuildShell() + private Shell BuildShell(LaunchActivatedEventArgs e) { _shell = new Shell(); AutomationProperties.SetAutomationId(_shell, "AppShell"); @@ -234,13 +231,16 @@ private Shell BuildShell() var nv = _shell.NavigationView; AddNavigationItems(nv); - // landing navigation - ShellNavigateTo( + if (!IsThereSampleFilteredByArgs(nv, e)) + { + // landing navigation + ShellNavigateTo( #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; @@ -248,6 +248,39 @@ private Shell BuildShell() return _shell; } + private bool IsThereSampleFilteredByArgs(MUXC.NavigationView nv, LaunchActivatedEventArgs e) + { +#if __WASM__ + var argumentsHash = Uno.Gallery.Wasm.FragmentHavigationHandler.CurrentFragment; +#else + var argumentsHash = e.Arguments; +#endif + if (argumentsHash.Contains("#")) + { + string searchTerm = (argumentsHash + string.Empty).Replace("#", string.Empty); + + foreach (MUXC.NavigationViewItem item in nv.MenuItems) + { + MUXC.NavigationViewItem sampleItem = item.MenuItems + .Cast() + .FirstOrDefault(i => i.Content.ToString().Contains(searchTerm)); + + if (sampleItem != null) + { + ShellNavigateTo( + (Uno.Gallery.Sample)sampleItem.DataContext +#if !WINDOWS + // workaround for uno#5069: setting NavView.SelectedItem at launch bricks it + , trySynchronizeCurrentItem: false +#endif + ); + return true; + } + } + } + return false; + } + private void OnCurrentSampleBackdoorChanged(DependencyObject sender, DependencyProperty dp) { var backdoorParts = _shell.CurrentSampleBackdoor.Split("-"); @@ -417,7 +450,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 } diff --git a/Uno.Gallery/Uno.Gallery.Skia.Gtk/Properties/launchSettings.json b/Uno.Gallery/Uno.Gallery.Skia.Gtk/Properties/launchSettings.json new file mode 100644 index 000000000..eb591c06a --- /dev/null +++ b/Uno.Gallery/Uno.Gallery.Skia.Gtk/Properties/launchSettings.json @@ -0,0 +1,10 @@ +{ + "profiles": { + "Uno.Gallery.Skia.Gtk": { + "commandName": "Project", + "commandLineArgs": "" + //To navigate to a specific sample on startup + //"commandLineArgs": "#CalendarDatePicker" + } + } +} diff --git a/Uno.Gallery/Uno.Gallery.Wasm/FragmentHavigationHandler.cs b/Uno.Gallery/Uno.Gallery.Wasm/FragmentHavigationHandler.cs new file mode 100644 index 000000000..da476a91d --- /dev/null +++ b/Uno.Gallery/Uno.Gallery.Wasm/FragmentHavigationHandler.cs @@ -0,0 +1,26 @@ +using Uno.Foundation; + +namespace Uno.Gallery.Wasm +{ + public class FragmentHavigationHandler + { + public static string CurrentFragment + { + get + { + const string command = "Uno.UI.FragmentNavigationHandler.getCurrentFragment()"; + var fragment = WebAssemblyRuntime.InvokeJS(command); + return fragment; + } + + set + { + var escaped = WebAssemblyRuntime.EscapeJs(value); + var command = + "Uno.UI.FragmentNavigationHandler.setCurrentFragment(\"" + escaped + "\")"; + + WebAssemblyRuntime.InvokeJS(command); + } + } + } +} diff --git a/Uno.Gallery/Uno.Gallery.Wasm/Uno.Gallery.Wasm.csproj b/Uno.Gallery/Uno.Gallery.Wasm/Uno.Gallery.Wasm.csproj index 92695c667..9cdb85514 100644 --- a/Uno.Gallery/Uno.Gallery.Wasm/Uno.Gallery.Wasm.csproj +++ b/Uno.Gallery/Uno.Gallery.Wasm/Uno.Gallery.Wasm.csproj @@ -146,4 +146,7 @@ + + <_Globbed_Compile Remove="FragmentHavigationHandler.cs" /> + diff --git a/Uno.Gallery/Uno.Gallery.Wasm/WasmScripts/UnoGallery.d.ts b/Uno.Gallery/Uno.Gallery.Wasm/WasmScripts/UnoGallery.d.ts index b56dfc32a..e7109846e 100644 --- a/Uno.Gallery/Uno.Gallery.Wasm/WasmScripts/UnoGallery.d.ts +++ b/Uno.Gallery/Uno.Gallery.Wasm/WasmScripts/UnoGallery.d.ts @@ -1,3 +1,15 @@ +declare namespace Uno.UI { + class FragmentNavigationHandler { + 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; diff --git a/Uno.Gallery/Uno.Gallery.Wasm/WasmScripts/UnoGallery.js b/Uno.Gallery/Uno.Gallery.Wasm/WasmScripts/UnoGallery.js index 64ff81268..32b970590 100644 --- a/Uno.Gallery/Uno.Gallery.Wasm/WasmScripts/UnoGallery.js +++ b/Uno.Gallery/Uno.Gallery.Wasm/WasmScripts/UnoGallery.js @@ -1,4 +1,49 @@ var Uno; +(function (Uno) { + var UI; + (function (UI) { + class FragmentNavigationHandler { + 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.UI.Wasm", "FragmentHavigationHandler"); + this.notifyFragmentChangedMethod = MonoRuntime.find_method(handlerClass, "NotifyFragmentChanged", -1); + } + } + FragmentNavigationHandler.subscribed = false; + UI.FragmentNavigationHandler = FragmentNavigationHandler; + })(UI = Uno.UI || (Uno.UI = {})); +})(Uno || (Uno = {})); +var Uno; (function (Uno) { var UI; (function (UI) { diff --git a/Uno.Gallery/Uno.Gallery.Wasm/ts/FragmentNavigation.ts b/Uno.Gallery/Uno.Gallery.Wasm/ts/FragmentNavigation.ts new file mode 100644 index 000000000..ddc8ee451 --- /dev/null +++ b/Uno.Gallery/Uno.Gallery.Wasm/ts/FragmentNavigation.ts @@ -0,0 +1,64 @@ +namespace Uno.UI { + export class FragmentNavigationHandler { + + private static currentFragment: string; + + public static getCurrentFragment(): string { + return window.location.hash; + } + + public static setCurrentFragment(fragment: string): string { + window.location.hash = fragment; + this.currentFragment = window.location.hash; + + return "ok"; + } + + private static subscribed: boolean = false; + + public static subscribeToFragmentChanged(): string { + + if (this.subscribed) { + return "already subscribed"; + } + + this.subscribed = true; + + this.currentFragment = this.getCurrentFragment(); + + window.addEventListener( + "hashchange", + _ => this.notifyFragmentChanged(), + false); + + return "ok"; + } + + private static notifyFragmentChangedMethod: any; + + private static notifyFragmentChanged() { + const newFragment: string = 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]); + } + + private static initializeMethods(): void { + if (this.notifyFragmentChangedMethod) { + return; // already initialized. + } + + const asm = MonoRuntime.assembly_load("Uno.Gallery.WASM"); + const handlerClass = MonoRuntime.find_class(asm, "Uno.UI.Wasm", "FragmentHavigationHandler"); + this.notifyFragmentChangedMethod = MonoRuntime.find_method(handlerClass, "NotifyFragmentChanged", -1); + } + } + + declare var MonoRuntime: any; +} From 790f2cb04cf1c4c09c673cee1cf6f7243ff0e98b Mon Sep 17 00:00:00 2001 From: Rafael Rosa Date: Wed, 20 Dec 2023 09:17:05 -0300 Subject: [PATCH 2/6] chore: delete profile sample --- .../Properties/launchSettings.json | 10 ---------- 1 file changed, 10 deletions(-) delete mode 100644 Uno.Gallery/Uno.Gallery.Skia.Gtk/Properties/launchSettings.json diff --git a/Uno.Gallery/Uno.Gallery.Skia.Gtk/Properties/launchSettings.json b/Uno.Gallery/Uno.Gallery.Skia.Gtk/Properties/launchSettings.json deleted file mode 100644 index eb591c06a..000000000 --- a/Uno.Gallery/Uno.Gallery.Skia.Gtk/Properties/launchSettings.json +++ /dev/null @@ -1,10 +0,0 @@ -{ - "profiles": { - "Uno.Gallery.Skia.Gtk": { - "commandName": "Project", - "commandLineArgs": "" - //To navigate to a specific sample on startup - //"commandLineArgs": "#CalendarDatePicker" - } - } -} From 693fdc652276cbdf3461413fe0b952e62fe3ccf4 Mon Sep 17 00:00:00 2001 From: Rafael Rosa Date: Wed, 20 Dec 2023 10:11:27 -0300 Subject: [PATCH 3/6] chore: redirect to root --- Uno.Gallery/Uno.Gallery.Shared/App.xaml.cs | 6 +- ...andler.cs => FragmentNavigationHandler.cs} | 6 +- .../LocationHrefNavigationHandler.cs | 26 ++++ .../Uno.Gallery.Wasm/Uno.Gallery.Wasm.csproj | 3 +- .../WasmScripts/UnoGallery.d.ts | 14 +- .../WasmScripts/UnoGallery.js | 127 ++++++++++++------ .../Uno.Gallery.Wasm/ts/FragmentNavigation.ts | 4 +- .../ts/LocationHrefNavigation.ts | 64 +++++++++ 8 files changed, 204 insertions(+), 46 deletions(-) rename Uno.Gallery/Uno.Gallery.Wasm/{FragmentHavigationHandler.cs => FragmentNavigationHandler.cs} (60%) create mode 100644 Uno.Gallery/Uno.Gallery.Wasm/LocationHrefNavigationHandler.cs create mode 100644 Uno.Gallery/Uno.Gallery.Wasm/ts/LocationHrefNavigation.ts diff --git a/Uno.Gallery/Uno.Gallery.Shared/App.xaml.cs b/Uno.Gallery/Uno.Gallery.Shared/App.xaml.cs index 68dee6956..ad4d7d966 100644 --- a/Uno.Gallery/Uno.Gallery.Shared/App.xaml.cs +++ b/Uno.Gallery/Uno.Gallery.Shared/App.xaml.cs @@ -251,7 +251,7 @@ private Shell BuildShell(LaunchActivatedEventArgs e) private bool IsThereSampleFilteredByArgs(MUXC.NavigationView nv, LaunchActivatedEventArgs e) { #if __WASM__ - var argumentsHash = Uno.Gallery.Wasm.FragmentHavigationHandler.CurrentFragment; + var argumentsHash = Wasm.FragmentNavigationHandler.CurrentFragment; #else var argumentsHash = e.Arguments; #endif @@ -277,6 +277,10 @@ private bool IsThereSampleFilteredByArgs(MUXC.NavigationView nv, LaunchActivated return true; } } +#if __WASM__ + //If there is a Hash that is not valid, redirect it to the root of the site. + Wasm.LocationHrefNavigationHandler.CurrentLocationHref = "/"; +#endif } return false; } diff --git a/Uno.Gallery/Uno.Gallery.Wasm/FragmentHavigationHandler.cs b/Uno.Gallery/Uno.Gallery.Wasm/FragmentNavigationHandler.cs similarity index 60% rename from Uno.Gallery/Uno.Gallery.Wasm/FragmentHavigationHandler.cs rename to Uno.Gallery/Uno.Gallery.Wasm/FragmentNavigationHandler.cs index da476a91d..e4ed67cc1 100644 --- a/Uno.Gallery/Uno.Gallery.Wasm/FragmentHavigationHandler.cs +++ b/Uno.Gallery/Uno.Gallery.Wasm/FragmentNavigationHandler.cs @@ -2,13 +2,13 @@ namespace Uno.Gallery.Wasm { - public class FragmentHavigationHandler + public class FragmentNavigationHandler { public static string CurrentFragment { get { - const string command = "Uno.UI.FragmentNavigationHandler.getCurrentFragment()"; + const string command = "Uno.Gallery.Wasm.FragmentNavigationHandler.getCurrentFragment()"; var fragment = WebAssemblyRuntime.InvokeJS(command); return fragment; } @@ -17,7 +17,7 @@ public static string CurrentFragment { var escaped = WebAssemblyRuntime.EscapeJs(value); var command = - "Uno.UI.FragmentNavigationHandler.setCurrentFragment(\"" + escaped + "\")"; + "Uno.Gallery.Wasm.FragmentNavigationHandler.setCurrentFragment(\"" + escaped + "\")"; WebAssemblyRuntime.InvokeJS(command); } diff --git a/Uno.Gallery/Uno.Gallery.Wasm/LocationHrefNavigationHandler.cs b/Uno.Gallery/Uno.Gallery.Wasm/LocationHrefNavigationHandler.cs new file mode 100644 index 000000000..964ed768e --- /dev/null +++ b/Uno.Gallery/Uno.Gallery.Wasm/LocationHrefNavigationHandler.cs @@ -0,0 +1,26 @@ +using Uno.Foundation; + +namespace Uno.Gallery.Wasm +{ + public class LocationHrefNavigationHandler + { + public static string CurrentLocationHref + { + get + { + const string command = "Uno.Gallery.Wasm.LocationHrefNavigationHandler.getCurrentLocationHref()"; + var locationHref = WebAssemblyRuntime.InvokeJS(command); + return locationHref; + } + + set + { + var escaped = WebAssemblyRuntime.EscapeJs(value); + var command = + "Uno.Gallery.Wasm.LocationHrefNavigationHandler.setCurrentLocationHref(\"" + escaped + "\")"; + + WebAssemblyRuntime.InvokeJS(command); + } + } + } +} diff --git a/Uno.Gallery/Uno.Gallery.Wasm/Uno.Gallery.Wasm.csproj b/Uno.Gallery/Uno.Gallery.Wasm/Uno.Gallery.Wasm.csproj index 9cdb85514..69c5d862f 100644 --- a/Uno.Gallery/Uno.Gallery.Wasm/Uno.Gallery.Wasm.csproj +++ b/Uno.Gallery/Uno.Gallery.Wasm/Uno.Gallery.Wasm.csproj @@ -147,6 +147,7 @@ - <_Globbed_Compile Remove="FragmentHavigationHandler.cs" /> + <_Globbed_Compile Remove="FragmentNavigationHandler.cs" /> + <_Globbed_Compile Remove="LocationHrefNavigationHandler.cs" /> diff --git a/Uno.Gallery/Uno.Gallery.Wasm/WasmScripts/UnoGallery.d.ts b/Uno.Gallery/Uno.Gallery.Wasm/WasmScripts/UnoGallery.d.ts index e7109846e..44dadff72 100644 --- a/Uno.Gallery/Uno.Gallery.Wasm/WasmScripts/UnoGallery.d.ts +++ b/Uno.Gallery/Uno.Gallery.Wasm/WasmScripts/UnoGallery.d.ts @@ -1,4 +1,4 @@ -declare namespace Uno.UI { +declare namespace Uno.Gallery.Wasm { class FragmentNavigationHandler { private static currentFragment; static getCurrentFragment(): string; @@ -17,3 +17,15 @@ declare namespace Uno.UI.Demo { private static init; } } +declare namespace Uno.Gallery.Wasm { + class LocationHrefNavigationHandler { + 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; + } +} diff --git a/Uno.Gallery/Uno.Gallery.Wasm/WasmScripts/UnoGallery.js b/Uno.Gallery/Uno.Gallery.Wasm/WasmScripts/UnoGallery.js index 32b970590..4f16bb479 100644 --- a/Uno.Gallery/Uno.Gallery.Wasm/WasmScripts/UnoGallery.js +++ b/Uno.Gallery/Uno.Gallery.Wasm/WasmScripts/UnoGallery.js @@ -1,47 +1,50 @@ var Uno; (function (Uno) { - var UI; - (function (UI) { - class FragmentNavigationHandler { - 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"; + var Gallery; + (function (Gallery) { + var Wasm; + (function (Wasm) { + class FragmentNavigationHandler { + static getCurrentFragment() { + return window.location.hash; } - 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 + static setCurrentFragment(fragment) { + window.location.hash = fragment; + this.currentFragment = window.location.hash; + return "ok"; } - 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. + 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.UI.Wasm", "FragmentNavigationHandler"); + this.notifyFragmentChangedMethod = MonoRuntime.find_method(handlerClass, "NotifyFragmentChanged", -1); } - const asm = MonoRuntime.assembly_load("Uno.Gallery.WASM"); - const handlerClass = MonoRuntime.find_class(asm, "Uno.UI.Wasm", "FragmentHavigationHandler"); - this.notifyFragmentChangedMethod = MonoRuntime.find_method(handlerClass, "NotifyFragmentChanged", -1); } - } - FragmentNavigationHandler.subscribed = false; - UI.FragmentNavigationHandler = FragmentNavigationHandler; - })(UI = Uno.UI || (Uno.UI = {})); + FragmentNavigationHandler.subscribed = false; + Wasm.FragmentNavigationHandler = FragmentNavigationHandler; + })(Wasm = Gallery.Wasm || (Gallery.Wasm = {})); + })(Gallery = Uno.Gallery || (Uno.Gallery = {})); })(Uno || (Uno = {})); var Uno; (function (Uno) { @@ -93,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 LocationHrefNavigationHandler { + 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.UI.Wasm", "LocationHrefHavigationHandler"); + this.notifyLocationHrefChangedMethod = MonoRuntime.find_method(handlerClass, "NotifyLocationHrefChanged", -1); + } + } + LocationHrefNavigationHandler.subscribed = false; + Wasm.LocationHrefNavigationHandler = LocationHrefNavigationHandler; + })(Wasm = Gallery.Wasm || (Gallery.Wasm = {})); + })(Gallery = Uno.Gallery || (Uno.Gallery = {})); +})(Uno || (Uno = {})); diff --git a/Uno.Gallery/Uno.Gallery.Wasm/ts/FragmentNavigation.ts b/Uno.Gallery/Uno.Gallery.Wasm/ts/FragmentNavigation.ts index ddc8ee451..ff3359adc 100644 --- a/Uno.Gallery/Uno.Gallery.Wasm/ts/FragmentNavigation.ts +++ b/Uno.Gallery/Uno.Gallery.Wasm/ts/FragmentNavigation.ts @@ -1,4 +1,4 @@ -namespace Uno.UI { +namespace Uno.Gallery.Wasm { export class FragmentNavigationHandler { private static currentFragment: string; @@ -55,7 +55,7 @@ } const asm = MonoRuntime.assembly_load("Uno.Gallery.WASM"); - const handlerClass = MonoRuntime.find_class(asm, "Uno.UI.Wasm", "FragmentHavigationHandler"); + const handlerClass = MonoRuntime.find_class(asm, "Uno.UI.Wasm", "FragmentNavigationHandler"); this.notifyFragmentChangedMethod = MonoRuntime.find_method(handlerClass, "NotifyFragmentChanged", -1); } } diff --git a/Uno.Gallery/Uno.Gallery.Wasm/ts/LocationHrefNavigation.ts b/Uno.Gallery/Uno.Gallery.Wasm/ts/LocationHrefNavigation.ts new file mode 100644 index 000000000..54a5372ce --- /dev/null +++ b/Uno.Gallery/Uno.Gallery.Wasm/ts/LocationHrefNavigation.ts @@ -0,0 +1,64 @@ +namespace Uno.Gallery.Wasm { + export class LocationHrefNavigationHandler { + + private static currentLocationHref: string; + + public static getCurrentLocationHref(): string { + return window.location.href; + } + + public static setCurrentLocationHref(locationHref: string): string { + window.location.href = locationHref; + this.currentLocationHref = window.location.href; + + return "ok"; + } + + private static subscribed: boolean = false; + + public static subscribeToLocationHrefChanged(): string { + + if (this.subscribed) { + return "already subscribed"; + } + + this.subscribed = true; + + this.currentLocationHref = this.getCurrentLocationHref(); + + window.addEventListener( + "hashchange", + _ => this.notifyLocationHrefChanged(), + false); + + return "ok"; + } + + private static notifyLocationHrefChangedMethod: any; + + private static notifyLocationHrefChanged() { + const newLocationHref: string = 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]); + } + + private static initializeMethods(): void { + if (this.notifyLocationHrefChangedMethod) { + return; // already initialized. + } + + const asm = MonoRuntime.assembly_load("Uno.Gallery.WASM"); + const handlerClass = MonoRuntime.find_class(asm, "Uno.UI.Wasm", "LocationHrefHavigationHandler"); + this.notifyLocationHrefChangedMethod = MonoRuntime.find_method(handlerClass, "NotifyLocationHrefChanged", -1); + } + } + + declare var MonoRuntime: any; +} From 296d4f97d7839d92db9d55ae679bb6e70cb9aad1 Mon Sep 17 00:00:00 2001 From: Rafael Rosa Date: Tue, 2 Jan 2024 09:16:41 -0300 Subject: [PATCH 4/6] chore: changed to JSImport --- .../FragmentNavigationHandler.Interop.wasm.cs | 18 ++++++++++++++++++ .../FragmentNavigationHandler.cs | 11 +++-------- ...cationHrefNavigationHandler.Interop.wasm.cs | 18 ++++++++++++++++++ .../LocationHrefNavigationHandler.cs | 11 +++-------- .../Uno.Gallery.Wasm/Uno.Gallery.Wasm.csproj | 2 ++ .../WasmScripts/UnoGallery.d.ts | 4 ++-- .../Uno.Gallery.Wasm/WasmScripts/UnoGallery.js | 16 ++++++++-------- .../Uno.Gallery.Wasm/ts/FragmentNavigation.ts | 4 ++-- .../ts/LocationHrefNavigation.ts | 4 ++-- 9 files changed, 58 insertions(+), 30 deletions(-) create mode 100644 Uno.Gallery/Uno.Gallery.Wasm/FragmentNavigationHandler.Interop.wasm.cs create mode 100644 Uno.Gallery/Uno.Gallery.Wasm/LocationHrefNavigationHandler.Interop.wasm.cs diff --git a/Uno.Gallery/Uno.Gallery.Wasm/FragmentNavigationHandler.Interop.wasm.cs b/Uno.Gallery/Uno.Gallery.Wasm/FragmentNavigationHandler.Interop.wasm.cs new file mode 100644 index 000000000..c10bf9b2c --- /dev/null +++ b/Uno.Gallery/Uno.Gallery.Wasm/FragmentNavigationHandler.Interop.wasm.cs @@ -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); + } + } +} diff --git a/Uno.Gallery/Uno.Gallery.Wasm/FragmentNavigationHandler.cs b/Uno.Gallery/Uno.Gallery.Wasm/FragmentNavigationHandler.cs index e4ed67cc1..69feed713 100644 --- a/Uno.Gallery/Uno.Gallery.Wasm/FragmentNavigationHandler.cs +++ b/Uno.Gallery/Uno.Gallery.Wasm/FragmentNavigationHandler.cs @@ -2,24 +2,19 @@ namespace Uno.Gallery.Wasm { - public class FragmentNavigationHandler + internal partial class FragmentNavigationHandler { public static string CurrentFragment { get { - const string command = "Uno.Gallery.Wasm.FragmentNavigationHandler.getCurrentFragment()"; - var fragment = WebAssemblyRuntime.InvokeJS(command); - return fragment; + return NativeMethods.GetCurrentFragment(); } set { var escaped = WebAssemblyRuntime.EscapeJs(value); - var command = - "Uno.Gallery.Wasm.FragmentNavigationHandler.setCurrentFragment(\"" + escaped + "\")"; - - WebAssemblyRuntime.InvokeJS(command); + NativeMethods.SetCurrentFragment(escaped); } } } diff --git a/Uno.Gallery/Uno.Gallery.Wasm/LocationHrefNavigationHandler.Interop.wasm.cs b/Uno.Gallery/Uno.Gallery.Wasm/LocationHrefNavigationHandler.Interop.wasm.cs new file mode 100644 index 000000000..7fcfb7d4e --- /dev/null +++ b/Uno.Gallery/Uno.Gallery.Wasm/LocationHrefNavigationHandler.Interop.wasm.cs @@ -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); + } + } +} diff --git a/Uno.Gallery/Uno.Gallery.Wasm/LocationHrefNavigationHandler.cs b/Uno.Gallery/Uno.Gallery.Wasm/LocationHrefNavigationHandler.cs index 964ed768e..c2bd9a2c7 100644 --- a/Uno.Gallery/Uno.Gallery.Wasm/LocationHrefNavigationHandler.cs +++ b/Uno.Gallery/Uno.Gallery.Wasm/LocationHrefNavigationHandler.cs @@ -2,24 +2,19 @@ namespace Uno.Gallery.Wasm { - public class LocationHrefNavigationHandler + internal partial class LocationHrefNavigationHandler { public static string CurrentLocationHref { get { - const string command = "Uno.Gallery.Wasm.LocationHrefNavigationHandler.getCurrentLocationHref()"; - var locationHref = WebAssemblyRuntime.InvokeJS(command); - return locationHref; + return NativeMethods.GetCurrentLocationHref(); } set { var escaped = WebAssemblyRuntime.EscapeJs(value); - var command = - "Uno.Gallery.Wasm.LocationHrefNavigationHandler.setCurrentLocationHref(\"" + escaped + "\")"; - - WebAssemblyRuntime.InvokeJS(command); + NativeMethods.SetCurrentLocationHref(escaped); } } } diff --git a/Uno.Gallery/Uno.Gallery.Wasm/Uno.Gallery.Wasm.csproj b/Uno.Gallery/Uno.Gallery.Wasm/Uno.Gallery.Wasm.csproj index 69c5d862f..199285beb 100644 --- a/Uno.Gallery/Uno.Gallery.Wasm/Uno.Gallery.Wasm.csproj +++ b/Uno.Gallery/Uno.Gallery.Wasm/Uno.Gallery.Wasm.csproj @@ -148,6 +148,8 @@ <_Globbed_Compile Remove="FragmentNavigationHandler.cs" /> + <_Globbed_Compile Remove="FragmentNavigationHandler.Interop.wasm.cs" /> <_Globbed_Compile Remove="LocationHrefNavigationHandler.cs" /> + <_Globbed_Compile Remove="LocationHrefNavigationHandler.Interop.wasm.cs" /> diff --git a/Uno.Gallery/Uno.Gallery.Wasm/WasmScripts/UnoGallery.d.ts b/Uno.Gallery/Uno.Gallery.Wasm/WasmScripts/UnoGallery.d.ts index 44dadff72..fd22bc2dd 100644 --- a/Uno.Gallery/Uno.Gallery.Wasm/WasmScripts/UnoGallery.d.ts +++ b/Uno.Gallery/Uno.Gallery.Wasm/WasmScripts/UnoGallery.d.ts @@ -1,5 +1,5 @@ declare namespace Uno.Gallery.Wasm { - class FragmentNavigationHandler { + class FragmentNavigation { private static currentFragment; static getCurrentFragment(): string; static setCurrentFragment(fragment: string): string; @@ -18,7 +18,7 @@ declare namespace Uno.UI.Demo { } } declare namespace Uno.Gallery.Wasm { - class LocationHrefNavigationHandler { + class LocationHrefNavigation { private static currentLocationHref; static getCurrentLocationHref(): string; static setCurrentLocationHref(locationHref: string): string; diff --git a/Uno.Gallery/Uno.Gallery.Wasm/WasmScripts/UnoGallery.js b/Uno.Gallery/Uno.Gallery.Wasm/WasmScripts/UnoGallery.js index 4f16bb479..d969ec84f 100644 --- a/Uno.Gallery/Uno.Gallery.Wasm/WasmScripts/UnoGallery.js +++ b/Uno.Gallery/Uno.Gallery.Wasm/WasmScripts/UnoGallery.js @@ -4,7 +4,7 @@ var Uno; (function (Gallery) { var Wasm; (function (Wasm) { - class FragmentNavigationHandler { + class FragmentNavigation { static getCurrentFragment() { return window.location.hash; } @@ -37,12 +37,12 @@ var Uno; return; // already initialized. } const asm = MonoRuntime.assembly_load("Uno.Gallery.WASM"); - const handlerClass = MonoRuntime.find_class(asm, "Uno.UI.Wasm", "FragmentNavigationHandler"); + const handlerClass = MonoRuntime.find_class(asm, "Uno.Gallery.Wasm", "FragmentNavigation"); this.notifyFragmentChangedMethod = MonoRuntime.find_method(handlerClass, "NotifyFragmentChanged", -1); } } - FragmentNavigationHandler.subscribed = false; - Wasm.FragmentNavigationHandler = FragmentNavigationHandler; + FragmentNavigation.subscribed = false; + Wasm.FragmentNavigation = FragmentNavigation; })(Wasm = Gallery.Wasm || (Gallery.Wasm = {})); })(Gallery = Uno.Gallery || (Uno.Gallery = {})); })(Uno || (Uno = {})); @@ -102,7 +102,7 @@ var Uno; (function (Gallery) { var Wasm; (function (Wasm) { - class LocationHrefNavigationHandler { + class LocationHrefNavigation { static getCurrentLocationHref() { return window.location.href; } @@ -135,12 +135,12 @@ var Uno; return; // already initialized. } const asm = MonoRuntime.assembly_load("Uno.Gallery.WASM"); - const handlerClass = MonoRuntime.find_class(asm, "Uno.UI.Wasm", "LocationHrefHavigationHandler"); + const handlerClass = MonoRuntime.find_class(asm, "Uno.Gallery.Wasm", "LocationHrefHavigation"); this.notifyLocationHrefChangedMethod = MonoRuntime.find_method(handlerClass, "NotifyLocationHrefChanged", -1); } } - LocationHrefNavigationHandler.subscribed = false; - Wasm.LocationHrefNavigationHandler = LocationHrefNavigationHandler; + LocationHrefNavigation.subscribed = false; + Wasm.LocationHrefNavigation = LocationHrefNavigation; })(Wasm = Gallery.Wasm || (Gallery.Wasm = {})); })(Gallery = Uno.Gallery || (Uno.Gallery = {})); })(Uno || (Uno = {})); diff --git a/Uno.Gallery/Uno.Gallery.Wasm/ts/FragmentNavigation.ts b/Uno.Gallery/Uno.Gallery.Wasm/ts/FragmentNavigation.ts index ff3359adc..93cf7b6ad 100644 --- a/Uno.Gallery/Uno.Gallery.Wasm/ts/FragmentNavigation.ts +++ b/Uno.Gallery/Uno.Gallery.Wasm/ts/FragmentNavigation.ts @@ -1,5 +1,5 @@ namespace Uno.Gallery.Wasm { - export class FragmentNavigationHandler { + export class FragmentNavigation { private static currentFragment: string; @@ -55,7 +55,7 @@ } const asm = MonoRuntime.assembly_load("Uno.Gallery.WASM"); - const handlerClass = MonoRuntime.find_class(asm, "Uno.UI.Wasm", "FragmentNavigationHandler"); + const handlerClass = MonoRuntime.find_class(asm, "Uno.Gallery.Wasm", "FragmentNavigation"); this.notifyFragmentChangedMethod = MonoRuntime.find_method(handlerClass, "NotifyFragmentChanged", -1); } } diff --git a/Uno.Gallery/Uno.Gallery.Wasm/ts/LocationHrefNavigation.ts b/Uno.Gallery/Uno.Gallery.Wasm/ts/LocationHrefNavigation.ts index 54a5372ce..22975f98a 100644 --- a/Uno.Gallery/Uno.Gallery.Wasm/ts/LocationHrefNavigation.ts +++ b/Uno.Gallery/Uno.Gallery.Wasm/ts/LocationHrefNavigation.ts @@ -1,5 +1,5 @@ namespace Uno.Gallery.Wasm { - export class LocationHrefNavigationHandler { + export class LocationHrefNavigation { private static currentLocationHref: string; @@ -55,7 +55,7 @@ } const asm = MonoRuntime.assembly_load("Uno.Gallery.WASM"); - const handlerClass = MonoRuntime.find_class(asm, "Uno.UI.Wasm", "LocationHrefHavigationHandler"); + const handlerClass = MonoRuntime.find_class(asm, "Uno.Gallery.Wasm", "LocationHrefHavigation"); this.notifyLocationHrefChangedMethod = MonoRuntime.find_method(handlerClass, "NotifyLocationHrefChanged", -1); } } From a2095df7d9cf1c1611d84529185c0f4dddc3aee9 Mon Sep 17 00:00:00 2001 From: Rafael Rosa Date: Wed, 3 Jan 2024 11:08:46 -0300 Subject: [PATCH 5/6] chore: keep just for wasm --- Uno.Gallery/Uno.Gallery.Shared/App.xaml.cs | 30 ++++++++-------------- 1 file changed, 10 insertions(+), 20 deletions(-) diff --git a/Uno.Gallery/Uno.Gallery.Shared/App.xaml.cs b/Uno.Gallery/Uno.Gallery.Shared/App.xaml.cs index ad4d7d966..6a4ddbbc0 100644 --- a/Uno.Gallery/Uno.Gallery.Shared/App.xaml.cs +++ b/Uno.Gallery/Uno.Gallery.Shared/App.xaml.cs @@ -29,8 +29,6 @@ public partial class App : Application public Window MainWindow { get; private set; } - private static Sample[] _samples; - private Shell _shell; /// @@ -67,11 +65,10 @@ protected override void OnLaunched(LaunchActivatedEventArgs e) base.OnLaunched(e); this.Log().Debug("Launched app."); - this.Log().Debug(e.Arguments.ToString()); - OnLaunchedOrActivated(e); + OnLaunchedOrActivated(); } - private void OnLaunchedOrActivated(LaunchActivatedEventArgs e) + private void OnLaunchedOrActivated() { #if WINDOWS && !HAS_UNO MainWindow = new Window(); @@ -87,7 +84,7 @@ private void OnLaunchedOrActivated(LaunchActivatedEventArgs e) Xamarin.Calabash.Start(); #endif - MainWindow.Content = _shell = BuildShell(e); + MainWindow.Content = _shell = BuildShell(); } // Ensure the current window is active @@ -223,15 +220,16 @@ public void SearchShellNavigateTo(Sample sample) _shell.NavigationView.Content = page; } - private Shell BuildShell(LaunchActivatedEventArgs e) + private Shell BuildShell() { _shell = new Shell(); AutomationProperties.SetAutomationId(_shell, "AppShell"); _shell.RegisterPropertyChangedCallback(Shell.CurrentSampleBackdoorProperty, OnCurrentSampleBackdoorChanged); var nv = _shell.NavigationView; AddNavigationItems(nv); - - if (!IsThereSampleFilteredByArgs(nv, e)) +#if __WASM__ + if (!IsThereSampleFilteredByArgs(nv)) +#endif { // landing navigation ShellNavigateTo( @@ -247,14 +245,10 @@ private Shell BuildShell(LaunchActivatedEventArgs e) return _shell; } - - private bool IsThereSampleFilteredByArgs(MUXC.NavigationView nv, LaunchActivatedEventArgs e) - { #if __WASM__ + private bool IsThereSampleFilteredByArgs(MUXC.NavigationView nv) + { var argumentsHash = Wasm.FragmentNavigationHandler.CurrentFragment; -#else - var argumentsHash = e.Arguments; -#endif if (argumentsHash.Contains("#")) { string searchTerm = (argumentsHash + string.Empty).Replace("#", string.Empty); @@ -269,21 +263,17 @@ private bool IsThereSampleFilteredByArgs(MUXC.NavigationView nv, LaunchActivated { ShellNavigateTo( (Uno.Gallery.Sample)sampleItem.DataContext -#if !WINDOWS - // workaround for uno#5069: setting NavView.SelectedItem at launch bricks it , trySynchronizeCurrentItem: false -#endif ); return true; } } -#if __WASM__ //If there is a Hash that is not valid, redirect it to the root of the site. Wasm.LocationHrefNavigationHandler.CurrentLocationHref = "/"; -#endif } return false; } +#endif private void OnCurrentSampleBackdoorChanged(DependencyObject sender, DependencyProperty dp) { From c11b53e31856f3324ff06239f6f850dc3c5f72a3 Mon Sep 17 00:00:00 2001 From: Rafael Rosa Date: Tue, 30 Jan 2024 16:17:20 -0300 Subject: [PATCH 6/6] chore: allow IgnoreCase --- Uno.Gallery/Uno.Gallery.Shared/App.xaml.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Uno.Gallery/Uno.Gallery.Shared/App.xaml.cs b/Uno.Gallery/Uno.Gallery.Shared/App.xaml.cs index 6a4ddbbc0..641230103 100644 --- a/Uno.Gallery/Uno.Gallery.Shared/App.xaml.cs +++ b/Uno.Gallery/Uno.Gallery.Shared/App.xaml.cs @@ -257,7 +257,7 @@ private bool IsThereSampleFilteredByArgs(MUXC.NavigationView nv) { MUXC.NavigationViewItem sampleItem = item.MenuItems .Cast() - .FirstOrDefault(i => i.Content.ToString().Contains(searchTerm)); + .FirstOrDefault(i => i.Content.ToString().Contains(searchTerm, StringComparison.InvariantCultureIgnoreCase)); if (sampleItem != null) {