Skip to content

Commit

Permalink
feat: App/Window activation and visibility on GTK
Browse files Browse the repository at this point in the history
  • Loading branch information
MartinZikmund committed May 22, 2021
1 parent 2a7959c commit 7b711d2
Show file tree
Hide file tree
Showing 3 changed files with 74 additions and 5 deletions.
45 changes: 45 additions & 0 deletions src/Uno.UI.Runtime.Skia.Gtk/GtkHost.cs
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,8 @@ void Dispatch(System.Action d)
WUX.Window.Current.OnNativeSizeChanged(new Windows.Foundation.Size(e.Allocation.Width, e.Allocation.Height));
};

_window.WindowStateEvent += OnWindowStateChanged;

var overlay = new Overlay();

_eventBox = new EventBox();
Expand Down Expand Up @@ -138,6 +140,49 @@ void CreateApp(ApplicationInitializationCallbackParams _)
Gtk.Application.Run();
}

private void OnWindowStateChanged(object o, WindowStateEventArgs args)
{
var winUIApplication = WUX.Application.Current;
var winUIWindow = WUX.Window.Current;
var newState = args.Event.NewWindowState;
var changedState = args.Event.ChangedMask;

var isVisible =
!(newState.HasFlag(Gdk.WindowState.Withdrawn) ||
newState.HasFlag(Gdk.WindowState.Iconified));

var isVisibleChanged =
changedState.HasFlag(Gdk.WindowState.Withdrawn) ||
changedState.HasFlag(Gdk.WindowState.Iconified);

var focused = newState.HasFlag(Gdk.WindowState.Focused);
var focusChanged = changedState.HasFlag(Gdk.WindowState.Focused);

if (!focused && focusChanged)
{
winUIWindow?.OnActivated(Windows.UI.Core.CoreWindowActivationState.Deactivated);
}

if (isVisibleChanged)
{
if (isVisible)
{
winUIApplication?.OnLeavingBackground();
winUIWindow?.OnVisibilityChanged(true);
}
else
{
winUIWindow?.OnVisibilityChanged(false);
winUIApplication?.OnEnteredBackground();
}
}

if (focused && focusChanged)
{
winUIWindow?.OnActivated(Windows.UI.Core.CoreWindowActivationState.CodeActivated);
}
}

private void UpdateWindowPropertiesFromPackage()
{
if (Windows.ApplicationModel.Package.Current.Logo is Uri uri)
Expand Down
19 changes: 17 additions & 2 deletions src/Uno.UI/UI/Xaml/Application.cs
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ public partial class Application
private ApplicationTheme? _requestedTheme;
private bool _systemThemeChangesObserved = false;
private SpecializedResourceDictionary.ResourceKey _requestedThemeForResources;
private bool _isInBackground = false;

static Application()
{
Expand Down Expand Up @@ -258,9 +259,23 @@ private IDisposable WritePhaseEventTrace(int startEventId, int stopEventId)
}
}

internal void OnEnteredBackground() => EnteredBackground?.Invoke(this, new EnteredBackgroundEventArgs());
internal void OnEnteredBackground()
{
if (!_isInBackground)
{
_isInBackground = true;
EnteredBackground?.Invoke(this, new EnteredBackgroundEventArgs());
}
}

internal void OnLeavingBackground() => LeavingBackground?.Invoke(this, new LeavingBackgroundEventArgs());
internal void OnLeavingBackground()
{
if (_isInBackground)
{
_isInBackground = false;
LeavingBackground?.Invoke(this, new LeavingBackgroundEventArgs());
}
}

internal void OnResuming()
{
Expand Down
15 changes: 12 additions & 3 deletions src/Uno.UI/UI/Xaml/Window.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ namespace Windows.UI.Xaml
/// </summary>
public sealed partial class Window
{
private CoreWindowActivationState? _lastActivationState;

private List<WeakEventHelper.GenericEventHandler> _sizeChangedHandlers = new List<WeakEventHelper.GenericEventHandler>();

#pragma warning disable 67
Expand Down Expand Up @@ -132,6 +134,9 @@ public void Activate()
InternalActivate();

OnActivated(CoreWindowActivationState.CodeActivated);

// Initialize visibility on first activation.
Visible = true;
}

partial void InternalActivate();
Expand All @@ -156,9 +161,13 @@ internal IDisposable RegisterSizeChangedEvent(Windows.UI.Xaml.WindowSizeChangedE

internal void OnActivated(CoreWindowActivationState state)
{
var activatedEventArgs = new WindowActivatedEventArgs(state);
CoreWindow.OnActivated(activatedEventArgs);
Activated?.Invoke(this, activatedEventArgs);
if (_lastActivationState != state)
{
_lastActivationState = state;
var activatedEventArgs = new WindowActivatedEventArgs(state);
CoreWindow.OnActivated(activatedEventArgs);
Activated?.Invoke(this, activatedEventArgs);
}
}

internal void OnVisibilityChanged(bool newVisibility)
Expand Down

0 comments on commit 7b711d2

Please sign in to comment.