Skip to content

Commit 165b419

Browse files
committed
fix(workspace): clamp plugin panel width on window resize
Plugin panels could encroach into the sidebar/content area when the window was resized smaller, or reopen too wide after a resize while closed. Add _clampPluginPanelWidth to enforce max width limits both during window resize events and when panels are first shown.
1 parent 6aef0c7 commit 165b419

2 files changed

Lines changed: 118 additions & 0 deletions

File tree

src/view/WorkspaceManager.js

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -213,6 +213,11 @@ define(function (require, exports, module) {
213213
return;
214214
}
215215

216+
// Clamp plugin panel toolbar width so it doesn't encroach into the sidebar/content area
217+
if (currentlyShownPanel && $mainToolbar.is(":visible")) {
218+
_clampPluginPanelWidth(currentlyShownPanel);
219+
}
220+
216221
// FIXME (issue #4564) Workaround https://github.com/codemirror/CodeMirror/issues/1787
217222
triggerUpdateLayout();
218223

@@ -496,12 +501,28 @@ define(function (require, exports, module) {
496501
return panel.initialSize;
497502
}
498503

504+
function _clampPluginPanelWidth(panelBeingShown) {
505+
let sidebarWidth = $("#sidebar").outerWidth() || 0;
506+
let pluginIconsBarWidth = $pluginIconsBar.outerWidth();
507+
let minToolbarWidth = (panelBeingShown.minWidth || 0) + pluginIconsBarWidth;
508+
let maxToolbarWidth = Math.max(
509+
minToolbarWidth,
510+
Math.min(window.innerWidth * 0.75, window.innerWidth - sidebarWidth - 100)
511+
);
512+
if ($mainToolbar.width() > maxToolbarWidth) {
513+
$mainToolbar.width(maxToolbarWidth);
514+
$windowContent.css("right", maxToolbarWidth);
515+
Resizer.resyncSizer($mainToolbar[0]);
516+
}
517+
}
518+
499519
function _showPluginSidePanel(panelID) {
500520
let panelBeingShown = getPanelForID(panelID);
501521
Resizer.makeResizable($mainToolbar, Resizer.DIRECTION_HORIZONTAL, Resizer.POSITION_LEFT,
502522
panelBeingShown.minWidth, false, undefined, true,
503523
undefined, $windowContent, undefined, _getInitialSize(panelBeingShown));
504524
Resizer.show($mainToolbar[0]);
525+
_clampPluginPanelWidth(panelBeingShown);
505526
recomputeLayout(true);
506527
}
507528

test/spec/MainViewManager-integ-test.js

Lines changed: 97 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1237,5 +1237,102 @@ define(function (require, exports, module) {
12371237
expect(maxSize).toBeLessThanOrEqual(testWindow.innerWidth * 0.75);
12381238
});
12391239
});
1240+
1241+
describe("Plugin panel clamping on window resize", function () {
1242+
let pluginPanel, $toolbarIcon;
1243+
const MIN_WIDTH = 200;
1244+
1245+
beforeAll(function () {
1246+
$toolbarIcon = _$('<a id="test-clamp-icon" href="#"></a>');
1247+
_$("#plugin-icons-bar").append($toolbarIcon);
1248+
1249+
let panelTemplate = '<div id="test-clamp-panel">Test Panel</div>';
1250+
pluginPanel = WorkspaceManager.createPluginPanel(
1251+
"test-clamp-panel", _$(panelTemplate), MIN_WIDTH, $toolbarIcon
1252+
);
1253+
});
1254+
1255+
afterAll(function () {
1256+
if (pluginPanel) {
1257+
pluginPanel.hide();
1258+
}
1259+
$toolbarIcon.remove();
1260+
});
1261+
1262+
afterEach(function () {
1263+
pluginPanel.hide();
1264+
});
1265+
1266+
it("should clamp plugin panel when window resizes smaller", function () {
1267+
pluginPanel.show();
1268+
WorkspaceManager.setPluginPanelWidth(600);
1269+
1270+
const $mainToolbar = _$("#main-toolbar");
1271+
const widthBefore = $mainToolbar.width();
1272+
1273+
// Simulate a narrow window resize
1274+
const sidebarWidth = _$("#sidebar").outerWidth() || 0;
1275+
const maxAllowed = Math.min(
1276+
testWindow.innerWidth * 0.75,
1277+
testWindow.innerWidth - sidebarWidth - 100
1278+
);
1279+
1280+
// Only expect clamping if the panel was wider than max
1281+
if (widthBefore > maxAllowed) {
1282+
testWindow.dispatchEvent(new testWindow.Event("resize"));
1283+
expect($mainToolbar.width()).toBeLessThanOrEqual(maxAllowed);
1284+
} else {
1285+
// Panel fits, dispatch resize and verify it stays unchanged
1286+
testWindow.dispatchEvent(new testWindow.Event("resize"));
1287+
expect($mainToolbar.width()).toEqual(widthBefore);
1288+
}
1289+
});
1290+
1291+
it("should not let toolbar disappear on window resize", function () {
1292+
pluginPanel.show();
1293+
1294+
const $mainToolbar = _$("#main-toolbar");
1295+
const $pluginIconsBar = _$("#plugin-icons-bar");
1296+
1297+
testWindow.dispatchEvent(new testWindow.Event("resize"));
1298+
1299+
// Toolbar must remain at least as wide as the icons bar + panel minWidth
1300+
const minToolbarWidth = MIN_WIDTH + $pluginIconsBar.outerWidth();
1301+
expect($mainToolbar.width()).toBeGreaterThanOrEqual(minToolbarWidth);
1302+
});
1303+
1304+
it("should clamp panel width when shown after window was resized", function () {
1305+
// Panel is hidden; compute what the max toolbar width would be
1306+
const sidebarWidth = _$("#sidebar").outerWidth() || 0;
1307+
const $pluginIconsBar = _$("#plugin-icons-bar");
1308+
const maxToolbarWidth = Math.min(
1309+
testWindow.innerWidth * 0.75,
1310+
testWindow.innerWidth - sidebarWidth - 100
1311+
);
1312+
1313+
// Now show the panel — it should be clamped to maxToolbarWidth
1314+
pluginPanel.show();
1315+
1316+
const $mainToolbar = _$("#main-toolbar");
1317+
expect($mainToolbar.width()).toBeLessThanOrEqual(maxToolbarWidth);
1318+
1319+
// And content area should match
1320+
const $windowContent = _$(".content");
1321+
const rightOffset = parseInt($windowContent.css("right"), 10);
1322+
expect(rightOffset).toEqual($mainToolbar.width());
1323+
});
1324+
1325+
it("should keep content right offset in sync after resize clamp", function () {
1326+
pluginPanel.show();
1327+
WorkspaceManager.setPluginPanelWidth(600);
1328+
1329+
testWindow.dispatchEvent(new testWindow.Event("resize"));
1330+
1331+
const $mainToolbar = _$("#main-toolbar");
1332+
const $windowContent = _$(".content");
1333+
const rightOffset = parseInt($windowContent.css("right"), 10);
1334+
expect(rightOffset).toEqual($mainToolbar.width());
1335+
});
1336+
});
12401337
});
12411338
});

0 commit comments

Comments
 (0)