Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Application.IsActive and IsActiveChanged APIs #2233

Merged
merged 1 commit into from
Jun 2, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 4 additions & 34 deletions src/Eto.Gtk/Forms/ApplicationHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -172,15 +172,13 @@ public bool IsActive
}


protected virtual void OnIsActiveChanged(EventArgs e) => _IsActiveChanged?.Invoke(this, e);

UITimer _timer;
bool? _isActive;
bool _didGetFocus;
internal void TriggerIsActiveChanged(bool gotFocus)
{
// don't do anything until we really need to know.
if (_IsActiveChanged == null)
if (!IsEventHandled(Application.IsActiveChangedEvent))
return;

_didGetFocus |= gotFocus;
Expand Down Expand Up @@ -216,7 +214,7 @@ internal void TriggerIsActiveChanged(bool gotFocus)
if (_isActive != isActive)
{
_isActive = isActive;
OnIsActiveChanged(EventArgs.Empty);
Callback.OnIsActiveChanged(Widget, EventArgs.Empty);
}
};
}
Expand Down Expand Up @@ -250,36 +248,6 @@ public void RegisterIsActiveChanged(Gtk.Window window)
new WindowActiveHelper(window);
}

EventHandler _IsActiveChanged;
public event EventHandler IsActiveChanged
{
add
{
if (_IsActiveChanged == null)
{
/*
foreach (var window in Application.Instance.Windows)
{
new WindowActiveHelper(window.ToGtk());
}*/

/* Does not work... Windows array is always empty.
Control.WindowAdded += (o, args) => new WindowActiveHelper(args.Window);
for (int i = 0; i < Control.Windows.Length; i++)
{
var window = Control.Windows[i];
new WindowActiveHelper(window);
}*/
}
_IsActiveChanged += value;

}
remove
{
_IsActiveChanged -= value;
}
}

class WindowActiveHelper
{
Gtk.Window _window;
Expand Down Expand Up @@ -347,6 +315,8 @@ public override void AttachEvent(string id)
case Eto.Forms.Application.NotificationActivatedEvent:
// handled by NotificationHandler
break;
case Eto.Forms.Application.IsActiveChangedEvent:
break;
default:
base.AttachEvent(id);
break;
Expand Down
10 changes: 5 additions & 5 deletions src/Eto.Gtk/Forms/FloatingFormHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,23 +18,23 @@ protected override void Initialize()
public override void OnLoad(EventArgs e)
{
base.OnLoad(e);
ApplicationHandler.Instance.IsActiveChanged += Application_IsActiveChanged;
if (!ApplicationHandler.Instance.IsActive)
Application.Instance.IsActiveChanged += Application_IsActiveChanged;
if (!Application.Instance.IsActive)
base.Visible = false;
}

public override void OnUnLoad(EventArgs e)
{
base.OnUnLoad(e);
ApplicationHandler.Instance.IsActiveChanged -= Application_IsActiveChanged;
Application.Instance.IsActiveChanged -= Application_IsActiveChanged;
}

private void Application_IsActiveChanged(object sender, EventArgs e)
{
var lastAcceptFocus = Control.AcceptFocus;
Control.AcceptFocus = false;

base.Visible = ApplicationHandler.Instance.IsActive && _visible;
base.Visible = Application.Instance.IsActive && _visible;
Control.AcceptFocus = lastAcceptFocus;
}

Expand All @@ -46,7 +46,7 @@ public override bool Visible
set
{
_visible = value;
if (ApplicationHandler.Instance.IsActive)
if (Application.Instance.IsActive)
base.Visible = value;
}
}
Expand Down
8 changes: 8 additions & 0 deletions src/Eto.Mac/Forms/ApplicationHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -275,12 +275,18 @@ public override void AttachEvent(string id)
NSApplication.Notifications.ObserveDidFinishLaunching(DidFinishLaunching);
}
break;
case Application.IsActiveChangedEvent:
NSNotificationCenter.DefaultCenter.AddObserver(NSApplication.DidBecomeActiveNotification, SharedApplication_ActiveChanged);
NSNotificationCenter.DefaultCenter.AddObserver(NSApplication.DidResignActiveNotification, SharedApplication_ActiveChanged);
break;
default:
base.AttachEvent(id);
break;
}
}

void SharedApplication_ActiveChanged(NSNotification obj) => Callback.OnIsActiveChanged(Widget, EventArgs.Empty);

