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

IActivatableLifetime doesn't work on MacOS #17561

Open
JamesSmith85 opened this issue Nov 19, 2024 · 5 comments · May be fixed by #17573
Open

IActivatableLifetime doesn't work on MacOS #17561

JamesSmith85 opened this issue Nov 19, 2024 · 5 comments · May be fixed by #17573
Labels
bug help-wanted A contribution from the community would be most welcome. os-macos

Comments

@JamesSmith85
Copy link

Describe the bug

I have updated the Avalonia version in my application from an older nightly build to 11.4.1 (The issue can be reproduced from 11.1.0 to 11.2.1).

I had to update my code as IActivatableApplicationLifetime interface doesn't exist anymore, but I can use TryGetFeature<IActivatableLifetime>() instead.
The new code successfully finds the feature and subscribe to its event, but sadly the behavior changed.
Deactivated event never gets called, at least in the scenario where it was called before now it is not.

To Reproduce

  1. Create a new Avalonia app.
  2. Hide it from macOS's Dock.
  3. Deactivated event is not called (Activated is called instead)

Simple repo:

 public MainViewModel()
    {
        if (Application.Current?.TryGetFeature<IActivatableLifetime>() is { } activatableLifetime)
        {
            activatableLifetime.Activated += OnActivated;
            activatableLifetime.Deactivated += OnDeactivated;
        }
        
    }

    private void OnActivated(object? sender, ActivatedEventArgs e)
    {
        Console.WriteLine("Activated");
    }
    
    private void OnDeactivated(object? sender, ActivatedEventArgs e)
    {
        Console.WriteLine("Deactivated");
    }

Expected behavior

No response

Avalonia version

11.1.0 - 11.2.1

OS

macOS

Additional context

No response

@maxkatz6
Copy link
Member

Can you confirm, that subscribing earlier doesn't solve the problem?
For example, in OnFrameworkInitializationCompleted method.

@JamesSmith85
Copy link
Author

JamesSmith85 commented Nov 20, 2024

Can you confirm, that subscribing earlier doesn't solve the problem? For example, in OnFrameworkInitializationCompleted method.

Doesn't solve it. The issue here is why the activated event gets invoked when the app is deactivated. With the current concept doesn't matter if the app gets activated or deactivated, always the Activated event gets fired.

@maxkatz6
Copy link
Member

You are right. It should call OnDeactived from OnHide method:

void IAvnApplicationEvents.OnHide()
{
if (AvaloniaLocator.Current.GetService<IActivatableLifetime>() is ActivatableLifetimeBase lifetime)
{
lifetime.OnActivated(ActivationKind.Background);
}
}
void IAvnApplicationEvents.OnUnhide()
{
if (AvaloniaLocator.Current.GetService<IActivatableLifetime>() is ActivatableLifetimeBase lifetime)
{
lifetime.OnActivated(ActivationKind.Background);
}
}

@maxkatz6 maxkatz6 added the help-wanted A contribution from the community would be most welcome. label Nov 20, 2024
@maxkatz6
Copy link
Member

If anybody wants to contribute, it should be a simple fix.

@JamesSmith85 JamesSmith85 linked a pull request Nov 20, 2024 that will close this issue
@JamesSmith85
Copy link
Author

If anybody wants to contribute, it should be a simple fix.

Done #17573

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug help-wanted A contribution from the community would be most welcome. os-macos
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants