From 801b555835e3ba2b3a09f4fbc102e0f70eae7f97 Mon Sep 17 00:00:00 2001 From: Karl Tauber Date: Sun, 4 Apr 2021 11:47:15 +0200 Subject: [PATCH] Window decorations: fixed random window title bar background for unified backgrounds in cases were background is not filled by custom window/rootpane components (issue #254) --- CHANGELOG.md | 3 +++ .../com/formdev/flatlaf/ui/FlatMenuBarUI.java | 24 +++++++++++-------- .../com/formdev/flatlaf/ui/FlatTitlePane.java | 8 +++---- 3 files changed, 21 insertions(+), 14 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 87da0078..9bf82237 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -16,6 +16,9 @@ FlatLaf Change Log Platform Module System (JPMS) for application. (issue #289) - Native window decorations: Removed superfluous pixel-line at top of screen when window is maximized. (issue #296) +- Window decorations: Fixed random window title bar background in cases were + background is not filled by custom window/rootpane components and + `TitlePane.unifiedBackground` is `true`. - Button and ToggleButton: Do not paint background of disabled (and unselected) toolBar buttons. (issue #292; regression since fixing #112) - TabbedPane: Fixed NPE when creating/modifying in another thread. (issue #299) diff --git a/flatlaf-core/src/main/java/com/formdev/flatlaf/ui/FlatMenuBarUI.java b/flatlaf-core/src/main/java/com/formdev/flatlaf/ui/FlatMenuBarUI.java index 67a67e3e..42fc745e 100644 --- a/flatlaf-core/src/main/java/com/formdev/flatlaf/ui/FlatMenuBarUI.java +++ b/flatlaf-core/src/main/java/com/formdev/flatlaf/ui/FlatMenuBarUI.java @@ -16,6 +16,7 @@ package com.formdev.flatlaf.ui; +import java.awt.Color; import java.awt.Graphics; import java.awt.Window; import java.awt.event.ActionEvent; @@ -84,36 +85,39 @@ public class FlatMenuBarUI @Override public void update( Graphics g, JComponent c ) { // paint background - if( isFillBackground( c ) ) { - g.setColor( c.getBackground() ); + Color background = getBackground( c ); + if( background != null ) { + g.setColor( background ); g.fillRect( 0, 0, c.getWidth(), c.getHeight() ); } paint( g, c ); } - protected boolean isFillBackground( JComponent c ) { + protected Color getBackground( JComponent c ) { + Color background = c.getBackground(); + // paint background if opaque or if having custom background color - if( c.isOpaque() || !(c.getBackground() instanceof UIResource) ) - return true; + if( c.isOpaque() || !(background instanceof UIResource) ) + return background; // paint background if menu bar is not the "main" menu bar JRootPane rootPane = SwingUtilities.getRootPane( c ); if( rootPane == null || !(rootPane.getParent() instanceof Window) || rootPane.getJMenuBar() != c ) - return true; + return background; - // do not paint background for unified title pane + // paint background for unified title pane in color of parent // (not storing value of "TitlePane.unifiedBackground" in class to allow changing at runtime) if( UIManager.getBoolean( "TitlePane.unifiedBackground" ) && FlatNativeWindowBorder.hasCustomDecoration( (Window) rootPane.getParent() ) ) - return false; + return FlatUIUtils.getParentBackground( c ); // paint background in full screen mode if( FlatUIUtils.isFullScreen( rootPane ) ) - return true; + return background; // do not paint background if menu bar is embedded into title pane - return !FlatRootPaneUI.isMenuBarEmbedded( rootPane ); + return FlatRootPaneUI.isMenuBarEmbedded( rootPane ) ? null :background; } //---- class TakeFocus ---------------------------------------------------- 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 3d843023..2b423a06 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 @@ -525,10 +525,10 @@ debug*/ @Override protected void paintComponent( Graphics g ) { // not storing value of "TitlePane.unifiedBackground" in class to allow changing at runtime - if( !UIManager.getBoolean( "TitlePane.unifiedBackground" ) ) { - g.setColor( getBackground() ); - g.fillRect( 0, 0, getWidth(), getHeight() ); - } + g.setColor( UIManager.getBoolean( "TitlePane.unifiedBackground" ) + ? FlatUIUtils.getParentBackground( this ) + : getBackground() ); + g.fillRect( 0, 0, getWidth(), getHeight() ); } protected void repaintWindowBorder() {