这是基于我的初始原型设计的解决方案。
我对某些部分不满意,例如修改“整数”和“字体”的规则非常“神奇”。这是我想从其他有更多 Swing / L&F 经验的人那里听到的一个地方。
我已经把它变成了一个社区维基,所以如果人们更愿意对此进行迭代并添加他们的知识,而不是发布他们自己的解决方案,那么请随意。
我从一个可以修改某些 ui-defaults 的界面开始:
public interface Tweaker {
void initialTweaks();
Font modifyFont(Object key, Font original);
Icon modifyIcon(Object key, Icon original);
Integer modifyInteger(Object key, Integer original);
}
可以这样调用:
public void scaleUiDefaults() {
float dpiScale = Toolkit.getDefaultToolkit().getScreenResolution() / 96f;
Tweaker delegate = createTweakerForCurrentLook(dpiScale);
tweakUiDefaults(delegate, dpiScale);
}
private Tweaker createTweakerForCurrentLook(float dpiScaling) {
String testString = UIManager.getLookAndFeel().getName().toLowerCase();
if (testString.contains("windows")) return new WindowsTweaker(dpiScaling);
if (testString.contains("nimbus")) return new NimbusTweaker(dpiScaling);
return new BasicTweaker(dpiScaling);
}
private void tweakUiDefaults(Tweaker delegate, float multiplier) {
UIDefaults defaults = UIManager.getLookAndFeelDefaults();
delegate.initialTweaks();
for (Object key: Collections.list(defaults.keys())) {
Object original = defaults.get(key);
Object newValue = getUpdatedValue(delegate, key, original);
if (newValue != null && newValue != original) {
defaults.put(key, newValue);
}
}
}
private Object getUpdatedValue(Tweaker delegate, Object key, Object original) {
if (original instanceof Font) return delegate.modifyFont(key, (Font) original);
if (original instanceof Icon) return delegate.modifyIcon(key, (Icon) original);
if (original instanceof Integer) return delegate.modifyInteger(key, (Integer) original);
return null;
}
我把看起来被大多数 L&F 使用的功能放在一个基类中。这似乎无需进一步改进就可以处理 Metal:
public class BasicTweaker {
protected final float scaleFactor;
protected final UIDefaults uiDefaults = UIManager.getLookAndFeelDefaults();
public BasicTweaker(float scaleFactor) {
this.scaleFactor = scaleFactor;
}
public void initialTweaks() {}
public Font modifyFont(Object key, Font original) {
// Ignores title & accelerator fonts (for example)
if (original instanceof FontUIResource && key.toString().endsWith(".font")) {
return newScaledFontUIResource(original, scaleFactor);
}
return original;
}
protected static FontUIResource newScaledFontUIResource(Font original, float scale) {
int newSize = Math.round(original.getSize() * scale);
return new FontUIResource(original.getName(), original.getStyle(), newSize);
}
public Icon modifyIcon(Object key, Icon original) {
return new IconUIResource(new ScaledIcon(original, scaleFactor));
}
public Integer modifyInteger(Object key, Integer original) {
if (!endsWithOneOf(lower(key), LOWER_SUFFIXES_FOR_SCALED_INTEGERS)) {
return original;
}
return (int) (original * scaleFactor);
}
private boolean endsWithOneOf(String text, String[] suffixes) {
return Arrays.stream(suffixes).anyMatch(suffix -> text.endsWith(suffix));
}
private String lower(Object key) {
return (key instanceof String) ? ((String) key).toLowerCase() : "";
}
private static final String[] LOWER_SUFFIXES_FOR_SCALED_INTEGERS =
new String[] { "width", "height", "indent", "size", "gap" };
}
上面使用的类ScaledIcon
可能超出了范围 - 但它本质上只是ImageIcon(image).paintIcon
使用修改后的宽度和高度进行调用。
然后覆盖BasicTweaker
其他 L&F ......
视窗:
public class WindowsTweaker extends BasicTweaker {
public WindowsTweaker(float scaleFactor) {
// Windows already scales fonts, scrollbar sizes (etc) according to the system DPI settings.
// This lets us adjust to the REQUESTED scale factor, relative to the CURRENT scale factor
super(scaleFactor / getCurrentScaling());
}
private static float getCurrentScaling() {
int dpi = Toolkit.getDefaultToolkit().getScreenResolution();
return dpi / 96f;
}
public Font modifyFont(Object key, Font original) {
return super.modifyFont(key, original);
}
public Icon modifyIcon(Object key, Icon original) {
return original;
}
}
雨云:
public class NimbusTweaker extends BasicTweaker {
public NimbusTweaker(float scaleFactor) {
super(scaleFactor);
}
public void initialTweaks() {
Font font = uiDefaults.getFont("defaultFont");
if (font != null) {
uiDefaults.put("defaultFont", new FontUIResource(
font.getName(), font.getStyle(), Math.round(font.getSize() * scaleFactor)));
}
}
// Setting "defaultFont" above is sufficient as this will be inherited by all others
public Font modifyFont(Object key, Font original) {
return original;
}
// Scaling Radio or CheckBox button icons leads to really weird artifacts in Nimbus? Disable
public Icon modifyIcon(Object key, Icon original) {
return original;
}
}