PasswordField: warn about enabled Caps Lock

This commit is contained in:
Karl Tauber
2020-02-23 13:15:12 +01:00
parent 33e6ce1673
commit cfd07cbcc8
12 changed files with 127 additions and 0 deletions

View File

@@ -3,6 +3,7 @@ FlatLaf Change Log
## Unreleased
- PasswordField: Warn about enabled Caps Lock.
- TextField, FormattedTextField and PasswordField: Support round borders (see UI
default value `TextComponent.arc`). (issue #65)
- IntelliJ Themes: Added Gradianto themes to demo.

View File

@@ -0,0 +1,59 @@
/*
* Copyright 2020 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
*
* https://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.icons;
import java.awt.Component;
import java.awt.Graphics2D;
import java.awt.geom.Path2D;
import java.awt.geom.Rectangle2D;
import java.awt.geom.RoundRectangle2D;
import javax.swing.UIManager;
import com.formdev.flatlaf.ui.FlatUIUtils;
/**
* "caps lock" icon for {@link javax.swing.JPasswordField}.
*
* @uiDefault PasswordField.capsLockIconColor Color
*
* @author Karl Tauber
*/
public class FlatCapsLockIcon
extends FlatAbstractIcon
{
public FlatCapsLockIcon() {
super( 16, 16, UIManager.getColor( "PasswordField.capsLockIconColor" ) );
}
@Override
protected void paintIcon( Component c, Graphics2D g ) {
/*
<svg xmlns="http://www.w3.org/2000/svg" width="16" height="16" viewBox="0 0 16 16">
<g fill="none" fill-rule="evenodd">
<rect width="16" height="16" fill="#6E6E6E" rx="3"/>
<rect width="6" height="2" x="5" y="12" fill="#FFF"/>
<path fill="#FFF" d="M2,8 L8,2 L14,8 L11,8 L11,10 L5,10 L5,8 L2,8 Z"/>
</g>
</svg>
*/
Path2D path = new Path2D.Float( Path2D.WIND_EVEN_ODD );
path.append( new RoundRectangle2D.Float( 0, 0, 16, 16, 6, 6 ), false );
path.append( new Rectangle2D.Float( 5, 12, 6, 2 ), false );
path.append( FlatUIUtils.createPath( 2,8, 8,2, 14,8, 11,8, 11,10, 5,10, 5,8 ), false );
g.fill( path );
}
}

View File

@@ -20,14 +20,20 @@ import static com.formdev.flatlaf.util.UIScale.scale;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Toolkit;
import java.awt.event.FocusListener;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.beans.PropertyChangeEvent;
import javax.swing.Icon;
import javax.swing.JComponent;
import javax.swing.LookAndFeel;
import javax.swing.UIManager;
import javax.swing.plaf.ComponentUI;
import javax.swing.plaf.basic.BasicPasswordFieldUI;
import javax.swing.text.Caret;
import javax.swing.text.JTextComponent;
import com.formdev.flatlaf.FlatClientProperties;
/**
@@ -56,6 +62,7 @@ import com.formdev.flatlaf.FlatClientProperties;
* @uiDefault Component.minimumWidth int
* @uiDefault Component.isIntelliJTheme boolean
* @uiDefault PasswordField.placeholderForeground Color
* @uiDefault PasswordField.capsLockIcon Icon
* @uiDefault TextComponent.selectAllOnFocusPolicy String never, once (default) or always
*
* @author Karl Tauber
@@ -68,8 +75,10 @@ public class FlatPasswordFieldUI
protected int minimumWidth;
protected boolean isIntelliJTheme;
protected Color placeholderForeground;
protected Icon capsLockIcon;
private FocusListener focusListener;
private KeyListener capsLockListener;
public static ComponentUI createUI( JComponent c ) {
return new FlatPasswordFieldUI();
@@ -85,6 +94,7 @@ public class FlatPasswordFieldUI
minimumWidth = UIManager.getInt( "Component.minimumWidth" );
isIntelliJTheme = UIManager.getBoolean( "Component.isIntelliJTheme" );
placeholderForeground = UIManager.getColor( prefix + ".placeholderForeground" );
capsLockIcon = UIManager.getIcon( "PasswordField.capsLockIcon" );
LookAndFeel.installProperty( getComponent(), "opaque", focusWidth == 0 );
@@ -96,6 +106,7 @@ public class FlatPasswordFieldUI
super.uninstallDefaults();
placeholderForeground = null;
capsLockIcon = null;
MigLayoutVisualPadding.uninstall( getComponent() );
}
@@ -105,7 +116,23 @@ public class FlatPasswordFieldUI
super.installListeners();
focusListener = new FlatUIUtils.RepaintFocusListener( getComponent() );
capsLockListener = new KeyAdapter() {
@Override
public void keyPressed( KeyEvent e ) {
repaint( e );
}
@Override
public void keyReleased( KeyEvent e ) {
repaint( e );
}
private void repaint( KeyEvent e ) {
if( e.getKeyCode() == KeyEvent.VK_CAPS_LOCK )
e.getComponent().repaint();
}
};
getComponent().addFocusListener( focusListener );
getComponent().addKeyListener( capsLockListener );
}
@Override
@@ -113,7 +140,9 @@ public class FlatPasswordFieldUI
super.uninstallListeners();
getComponent().removeFocusListener( focusListener );
getComponent().removeKeyListener( capsLockListener );
focusListener = null;
capsLockListener = null;
}
@Override
@@ -133,9 +162,21 @@ public class FlatPasswordFieldUI
protected void paintSafely( Graphics g ) {
FlatTextFieldUI.paintBackground( g, getComponent(), focusWidth, arc, isIntelliJTheme );
FlatTextFieldUI.paintPlaceholder( g, getComponent(), placeholderForeground );
paintCapsLock( g );
super.paintSafely( g );
}
protected void paintCapsLock( Graphics g ) {
JTextComponent c = getComponent();
if( !c.isFocusOwner() ||
!Toolkit.getDefaultToolkit().getLockingKeyState( KeyEvent.VK_CAPS_LOCK ) )
return;
int y = (c.getHeight() - capsLockIcon.getIconHeight()) / 2;
int x = c.getWidth() - capsLockIcon.getIconWidth() - y;
capsLockIcon.paintIcon( c, g, x, y );
}
@Override
protected void paintBackground( Graphics g ) {
// background is painted elsewhere

View File

@@ -182,6 +182,11 @@ MenuItemCheckBox.icon.checkmarkColor=#A7A7A7
MenuItemCheckBox.icon.disabledCheckmarkColor=#606060
#---- PasswordField ----
PasswordField.capsLockIconColor=#ffffff64
#---- PopupMenu ----
PopupMenu.borderColor=#5e5e5e

View File

@@ -345,6 +345,7 @@ PasswordField.margin=@textComponentMargin
PasswordField.background=@textComponentBackground
PasswordField.placeholderForeground=@disabledText
PasswordField.echoChar=\u2022
PasswordField.capsLockIcon=com.formdev.flatlaf.icons.FlatCapsLockIcon
#---- PopupMenu ----

View File

@@ -189,6 +189,11 @@ MenuItemCheckBox.icon.checkmarkColor=#4F9EE3
MenuItemCheckBox.icon.disabledCheckmarkColor=#ABABAB
#---- PasswordField ----
PasswordField.capsLockIconColor=#00000064
#---- PopupMenu ----
PopupMenu.borderColor=#adadad

View File

@@ -0,0 +1,7 @@
<svg xmlns="http://www.w3.org/2000/svg" width="16" height="16" viewBox="0 0 16 16">
<g fill="none" fill-rule="evenodd">
<rect width="16" height="16" fill="#6E6E6E" rx="3"/>
<rect width="6" height="2" x="5" y="12" fill="#FFF"/>
<path fill="#FFF" d="M2,8 L8,2 L14,8 L11,8 L11,10 L5,10 L5,8 L2,8 Z"/>
</g>
</svg>

After

Width:  |  Height:  |  Size: 326 B

View File

@@ -599,6 +599,8 @@ PanelUI com.formdev.flatlaf.ui.FlatPanelUI
PasswordField.background #45494a javax.swing.plaf.ColorUIResource [UI]
PasswordField.border [lazy] 1,1,1,1 false com.formdev.flatlaf.ui.FlatTextBorder [UI]
PasswordField.capsLockIcon [lazy] 16,16 com.formdev.flatlaf.icons.FlatCapsLockIcon [UI]
PasswordField.capsLockIconColor #64ffffff javax.swing.plaf.ColorUIResource [UI]
PasswordField.caretBlinkRate 500
PasswordField.caretForeground #bbbbbb javax.swing.plaf.ColorUIResource [UI]
PasswordField.disabledBackground #3c3f41 javax.swing.plaf.ColorUIResource [UI]

View File

@@ -598,6 +598,8 @@ PanelUI com.formdev.flatlaf.ui.FlatPanelUI
PasswordField.background #45494a javax.swing.plaf.ColorUIResource [UI]
PasswordField.border [lazy] 1,1,1,1 false com.formdev.flatlaf.ui.FlatTextBorder [UI]
PasswordField.capsLockIcon [lazy] 16,16 com.formdev.flatlaf.icons.FlatCapsLockIcon [UI]
PasswordField.capsLockIconColor #64ffffff javax.swing.plaf.ColorUIResource [UI]
PasswordField.caretBlinkRate 500
PasswordField.caretForeground #bbbbbb javax.swing.plaf.ColorUIResource [UI]
PasswordField.disabledBackground #3c3f41 javax.swing.plaf.ColorUIResource [UI]

View File

@@ -601,6 +601,8 @@ PanelUI com.formdev.flatlaf.ui.FlatPanelUI
PasswordField.background #ffffff javax.swing.plaf.ColorUIResource [UI]
PasswordField.border [lazy] 1,1,1,1 false com.formdev.flatlaf.ui.FlatTextBorder [UI]
PasswordField.capsLockIcon [lazy] 16,16 com.formdev.flatlaf.icons.FlatCapsLockIcon [UI]
PasswordField.capsLockIconColor #64000000 javax.swing.plaf.ColorUIResource [UI]
PasswordField.caretBlinkRate 500
PasswordField.caretForeground #000000 javax.swing.plaf.ColorUIResource [UI]
PasswordField.disabledBackground #f2f2f2 javax.swing.plaf.ColorUIResource [UI]

View File

@@ -600,6 +600,8 @@ PanelUI com.formdev.flatlaf.ui.FlatPanelUI
PasswordField.background #ffffff javax.swing.plaf.ColorUIResource [UI]
PasswordField.border [lazy] 1,1,1,1 false com.formdev.flatlaf.ui.FlatTextBorder [UI]
PasswordField.capsLockIcon [lazy] 16,16 com.formdev.flatlaf.icons.FlatCapsLockIcon [UI]
PasswordField.capsLockIconColor #64000000 javax.swing.plaf.ColorUIResource [UI]
PasswordField.caretBlinkRate 500
PasswordField.caretForeground #000000 javax.swing.plaf.ColorUIResource [UI]
PasswordField.disabledBackground #f2f2f2 javax.swing.plaf.ColorUIResource [UI]