Skip to content

Commit

Permalink
SearchHistoryMenu: improve scrolling through selection with arrow keys
Browse files Browse the repository at this point in the history
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 eclipse-platform#2139
  • Loading branch information
Maximilian Wittmer authored and HeikoKlare committed Oct 11, 2024
1 parent 6d7fa39 commit dea7b79
Showing 1 changed file with 45 additions and 14 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ public void shellDeactivated(ShellEvent e) {
private int width;
private Table table;
private TableColumn column;
private int selectedIndexInTable = -1;

public SearchHistoryMenu(Shell parent, HistoryStore history, Consumer<String> historyEntrySelectedCallback) {
super(parent);
Expand Down Expand Up @@ -92,28 +93,58 @@ public Control createContents(Composite parent) {
return table;
}

private void moveSelectionInTable(int indexShift) {
selectedIndexInTable += indexShift;
if (selectedIndexInTable < 0) {
selectedIndexInTable = table.getItemCount() - 1;
} else if (selectedIndexInTable > table.getItemCount() - 1) {
selectedIndexInTable = 0;
}
table.setSelection(selectedIndexInTable);
historyEntrySelectedCallback.accept(table.getSelection()[0].getText());
}

private void attachTableListeners() {
table.addSelectionListener(SelectionListener.widgetSelectedAdapter(e -> {
TableItem[] selection = table.getSelection();
if (selection.length == 0) {
historyEntrySelectedCallback.accept(null);
return;
table.addListener(SWT.MouseMove, event -> {
Point point = new Point(event.x, event.y);
TableItem item = table.getItem(point);
if (item != null) {
table.setSelection(item);
selectedIndexInTable = table.getSelectionIndex();
}
String text = selection[0].getText();
if (text != null) {
historyEntrySelectedCallback.accept(text);
});
table.addKeyListener(KeyListener.keyPressedAdapter(e -> {
if (e.keyCode == SWT.ARROW_DOWN) {
moveSelectionInTable(1);
e.doit = false;
} else if (e.keyCode == SWT.ARROW_UP) {
moveSelectionInTable(-1);
e.doit = false;
} else if (e.keyCode == SWT.CR || e.keyCode == SWT.KEYPAD_CR) {
notifyParentOfSelectionInput();
close();
}
historyEntrySelectedCallback.accept(null);
}));
table.addSelectionListener(SelectionListener.widgetSelectedAdapter(e -> {
notifyParentOfSelectionInput();
}));
table.addMouseListener(MouseListener.mouseDownAdapter(e -> {
table.notifyListeners(SWT.Selection, null);
close();
}));
table.addKeyListener(KeyListener.keyPressedAdapter(e -> {
if (e.keyCode == SWT.CR || e.keyCode == SWT.KEYPAD_CR) {
close();
}
}));
}

private void notifyParentOfSelectionInput() {
TableItem[] selection = table.getSelection();
if (selection.length == 0) {
historyEntrySelectedCallback.accept(null);
return;
}
String text = selection[0].getText();
if (text != null) {
historyEntrySelectedCallback.accept(text);
}
historyEntrySelectedCallback.accept(null);
}

private void positionShell() {
Expand Down

0 comments on commit dea7b79

Please sign in to comment.