Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@
import java.util.Comparator;
import java.util.List;

import static org.quiltmc.enigma.gui.util.GuiUtil.putKeyBindAction;

public class ClassSelector extends JTree {
public static final Comparator<ClassEntry> DEOBF_CLASS_COMPARATOR = Comparator.comparing(ClassEntry::getFullName);

Expand Down Expand Up @@ -49,27 +51,29 @@ public ClassSelector(Gui gui, Comparator<ClassEntry> comparator) {
}
}));

this.addKeyListener(GuiUtil.onKeyPress(e -> {
TreePath[] paths = this.getSelectionPaths();

putKeyBindAction(KeyBinds.EDITOR_TOGGLE_MAPPING, this, e -> {
final TreePath[] paths = this.getSelectionPaths();
if (paths != null) {
if (KeyBinds.EDITOR_TOGGLE_MAPPING.matches(e)) {
for (TreePath path : paths) {
if (path.getLastPathComponent() instanceof ClassSelectorClassNode node) {
gui.toggleMappingFromEntry(node.getObfEntry());
}
for (final TreePath path : paths) {
if (path.getLastPathComponent() instanceof ClassSelectorClassNode node) {
gui.toggleMappingFromEntry(node.getObfEntry());
}
}
}
});

if (this.selectionListener != null && KeyBinds.SELECT.matches(e)) {
for (TreePath path : paths) {
putKeyBindAction(KeyBinds.SELECT, this, e -> {
if (this.selectionListener != null) {
final TreePath[] paths = this.getSelectionPaths();
if (paths != null) {
for (final TreePath path : paths) {
if (path.getLastPathComponent() instanceof ClassSelectorClassNode node) {
this.selectionListener.onSelectClass(node.getObfEntry());
}
}
}
}
}));
});

this.setCellRenderer(new ClassTreeCellRenderer(gui, this));
ToolTipManager.sharedInstance().registerComponent(this);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,18 @@
package org.quiltmc.enigma.gui.config.keybind;

import org.quiltmc.enigma.gui.util.GuiUtil;
import org.quiltmc.enigma.util.I18n;
import org.tinylog.Logger;

import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.util.ArrayList;
import java.util.List;
import java.util.Locale;
import javax.swing.ActionMap;
import javax.swing.InputMap;
import javax.swing.JComponent;
import javax.swing.KeyStroke;

public record KeyBind(String name, String category, List<Combination> combinations) {
Expand Down Expand Up @@ -42,6 +48,12 @@ public void setFrom(KeyBind other) {
this.combinations.addAll(other.combinations);
}

/**
* Prefer using component's {@link InputMap}s and {@link ActionMap}s to checking this in {@link KeyListener}s.
*
* @see GuiUtil#putKeyBindAction(KeyBind, JComponent, GuiUtil.FocusCondition, ActionListener)
*/
@Deprecated
public boolean matches(KeyEvent e) {
return this.combinations.stream().anyMatch(c -> c.matches(e));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ public final class KeyBinds {
public static final KeyBind EXIT = KeyBind.builder("close").key(KeyEvent.VK_ESCAPE).build();
public static final KeyBind SELECT = KeyBind.builder("select").key(KeyEvent.VK_ENTER).build();
public static final KeyBind DIALOG_SAVE = KeyBind.builder("dialog_save").key(KeyEvent.VK_ENTER).build();
public static final KeyBind MULTILINE_DIALOG_SAVE = KeyBind.builder("multiline_dialog_save").mod(InputEvent.CTRL_DOWN_MASK).key(KeyEvent.VK_ENTER).build();

public static final KeyBind QUICK_FIND_DIALOG_IGNORE_CASE = KeyBind.builder("ignore_case", QUICK_FIND_DIALOG_CATEGORY).key(KeyEvent.VK_C).build();
public static final KeyBind QUICK_FIND_DIALOG_REGEX = KeyBind.builder("regex", QUICK_FIND_DIALOG_CATEGORY).key(KeyEvent.VK_R).build();
Expand All @@ -35,7 +36,7 @@ public final class KeyBinds {
public static final KeyBind EDITOR_SEARCH_STRUCTURE = KeyBind.builder("search_structure", EDITOR_CATEGORY).mod(InputEvent.CTRL_DOWN_MASK | InputEvent.SHIFT_DOWN_MASK).key(KeyEvent.VK_T).build();
public static final KeyBind EDITOR_SHOW_INHERITANCE = KeyBind.builder("show_inheritance", EDITOR_CATEGORY).mod(InputEvent.CTRL_DOWN_MASK).key(KeyEvent.VK_I).build();
public static final KeyBind EDITOR_SHOW_IMPLEMENTATIONS = KeyBind.builder("show_implementations", EDITOR_CATEGORY).mod(InputEvent.CTRL_DOWN_MASK).key(KeyEvent.VK_M).build();
public static final KeyBind EDITOR_SHOW_CALLS = KeyBind.builder("show_calls", EDITOR_CATEGORY).mod(InputEvent.CTRL_DOWN_MASK).key(KeyEvent.VK_C).build();
public static final KeyBind EDITOR_SHOW_CALLS = KeyBind.builder("show_calls", EDITOR_CATEGORY).mod(InputEvent.CTRL_DOWN_MASK | InputEvent.ALT_DOWN_MASK).key(KeyEvent.VK_C).build();
public static final KeyBind EDITOR_SHOW_CALLS_SPECIFIC = KeyBind.builder("show_calls_specific", EDITOR_CATEGORY).mod(InputEvent.CTRL_DOWN_MASK | InputEvent.SHIFT_DOWN_MASK).key(KeyEvent.VK_C).build();
public static final KeyBind EDITOR_OPEN_ENTRY = KeyBind.builder("open_entry", EDITOR_CATEGORY).mod(InputEvent.CTRL_DOWN_MASK).key(KeyEvent.VK_N).build();
public static final KeyBind EDITOR_OPEN_PREVIOUS = KeyBind.builder("open_previous", EDITOR_CATEGORY).mod(InputEvent.CTRL_DOWN_MASK).key(KeyEvent.VK_P).build();
Expand All @@ -61,10 +62,10 @@ public final class KeyBinds {
public static final KeyBind SEARCH_METHOD = KeyBind.builder("search_method", MENU_CATEGORY).build();
public static final KeyBind SEARCH_FIELD = KeyBind.builder("search_field", MENU_CATEGORY).build();

private static final List<KeyBind> DEFAULT_KEY_BINDS = Stream.of(EXIT, SELECT, DIALOG_SAVE, QUICK_FIND_DIALOG_NEXT,
QUICK_FIND_DIALOG_PREVIOUS, SEARCH_DIALOG_NEXT, SEARCH_DIALOG_PREVIOUS, EDITOR_RENAME, EDITOR_PASTE,
EDITOR_EDIT_JAVADOC, EDITOR_SHOW_INHERITANCE, EDITOR_SHOW_IMPLEMENTATIONS, EDITOR_SHOW_CALLS,
EDITOR_SHOW_CALLS_SPECIFIC, EDITOR_OPEN_ENTRY, EDITOR_OPEN_PREVIOUS, EDITOR_OPEN_NEXT,
private static final List<KeyBind> DEFAULT_KEY_BINDS = Stream.of(EXIT, SELECT, DIALOG_SAVE, MULTILINE_DIALOG_SAVE,
QUICK_FIND_DIALOG_NEXT, QUICK_FIND_DIALOG_PREVIOUS, SEARCH_DIALOG_NEXT, SEARCH_DIALOG_PREVIOUS,
EDITOR_RENAME, EDITOR_PASTE, EDITOR_EDIT_JAVADOC, EDITOR_SHOW_INHERITANCE, EDITOR_SHOW_IMPLEMENTATIONS,
EDITOR_SHOW_CALLS, EDITOR_SHOW_CALLS_SPECIFIC, EDITOR_OPEN_ENTRY, EDITOR_OPEN_PREVIOUS, EDITOR_OPEN_NEXT,
EDITOR_TOGGLE_MAPPING, EDITOR_ZOOM_IN, EDITOR_ZOOM_OUT, EDITOR_CLOSE_TAB, EDITOR_RELOAD_CLASS,
EDITOR_QUICK_FIND, EDITOR_SHOW_STRUCTURE, EDITOR_SEARCH_STRUCTURE, SAVE_MAPPINGS, DROP_MAPPINGS,
RELOAD_MAPPINGS, RELOAD_ALL, MAPPING_STATS, SEARCH, SEARCH_ALL, SEARCH_CLASS, SEARCH_METHOD, SEARCH_FIELD).map(KeyBind::toImmutable).toList();
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package org.quiltmc.enigma.gui.dialog;

import org.quiltmc.enigma.gui.config.keybind.KeyBinds;
import org.quiltmc.enigma.gui.util.GuiUtil;
import org.quiltmc.enigma.util.I18n;

import java.awt.BorderLayout;
Expand All @@ -12,6 +11,8 @@
import javax.swing.JLabel;
import javax.swing.JPanel;

import static org.quiltmc.enigma.gui.util.GuiUtil.putKeyBindAction;

public class ChangeDialog {
public static void show(Window parent) {
// init frame
Expand All @@ -31,11 +32,7 @@ public static void show(Window parent) {
JButton okButton = new JButton(I18n.translate("prompt.ok"));
buttonPanel.add(okButton);
okButton.addActionListener(event -> frame.dispose());
okButton.addKeyListener(GuiUtil.onKeyPress(e -> {
if (KeyBinds.EXIT.matches(e)) {
frame.dispose();
}
}));
putKeyBindAction(KeyBinds.EXIT, okButton, e -> frame.dispose());

// show the frame
frame.pack();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,8 @@
import org.quiltmc.syntaxpain.QuickFindDialog;

import javax.swing.text.JTextComponent;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;

import static org.quiltmc.enigma.gui.util.GuiUtil.putKeyBindAction;

/**
* Extension of {@link QuickFindDialog} to allow using keybindings, and improve UI.
Expand All @@ -35,16 +35,8 @@ protected void initComponents() {
super.initComponents();

// keybinding support
this.searchField.addKeyListener(new KeyAdapter() {
@Override
public void keyPressed(KeyEvent e) {
if (KeyBinds.QUICK_FIND_DIALOG_PREVIOUS.matches(e)) {
EnigmaQuickFindDialog.this.prevButton.doClick();
} else if (KeyBinds.QUICK_FIND_DIALOG_NEXT.matches(e)) {
EnigmaQuickFindDialog.this.nextButton.doClick();
}
}
});
putKeyBindAction(KeyBinds.QUICK_FIND_DIALOG_PREVIOUS, this.searchField, e -> this.prevButton.doClick());
putKeyBindAction(KeyBinds.QUICK_FIND_DIALOG_NEXT, this.searchField, e -> this.nextButton.doClick());

this.ignoreCaseCheckBox.setMnemonic(KeyBinds.QUICK_FIND_DIALOG_IGNORE_CASE.getKeyCode());
this.regexCheckBox.setMnemonic(KeyBinds.QUICK_FIND_DIALOG_REGEX.getKeyCode());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@
import javax.swing.WindowConstants;
import javax.swing.text.html.HTML;

import static org.quiltmc.enigma.gui.util.GuiUtil.putKeyBindAction;

public class JavadocDialog {
private final JDialog ui;
private final GuiController controller;
Expand All @@ -53,18 +55,16 @@ private JavadocDialog(JFrame parent, GuiController controller, Entry<?> entry, S
this.text.setText(preset);
this.text.setTabSize(2);
contentPane.add(new JScrollPane(this.text), BorderLayout.CENTER);
this.text.addKeyListener(GuiUtil.onKeyPress(event -> {
if (KeyBinds.DIALOG_SAVE.matches(event)) {
if (event.isControlDown()) {
this.doSave();
if (this.vc.canProceed()) {
this.close();
}
}
} else if (KeyBinds.EXIT.matches(event)) {

putKeyBindAction(KeyBinds.MULTILINE_DIALOG_SAVE, this.text, e -> {
this.doSave();
if (this.vc.canProceed()) {
this.close();
}
}));
});

putKeyBindAction(KeyBinds.EXIT, this.text, e -> this.close());

this.text.setFont(ScaleUtil.scaleFont(Config.currentFonts().editor.value()));

// buttons panel
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@
import java.awt.Font;
import java.awt.event.ComponentAdapter;
import java.awt.event.ComponentEvent;
import java.awt.event.KeyEvent;
import java.awt.event.MouseListener;
import java.util.ArrayList;
import java.util.Arrays;
Expand All @@ -48,6 +47,8 @@
import javax.swing.event.DocumentEvent;
import javax.swing.event.DocumentListener;

import static org.quiltmc.enigma.gui.util.GuiUtil.putKeyBindAction;

public class SearchDialog {
private final JTextField searchField;
private final JCheckBox onlyExactMatchesCheckbox;
Expand Down Expand Up @@ -90,7 +91,6 @@ public void changedUpdate(DocumentEvent e) {
SearchDialog.this.updateList();
}
});
this.searchField.addKeyListener(GuiUtil.onKeyPress(this::onKeyPressed));
this.searchField.addActionListener(e -> this.openSelected());

this.onlyExactMatchesCheckbox = new JCheckBox(I18n.translate("menu.search.only_exact_matches"));
Expand Down Expand Up @@ -156,6 +156,22 @@ public void componentShown(ComponentEvent e) {
this.dialog.setContentPane(contentPane);
this.dialog.setSize(ScaleUtil.getDimension(400, 500));
this.dialog.setLocationRelativeTo(gui.getFrame());

putKeyBindAction(KeyBinds.SEARCH_DIALOG_NEXT, contentPane, e -> {
final int next = this.classList.isSelectionEmpty() ? 0 : this.classList.getSelectedIndex() + 1;
this.classList.setSelectedIndex(next);
this.classList.ensureIndexIsVisible(next);
});

putKeyBindAction(KeyBinds.SEARCH_DIALOG_PREVIOUS, contentPane, e -> {
final int prev = this.classList.isSelectionEmpty()
? this.classList.getModel().getSize()
: this.classList.getSelectedIndex() - 1;
this.classList.setSelectedIndex(prev);
this.classList.ensureIndexIsVisible(prev);
});

putKeyBindAction(KeyBinds.EXIT, contentPane, e -> this.close());
}

private MouseListener createCheckboxListener(Type type) {
Expand Down Expand Up @@ -313,20 +329,6 @@ public void dispose() {
this.dialog.dispose();
}

private void onKeyPressed(KeyEvent e) {
if (KeyBinds.SEARCH_DIALOG_NEXT.matches(e)) {
int next = this.classList.isSelectionEmpty() ? 0 : this.classList.getSelectedIndex() + 1;
this.classList.setSelectedIndex(next);
this.classList.ensureIndexIsVisible(next);
} else if (KeyBinds.SEARCH_DIALOG_PREVIOUS.matches(e)) {
int prev = this.classList.isSelectionEmpty() ? this.classList.getModel().getSize() : this.classList.getSelectedIndex() - 1;
this.classList.setSelectedIndex(prev);
this.classList.ensureIndexIsVisible(prev);
} else if (KeyBinds.EXIT.matches(e)) {
this.close();
}
}

private record SearchEntryImpl(ParentedEntry<?> obf, ParentedEntry<?> deobf) implements SearchEntry {
@Override
public List<String> getSearchableNames() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -170,7 +170,7 @@ private void reset() {
}

private boolean isNewCombination(CombinationPanel panel) {
return panel.getOriginalCombination() != KeyBind.Combination.EMPTY;
return panel.getOriginalCombination() == KeyBind.Combination.EMPTY;
}

// Stop editing all combination panels but the excluded one
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,8 @@
import javax.swing.tree.TreeNode;
import javax.swing.tree.TreePath;

import static org.quiltmc.enigma.gui.util.GuiUtil.putKeyBindAction;

public class StructureDocker extends Docker {
private final JPanel optionsPanel;

Expand Down Expand Up @@ -91,7 +93,8 @@ public void keyReleased(KeyEvent e) {
this.structureTree.setCellRenderer(new StructureTreeCellRenderer(gui));
this.structureTree.setSelectionModel(new SingleTreeSelectionModel());
this.structureTree.setShowsRootHandles(true);
this.structureTree.addKeyListener(GuiUtil.onKeyPress(this::onKeyPress));
putKeyBindAction(KeyBinds.SELECT, this.structureTree, e -> this.navigateToSelectedNode());

this.structureTree.addMouseListener(GuiUtil.onMouseClick(this::onClick));

this.retranslateUi();
Expand Down Expand Up @@ -135,12 +138,6 @@ public void focusTree() {
this.structureTree.requestFocus();
}

private void onKeyPress(KeyEvent e) {
if (KeyBinds.SELECT.matches(e)) {
this.navigateToSelectedNode();
}
}

private void onClick(MouseEvent event) {
if (event.getClickCount() >= 2 && event.getButton() == MouseEvent.BUTTON1) {
this.navigateToSelectedNode();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@
import javax.swing.JTextField;
import javax.swing.text.Document;

import static org.quiltmc.enigma.gui.util.GuiUtil.putKeyBindAction;

/**
* A label that converts into an editable text field when you click it.
*/
Expand Down Expand Up @@ -47,13 +49,8 @@ public void focusLost(FocusEvent e) {
}
});

this.textField.addKeyListener(GuiUtil.onKeyPress(e -> {
if (KeyBinds.EXIT.matches(e)) {
this.stopEditing(true);
} else if (KeyBinds.DIALOG_SAVE.matches(e)) {
this.stopEditing(false);
}
}));
putKeyBindAction(KeyBinds.EXIT, this.textField, e -> this.stopEditing(true));
putKeyBindAction(KeyBinds.DIALOG_SAVE, this.textField, e -> this.stopEditing(false));

this.ui.add(this.label);
}
Expand Down
Loading