Skip to content

Commit

Permalink
Remove event filter when owner window is hiding (#1443)
Browse files Browse the repository at this point in the history
  • Loading branch information
rhabarberbarberah committed Jun 10, 2022
1 parent 6fa8df9 commit 7433057
Showing 1 changed file with 10 additions and 22 deletions.
32 changes: 10 additions & 22 deletions controlsfx/src/main/java/org/controlsfx/control/PopOver.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/**
* Copyright (c) 2013, 2016 ControlsFX
* Copyright (c) 2013, 2022, ControlsFX
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
Expand Down Expand Up @@ -270,7 +270,7 @@ public void changed(ObservableValue<? extends Number> value,
yListener);

private Window ownerWindow;
private final EventHandler<WindowEvent> closePopOverOnOwnerWindowCloseLambda = event -> ownerWindowClosing();
private final EventHandler<WindowEvent> closePopOverOnOwnerWindowCloseLambda = event -> ownerWindowHiding();
private final WeakEventHandler<WindowEvent> closePopOverOnOwnerWindowClose = new WeakEventHandler<>(closePopOverOnOwnerWindowCloseLambda);

/**
Expand Down Expand Up @@ -347,10 +347,7 @@ public final void show(Window owner) {
showFadeInAnimation(getFadeInDuration());
}

ownerWindow.addEventFilter(WindowEvent.WINDOW_CLOSE_REQUEST,
closePopOverOnOwnerWindowClose);
ownerWindow.addEventFilter(WindowEvent.WINDOW_HIDING,
closePopOverOnOwnerWindowClose);
ownerWindow.addEventFilter(WindowEvent.WINDOW_HIDING, closePopOverOnOwnerWindowClose);
}

/** {@inheritDoc} */
Expand All @@ -363,10 +360,7 @@ public final void show(Window ownerWindow, double anchorX, double anchorY) {
showFadeInAnimation(getFadeInDuration());
}

ownerWindow.addEventFilter(WindowEvent.WINDOW_CLOSE_REQUEST,
closePopOverOnOwnerWindowClose);
ownerWindow.addEventFilter(WindowEvent.WINDOW_HIDING,
closePopOverOnOwnerWindowClose);
ownerWindow.addEventFilter(WindowEvent.WINDOW_HIDING, closePopOverOnOwnerWindowClose);
}

/**
Expand Down Expand Up @@ -467,10 +461,7 @@ public final void show(Node owner, double x, double y,
}

// Bug fix - close popup when owner window is closing
ownerWindow.addEventFilter(WindowEvent.WINDOW_CLOSE_REQUEST,
closePopOverOnOwnerWindowClose);
ownerWindow.addEventFilter(WindowEvent.WINDOW_HIDING,
closePopOverOnOwnerWindowClose);
ownerWindow.addEventFilter(WindowEvent.WINDOW_HIDING, closePopOverOnOwnerWindowClose);
}

private void showFadeInAnimation(Duration fadeInDuration) {
Expand All @@ -484,8 +475,12 @@ private void showFadeInAnimation(Duration fadeInDuration) {
fadeIn.play();
}

private void ownerWindowClosing() {
private void ownerWindowHiding() {
hide(Duration.ZERO);
if (ownerWindow != null) {
// remove EventFilter to prevent memory leak
ownerWindow.removeEventFilter(WindowEvent.WINDOW_HIDING, closePopOverOnOwnerWindowClose);
}
}

/**
Expand All @@ -507,13 +502,6 @@ public final void hide() {
* @since 1.0
*/
public final void hide(Duration fadeOutDuration) {
//We must remove EventFilter in order to prevent memory leak.
if (ownerWindow != null){
ownerWindow.removeEventFilter(WindowEvent.WINDOW_CLOSE_REQUEST,
closePopOverOnOwnerWindowClose);
ownerWindow.removeEventFilter(WindowEvent.WINDOW_HIDING,
closePopOverOnOwnerWindowClose);
}
if (fadeOutDuration == null) {
fadeOutDuration = DEFAULT_FADE_DURATION;
}
Expand Down

0 comments on commit 7433057

Please sign in to comment.