more fixes for AWT components on macOS (issue #583)

- ScrollBar: disable hover because scroll bar does not receive mouse exited event
This commit is contained in:
Karl Tauber
2022-09-10 18:56:33 +02:00
parent 16c6ffb032
commit eb9fa585f7

View File

@@ -120,6 +120,7 @@ public class FlatScrollBarUI
protected boolean hoverThumb; protected boolean hoverThumb;
private Map<String, Object> oldStyleValues; private Map<String, Object> oldStyleValues;
private boolean isAWTPeer;
public static ComponentUI createUI( JComponent c ) { public static ComponentUI createUI( JComponent c ) {
return new FlatScrollBarUI(); return new FlatScrollBarUI();
@@ -235,17 +236,30 @@ public class FlatScrollBarUI
break; break;
case "ancestor": case "ancestor":
// check whether scroll bar is used as AWT peer on macOS and // check whether scroll bar is used as AWT peer on macOS
// dark theme is active --> reinstall using light theme if( SystemInfo.isMacOS ) {
if( SystemInfo.isMacOS && FlatLaf.isLafDark() ) {
Container p = scrollbar.getParent(); Container p = scrollbar.getParent();
for( int i = 0; i < 2 && p != null; i++, p = p.getParent() ) { for( int i = 0; i < 2 && p != null; i++, p = p.getParent() ) {
if( FlatUIUtils.isAWTPeer( p ) ) { if( FlatUIUtils.isAWTPeer( p ) ) {
FlatUIUtils.runWithLightAWTPeerUIDefaults( () -> { // Used to disable hover, which does not work correctly
JScrollBar scrollbar = this.scrollbar; // because scroll bars do not receive mouse exited event.
uninstallUI( scrollbar ); // The scroll pane, including its scroll bars, is not part
installUI( scrollbar ); // of the component hierarchy and does not receive mouse events
} ); // directly. Instead LWComponentPeer receives mouse events
// and delegates them to peers, but entered/exited events
// are sent only for the whole scroll pane.
// Exited event is only sent when mouse leaves scroll pane.
// If mouse enters/exits scroll bar, no entered/exited events are sent.
isAWTPeer = true;
// if dark theme is active, reinstall using light theme
if( FlatLaf.isLafDark() ) {
FlatUIUtils.runWithLightAWTPeerUIDefaults( () -> {
JScrollBar scrollbar = this.scrollbar;
uninstallUI( scrollbar );
installUI( scrollbar );
} );
}
break; break;
} }
} }
@@ -384,7 +398,7 @@ public class FlatScrollBarUI
Color trackColor = FlatUIUtils.deriveColor( this.trackColor, c.getBackground() ); Color trackColor = FlatUIUtils.deriveColor( this.trackColor, c.getBackground() );
return (pressed && pressedTrackColor != null) return (pressed && pressedTrackColor != null)
? FlatUIUtils.deriveColor( pressedTrackColor, trackColor ) ? FlatUIUtils.deriveColor( pressedTrackColor, trackColor )
: ((hover && hoverTrackColor != null) : ((hover && hoverTrackColor != null && !isAWTPeer)
? FlatUIUtils.deriveColor( hoverTrackColor, trackColor ) ? FlatUIUtils.deriveColor( hoverTrackColor, trackColor )
: trackColor); : trackColor);
} }
@@ -394,7 +408,7 @@ public class FlatScrollBarUI
Color thumbColor = FlatUIUtils.deriveColor( this.thumbColor, trackColor ); Color thumbColor = FlatUIUtils.deriveColor( this.thumbColor, trackColor );
return (pressed && pressedThumbColor != null) return (pressed && pressedThumbColor != null)
? FlatUIUtils.deriveColor( pressedThumbColor, thumbColor ) ? FlatUIUtils.deriveColor( pressedThumbColor, thumbColor )
: ((hover && hoverThumbColor != null) : ((hover && hoverThumbColor != null && !isAWTPeer)
? FlatUIUtils.deriveColor( hoverThumbColor, thumbColor ) ? FlatUIUtils.deriveColor( hoverThumbColor, thumbColor )
: thumbColor); : thumbColor);
} }