Skip to content

RDEV-8025 - Adapt to different order Avalonia events are triggered #373

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

Merged
merged 1 commit into from
Dec 2, 2024

Conversation

heldergoncalves92
Copy link
Collaborator

An update: this is the PR which caused the change in behavior: AvaloniaUI/Avalonia#17059

Previously, events were fired in this order:

  1. WindowClosed
  2. DetachedFromLogicalTree
  3. Closed

After #17059, events are fired in this order:

  1. DetachedFromLogicalTree
  2. Closed
  3. WindowClosed

As you can see, previously the WindowClosed attached event was raised before the window itself was notified that it's been closed. This itself doesn't make much sense, as logically the Closed event on the window should have finished before the attached WindowClosed event is raised, so from a logical point of view the new event ordering makes more sense. This does however cause the problem that you're seeing, where you were using that event to determine whether a control is being removed from the logical tree due to its window closing, or for some other reason.

This is just an update: I'll let you know when I have a decision about whether we want to revert to the old (subtly incorrect) ordering, or if we have a better way of doing this.

// need to subscribe the event this way because close gets called after all elements get detached
window.AddHandler(Window.WindowClosedEvent, (EventHandler<RoutedEventArgs>)OnHostWindowClosed);
if (e.Root is Window window && attachedWindow != window) {
attachedWindow?.RemoveHandler(Window.WindowClosedEvent, (EventHandler<RoutedEventArgs>)OnHostWindowClosed);
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This new logic means that we are just removing the previous handler when we are attaching a new window to the tree?

Copy link
Collaborator Author

@heldergoncalves92 heldergoncalves92 Nov 20, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes. I don't fully like it because does not allow to reuse the Control unless we first attach it to a new window.

It may be an issue in this use case:

  1. Detach from logical tree
  2. Close current window
  3. Attach to new window (This is not possible)

We are forced to do it in this order:

  1. Detach from logical tree
  2. Attach to new window
  3. Close previous window

Copy link
Contributor

@joaompneves joaompneves Nov 21, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I looked at the Avalonia source and when the detached is called it seems that we can look at some properties to infer that the window is about to close, namelly PlatformImpl and IsVisible

https://github.com/AvaloniaUI/Avalonia/blob/master/src/Avalonia.Controls/WindowBase.cs#L236

https://github.com/AvaloniaUI/Avalonia/blob/master/src/Avalonia.Controls/TopLevel.cs#L227

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

image

using Avalonia.LogicalTree;

namespace WebViewControl {
namespace WebViewControl;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

revert... we should do this for all files in a diff PR

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fine by me. In the IDE projects we migrate them on the fly when we touch them

@heldergoncalves92 heldergoncalves92 force-pushed the RDEV-8025-reproduce-avalonia-leak branch from 16234e0 to cd50dd5 Compare December 2, 2024 16:07
@heldergoncalves92 heldergoncalves92 merged commit 800c10d into master Dec 2, 2024
2 of 6 checks passed
@heldergoncalves92 heldergoncalves92 deleted the RDEV-8025-reproduce-avalonia-leak branch December 2, 2024 16:07
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants