mirror of
https://gitflic.ru/project/openide/openide.git
synced 2026-09-27 10:03:11 +07:00
Make JBUI.scale a bean property; add JBDimension.update()
- update ActionToolbar cached dimensions on JBUI.scale change.
This commit is contained in:
+13
-4
@@ -39,10 +39,7 @@ import com.intellij.ui.awt.RelativePoint;
|
||||
import com.intellij.ui.awt.RelativeRectangle;
|
||||
import com.intellij.util.ObjectUtils;
|
||||
import com.intellij.util.containers.ContainerUtil;
|
||||
import com.intellij.util.ui.JBInsets;
|
||||
import com.intellij.util.ui.JBSwingUtilities;
|
||||
import com.intellij.util.ui.JBUI;
|
||||
import com.intellij.util.ui.UIUtil;
|
||||
import com.intellij.util.ui.*;
|
||||
import com.intellij.util.ui.update.Activatable;
|
||||
import com.intellij.util.ui.update.UiNotifyConnector;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
@@ -53,6 +50,8 @@ import java.awt.*;
|
||||
import java.awt.event.ComponentAdapter;
|
||||
import java.awt.event.ComponentEvent;
|
||||
import java.awt.event.MouseEvent;
|
||||
import java.beans.PropertyChangeEvent;
|
||||
import java.beans.PropertyChangeListener;
|
||||
import java.lang.ref.ReferenceQueue;
|
||||
import java.lang.ref.WeakReference;
|
||||
import java.util.ArrayList;
|
||||
@@ -65,6 +64,16 @@ public class ActionToolbarImpl extends JPanel implements ActionToolbar {
|
||||
private static final List<ActionToolbarImpl> ourToolbars = new LinkedList<>();
|
||||
private static final String RIGHT_ALIGN_KEY = "RIGHT_ALIGN";
|
||||
|
||||
static {
|
||||
JBUI.addPropertyChangeListener(JBUI.SCALE_FACTOR_PROPERTY, new PropertyChangeListener() {
|
||||
@Override
|
||||
public void propertyChange(PropertyChangeEvent e) {
|
||||
((JBDimension)ActionToolbar.DEFAULT_MINIMUM_BUTTON_SIZE).update();
|
||||
((JBDimension)ActionToolbar.NAVBAR_MINIMUM_BUTTON_SIZE).update();
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
public static void updateAllToolbarsImmediately() {
|
||||
for (ActionToolbarImpl toolbar : new ArrayList<>(ourToolbars)) {
|
||||
toolbar.updateActionsImmediately();
|
||||
|
||||
@@ -38,6 +38,8 @@ import javax.swing.*;
|
||||
import java.awt.*;
|
||||
import java.awt.image.BufferedImage;
|
||||
import java.awt.image.ImageFilter;
|
||||
import java.beans.PropertyChangeEvent;
|
||||
import java.beans.PropertyChangeListener;
|
||||
import java.lang.ref.Reference;
|
||||
import java.lang.reflect.Field;
|
||||
import java.net.URL;
|
||||
@@ -58,10 +60,20 @@ public final class IconLoader {
|
||||
|
||||
private static boolean USE_DARK_ICONS = UIUtil.isUnderDarcula();
|
||||
|
||||
private static float SCALE = JBUI.scale(1f);
|
||||
private static float myJBUIScale = 1f;
|
||||
private static ImageFilter IMAGE_FILTER;
|
||||
|
||||
static {
|
||||
JBUI.addPropertyChangeListener(JBUI.SCALE_FACTOR_PROPERTY, new PropertyChangeListener() {
|
||||
@Override
|
||||
public void propertyChange(PropertyChangeEvent e) {
|
||||
float scale = (Float)e.getNewValue();
|
||||
if (scale != myJBUIScale) {
|
||||
myJBUIScale = scale;
|
||||
clearCache();
|
||||
}
|
||||
}
|
||||
});
|
||||
installPathPatcher(new DeprecatedDuplicatesIconPathPatcher());
|
||||
}
|
||||
|
||||
@@ -91,13 +103,6 @@ public final class IconLoader {
|
||||
clearCache();
|
||||
}
|
||||
|
||||
public static void setScale(float scale) {
|
||||
if (scale != SCALE) {
|
||||
SCALE = scale;
|
||||
clearCache();
|
||||
}
|
||||
}
|
||||
|
||||
public static void setFilter(ImageFilter filter) {
|
||||
if (IMAGE_FILTER != filter) {
|
||||
IMAGE_FILTER = filter;
|
||||
@@ -349,7 +354,7 @@ public final class IconLoader {
|
||||
|
||||
/**
|
||||
* Gets a snapshot of the icon, immune to changes made by these calls:
|
||||
* {@link IconLoader#setScale(float)}, {@link IconLoader#setFilter(ImageFilter)}, {@link IconLoader#setUseDarkIcons(boolean)}
|
||||
* {@link IconLoader#setFilter(ImageFilter)}, {@link IconLoader#setUseDarkIcons(boolean)}
|
||||
*
|
||||
* @param icon the source icon
|
||||
* @return the icon snapshot
|
||||
|
||||
@@ -22,7 +22,7 @@ import java.awt.*;
|
||||
* @author Konstantin Bulenkov
|
||||
*/
|
||||
public class JBDimension extends Dimension {
|
||||
public final float originalScale = JBUI.scale(1f);
|
||||
public float myJBUIScale = JBUI.scale(1f);
|
||||
|
||||
public JBDimension(int width, int height) {
|
||||
super(scale(width), scale(height));
|
||||
@@ -64,4 +64,12 @@ public class JBDimension extends Dimension {
|
||||
size.height = scale(height);
|
||||
return size;
|
||||
}
|
||||
|
||||
// [tav] todo: may lose precision
|
||||
public void update() {
|
||||
float scale = JBUI.scale(1f);
|
||||
width = (int)(width * scale / myJBUIScale);
|
||||
height = (int)(height * scale / myJBUIScale);
|
||||
myJBUIScale = scale;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -16,7 +16,6 @@
|
||||
package com.intellij.util.ui;
|
||||
|
||||
import com.intellij.openapi.diagnostic.Logger;
|
||||
import com.intellij.openapi.util.IconLoader;
|
||||
import com.intellij.openapi.util.Pair;
|
||||
import com.intellij.openapi.util.ScalableIcon;
|
||||
import com.intellij.openapi.util.SystemInfo;
|
||||
@@ -31,6 +30,8 @@ import javax.swing.border.Border;
|
||||
import javax.swing.border.CompoundBorder;
|
||||
import javax.swing.plaf.UIResource;
|
||||
import java.awt.*;
|
||||
import java.beans.PropertyChangeListener;
|
||||
import java.beans.PropertyChangeSupport;
|
||||
|
||||
/**
|
||||
* @author Konstantin Bulenkov
|
||||
@@ -38,6 +39,10 @@ import java.awt.*;
|
||||
public class JBUI {
|
||||
private static final Logger LOG = Logger.getInstance("#com.intellij.util.ui.JBUI");
|
||||
|
||||
public static final String SCALE_FACTOR_PROPERTY = "JBUI.scale";
|
||||
|
||||
private static final PropertyChangeSupport PCS = new PropertyChangeSupport(new JBUI());
|
||||
|
||||
/**
|
||||
* A default system scale factor.
|
||||
*/
|
||||
@@ -49,6 +54,21 @@ public class JBUI {
|
||||
setScaleFactor(SYSTEM_DEF_SCALE);
|
||||
}
|
||||
|
||||
/**
|
||||
* Adds property change listener. Supported properties:
|
||||
* {@link #SCALE_FACTOR_PROPERTY}
|
||||
*/
|
||||
public static void addPropertyChangeListener(String propertyName, PropertyChangeListener listener) {
|
||||
PCS.addPropertyChangeListener(propertyName, listener);
|
||||
}
|
||||
|
||||
/**
|
||||
* @see #addPropertyChangeListener(String, PropertyChangeListener)
|
||||
*/
|
||||
public static void removePropertyChangeListener(String propertyName, PropertyChangeListener listener) {
|
||||
PCS.removePropertyChangeListener(propertyName, listener);
|
||||
}
|
||||
|
||||
private static float getSystemDefScale() {
|
||||
if (SystemInfo.isMac) {
|
||||
return 1.0f;
|
||||
@@ -70,10 +90,14 @@ public class JBUI {
|
||||
return size / UIUtil.DEF_SYSTEM_FONT_SIZE;
|
||||
}
|
||||
|
||||
private static void setScaleFactorProperty(float scale) {
|
||||
PCS.firePropertyChange(SCALE_FACTOR_PROPERTY, scaleFactor, scaleFactor = scale);
|
||||
LOG.info("UI scale factor: " + scaleFactor);
|
||||
}
|
||||
|
||||
public static void setScaleFactor(float scale) {
|
||||
if (SystemProperties.has("hidpi") && !SystemProperties.is("hidpi")) {
|
||||
scaleFactor = 1.0f;
|
||||
LOG.info("UI scale factor: 1.0");
|
||||
setScaleFactorProperty(1.0f);
|
||||
return;
|
||||
}
|
||||
|
||||
@@ -90,10 +114,7 @@ public class JBUI {
|
||||
if (scaleFactor == scale) {
|
||||
return;
|
||||
}
|
||||
LOG.info("UI scale factor: " + scale);
|
||||
|
||||
scaleFactor = scale;
|
||||
IconLoader.setScale(scale);
|
||||
setScaleFactorProperty(scale);
|
||||
}
|
||||
|
||||
public static int scale(int i) {
|
||||
@@ -117,10 +138,10 @@ public class JBUI {
|
||||
public static JBDimension size(Dimension size) {
|
||||
if (size instanceof JBDimension) {
|
||||
final JBDimension jbSize = (JBDimension)size;
|
||||
if (jbSize.originalScale == scale(1f)) {
|
||||
if (jbSize.myJBUIScale == scale(1f)) {
|
||||
return jbSize;
|
||||
}
|
||||
final JBDimension newSize = new JBDimension((int)(jbSize.width / jbSize.originalScale), (int)(jbSize.height / jbSize.originalScale));
|
||||
final JBDimension newSize = new JBDimension((int)(jbSize.width / jbSize.myJBUIScale), (int)(jbSize.height / jbSize.myJBUIScale));
|
||||
return size instanceof UIResource ? newSize.asUIResource() : newSize;
|
||||
}
|
||||
return new JBDimension(size.width, size.height);
|
||||
|
||||
Reference in New Issue
Block a user