1
1
/*
2
- * Copyright (c) 2004, 2024 , Oracle and/or its affiliates. All rights reserved.
2
+ * Copyright (c) 2004, 2025 , Oracle and/or its affiliates. All rights reserved.
3
3
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4
4
*
5
5
* This code is free software; you can redistribute it and/or modify it
51
51
import java .io .BufferedReader ;
52
52
import java .io .IOException ;
53
53
import java .io .InputStreamReader ;
54
- import java .util .Arrays ;
55
54
56
55
import sun .awt .X11 .XBaseWindow ;
57
56
import com .sun .java .swing .plaf .gtk .GTKConstants .TextDirection ;
@@ -521,6 +520,20 @@ public boolean isRunningOnWayland() {
521
520
// application icons).
522
521
private static final WindowFocusListener waylandWindowFocusListener ;
523
522
523
+ private static boolean containsWaylandWindowFocusListener (Window window ) {
524
+ if (window == null ) {
525
+ return false ;
526
+ }
527
+
528
+ for (WindowFocusListener focusListener : window .getWindowFocusListeners ()) {
529
+ if (focusListener == waylandWindowFocusListener ) {
530
+ return true ;
531
+ }
532
+ }
533
+
534
+ return false ;
535
+ }
536
+
524
537
static {
525
538
if (isOnWayland ()) {
526
539
waylandWindowFocusListener = new WindowAdapter () {
@@ -530,13 +543,22 @@ public void windowLostFocus(WindowEvent e) {
530
543
Window oppositeWindow = e .getOppositeWindow ();
531
544
532
545
// The focus can move between the window calling the popup,
533
- // and the popup window itself.
546
+ // and the popup window itself or its children .
534
547
// We only dismiss the popup in other cases.
535
548
if (oppositeWindow != null ) {
536
- if (window == oppositeWindow .getParent () ) {
549
+ if (containsWaylandWindowFocusListener ( oppositeWindow .getOwner ()) ) {
537
550
addWaylandWindowFocusListenerToWindow (oppositeWindow );
538
551
return ;
539
552
}
553
+
554
+ Window owner = window .getOwner ();
555
+ while (owner != null ) {
556
+ if (owner == oppositeWindow ) {
557
+ return ;
558
+ }
559
+ owner = owner .getOwner ();
560
+ }
561
+
540
562
if (window .getParent () == oppositeWindow ) {
541
563
return ;
542
564
}
@@ -557,11 +579,11 @@ public void windowLostFocus(WindowEvent e) {
557
579
}
558
580
559
581
private static void addWaylandWindowFocusListenerToWindow (Window window ) {
560
- if (!Arrays
561
- .asList (window .getWindowFocusListeners ())
562
- .contains (waylandWindowFocusListener )
563
- ) {
582
+ if (!containsWaylandWindowFocusListener (window )) {
564
583
window .addWindowFocusListener (waylandWindowFocusListener );
584
+ for (Window ownedWindow : window .getOwnedWindows ()) {
585
+ addWaylandWindowFocusListenerToWindow (ownedWindow );
586
+ }
565
587
}
566
588
}
567
589
0 commit comments