Skip to content

Commit

Permalink
PasswordField: Caps lock icon no longer painted over long text (issue #…
Browse files Browse the repository at this point in the history
  • Loading branch information
DevCharly committed Jul 9, 2021
1 parent 74a748d commit cc6d3c1
Show file tree
Hide file tree
Showing 5 changed files with 64 additions and 6 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ FlatLaf Change Log
non-opaque. (issue #349)
- OptionPane: Align wrapped lines to the right if component orientation is
right-to-left. (issue #350)
- PasswordField: Caps lock icon no longer painted over long text. (issue #172)
- Window decorations: Window title bar width is no longer considered when
calculating preferred/minimum width of window. (issue #351)

Expand Down
20 changes: 20 additions & 0 deletions flatlaf-core/src/main/java/com/formdev/flatlaf/ui/FlatCaret.java
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
import java.awt.event.MouseEvent;
import javax.swing.JFormattedTextField;
import javax.swing.plaf.UIResource;
import javax.swing.text.BadLocationException;
import javax.swing.text.DefaultCaret;
import javax.swing.text.Document;
import javax.swing.text.JTextComponent;
Expand Down Expand Up @@ -142,4 +143,23 @@ protected void selectAllOnFocusGained() {
moveDot( doc.getLength() );
}
}

/**
* @since 1.4
*/
public void scrollCaretToVisible() {
JTextComponent c = getComponent();
if( c == null || c.getUI() == null )
return;

try {
Rectangle loc = c.getUI().modelToView( c, getDot(), getDotBias() );
if( loc != null ) {
adjustVisibility( loc );
damage( loc );
}
} catch( BadLocationException ex ) {
// ignore
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
package com.formdev.flatlaf.ui;

import java.awt.Graphics;
import java.awt.Insets;
import java.awt.Shape;
import java.awt.Toolkit;
import java.awt.event.KeyAdapter;
Expand Down Expand Up @@ -123,8 +124,10 @@ public void keyReleased( KeyEvent e ) {
repaint( e );
}
private void repaint( KeyEvent e ) {
if( e.getKeyCode() == KeyEvent.VK_CAPS_LOCK )
if( e.getKeyCode() == KeyEvent.VK_CAPS_LOCK ) {
e.getComponent().repaint();
scrollCaretToVisible();
}
}
};

Expand Down Expand Up @@ -169,16 +172,36 @@ protected void paintSafely( Graphics g ) {
}

protected void paintCapsLock( Graphics g ) {
if( !showCapsLock )
if( !isCapsLockVisible() )
return;

JTextComponent c = getComponent();
if( !FlatUIUtils.isPermanentFocusOwner( c ) ||
!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 );
}

/**
* @since 1.4
*/
protected boolean isCapsLockVisible() {
if( !showCapsLock )
return false;

JTextComponent c = getComponent();
return FlatUIUtils.isPermanentFocusOwner( c ) &&
Toolkit.getDefaultToolkit().getLockingKeyState( KeyEvent.VK_CAPS_LOCK );
}

/**
* @since 1.4
*/
@Override
protected Insets getPadding() {
Insets padding = super.getPadding();
if( !isCapsLockVisible() )
return padding;

return FlatUIUtils.addInsets( padding, new Insets( 0, 0, 0, capsLockIcon.getIconWidth() ) );
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -290,4 +290,13 @@ protected Insets getPadding() {
Object padding = getComponent().getClientProperty( FlatClientProperties.TEXT_FIELD_PADDING );
return (padding instanceof Insets) ? UIScale.scale( (Insets) padding ) : null;
}

/**
* @since 1.4
*/
protected void scrollCaretToVisible() {
Caret caret = getComponent().getCaret();
if( caret instanceof FlatCaret )
((FlatCaret)caret).scrollCaretToVisible();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,11 @@ public static Dimension addInsets( Dimension dim, Insets insets ) {
}

public static Insets addInsets( Insets insets1, Insets insets2 ) {
if( insets1 == null )
return insets2;
if( insets2 == null )
return insets1;

return new Insets(
insets1.top + insets2.top,
insets1.left + insets2.left,
Expand Down

0 comments on commit cc6d3c1

Please sign in to comment.