Skip to content

Commit 8fe952f

Browse files
supersaiyansubtletyix0rai
authored andcommitted
remove JEditorPane ui key strokes that conflict with EditorPopupMenu KeyBinds, fixes #323 (#324)
make conflict removal more agressive
1 parent 01c9598 commit 8fe952f

File tree

4 files changed

+29
-8
lines changed

4 files changed

+29
-8
lines changed

enigma-swing/src/main/java/org/quiltmc/enigma/gui/config/keybind/KeyBind.java

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@
1515
import javax.swing.JComponent;
1616
import javax.swing.KeyStroke;
1717

18+
import static javax.swing.SwingUtilities.getUIInputMap;
19+
1820
public record KeyBind(String name, String category, List<Combination> combinations) {
1921
public record Combination(int keyCode, int keyModifiers) {
2022
public static final Combination EMPTY = new Combination(-1, 0);
@@ -23,6 +25,10 @@ public boolean matches(KeyEvent e) {
2325
return e.getKeyCode() == this.keyCode && e.getModifiersEx() == this.keyModifiers;
2426
}
2527

28+
public KeyStroke toKeyStroke() {
29+
return this.toKeyStroke(0);
30+
}
31+
2632
public KeyStroke toKeyStroke(int modifiers) {
2733
modifiers = this.keyModifiers | modifiers;
2834
return KeyStroke.getKeyStroke(this.keyCode, modifiers);
@@ -74,6 +80,19 @@ public boolean isEmpty() {
7480
return this.combinations.isEmpty();
7581
}
7682

83+
public void removeUiConflicts(JComponent component) {
84+
final List<KeyStroke> keyStrokes = this.combinations.stream().map(Combination::toKeyStroke).toList();
85+
86+
for (final GuiUtil.FocusCondition condition : GuiUtil.FocusCondition.VALUES) {
87+
InputMap uiInputMap = getUIInputMap(component, condition.getValue());
88+
while (uiInputMap != null) {
89+
keyStrokes.forEach(uiInputMap::remove);
90+
91+
uiInputMap = uiInputMap.getParent();
92+
}
93+
}
94+
}
95+
7796
public String[] serializeCombinations() {
7897
return this.combinations.stream().map(Combination::serialize).toArray(String[]::new);
7998
}

enigma-swing/src/main/java/org/quiltmc/enigma/gui/element/EditorTabbedPane.java

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -16,14 +16,11 @@
1616
import java.awt.event.MouseEvent;
1717
import java.util.Iterator;
1818
import javax.annotation.Nullable;
19-
import javax.swing.InputMap;
20-
import javax.swing.JComponent;
2119
import javax.swing.JTabbedPane;
2220
import javax.swing.SwingConstants;
2321
import javax.swing.SwingUtilities;
2422

2523
import static org.quiltmc.enigma.gui.util.GuiUtil.putKeyBindAction;
26-
import static javax.swing.SwingUtilities.getUIInputMap;
2724

2825
public class EditorTabbedPane {
2926
private final JTabbedPane openFiles = new JTabbedPane(SwingConstants.TOP, JTabbedPane.SCROLL_TAB_LAYOUT);
@@ -39,11 +36,7 @@ public EditorTabbedPane(Gui gui) {
3936
this.navigator = new NavigatorPanel(this.gui);
4037

4138
this.openFiles.addMouseListener(GuiUtil.onMousePress(this::onTabPressed));
42-
final InputMap openFilesInputMap = getUIInputMap(this.openFiles, JComponent.WHEN_ANCESTOR_OF_FOCUSED_COMPONENT);
43-
if (openFilesInputMap != null) {
44-
// remove default JTabbedPane binding that conflicts with KeyBind for editors
45-
openFilesInputMap.remove(KeyBinds.ENTRY_NAVIGATOR_LAST.toKeyStroke());
46-
}
39+
KeyBinds.ENTRY_NAVIGATOR_LAST.removeUiConflicts(this.openFiles);
4740
}
4841

4942
public EditorPanel openClass(ClassEntry entry) {

enigma-swing/src/main/java/org/quiltmc/enigma/gui/panel/EditorPanel.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -139,6 +139,8 @@ public void keyTyped(KeyEvent event) {
139139
}
140140
});
141141

142+
this.popupMenu.getButtonKeyBinds().keySet().forEach(keyBind -> keyBind.removeUiConflicts(this.editor));
143+
142144
this.reloadKeyBinds();
143145
this.addSourceSetListener(source -> {
144146
if (this.navigatorPanel != null) {

enigma-swing/src/main/java/org/quiltmc/enigma/gui/util/GuiUtil.java

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package org.quiltmc.enigma.gui.util;
22

33
import com.formdev.flatlaf.extras.FlatSVGIcon;
4+
import com.google.common.collect.ImmutableList;
45
import org.quiltmc.config.api.values.TrackedValue;
56
import org.quiltmc.enigma.api.analysis.index.jar.EntryIndex;
67
import org.quiltmc.enigma.api.service.JarIndexerService;
@@ -485,10 +486,16 @@ public enum FocusCondition {
485486
*/
486487
WHEN_ANCESTOR_OF_FOCUSED_COMPONENT(JComponent.WHEN_ANCESTOR_OF_FOCUSED_COMPONENT);
487488

489+
public static final ImmutableList<FocusCondition> VALUES = ImmutableList.copyOf(values());
490+
488491
private final int value;
489492

490493
FocusCondition(int value) {
491494
this.value = value;
492495
}
496+
497+
public int getValue() {
498+
return this.value;
499+
}
493500
}
494501
}

0 commit comments

Comments
 (0)