From 1b3cc223daaba6dddf8a14691387c59e3c2f8f0c Mon Sep 17 00:00:00 2001 From: Karl Tauber Date: Sun, 12 Dec 2021 18:43:29 +0100 Subject: [PATCH] TextField: added styles for buttons and toolbars in leading and trailing components (smaller margins/insets, better hover/pressed/selected colors) --- .../com/formdev/flatlaf/UIDefaultsLoader.java | 6 ++++ .../formdev/flatlaf/ui/FlatTextFieldUI.java | 35 ++++++++++++++----- .../formdev/flatlaf/FlatDarkLaf.properties | 16 +++++++++ .../com/formdev/flatlaf/FlatLaf.properties | 18 ++++++++++ .../formdev/flatlaf/FlatLightLaf.properties | 16 +++++++++ .../dumps/uidefaults/FlatDarkLaf_1.8.0.txt | 20 +++++++++++ .../dumps/uidefaults/FlatLightLaf_1.8.0.txt | 20 +++++++++++ .../dumps/uidefaults/FlatTestLaf_1.8.0.txt | 20 +++++++++++ .../flatlaf/testing/FlatTestLaf.properties | 12 +++++++ .../flatlaf/themeeditor/FlatLafUIKeys.txt | 4 +++ 10 files changed, 159 insertions(+), 8 deletions(-) diff --git a/flatlaf-core/src/main/java/com/formdev/flatlaf/UIDefaultsLoader.java b/flatlaf-core/src/main/java/com/formdev/flatlaf/UIDefaultsLoader.java index 8e641c95..165c407f 100644 --- a/flatlaf-core/src/main/java/com/formdev/flatlaf/UIDefaultsLoader.java +++ b/flatlaf-core/src/main/java/com/formdev/flatlaf/UIDefaultsLoader.java @@ -364,6 +364,12 @@ class UIDefaultsLoader if( resultValueType == null ) resultValueType = tempResultValueType; + // do not parse styles here + if( key.startsWith( "[style]" ) ) { + resultValueType[0] = ValueType.STRING; + return value; + } + value = value.trim(); // null diff --git a/flatlaf-core/src/main/java/com/formdev/flatlaf/ui/FlatTextFieldUI.java b/flatlaf-core/src/main/java/com/formdev/flatlaf/ui/FlatTextFieldUI.java index 3f939472..b6946080 100644 --- a/flatlaf-core/src/main/java/com/formdev/flatlaf/ui/FlatTextFieldUI.java +++ b/flatlaf-core/src/main/java/com/formdev/flatlaf/ui/FlatTextFieldUI.java @@ -21,6 +21,7 @@ import static com.formdev.flatlaf.util.UIScale.scale; import java.awt.Color; import java.awt.Component; import java.awt.Container; +import java.awt.Cursor; import java.awt.Dimension; import java.awt.FontMetrics; import java.awt.Graphics; @@ -35,10 +36,13 @@ import java.util.Map; import java.util.Objects; import java.util.concurrent.atomic.AtomicBoolean; import javax.swing.Icon; +import javax.swing.JButton; import javax.swing.JComboBox; import javax.swing.JComponent; import javax.swing.JSpinner; import javax.swing.JTextField; +import javax.swing.JToggleButton; +import javax.swing.JToolBar; import javax.swing.LookAndFeel; import javax.swing.UIManager; import javax.swing.plaf.ComponentUI; @@ -556,19 +560,19 @@ debug*/ // remove width of leading/trailing components JComponent leftComponent = ltr ? leadingComponent : trailingComponent; JComponent rightComponent = ltr ? trailingComponent : leadingComponent; - if( leftComponent != null ) { + boolean leftVisible = leftComponent != null && leftComponent.isVisible(); + boolean rightVisible = rightComponent != null && rightComponent.isVisible(); + if( leftVisible ) { int w = leftComponent.getPreferredSize().width; r.x += w; r.width -= w; } - if( rightComponent != null ) + if( rightVisible ) r.width -= rightComponent.getPreferredSize().width; // if a leading/trailing icons (or components) are shown, then the left/right margins are reduced // to the top margin, which places the icon nicely centered on left/right side - if( (ltr ? hasLeadingIcon() : hasTrailingIcon()) || - (leftComponent != null && leftComponent.isVisible()) ) - { + if( leftVisible || (ltr ? hasLeadingIcon() : hasTrailingIcon()) ) { // reduce left margin Insets margin = getComponent().getMargin(); int newLeftMargin = Math.min( margin.left, margin.top ); @@ -578,9 +582,7 @@ debug*/ r.width += diff; } } - if( (ltr ? hasTrailingIcon() : hasLeadingIcon()) || - (rightComponent != null && rightComponent.isVisible()) ) - { + if( rightVisible || (ltr ? hasTrailingIcon() : hasLeadingIcon()) ) { // reduce right margin Insets margin = getComponent().getMargin(); int newRightMargin = Math.min( margin.right, margin.top ); @@ -636,6 +638,7 @@ debug*/ JTextComponent c = getComponent(); leadingComponent = clientProperty( c, TEXT_FIELD_LEADING_COMPONENT, null, JComponent.class ); if( leadingComponent != null ) { + prepareLeadingOrTrailingComponent( leadingComponent ); installLayout(); c.add( leadingComponent ); } @@ -646,6 +649,7 @@ debug*/ JTextComponent c = getComponent(); trailingComponent = clientProperty( c, TEXT_FIELD_TRAILING_COMPONENT, null, JComponent.class ); if( trailingComponent != null ) { + prepareLeadingOrTrailingComponent( trailingComponent ); installLayout(); c.add( trailingComponent ); } @@ -667,6 +671,21 @@ debug*/ } } + /** @since 2 */ + protected void prepareLeadingOrTrailingComponent( JComponent c ) { + c.putClientProperty( STYLE_CLASS, "inTextField" ); + c.setCursor( Cursor.getDefaultCursor() ); + + if( c instanceof JButton || c instanceof JToggleButton ) + c.putClientProperty( BUTTON_TYPE, BUTTON_TYPE_TOOLBAR_BUTTON ); + else if( c instanceof JToolBar ) { + for( Component child : c.getComponents() ) { + if( child instanceof JComponent ) + ((JComponent)child).putClientProperty( STYLE_CLASS, "inTextField" ); + } + } + } + private void installLayout() { JTextComponent c = getComponent(); LayoutManager oldLayout = c.getLayout(); diff --git a/flatlaf-core/src/main/resources/com/formdev/flatlaf/FlatDarkLaf.properties b/flatlaf-core/src/main/resources/com/formdev/flatlaf/FlatDarkLaf.properties index 82c080a7..9307635e 100644 --- a/flatlaf-core/src/main/resources/com/formdev/flatlaf/FlatDarkLaf.properties +++ b/flatlaf-core/src/main/resources/com/formdev/flatlaf/FlatDarkLaf.properties @@ -355,3 +355,19 @@ ToolTip.background = shade(@background,50%) #---- Tree ---- Tree.hash = lighten($Tree.background,5%) + + + +#---- Styles ------------------------------------------------------------------ + +#---- inTextField ---- +# for leading/trailing components in text fields + +[style]Button.inTextField = \ + focusable: false; \ + toolbar.margin: 1,1,1,1; \ + toolbar.spacingInsets: 1,1,1,1; \ + background: $TextField.background; \ + toolbar.hoverBackground: lighten($TextField.background,4%,derived); \ + toolbar.pressedBackground: lighten($TextField.background,6%,derived); \ + toolbar.selectedBackground: lighten($TextField.background,12%,derived) diff --git a/flatlaf-core/src/main/resources/com/formdev/flatlaf/FlatLaf.properties b/flatlaf-core/src/main/resources/com/formdev/flatlaf/FlatLaf.properties index 33340e78..53879b71 100644 --- a/flatlaf-core/src/main/resources/com/formdev/flatlaf/FlatLaf.properties +++ b/flatlaf-core/src/main/resources/com/formdev/flatlaf/FlatLaf.properties @@ -227,6 +227,7 @@ Button.defaultButtonFollowsFocus = false Button.borderWidth = 1 Button.default.borderWidth = 1 +# for buttons in toolbars Button.toolbar.margin = 3,3,3,3 Button.toolbar.spacingInsets = 1,2,1,2 @@ -889,3 +890,20 @@ Tree.icon.collapsedColor = @icon Tree.icon.leafColor = @icon Tree.icon.closedColor = @icon Tree.icon.openColor = @icon + + + +#---- Styles ------------------------------------------------------------------ + +#---- inTextField ---- +# for leading/trailing components in text fields + +[style]ToggleButton.inTextField = $[style]Button.inTextField + +[style]ToolBar.inTextField = \ + floatable: false; \ + opaque: false; \ + borderMargins: 0,0,0,0 + +[style]ToolBarSeparator.inTextField = \ + separatorWidth: 3 diff --git a/flatlaf-core/src/main/resources/com/formdev/flatlaf/FlatLightLaf.properties b/flatlaf-core/src/main/resources/com/formdev/flatlaf/FlatLightLaf.properties index a0ab8a28..9be66470 100644 --- a/flatlaf-core/src/main/resources/com/formdev/flatlaf/FlatLightLaf.properties +++ b/flatlaf-core/src/main/resources/com/formdev/flatlaf/FlatLightLaf.properties @@ -362,3 +362,19 @@ ToolTip.background = lighten(@background,3%) #---- Tree ---- Tree.hash = darken($Tree.background,10%) + + + +#---- Styles ------------------------------------------------------------------ + +#---- inTextField ---- +# for leading/trailing components in text fields + +[style]Button.inTextField = \ + focusable: false; \ + toolbar.margin: 1,1,1,1; \ + toolbar.spacingInsets: 1,1,1,1; \ + background: $TextField.background; \ + toolbar.hoverBackground: darken($TextField.background,4%,derived); \ + toolbar.pressedBackground: darken($TextField.background,8%,derived); \ + toolbar.selectedBackground: darken($TextField.background,12%,derived) diff --git a/flatlaf-testing/dumps/uidefaults/FlatDarkLaf_1.8.0.txt b/flatlaf-testing/dumps/uidefaults/FlatDarkLaf_1.8.0.txt index 5d305543..7cf0ed6a 100644 --- a/flatlaf-testing/dumps/uidefaults/FlatDarkLaf_1.8.0.txt +++ b/flatlaf-testing/dumps/uidefaults/FlatDarkLaf_1.8.0.txt @@ -1419,6 +1419,26 @@ ViewportUI com.formdev.flatlaf.ui.FlatViewportUI [style].small font: $small.font +#---- [style]Button ---- + +[style]Button.inTextField focusable: false; toolbar.margin: 1,1,1,1; toolbar.spacingInsets: 1,1,1,1; background: $TextField.background; toolbar.hoverBackground: lighten($TextField.background,4%,derived); toolbar.pressedBackground: lighten($TextField.background,6%,derived); toolbar.selectedBackground: lighten($TextField.background,12%,derived) + + +#---- [style]ToggleButton ---- + +[style]ToggleButton.inTextField focusable: false; toolbar.margin: 1,1,1,1; toolbar.spacingInsets: 1,1,1,1; background: $TextField.background; toolbar.hoverBackground: lighten($TextField.background,4%,derived); toolbar.pressedBackground: lighten($TextField.background,6%,derived); toolbar.selectedBackground: lighten($TextField.background,12%,derived) + + +#---- [style]ToolBar ---- + +[style]ToolBar.inTextField floatable: false; opaque: false; borderMargins: 0,0,0,0 + + +#---- [style]ToolBarSeparator ---- + +[style]ToolBarSeparator.inTextField separatorWidth: 3 + + #---- ---- activeCaption #434e60 HSL 217 18 32 javax.swing.plaf.ColorUIResource [UI] diff --git a/flatlaf-testing/dumps/uidefaults/FlatLightLaf_1.8.0.txt b/flatlaf-testing/dumps/uidefaults/FlatLightLaf_1.8.0.txt index d9efdd96..c0ae8ece 100644 --- a/flatlaf-testing/dumps/uidefaults/FlatLightLaf_1.8.0.txt +++ b/flatlaf-testing/dumps/uidefaults/FlatLightLaf_1.8.0.txt @@ -1424,6 +1424,26 @@ ViewportUI com.formdev.flatlaf.ui.FlatViewportUI [style].small font: $small.font +#---- [style]Button ---- + +[style]Button.inTextField focusable: false; toolbar.margin: 1,1,1,1; toolbar.spacingInsets: 1,1,1,1; background: $TextField.background; toolbar.hoverBackground: darken($TextField.background,4%,derived); toolbar.pressedBackground: darken($TextField.background,8%,derived); toolbar.selectedBackground: darken($TextField.background,12%,derived) + + +#---- [style]ToggleButton ---- + +[style]ToggleButton.inTextField focusable: false; toolbar.margin: 1,1,1,1; toolbar.spacingInsets: 1,1,1,1; background: $TextField.background; toolbar.hoverBackground: darken($TextField.background,4%,derived); toolbar.pressedBackground: darken($TextField.background,8%,derived); toolbar.selectedBackground: darken($TextField.background,12%,derived) + + +#---- [style]ToolBar ---- + +[style]ToolBar.inTextField floatable: false; opaque: false; borderMargins: 0,0,0,0 + + +#---- [style]ToolBarSeparator ---- + +[style]ToolBarSeparator.inTextField separatorWidth: 3 + + #---- ---- activeCaption #99b4d1 HSL 211 38 71 javax.swing.plaf.ColorUIResource [UI] diff --git a/flatlaf-testing/dumps/uidefaults/FlatTestLaf_1.8.0.txt b/flatlaf-testing/dumps/uidefaults/FlatTestLaf_1.8.0.txt index 43836b5f..be298499 100644 --- a/flatlaf-testing/dumps/uidefaults/FlatTestLaf_1.8.0.txt +++ b/flatlaf-testing/dumps/uidefaults/FlatTestLaf_1.8.0.txt @@ -1437,6 +1437,26 @@ ViewportUI com.formdev.flatlaf.ui.FlatViewportUI [style].small font: $small.font +#---- [style]Button ---- + +[style]Button.inTextField focusable: false; toolbar.margin: 1,1,1,1; toolbar.spacingInsets: 1,1,1,1 + + +#---- [style]ToggleButton ---- + +[style]ToggleButton.inTextField focusable: false; toolbar.margin: 1,1,1,1; toolbar.spacingInsets: 1,1,1,1 + + +#---- [style]ToolBar ---- + +[style]ToolBar.inTextField floatable: false; opaque: false; borderMargins: 0,0,0,0 + + +#---- [style]ToolBarSeparator ---- + +[style]ToolBarSeparator.inTextField separatorWidth: 3 + + #---- ---- activeCaption #99b4d1 HSL 211 38 71 javax.swing.plaf.ColorUIResource [UI] diff --git a/flatlaf-testing/src/main/resources/com/formdev/flatlaf/testing/FlatTestLaf.properties b/flatlaf-testing/src/main/resources/com/formdev/flatlaf/testing/FlatTestLaf.properties index 9e47fa47..98fd7fdb 100644 --- a/flatlaf-testing/src/main/resources/com/formdev/flatlaf/testing/FlatTestLaf.properties +++ b/flatlaf-testing/src/main/resources/com/formdev/flatlaf/testing/FlatTestLaf.properties @@ -443,3 +443,15 @@ Tree.paintLines = true Tree.hash = #f00 Tree.editorBorder = 1,1,1,1,#f00 Tree.editorBorderSelectionColor = #00f + + + +#---- Styles ------------------------------------------------------------------ + +#---- inTextField ---- +# for leading/trailing components in text fields + +[style]Button.inTextField = \ + focusable: false; \ + toolbar.margin: 1,1,1,1; \ + toolbar.spacingInsets: 1,1,1,1 diff --git a/flatlaf-theme-editor/src/main/resources/com/formdev/flatlaf/themeeditor/FlatLafUIKeys.txt b/flatlaf-theme-editor/src/main/resources/com/formdev/flatlaf/themeeditor/FlatLafUIKeys.txt index c9e7c778..3257247b 100644 --- a/flatlaf-theme-editor/src/main/resources/com/formdev/flatlaf/themeeditor/FlatLafUIKeys.txt +++ b/flatlaf-theme-editor/src/main/resources/com/formdev/flatlaf/themeeditor/FlatLafUIKeys.txt @@ -1115,6 +1115,10 @@ ViewportUI [style].monospaced [style].semibold [style].small +[style]Button.inTextField +[style]ToggleButton.inTextField +[style]ToolBar.inTextField +[style]ToolBarSeparator.inTextField activeCaption activeCaptionBorder activeCaptionText