Skip to content

Commit

Permalink
feat: App/Window activation and visibility on WPF
Browse files Browse the repository at this point in the history
  • Loading branch information
MartinZikmund committed May 22, 2021
1 parent 8b7914f commit 2a7959c
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 5 deletions.
2 changes: 1 addition & 1 deletion src/Uno.UI.Runtime.Skia.Wpf/Uno.UI.Runtime.Skia.Wpf.csproj
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<Project Sdk="MSBuild.Sdk.Extras">

<PropertyGroup>
<TargetFrameworks>net47;netcoreapp3.1</TargetFrameworks>
<TargetFrameworks>net47;netcoreapp3.1;net5.0-windows</TargetFrameworks>
</PropertyGroup>

<Import Project="../netcore-build-windows.props" />
Expand Down
30 changes: 30 additions & 0 deletions src/Uno.UI.Runtime.Skia.Wpf/WpfHost.cs
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,10 @@ bool EnqueueNative(DispatcherQueuePriority priority, DispatcherQueueHandler call

WinUI.Window.InvalidateRender += () => InvalidateVisual();

WpfApplication.Current.Activated += Current_Activated;
WpfApplication.Current.Deactivated += Current_Deactivated;
WpfApplication.Current.MainWindow.StateChanged += MainWindow_StateChanged;

SizeChanged += WpfHost_SizeChanged;
Loaded += WpfHost_Loaded;
}
Expand All @@ -119,6 +123,32 @@ public override void OnApplyTemplate()
_nativeOverlayLayer = GetTemplateChild(NativeOverlayLayerPart) as WpfCanvas;
}

private void MainWindow_StateChanged(object? sender, EventArgs e)
{
var wpfWindow = WpfApplication.Current.MainWindow;
var winUIWindow = WinUI.Window.Current;
var isVisible = wpfWindow.WindowState != WindowState.Minimized;
winUIWindow.OnVisibilityChanged(isVisible);
}

private void Current_Deactivated(object? sender, EventArgs e)
{
var winUIWindow = WinUI.Window.Current;
winUIWindow?.OnActivated(Windows.UI.Core.CoreWindowActivationState.Deactivated);

var application = WinUI.Application.Current;
application?.OnEnteredBackground();
}

private void Current_Activated(object? sender, EventArgs e)
{
var application = WinUI.Application.Current;
application?.OnLeavingBackground();

var winUIWindow = WinUI.Window.Current;
winUIWindow?.OnActivated(Windows.UI.Core.CoreWindowActivationState.CodeActivated);
}

private void WpfHost_Loaded(object sender, RoutedEventArgs e)
{
WinUI.Window.Current.OnNativeSizeChanged(new Windows.Foundation.Size(ActualWidth, ActualHeight));
Expand Down
11 changes: 7 additions & 4 deletions src/Uno.UI/UI/Xaml/Window.cs
Original file line number Diff line number Diff line change
Expand Up @@ -163,11 +163,14 @@ internal void OnActivated(CoreWindowActivationState state)

internal void OnVisibilityChanged(bool newVisibility)
{
Visible = newVisibility;
if (Visible != newVisibility)
{
Visible = newVisibility;

var args = new VisibilityChangedEventArgs();
CoreWindow.OnVisibilityChanged(args);
VisibilityChanged?.Invoke(this, args);
var args = new VisibilityChangedEventArgs();
CoreWindow.OnVisibilityChanged(args);
VisibilityChanged?.Invoke(this, args);
}
}

private void RootSizeChanged(object sender, SizeChangedEventArgs args)
Expand Down

0 comments on commit 2a7959c

Please sign in to comment.