Bug 1880914 - Move browser/tab closing functions. r=Gijs,perftest-reviewers,devtools-reviewers,fxview-reviewers,sessionstore-reviewers,sfoster,kshampur

Differential Revision: https://phabricator.services.mozilla.com/D206197
This commit is contained in:
Yi Xiong Wong 2024-04-08 17:21:01 +00:00
parent 2eb9e6b7a0
commit 3839c61dbc
16 changed files with 58 additions and 60 deletions

View file

@ -378,4 +378,40 @@ var BrowserCommands = {
fp.open(fpCallback);
} catch (ex) {}
},
closeTabOrWindow(event) {
// If we're not a browser window, just close the window.
if (window.location.href != AppConstants.BROWSER_CHROME_URL) {
closeWindow(true);
return;
}
// In a multi-select context, close all selected tabs
if (gBrowser.multiSelectedTabsCount) {
gBrowser.removeMultiSelectedTabs();
return;
}
// Keyboard shortcuts that would close a tab that is pinned select the first
// unpinned tab instead.
if (
event &&
(event.ctrlKey || event.metaKey || event.altKey) &&
gBrowser.selectedTab.pinned
) {
if (gBrowser.visibleTabs.length > gBrowser._numPinnedTabs) {
gBrowser.tabContainer.selectedIndex = gBrowser._numPinnedTabs;
}
return;
}
// If the current tab is the last one, this will close the window.
gBrowser.removeCurrentTab({ animate: true });
},
tryToCloseWindow(event) {
if (WindowIsClosing(event)) {
window.close();
} // WindowIsClosing does all the necessary checks
},
};

View file

@ -32,8 +32,8 @@
<command id="cmd_printPreviewToggle" oncommand="PrintUtils.togglePrintPreview(gBrowser.selectedBrowser.browsingContext);"/>
<command id="cmd_file_importFromAnotherBrowser" oncommand="MigrationUtils.showMigrationWizard(window, { entrypoint: MigrationUtils.MIGRATION_ENTRYPOINTS.FILE_MENU });"/>
<command id="cmd_help_importFromAnotherBrowser" oncommand="MigrationUtils.showMigrationWizard(window, { entrypoint: MigrationUtils.MIGRATION_ENTRYPOINTS.HELP_MENU });"/>
<command id="cmd_close" oncommand="BrowserCloseTabOrWindow(event);"/>
<command id="cmd_closeWindow" oncommand="BrowserTryToCloseWindow(event)"/>
<command id="cmd_close" oncommand="BrowserCommands.closeTabOrWindow(event);"/>
<command id="cmd_closeWindow" oncommand="BrowserCommands.tryToCloseWindow(event)"/>
<command id="cmd_toggleMute" oncommand="gBrowser.toggleMuteAudioOnMultiSelectedTabs(gBrowser.selectedTab)"/>
<command id="cmd_CustomizeToolbars" oncommand="gCustomizeMode.enter()"/>
<command id="cmd_toggleOfflineStatus" oncommand="BrowserOffline.toggleOfflineStatus();"/>

View file

@ -2650,7 +2650,7 @@ function HandleAppCommandEvent(evt) {
BrowserCommands.openTab();
break;
case "Close":
BrowserCloseTabOrWindow();
BrowserCommands.closeTabOrWindow();
break;
case "Find":
gLazyFindCommand("onFindCommand");
@ -2765,42 +2765,6 @@ var gLastOpenDirectory = {
},
};
function BrowserCloseTabOrWindow(event) {
// If we're not a browser window, just close the window.
if (window.location.href != AppConstants.BROWSER_CHROME_URL) {
closeWindow(true);
return;
}
// In a multi-select context, close all selected tabs
if (gBrowser.multiSelectedTabsCount) {
gBrowser.removeMultiSelectedTabs();
return;
}
// Keyboard shortcuts that would close a tab that is pinned select the first
// unpinned tab instead.
if (
event &&
(event.ctrlKey || event.metaKey || event.altKey) &&
gBrowser.selectedTab.pinned
) {
if (gBrowser.visibleTabs.length > gBrowser._numPinnedTabs) {
gBrowser.tabContainer.selectedIndex = gBrowser._numPinnedTabs;
}
return;
}
// If the current tab is the last one, this will close the window.
gBrowser.removeCurrentTab({ animate: true });
}
function BrowserTryToCloseWindow(event) {
if (WindowIsClosing(event)) {
window.close();
} // WindowIsClosing does all the necessary checks
}
function getLoadContext() {
return window.docShell.QueryInterface(Ci.nsILoadContext);
}

View file

@ -37,8 +37,6 @@
"loadOneOrMoreURIs",
"openLocation",
"gLastOpenDirectory",
"BrowserCloseTabOrWindow",
"BrowserTryToCloseWindow",
"getLoadContext",
"readFromClipboard",
"BrowserViewSourceOfDocument",

View file

