Skip to content

Commit

Permalink
feat: App/Window activation and visibility on WASM
Browse files Browse the repository at this point in the history
  • Loading branch information
MartinZikmund committed May 22, 2021
1 parent 5dbf801 commit 8b7914f
Show file tree
Hide file tree
Showing 7 changed files with 60 additions and 6 deletions.
3 changes: 2 additions & 1 deletion src/Uno.UI/LinkerDefinition.Wasm.xml
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,8 @@
</type>
<type fullname="Windows.UI.Xaml.Application">
<method name="DispatchSuspending" />
<method name="DispatchSystemThemeChange" />
<method name="DispatchSystemThemeChange" />
<method name="DispatchVisibilityChange" />
</type>
</assembly>

Expand Down
29 changes: 28 additions & 1 deletion src/Uno.UI/UI/Xaml/Application.wasm.cs
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,8 @@ public Application()
Package.SetEntryAssembly(this.GetType().Assembly);

CoreDispatcher.Main.RunAsync(CoreDispatcherPriority.Normal, Initialize);

ObserveApplicationVisibility();
}

[Preserve]
Expand All @@ -53,6 +55,27 @@ public static int DispatchSystemThemeChange()
return 0;
}

[Preserve]
public static int DispatchVisibilityChange(bool isVisible)
{
var application = Windows.UI.Xaml.Application.Current;
var window = Windows.UI.Xaml.Window.Current;
if (isVisible)
{
application?.LeavingBackground?.Invoke(application, new LeavingBackgroundEventArgs());
window?.OnVisibilityChanged(true);
window?.OnActivated(CoreWindowActivationState.CodeActivated);
}
else
{
window?.OnActivated(CoreWindowActivationState.Deactivated);
window?.OnVisibilityChanged(false);
application?.EnteredBackground?.Invoke(application, new EnteredBackgroundEventArgs());
}

return 0;
}

static partial void StartPartial(ApplicationInitializationCallback callback)
{
_startInvoked = true;
Expand All @@ -74,7 +97,6 @@ public static int DispatchSystemThemeChange()
WebAssemblyRuntime.InvokeJS("Windows.UI.Xaml.Application.observeSystemTheme()");
}


private void Initialize()
{
using (WritePhaseEventTrace(TraceProvider.LauchedStart, TraceProvider.LauchedStop))
Expand Down Expand Up @@ -124,6 +146,11 @@ internal static void DispatchSuspending()
this.Log().LogWarning($"This platform does not support asynchronous Suspending deferral. Code executed after the of the method called by Suspending may not get executed.");
}
}

private void ObserveApplicationVisibility()
{
WebAssemblyRuntime.InvokeJS("Windows.UI.Xaml.Application.observeVisibility()");
}
}
}
#endif
2 changes: 2 additions & 0 deletions src/Uno.UI/WasmScripts/Uno.UI.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1116,8 +1116,10 @@ declare namespace Windows.UI.ViewManagement {
declare namespace Windows.UI.Xaml {
class Application {
private static dispatchThemeChange;
private static dispatchVisibilityChange;
static getDefaultSystemTheme(): string;
static observeSystemTheme(): void;
static observeVisibility(): void;
}
}
declare namespace Windows.UI.Xaml {
Expand Down
8 changes: 8 additions & 0 deletions src/Uno.UI/WasmScripts/Uno.UI.js
Original file line number Diff line number Diff line change
Expand Up @@ -4037,6 +4037,14 @@ var Windows;
});
}
}
static observeVisibility() {
if (!this.dispatchVisibilityChange) {
this.dispatchVisibilityChange = Module.mono_bind_static_method("[Uno.UI] Windows.UI.Xaml.Application:DispatchVisibilityChange");
}
document.addEventListener("visibilitychange", () => {
Application.dispatchVisibilityChange(document.visibilityState == "visible");
});
}
}
Xaml.Application = Application;
})(Xaml = UI.Xaml || (UI.Xaml = {}));
Expand Down
18 changes: 14 additions & 4 deletions src/Uno.UI/ts/Windows/UI/Xaml/Application.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

export class Application {
private static dispatchThemeChange: () => number;
private static dispatchVisibilityChange: (isVisible: boolean) => number;

public static getDefaultSystemTheme(): string {
if (window.matchMedia) {
Expand All @@ -13,19 +14,28 @@
}
}
return null;
}
}

public static observeSystemTheme() {
if (!this.dispatchThemeChange) {
this.dispatchThemeChange = (<any>Module).mono_bind_static_method("[Uno.UI] Windows.UI.Xaml.Application:DispatchSystemThemeChange");
}

if (window.matchMedia)
{
if (window.matchMedia) {
window.matchMedia('(prefers-color-scheme: dark)').addEventListener("change", () => {
Application.dispatchThemeChange();
});
}
}
}

public static observeVisibility() {
if (!this.dispatchVisibilityChange) {
this.dispatchVisibilityChange = (<any>Module).mono_bind_static_method("[Uno.UI] Windows.UI.Xaml.Application:DispatchVisibilityChange");
}

document.addEventListener("visibilitychange", () => {
Application.dispatchVisibilityChange(document.visibilityState == "visible");
});
}
}
}
3 changes: 3 additions & 0 deletions src/Uno.UWP/ApplicationModel/EnteredBackgroundEventArgs.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,8 @@ namespace Windows.ApplicationModel
{
public partial class EnteredBackgroundEventArgs : IEnteredBackgroundEventArgs
{
internal EnteredBackgroundEventArgs()
{
}
}
}
3 changes: 3 additions & 0 deletions src/Uno.UWP/ApplicationModel/LeavingBackgroundEventArgs.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,8 @@ namespace Windows.ApplicationModel
{
public partial class LeavingBackgroundEventArgs : ILeavingBackgroundEventArgs
{
internal LeavingBackgroundEventArgs()
{
}
}
}

0 comments on commit 8b7914f

Please sign in to comment.