Skip to content
Open
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
20 changes: 13 additions & 7 deletions src/Eto.Mac/Forms/ApplicationHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
using System.Runtime.CompilerServices;
namespace Eto.Mac.Forms
{
public class ApplicationHandler : MacObject<NSApplication, Application, Application.ICallback>, Application.IHandler
public class ApplicationHandler : MacObject<NSApplication, Application, Application.ICallback>, Application.IHandler, Application.IActivateHandler
{
bool _attached;
bool? _activateOnStartup;
Expand Down Expand Up @@ -371,15 +371,21 @@ internal void EnsureActivated(bool showInTaskbar, bool showActivated, Icon icon)
if (!showActivated)
return;

// taking activation from whatever app has it is only for callers that opted in, and has to be done
// for every window since another app can take it back at any point during a run
if (ActivateIgnoringOtherApps)
Control.ActivateIgnoringOtherApps(true);
// otherwise only ask for it as the app gets its first window, which is all macOS will grant anyway
else if (showInTaskbar && wasProhibited)
// Later shows should not take activation unless explicitly opted in.
if (ActivateIgnoringOtherApps || showInTaskbar && wasProhibited)
RequestActivation(false);
}

void RequestActivation(bool explicitlyRequested)
{
if (MacVersion.IsAtLeast(14, 0) && !ActivateIgnoringOtherApps)
Control.Activate();
else
Control.ActivateIgnoringOtherApps(explicitlyRequested || ActivateIgnoringOtherApps);
}

public void Activate() => RequestActivation(true);

public Keys CommonModifier => Keys.Application;

public Keys AlternateModifier => Keys.Alt;
Expand Down
28 changes: 28 additions & 0 deletions src/Eto/Forms/Application.cs
Original file line number Diff line number Diff line change
Expand Up @@ -583,6 +583,23 @@ public string BadgeLabel
/// <seealso cref="IsActiveChanged"/>
public bool IsActive => Handler.IsActive;

/// <summary>
/// Requests native application activation.
/// </summary>
/// <remarks>
/// This method is supported by the native macOS backend and has no effect on other backends.
/// The operating system may delay or decline activation; use <see cref="IsActive"/> and
/// <see cref="IsActiveChanged"/> to observe the activation state.
/// Call this method on the UI thread before calling <see cref="Control.Focus"/> on the desired window.
/// </remarks>
/// <seealso cref="IsActive"/>
/// <seealso cref="IsActiveChanged"/>
public void Activate()
{
EnsureUIThread();
(Handler as IActivateHandler)?.Activate();
}

/// <summary>
/// Advanced. Runs an iteration of the main UI loop when you are blocking the UI thread with logic.
/// </summary>
Expand Down Expand Up @@ -830,4 +847,15 @@ public void OnThemeChanged(Application widget, EventArgs e)
/// </summary>
Theme Theme { get; set; }
}

/// <summary>
/// Optional handler for native application activation.
/// </summary>
public interface IActivateHandler
{
/// <summary>
/// Requests native application activation.
/// </summary>
void Activate();
}
}
Loading