Skip to content

Commit c97ddb6

Browse files
committed
Wrap mouseover selection in a preference
Part of me really likes the mouseover selection, but I find it makes it difficult to navigate to the action buttons without accidentally changing the selected search result. And for comparison: Spotlight also does not do it. So let's disable it by default.
1 parent 7427562 commit c97ddb6

File tree

1 file changed

+29
-14
lines changed

1 file changed

+29
-14
lines changed

src/main/java/org/scijava/ui/swing/search/SwingSearchBar.java

Lines changed: 29 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -131,6 +131,9 @@ public class SwingSearchBar extends JTextField {
131131
/** The maximum number of results per search category. */
132132
private int resultLimit = 8;
133133

134+
/** Whether the selection should change upon mouseover. */
135+
private boolean mouseoverEnabled;
136+
134137
public SwingSearchBar(final Context context) {
135138
super(DEFAULT_MESSAGE, 12);
136139
context.inject(this);
@@ -207,6 +210,16 @@ public void setResultLimit(final int resultLimit) {
207210
this.resultLimit = resultLimit;
208211
}
209212

213+
/** Gets whether the selection should change upon mouseover. */
214+
public boolean isMouseoverEnabled() {
215+
return mouseoverEnabled;
216+
}
217+
218+
/** Sets whether the selection should change upon mouseover. */
219+
public void setMouseoverEnabled(final boolean mouseoverEnabled) {
220+
this.mouseoverEnabled = mouseoverEnabled;
221+
}
222+
210223
// -- Utility methods --
211224

212225
// TODO: Move this method to PluginService.
@@ -324,8 +337,6 @@ private class SwingSearchPanel extends JPanel {
324337
private final Map<Class<?>, JCheckBox> headerCheckboxes;
325338
private final JList<SearchResult> resultsList;
326339

327-
private SearchResult selected = null;
328-
329340
public SwingSearchPanel() {
330341
setLayout(new BorderLayout());
331342
setPreferredSize(new Dimension(800, 300));
@@ -422,21 +433,25 @@ public void mouseClicked(final MouseEvent e) {
422433
}
423434
});
424435

425-
resultsList.addMouseMotionListener(new MouseMotionAdapter() {
436+
if (mouseoverEnabled) {
437+
resultsList.addMouseMotionListener(new MouseMotionAdapter() {
426438

427-
@Override
428-
public void mouseMoved(final MouseEvent e) {
429-
final int index = resultsList.locationToIndex(e.getPoint());
430-
final SearchResult _selected = resultsList.getModel().getElementAt(
431-
index);
432-
if (selected != _selected) {
433-
selected = _selected;
434-
if (selected != null && !isHeader(selected)) {
435-
resultsList.setSelectedValue(selected, false);
439+
private SearchResult lastSelected;
440+
441+
@Override
442+
public void mouseMoved(final MouseEvent e) {
443+
final int index = resultsList.locationToIndex(e.getPoint());
444+
final SearchResult selected = //
445+
resultsList.getModel().getElementAt(index);
446+
if (lastSelected != selected) {
447+
lastSelected = selected;
448+
if (lastSelected != null && !isHeader(lastSelected)) {
449+
resultsList.setSelectedValue(lastSelected, false);
450+
}
436451
}
437452
}
438-
}
439-
});
453+
});
454+
}
440455

441456
resultsList.addListSelectionListener(lse -> {
442457
if (lse.getValueIsAdjusting()) return;

0 commit comments

Comments
 (0)