Skip to content

Commit fece3fc

Browse files
committed
Move focus out of any column a layout pass makes inert
A mouse drag of a splitter, or a window shrink, can collapse the column that holds focus; the splitter takes no focus on mousedown. Once the column is inert the browser drops focus to the page body. MainSplitPanel now reports such a collapse, and PaneManager focuses the widest column. That also covers column zooms, so zoomColumn() no longer needs its own rescue. Addresses #19181.
1 parent 85b3544 commit fece3fc

3 files changed

Lines changed: 135 additions & 37 deletions

File tree

‎e2e/rstudio/tests/panes/layout/zoom_inert.test.ts‎

Lines changed: 54 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,26 @@ async function hasFocusWithin(page: Page, pane: string): Promise<boolean> {
5151
return page.locator(pane).evaluate(el => el.contains(document.activeElement));
5252
}
5353

54+
// Whether focus is on something the user can still reach, rather than dropped
55+
// to the page body or left inside a hidden column.
56+
async function hasReachableFocus(page: Page): Promise<boolean> {
57+
return page.evaluate(() => {
58+
const el = document.activeElement;
59+
return el !== null && el !== document.body &&
60+
el.closest('[inert]') === null && el.checkVisibility();
61+
});
62+
}
63+
64+
// Re-focuses on each poll: a terminal that is still starting can take focus
65+
// after the first attempt.
66+
async function focusSelectedTab(page: Page, pane: string): Promise<void> {
67+
const tab = page.locator(`${pane} [role="tab"][aria-selected="true"]`);
68+
await expect.poll(async () => {
69+
await tab.focus();
70+
return hasFocusWithin(page, pane);
71+
}).toBe(true);
72+
}
73+
5474
async function getOffsetWidth(page: Page, selector: string): Promise<number> {
5575
return page.locator(selector).evaluate(el => (el as HTMLElement).offsetWidth);
5676
}
@@ -126,7 +146,7 @@ test.describe('Panes hidden by zoom leave the accessibility tree (#19181)', () =
126146
await expectExposed(page, CONSOLE_PANE);
127147
});
128148

