Skip to content

Commit

Permalink
Merge branch 'JFormDesigner:main' into classx
Browse files Browse the repository at this point in the history
  • Loading branch information
MikOfClassX authored Jun 21, 2024
2 parents 2177aa6 + 0c0d4bf commit 95b22e7
Show file tree
Hide file tree
Showing 16 changed files with 327 additions and 101 deletions.
10 changes: 8 additions & 2 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,12 @@ FlatLaf Change Log
`<big>`, `<small>` and `<samp>` in HTML text for components Button, CheckBox,
RadioButton, MenuItem (and subclasses), JideLabel, JideButton, JXBusyLabel and
JXHyperlink. Also fixed for Label and ToolTip if using Java 11+.
- ScrollPane: Fixed/improved border painting at 125% - 175% scaling to avoid
different border thicknesses. (issue #743)
- Table: Fixed painting of alternating rows below table if auto-resize mode is
`JTable.AUTO_RESIZE_OFF` and table width is smaller than scroll pane (was not
updated when table width changed and was painted on wrong side in
right-to-left component orientation).
- Theme Editor: Fixed occasional empty window on startup on macOS.

#### Incompatibilities
Expand Down Expand Up @@ -88,8 +94,8 @@ FlatLaf Change Log
- Improved log messages for loading fails.
- Fonts: Updated **Inter** to
[v4.0](https://github.com/rsms/inter/releases/tag/v4.0).
- Table: Select all text in cell editor when starting editing using `F2` key.
(issue #652)
- Table: Select all text in cell editor when starting editing using `F2` key on
Windows or Linux. (issue #652)

#### Fixed bugs

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,7 @@ public void paintBorder( Component c, Graphics g, int x, int y, int width, int h
Paint borderColor = (outlineColor != null) ? outlineColor : getBorderColor( c );
FlatUIUtils.paintOutlinedComponent( g2, x, y, width, height,
focusWidth, 1, focusInnerWidth, borderWidth, arc,
focusColor, borderColor, null );
focusColor, borderColor, null, c instanceof JScrollPane );
} finally {
g2.dispose();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ public static void updateRendererCSSFontBaseSize( JComponent c ) {

/**
* Updates foreground in style sheet of the HTML view.
* Adds "body { color: #<foreground-hex>; }"
* Adds "body { color: #&lt;foreground-hex&gt;; }"
*/
public static void updateRendererCSSForeground( View view, Color foreground ) {
Document doc = view.getDocument();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -422,7 +422,7 @@ public static void paintThumb( Graphics g, JSlider slider, Rectangle thumbRect,
Color thumbColor, Color thumbBorderColor, Color focusedColor, float thumbBorderWidth, int focusWidth )
{
double systemScaleFactor = UIScale.getSystemScaleFactor( (Graphics2D) g );
if( systemScaleFactor != 1 && systemScaleFactor != 2 ) {
if( systemScaleFactor != (int) systemScaleFactor ) {
// paint at scale 1x to avoid clipping on right and bottom edges at 125%, 150% or 175%
HiDPIUtils.paintAtScale1x( (Graphics2D) g, thumbRect.x, thumbRect.y, thumbRect.width, thumbRect.height,
(g2d, x2, y2, width2, height2, scaleFactor) -> {
Expand Down
59 changes: 56 additions & 3 deletions flatlaf-core/src/main/java/com/formdev/flatlaf/ui/FlatTableUI.java
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,9 @@
import java.awt.Graphics2D;
import java.awt.Insets;
import java.awt.event.ActionEvent;
import java.awt.event.ComponentAdapter;
import java.awt.event.ComponentEvent;
import java.awt.event.ComponentListener;
import java.awt.event.FocusEvent;
import java.awt.event.FocusListener;
import java.awt.geom.Rectangle2D;
Expand Down Expand Up @@ -132,6 +135,7 @@ public class FlatTableUI
private TableCellRenderer oldBooleanRenderer;

private PropertyChangeListener propertyChangeListener;
private ComponentListener outsideAlternateRowsListener;
private Map<String, Object> oldStyleValues;

public static ComponentUI createUI( JComponent c ) {
Expand Down Expand Up @@ -266,6 +270,11 @@ protected void uninstallListeners() {

table.removePropertyChangeListener( propertyChangeListener );
propertyChangeListener = null;

if( outsideAlternateRowsListener != null ) {
table.removeComponentListener( outsideAlternateRowsListener );
outsideAlternateRowsListener = null;
}
}

@Override
Expand Down Expand Up @@ -513,8 +522,6 @@ public void paintViewport( Graphics g, JComponent c, JViewport viewport ) {
boolean paintOutside = UIManager.getBoolean( "Table.paintOutsideAlternateRows" );
Color alternateColor;
if( paintOutside && (alternateColor = UIManager.getColor( "Table.alternateRowColor" )) != null ) {
g.setColor( alternateColor );

int rowCount = table.getRowCount();

// paint alternating empty rows below the table
Expand All @@ -523,14 +530,60 @@ public void paintViewport( Graphics g, JComponent c, JViewport viewport ) {
int tableWidth = table.getWidth();
int rowHeight = table.getRowHeight();

g.setColor( alternateColor );

int x = viewport.getComponentOrientation().isLeftToRight() ? 0 : viewportWidth - tableWidth;
for( int y = tableHeight, row = rowCount; y < viewportHeight; y += rowHeight, row++ ) {
if( row % 2 != 0 )
g.fillRect( 0, y, tableWidth, rowHeight );
g.fillRect( x, y, tableWidth, rowHeight );
}

// add listener on demand
if( outsideAlternateRowsListener == null && table.getAutoResizeMode() == JTable.AUTO_RESIZE_OFF ) {
outsideAlternateRowsListener = new FlatOutsideAlternateRowsListener();
table.addComponentListener( outsideAlternateRowsListener );
}
}
}
}

//---- class OutsideAlternateRowsListener ---------------------------------

/**
* Used if table auto-resize-mode is off to repaint outside alternate rows
* when table width changed (column resized) or component orientation changed.
*/
private class FlatOutsideAlternateRowsListener
extends ComponentAdapter
{
@Override
public void componentHidden( ComponentEvent e ) {
Container viewport = SwingUtilities.getUnwrappedParent( table );
if( viewport instanceof JViewport )
viewport.repaint();
}

@Override
public void componentMoved( ComponentEvent e ) {
repaintAreaBelowTable();
}

@Override
public void componentResized( ComponentEvent e ) {
repaintAreaBelowTable();
}

private void repaintAreaBelowTable() {
Container viewport = SwingUtilities.getUnwrappedParent( table );
if( viewport instanceof JViewport ) {
int viewportHeight = viewport.getHeight();
int tableHeight = table.getHeight();
if( tableHeight < viewportHeight )
viewport.repaint( 0, tableHeight, viewport.getWidth(), viewportHeight - tableHeight );
}
}
}

//---- class FlatTablePropertyWatcher -------------------------------------

/**
Expand Down
37 changes: 32 additions & 5 deletions flatlaf-core/src/main/java/com/formdev/flatlaf/ui/FlatUIUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -601,28 +601,55 @@ public static void paintComponentBackground( Graphics2D g, int x, int y, int wid
public static void paintOutlinedComponent( Graphics2D g, int x, int y, int width, int height,
float focusWidth, float focusWidthFraction, float focusInnerWidth, float borderWidth, float arc,
Paint focusColor, Paint borderColor, Paint background )
{
paintOutlinedComponent( g, x, y, width, height, focusWidth, focusWidthFraction, focusInnerWidth,
borderWidth, arc, focusColor, borderColor, background, false );
}

static void paintOutlinedComponent( Graphics2D g, int x, int y, int width, int height,
float focusWidth, float focusWidthFraction, float focusInnerWidth, float borderWidth, float arc,
Paint focusColor, Paint borderColor, Paint background, boolean scrollPane )
{
double systemScaleFactor = UIScale.getSystemScaleFactor( g );
if( systemScaleFactor != 1 && systemScaleFactor != 2 ) {
if( (int) systemScaleFactor != systemScaleFactor ) {
// paint at scale 1x to avoid clipping on right and bottom edges at 125%, 150% or 175%
HiDPIUtils.paintAtScale1x( g, x, y, width, height,
(g2d, x2, y2, width2, height2, scaleFactor) -> {
paintOutlinedComponentImpl( g2d, x2, y2, width2, height2,
(float) (focusWidth * scaleFactor), focusWidthFraction, (float) (focusInnerWidth * scaleFactor),
(float) (borderWidth * scaleFactor), (float) (arc * scaleFactor),
focusColor, borderColor, background );
focusColor, borderColor, background, scrollPane, scaleFactor );
} );
return;
}

paintOutlinedComponentImpl( g, x, y, width, height, focusWidth, focusWidthFraction, focusInnerWidth,
borderWidth, arc, focusColor, borderColor, background );
borderWidth, arc, focusColor, borderColor, background, scrollPane, systemScaleFactor );
}

@SuppressWarnings( "SelfAssignment" ) // Error Prone
private static void paintOutlinedComponentImpl( Graphics2D g, int x, int y, int width, int height,
float focusWidth, float focusWidthFraction, float focusInnerWidth, float borderWidth, float arc,
Paint focusColor, Paint borderColor, Paint background )
Paint focusColor, Paint borderColor, Paint background, boolean scrollPane, double scaleFactor )
{
// Special handling for scrollpane and fractional scale factors (e.g. 1.25 - 1.75),
// where Swing scales one "logical" pixel (border insets) to either one or two physical pixels.
// Antialiasing is used to paint the border, which usually needs two physical pixels
// at small scale factors. 1px for the solid border and another 1px for antialiasing.
// But scrollpane view is painted over the border, which results in a painted border
// that is 1px thick at some sides and 2px thick at other sides.
if( scrollPane && scaleFactor != (int) scaleFactor ) {
if( focusWidth > 0 ) {
// reduce outer border thickness (focusWidth) so that inner side of
// component border (focusWidth + borderWidth) is at a full pixel
int totalWidth = (int) (focusWidth + borderWidth);
focusWidth = totalWidth - borderWidth;
} else {// if( scaleFactor > 1 && scaleFactor < 2 ) {
// reduce component border thickness (borderWidth) to full pixels
borderWidth = (int) borderWidth;
}
}

// outside bounds of the border and the background
float x1 = x + focusWidth;
float y1 = y + focusWidth;
Expand Down Expand Up @@ -780,7 +807,7 @@ public static void paintSelection( Graphics2D g, int x, int y, int width, int he

if( arcTopLeft > 0 || arcTopRight > 0 || arcBottomLeft > 0 || arcBottomRight > 0 ) {
double systemScaleFactor = UIScale.getSystemScaleFactor( g );
if( systemScaleFactor != 1 && systemScaleFactor != 2 ) {
if( systemScaleFactor != (int) systemScaleFactor ) {
// paint at scale 1x to avoid clipping on right and bottom edges at 125%, 150% or 175%
HiDPIUtils.paintAtScale1x( g, x, y, width, height,
(g2d, x2, y2, width2, height2, scaleFactor) -> {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -140,8 +140,8 @@ else if( SystemInfo.isMacOS )
registerSwitchToLookAndFeel( KeyEvent.VK_F9, "com.apple.laf.AquaLookAndFeel" );
else if( SystemInfo.isLinux )
registerSwitchToLookAndFeel( KeyEvent.VK_F9, "com.sun.java.swing.plaf.gtk.GTKLookAndFeel" );
registerSwitchToLookAndFeel( KeyEvent.VK_F12, MetalLookAndFeel.class.getName() );
registerSwitchToLookAndFeel( KeyEvent.VK_F11, NimbusLookAndFeel.class.getName() );
registerSwitchToLookAndFeel( KeyEvent.VK_F12, MetalLookAndFeel.class.getName() );

// register Alt+UP and Alt+DOWN to switch to previous/next theme
((JComponent)frame.getContentPane()).registerKeyboardAction(
Expand All @@ -153,6 +153,9 @@ else if( SystemInfo.isLinux )
KeyStroke.getKeyStroke( KeyEvent.VK_DOWN, KeyEvent.ALT_DOWN_MASK ),
JComponent.WHEN_ANCESTOR_OF_FOCUSED_COMPONENT );

// register Alt+Shift+F1, F2, ... keys to change system scale factor
DemoPrefs.registerSystemScaleFactors( frame );

// register ESC key to close frame
((JComponent)frame.getContentPane()).registerKeyboardAction(
e -> {
Expand Down
68 changes: 68 additions & 0 deletions flatlaf-demo/src/main/java/com/formdev/flatlaf/demo/DemoPrefs.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,10 @@
import java.io.File;
import java.io.FileInputStream;
import java.util.prefs.Preferences;
import javax.swing.JComponent;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.KeyStroke;
import javax.swing.UIManager;
import com.formdev.flatlaf.FlatLaf;
import com.formdev.flatlaf.FlatLightLaf;
Expand All @@ -27,6 +31,7 @@
import com.formdev.flatlaf.demo.intellijthemes.IJThemesPanel;
import com.formdev.flatlaf.util.LoggingFacade;
import com.formdev.flatlaf.util.StringUtils;
import com.formdev.flatlaf.util.SystemInfo;

/**
* @author Karl Tauber
Expand All @@ -35,6 +40,7 @@ public class DemoPrefs
{
public static final String KEY_LAF = "laf";
public static final String KEY_LAF_THEME = "lafTheme";
public static final String KEY_SYSTEM_SCALE_FACTOR = "systemScaleFactor";

public static final String RESOURCE_PREFIX = "res:";
public static final String FILE_PREFIX = "file:";
Expand Down Expand Up @@ -96,4 +102,66 @@ else if( theme.startsWith( FILE_PREFIX ) )
state.put( KEY_LAF, UIManager.getLookAndFeel().getClass().getName() );
} );
}

public static void initSystemScale() {
if( System.getProperty( "sun.java2d.uiScale" ) == null ) {
String scaleFactor = getState().get( KEY_SYSTEM_SCALE_FACTOR, null );
if( scaleFactor != null )
System.setProperty( "sun.java2d.uiScale", scaleFactor );
}
}

/**
* register Alt+Shift+F1, F2, ... F12 keys to change system scale factor
*/
public static void registerSystemScaleFactors( JFrame frame ) {
registerSystemScaleFactor( frame, "alt shift F1", null );
registerSystemScaleFactor( frame, "alt shift F2", "1" );

if( SystemInfo.isWindows ) {
registerSystemScaleFactor( frame, "alt shift F3", "1.25" );
registerSystemScaleFactor( frame, "alt shift F4", "1.5" );
registerSystemScaleFactor( frame, "alt shift F5", "1.75" );
registerSystemScaleFactor( frame, "alt shift F6", "2" );
registerSystemScaleFactor( frame, "alt shift F7", "2.25" );
registerSystemScaleFactor( frame, "alt shift F8", "2.5" );
registerSystemScaleFactor( frame, "alt shift F9", "2.75" );
registerSystemScaleFactor( frame, "alt shift F10", "3" );
registerSystemScaleFactor( frame, "alt shift F11", "3.5" );
registerSystemScaleFactor( frame, "alt shift F12", "4" );
} else {
// Java on macOS and Linux supports only integer scale factors
registerSystemScaleFactor( frame, "alt shift F3", "2" );
registerSystemScaleFactor( frame, "alt shift F4", "3" );
registerSystemScaleFactor( frame, "alt shift F5", "4" );

}
}

private static void registerSystemScaleFactor( JFrame frame, String keyStrokeStr, String scaleFactor ) {
KeyStroke keyStroke = KeyStroke.getKeyStroke( keyStrokeStr );
if( keyStroke == null )
throw new IllegalArgumentException( "Invalid key stroke '" + keyStrokeStr + "'" );

((JComponent)frame.getContentPane()).registerKeyboardAction(
e -> applySystemScaleFactor( frame, scaleFactor ),
keyStroke,
JComponent.WHEN_ANCESTOR_OF_FOCUSED_COMPONENT );
}

private static void applySystemScaleFactor( JFrame frame, String scaleFactor ) {
if( JOptionPane.showConfirmDialog( frame,
"Change system scale factor to "
+ (scaleFactor != null ? scaleFactor : "default")
+ " and exit?",
frame.getTitle(), JOptionPane.YES_NO_OPTION ) != JOptionPane.YES_OPTION )
return;

if( scaleFactor != null )
DemoPrefs.getState().put( KEY_SYSTEM_SCALE_FACTOR, scaleFactor );
else
DemoPrefs.getState().remove( KEY_SYSTEM_SCALE_FACTOR );

System.exit( 0 );
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -70,9 +70,10 @@ public static void main( String[] args ) {
if( FlatLafDemo.screenshotsMode && !SystemInfo.isJava_9_orLater && System.getProperty( "flatlaf.uiScale" ) == null )
System.setProperty( "flatlaf.uiScale", "2x" );

SwingUtilities.invokeLater( () -> {
DemoPrefs.init( PREFS_ROOT_PATH );
DemoPrefs.init( PREFS_ROOT_PATH );
DemoPrefs.initSystemScale();

SwingUtilities.invokeLater( () -> {
// install fonts for lazy loading
FlatInterFont.installLazy();
FlatJetBrainsMonoFont.installLazy();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,8 @@
import com.formdev.flatlaf.FlatLightLaf;
import com.formdev.flatlaf.extras.FlatInspector;
import com.formdev.flatlaf.extras.components.FlatTriStateCheckBox;
import com.formdev.flatlaf.themes.FlatMacDarkLaf;
import com.formdev.flatlaf.themes.FlatMacLightLaf;
import com.formdev.flatlaf.ui.FlatLineBorder;
import com.formdev.flatlaf.util.SystemInfo;
import net.miginfocom.swing.*;
Expand Down Expand Up @@ -161,6 +163,8 @@ public void windowDeactivated( WindowEvent e ) {
registerSwitchToLookAndFeel( "F2", FlatDarkLaf.class.getName() );
registerSwitchToLookAndFeel( "F3", FlatIntelliJLaf.class.getName() );
registerSwitchToLookAndFeel( "F4", FlatDarculaLaf.class.getName() );
registerSwitchToLookAndFeel( "F5", FlatMacLightLaf.class.getName() );
registerSwitchToLookAndFeel( "F6", FlatMacDarkLaf.class.getName() );

registerSwitchToLookAndFeel( "F8", FlatTestLaf.class.getName() );

Expand All @@ -170,8 +174,8 @@ else if( SystemInfo.isMacOS )
registerSwitchToLookAndFeel( "F9", "com.apple.laf.AquaLookAndFeel" );
else if( SystemInfo.isLinux )
registerSwitchToLookAndFeel( "F9", "com.sun.java.swing.plaf.gtk.GTKLookAndFeel" );
registerSwitchToLookAndFeel( "F12", MetalLookAndFeel.class.getName() );
registerSwitchToLookAndFeel( "F11", NimbusLookAndFeel.class.getName() );
registerSwitchToLookAndFeel( "F12", MetalLookAndFeel.class.getName() );
}

private void updateInfo() {
Expand Down
Loading

0 comments on commit 95b22e7

Please sign in to comment.