PasswordField: preserve reveal button state when switching theme (issue #173)

This commit is contained in:
Karl Tauber
2021-12-27 20:07:27 +01:00
parent 6e807f44b2
commit 5fae367fab

View File

@@ -38,6 +38,7 @@ import javax.swing.text.Element;
import javax.swing.text.JTextComponent; import javax.swing.text.JTextComponent;
import javax.swing.text.PasswordView; import javax.swing.text.PasswordView;
import javax.swing.text.View; import javax.swing.text.View;
import com.formdev.flatlaf.FlatClientProperties;
import com.formdev.flatlaf.icons.FlatCapsLockIcon; import com.formdev.flatlaf.icons.FlatCapsLockIcon;
import com.formdev.flatlaf.ui.FlatStylingSupport.Styleable; import com.formdev.flatlaf.ui.FlatStylingSupport.Styleable;
import com.formdev.flatlaf.util.UIScale; import com.formdev.flatlaf.util.UIScale;
@@ -83,6 +84,9 @@ import com.formdev.flatlaf.util.UIScale;
public class FlatPasswordFieldUI public class FlatPasswordFieldUI
extends FlatTextFieldUI extends FlatTextFieldUI
{ {
// used to preserve reveal button state when switching theme
private static final String KEY_REVEAL_SELECTED = "FlatLaf.internal.FlatPasswordFieldUI.revealSelected";
private Character echoChar; private Character echoChar;
@Styleable protected boolean showCapsLock; @Styleable protected boolean showCapsLock;
@@ -269,18 +273,16 @@ public class FlatPasswordFieldUI
if( !showCapsLock ) if( !showCapsLock )
return false; return false;
JTextComponent c = getComponent(); return FlatUIUtils.isPermanentFocusOwner( getComponent() ) &&
return FlatUIUtils.isPermanentFocusOwner( c ) &&
Toolkit.getDefaultToolkit().getLockingKeyState( KeyEvent.VK_CAPS_LOCK ); Toolkit.getDefaultToolkit().getLockingKeyState( KeyEvent.VK_CAPS_LOCK );
} }
/** @since 2 */ /** @since 2 */
protected void installRevealButton() { protected void installRevealButton() {
JTextComponent c = getComponent();
if( showRevealButton ) { if( showRevealButton ) {
revealButton = createRevealButton(); revealButton = createRevealButton();
installLayout(); installLayout();
c.add( revealButton ); getComponent().add( revealButton );
} }
} }
@@ -288,14 +290,24 @@ public class FlatPasswordFieldUI
protected JToggleButton createRevealButton() { protected JToggleButton createRevealButton() {
JToggleButton button = new JToggleButton( revealIcon ); JToggleButton button = new JToggleButton( revealIcon );
prepareLeadingOrTrailingComponent( button ); prepareLeadingOrTrailingComponent( button );
if( FlatClientProperties.clientPropertyBoolean( getComponent(), KEY_REVEAL_SELECTED, false ) ) {
button.setSelected( true );
updateEchoChar( true );
}
button.addActionListener( e -> { button.addActionListener( e -> {
LookAndFeel.installProperty( getComponent(), "echoChar", button.isSelected() boolean selected = button.isSelected();
? '\0' updateEchoChar( selected );
: (echoChar != null ? echoChar : '*')); getComponent().putClientProperty( KEY_REVEAL_SELECTED, selected );
} ); } );
return button; return button;
} }
private void updateEchoChar( boolean selected ) {
LookAndFeel.installProperty( getComponent(), "echoChar", selected
? '\0'
: (echoChar != null ? echoChar : '*'));
}
/** @since 2 */ /** @since 2 */
protected void uninstallRevealButton() { protected void uninstallRevealButton() {
if( revealButton != null ) { if( revealButton != null ) {