From 7e8aaffb92563f5c9d1aa9cdc54c8ccb0d0f54db Mon Sep 17 00:00:00 2001 From: Karl Tauber Date: Fri, 26 Jun 2020 10:49:49 +0200 Subject: [PATCH] Window decorations: - double-click on icon closes window - after switching LaF is was not possible to move window when running in JetBrains Runtime --- .../com/formdev/flatlaf/ui/FlatTitlePane.java | 58 +++++++++++-------- 1 file changed, 34 insertions(+), 24 deletions(-) diff --git a/flatlaf-core/src/main/java/com/formdev/flatlaf/ui/FlatTitlePane.java b/flatlaf-core/src/main/java/com/formdev/flatlaf/ui/FlatTitlePane.java index 793027cd..502f3af5 100644 --- a/flatlaf-core/src/main/java/com/formdev/flatlaf/ui/FlatTitlePane.java +++ b/flatlaf-core/src/main/java/com/formdev/flatlaf/ui/FlatTitlePane.java @@ -122,6 +122,9 @@ class FlatTitlePane addMouseListener( handler ); addMouseMotionListener( handler ); + + // necessary for closing window with double-click on icon + iconLabel.addMouseListener( handler ); } private void addSubComponents() { @@ -258,6 +261,8 @@ class FlatTitlePane if( hasImages ) iconLabel.setIcon( FlatTitlePaneIcon.create( images, iconSize ) ); + + updateJBRHitTestSpotsAndTitleBarHeightLater(); } @Override @@ -275,7 +280,7 @@ class FlatTitlePane installWindowListeners(); } - updateJBRHitTestSpotsAndTitleBarHeight(); + updateJBRHitTestSpotsAndTitleBarHeightLater(); } @Override @@ -446,11 +451,17 @@ class FlatTitlePane } private boolean hasJBRCustomDecoration() { - return window != null && - FlatRootPaneUI.canUseJBRCustomDecorations && + return FlatRootPaneUI.canUseJBRCustomDecorations && + window != null && JBRCustomDecorations.hasCustomDecoration( window ); } + private void updateJBRHitTestSpotsAndTitleBarHeightLater() { + EventQueue.invokeLater( () -> { + updateJBRHitTestSpotsAndTitleBarHeight(); + } ); + } + private void updateJBRHitTestSpotsAndTitleBarHeight() { if( !isDisplayable() ) return; @@ -459,8 +470,10 @@ class FlatTitlePane return; List hitTestSpots = new ArrayList<>(); + if( iconLabel.isVisible() ) + addJBRHitTestSpot( iconLabel, false, hitTestSpots ); addJBRHitTestSpot( buttonPanel, false, hitTestSpots ); - addJBRHitTestSpot( menuBarPlaceholder, true, hitTestSpots );//TOOD + addJBRHitTestSpot( menuBarPlaceholder, true, hitTestSpots ); int titleBarHeight = getHeight(); // slightly reduce height so that component receives mouseExit events @@ -548,9 +561,7 @@ class FlatTitlePane break; case "componentOrientation": - EventQueue.invokeLater( () -> { - updateJBRHitTestSpotsAndTitleBarHeight(); - } ); + updateJBRHitTestSpotsAndTitleBarHeightLater(); break; } } @@ -588,20 +599,21 @@ class FlatTitlePane @Override public void mouseClicked( MouseEvent e ) { - if( hasJBRCustomDecoration() ) - return; // do nothing if running in JBR - - if( e.getClickCount() == 2 && - SwingUtilities.isLeftMouseButton( e ) && - window instanceof Frame && - ((Frame)window).isResizable() ) - { - // maximize/restore on double-click - Frame frame = (Frame) window; - if( (frame.getExtendedState() & Frame.MAXIMIZED_BOTH) != 0 ) - restore(); - else - maximize(); + if( e.getClickCount() == 2 && SwingUtilities.isLeftMouseButton( e ) ) { + if( e.getSource() == iconLabel ) { + // double-click on icon closes window + close(); + } else if( !hasJBRCustomDecoration() && + window instanceof Frame && + ((Frame)window).isResizable() ) + { + // maximize/restore on double-click + Frame frame = (Frame) window; + if( (frame.getExtendedState() & Frame.MAXIMIZED_BOTH) != 0 ) + restore(); + else + maximize(); + } } } @@ -667,9 +679,7 @@ class FlatTitlePane @Override public void componentResized( ComponentEvent e ) { - EventQueue.invokeLater( () -> { - updateJBRHitTestSpotsAndTitleBarHeight(); - } ); + updateJBRHitTestSpotsAndTitleBarHeightLater(); } @Override public void componentMoved( ComponentEvent e ) {}