TextField: added styles for buttons and toolbars in leading and trailing components (smaller margins/insets, better hover/pressed/selected colors)

This commit is contained in:
Karl Tauber
2021-12-12 18:43:29 +01:00
parent 51be7ad832
commit 1b3cc223da
10 changed files with 159 additions and 8 deletions

View File

@@ -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

View File

@@ -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();

View File

@@ -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)

View File

@@ -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

View File

@@ -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)

View File

@@ -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]

View File

@@ -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]

View File

@@ -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]

View File

@@ -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

View File

@@ -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