Skip to content

Commit

Permalink
fix(gui): use another implementation for font dialog (#2310)
Browse files Browse the repository at this point in the history
  • Loading branch information
skylot committed Oct 22, 2024
1 parent 3788e4e commit e5be41b
Show file tree
Hide file tree
Showing 6 changed files with 125 additions and 65 deletions.
2 changes: 1 addition & 1 deletion jadx-gui/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ dependencies {
implementation("io.github.oshai:kotlin-logging-jvm:7.0.0")

implementation("com.fifesoft:rsyntaxtextarea:3.4.1")
implementation(files("libs/jfontchooser-1.0.5.jar"))
implementation("org.drjekyll:fontchooser:3.1.0")
implementation("hu.kazocsaba:image-viewer:1.2.3")

implementation("com.formdev:flatlaf:3.5.1")
Expand Down
Binary file removed jadx-gui/libs/jfontchooser-1.0.5.jar
Binary file not shown.

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -44,8 +44,6 @@
import com.google.gson.GsonBuilder;
import com.google.gson.JsonObject;

import say.swing.JFontChooser;

import jadx.api.CommentsLevel;
import jadx.api.DecompilationMode;
import jadx.api.JadxArgs;
Expand All @@ -63,6 +61,7 @@
import jadx.gui.settings.LineNumbersMode;
import jadx.gui.settings.XposedCodegenLanguage;
import jadx.gui.settings.ui.cache.CacheSettingsGroup;
import jadx.gui.settings.ui.font.JadxFontDialog;
import jadx.gui.settings.ui.plugins.PluginSettings;
import jadx.gui.settings.ui.shortcut.ShortcutsSettingsGroup;
import jadx.gui.ui.MainWindow;
Expand Down Expand Up @@ -371,11 +370,9 @@ private SettingsGroup makeAppearanceGroup() {
fontBtn.addMouseListener(new MouseAdapter() {
@Override
public void mouseClicked(MouseEvent e) {
JFontChooser fontChooser = new JFontChooser();
fontChooser.setSelectedFont(settings.getFont());
int result = fontChooser.showDialog(JadxSettingsWindow.this);
if (result == JFontChooser.OK_OPTION) {
Font font = fontChooser.getSelectedFont();
Font font = new JadxFontDialog(JadxSettingsWindow.this, NLS.str("preferences.font"))
.select(settings.getFont(), false);
if (font != null) {
LOG.debug("Selected Font: {}", font);
settings.setFont(font);
mainWindow.loadSettings();
Expand All @@ -387,11 +384,9 @@ public void mouseClicked(MouseEvent e) {
smaliFontBtn.addMouseListener(new MouseAdapter() {
@Override
public void mouseClicked(MouseEvent e) {
JFontChooser fontChooser = new JPreferredFontChooser();
fontChooser.setSelectedFont(settings.getSmaliFont());
int result = fontChooser.showDialog(JadxSettingsWindow.this);
if (result == JFontChooser.OK_OPTION) {
Font font = fontChooser.getSelectedFont();
Font font = new JadxFontDialog(JadxSettingsWindow.this, NLS.str("preferences.smali_font"))
.select(settings.getSmaliFont(), true);
if (font != null) {
LOG.debug("Selected Font: {} for smali", font);
settings.setSmaliFont(font);
mainWindow.loadSettings();
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
package jadx.gui.settings.ui.font;

import java.lang.reflect.Field;

import javax.swing.JCheckBox;

import org.drjekyll.fontchooser.FontChooser;
import org.drjekyll.fontchooser.panes.FamilyPane;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class FontChooserHack {
private static final Logger LOG = LoggerFactory.getLogger(FontChooserHack.class);

public static void setOnlyMonospace(FontChooser fontChooser) {
try {
FamilyPane familyPane = (FamilyPane) getPrivateField(fontChooser, "familyPane");
JCheckBox monospacedCheckBox = (JCheckBox) getPrivateField(familyPane, "monospacedCheckBox");
monospacedCheckBox.setSelected(true);
monospacedCheckBox.setEnabled(false);
} catch (Throwable e) {
LOG.debug("Failed to set only monospace check box", e);
}
}

private static Object getPrivateField(Object obj, String fieldName) throws NoSuchFieldException, IllegalAccessException {
Field f = obj.getClass().getDeclaredField(fieldName);
f.setAccessible(true);
return f.get(obj);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
package jadx.gui.settings.ui.font;

import java.awt.BorderLayout;
import java.awt.FlowLayout;
import java.awt.Font;

import javax.swing.BorderFactory;
import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JPanel;
import javax.swing.WindowConstants;

import org.drjekyll.fontchooser.FontChooser;
import org.jetbrains.annotations.Nullable;

import jadx.gui.settings.JadxSettings;
import jadx.gui.settings.ui.JadxSettingsWindow;
import jadx.gui.utils.NLS;

public class JadxFontDialog extends JDialog {
private static final long serialVersionUID = 7609857698785777587L;

private final FontChooser fontChooser = new FontChooser();
private final JadxSettings settings;
private boolean selected = false;

public JadxFontDialog(JadxSettingsWindow settingsWindow, String title) {
super(settingsWindow, title, true);
settings = settingsWindow.getMainWindow().getSettings();
initComponents();
setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
if (!settings.loadWindowPos(this)) {
pack();
}
}

public @Nullable Font select(Font currentFont, boolean onlyMonospace) {
fontChooser.setSelectedFont(currentFont);
if (onlyMonospace) {
FontChooserHack.setOnlyMonospace(fontChooser);
}
setVisible(true);
Font selectedFont = fontChooser.getSelectedFont();
if (selected && !selectedFont.equals(currentFont)) {
return selectedFont;
}
return null;
}

private void initComponents() {
JPanel chooserPanel = new JPanel();
chooserPanel.setBorder(BorderFactory.createEmptyBorder(10, 10, 0, 10));
chooserPanel.setLayout(new BorderLayout(0, 10));
chooserPanel.add(fontChooser);

JPanel controlPanel = new JPanel();
controlPanel.setBorder(BorderFactory.createEmptyBorder(5, 5, 5, 5));
controlPanel.setLayout(new FlowLayout(FlowLayout.TRAILING));

JButton okBtn = new JButton();
okBtn.setText(NLS.str("common_dialog.ok"));
okBtn.setMnemonic('o');
okBtn.addActionListener(event -> {
selected = true;
dispose();
});

JButton cancelBtn = new JButton();
cancelBtn.setText(NLS.str("common_dialog.cancel"));
cancelBtn.setMnemonic('c');
cancelBtn.addActionListener(event -> dispose());

controlPanel.add(okBtn);
controlPanel.add(cancelBtn);

add(chooserPanel);
add(controlPanel, BorderLayout.PAGE_END);
getRootPane().setDefaultButton(okBtn);
}

@Override
public void dispose() {
settings.saveWindowPos(this);
super.dispose();
}
}

0 comments on commit e5be41b

Please sign in to comment.