Skip to content

Commit

Permalink
FlatLaf window decorations: strech iconify/maximize/close buttons to …
Browse files Browse the repository at this point in the history
…always fill whole title bar height (issue #897)
  • Loading branch information
DevCharly committed Oct 17, 2024
1 parent 566568f commit 8eab86e
Show file tree
Hide file tree
Showing 6 changed files with 61 additions and 11 deletions.
11 changes: 7 additions & 4 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,13 @@ FlatLaf Change Log
`sun.java2d.d3d` and `sun.java2d.noddraw` are not yet set), which disables
usage of Windows Direct3D (DirectX) onscreen surfaces. Component rendering
still uses Direct3D. (issue #887)
- FlatLaf window decorations on Windows: Fixed possible application freeze when
using custom component that overrides `Component.contains(int x, int y)` and
invokes `SwingUtilities.convertPoint()` (or similar) from the overridden
method. (issue #878)
- FlatLaf window decorations:
- Iconify/maximize/close buttons did not fill whole title bar height, if some
custom component in menu bar increases title bar height. (issue #897)
- Windows: Fixed possible application freeze when using custom component that
overrides `Component.contains(int x, int y)` and invokes
`SwingUtilities.convertPoint()` (or similar) from the overridden method.
(issue #878)
- TextComponents: Fixed too fast scrolling in multi-line text components when
using touchpads (e.g. on macOS). (issue #892)
- ToolBar: Fixed endless loop if button in Toolbar has focus and is made
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,8 @@ public void paintIcon( Component c, Graphics g, int x, int y ) {
// g2.setColor( Color.blue );
// g2.drawRect( x, y, getIconWidth() - 1, getIconHeight() - 1 );

paintBackground( c, g2, x, y );

g2.translate( x, y );
UIScale.scaleGraphics( g2 );

Expand All @@ -69,7 +71,11 @@ public void paintIcon( Component c, Graphics g, int x, int y ) {
}
}

protected abstract void paintIcon( Component c, Graphics2D g2 );
/** @since 3.5.2 */
protected void paintBackground( Component c, Graphics2D g, int x, int y ) {
}

protected abstract void paintIcon( Component c, Graphics2D g );

@Override
public int getIconWidth() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -60,23 +60,24 @@ protected FlatWindowAbstractIcon( Dimension size, int symbolHeight, Color hoverB

@Override
protected void paintIcon( Component c, Graphics2D g ) {
paintBackground( c, g );

g.setColor( getForeground( c ) );
HiDPIUtils.paintAtScale1x( g, 0, 0, width, height, this::paintIconAt1x );
}

protected abstract void paintIconAt1x( Graphics2D g, int x, int y, int width, int height, double scaleFactor );

protected void paintBackground( Component c, Graphics2D g ) {
/** @since 3.5.2 */
@Override
protected void paintBackground( Component c, Graphics2D g, int x, int y ) {
Color background = FlatButtonUI.buttonStateColor( c, null, null, null, hoverBackground, pressedBackground );
if( background != null ) {
// disable antialiasing for background rectangle painting to avoid blurry edges when scaled (e.g. at 125% or 175%)
Object oldHint = g.getRenderingHint( RenderingHints.KEY_ANTIALIASING );
g.setRenderingHint( RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_OFF );

// fill background of whole component
g.setColor( FlatUIUtils.deriveColor( background, c.getBackground() ) );
g.fillRect( 0, 0, width, height );
g.fillRect( 0, 0, c.getWidth(), c.getHeight() );

g.setRenderingHint( RenderingHints.KEY_ANTIALIASING, oldHint );
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -395,6 +395,12 @@ public Dimension getMinimumSize() {
// allow the button to shrink if space is rare
return new Dimension( UIScale.scale( buttonMinimumWidth ), super.getMinimumSize().height );
}
@Override
public Dimension getMaximumSize() {
// allow the button to fill whole button area height
// see also BasicMenuUI.getMaximumSize()
return new Dimension( super.getMaximumSize().width, Short.MAX_VALUE );
}
};
button.setFocusable( false );
button.setContentAreaFilled( false );
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -189,6 +189,7 @@ private void rightCompChanged() {

if( rightCompCheckBox.isSelected() ) {
rightStretchCompCheckBox.setSelected( false );
tallCompCheckBox.setSelected( false );

JButton myButton = new JButton( "?" );
myButton.putClientProperty( "JButton.buttonType", "toolBarButton" );
Expand All @@ -207,6 +208,7 @@ private void rightStretchCompChanged() {

if( rightStretchCompCheckBox.isSelected() ) {
rightCompCheckBox.setSelected( false );
tallCompCheckBox.setSelected( false );

menuBar.add( Box.createGlue() );
menuBar.add( new JProgressBar() );
Expand All @@ -216,6 +218,20 @@ private void rightStretchCompChanged() {
menuBar.repaint();
}

private void tallCompChanged() {
removeNonMenusFromMenuBar();

if( tallCompCheckBox.isSelected() ) {
rightCompCheckBox.setSelected( false );
rightStretchCompCheckBox.setSelected( false );

menuBar.add( new JButton( "<html>large<br>button<br>large<br>button</html>" ) );
}

menuBar.revalidate();
menuBar.repaint();
}

private void removeNonMenusFromMenuBar() {
Component[] components = menuBar.getComponents();
for( int i = components.length - 1; i >= 0; i-- ) {
Expand Down Expand Up @@ -585,6 +601,7 @@ private void initComponents() {
menuBarVisibleCheckBox = new JCheckBox();
rightCompCheckBox = new JCheckBox();
rightStretchCompCheckBox = new JCheckBox();
tallCompCheckBox = new JCheckBox();
JPanel panel3 = new JPanel();
addMenuButton = new JButton();
addGlueButton = new JButton();
Expand Down Expand Up @@ -777,6 +794,7 @@ private void initComponents() {
"[]" +
"[]" +
"[]" +
"[]" +
"[]"));

//---- menuBarCheckBox ----
Expand Down Expand Up @@ -806,6 +824,11 @@ private void initComponents() {
rightStretchCompCheckBox.setText("right aligned stretching component");
rightStretchCompCheckBox.addActionListener(e -> rightStretchCompChanged());
panel6.add(rightStretchCompCheckBox, "cell 0 4");

//---- tallCompCheckBox ----
tallCompCheckBox.setText("tall component");
tallCompCheckBox.addActionListener(e -> tallCompChanged());
panel6.add(tallCompCheckBox, "cell 0 5");
}
add(panel6, "cell 2 0");

Expand Down Expand Up @@ -1254,6 +1277,7 @@ private void initComponents() {
private JCheckBox menuBarVisibleCheckBox;
private JCheckBox rightCompCheckBox;
private JCheckBox rightStretchCompCheckBox;
private JCheckBox tallCompCheckBox;
private JButton addMenuButton;
private JButton addGlueButton;
private JButton addCaptionButton;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
JFDML JFormDesigner: "8.2.1.0.348" Java: "21.0.1" encoding: "UTF-8"
JFDML JFormDesigner: "8.2.3.0.386" Java: "21" encoding: "UTF-8"

new FormModel {
contentType: "form/swing"
Expand Down Expand Up @@ -166,7 +166,7 @@ new FormModel {
add( new FormContainer( "javax.swing.JPanel", new FormLayoutManager( class net.miginfocom.swing.MigLayout ) {
"$layoutConstraints": "ltr,hidemode 3,gap 0 0"
"$columnConstraints": "[left]"
"$rowConstraints": "[][][][][]"
"$rowConstraints": "[][][][][][]"
} ) {
name: "panel6"
"border": new javax.swing.border.TitledBorder( "Menu Bar" )
Expand Down Expand Up @@ -223,6 +223,16 @@ new FormModel {
}, new FormLayoutConstraints( class net.miginfocom.layout.CC ) {
"value": "cell 0 4"
} )
add( new FormComponent( "javax.swing.JCheckBox" ) {
name: "tallCompCheckBox"
"text": "tall component"
auxiliary() {
"JavaCodeGenerator.variableLocal": false
}
addEvent( new FormEvent( "java.awt.event.ActionListener", "actionPerformed", "tallCompChanged", false ) )
}, new FormLayoutConstraints( class net.miginfocom.layout.CC ) {
"value": "cell 0 5"
} )
}, new FormLayoutConstraints( class net.miginfocom.layout.CC ) {
"value": "cell 2 0"
} )
Expand Down

0 comments on commit 8eab86e

Please sign in to comment.