Skip to content

Commit cd802ef

Browse files
committed
Delete tag chips with a middle click and place the editor by clicking
Like a browser tab, a middle click anywhere on a chip deletes it. The "×" deletes on a left or middle click and ignores other buttons, which on X11 include horizontal scrolling. Both act on release, and only if the pointer is still over what was pressed; a click event is dropped if the pointer moves while the button is down, which a trackpoint nearly always does. A left click in a gap between chips, or past the last one, puts the editor in that gap, so tags can be inserted with the mouse. Clicks on a chip itself don't move the editor, and neither does any click while text is being edited or the field is disabled. Assisted-by: Claude:claude-opus-5-5 Signed-off-by: Tony Germano <tony@germano.name>
1 parent d8adfcc commit cd802ef

2 files changed

Lines changed: 39 additions & 5 deletions

File tree

‎client/src/main/java/com/mirth/connect/client/ui/components/tag/MirthTagField.java‎

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -254,9 +254,9 @@ public void mousePressed(MouseEvent evt) {
254254
return;
255255
}
256256

257-
// A click outside the chips goes back to adding tags at the end.
258-
if (!editing && !(evt.getComponent() instanceof TagChip)) {
259-
moveEditor(tokens.size());
257+
// A click between chips, or past the last one, puts the editor in the nearest gap.
258+
if (!editing && isEnabled() && !(evt.getComponent() instanceof TagChip)) {
259+
moveEditor(editorIndexAt(SwingUtilities.convertPoint(evt.getComponent(), evt.getPoint(), chipRow).x));
260260
}
261261
editor.requestFocusInWindow();
262262
}
@@ -640,6 +640,19 @@ public void run() {
640640
});
641641
}
642642

643+
/** The editor position nearest to x in the row: after every chip whose middle is left of x. */
644+
private int editorIndexAt(int x) {
645+
int index = 0;
646+
647+
for (Component component : chipRow.getComponents()) {
648+
if (component instanceof TagChip && component.getX() + component.getWidth() / 2 < x) {
649+
index++;
650+
}
651+
}
652+
653+
return index;
654+
}
655+
643656
private void moveEditor(int index) {
644657
index = Math.max(0, Math.min(index, tokens.size()));
645658

‎client/src/main/java/com/mirth/connect/client/ui/components/tag/TagChip.java‎

Lines changed: 23 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,11 +16,15 @@
1616

1717
import javax.swing.JLabel;
1818
import javax.swing.JPanel;
19+
import javax.swing.SwingUtilities;
1920

2021
import net.miginfocom.swing.MigLayout;
2122

2223
/**
2324
* A single rounded tag "chip" with a delete affordance. Used by {@link MirthTagField}.
25+
* <p>
26+
* Like a browser tab, a chip is deleted by a left click on its "×" or a middle click anywhere on
27+
* it. Either acts on release, and only if the pointer is still over what was pressed.
2428
*/
2529
public class TagChip extends JPanel {
2630
/** Also used by {@link MirthTagField}'s editor, so typed text looks like the chips. */
@@ -46,14 +50,31 @@ public TagChip(String name, Color background, Color foreground, final Runnable o
4650
nameLabel.setForeground(foreground);
4751
add(nameLabel);
4852

53+
// The name label has no listeners of its own, so presses on it land here too.
54+
addMouseListener(new MouseAdapter() {
55+
@Override
56+
public void mouseReleased(MouseEvent e) {
57+
if (isEnabled() && SwingUtilities.isMiddleMouseButton(e) && contains(e.getPoint())) {
58+
onDelete.run();
59+
}
60+
}
61+
});
62+
4963
closeLabel = new JLabel("×");
5064
closeLabel.setFont(CLOSE_FONT);
5165
closeLabel.setForeground(foreground);
5266
closeLabel.setCursor(Cursor.getPredefinedCursor(Cursor.HAND_CURSOR));
5367
closeLabel.addMouseListener(new MouseAdapter() {
68+
/*
69+
* Released rather than clicked: a click is dropped if the pointer moves at all while the
70+
* button is down, which a trackpoint nearly always does. Other buttons are ignored; they
71+
* include horizontal scrolling on X11, which arrives as buttons 4 and 5.
72+
*/
5473
@Override
55-
public void mouseClicked(MouseEvent e) {
56-
if (isEnabled()) {
74+
public void mouseReleased(MouseEvent e) {
75+
boolean deleteButton = SwingUtilities.isLeftMouseButton(e) || SwingUtilities.isMiddleMouseButton(e);
76+
77+
if (isEnabled() && deleteButton && closeLabel.contains(e.getPoint())) {
5778
onDelete.run();
5879
}
5980
}

0 commit comments

Comments
 (0)