Skip to content

Commit b6186f5

Browse files
Maximilian WittmerMaximilian Wittmer
authored andcommitted
SearchHistoryMenu: improve scrolling through selection with arrow keys
Scrolling through the selection of the Search History now correctly starts at the first item and cycles through the boundaries (ie. scrolling down from the last item returns correctly to the first item of the list) fixes #2139
1 parent 8df2017 commit b6186f5

File tree

1 file changed

+45
-14
lines changed
  • bundles/org.eclipse.ui.workbench.texteditor/src/org/eclipse/ui/internal/findandreplace/overlay

1 file changed

+45
-14
lines changed

bundles/org.eclipse.ui.workbench.texteditor/src/org/eclipse/ui/internal/findandreplace/overlay/SearchHistoryMenu.java

Lines changed: 45 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,7 @@ public void shellDeactivated(ShellEvent e) {
5454
private int width;
5555
private Table table;
5656
private TableColumn column;
57+
private int selectedIndexInTable = -1;
5758

5859
public SearchHistoryMenu(Shell parent, HistoryStore history, Consumer<String> historyEntrySelectedCallback) {
5960
super(parent);
@@ -92,28 +93,58 @@ public Control createContents(Composite parent) {
9293
return table;
9394
}
9495

96+
private void moveSelectionInTable(int indexShift) {
97+
selectedIndexInTable += indexShift;
98+
if (selectedIndexInTable < 0) {
99+
selectedIndexInTable = table.getItemCount() - 1;
100+
} else if (selectedIndexInTable > table.getItemCount() - 1) {
101+
selectedIndexInTable = 0;
102+
}
103+
table.setSelection(selectedIndexInTable);
104+
historyEntrySelectedCallback.accept(table.getSelection()[0].getText());
105+
}
106+
95107
private void attachTableListeners() {
96-
table.addSelectionListener(SelectionListener.widgetSelectedAdapter(e -> {
97-
TableItem[] selection = table.getSelection();
98-
if (selection.length == 0) {
99-
historyEntrySelectedCallback.accept(null);
100-
return;
108+
table.addListener(SWT.MouseMove, event -> {
109+
Point point = new Point(event.x, event.y);
110+
TableItem item = table.getItem(point);
111+
if (item != null) {
112+
table.setSelection(item);
113+
selectedIndexInTable = table.getSelectionIndex();
101114
}
102-
String text = selection[0].getText();
103-
if (text != null) {
104-
historyEntrySelectedCallback.accept(text);
115+
});
116+
table.addKeyListener(KeyListener.keyPressedAdapter(e -> {
117+
if (e.keyCode == SWT.ARROW_DOWN) {
118+
moveSelectionInTable(1);
119+
e.doit = false;
120+
} else if (e.keyCode == SWT.ARROW_UP) {
121+
moveSelectionInTable(-1);
122+
e.doit = false;
123+
} else if (e.keyCode == SWT.CR || e.keyCode == SWT.KEYPAD_CR) {
124+
notifyParentOfSelectionInput();
125+
close();
105126
}
106-
historyEntrySelectedCallback.accept(null);
127+
}));
128+
table.addSelectionListener(SelectionListener.widgetSelectedAdapter(e -> {
129+
notifyParentOfSelectionInput();
107130
}));
108131
table.addMouseListener(MouseListener.mouseDownAdapter(e -> {
109132
table.notifyListeners(SWT.Selection, null);
110133
close();
111134
}));
112-
table.addKeyListener(KeyListener.keyPressedAdapter(e -> {
113-
if (e.keyCode == SWT.CR || e.keyCode == SWT.KEYPAD_CR) {
114-
close();
115-
}
116-
}));
135+
}
136+
137+
private void notifyParentOfSelectionInput() {
138+
TableItem[] selection = table.getSelection();
139+
if (selection.length == 0) {
140+
historyEntrySelectedCallback.accept(null);
141+
return;
142+
}
143+
String text = selection[0].getText();
144+
if (text != null) {
145+
historyEntrySelectedCallback.accept(text);
146+
}
147+
historyEntrySelectedCallback.accept(null);
117148
}
118149

119150
private void positionShell() {

0 commit comments

Comments
 (0)