Icons: support scaling Laf icons (checkbox, radiobutton, etc) (issue #1061)

This commit is contained in:
Karl Tauber
2025-12-03 11:41:39 +01:00
parent d7a5c353fe
commit 5b0f13110a
15 changed files with 166 additions and 97 deletions

View File

@@ -16,7 +16,6 @@
package com.formdev.flatlaf.icons;
import static com.formdev.flatlaf.util.UIScale.*;
import java.awt.Color;
import java.awt.Component;
import java.awt.Graphics;
@@ -29,7 +28,7 @@ import com.formdev.flatlaf.util.UIScale;
/**
* Base class for icons that scales width and height, creates and initializes
* a scaled graphics context for icon painting.
*
* <p>
* Subclasses do not need to scale icon painting.
*
* @author Karl Tauber
@@ -37,10 +36,15 @@ import com.formdev.flatlaf.util.UIScale;
public abstract class FlatAbstractIcon
implements Icon, UIResource
{
/** Unscaled icon width. */
protected final int width;
/** Unscaled icon height. */
protected final int height;
protected Color color;
/** Additional icon scale factor. */
private float scale = 1;
public FlatAbstractIcon( int width, int height, Color color ) {
this.width = width;
this.height = height;
@@ -61,6 +65,9 @@ public abstract class FlatAbstractIcon
g2.translate( x, y );
UIScale.scaleGraphics( g2 );
float scale = getScale();
if( scale != 1 )
g2.scale( scale, scale );
if( color != null )
g2.setColor( color );
@@ -71,19 +78,71 @@ public abstract class FlatAbstractIcon
}
}
/** @since 3.5.2 */
/**
* Paints icon background. Default implementation does nothing.
* Can be overridden to paint specific icon background.
* <p>
* The bounds of the area to be filled are:
* x, y, {@link #getIconWidth()}, {@link #getIconHeight()}.
* <p>
* In contrast to {@link #paintIcon(Component, Graphics2D)},
* the graphics context {@code g} is not translated and not scaled.
*
* @since 3.5.2
*/
protected void paintBackground( Component c, Graphics2D g, int x, int y ) {
}
/**
* Paints icon.
* <p>
* The graphics context is translated and scaled.
* This means that icon x,y coordinates are {@code 0,0}
* and it is not necessary to scale coordinates within this method.
* <p>
* The bounds to be used for icon painting are:
* 0, 0, {@link #width}, {@link #height}.
*/
protected abstract void paintIcon( Component c, Graphics2D g );
/**
* Returns the scaled icon width.
*/
@Override
public int getIconWidth() {
return scale( width );
return scale( UIScale.scale( width ) );
}
/**
* Returns the scaled icon height.
*/
@Override
public int getIconHeight() {
return scale( height );
return scale( UIScale.scale( height ) );
}
/** @since 3.7 */
public float getScale() {
return scale;
}
/** @since 3.7 */
public void setScale( float scale ) {
this.scale = scale;
}
/**
* Multiplies the given value by the icon scale factor {@link #getScale()} and rounds the result.
* <p>
* If you want scale a {@code float} or {@code double} value,
* simply use: {@code myFloatValue * }{@link #getScale()}.
* <p>
* Do not use this method when painting icon in {@link #paintIcon(Component, Graphics2D)}.
*
* @since 3.7
*/
protected int scale( int size ) {
float scale = getScale();
return (scale == 1) ? size : Math.round( size * scale );
}
}

View File

