From dea7b79dd7366084f864ffa56bab39ffde30f228 Mon Sep 17 00:00:00 2001 From: Maximilian Wittmer Date: Mon, 2 Sep 2024 13:14:51 +0200 Subject: [PATCH] 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 --- .../overlay/SearchHistoryMenu.java | 59 ++++++++++++++----- 1 file changed, 45 insertions(+), 14 deletions(-) diff --git a/bundles/org.eclipse.ui.workbench.texteditor/src/org/eclipse/ui/internal/findandreplace/overlay/SearchHistoryMenu.java b/bundles/org.eclipse.ui.workbench.texteditor/src/org/eclipse/ui/internal/findandreplace/overlay/SearchHistoryMenu.java index 4360d900f91..4c7d127ae52 100644 --- a/bundles/org.eclipse.ui.workbench.texteditor/src/org/eclipse/ui/internal/findandreplace/overlay/SearchHistoryMenu.java +++ b/bundles/org.eclipse.ui.workbench.texteditor/src/org/eclipse/ui/internal/findandreplace/overlay/SearchHistoryMenu.java @@ -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 historyEntrySelectedCallback) { super(parent); @@ -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() {