129-
test('dragging a column splitter closed hides the column', async ({ rstudioPage: page }) => {
149+
test('dragging a column splitter closed hides the column and moves focus out', async ({ rstudioPage: page }) => {
130150
const splitter = page.locator(MIDDLE_COLUMN_SPLITTER);
131151
const box = await splitter.boundingBox();
132152
if (!box)
@@ -140,9 +160,13 @@ test.describe('Panes hidden by zoom leave the accessibility tree (#19181)', () =
140160
// splitter back however the test ends.
141161
try {
142162
// Mouse: drag the right column shut against the window edge, then back.
163+
// The splitter takes no focus on mousedown, so focus is still in the
164+
// column it shuts and has to move out.
165+
await focusSelectedTab(page, TABSET1_PANE);
143166
await dragSplitterTo(page, MIDDLE_COLUMN_SPLITTER, windowWidth - 1, splitterY);
144167
await expect.poll(() => getOffsetWidth(page, TABSET1_PANE)).toBeLessThan(20);
145168
await expectHidden(page, TABSET1_PANE, TABSET2_PANE);
169+
await expect.poll(() => hasReachableFocus(page)).toBe(true);
146170

147171
await dragSplitterTo(page, MIDDLE_COLUMN_SPLITTER, splitterX, splitterY);
148172
await expectExposed(page, CONSOLE_PANE, TABSET1_PANE, TABSET2_PANE);
@@ -163,6 +187,34 @@ test.describe('Panes hidden by zoom leave the accessibility tree (#19181)', () =
163187
}
164188
});
165189

190+
// Columns keep their share of the window as it resizes, so a shrink collapses
191+
// only a column that was already narrow.
192+
test('shrinking the window past a narrow column hides it and moves focus out', async ({ rstudioPage: page }) => {
193+
const viewport = await page.evaluate(() => ({ width: innerWidth, height: innerHeight }));
194+
const box = await page.locator(MIDDLE_COLUMN_SPLITTER).boundingBox();
195+
if (!box)
196+
throw new Error('Middle column splitter has no bounding box; is it rendered?');
197+
const splitterY = box.y + box.height / 2;
198+
199+
try {
200+
await dragSplitterTo(page, MIDDLE_COLUMN_SPLITTER, viewport.width - 40, splitterY);
201+
await expect.poll(() => getOffsetWidth(page, TABSET1_PANE)).toBeGreaterThanOrEqual(20);
202+
await expectExposed(page, TABSET1_PANE);
203+
await focusSelectedTab(page, TABSET1_PANE);
204+
205+
await page.setViewportSize({ width: Math.round(viewport.width / 3), height: viewport.height });
206+
await expect.poll(() => getOffsetWidth(page, TABSET1_PANE)).toBeLessThan(20);
207+
await expectHidden(page, TABSET1_PANE, TABSET2_PANE);
208+
await expect.poll(() => hasReachableFocus(page)).toBe(true);
209+
} finally {
210+
await page.setViewportSize(viewport);
211+
await dragSplitterTo(page, MIDDLE_COLUMN_SPLITTER, box.x + box.width / 2, splitterY);
212+
await expect.poll(async () => Math.abs(
213+
((await page.locator(MIDDLE_COLUMN_SPLITTER).boundingBox())?.x ?? 0) - box.x))
214+
.toBeLessThanOrEqual(2);
215+
}
216+
});
217+
166218
// The zoom moves to Source while its column is still collapsed, and the new
167219
// editor must still get focus.
168220
test('opening a document while a pane is zoomed focuses the editor', async ({ rstudioPage: page }) => {
@@ -247,8 +299,7 @@ test.describe('Panes hidden by zoom leave the accessibility tree (#19181)', () =
247299
if (tabName === 'Terminal')
248300
await expect(page.locator(`${CONSOLE_PANE} ${XTERM_SELECTOR}`)).toBeVisible();
249301

250-
await page.locator(`${TABSET1_PANE} [role="tab"][aria-selected="true"]`).focus();
251-
await expect.poll(() => hasFocusWithin(page, TABSET1_PANE)).toBe(true);
302+
await focusSelectedTab(page, TABSET1_PANE);
252303

253304
await executeCommand(page, 'layoutZoomLeftColumn');
254305
await expectHidden(page, TABSET1_PANE);

‎src/gwt/src/org/rstudio/studio/client/workbench/ui/MainSplitPanel.java‎

Lines changed: 64 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -18,9 +18,11 @@
1818
import java.util.Arrays;
1919
import java.util.HashMap;
2020
import java.util.Map;
21+
import java.util.function.Consumer;
2122

2223
import org.rstudio.core.client.ElementIds;
2324
import org.rstudio.core.client.a11y.A11y;
25+
import org.rstudio.core.client.dom.DomUtils;
2426
import org.rstudio.core.client.events.ManageLayoutCommandsEvent;
2527
import org.rstudio.core.client.js.JsObject;
2628
import org.rstudio.studio.client.application.events.EventBus;
@@ -683,27 +685,76 @@ private void updateCollapsedColumns()
683685
if (center_ == null || getOffsetWidth() == 0)
684686
return;
685687

688+
boolean hidFocus = false;
686689
for (Widget column : leftList_)
687-
updateCollapsedColumn(column);
688-
updateCollapsedColumn(center_);
689-
updateCollapsedColumn(right_);
690-
updateCollapsedColumn(sidebar_);
690+
hidFocus |= updateCollapsedColumn(column);
691+
hidFocus |= updateCollapsedColumn(center_);
692+
hidFocus |= updateCollapsedColumn(right_);
693+
hidFocus |= updateCollapsedColumn(sidebar_);
694+
695+
if (hidFocus && focusedColumnCollapsedHandler_ != null)
696+
focusedColumnCollapsedHandler_.accept(getWidestColumn());
691697
}
692698

693-
private void updateCollapsedColumn(Widget column)
699+
/**
700+
* Update one column's inert state. Returns true if the column has just
701+
* become inert while holding focus, which the browser will drop to the page
702+
* body -- e.g. a mouse drag of a splitter leaves focus in the column it shuts.
703+
*/
704+
private boolean updateCollapsedColumn(Widget column)
694705
{
695-
// insert() and remove() lay out while initialize() or a sidebar change
696-
// is partway through, when a field can name a widget that is not, or no
697-
// longer, a child.
698-
if (column == null || column.getParent() != this)
699-
return;
706+
if (!isColumn(column))
707+
return false;
700708

701709
// This runs on every layout pass, splitter drags included; write the
702710
// attributes only when the column's state changes.
703711
Element container = getWidgetContainerElement(column);
704712
boolean collapsed = column.getOffsetWidth() < COLLAPSED_COLUMN_WIDTH;
705-
if (container.hasAttribute("inert") != collapsed)
706-
A11y.setInert(container, collapsed);
713+
if (container.hasAttribute("inert") == collapsed)
714+
return false;
715+
716+
boolean hidFocus = collapsed &&
717+
DomUtils.contains(container, DomUtils.getActiveElement());
718+
A11y.setInert(container, collapsed);
719+
return hidFocus;
720+
}
721+
722+
private Widget getWidestColumn()
723+
{
724+
ArrayList<Widget> columns = new ArrayList<>(leftList_);
725+
columns.add(center_);
726+
columns.add(right_);
727+
columns.add(sidebar_);
728+
729+
Widget widest = null;
730+
for (Widget column : columns)
731+
{
732+
if (isColumn(column) &&
733+
(widest == null || column.getOffsetWidth() > widest.getOffsetWidth()))
734+
{
735+
widest = column;
736+
}
737+
}
738+
return widest;
739+
}
740+
741+
/**
742+
* insert() and remove() lay out while initialize() or a sidebar change is
743+
* partway through, when a field can name a widget that is not, or no
744+
* longer, a child.
745+
*/
746+
private boolean isColumn(Widget widget)
747+
{
748+
return widget != null && widget.getParent() == this;
749+
}
750+
751+
/**
752+
* Called with the widest column when a layout pass collapses the column
753+
* holding focus, so focus can move somewhere still showing.
754+
*/
755+
public void setFocusedColumnCollapsedHandler(Consumer<Widget> handler)
756+
{
757+
focusedColumnCollapsedHandler_ = handler;
707758
}
708759

709760
@Override
@@ -838,6 +889,7 @@ private static boolean isZoomedWidth(double width, int panelWidth)
838889
// collapsed columns at 0 or 1px; isZoomedColumnState uses the same cutoff.
839890
private static final int COLLAPSED_COLUMN_WIDTH = 20;
840891
private Command layoutCommand_;
892+
private Consumer<Widget> focusedColumnCollapsedHandler_;
841893

842894
/**
843895
* Check if the saved state has a sidebar width below the minimum allowed.

‎src/gwt/src/org/rstudio/studio/client/workbench/ui/PaneManager.java‎

Lines changed: 17 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -377,6 +377,7 @@ public PaneManager(Provider<MainSplitPanel> pSplitPanel,
377377
center_ = createSplitWindow(panes_.get(0), panes_.get(1), LEFT_COLUMN, 0.4, splitterSize);
378378
right_ = createSplitWindow(panes_.get(2), panes_.get(3), RIGHT_COLUMN, 0.6, splitterSize);
379379
panel_ = pSplitPanel.get();
380+
panel_.setFocusedColumnCollapsedHandler(this::onFocusedColumnCollapsed);
380381

381382
// get the widgets for the extra source columns to be displayed
382383
additionalSourceCount_ = userPrefs_.panes().getValue().getAdditionalSourceColumns();
@@ -2483,7 +2484,6 @@ public void zoomColumn(String columnId)
24832484

24842485
String currentZoomedColumn = getZoomedColumn();
24852486
boolean unZooming = false;
2486-
final Element focusedBeforeZoom = DomUtils.getActiveElement();
24872487

24882488
// A tracked pane zoom makes its column read as zoomed, and the
24892489
// same-column branch below ends it through restoreLayout(). A request
@@ -2608,33 +2608,27 @@ else if (StringUtil.equals(columnId, SIDEBAR_COLUMN))
26082608
}
26092609
}
26102610

2611-
final Widget zoomedColumn = getColumnWidget(columnId);
2612-
Command afterZoom = () ->
2613-
{
2614-
manageLayoutCommands();
2615-
if (zoomHidFocus(focusedBeforeZoom))
2616-
focusPane(getFocusTargetIn(zoomedColumn));
2617-
};
2618-
26192611
if (sidebarOnRight)
2620-
resizeHorizontallyWithSidebarOnRight(
2621-
rightTargetSize, leftTargetSize, sidebarTargetSize, afterZoom);
2612+
resizeHorizontallyWithSidebarOnRight(rightTargetSize, leftTargetSize, sidebarTargetSize, () -> manageLayoutCommands());
26222613
else if (sidebarOnLeft)
2623-
resizeHorizontallyWithSidebarOnLeft(
2624-
rightTargetSize, leftTargetSize, sidebarTargetSize, afterZoom);
2614+
resizeHorizontallyWithSidebarOnLeft(rightTargetSize, leftTargetSize, sidebarTargetSize, () -> manageLayoutCommands());
26252615
else
2626-
resizeHorizontally(rightTargetSize, leftTargetSize, afterZoom);
2616+
resizeHorizontally(rightTargetSize, leftTargetSize, () -> manageLayoutCommands());
26272617
}
26282618

2629-
private Widget getColumnWidget(String columnId)
2619+
/**
2620+
* A layout pass made the column holding focus inert (#19181): a zoom, a
2621+
* splitter drag, or a window resize. Move focus into the widest column,
2622+
* unless something else has placed it by the time the layout settles.
2623+
*/
2624+
private void onFocusedColumnCollapsed(Widget widestColumn)
26302625
{
2631-
if (StringUtil.equals(columnId, LEFT_COLUMN))
2632-
return center_;
2633-
if (StringUtil.equals(columnId, RIGHT_COLUMN))
2634-
return right_;
2635-
if (StringUtil.equals(columnId, SIDEBAR_COLUMN))
2636-
return sidebar_;
2637-
return null;
2626+
Scheduler.get().scheduleDeferred(() ->
2627+
{
2628+
Element active = DomUtils.getActiveElement();
2629+
if (active == null || active == Document.get().getBody() || A11y.isInert(active))
2630+
focusPane(getFocusTargetIn(widestColumn));
2631+
});
26382632
}
26392633

26402634
/**
@@ -2651,6 +2645,7 @@ private LogicalWindow getFocusTargetIn(Widget column)
26512645
// panesByName_ has the live one.
26522646
ArrayList<LogicalWindow> windows = new ArrayList<>(panes_);
26532647
windows.add(panesByName_.get(UserPrefsAccessor.Panes.QUADRANTS_SIDEBAR));
2648+
windows.addAll(sourceLogicalWindows_);
26542649
LogicalWindow topmost = null;
26552650
for (LogicalWindow window : windows)
26562651
{

0 commit comments

Comments
 (0)