Repository navigation
Add arrow-key navigation to toolbars - #19676
Conversation
Toolbars follow the WAI-ARIA toolbar pattern: Left, Right, Home and End move focus among a toolbar's items without wrapping, and Tab stops once per run of items, on the one focused last. A text field such as Go to File/Function keeps those keys for editing, so it keeps its own Tab stop and splits the items around it into separate runs. Disabled and hidden items are skipped. Focus Main Toolbar now goes to the toolbar's first Tab stop instead of always to the New File button. Addresses #5960
While a Find in Files search runs, the toggle was marked unavailable only with aria-disabled on its table, so its two buttons stayed focusable and the toolbar's arrow keys still stopped on them. LeftRightToggleButton now has setEnabled(), which disables both buttons, so arrow keys skip them like other disabled toolbar buttons and the disabled state is exposed on the buttons themselves. Addresses #5960
The clear button that a search box such as Go to File/Function shows after typing is part of the same widget as the box, but it was grouped with the buttons after the box, so Tab from the box went to that group's remembered button and skipped the clear button. Toolbar now marks each widget it holds, and the controls in a widget that contains a text field each keep their own Tab stop, as the field does. Addresses #5960
Disconnecting the toolbar's mutation observer discarded changes it had not yet reported, so a widget removed just before the toolbar was unloaded kept tabindex -1 after moving elsewhere. Those changes are now handled before disconnecting. Updating Tab stops also rewrote the item marker attributes on every item each time; it now writes them only when they change. Addresses #5960
focusFirstTabStop returned true whenever the toolbar had an item, even when the toolbar was hidden and focus() did nothing; it now returns whether the item took focus. Its Javadoc also said it skips a leading text field, which it doesn't; the comment now says it focuses that field. Addresses #5960
The toolbar tests gave random ids to IDE elements that had none, so they could compare focus targets. Those ids outlived each test in the shared session. The tests now tag elements with a data-pw-ref attribute instead. Addresses #5960
gtritchie
left a comment
There was a problem hiding this comment.
Findings from a code review, each checked against the code. Headless Chromium was used to test how MutationObserver, contenteditable and visibility behave.
- Confirmed: the Jobs toolbar rescan every second, the untested
detach()flush, and the double scan infocusFirstTabStop. - Work as described, but nothing triggers them today: a widget removed from an unloaded toolbar,
contenteditable="", radio inputs, andvisibility:hiddenancestors. - Design:
LeftRightToggleButton's disabled styling lives inFindOutputPane.
Nothing here is blocking.
| root.addEventListener("focusin", focusInListener_); | ||
|
|
||
| MutationObserverInit init = MutationObserverInit.create(); | ||
| init.setChildList(true); |
There was a problem hiding this comment.
The observer reacts to childList changes anywhere in the toolbar, and each batch reruns getItems(), which calls getComputedStyle on every candidate and each of its ancestors. A label's text change triggers this too.
While a job is shown, JobManager's 1000 ms timer fires JobElapsedTickEvent, and JobsPaneWidgets.updateProgress → JobProgress.updateElapsed calls Label.setText inside the Jobs Toolbar. In headless Chromium, setting textContent produced one childList record, so the Jobs toolbar does a full style-forcing rescan every second. Ignoring records whose added and removed nodes are all text nodes would skip these.
There was a problem hiding this comment.
Fixed in 29e623e. A batch of changes now triggers a rescan only if it has an attribute change or adds or removes an element, so text-only changes like the elapsed-time label are skipped. A new e2e test counts the getComputedStyle calls on main-toolbar elements: 140 after a text-only change before the fix, 0 now. Adding an element is checked as a control and still rescans.
| root.removeEventListener("focusin", focusInListener_); | ||
|
|
||
| // Handle what changed since the last callback; disconnecting discards it. | ||
| onMutations(observer_.takeRecords(), observer_); |
There was a problem hiding this comment.
Nothing tests this flush. The unit tests never detach or remove a widget, and the e2e test only moves a widget out while its toolbar is still attached. If takeRecords() were dropped, or moved after disconnect(), everything would still pass. HtmlUnit doesn't report childList changes, so this would need an e2e case that removes a widget and unloads its toolbar in the same task.
There was a problem hiding this comment.
Removed the flush in 1d381cc instead of testing it. Nothing in the IDE removes a widget and unloads its toolbar in the same task, and the flush didn't cover a widget removed after unloading either, so no realistic test could exercise it.
| updateTabStops(null); | ||
| } | ||
|
|
||
| void detach() |
There was a problem hiding this comment.
After disconnect(), a widget removed from the toolbar keeps tabindex=-1 and data-rs-toolbar-item, because release() never runs for it. So "a widget moved out of a toolbar is put back into the Tab order" only holds while the toolbar is attached.
Nothing triggers this today. The only widget moved between containers is TextEditingTargetWidget's toggleDocOutlineButton_, and it always moves into another Toolbar, which takes it over on attach. Low priority; it may be enough to note the limit in the PR description.
There was a problem hiding this comment.
Agreed. 1d381cc adds a comment at the release code saying it holds only while the toolbar is loaded, and the PR description now says so too.
| ClassIds.assignClassId(right_, ClassIds.RIGHT_TOGGLE_BTN + "_" + rightId); | ||
| } | ||
|
|
||
| public void setEnabled(boolean enabled) |
There was a problem hiding this comment.
setEnabled only sets disabled, and LeftRightToggleButton.css has no :disabled rule. The disabled look (opacity 0.4) and the pointer blocking stay in FindOutputPane. That pane is the only caller today, but CompilePanel, HistoryPanel, GitReviewPanel and SVNReviewPanel use this widget too, and setEnabled(false) there would give buttons that are disabled but look unchanged. A :disabled rule here would let FindOutputPane drop its opacity and pointerEvents code.
There was a problem hiding this comment.
Fixed in f9c8745. setEnabled() now also toggles a disabled style on the widget (opacity 0.4, no pointer events). I used a class rather than a :disabled rule so the dimming covers the whole table, cell backgrounds included. FindOutputPane no longer styles the toggle, but its click handler still checks replaceModeToggleEnabled_. New unit test: testDisabledToggleButtonLooksDisabled.
| return true; | ||
| if (tag.equals("input")) | ||
| return !NON_TEXT_INPUT_TYPES.contains(el.getAttribute("type").toLowerCase()); | ||
| return "true".equals(el.getAttribute("contenteditable")); |
There was a problem hiding this comment.
contenteditable="" and "plaintext-only" are editable too (headless Chromium reports isContentEditable === true for ""). An element written that way with a tabindex would be treated as a plain item and lose its arrow keys. No toolbar has one today. el.isContentEditable would cover all three forms, here and in CANDIDATES.
There was a problem hiding this comment.
Fixed in 6e10d75. The check now uses the element's isContentEditable, and the selector is [contenteditable]. New unit test: testContentEditableKeepsArrowKeys, with contenteditable="".
| private static final String CANDIDATES = | ||
| "button, input, select, textarea, a[href], [tabindex], [contenteditable=\"true\"]"; | ||
| private static final List<String> NON_TEXT_INPUT_TYPES = Arrays.asList( | ||
| "button", "checkbox", "radio", "submit", "reset", "image", "color", "file"); |
There was a problem hiding this comment.
With radio listed here, a radio in a toolbar becomes a plain arrow item, and preventDefault blocks the native arrow-key selection within its group. No toolbar has a radio today; GitReviewPanel's Staged/Unstaged radios sit beside diffToolbar_, not inside it. Worth keeping in mind if one is added.
There was a problem hiding this comment.
Leaving this as is, since no toolbar has a radio input. Handling one properly would mean a separate kind of group in which the arrow keys change the selection, which is better added along with the first toolbar that needs it.
| // whole toolbar is hidden (as in a hidden pane). | ||
| private boolean isDisplayed(Element el) | ||
| { | ||
| if ("hidden".equals(DomUtils.getComputedStyles(el).getVisibility())) |
There was a problem hiding this comment.
visibility is inherited, so if an ancestor of the toolbar is visibility:hidden, every item fails this check. getItems() comes back empty and updateTabStops clears data-rs-toolbar-last-focused everywhere. That contradicts the comment above ("the answer doesn't change while the whole toolbar is hidden"). Nothing hides a toolbar's ancestor with visibility today, so this is about the comment's promise, not a live bug.
There was a problem hiding this comment.
Fixed in 3b51d24. visibility now counts only while the toolbar itself is visible, matching how the display check already treats a hidden toolbar. New unit test: testHidingToolbarKeepsLastFocused, which failed before the fix.
| widget.setAttribute(WIDGET, ""); | ||
| } | ||
|
|
||
| boolean focusFirstTabStop() |
There was a problem hiding this comment.
updateTabStops(null) already builds the items and groups, and then getItems() runs again with all the computed-style checks. If updateTabStops returned the first group's stop, this would only need to focus it. It's a minor cost, since this only runs on a user command.
There was a problem hiding this comment.
Fixed in 3b51d24. updateTabStops now returns the first group's stop, and focusFirstTabStop focuses that without rescanning.
Any change in a toolbar's subtree reran the item scan, which looks up the computed style of every candidate and its ancestors. While a job is shown, the Jobs toolbar's elapsed-time label changes every second, so it rescanned every second. Changes that only add or remove text nodes can't change the items, so they no longer trigger a rescan. In the e2e test, a text-only change led to 140 style lookups on main-toolbar elements before and 0 now. Addresses #5960
setEnabled() disabled the toggle's buttons, but the dimmed look and the pointer blocking were set by FindOutputPane, so any other caller would get buttons that are disabled but look unchanged. setEnabled() now also adds a disabled style (opacity 0.4, no pointer events) that covers the whole toggle, and FindOutputPane no longer styles it. Addresses #5960
Only contenteditable="true" counted as a text field, so an element made editable with contenteditable="" or "plaintext-only" was a plain toolbar item and lost its arrow keys to toolbar navigation. The check now uses the element's isContentEditable, which covers all three. Addresses #5960
Items with visibility:hidden don't count, but visibility is inherited, so hiding an ancestor of the toolbar with it made every item not count, and the next update forgot which item was focused last. Visibility now counts only while the toolbar itself is visible, as the comment on the display check already promised. focusFirstTabStop also rescanned the items after updating the Tab stops; updateTabStops now returns the first stop, so one scan does. Addresses #5960
detach() handled the observer's pending records before disconnecting, so a widget removed in the same task that unloaded its toolbar would still be put back into the Tab order. Nothing in the IDE does that, a widget removed after the toolbar is unloaded wasn't covered either, and no test could exercise it. The flush is gone, and a comment states that removed widgets are put back into the Tab order only while the toolbar is loaded. Addresses #5960
gtritchie
left a comment
There was a problem hiding this comment.
The new commits fix the earlier findings, or document them as known limits. With the visibility fix and the contenteditable fix each undone, testHidingToolbarKeepsLastFocused and testContentEditableKeepsArrowKeys fail, so both tests catch their bugs. One new test fails; see inline.
| RootPanel.get().add(toggle); | ||
|
|
||
| toggle.setEnabled(false); | ||
| assertEquals("0.4", DomUtils.getComputedStyles(toggle.getElement()).getOpacity()); |
There was a problem hiding this comment.
This fails on 1d381cc: ant unittest reports Tests run: 1012, Failures: 1, with expected: <0.4>, actual: <1> here. ensureInjected() defers the CssResource's injection, so the .disabled rule isn't on the page yet when the test reads the computed opacity.
Calling StyleInjector.flush() before toggle.setEnabled(false) makes all 1012 tests pass. The widget itself is fine in the IDE, where the style is injected long before the toggle is disabled.
There was a problem hiding this comment.
Fixed in 97ac82f. The test now calls StyleInjector.flush() after adding the toggle, with a comment explaining why. My earlier single-class runs passed only because an earlier test had already loaded the widget's styles. ant unittest now reports OK (1012 tests).
testDisabledToggleButtonLooksDisabled read the toggle's computed opacity before LeftRightToggleButton's styles were on the page, because ensureInjected() defers injection. It passed only when an earlier test had already loaded the widget; in the full suite it failed with "expected: <0.4>, actual: <1>". The test now flushes the style injector first, and all 1012 unit tests pass. Addresses #5960
gtritchie
left a comment
There was a problem hiding this comment.
97ac82f fixes testDisabledToggleButtonLooksDisabled. The same StyleInjector.flush() change, on 1d381cc, ran OK (1012 tests) earlier in this review. I didn't rerun the suite on the new head.
One small point about the commit message, which says the test "passed only when an earlier test had already loaded the widget". In my full-suite run it failed even though testDisabledToggleButtonIsSkipped also creates a toggle, so test order may not fully explain it. That doesn't affect the fix.
Two earlier findings are deliberately left as they are: radios in toolbars, and widgets removed while their toolbar is unloaded. The first has no instance in a toolbar today, and the second is now documented. There's also one small gap that nothing triggers today: while a toolbar is hidden with visibility, a button that is itself visibility:hidden can become its group's Tab stop, and showing the toolbar again doesn't update the stops.
No further changes needed.
RStudio E2E - all platformsAll tests passed!
All platforms - commit |
Toolbars now follow the WAI-ARIA toolbar pattern. Before this change, every toolbar button was its own Tab stop.
Behavior
Implementation
ToolbarKeyboardNavigationis attached byToolbarwhile it is loaded, so every toolbar gets this behavior, including subclasses and rstudio-pro's toolbars. It reads the toolbar's items from the DOM each time instead of caching them, and aMutationObserverkeeps the Tab stops correct as buttons are added, removed, disabled, or hidden. A widget moved out of a toolbar while the toolbar is loaded is put back into the Tab order. Changes that only touch text, such as the Jobs toolbar's elapsed time, don't trigger a rescan.While a Find in Files search runs, the Find/Replace toggle's buttons are now natively disabled. Before, the toggle was marked unavailable only with
aria-disabledon its table, so its buttons stayed focusable.LeftRightToggleButton.setEnabled()disables both buttons and greys out the toggle, soFindOutputPaneno longer styles it.Pane focus commands are unchanged. They still focus the first enabled, visible control in the pane, which then becomes that toolbar's Tab stop.
Tests
ToolbarKeyboardNavigationTests. The HtmlUnit browser that runs these doesn't report added or removed elements to aMutationObserver, so the tests that depend on it are e2e tests.tests/toolbar/toolbar_keyboard_navigation.test.ts, run against the main toolbar on a dev server.addins_menu.test.tsis updated, because Tab from the Addins button now leaves the toolbar.Fixes #5960.