@@ -17,7 +17,6 @@
package com.formdev.flatlaf.icons;
import java.awt.BasicStroke;
import java.awt.Color;
import java.awt.Component;
import java.awt.Graphics2D;
import java.awt.RenderingHints;
@@ -25,11 +24,9 @@ import java.awt.geom.Area;
import java.awt.geom.Path2D;
import java.awt.geom.Rectangle2D;
import java.awt.geom.RoundRectangle2D;
import java.util.Collections;
import java.util.Map;
import javax.swing.UIManager;
import com.formdev.flatlaf.ui.FlatStylingSupport.StyleableField;
import com.formdev.flatlaf.ui.FlatStylingSupport.StyleableObject;
import com.formdev.flatlaf.ui.FlatStylingSupport.UnknownStyleException;
import com.formdev.flatlaf.ui.FlatUIUtils;
/**
@@ -39,6 +36,8 @@ import com.formdev.flatlaf.ui.FlatUIUtils;
*
* @author Karl Tauber
*/
@StyleableField( cls=FlatAbstractIcon.class, key="capsLockIconScale", fieldName="scale" )
@StyleableField( cls=FlatAbstractIcon.class, key="capsLockIconColor", fieldName="color" )
public class FlatCapsLockIcon
extends FlatAbstractIcon
implements StyleableObject
@@ -49,31 +48,6 @@ public class FlatCapsLockIcon
super( 16, 16, UIManager.getColor( "PasswordField.capsLockIconColor" ) );
}
/** @since 2 */
@Override
public Object applyStyleProperty( String key, Object value ) {
Object oldValue;
switch( key ) {
case "capsLockIconColor": oldValue = color; color = (Color) value; return oldValue;
default: throw new UnknownStyleException( key );
}
}
/** @since 3.7 */
@Override
public Map<String, Class<?>> getStyleableInfos() throws IllegalArgumentException {
return Collections.singletonMap( "capsLockIconColor", Color.class );
}
/** @since 2.5 */
@Override
public Object getStyleableValue( String key ) {
switch( key ) {
case "capsLockIconColor": return color;
default: return null;
}
}
@Override
protected void paintIcon( Component c, Graphics2D g ) {
/*

View File

@@ -29,6 +29,7 @@ import javax.swing.JComponent;
import javax.swing.UIManager;
import com.formdev.flatlaf.ui.FlatButtonUI;
import com.formdev.flatlaf.ui.FlatStylingSupport.Styleable;
import com.formdev.flatlaf.ui.FlatStylingSupport.StyleableField;
import com.formdev.flatlaf.ui.FlatStylingSupport.StyleableObject;
import com.formdev.flatlaf.ui.FlatUIUtils;
@@ -100,6 +101,7 @@ import com.formdev.flatlaf.ui.FlatUIUtils;
*
* @author Karl Tauber
*/
@StyleableField( cls=FlatAbstractIcon.class, key="scale" )
public class FlatCheckBoxIcon
extends FlatAbstractIcon
implements StyleableObject
@@ -291,7 +293,7 @@ public class FlatCheckBoxIcon
/** @since 2 */
public float getFocusWidth() {
return focusWidth;
return focusWidth * getScale();
}
protected Color getFocusColor( Component c ) {

View File

@@ -25,6 +25,7 @@ import javax.swing.AbstractButton;
import javax.swing.JMenuItem;
import javax.swing.UIManager;
import com.formdev.flatlaf.ui.FlatStylingSupport.Styleable;
import com.formdev.flatlaf.ui.FlatStylingSupport.StyleableField;
import com.formdev.flatlaf.ui.FlatStylingSupport.StyleableObject;
/**
@@ -37,6 +38,7 @@ import com.formdev.flatlaf.ui.FlatStylingSupport.StyleableObject;
*
* @author Karl Tauber
*/
@StyleableField( cls=FlatAbstractIcon.class, key="scale" )
public class FlatCheckBoxMenuItemIcon
extends FlatAbstractIcon
implements StyleableObject

View File

@@ -25,6 +25,7 @@ import javax.swing.AbstractButton;
import javax.swing.ButtonModel;
import javax.swing.UIManager;
import com.formdev.flatlaf.ui.FlatStylingSupport.Styleable;
import com.formdev.flatlaf.ui.FlatStylingSupport.StyleableField;
import com.formdev.flatlaf.ui.FlatStylingSupport.StyleableObject;
import com.formdev.flatlaf.ui.FlatUIUtils;
@@ -38,6 +39,7 @@ import com.formdev.flatlaf.ui.FlatUIUtils;
* @author Karl Tauber
* @since 1.5
*/
@StyleableField( cls=FlatAbstractIcon.class, key="clearIconScale", fieldName="scale" )
public class FlatClearIcon
extends FlatAbstractIcon
implements StyleableObject

View File

@@ -16,7 +16,6 @@
package com.formdev.flatlaf.icons;
import static com.formdev.flatlaf.util.UIScale.*;
import java.awt.BasicStroke;
import java.awt.Color;
import java.awt.Component;
@@ -27,7 +26,9 @@ import java.awt.geom.Path2D;
import javax.swing.UIManager;
import com.formdev.flatlaf.ui.FlatButtonUI;
import com.formdev.flatlaf.ui.FlatStylingSupport.Styleable;
import com.formdev.flatlaf.ui.FlatStylingSupport.StyleableField;
import com.formdev.flatlaf.ui.FlatStylingSupport.StyleableObject;
import com.formdev.flatlaf.util.UIScale;
import com.formdev.flatlaf.ui.FlatUIUtils;
/**
@@ -51,6 +52,7 @@ import com.formdev.flatlaf.ui.FlatUIUtils;
*
* @author Karl Tauber
*/
@StyleableField( cls=FlatAbstractIcon.class, key="scale" )
public class FlatHelpButtonIcon
extends FlatAbstractIcon
implements StyleableObject
@@ -152,12 +154,12 @@ public class FlatHelpButtonIcon
@Override
public int getIconWidth() {
return scale( iconSize() );
return scale( UIScale.scale( iconSize() ) );
}
@Override
public int getIconHeight() {
return scale( iconSize() );
return scale( UIScale.scale( iconSize() ) );
}
private int iconSize() {

View File

@@ -25,6 +25,7 @@ import javax.swing.JMenu;
import javax.swing.UIManager;
import com.formdev.flatlaf.ui.FlatUIUtils;
import com.formdev.flatlaf.ui.FlatStylingSupport.Styleable;
import com.formdev.flatlaf.ui.FlatStylingSupport.StyleableField;
import com.formdev.flatlaf.ui.FlatStylingSupport.StyleableObject;
/**
@@ -38,6 +39,7 @@ import com.formdev.flatlaf.ui.FlatStylingSupport.StyleableObject;
*
* @author Karl Tauber
*/
@StyleableField( cls=FlatAbstractIcon.class, key="scale" )
public class FlatMenuArrowIcon
extends FlatAbstractIcon
implements StyleableObject

View File

@@ -24,6 +24,7 @@ import java.awt.geom.Ellipse2D;
import javax.swing.UIManager;
import com.formdev.flatlaf.ui.FlatButtonUI;
import com.formdev.flatlaf.ui.FlatStylingSupport.Styleable;
import com.formdev.flatlaf.ui.FlatStylingSupport.StyleableField;
import com.formdev.flatlaf.ui.FlatStylingSupport.StyleableObject;
import com.formdev.flatlaf.ui.FlatUIUtils;
@@ -37,6 +38,7 @@ import com.formdev.flatlaf.ui.FlatUIUtils;
* @author Karl Tauber
* @since 1.5
*/
@StyleableField( cls=FlatAbstractIcon.class, key="searchIconScale", fieldName="scale" )
public class FlatSearchIcon
extends FlatAbstractIcon
implements StyleableObject

View File

@@ -25,6 +25,7 @@ import java.awt.geom.Path2D;
import javax.swing.UIManager;
import com.formdev.flatlaf.ui.FlatButtonUI;
import com.formdev.flatlaf.ui.FlatStylingSupport.Styleable;
import com.formdev.flatlaf.ui.FlatStylingSupport.StyleableField;
import com.formdev.flatlaf.ui.FlatStylingSupport.StyleableObject;
import com.formdev.flatlaf.ui.FlatUIUtils;
@@ -45,6 +46,7 @@ import com.formdev.flatlaf.ui.FlatUIUtils;
*
* @author Karl Tauber
*/
@StyleableField( cls=FlatAbstractIcon.class, key="closeScale", fieldName="scale" )
public class FlatTabbedPaneCloseIcon
extends FlatAbstractIcon
implements StyleableObject

View File

@@ -214,7 +214,7 @@ public class FlatPasswordFieldUI
/** @since 2 */
@Override
protected Object applyStyleProperty( String key, Object value ) {
if( key.equals( "capsLockIconColor" ) && capsLockIcon instanceof StyleableObject ) {
if( key.startsWith( "capsLockIcon" ) && capsLockIcon instanceof StyleableObject ) {
if( capsLockIconShared ) {
capsLockIcon = FlatStylingSupport.cloneIcon( capsLockIcon );
capsLockIconShared = false;
@@ -236,7 +236,7 @@ public class FlatPasswordFieldUI
@Override
public Object getStyleableValue( JComponent c, String key ) {
if( key.equals( "capsLockIconColor" ) && capsLockIcon instanceof StyleableObject )
if( key.startsWith( "capsLockIcon" ) && capsLockIcon instanceof StyleableObject )
return ((StyleableObject)capsLockIcon).getStyleableValue( key );
return super.getStyleableValue( c, key );

View File

@@ -228,10 +228,8 @@ public class FlatRadioButtonUI
public Map<String, Class<?>> getStyleableInfos( JComponent c ) {
Map<String, Class<?>> infos = FlatStylingSupport.getAnnotatedStyleableInfos( this );
Icon icon = getRealIcon( c );
if( icon instanceof StyleableObject ) {
for( Map.Entry<String, Class<?>> e : ((StyleableObject)icon).getStyleableInfos().entrySet() )
infos.put( "icon.".concat( e.getKey() ), e.getValue() );
}
if( icon instanceof StyleableObject )
FlatStylingSupport.putAllPrefixKey( infos, "icon.", ((StyleableObject)icon).getStyleableInfos() );
return infos;
}

View File

@@ -74,6 +74,8 @@ public class TestFlatStyleableInfo
//---- FlatHelpButtonIcon ----
expectedMap( expected,
"help.scale", float.class,
"help.focusWidth", int.class,
"help.focusColor", Color.class,
"help.innerFocusWidth", float.class,
@@ -413,6 +415,8 @@ public class TestFlatStyleableInfo
private void menuItem_checkIcon( Map<String, Class<?>> expected ) {
expectedMap( expected,
"icon.scale", float.class,
"icon.checkmarkColor", Color.class,
"icon.disabledCheckmarkColor", Color.class,
"selectionForeground", Color.class
@@ -421,6 +425,8 @@ public class TestFlatStyleableInfo
private void menuItem_arrowIcon( Map<String, Class<?>> expected ) {
expectedMap( expected,
"icon.scale", float.class,
"icon.arrowType", String.class,
"icon.arrowColor", Color.class,
"icon.disabledArrowColor", Color.class,
@@ -456,7 +462,8 @@ public class TestFlatStyleableInfo
expectedMap( expected,
// capsLockIcon
"capsLockIconColor", Color.class
"capsLockIconColor", Color.class,
"capsLockIconScale", float.class
);
// border
@@ -553,6 +560,8 @@ public class TestFlatStyleableInfo
//---- icon ----
"icon.scale", float.class,
"icon.focusWidth", float.class,
"icon.focusColor", Color.class,
"icon.borderWidth", float.class,
@@ -831,6 +840,7 @@ public class TestFlatStyleableInfo
"tabIconPlacement", int.class,
// FlatTabbedPaneCloseIcon
"closeScale", float.class,
"closeSize", Dimension.class,
"closeArc", int.class,
"closeCrossPlainSize", float.class,
@@ -1269,6 +1279,8 @@ public class TestFlatStyleableInfo
private void flatCheckBoxIcon( Map<String, Class<?>> expected ) {
expectedMap( expected,
"scale", float.class,
"focusWidth", float.class,
"focusColor", Color.class,
"borderWidth", float.class,
@@ -1353,6 +1365,8 @@ public class TestFlatStyleableInfo
private void flatCheckBoxMenuItemIcon( Map<String, Class<?>> expected ) {
expectedMap( expected,
"scale", float.class,
"checkmarkColor", Color.class,
"disabledCheckmarkColor", Color.class,
"selectionForeground", Color.class
@@ -1371,6 +1385,8 @@ public class TestFlatStyleableInfo
private void flatMenuArrowIcon( Map<String, Class<?>> expected ) {
expectedMap( expected,
"scale", float.class,
"arrowType", String.class,
"arrowColor", Color.class,
"disabledArrowColor", Color.class,
@@ -1383,6 +1399,8 @@ public class TestFlatStyleableInfo
FlatHelpButtonIcon icon = new FlatHelpButtonIcon();
Map<String, Class<?>> expected = expectedMap(
"scale", float.class,
"focusWidth", int.class,
"focusColor", Color.class,
"innerFocusWidth", float.class,
@@ -1409,6 +1427,8 @@ public class TestFlatStyleableInfo
FlatClearIcon icon = new FlatClearIcon();
Map<String, Class<?>> expected = expectedMap(
"clearIconScale", float.class,
"clearIconColor", Color.class,
"clearIconHoverColor", Color.class,
"clearIconPressedColor", Color.class
@@ -1439,6 +1459,8 @@ public class TestFlatStyleableInfo
private void flatSearchIcon( Map<String, Class<?>> expected ) {
expectedMap( expected,
"searchIconScale", float.class,
"searchIconColor", Color.class,
"searchIconHoverColor", Color.class,
"searchIconPressedColor", Color.class
@@ -1450,6 +1472,8 @@ public class TestFlatStyleableInfo
FlatCapsLockIcon icon = new FlatCapsLockIcon();
Map<String, Class<?>> expected = expectedMap(
"capsLockIconScale", float.class,
"capsLockIconColor", Color.class
);
@@ -1461,6 +1485,10 @@ public class TestFlatStyleableInfo
FlatTabbedPaneCloseIcon icon = new FlatTabbedPaneCloseIcon();
Map<String, Class<?>> expected = expectedMap(
//TODO closeScale ?
// "scale", float.class,
"closeScale", float.class,
"closeSize", Dimension.class,
"closeArc", int.class,
"closeCrossPlainSize", float.class,

View File

@@ -273,6 +273,8 @@ public class TestFlatStyleableValue
//---- FlatHelpButtonIcon ----
testFloat( c, ui, "help.scale" );
testInteger( c, ui, "help.focusWidth" );
testColor( c, ui, "help.focusColor" );
testFloat( c, ui, "help.innerFocusWidth" );
@@ -569,12 +571,16 @@ public class TestFlatStyleableValue
}
private void menuItem_checkIcon( JComponent c, StyleableUI ui ) {
testFloat( c, ui, "icon.scale" );
testColor( c, ui, "icon.checkmarkColor" );
testColor( c, ui, "icon.disabledCheckmarkColor" );
testColor( c, ui, "selectionForeground" );
}
private void menuItem_arrowIcon( JComponent c, StyleableUI ui ) {
testFloat( c, ui, "icon.scale" );
testString( c, ui, "icon.arrowType", "chevron" );
testColor( c, ui, "icon.arrowColor" );
testColor( c, ui, "icon.disabledArrowColor" );
@@ -603,6 +609,7 @@ public class TestFlatStyleableValue
testBoolean( c, ui, "showRevealButton" );
// capsLockIcon
testFloat( c, ui, "capsLockIconScale" );
testColor( c, ui, "capsLockIconColor" );
// border
@@ -694,6 +701,8 @@ public class TestFlatStyleableValue
return;
}
testFloat( b, ui, "icon.scale" );
testFloat( b, ui, "icon.focusWidth" );
testColor( b, ui, "icon.focusColor" );
testFloat( b, ui, "icon.borderWidth" );
@@ -952,6 +961,7 @@ public class TestFlatStyleableValue
testString( c, ui, "tabIconPlacement", "top" );
// FlatTabbedPaneCloseIcon
testFloat( c, ui, "closeScale" );
testDimension( c, ui, "closeSize" );
testInteger( c, ui, "closeArc" );
testFloat( c, ui, "closeCrossPlainSize" );
@@ -1383,6 +1393,8 @@ public class TestFlatStyleableValue
}
private void flatCheckBoxIcon( FlatCheckBoxIcon icon ) {
testValueFloat( icon, "scale" );
testValueFloat( icon, "focusWidth" );
testValueColor( icon, "focusColor" );
testValueFloat( icon, "borderWidth" );
@@ -1461,6 +1473,8 @@ public class TestFlatStyleableValue
}
private void flatCheckBoxMenuItemIcon( FlatCheckBoxMenuItemIcon icon ) {
testValueFloat( icon, "scale" );
testValueColor( icon, "checkmarkColor" );
testValueColor( icon, "disabledCheckmarkColor" );
testValueColor( icon, "selectionForeground" );
@@ -1471,6 +1485,8 @@ public class TestFlatStyleableValue
FlatMenuArrowIcon icon = new FlatMenuArrowIcon();
expectedStyleableInfos = icon.getStyleableInfos();
testValueFloat( icon, "scale" );
testValueString( icon, "arrowType", "chevron" );
testValueColor( icon, "arrowColor" );
testValueColor( icon, "disabledArrowColor" );
@@ -1482,6 +1498,8 @@ public class TestFlatStyleableValue
FlatHelpButtonIcon icon = new FlatHelpButtonIcon();
expectedStyleableInfos = icon.getStyleableInfos();
testValueFloat( icon, "scale" );
testValueInteger( icon, "focusWidth" );
testValueColor( icon, "focusColor" );
testValueFloat( icon, "innerFocusWidth" );
@@ -1505,6 +1523,8 @@ public class TestFlatStyleableValue
FlatClearIcon icon = new FlatClearIcon();
expectedStyleableInfos = icon.getStyleableInfos();
testValueFloat( icon, "clearIconScale" );
testValueColor( icon, "clearIconColor" );
testValueColor( icon, "clearIconHoverColor" );
testValueColor( icon, "clearIconPressedColor" );
@@ -1527,6 +1547,8 @@ public class TestFlatStyleableValue
}
private void flatSearchIcon( FlatSearchIcon icon ) {
testValueFloat( icon, "searchIconScale" );
testValueColor( icon, "searchIconColor" );
testValueColor( icon, "searchIconHoverColor" );
testValueColor( icon, "searchIconPressedColor" );
@@ -1537,6 +1559,7 @@ public class TestFlatStyleableValue
FlatCapsLockIcon icon = new FlatCapsLockIcon();
expectedStyleableInfos = icon.getStyleableInfos();
testValueFloat( icon, "capsLockIconScale" );
testValueColor( icon, "capsLockIconColor" );
}
@@ -1545,6 +1568,8 @@ public class TestFlatStyleableValue
FlatTabbedPaneCloseIcon icon = new FlatTabbedPaneCloseIcon();
expectedStyleableInfos = icon.getStyleableInfos();
testValueFloat( icon, "closeScale" );
testValueDimension( icon, "closeSize" );
testValueInteger( icon, "closeArc" );
testValueFloat( icon, "closeCrossPlainSize" );