TextField, FormattedTextField and PasswordField: support round border (issue #65)

This commit is contained in:
Karl Tauber
2020-02-17 22:39:16 +01:00
parent f736ed401f
commit b522500379
6 changed files with 66 additions and 9 deletions

View File

@@ -1,6 +1,12 @@
FlatLaf Change Log
==================
## Unreleased
- TextField, FormattedTextField and PasswordField: Support round borders (see UI
default value `TextComponent.arc`). (issue #65)
## 0.27
- Support `JInternalFrame` and `JDesktopPane`. (issues #39 and #11)

View File

@@ -39,6 +39,7 @@ import javax.swing.plaf.ComponentUI;
*
* <!-- FlatTextFieldUI -->
*
* @uiDefault TextComponent.arc int
* @uiDefault Component.focusWidth int
* @uiDefault Component.minimumWidth int
* @uiDefault Component.isIntelliJTheme boolean

View File

@@ -51,6 +51,7 @@ import com.formdev.flatlaf.FlatClientProperties;
*
* <!-- FlatPasswordFieldUI -->
*
* @uiDefault TextComponent.arc int
* @uiDefault Component.focusWidth int
* @uiDefault Component.minimumWidth int
* @uiDefault Component.isIntelliJTheme boolean
@@ -62,6 +63,7 @@ import com.formdev.flatlaf.FlatClientProperties;
public class FlatPasswordFieldUI
extends BasicPasswordFieldUI
{
protected int arc;
protected int focusWidth;
protected int minimumWidth;
protected boolean isIntelliJTheme;
@@ -78,6 +80,7 @@ public class FlatPasswordFieldUI
super.installDefaults();
String prefix = getPropertyPrefix();
arc = UIManager.getInt( "TextComponent.arc" );
focusWidth = UIManager.getInt( "Component.focusWidth" );
minimumWidth = UIManager.getInt( "Component.minimumWidth" );
isIntelliJTheme = UIManager.getBoolean( "Component.isIntelliJTheme" );
@@ -128,7 +131,7 @@ public class FlatPasswordFieldUI
@Override
protected void paintSafely( Graphics g ) {
FlatTextFieldUI.paintBackground( g, getComponent(), focusWidth, isIntelliJTheme );
FlatTextFieldUI.paintBackground( g, getComponent(), focusWidth, arc, isIntelliJTheme );
FlatTextFieldUI.paintPlaceholder( g, getComponent(), placeholderForeground );
super.paintSafely( g );
}

View File

@@ -0,0 +1,39 @@
/*
* Copyright 2019 FormDev Software GmbH
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.formdev.flatlaf.ui;
import static com.formdev.flatlaf.util.UIScale.scale;
import java.awt.Component;
import javax.swing.UIManager;
/**
* Border for various text components (e.g. {@link javax.swing.JTextField}).
*
* @uiDefault Component.arc int
*
* @author Karl Tauber
*/
public class FlatTextBorder
extends FlatBorder
{
protected final int arc = UIManager.getInt( "TextComponent.arc" );
@Override
protected float getArc( Component c ) {
return scale( (float) arc );
}
}

View File

@@ -32,6 +32,7 @@ import javax.swing.JSpinner;
import javax.swing.JTextField;
import javax.swing.LookAndFeel;
import javax.swing.UIManager;
import javax.swing.border.Border;
import javax.swing.plaf.ComponentUI;
import javax.swing.plaf.UIResource;
import javax.swing.plaf.basic.BasicTextFieldUI;
@@ -59,6 +60,7 @@ import com.formdev.flatlaf.FlatClientProperties;
*
* <!-- FlatTextFieldUI -->
*
* @uiDefault TextComponent.arc int
* @uiDefault Component.focusWidth int
* @uiDefault Component.minimumWidth int
* @uiDefault Component.isIntelliJTheme boolean
@@ -70,6 +72,7 @@ import com.formdev.flatlaf.FlatClientProperties;
public class FlatTextFieldUI
extends BasicTextFieldUI
{
protected int arc;
protected int focusWidth;
protected int minimumWidth;
protected boolean isIntelliJTheme;
@@ -86,6 +89,7 @@ public class FlatTextFieldUI
super.installDefaults();
String prefix = getPropertyPrefix();
arc = UIManager.getInt( "TextComponent.arc" );
focusWidth = UIManager.getInt( "Component.focusWidth" );
minimumWidth = UIManager.getInt( "Component.minimumWidth" );
isIntelliJTheme = UIManager.getBoolean( "Component.isIntelliJTheme" );
@@ -136,7 +140,7 @@ public class FlatTextFieldUI
@Override
protected void paintSafely( Graphics g ) {
paintBackground( g, getComponent(), focusWidth, isIntelliJTheme );
paintBackground( g, getComponent(), focusWidth, arc, isIntelliJTheme );
paintPlaceholder( g, getComponent(), placeholderForeground );
super.paintSafely( g );
}
@@ -146,13 +150,15 @@ public class FlatTextFieldUI
// background is painted elsewhere
}
static void paintBackground( Graphics g, JTextComponent c, int focusWidth, boolean isIntelliJTheme ) {
static void paintBackground( Graphics g, JTextComponent c, int focusWidth, int arc, boolean isIntelliJTheme ) {
Border border = c.getBorder();
// do not paint background if:
// - not opaque and
// - border is not a flat border and
// - opaque was explicitly set (to false)
// (same behaviour as in AquaTextFieldUI)
if( !c.isOpaque() && !(c.getBorder() instanceof FlatBorder) && FlatUIUtils.hasOpaqueBeenExplicitlySet( c ) )
if( !c.isOpaque() && !(border instanceof FlatBorder) && FlatUIUtils.hasOpaqueBeenExplicitlySet( c ) )
return;
// fill background if opaque to avoid garbage if user sets opaque to true
@@ -164,7 +170,8 @@ public class FlatTextFieldUI
try {
FlatUIUtils.setRenderingHints( g2 );
float fFocusWidth = (c.getBorder() instanceof FlatBorder) ? scale( (float) focusWidth ) : 0;
float fFocusWidth = (border instanceof FlatBorder) ? scale( (float) focusWidth ) : 0;
float fArc = (border instanceof FlatTextBorder) ? scale( (float) arc ) : 0;
Color background = c.getBackground();
g2.setColor( !(background instanceof UIResource)
@@ -172,7 +179,7 @@ public class FlatTextFieldUI
: (isIntelliJTheme && (!c.isEnabled() || !c.isEditable())
? FlatUIUtils.getParentBackground( c )
: background) );
FlatUIUtils.paintComponentBackground( g2, 0, 0, c.getWidth(), c.getHeight(), fFocusWidth, 0 );
FlatUIUtils.paintComponentBackground( g2, 0, 0, c.getWidth(), c.getHeight(), fFocusWidth, fArc );
} finally {
g2.dispose();
}

View File

@@ -189,7 +189,7 @@ FileView.floppyDriveIcon=com.formdev.flatlaf.icons.FlatFileViewFloppyDriveIcon
#---- FormattedTextField ----
FormattedTextField.border=com.formdev.flatlaf.ui.FlatBorder
FormattedTextField.border=com.formdev.flatlaf.ui.FlatTextBorder
FormattedTextField.margin=@textComponentMargin
FormattedTextField.background=@textComponentBackground
FormattedTextField.placeholderForeground=@disabledText
@@ -296,7 +296,7 @@ OptionPane.warningIcon=com.formdev.flatlaf.icons.FlatOptionPaneWarningIcon
#---- PasswordField ----
PasswordField.border=com.formdev.flatlaf.ui.FlatBorder
PasswordField.border=com.formdev.flatlaf.ui.FlatTextBorder
PasswordField.margin=@textComponentMargin
PasswordField.background=@textComponentBackground
PasswordField.placeholderForeground=@disabledText
@@ -457,11 +457,12 @@ TextArea.background=@textComponentBackground
# allowed values: "never", "once" (default) or "always"
TextComponent.selectAllOnFocusPolicy=once
TextComponent.arc=0
#---- TextField ----
TextField.border=com.formdev.flatlaf.ui.FlatBorder
TextField.border=com.formdev.flatlaf.ui.FlatTextBorder
TextField.margin=@textComponentMargin
TextField.background=@textComponentBackground
TextField.placeholderForeground=@disabledText