Skip to content

Commit

Permalink
Find/replace overlay: improve focus/tab order eclipse-platform#2161
Browse files Browse the repository at this point in the history
The FindReplaceOverlay currently uses the default focus order. When
tabbing through the widgets, this means that when starting from the find
input field you have to tab over all the search options before coming to
the replace input field. Since the most often used navigation is between
the two input fields for find and replace, they should come after each
other in the focus order.

This change adapts the focus order of the involved elements:
- Tab between find and replace input field
- Tab between replace input field and the tools (starting from the
search tools)
- Tab between search/close and replace search tools (instead of coming
from search/close tools to the replace input field)

Fixes eclipse-platform#2161
  • Loading branch information
HeikoKlare committed Nov 12, 2024
1 parent 69f6e45 commit 1ecc87f
Show file tree
Hide file tree
Showing 3 changed files with 88 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -74,4 +74,9 @@ void registerActionShortcutsAtControl(Control control) {
}
}

Control getFirstControl() {
Control[] children = getChildren();
return children.length == 0 ? null : getChildren()[0];
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Control;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Listener;
import org.eclipse.swt.widgets.ScrollBar;
import org.eclipse.swt.widgets.Scrollable;
import org.eclipse.swt.widgets.Shell;
Expand Down Expand Up @@ -218,6 +219,81 @@ public void reactivate() {

};

private final CustomFocusOrder customFocusOrder = new CustomFocusOrder();

private class CustomFocusOrder {
private final Listener searchBarToReplaceBar = e -> {
if (e.detail == SWT.TRAVERSE_TAB_NEXT) {
e.doit = false;
replaceBar.forceFocus();
}
};

private final Listener replaceBarToSearchBarAndTools = e -> {
switch (e.detail) {
case SWT.TRAVERSE_TAB_NEXT:
e.doit = false;
searchBar.getDropDownTool().getFirstControl().forceFocus();
break;
case SWT.TRAVERSE_TAB_PREVIOUS:
e.doit = false;
searchBar.getTextBar().forceFocus();
break;
default:
// Proceed as normal
}
};

private final Listener searchToolsToReplaceBar = e -> {
switch (e.detail) {
case SWT.TRAVERSE_TAB_PREVIOUS:
e.doit = false;
replaceBar.forceFocus();
break;
default:
// Proceed as normal
}
};

private final Listener closeToolsToReplaceTools = e -> {
switch (e.detail) {
case SWT.TRAVERSE_TAB_NEXT:
e.doit = false;
replaceBar.getDropDownTool().getFirstControl().forceFocus();
break;
default:
// Proceed as normal
}
};

private final Listener replaceToolsToCloseTools = e -> {
switch (e.detail) {
case SWT.TRAVERSE_TAB_PREVIOUS:
e.doit = false;
closeTools.getFirstControl().forceFocus();
break;
default:
// Proceed as normal
}
};

void apply() {
searchBar.getTextBar().addListener(SWT.Traverse, searchBarToReplaceBar);
replaceBar.getTextBar().addListener(SWT.Traverse, replaceBarToSearchBarAndTools);
searchBar.getDropDownTool().getFirstControl().addListener(SWT.Traverse, searchToolsToReplaceBar);
closeTools.getFirstControl().addListener(SWT.Traverse, closeToolsToReplaceTools);
replaceBar.getDropDownTool().getFirstControl().addListener(SWT.Traverse, replaceToolsToCloseTools);
}

void dispose() {
searchBar.getTextBar().removeListener(SWT.Traverse, searchBarToReplaceBar);
replaceBar.getTextBar().removeListener(SWT.Traverse, replaceBarToSearchBarAndTools);
searchBar.getDropDownTool().getFirstControl().removeListener(SWT.Traverse, searchToolsToReplaceBar);
closeTools.getFirstControl().removeListener(SWT.Traverse, closeToolsToReplaceTools);
replaceBar.getDropDownTool().getFirstControl().removeListener(SWT.Traverse, replaceToolsToCloseTools);
}
}

public FindReplaceOverlay(Shell parent, IWorkbenchPart part, IFindReplaceTarget target) {
targetPart = part;
targetControl = getTargetControl(parent, part);
Expand Down Expand Up @@ -647,6 +723,7 @@ public void focusLost(FocusEvent e) {
searchBar.addModifyListener(Event -> {
decorate();
});
searchBar.setTabList(null);
}

private void updateIncrementalSearch() {
Expand Down Expand Up @@ -710,6 +787,7 @@ private void hideReplace() {
if (!replaceBarOpen) {
return;
}
customFocusOrder.dispose();
searchBar.forceFocus();
contentAssistReplaceField = null;
replaceBarOpen = false;
Expand All @@ -728,6 +806,7 @@ private void createReplaceDialog() {
updatePlacementAndVisibility();
assignIDs();
replaceBar.forceFocus();
customFocusOrder.apply();
}

private void initializeReplaceShortcutHandlers() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -207,4 +207,8 @@ public Text getTextBar() {
return textBar;
}

AccessibleToolBar getDropDownTool() {
return tools;
}

}

0 comments on commit 1ecc87f

Please sign in to comment.