void OnCurrentDomainUnhandledException(object sender, System.UnhandledExceptionEventArgs e)
{
var unhandledExceptionArgs = new UnhandledExceptionEventArgs(e.ExceptionObject, e.IsTerminating);
Expand Down Expand Up @@ -321,5 +327,7 @@ public void EnableFullScreen()
public Keys CommonModifier => Keys.Application;

public Keys AlternateModifier => Keys.Alt;

public bool IsActive => NSApplication.SharedApplication.Active;
}
}
3 changes: 1 addition & 2 deletions src/Eto.WinForms/Forms/ApplicationHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@ public class ApplicationHandler : WidgetHandler<object, Application, Application

public static ApplicationHandler Instance => Eto.Forms.Application.Instance?.Handler as ApplicationHandler;

public event EventHandler IsActiveChanged;
bool _isActive;
public bool IsActive
{
Expand All @@ -32,7 +31,7 @@ public bool IsActive
if (_isActive != value)
{
_isActive = value;
IsActiveChanged?.Invoke(this, EventArgs.Empty);
Callback.OnIsActiveChanged(Widget, EventArgs.Empty);
}
}
}
Expand Down
10 changes: 5 additions & 5 deletions src/Eto.WinForms/Forms/FloatingFormHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,22 +17,22 @@ protected override void Initialize()
public override void OnLoad(EventArgs e)
{
base.OnLoad(e);
ApplicationHandler.Instance.IsActiveChanged += Application_IsActiveChanged;
if (!ApplicationHandler.Instance.IsActive)
Application.Instance.IsActiveChanged += Application_IsActiveChanged;
if (!Application.Instance.IsActive)
base.Visible = false;
}

public override void OnUnLoad(EventArgs e)
{
base.OnUnLoad(e);
ApplicationHandler.Instance.IsActiveChanged -= Application_IsActiveChanged;
Application.Instance.IsActiveChanged -= Application_IsActiveChanged;
}

private void Application_IsActiveChanged(object sender, EventArgs e)
{
var showActivated = ShowActivated;
ShowActivated = false;
base.Visible = ApplicationHandler.Instance.IsActive && _visible;
base.Visible = Application.Instance.IsActive && _visible;
ShowActivated = showActivated;
}

Expand All @@ -44,7 +44,7 @@ public override bool Visible
set
{
_visible = value;
if (ApplicationHandler.Instance.IsActive)
if (Application.Instance.IsActive)
base.Visible = value;
}
}
Expand Down
7 changes: 4 additions & 3 deletions src/Eto.Wpf/Forms/ApplicationHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -140,8 +140,6 @@ void HandleStartup(object sender, sw.StartupEventArgs e)
}
}

public event EventHandler IsActiveChanged;

bool _isActive;
public bool IsActive
{
Expand All @@ -151,7 +149,7 @@ public bool IsActive
if (_isActive != value)
{
_isActive = value;
IsActiveChanged?.Invoke(this, EventArgs.Empty);
Callback.OnIsActiveChanged(Widget, EventArgs.Empty);
}
}
}
Expand Down Expand Up @@ -307,6 +305,9 @@ public override void AttachEvent(string id)
case Application.NotificationActivatedEvent:
// handled by NotificationHandler
break;
case Application.IsActiveChangedEvent:
// handled always
break;
default:
base.AttachEvent(id);
break;
Expand Down
10 changes: 4 additions & 6 deletions src/Eto.Wpf/Forms/FloatingFormHandler.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
using System;
using System.Windows;
using System.Windows.Interop;
using Eto.Forms;

namespace Eto.Wpf.Forms
Expand All @@ -24,15 +22,15 @@ protected override void Initialize()
public override void OnLoad(EventArgs e)
{
base.OnLoad(e);
ApplicationHandler.Instance.IsActiveChanged += Application_IsActiveChanged;
if (!ApplicationHandler.Instance.IsActive)
Application.Instance.IsActiveChanged += Application_IsActiveChanged;
if (!Application.Instance.IsActive)
base.Visible = false;
}

public override void OnUnLoad(EventArgs e)
{
base.OnUnLoad(e);
ApplicationHandler.Instance.IsActiveChanged -= Application_IsActiveChanged;
Application.Instance.IsActiveChanged -= Application_IsActiveChanged;
}

private void Application_IsActiveChanged(object sender, EventArgs e)
Expand All @@ -59,7 +57,7 @@ public override void Show()
void SetVisibility()
{
var currentlyVisible = base.Visible;
var isVisible = ApplicationHandler.Instance.IsActive && Visible;
var isVisible = Application.Instance.IsActive && Visible;
if (isVisible == currentlyVisible)
return;

Expand Down
Loading