Skip to content

Commit

Permalink
Fix editor scaling (#536)
Browse files Browse the repository at this point in the history
  • Loading branch information
NebelNidas authored Dec 24, 2023
1 parent 7622b75 commit e82b5a0
Show file tree
Hide file tree
Showing 5 changed files with 49 additions and 26 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -276,7 +276,7 @@ public static void setEditorFont(Font font) {
* @return the fallback editor font
*/
public static Font getFallbackEditorFont() {
return ScaleUtil.scaleFont(Font.decode(Font.MONOSPACED));
return ScaleUtil.scaleFont(Font.decode(Font.MONOSPACED).deriveFont(12f));
}

public static String encodeFont(Font font) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@
import javax.swing.text.Highlighter;
import javax.swing.text.JTextComponent;

import cuchaz.enigma.gui.util.ScaleUtil;

public class BoxHighlightPainter implements Highlighter.HighlightPainter {
private Color fillColor;
private Color borderColor;
Expand All @@ -41,10 +43,10 @@ public static Rectangle getBounds(JTextComponent text, int start, int end) {
Rectangle bounds = startRect.union(endRect);

// adjust the box so it looks nice
bounds.x -= 2;
bounds.width += 2;
bounds.y += 1;
bounds.height -= 2;
bounds.x -= ScaleUtil.scale(2);
bounds.width += ScaleUtil.scale(2);
bounds.y += ScaleUtil.scale(1);
bounds.height -= ScaleUtil.scale(2);

return bounds;
} catch (BadLocationException ex) {
Expand All @@ -56,15 +58,16 @@ public static Rectangle getBounds(JTextComponent text, int start, int end) {
@Override
public void paint(Graphics g, int start, int end, Shape shape, JTextComponent text) {
Rectangle bounds = getBounds(text, start, end);
int arcSize = ScaleUtil.scale(4);

// fill the area
if (this.fillColor != null) {
g.setColor(this.fillColor);
g.fillRoundRect(bounds.x, bounds.y, bounds.width, bounds.height, 4, 4);
g.fillRoundRect(bounds.x, bounds.y, bounds.width, bounds.height, arcSize, arcSize);
}

// draw a box around the area
g.setColor(this.borderColor);
g.drawRoundRect(bounds.x, bounds.y, bounds.width, bounds.height, 4, 4);
g.drawRoundRect(bounds.x, bounds.y, bounds.width, bounds.height, arcSize, arcSize);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
import javax.swing.text.JTextComponent;

import cuchaz.enigma.gui.config.UiConfig;
import cuchaz.enigma.gui.util.ScaleUtil;

public class SelectionHighlightPainter implements Highlighter.HighlightPainter {
public static final SelectionHighlightPainter INSTANCE = new SelectionHighlightPainter();
Expand All @@ -31,7 +32,9 @@ public void paint(Graphics g, int start, int end, Shape shape, JTextComponent te
Graphics2D g2d = (Graphics2D) g;
Rectangle bounds = BoxHighlightPainter.getBounds(text, start, end);
g2d.setColor(UiConfig.getSelectionHighlightColor());
g2d.setStroke(new BasicStroke(2.0f));
g2d.drawRoundRect(bounds.x, bounds.y, bounds.width, bounds.height, 4, 4);
g2d.setStroke(new BasicStroke(ScaleUtil.scale(2.0f)));

int arcSize = ScaleUtil.scale(4);
g2d.drawRoundRect(bounds.x, bounds.y, bounds.width, bounds.height, arcSize, arcSize);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,6 @@ public EditorPanel(Gui gui) {
this.editor.setEditable(false);
this.editor.setSelectionColor(new Color(31, 46, 90));
this.editor.setCaret(new BrowserCaret());
this.editor.setFont(ScaleUtil.getFont(this.editor.getFont().getFontName(), Font.PLAIN, this.fontSize));
this.editor.addCaretListener(event -> onCaretMove(event.getDot(), this.mouseIsPressed));
this.editor.setCaretColor(UiConfig.getCaretColor());
this.editor.setContentType("text/enigma-sources");
Expand Down
50 changes: 34 additions & 16 deletions enigma-swing/src/main/java/cuchaz/enigma/gui/util/ScaleUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@
import java.awt.Dimension;
import java.awt.Font;
import java.awt.Insets;
import java.lang.reflect.Field;
import java.util.ArrayList;
import java.util.List;
import java.util.function.Predicate;

import javax.swing.BorderFactory;
import javax.swing.UIManager;
Expand All @@ -16,7 +16,6 @@
import com.github.swingdpi.plaf.MetalTweaker;
import com.github.swingdpi.plaf.NimbusTweaker;
import com.github.swingdpi.plaf.WindowsTweaker;
import de.sciss.syntaxpane.DefaultSyntaxKit;

import cuchaz.enigma.gui.config.UiConfig;

Expand Down Expand Up @@ -95,33 +94,52 @@ public static void applyScaling() {
if (UiConfig.getActiveLookAndFeel().needsScaling()) {
UiDefaultsScaler.updateAndApplyGlobalScaling((int) (100 * scale), true);
}

try {
Field defaultFontField = DefaultSyntaxKit.class.getDeclaredField("DEFAULT_FONT");
defaultFontField.setAccessible(true);
Font font = (Font) defaultFontField.get(null);
font = font.deriveFont(12 * scale);
defaultFontField.set(null, font);
} catch (NoSuchFieldException | IllegalAccessException e) {
e.printStackTrace();
}
}

@SuppressWarnings("null")
private static BasicTweaker createTweakerForCurrentLook(float dpiScaling) {
String testString = UIManager.getLookAndFeel().getName().toLowerCase();

if (testString.contains("windows")) {
return new WindowsTweaker(dpiScaling, testString.contains("classic"));
return new WindowsTweaker(dpiScaling, testString.contains("classic")) {
@Override
public Font modifyFont(Object key, Font original) {
return ScaleUtil.fallbackModifyFont(key, original, super.modifyFont(key, original), scaleFactor, BasicTweaker::isUnscaled);
}
};
}

if (testString.contains("metal")) {
return new MetalTweaker(dpiScaling);
return new MetalTweaker(dpiScaling) {
@Override
public Font modifyFont(Object key, Font original) {
return ScaleUtil.fallbackModifyFont(key, original, super.modifyFont(key, original), scaleFactor, BasicTweaker::isUnscaled);
}
};
}

if (testString.contains("nimbus")) {
return new NimbusTweaker(dpiScaling);
return new NimbusTweaker(dpiScaling) {
@Override
public Font modifyFont(Object key, Font original) {
return ScaleUtil.fallbackModifyFont(key, original, super.modifyFont(key, original), scaleFactor, BasicTweaker::isUnscaled);
}
};
}

return new BasicTweaker(dpiScaling) {
@Override
public Font modifyFont(Object key, Font original) {
return ScaleUtil.fallbackModifyFont(key, original, super.modifyFont(key, original), scaleFactor, BasicTweaker::isUnscaled);
}
};
}

private static Font fallbackModifyFont(Object key, Font original, Font modified, float scaleFactor, Predicate<Float> unscaledCheck) {
if (modified == original && !unscaledCheck.test(scaleFactor)) {
return original.deriveFont(original.getSize() * scaleFactor);
}

return new BasicTweaker(dpiScaling);
return modified;
}
}

0 comments on commit e82b5a0

Please sign in to comment.