Skip to content

Commit

Permalink
Window decorations: fixed random window title bar background for unif…
Browse files Browse the repository at this point in the history
…ied backgrounds in cases were background is not filled by custom window/rootpane components (issue #254)
  • Loading branch information
DevCharly committed Apr 4, 2021
1 parent eee177e commit 801b555
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 14 deletions.
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -84,36 +85,39 @@ protected void installKeyboardActions() {
@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 ----------------------------------------------------
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -525,10 +525,10 @@ public void paint( Graphics g ) {
@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() {
Expand Down

0 comments on commit 801b555

Please sign in to comment.