-
-
Notifications
You must be signed in to change notification settings - Fork 2.3k
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 an event so that users can detect when an Application icon is clicked #14106
Merged
maxkatz6
merged 14 commits into
master
from
features/osx-allow-app-to-reopen-main-window
Jan 10, 2024
Merged
Changes from 9 commits
Commits
Show all changes
14 commits
Select commit
Hold shift + click to select a range
9933f22
Add an event so that users can detect when an Application icon is cli…
danwalmsley bca24ff
refactor to use Lifetime apis.
danwalmsley d1821bf
Merge remote-tracking branch 'origin/master' into features/osx-allow-…
danwalmsley b5fd8ad
use ActivationKind instead of reason to be consistent with other xaml…
danwalmsley 440f300
implement macos raise url events.
danwalmsley 6dbb7ea
add docs.
danwalmsley 3366aa9
add apis to programatically Activate and Deactivate the application.
danwalmsley bbd95d7
Merge branch 'master' into features/osx-allow-app-to-reopen-main-window
danwalmsley 7035aba
fix api naming.
danwalmsley 71ec00c
Add Browser IActivatableApplicationLifetime impl
maxkatz6 9c0c193
Implement IActivatableApplicationLifetime on Android
maxkatz6 a20498a
Add IActivatableApplicationLifetime iOS implementation
maxkatz6 0ea340c
Adjust android impl a little
maxkatz6 cc2f6f6
Merge branch 'master' into features/osx-allow-app-to-reopen-main-window
maxkatz6 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
23 changes: 23 additions & 0 deletions
23
src/Avalonia.Controls/ApplicationLifetimes/ActivatedEventArgs.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
using System; | ||
|
||
namespace Avalonia.Controls.ApplicationLifetimes; | ||
|
||
/// <summary> | ||
/// Event args for an Application Lifetime Activated or Deactivated events. | ||
/// </summary> | ||
public class ActivatedEventArgs : EventArgs | ||
{ | ||
/// <summary> | ||
/// Ctor for ActivatedEventArgs | ||
/// </summary> | ||
/// <param name="kind">The <see cref="ActivationKind"/> that this event represents</param> | ||
public ActivatedEventArgs(ActivationKind kind) | ||
{ | ||
Kind = kind; | ||
} | ||
|
||
/// <summary> | ||
/// The <see cref="ActivationKind"/> that this event represents. | ||
/// </summary> | ||
public ActivationKind Kind { get; } | ||
} |
25 changes: 25 additions & 0 deletions
25
src/Avalonia.Controls/ApplicationLifetimes/ActivationKind.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
namespace Avalonia.Controls.ApplicationLifetimes; | ||
|
||
public enum ActivationKind | ||
{ | ||
/// <summary> | ||
/// When the application is passed a URI to open. | ||
/// </summary> | ||
OpenUri = 20, | ||
|
||
/// <summary> | ||
/// When the application is asked to reopen. | ||
/// An example of this is on MacOS when all the windows are closed, | ||
/// application continues to run in the background and the user clicks | ||
/// the application's dock icon. | ||
/// </summary> | ||
Reopen = 30, | ||
|
||
/// <summary> | ||
/// When the application enters or leaves a background state. | ||
/// An example is when on MacOS the user hides or shows and application (not window), | ||
/// or when a browser application switchs tabs or when a mobile applications goes into | ||
/// the background. | ||
/// </summary> | ||
Background = 40 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
35 changes: 35 additions & 0 deletions
35
src/Avalonia.Controls/ApplicationLifetimes/IActivatableApplicationLifetime.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
using System; | ||
|
||
namespace Avalonia.Controls.ApplicationLifetimes; | ||
|
||
/// <summary> | ||
/// An interface for ApplicationLifetimes where the application can be Activated and Deactivated. | ||
/// </summary> | ||
public interface IActivatableApplicationLifetime | ||
{ | ||
/// <summary> | ||
/// An event that is raised when the application is Activated for various reasons | ||
/// as described by the <see cref="ActivationKind"/> enumeration. | ||
/// </summary> | ||
event EventHandler<ActivatedEventArgs> Activated; | ||
|
||
/// <summary> | ||
/// An event that is raised when the application is Deactivated for various reasons | ||
/// as described by the <see cref="ActivationKind"/> enumeration. | ||
/// </summary> | ||
event EventHandler<ActivatedEventArgs> Deactivated; | ||
|
||
/// <summary> | ||
/// Tells the application that it should attempt to leave its background state. | ||
/// For example on OSX this would be [NSApp unhide] | ||
/// </summary> | ||
/// <returns>true if it was possible and the platform supports this. false otherwise</returns> | ||
public bool TryLeaveBackground(); | ||
|
||
/// <summary> | ||
/// Tells the application that it should attempt to enter its background state. | ||
/// For example on OSX this would be [NSApp hide] | ||
/// </summary> | ||
/// <returns>true if it was possible and the platform supports this. false otherwise</returns> | ||
public bool TryEnterBackground(); | ||
} |
13 changes: 13 additions & 0 deletions
13
src/Avalonia.Controls/ApplicationLifetimes/ProtocolActivatedEventArgs.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
using System; | ||
|
||
namespace Avalonia.Controls.ApplicationLifetimes; | ||
|
||
public class ProtocolActivatedEventArgs : ActivatedEventArgs | ||
{ | ||
public ProtocolActivatedEventArgs(ActivationKind kind, Uri uri) : base(kind) | ||
{ | ||
Uri = uri; | ||
} | ||
|
||
public Uri Uri { get; } | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
50 changes: 50 additions & 0 deletions
50
src/Avalonia.Native/MacOSClassicDesktopStyleApplicationLifetime.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
using System; | ||
using Avalonia.Controls.ApplicationLifetimes; | ||
using Avalonia.Controls.Platform; | ||
|
||
namespace Avalonia.Native; | ||
|
||
#nullable enable | ||
|
||
internal class MacOSClassicDesktopStyleApplicationLifetime : ClassicDesktopStyleApplicationLifetime, | ||
IActivatableApplicationLifetime | ||
{ | ||
/// <inheritdoc /> | ||
public event EventHandler<ActivatedEventArgs>? Activated; | ||
|
||
/// <inheritdoc /> | ||
public event EventHandler<ActivatedEventArgs>? Deactivated; | ||
|
||
/// <inheritdoc /> | ||
public bool TryLeaveBackground() | ||
{ | ||
var nativeApplicationCommands = AvaloniaLocator.Current.GetService<INativeApplicationCommands>(); | ||
nativeApplicationCommands?.ShowApp(); | ||
|
||
return true; | ||
} | ||
|
||
/// <inheritdoc /> | ||
public bool TryEnterBackground() | ||
{ | ||
var nativeApplicationCommands = AvaloniaLocator.Current.GetService<INativeApplicationCommands>(); | ||
nativeApplicationCommands?.HideApp(); | ||
|
||
return true; | ||
} | ||
|
||
internal void RaiseUrl(Uri uri) | ||
{ | ||
Activated?.Invoke(this, new ProtocolActivatedEventArgs(ActivationKind.OpenUri, uri)); | ||
} | ||
|
||
internal void RaiseActivated(ActivationKind kind) | ||
{ | ||
Activated?.Invoke(this, new ActivatedEventArgs(kind)); | ||
} | ||
|
||
internal void RaiseDeactivated(ActivationKind kind) | ||
{ | ||
Deactivated?.Invoke(this, new ActivatedEventArgs(kind)); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I feel like we should split these args, and add DeactivatedEventArgs instead of reusing. Maybe even with DeactivationKind.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If I interpreted correctly what @danwalmsley meant if
ActivationKind.Background
occurs in the Activated event, it indicates that the app has been reactivated from suspension, otherwise in Deactivated it indicates that the app has been suspended. So for each of the elements of the enum.If so I would suggest renaming:
ActivatedEventArgs in ApplicationStateEventArgs
ActivationKind in ApplicationStateKind
and create
ApplicationStateActivatedEventArgs: ApplicationStateEventArgs
ApplicationStateDeactivatedEventArgs: ApplicationStateEventArgs
cc @robloo