UI defaults: get rid of unused Aqua UI defaults on macOS

UI defaults on macOS are now (nearly) equal to other platforms.
There are only minor platform specific differences.
InputMaps are still used from Aqua LaF.
This commit is contained in:
Karl Tauber
2020-02-23 10:57:37 +01:00
parent 00ccda83f9
commit 33e6ce1673
6 changed files with 75 additions and 538 deletions

View File

@@ -31,11 +31,11 @@ import com.formdev.flatlaf.util.SystemInfo;
*/
class FlatInputMaps
{
static void initInputMaps( UIDefaults defaults ) {
static void initInputMaps( UIDefaults defaults, UIDefaults baseDefaults ) {
if( !SystemInfo.IS_MAC )
initBasicInputMaps( defaults );
else
initMacInputMaps( defaults );
initMacInputMaps( defaults, baseDefaults );
}
private static void initBasicInputMaps( UIDefaults defaults ) {
@@ -233,7 +233,26 @@ class FlatInputMaps
defaults.put( "EditorPane.focusInputMap", multiLineInputMap );
}
private static void initMacInputMaps( UIDefaults defaults ) {
private static void initMacInputMaps( UIDefaults defaults, UIDefaults baseDefaults ) {
// copy Aqua LaF input maps
copyInputMaps( baseDefaults, defaults,
"Button.focusInputMap",
"EditorPane.focusInputMap",
"FormattedTextField.focusInputMap",
"List.focusInputMap",
"PasswordField.focusInputMap",
"ScrollBar.focusInputMap.RightToLeft",
"ScrollBar.focusInputMap",
"ScrollPane.ancestorInputMap.RightToLeft",
"ScrollPane.ancestorInputMap",
"Table.ancestorInputMap.RightToLeft",
"Table.ancestorInputMap",
"TextArea.focusInputMap",
"TextField.focusInputMap",
"TextPane.focusInputMap",
"Tree.focusInputMap" );
// AquaLookAndFeel (the base for UI defaults on macOS) uses special
// action keys (e.g. "aquaExpandNode") for some macOS specific behaviour.
// Those action keys are not available in FlatLaf, which makes it
@@ -277,6 +296,12 @@ class FlatInputMaps
} ) );
}
private static void copyInputMaps( UIDefaults src, UIDefaults dest, String... keys ) {
// Note: not using `src.get(key)` here because this would resolve the lazy value
for( String key : keys )
dest.put( key, src.remove( key ) );
}
private static void modifyInputMap( UIDefaults defaults, String key, Object... bindings ) {
// Note: not using `defaults.get(key)` here because this would resolve the lazy value
defaults.put( key, new LazyModifyInputMap( defaults.remove( key ), bindings ) );

View File

@@ -64,7 +64,7 @@ public abstract class FlatLaf
static final Logger LOG = Logger.getLogger( FlatLaf.class.getName() );
private static final String DESKTOPFONTHINTS = "awt.font.desktophints";
private BasicLookAndFeel base;
private BasicLookAndFeel aquaLaf;
private String desktopPropertyName;
private PropertyChangeListener desktopPropertyListener;
@@ -114,10 +114,11 @@ public abstract class FlatLaf
@Override
public void initialize() {
initBase();
createAquaLaf();
if( base != null )
base.initialize();
// this is required for using Mac screen menubar
if( aquaLaf != null )
aquaLaf.initialize();
super.initialize();
@@ -195,40 +196,39 @@ public abstract class FlatLaf
new HTMLEditorKit().getStyleSheet().addRule( "a { color: blue; }" );
postInitialization = null;
if( base != null )
base.uninitialize();
if( aquaLaf != null )
aquaLaf.uninitialize();
super.uninitialize();
}
/**
* Create base LaF. This is used to grab base UI defaults from different LaFs.
* On Mac from Aqua LaF, otherwise from Basic LaF.
* Create Aqua LaF on macOS.
* Initializing Aqua LaF is required for using Mac screen menubar.
*/
private void initBase() {
if( SystemInfo.IS_MAC && base == null ) {
// use Mac Aqua LaF as base
String aquaLafClassName = "com.apple.laf.AquaLookAndFeel";
try {
if( SystemInfo.IS_JAVA_9_OR_LATER ) {
Method m = UIManager.class.getMethod( "createLookAndFeel", String.class );
base = (BasicLookAndFeel) m.invoke( null, "Mac OS X" );
} else
base = (BasicLookAndFeel) Class.forName( aquaLafClassName ).newInstance();
} catch( Exception ex ) {
LOG.log( Level.SEVERE, "FlatLaf: Failed to initialize base look and feel '" + aquaLafClassName + "'.", ex );
throw new IllegalStateException();
}
private void createAquaLaf() {
if( !SystemInfo.IS_MAC || aquaLaf != null )
return;
// create macOS Aqua LaF
String aquaLafClassName = "com.apple.laf.AquaLookAndFeel";
try {
if( SystemInfo.IS_JAVA_9_OR_LATER ) {
Method m = UIManager.class.getMethod( "createLookAndFeel", String.class );
aquaLaf = (BasicLookAndFeel) m.invoke( null, "Mac OS X" );
} else
aquaLaf = (BasicLookAndFeel) Class.forName( aquaLafClassName ).newInstance();
} catch( Exception ex ) {
LOG.log( Level.SEVERE, "FlatLaf: Failed to initialize base look and feel '" + aquaLafClassName + "'.", ex );
throw new IllegalStateException();
}
}
@Override
public UIDefaults getDefaults() {
initBase();
createAquaLaf();
UIDefaults defaults = (base != null) ? base.getDefaults() : super.getDefaults();
if( base != null )
UIDefaultsRemover.removeDefaults( defaults );
UIDefaults defaults = super.getDefaults();
// add Metal resource bundle, which is required for FlatFileChooserUI
defaults.addResourceBundle( "com.sun.swing.internal.plaf.metal.resources.metal" );
@@ -259,13 +259,9 @@ public abstract class FlatLaf
putDefaults( defaults, defaults.getColor( "textText" ),
"DesktopIcon.foreground" );
// remember MenuBarUI from Mac Aqua LaF if Mac screen menubar is enabled
boolean useScreenMenuBar = SystemInfo.IS_MAC && "true".equals( System.getProperty( "apple.laf.useScreenMenuBar" ) );
Object aquaMenuBarUI = useScreenMenuBar ? defaults.get( "MenuBarUI" ) : null;
initFonts( defaults );
initIconColors( defaults, isDark() );
FlatInputMaps.initInputMaps( defaults );
FlatInputMaps.initInputMaps( defaults, (aquaLaf != null) ? aquaLaf.getDefaults() : null );
// load defaults from properties
List<Class<?>> lafClassesForDefaultsLoading = getLafClassesForDefaultsLoading();
@@ -275,12 +271,11 @@ public abstract class FlatLaf
UIDefaultsLoader.loadDefaultsFromProperties( getClass(), defaults );
// use Aqua MenuBarUI if Mac screen menubar is enabled
if( useScreenMenuBar )
defaults.put( "MenuBarUI", aquaMenuBarUI );
if( SystemInfo.IS_MAC && Boolean.getBoolean( "apple.laf.useScreenMenuBar" ) )
defaults.put( "MenuBarUI", "com.apple.laf.AquaMenuBarUI" );
// initialize text antialiasing
if( !SystemInfo.IS_MAC )
putAATextInfo( defaults );
putAATextInfo( defaults );
invokePostInitialization( defaults );
@@ -307,14 +302,15 @@ public abstract class FlatLaf
uiFont = new FontUIResource( winFont );
} else if( SystemInfo.IS_MAC ) {
Font font = defaults.getFont( "Label.font" );
String fontName;
if( SystemInfo.IS_MAC_OS_10_11_EL_CAPITAN_OR_LATER ) {
// use San Francisco Text font
font = new FontUIResource( ".SF NS Text", font.getStyle(), font.getSize() );
fontName = ".SF NS Text";
} else {
// default font on older systems (see com.apple.laf.AquaFonts)
fontName = "Lucida Grande";
}
uiFont = (font instanceof FontUIResource) ? (FontUIResource) font : new FontUIResource( font );
uiFont = new FontUIResource( fontName, Font.PLAIN, 13 );
} else if( SystemInfo.IS_LINUX ) {
Font font = LinuxFontPolicy.getFont();

View File

@@ -1,57 +0,0 @@
/*
* 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;
import javax.swing.UIDefaults;
/**
* Removes UI defaults that are defined in "base" LaF (Aqua), but not used in FlatLaf.
*
* This is a temporary class that can be removed when dropping "base" LaF.
*
* @author Karl Tauber
*/
class UIDefaultsRemover
{
static final String[] REMOVE_KEYS = {
"Button.select",
"CheckBox.select",
"RadioButton.select",
"Tree.line",
};
static void removeDefaults( UIDefaults defaults ) {
for( String key : REMOVE_KEYS )
defaults.remove( key );
/*
Iterator<Object> itr = defaults.keySet().iterator();
while( itr.hasNext() ) {
Object key = itr.next();
if( key instanceof String &&
(((String)key).endsWith( ".gradient" ) ||
((String)key).endsWith( "Sound" )) )
{
itr.remove();
}
}
*/
}
}

View File

@@ -40,8 +40,7 @@ PopupMenuSeparatorUI=com.formdev.flatlaf.ui.FlatPopupMenuSeparatorUI
ProgressBarUI=com.formdev.flatlaf.ui.FlatProgressBarUI
RadioButtonUI=com.formdev.flatlaf.ui.FlatRadioButtonUI
RadioButtonMenuItemUI=com.formdev.flatlaf.ui.FlatRadioButtonMenuItemUI
[win]RootPaneUI=com.formdev.flatlaf.ui.FlatRootPaneUI
[linux]RootPaneUI=com.formdev.flatlaf.ui.FlatRootPaneUI
RootPaneUI=com.formdev.flatlaf.ui.FlatRootPaneUI
ScrollBarUI=com.formdev.flatlaf.ui.FlatScrollBarUI
ScrollPaneUI=com.formdev.flatlaf.ui.FlatScrollPaneUI
SeparatorUI=com.formdev.flatlaf.ui.FlatSeparatorUI