@ -57,7 +57,7 @@ add_task(async function closeWindowWithMultipleTabsIncludingOneBeforeUnload() {
);
let windowClosedPromise = BrowserTestUtils.domWindowClosed(newWin);
expectingDialog = true;
newWin.BrowserTryToCloseWindow();
newWin.BrowserCommands.tryToCloseWindow();
await windowClosedPromise;
ok(!expectingDialog, "There should have been a dialog.");
ok(newWin.closed, "Window should be closed.");

View file

@ -4,7 +4,7 @@ function test() {
whenDelayedStartupFinished(privateWin, function () {
privateWin.BrowserCommands.openTab();
privateWin.BrowserTryToCloseWindow();
privateWin.BrowserCommands.tryToCloseWindow();
ok(true, "didn't prompt");
executeSoon(finish);

View file

@ -37,7 +37,7 @@ add_task(async function closing_last_window_equals_quitting() {
Services.obs.addObserver(obs, "browser-lastwindow-close-requested");
let newWin = await BrowserTestUtils.openNewBrowserWindow();
let closedPromise = BrowserTestUtils.windowClosed(newWin);
newWin.BrowserTryToCloseWindow();
newWin.BrowserCommands.tryToCloseWindow();
await closedPromise;
is(observed, 1, "Got a notification for closing the normal window.");
Services.obs.removeObserver(obs, "browser-lastwindow-close-requested");
@ -68,12 +68,12 @@ add_task(async function closing_last_window_equals_quitting() {
});
let popupWin = await popupPromise;
let closedPromise = BrowserTestUtils.windowClosed(newWin);
newWin.BrowserTryToCloseWindow();
newWin.BrowserCommands.tryToCloseWindow();
await closedPromise;
is(observed, 0, "Got no notification for closing the normal window.");
closedPromise = BrowserTestUtils.windowClosed(popupWin);
popupWin.BrowserTryToCloseWindow();
popupWin.BrowserCommands.tryToCloseWindow();
await closedPromise;
is(
observed,

View file

@ -31,7 +31,7 @@ add_task(
info("Opening Firefox View tab...");
await openFirefoxViewTab(win);
info("Trigger warnAboutClosingWindow()");
win.BrowserTryToCloseWindow();
win.BrowserCommands.tryToCloseWindow();
await BrowserTestUtils.closeWindow(win);
ok(!dialogObserver.wasOpened, "Dialog was not opened");
dialogObserver.cleanup();

View file

@ -21,7 +21,7 @@
* not enabled on that platform (platform shim; the application is kept running
* although there are no windows left)
* @note There is a difference when closing a browser window with
* BrowserTryToCloseWindow() as opposed to close(). The former will make
* BrowserCommands.tryToCloseWindow() as opposed to close(). The former will make
* nsSessionStore restore a window next time it gets a chance and will post
* notifications. The latter won't.
*/
@ -182,7 +182,7 @@ function injectTestTabs(win) {
}
/**
* Attempts to close a window via BrowserTryToCloseWindow so that
* Attempts to close a window via BrowserCommands.tryToCloseWindow so that
* we get the browser-lastwindow-close-requested and
* browser-lastwindow-close-granted observer notifications.
*
@ -195,7 +195,7 @@ function injectTestTabs(win) {
function closeWindowForRestoration(win) {
return new Promise(resolve => {
let closePromise = BrowserTestUtils.windowClosed(win);
win.BrowserTryToCloseWindow();
win.BrowserCommands.tryToCloseWindow();
if (!win.closed) {
resolve(false);
return;

View file

@ -32,10 +32,10 @@ function closeFirstWin(win) {
win.gBrowser.pinTab(win.gBrowser.tabs[1]);
let winClosed = BrowserTestUtils.windowClosed(win);
// We need to call BrowserTryToCloseWindow in order to trigger
// We need to call BrowserCommands.tryToCloseWindow in order to trigger
// the machinery that chooses whether or not to save the session
// for the last window.
win.BrowserTryToCloseWindow();
win.BrowserCommands.tryToCloseWindow();
ok(win.closed, "window closed");
winClosed.then(() => {

View file

@ -216,12 +216,12 @@ function onStateRestored() {
newWin.gBrowser.removeTab(newTab);
newWin.gBrowser.removeTab(newTab2);
}
newWin.BrowserTryToCloseWindow();
newWin.BrowserCommands.tryToCloseWindow();
},
{ capture: true, once: true }
);
} else {
newWin.BrowserTryToCloseWindow();
newWin.BrowserCommands.tryToCloseWindow();
}
});
},

View file

@ -121,6 +121,6 @@ add_task(async function () {
await resetDownloads();
const closePromise = BrowserTestUtils.windowClosed(privateWindow);
privateWindow.BrowserTryToCloseWindow();
privateWindow.BrowserCommands.tryToCloseWindow();
await closePromise;
});

View file

@ -141,7 +141,7 @@ add_task(async function () {
info("close the private window and check if private messages are removed");
const onPrivateMessagesCleared = hud.ui.once("private-messages-cleared");
privateWindow.BrowserTryToCloseWindow();
privateWindow.BrowserCommands.tryToCloseWindow();
await onPrivateMessagesCleared;
ok(

View file

@ -66,7 +66,7 @@ add_task(async function () {
});
info("Close the second private window");
secondPrivateWindow.BrowserTryToCloseWindow();
secondPrivateWindow.BrowserCommands.tryToCloseWindow();
// Let a chance to an unexpected async event to be fired
await wait(1000);
@ -80,7 +80,7 @@ add_task(async function () {
info(
"close the private window and check if LAST_PRIVATE_CONTEXT_EXIT resource is sent"
);
privateWindow.BrowserTryToCloseWindow();
privateWindow.BrowserCommands.tryToCloseWindow();
info("Wait for LAST_PRIVATE_CONTEXT_EXIT");
await waitFor(() => availableResources.length == 1);

View file

@ -134,7 +134,7 @@ add_task(async function () {
info("Close the test window");
const winClosed = BrowserTestUtils.windowClosed(win);
win.BrowserTryToCloseWindow();
win.BrowserCommands.tryToCloseWindow();
await winClosed;
});

View file

@ -146,7 +146,7 @@ Tart.prototype = {
clickCloseCurrentTab() {
this._endDetection = this.tabDetector;
this._win.BrowserCloseTabOrWindow();
this._win.BrowserCommands.closeTabOrWindow();
return this._win.gBrowser.selectedTab;
},