diff options
| author | 2023-12-24 12:46:40 +0100 | |
|---|---|---|
| committer | 2023-12-24 11:46:40 +0000 | |
| commit | e82b5a0bd7a93fdc27b30604da14014864919a6b (patch) | |
| tree | cda1dfa881d86cde378793d172fde5b9e70ee2d6 | |
| parent | Merge pull request #463 from NebelNidas/mapping-io (diff) | |
| download | enigma-e82b5a0bd7a93fdc27b30604da14014864919a6b.tar.gz enigma-e82b5a0bd7a93fdc27b30604da14014864919a6b.tar.xz enigma-e82b5a0bd7a93fdc27b30604da14014864919a6b.zip | |
Fix editor scaling (#536)
5 files changed, 49 insertions, 26 deletions
diff --git a/enigma-swing/src/main/java/cuchaz/enigma/gui/config/UiConfig.java b/enigma-swing/src/main/java/cuchaz/enigma/gui/config/UiConfig.java index 0d18df74..32d6d762 100644 --- a/enigma-swing/src/main/java/cuchaz/enigma/gui/config/UiConfig.java +++ b/enigma-swing/src/main/java/cuchaz/enigma/gui/config/UiConfig.java | |||
| @@ -276,7 +276,7 @@ public final class UiConfig { | |||
| 276 | * @return the fallback editor font | 276 | * @return the fallback editor font |
| 277 | */ | 277 | */ |
| 278 | public static Font getFallbackEditorFont() { | 278 | public static Font getFallbackEditorFont() { |
| 279 | return ScaleUtil.scaleFont(Font.decode(Font.MONOSPACED)); | 279 | return ScaleUtil.scaleFont(Font.decode(Font.MONOSPACED).deriveFont(12f)); |
| 280 | } | 280 | } |
| 281 | 281 | ||
| 282 | public static String encodeFont(Font font) { | 282 | public static String encodeFont(Font font) { |
diff --git a/enigma-swing/src/main/java/cuchaz/enigma/gui/highlight/BoxHighlightPainter.java b/enigma-swing/src/main/java/cuchaz/enigma/gui/highlight/BoxHighlightPainter.java index a97b3779..6c0b220d 100644 --- a/enigma-swing/src/main/java/cuchaz/enigma/gui/highlight/BoxHighlightPainter.java +++ b/enigma-swing/src/main/java/cuchaz/enigma/gui/highlight/BoxHighlightPainter.java | |||
| @@ -20,6 +20,8 @@ import javax.swing.text.BadLocationException; | |||
| 20 | import javax.swing.text.Highlighter; | 20 | import javax.swing.text.Highlighter; |
| 21 | import javax.swing.text.JTextComponent; | 21 | import javax.swing.text.JTextComponent; |
| 22 | 22 | ||
| 23 | import cuchaz.enigma.gui.util.ScaleUtil; | ||
| 24 | |||
| 23 | public class BoxHighlightPainter implements Highlighter.HighlightPainter { | 25 | public class BoxHighlightPainter implements Highlighter.HighlightPainter { |
| 24 | private Color fillColor; | 26 | private Color fillColor; |
| 25 | private Color borderColor; | 27 | private Color borderColor; |
| @@ -41,10 +43,10 @@ public class BoxHighlightPainter implements Highlighter.HighlightPainter { | |||
| 41 | Rectangle bounds = startRect.union(endRect); | 43 | Rectangle bounds = startRect.union(endRect); |
| 42 | 44 | ||
| 43 | // adjust the box so it looks nice | 45 | // adjust the box so it looks nice |
| 44 | bounds.x -= 2; | 46 | bounds.x -= ScaleUtil.scale(2); |
| 45 | bounds.width += 2; | 47 | bounds.width += ScaleUtil.scale(2); |
| 46 | bounds.y += 1; | 48 | bounds.y += ScaleUtil.scale(1); |
| 47 | bounds.height -= 2; | 49 | bounds.height -= ScaleUtil.scale(2); |
| 48 | 50 | ||
| 49 | return bounds; | 51 | return bounds; |
| 50 | } catch (BadLocationException ex) { | 52 | } catch (BadLocationException ex) { |
| @@ -56,15 +58,16 @@ public class BoxHighlightPainter implements Highlighter.HighlightPainter { | |||
| 56 | @Override | 58 | @Override |
| 57 | public void paint(Graphics g, int start, int end, Shape shape, JTextComponent text) { | 59 | public void paint(Graphics g, int start, int end, Shape shape, JTextComponent text) { |
| 58 | Rectangle bounds = getBounds(text, start, end); | 60 | Rectangle bounds = getBounds(text, start, end); |
| 61 | int arcSize = ScaleUtil.scale(4); | ||
| 59 | 62 | ||
| 60 | // fill the area | 63 | // fill the area |
| 61 | if (this.fillColor != null) { | 64 | if (this.fillColor != null) { |
| 62 | g.setColor(this.fillColor); | 65 | g.setColor(this.fillColor); |
| 63 | g.fillRoundRect(bounds.x, bounds.y, bounds.width, bounds.height, 4, 4); | 66 | g.fillRoundRect(bounds.x, bounds.y, bounds.width, bounds.height, arcSize, arcSize); |
| 64 | } | 67 | } |
| 65 | 68 | ||
| 66 | // draw a box around the area | 69 | // draw a box around the area |
| 67 | g.setColor(this.borderColor); | 70 | g.setColor(this.borderColor); |
| 68 | g.drawRoundRect(bounds.x, bounds.y, bounds.width, bounds.height, 4, 4); | 71 | g.drawRoundRect(bounds.x, bounds.y, bounds.width, bounds.height, arcSize, arcSize); |
| 69 | } | 72 | } |
| 70 | } | 73 | } |
diff --git a/enigma-swing/src/main/java/cuchaz/enigma/gui/highlight/SelectionHighlightPainter.java b/enigma-swing/src/main/java/cuchaz/enigma/gui/highlight/SelectionHighlightPainter.java index a807802d..ff2020e7 100644 --- a/enigma-swing/src/main/java/cuchaz/enigma/gui/highlight/SelectionHighlightPainter.java +++ b/enigma-swing/src/main/java/cuchaz/enigma/gui/highlight/SelectionHighlightPainter.java | |||
| @@ -21,6 +21,7 @@ import javax.swing.text.Highlighter; | |||
| 21 | import javax.swing.text.JTextComponent; | 21 | import javax.swing.text.JTextComponent; |
| 22 | 22 | ||
| 23 | import cuchaz.enigma.gui.config.UiConfig; | 23 | import cuchaz.enigma.gui.config.UiConfig; |
| 24 | import cuchaz.enigma.gui.util.ScaleUtil; | ||
| 24 | 25 | ||
| 25 | public class SelectionHighlightPainter implements Highlighter.HighlightPainter { | 26 | public class SelectionHighlightPainter implements Highlighter.HighlightPainter { |
| 26 | public static final SelectionHighlightPainter INSTANCE = new SelectionHighlightPainter(); | 27 | public static final SelectionHighlightPainter INSTANCE = new SelectionHighlightPainter(); |
| @@ -31,7 +32,9 @@ public class SelectionHighlightPainter implements Highlighter.HighlightPainter { | |||
| 31 | Graphics2D g2d = (Graphics2D) g; | 32 | Graphics2D g2d = (Graphics2D) g; |
| 32 | Rectangle bounds = BoxHighlightPainter.getBounds(text, start, end); | 33 | Rectangle bounds = BoxHighlightPainter.getBounds(text, start, end); |
| 33 | g2d.setColor(UiConfig.getSelectionHighlightColor()); | 34 | g2d.setColor(UiConfig.getSelectionHighlightColor()); |
| 34 | g2d.setStroke(new BasicStroke(2.0f)); | 35 | g2d.setStroke(new BasicStroke(ScaleUtil.scale(2.0f))); |
| 35 | g2d.drawRoundRect(bounds.x, bounds.y, bounds.width, bounds.height, 4, 4); | 36 | |
| 37 | int arcSize = ScaleUtil.scale(4); | ||
| 38 | g2d.drawRoundRect(bounds.x, bounds.y, bounds.width, bounds.height, arcSize, arcSize); | ||
| 36 | } | 39 | } |
| 37 | } | 40 | } |
diff --git a/enigma-swing/src/main/java/cuchaz/enigma/gui/panels/EditorPanel.java b/enigma-swing/src/main/java/cuchaz/enigma/gui/panels/EditorPanel.java index cb74ceca..b285689e 100644 --- a/enigma-swing/src/main/java/cuchaz/enigma/gui/panels/EditorPanel.java +++ b/enigma-swing/src/main/java/cuchaz/enigma/gui/panels/EditorPanel.java | |||
| @@ -111,7 +111,6 @@ public class EditorPanel { | |||
| 111 | this.editor.setEditable(false); | 111 | this.editor.setEditable(false); |
| 112 | this.editor.setSelectionColor(new Color(31, 46, 90)); | 112 | this.editor.setSelectionColor(new Color(31, 46, 90)); |
| 113 | this.editor.setCaret(new BrowserCaret()); | 113 | this.editor.setCaret(new BrowserCaret()); |
| 114 | this.editor.setFont(ScaleUtil.getFont(this.editor.getFont().getFontName(), Font.PLAIN, this.fontSize)); | ||
| 115 | this.editor.addCaretListener(event -> onCaretMove(event.getDot(), this.mouseIsPressed)); | 114 | this.editor.addCaretListener(event -> onCaretMove(event.getDot(), this.mouseIsPressed)); |
| 116 | this.editor.setCaretColor(UiConfig.getCaretColor()); | 115 | this.editor.setCaretColor(UiConfig.getCaretColor()); |
| 117 | this.editor.setContentType("text/enigma-sources"); | 116 | this.editor.setContentType("text/enigma-sources"); |
diff --git a/enigma-swing/src/main/java/cuchaz/enigma/gui/util/ScaleUtil.java b/enigma-swing/src/main/java/cuchaz/enigma/gui/util/ScaleUtil.java index bc587fa7..c11963df 100644 --- a/enigma-swing/src/main/java/cuchaz/enigma/gui/util/ScaleUtil.java +++ b/enigma-swing/src/main/java/cuchaz/enigma/gui/util/ScaleUtil.java | |||
| @@ -3,9 +3,9 @@ package cuchaz.enigma.gui.util; | |||
| 3 | import java.awt.Dimension; | 3 | import java.awt.Dimension; |
| 4 | import java.awt.Font; | 4 | import java.awt.Font; |
| 5 | import java.awt.Insets; | 5 | import java.awt.Insets; |
| 6 | import java.lang.reflect.Field; | ||
| 7 | import java.util.ArrayList; | 6 | import java.util.ArrayList; |
| 8 | import java.util.List; | 7 | import java.util.List; |
| 8 | import java.util.function.Predicate; | ||
| 9 | 9 | ||
| 10 | import javax.swing.BorderFactory; | 10 | import javax.swing.BorderFactory; |
| 11 | import javax.swing.UIManager; | 11 | import javax.swing.UIManager; |
| @@ -16,7 +16,6 @@ import com.github.swingdpi.plaf.BasicTweaker; | |||
| 16 | import com.github.swingdpi.plaf.MetalTweaker; | 16 | import com.github.swingdpi.plaf.MetalTweaker; |
| 17 | import com.github.swingdpi.plaf.NimbusTweaker; | 17 | import com.github.swingdpi.plaf.NimbusTweaker; |
| 18 | import com.github.swingdpi.plaf.WindowsTweaker; | 18 | import com.github.swingdpi.plaf.WindowsTweaker; |
| 19 | import de.sciss.syntaxpane.DefaultSyntaxKit; | ||
| 20 | 19 | ||
| 21 | import cuchaz.enigma.gui.config.UiConfig; | 20 | import cuchaz.enigma.gui.config.UiConfig; |
| 22 | 21 | ||
| @@ -95,33 +94,52 @@ public class ScaleUtil { | |||
| 95 | if (UiConfig.getActiveLookAndFeel().needsScaling()) { | 94 | if (UiConfig.getActiveLookAndFeel().needsScaling()) { |
| 96 | UiDefaultsScaler.updateAndApplyGlobalScaling((int) (100 * scale), true); | 95 | UiDefaultsScaler.updateAndApplyGlobalScaling((int) (100 * scale), true); |
| 97 | } | 96 | } |
| 98 | |||
| 99 | try { | ||
| 100 | Field defaultFontField = DefaultSyntaxKit.class.getDeclaredField("DEFAULT_FONT"); | ||
| 101 | defaultFontField.setAccessible(true); | ||
| 102 | Font font = (Font) defaultFontField.get(null); | ||
| 103 | font = font.deriveFont(12 * scale); | ||
| 104 | defaultFontField.set(null, font); | ||
| 105 | } catch (NoSuchFieldException | IllegalAccessException e) { | ||
| 106 | e.printStackTrace(); | ||
| 107 | } | ||
| 108 | } | 97 | } |
| 109 | 98 | ||
| 99 | @SuppressWarnings("null") | ||
| 110 | private static BasicTweaker createTweakerForCurrentLook(float dpiScaling) { | 100 | private static BasicTweaker createTweakerForCurrentLook(float dpiScaling) { |
| 111 | String testString = UIManager.getLookAndFeel().getName().toLowerCase(); | 101 | String testString = UIManager.getLookAndFeel().getName().toLowerCase(); |
| 112 | 102 | ||
| 113 | if (testString.contains("windows")) { | 103 | if (testString.contains("windows")) { |
| 114 | return new WindowsTweaker(dpiScaling, testString.contains("classic")); | 104 | return new WindowsTweaker(dpiScaling, testString.contains("classic")) { |
| 105 | @Override | ||
| 106 | public Font modifyFont(Object key, Font original) { | ||
| 107 | return ScaleUtil.fallbackModifyFont(key, original, super.modifyFont(key, original), scaleFactor, BasicTweaker::isUnscaled); | ||
| 108 | } | ||
| 109 | }; | ||
| 115 | } | 110 | } |
| 116 | 111 | ||
| 117 | if (testString.contains("metal")) { | 112 | if (testString.contains("metal")) { |
| 118 | return new MetalTweaker(dpiScaling); | 113 | return new MetalTweaker(dpiScaling) { |
| 114 | @Override | ||
| 115 | public Font modifyFont(Object key, Font original) { | ||
| 116 | return ScaleUtil.fallbackModifyFont(key, original, super.modifyFont(key, original), scaleFactor, BasicTweaker::isUnscaled); | ||
| 117 | } | ||
| 118 | }; | ||
| 119 | } | 119 | } |
| 120 | 120 | ||
| 121 | if (testString.contains("nimbus")) { | 121 | if (testString.contains("nimbus")) { |
| 122 | return new NimbusTweaker(dpiScaling); | 122 | return new NimbusTweaker(dpiScaling) { |
| 123 | @Override | ||
| 124 | public Font modifyFont(Object key, Font original) { | ||
| 125 | return ScaleUtil.fallbackModifyFont(key, original, super.modifyFont(key, original), scaleFactor, BasicTweaker::isUnscaled); | ||
| 126 | } | ||
| 127 | }; | ||
| 128 | } | ||
| 129 | |||
| 130 | return new BasicTweaker(dpiScaling) { | ||
| 131 | @Override | ||
| 132 | public Font modifyFont(Object key, Font original) { | ||
| 133 | return ScaleUtil.fallbackModifyFont(key, original, super.modifyFont(key, original), scaleFactor, BasicTweaker::isUnscaled); | ||
| 134 | } | ||
| 135 | }; | ||
| 136 | } | ||
| 137 | |||
| 138 | private static Font fallbackModifyFont(Object key, Font original, Font modified, float scaleFactor, Predicate<Float> unscaledCheck) { | ||
| 139 | if (modified == original && !unscaledCheck.test(scaleFactor)) { | ||
| 140 | return original.deriveFont(original.getSize() * scaleFactor); | ||
| 123 | } | 141 | } |
| 124 | 142 | ||
| 125 | return new BasicTweaker(dpiScaling); | 143 | return modified; |
| 126 | } | 144 | } |
| 127 | } | 145 | } |