Merge remote-tracking branch 'origin/drop-shadows' into master

This commit is contained in:
Karl Tauber
2020-05-08 19:02:13 +02:00
8 changed files with 361 additions and 34 deletions

View File

@@ -56,6 +56,7 @@ import javax.swing.plaf.UIResource;
import javax.swing.plaf.basic.BasicLookAndFeel; import javax.swing.plaf.basic.BasicLookAndFeel;
import javax.swing.text.StyleContext; import javax.swing.text.StyleContext;
import javax.swing.text.html.HTMLEditorKit; import javax.swing.text.html.HTMLEditorKit;
import com.formdev.flatlaf.ui.FlatPopupFactory;
import com.formdev.flatlaf.util.GrayFilter; import com.formdev.flatlaf.util.GrayFilter;
import com.formdev.flatlaf.util.MultiResolutionImageSupport; import com.formdev.flatlaf.util.MultiResolutionImageSupport;
import com.formdev.flatlaf.util.SystemInfo; import com.formdev.flatlaf.util.SystemInfo;
@@ -78,6 +79,7 @@ public abstract class FlatLaf
private static boolean aquaLoaded; private static boolean aquaLoaded;
private static boolean updateUIPending; private static boolean updateUIPending;
private PopupFactory oldPopupFactory;
private MnemonicHandler mnemonicHandler; private MnemonicHandler mnemonicHandler;
private Consumer<UIDefaults> postInitialization; private Consumer<UIDefaults> postInitialization;
@@ -144,6 +146,10 @@ public abstract class FlatLaf
super.initialize(); super.initialize();
// install popup factory
oldPopupFactory = PopupFactory.getSharedInstance();
PopupFactory.setSharedInstance( new FlatPopupFactory() );
// install mnemonic handler // install mnemonic handler
mnemonicHandler = new MnemonicHandler(); mnemonicHandler = new MnemonicHandler();
mnemonicHandler.install(); mnemonicHandler.install();
@@ -200,6 +206,12 @@ public abstract class FlatLaf
desktopPropertyListener = null; desktopPropertyListener = null;
} }
// uninstall popup factory
if( oldPopupFactory != null ) {
PopupFactory.setSharedInstance( oldPopupFactory );
oldPopupFactory = null;
}
// uninstall mnemonic handler // uninstall mnemonic handler
if( mnemonicHandler != null ) { if( mnemonicHandler != null ) {
mnemonicHandler.uninstall(); mnemonicHandler.uninstall();

View File

@@ -0,0 +1,159 @@
/*
* 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.ui;
import java.awt.Color;
import java.awt.Component;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Image;
import java.awt.Insets;
import java.awt.RadialGradientPaint;
import java.awt.image.BufferedImage;
import com.formdev.flatlaf.util.HiDPIUtils;
import com.formdev.flatlaf.util.UIScale;
/**
* Paints a drop shadow border around the component.
*
* @author Karl Tauber
*/
public class FlatDropShadowBorder
extends FlatEmptyBorder
{
private final Color shadowColor;
private final int shadowSize;
private final int cornerInset;
private final int shadowAlpha;
private Image shadowImage;
private Color lastShadowColor;
private double lastSystemScaleFactor;
private float lastUserScaleFactor;
public FlatDropShadowBorder() {
this( null );
}
public FlatDropShadowBorder( Color shadowColor ) {
this( shadowColor, 4, 4, 128 );
}
public FlatDropShadowBorder( Color shadowColor, int shadowSize, int cornerInset, int shadowAlpha ) {
super( new Insets( 0, 0, shadowSize, shadowSize ) );
this.shadowColor = shadowColor;
this.shadowSize = shadowSize;
this.cornerInset = cornerInset;
this.shadowAlpha = shadowAlpha;
}
@Override
public void paintBorder( Component c, Graphics g, int x, int y, int width, int height ) {
HiDPIUtils.paintAtScale1x( (Graphics2D) g, x, y, width, height, this::paintImpl );
}
private void paintImpl( Graphics2D g, int x, int y, int width, int height, double scaleFactor ) {
Color shadowColor = (this.shadowColor != null) ? this.shadowColor : g.getColor();
int shadowSize = (int) Math.ceil( UIScale.scale( this.shadowSize ) * scaleFactor );
int cornerInset = (int) Math.ceil( UIScale.scale( this.cornerInset ) * scaleFactor );
// create and cache shadow image
float userScaleFactor = UIScale.getUserScaleFactor();
if( shadowImage == null ||
!shadowColor.equals( lastShadowColor ) ||
lastSystemScaleFactor != scaleFactor ||
lastUserScaleFactor != userScaleFactor )
{
shadowImage = createShadowImage( shadowColor, shadowSize, shadowAlpha,
(float) (scaleFactor * userScaleFactor) );
lastShadowColor = shadowColor;
lastSystemScaleFactor = scaleFactor;
lastUserScaleFactor = userScaleFactor;
}
/*debug
int m = shadowImage.getWidth( null );
g.setColor( Color.lightGray );
g.drawRect( x - m - 1, y - m - 1, m + 1, m + 1 );
g.setColor( Color.white );
g.fillRect( x - m, y - m, m, m );
g.drawImage( shadowImage, x - m, y - m, null );
debug*/
int x1c = x + cornerInset;
int y1c = y + cornerInset;
int x1cs = x1c + shadowSize;
int y1cs = y1c + shadowSize;
int x2s = x + width;
int y2s = y + height;
int x2 = x2s - shadowSize;
int y2 = y2s - shadowSize;
int wh = (shadowSize * 2) - 1;
int center = shadowSize - 1;
// left-bottom edge
g.drawImage( shadowImage, x1c, y2, x1cs, y2s,
0, center, shadowSize, wh, null );
// bottom shadow
g.drawImage( shadowImage, x1cs, y2, x2, y2s,
center, center, center + 1, wh, null );
// right-bottom edge
g.drawImage( shadowImage, x2, y2, x2s, y2s,
center, center, wh, wh, null );
// right shadow
g.drawImage( shadowImage, x2, y1cs, x2s, y2,
center, center, wh, center + 1, null );
// right-top edge
g.drawImage( shadowImage, x2, y1c, x2s, y1cs,
center, 0, wh, shadowSize, null );
}
private static BufferedImage createShadowImage( Color shadowColor, int shadowSize,
int shadowAlpha, float scaleFactor )
{
int shadowRGB = shadowColor.getRGB() & 0xffffff;
Color startColor = new Color( shadowRGB | ((shadowAlpha & 0xff) << 24), true );
Color midColor = new Color( shadowRGB | (((shadowAlpha / 2) & 0xff) << 24), true );
Color endColor = new Color( shadowRGB, true );
int wh = (shadowSize * 2) - 1;
int center = shadowSize - 1;
RadialGradientPaint p = new RadialGradientPaint( center, center,
shadowSize - (0.75f * scaleFactor),
new float[] { 0, 0.35f, 1 },
new Color[] { startColor, midColor, endColor } );
BufferedImage image = new BufferedImage( wh, wh, BufferedImage.TYPE_INT_ARGB );
Graphics2D g = image.createGraphics();
try {
g.setPaint( p );
g.fillRect( 0, 0, wh, wh );
} finally {
g.dispose();
}
return image;
}
}

View File

@@ -113,6 +113,8 @@ public class FlatInternalFrameUI
private final Color inactiveBorderColor = UIManager.getColor( "InternalFrame.inactiveBorderColor" ); private final Color inactiveBorderColor = UIManager.getColor( "InternalFrame.inactiveBorderColor" );
private final int borderLineWidth = FlatUIUtils.getUIInt( "InternalFrame.borderLineWidth", 1 ); private final int borderLineWidth = FlatUIUtils.getUIInt( "InternalFrame.borderLineWidth", 1 );
private final FlatDropShadowBorder dropShadowBorder = new FlatDropShadowBorder();
public FlatInternalFrameBorder() { public FlatInternalFrameBorder() {
super( UIManager.getInsets( "InternalFrame.borderMargins" ) ); super( UIManager.getInsets( "InternalFrame.borderMargins" ) );
} }
@@ -137,16 +139,24 @@ public class FlatInternalFrameUI
Insets insets = getBorderInsets( c ); Insets insets = getBorderInsets( c );
float lineWidth = scale( (float) borderLineWidth ); float lineWidth = scale( (float) borderLineWidth );
float rx = x + insets.left - lineWidth;
float ry = y + insets.top - lineWidth;
float rwidth = width - insets.left - insets.right + (lineWidth * 2);
float rheight = height - insets.top - insets.bottom + (lineWidth * 2);
Graphics2D g2 = (Graphics2D) g.create(); Graphics2D g2 = (Graphics2D) g.create();
try { try {
FlatUIUtils.setRenderingHints( g2 ); FlatUIUtils.setRenderingHints( g2 );
g2.setColor( f.isSelected() ? activeBorderColor : inactiveBorderColor ); g2.setColor( f.isSelected() ? activeBorderColor : inactiveBorderColor );
g2.fill( FlatUIUtils.createRectangle(
x + insets.left - lineWidth, // paint drop shadow
y + insets.top - lineWidth, Insets dropShadowInsets = dropShadowBorder.getBorderInsets();
width - insets.left - insets.right + (lineWidth * 2), dropShadowBorder.paintBorder( c, g2, (int) rx, (int) ry,
height - insets.top - insets.bottom + (lineWidth * 2), (int) rwidth + dropShadowInsets.right,
lineWidth ) ); (int) rheight + dropShadowInsets.bottom );
// paint border
g2.fill( FlatUIUtils.createRectangle( rx, ry, rwidth, rheight, lineWidth ) );
} finally { } finally {
g2.dispose(); g2.dispose();
} }

View File

@@ -0,0 +1,168 @@
/*
* 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.ui;
import java.awt.Color;
import java.awt.Component;
import java.awt.Container;
import java.awt.Dimension;
import java.awt.Window;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import javax.swing.JComponent;
import javax.swing.Popup;
import javax.swing.PopupFactory;
import javax.swing.SwingUtilities;
import javax.swing.border.Border;
import com.formdev.flatlaf.util.SystemInfo;
/**
* A popup factory that adds drop shadows to popups on Windows and Linux.
* On macOS, heavy weight popups (without drop shadow) are produced and the
* operating system automatically adds drop shadows.
*
* @author Karl Tauber
*/
public class FlatPopupFactory
extends PopupFactory
{
private Method java8getPopupMethod;
private Method java9getPopupMethod;
@Override
public Popup getPopup( Component owner, Component contents, int x, int y )
throws IllegalArgumentException
{
// always use heavy weight popup because the drop shadow increases
// the popup size and may overlap the window bounds
Popup popup = getHeavyWeightPopup( owner, contents, x, y );
// failed to get heavy weight popup --> do not add drop shadow
if( popup == null )
return super.getPopup( owner, contents, x, y );
// macOS adds drop shadow to heavy weight popups
if( SystemInfo.IS_MAC )
return popup;
// create drop shadow popup
return new DropShadowPopup( popup, contents );
}
/**
* There is no API in Java 8 to force creation of heavy weight popups,
* but it is possible with reflection. Java 9 provides a new method.
*
* When changing FlatLaf system requirements to Java 9+,
* then this method can be replaced with:
* return getPopup( owner, contents, x, y, true );
*/
private Popup getHeavyWeightPopup( Component owner, Component contents, int x, int y )
throws IllegalArgumentException
{
try {
if( SystemInfo.IS_JAVA_9_OR_LATER ) {
if( java9getPopupMethod == null ) {
java9getPopupMethod = PopupFactory.class.getDeclaredMethod(
"getPopup", Component.class, Component.class, int.class, int.class, boolean.class );
}
return (Popup) java9getPopupMethod.invoke( this, owner, contents, x, y, true );
} else {
// Java 8
if( java8getPopupMethod == null ) {
java8getPopupMethod = PopupFactory.class.getDeclaredMethod(
"getPopup", Component.class, Component.class, int.class, int.class, int.class );
java8getPopupMethod.setAccessible( true );
}
return (Popup) java8getPopupMethod.invoke( this, owner, contents, x, y, /*HEAVY_WEIGHT_POPUP*/ 2 );
}
} catch( NoSuchMethodException | SecurityException | IllegalAccessException | InvocationTargetException ex ) {
// ignore
return null;
}
}
//---- class DropShadowPopup ----------------------------------------------
private static class DropShadowPopup
extends Popup
{
private Popup delegate;
private JComponent parent;
private Border oldBorder;
private boolean oldOpaque;
private Window window;
private Color oldBackground;
DropShadowPopup( Popup delegate, Component contents ) {
this.delegate = delegate;
if( delegate.getClass().getName().endsWith( "MediumWeightPopup" ) )
return;
Dimension size = contents.getPreferredSize();
if( size.width <= 0 || size.height <= 0 )
return;
Container p = contents.getParent();
if( !(p instanceof JComponent) )
return;
parent = (JComponent) p;
oldBorder = parent.getBorder();
oldOpaque = parent.isOpaque();
parent.setBorder( new FlatDropShadowBorder( null, 4, 4, 32 ) ); //TODO
parent.setOpaque( false );
window = SwingUtilities.windowForComponent( contents );
if( window != null ) {
oldBackground = window.getBackground();
window.setBackground( new Color( 0, true ) );
window.setSize( window.getPreferredSize() );
} else
parent.setSize( parent.getPreferredSize() );
}
@Override
public void show() {
delegate.show();
}
@Override
public void hide() {
if( delegate == null )
return;
delegate.hide();
if( parent != null ) {
parent.setBorder( oldBorder );
parent.setOpaque( oldOpaque );
parent = null;
}
if( window != null ) {
window.setBackground( oldBackground );
window = null;
}
delegate = null;
}
}
}

View File

@@ -19,7 +19,6 @@ package com.formdev.flatlaf.ui;
import javax.swing.JComponent; import javax.swing.JComponent;
import javax.swing.plaf.ComponentUI; import javax.swing.plaf.ComponentUI;
import javax.swing.plaf.basic.BasicPopupMenuUI; import javax.swing.plaf.basic.BasicPopupMenuUI;
import com.formdev.flatlaf.util.SystemInfo;
/** /**
* Provides the Flat LaF UI delegate for {@link javax.swing.JPopupMenu}. * Provides the Flat LaF UI delegate for {@link javax.swing.JPopupMenu}.
@@ -36,28 +35,7 @@ import com.formdev.flatlaf.util.SystemInfo;
public class FlatPopupMenuUI public class FlatPopupMenuUI
extends BasicPopupMenuUI extends BasicPopupMenuUI
{ {
private boolean oldLightWeightPopupEnabled;
public static ComponentUI createUI( JComponent c ) { public static ComponentUI createUI( JComponent c ) {
return new FlatPopupMenuUI(); return new FlatPopupMenuUI();
} }
@Override
public void installDefaults() {
super.installDefaults();
// use heavy-weight popups on macOS to get nice drop shadow from OS
if( SystemInfo.IS_MAC ) {
oldLightWeightPopupEnabled = popupMenu.isLightWeightPopupEnabled();
popupMenu.setLightWeightPopupEnabled( false );
}
}
@Override
protected void uninstallDefaults() {
super.uninstallDefaults();
if( SystemInfo.IS_MAC )
popupMenu.setLightWeightPopupEnabled( oldLightWeightPopupEnabled );
}
} }

View File

@@ -148,8 +148,8 @@ InternalFrame.activeTitleForeground=@foreground
InternalFrame.inactiveTitleBackground=darken(@background,5%) InternalFrame.inactiveTitleBackground=darken(@background,5%)
InternalFrame.inactiveTitleForeground=@disabledText InternalFrame.inactiveTitleForeground=@disabledText
InternalFrame.activeBorderColor=lighten($Component.borderColor,10%) InternalFrame.activeBorderColor=darken(@background,7%)
InternalFrame.inactiveBorderColor=$Component.borderColor InternalFrame.inactiveBorderColor=darken(@background,3%)
InternalFrame.buttonHoverBackground=lighten($InternalFrame.activeTitleBackground,10%,derived autoInverse) InternalFrame.buttonHoverBackground=lighten($InternalFrame.activeTitleBackground,10%,derived autoInverse)
InternalFrame.buttonPressedBackground=lighten($InternalFrame.activeTitleBackground,20%,derived autoInverse) InternalFrame.buttonPressedBackground=lighten($InternalFrame.activeTitleBackground,20%,derived autoInverse)

View File

@@ -327,7 +327,7 @@ HyperlinkUI com.formdev.flatlaf.swingx.ui.FlatHyperlinkUI
#---- InternalFrame ---- #---- InternalFrame ----
InternalFrame.activeBorderColor #7e7e7e javax.swing.plaf.ColorUIResource [UI] InternalFrame.activeBorderColor #2b2d2e javax.swing.plaf.ColorUIResource [UI]
InternalFrame.activeTitleBackground #242526 javax.swing.plaf.ColorUIResource [UI] InternalFrame.activeTitleBackground #242526 javax.swing.plaf.ColorUIResource [UI]
InternalFrame.activeTitleForeground #bbbbbb javax.swing.plaf.ColorUIResource [UI] InternalFrame.activeTitleForeground #bbbbbb javax.swing.plaf.ColorUIResource [UI]
InternalFrame.border [lazy] 6,6,6,6 false com.formdev.flatlaf.ui.FlatInternalFrameUI$FlatInternalFrameBorder [UI] InternalFrame.border [lazy] 6,6,6,6 false com.formdev.flatlaf.ui.FlatInternalFrameUI$FlatInternalFrameBorder [UI]
@@ -348,7 +348,7 @@ InternalFrame.closePressedBackground [lazy] #ad3b37 javax.swing.plaf.ColorUIR
InternalFrame.closePressedForeground #ffffff javax.swing.plaf.ColorUIResource [UI] InternalFrame.closePressedForeground #ffffff javax.swing.plaf.ColorUIResource [UI]
InternalFrame.icon [lazy] 16,16 sun.swing.ImageIconUIResource [UI] (sun.awt.image.ToolkitImage) InternalFrame.icon [lazy] 16,16 sun.swing.ImageIconUIResource [UI] (sun.awt.image.ToolkitImage)
InternalFrame.iconifyIcon [lazy] 24,24 com.formdev.flatlaf.icons.FlatInternalFrameIconifyIcon [UI] InternalFrame.iconifyIcon [lazy] 24,24 com.formdev.flatlaf.icons.FlatInternalFrameIconifyIcon [UI]
InternalFrame.inactiveBorderColor #646464 javax.swing.plaf.ColorUIResource [UI] InternalFrame.inactiveBorderColor #353739 javax.swing.plaf.ColorUIResource [UI]
InternalFrame.inactiveTitleBackground #303234 javax.swing.plaf.ColorUIResource [UI] InternalFrame.inactiveTitleBackground #303234 javax.swing.plaf.ColorUIResource [UI]
InternalFrame.inactiveTitleForeground #777777 javax.swing.plaf.ColorUIResource [UI] InternalFrame.inactiveTitleForeground #777777 javax.swing.plaf.ColorUIResource [UI]
InternalFrame.maximizeIcon [lazy] 24,24 com.formdev.flatlaf.icons.FlatInternalFrameMaximizeIcon [UI] InternalFrame.maximizeIcon [lazy] 24,24 com.formdev.flatlaf.icons.FlatInternalFrameMaximizeIcon [UI]

View File

@@ -326,7 +326,7 @@ HyperlinkUI com.formdev.flatlaf.swingx.ui.FlatHyperlinkUI
#---- InternalFrame ---- #---- InternalFrame ----
InternalFrame.activeBorderColor #7e7e7e javax.swing.plaf.ColorUIResource [UI] InternalFrame.activeBorderColor #2b2d2e javax.swing.plaf.ColorUIResource [UI]
InternalFrame.activeTitleBackground #242526 javax.swing.plaf.ColorUIResource [UI] InternalFrame.activeTitleBackground #242526 javax.swing.plaf.ColorUIResource [UI]
InternalFrame.activeTitleForeground #bbbbbb javax.swing.plaf.ColorUIResource [UI] InternalFrame.activeTitleForeground #bbbbbb javax.swing.plaf.ColorUIResource [UI]
InternalFrame.border [lazy] 6,6,6,6 false com.formdev.flatlaf.ui.FlatInternalFrameUI$FlatInternalFrameBorder [UI] InternalFrame.border [lazy] 6,6,6,6 false com.formdev.flatlaf.ui.FlatInternalFrameUI$FlatInternalFrameBorder [UI]
@@ -347,7 +347,7 @@ InternalFrame.closePressedBackground [lazy] #ad3b37 javax.swing.plaf.ColorUIR
InternalFrame.closePressedForeground #ffffff javax.swing.plaf.ColorUIResource [UI] InternalFrame.closePressedForeground #ffffff javax.swing.plaf.ColorUIResource [UI]
InternalFrame.icon [lazy] 16,16 sun.swing.ImageIconUIResource [UI] (sun.awt.image.ToolkitImage) InternalFrame.icon [lazy] 16,16 sun.swing.ImageIconUIResource [UI] (sun.awt.image.ToolkitImage)
InternalFrame.iconifyIcon [lazy] 24,24 com.formdev.flatlaf.icons.FlatInternalFrameIconifyIcon [UI] InternalFrame.iconifyIcon [lazy] 24,24 com.formdev.flatlaf.icons.FlatInternalFrameIconifyIcon [UI]
InternalFrame.inactiveBorderColor #646464 javax.swing.plaf.ColorUIResource [UI] InternalFrame.inactiveBorderColor #353739 javax.swing.plaf.ColorUIResource [UI]
InternalFrame.inactiveTitleBackground #303234 javax.swing.plaf.ColorUIResource [UI] InternalFrame.inactiveTitleBackground #303234 javax.swing.plaf.ColorUIResource [UI]
InternalFrame.inactiveTitleForeground #777777 javax.swing.plaf.ColorUIResource [UI] InternalFrame.inactiveTitleForeground #777777 javax.swing.plaf.ColorUIResource [UI]
InternalFrame.maximizeIcon [lazy] 24,24 com.formdev.flatlaf.icons.FlatInternalFrameMaximizeIcon [UI] InternalFrame.maximizeIcon [lazy] 24,24 com.formdev.flatlaf.icons.FlatInternalFrameMaximizeIcon [UI]