Skip to content

Commit

Permalink
Add keybinds to change editor font size
Browse files Browse the repository at this point in the history
  • Loading branch information
yapht committed Feb 2, 2022
1 parent 9358cbf commit e2cbecc
Show file tree
Hide file tree
Showing 6 changed files with 83 additions and 15 deletions.
16 changes: 16 additions & 0 deletions src/main/java/me/coley/recaf/config/ConfKeybinding.java
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,22 @@ public class ConfKeybinding extends Config {
Binding.from(KeyCode.CONTROL, KeyCode.Q), // META + Q on Mac closes the window so probably not a great idea
BindingCreator.OSBinding.from(OSUtil.MAC, Binding.from(KeyCode.META, KeyCode.A))
).buildKeyBindingForCurrentOS();
/**
* Increase editor font size.
*/
@Conf("binding.incfontsize")
public Binding incFontSize = BindingCreator.from(
Binding.from(KeyCode.CONTROL, KeyCode.EQUALS),
BindingCreator.OSBinding.from(OSUtil.MAC, Binding.from(KeyCode.META, KeyCode.EQUALS))
).buildKeyBindingForCurrentOS();
/**
* Decrease editor font size.
*/
@Conf("binding.decfontsize")
public Binding decFontSize = BindingCreator.from(
Binding.from(KeyCode.CONTROL, KeyCode.MINUS),
BindingCreator.OSBinding.from(OSUtil.MAC, Binding.from(KeyCode.META, KeyCode.MINUS))
).buildKeyBindingForCurrentOS();

/**
* Track if the user is updating a keybind, so if when they are and they hit a key that is bound,
Expand Down
34 changes: 30 additions & 4 deletions src/main/java/me/coley/recaf/ui/controls/FontSlider.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import javafx.scene.Scene;
import javafx.scene.control.Slider;
import me.coley.recaf.Recaf;
import me.coley.recaf.config.ConfigManager;
import me.coley.recaf.config.FieldWrapper;
import me.coley.recaf.control.gui.GuiController;
import org.apache.commons.io.FileUtils;
Expand All @@ -19,6 +20,8 @@
*/
public class FontSlider extends Slider {
private static final File FONT_SIZE_CSS = Recaf.getDirectory("style").resolve("font-size.css").toFile();
private static final int FONT_SIZE_MIN = 10;
private static final int FONT_SIZE_MAX = 16;

/**
* @param controller
Expand All @@ -27,8 +30,8 @@ public class FontSlider extends Slider {
* Font size field wrapper.
*/
public FontSlider(GuiController controller, FieldWrapper wrapper) {
setMin(10);
setMax(16);
setMin(FONT_SIZE_MIN);
setMax(FONT_SIZE_MAX);
setMajorTickUnit(1);
setMinorTickCount(0);
setShowTickMarks(true);
Expand All @@ -47,21 +50,44 @@ public FontSlider(GuiController controller, FieldWrapper wrapper) {
}));
}

/**
* Clamps the font size so that it doesn't go out of bounds.
* @param configManager
* ConfigManager to update.
*/
private static void clampFontSize(ConfigManager configManager) {
if(configManager.display().uiFontSize > FONT_SIZE_MAX)
configManager.display().uiFontSize = FONT_SIZE_MAX;

if(configManager.display().uiFontSize < FONT_SIZE_MIN)
configManager.display().uiFontSize = FONT_SIZE_MIN;

if(configManager.display().monoFontSize > FONT_SIZE_MAX)
configManager.display().monoFontSize = FONT_SIZE_MAX;

if(configManager.display().monoFontSize < FONT_SIZE_MIN)
configManager.display().monoFontSize = FONT_SIZE_MIN;
}

/**
* Update's the font-size override sheet and reapplies styles to open windows.
*
* @param controller
* Controller to update.
*/
private static void update(GuiController controller) {
public static void update(GuiController controller) {
try {
clampFontSize(controller.config());

double uiFontSize = controller.config().display().uiFontSize;
double monoFontSize = controller.config().display().monoFontSize;

String css = ".root { -fx-font-size: " + uiFontSize + "px; }\n" +
".lineno { -fx-font-size: " + uiFontSize + "px; }\n" +
".h1 { -fx-font-size: " + (uiFontSize + 5) + "px; }\n" +
".h2 { -fx-font-size: " + (uiFontSize + 3) + "px; }\n" +
".monospaced { -fx-font-size: " + monoFontSize + "px; }\n";
".monospaced { -fx-font-size: " + monoFontSize + "px; }\n" +
".monospaced-tree { -fx-font-size: " + uiFontSize + "px; }\n";
FileUtils.write(FONT_SIZE_CSS, css, StandardCharsets.UTF_8);
controller.windows().reapplyStyles();
} catch (IOException ex) {
Expand Down
20 changes: 10 additions & 10 deletions src/main/java/me/coley/recaf/ui/controls/tree/JavaResourceCell.java
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ private static GuiController getController() {
String text = ci.getLocalName();
Node g = UiUtil.createClassGraphic(access);
cell.getStyleClass().add("tree-cell-class");
cell.getStyleClass().add("monospaced");
cell.getStyleClass().add("monospaced-tree");
cell.setContextMenu(setupMenu(cell, ci).ofClass(ci.getClassName()));
cell.setGraphic(g);
cell.setText(text);
Expand All @@ -104,7 +104,7 @@ private static GuiController getController() {
String owner = ((ClassItem) mi.getParent()).getClassName();
ContextBuilder menu = setupMenu(cell, mi);
String text = mi.getLocalName();
Node g = null;
Node g;
if(mi.isField()) {
g = UiUtil.createFieldGraphic(mi.getMemberAccess());
cell.setContextMenu(menu.ofField(owner, mi.getMemberName(), mi.getMemberDesc()));
Expand All @@ -113,7 +113,7 @@ private static GuiController getController() {
cell.setContextMenu(menu.ofMethod(owner, mi.getMemberName(), mi.getMemberDesc()));
}
cell.getStyleClass().add("tree-cell-member");
cell.getStyleClass().add("monospaced");
cell.getStyleClass().add("monospaced-tree");
cell.setGraphic(g);
cell.setText(text);
});
Expand All @@ -128,7 +128,7 @@ private static GuiController getController() {
String text = ii.getLocalName();
Node g = new IconView("icons/result.png");
cell.getStyleClass().add("tree-cell-instruction");
cell.getStyleClass().add("monospaced");
cell.getStyleClass().add("monospaced-tree");
cell.setContextMenu(setupMenu(cell, ii).ofInsn(owner, name, desc, ii.getInsn()));
cell.setGraphic(g);
cell.setText(text);
Expand All @@ -139,7 +139,7 @@ private static GuiController getController() {
String text = ai.getLocalName();
Node g = new IconView("icons/class/annotation.png");
cell.getStyleClass().add("tree-cell-annotation");
cell.getStyleClass().add("monospaced");
cell.getStyleClass().add("monospaced-tree");
cell.setContextMenu(setupMenu(cell, ai).ofClass(ai.getAnnoName()));
cell.setGraphic(g);
cell.setText(text);
Expand All @@ -155,7 +155,7 @@ private static GuiController getController() {
li.getLocal().getDescriptor();
Node g = UiUtil.createClassGraphic(access);
cell.getStyleClass().add("tree-cell-local");
cell.getStyleClass().add("monospaced");
cell.getStyleClass().add("monospaced-tree");
cell.setContextMenu(setupMenu(cell, li).ofClass(className));
cell.setGraphic(g);
cell.setText(text);
Expand All @@ -168,7 +168,7 @@ private static GuiController getController() {
String text = "CATCH " + className;
Node g = UiUtil.createClassGraphic(access);
cell.getStyleClass().add("tree-cell-catch");
cell.getStyleClass().add("monospaced");
cell.getStyleClass().add("monospaced-tree");
cell.setContextMenu(setupMenu(cell, ci).ofClass(className));
cell.setGraphic(g);
cell.setText(text);
Expand All @@ -181,7 +181,7 @@ private static GuiController getController() {
Node g = UiUtil.createFileGraphic(fi.getLocalName());
cell.setContextMenu(setupMenu(cell, fi).ofFile(fileName));
cell.getStyleClass().add("tree-cell-file");
cell.getStyleClass().add("monospaced");
cell.getStyleClass().add("monospaced-tree");
cell.setGraphic(g);
cell.setText(text);
});
Expand All @@ -192,7 +192,7 @@ private static GuiController getController() {
Node g = new IconView("icons/class/package-flat.png");
cell.setContextMenu(setupMenu(cell, pi).ofPackage(pi.getPackageName()));
cell.getStyleClass().add("tree-cell-directory");
cell.getStyleClass().add("monospaced");
cell.getStyleClass().add("monospaced-tree");
cell.setGraphic(g);
cell.setText(text);
});
Expand All @@ -202,7 +202,7 @@ private static GuiController getController() {
String text = di.getLocalName();
Node g = new IconView("icons/class/package-flat.png");
cell.getStyleClass().add("tree-cell-directory");
cell.getStyleClass().add("monospaced");
cell.getStyleClass().add("monospaced-tree");
cell.setGraphic(g);
cell.setText(text);
});
Expand Down
18 changes: 17 additions & 1 deletion src/main/java/me/coley/recaf/ui/controls/view/ClassViewport.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,12 @@

import javafx.application.Platform;
import javafx.scene.input.KeyEvent;
import me.coley.recaf.config.ConfigManager;
import me.coley.recaf.control.gui.GuiController;
import me.coley.recaf.decompile.DecompileImpl;
import me.coley.recaf.plugin.PluginKeybinds;
import me.coley.recaf.ui.controls.ClassEditor;
import me.coley.recaf.ui.controls.FontSlider;
import me.coley.recaf.ui.controls.HexEditor;
import me.coley.recaf.ui.controls.popup.SuggestionWindow;
import me.coley.recaf.ui.controls.node.ClassNodeEditorPane;
Expand Down Expand Up @@ -59,10 +61,24 @@ protected void handleKeyReleased(KeyEvent e) {
}
});

if(controller.config().keys().swapview.match(e)) {
ConfigManager config = controller.config();

if(config.keys().swapview.match(e)) {
setOverrideMode(ClassMode.values()[(getClassMode().ordinal() + 1) % ClassMode.values().length]);
requestFocus();
}

if(config.keys().incFontSize.match(e)) {
config.display().monoFontSize++;
FontSlider.update(controller);
config.save();
}

if(config.keys().decFontSize.match(e)) {
config.display().monoFontSize--;
FontSlider.update(controller);
config.save();
}
}

@Override
Expand Down
6 changes: 6 additions & 0 deletions src/main/resources/style/base.css
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,12 @@
.monospaced {
-fx-font-family: 'monospaced';
}

/* Separate class for tree cells, they need to use the ui font size */
.monospaced-tree {
-fx-font-family: 'monospaced';
}

.drag-target {
-fx-effect: innershadow(one-pass-box, rgba(15, 200, 255), 50, 0.333, 0, 0);
}
Expand Down
4 changes: 4 additions & 0 deletions src/main/resources/translations/en.json
Original file line number Diff line number Diff line change
Expand Up @@ -531,6 +531,10 @@
"binding.gotodef.desc": "Jump to the definition of the selected item.",
"binding.swapview.name": "Swap view",
"binding.swapview.desc": "Swaps your view to the next available viewport type.",
"binding.incfontsize.name": "Increase editor font size",
"binding.incfontsize.desc": "Increases the editor's font size.",
"binding.decfontsize.name": "Decrease editor font size",
"binding.decfontsize.desc": "Decreases the editor's font size.",

"backend": "Backend / Private",

Expand Down

0 comments on commit e2cbecc

Please # to comment.