feat: add option to disable Ctrl+W close shortcut

This commit is contained in:
Alex Kontos
2025-08-05 15:35:07 +01:00
parent 7383a3518b
commit 92c159f358
6 changed files with 85 additions and 0 deletions

View File

@@ -1377,6 +1377,13 @@ var gLastOpenDirectory = {
this._lastDir this._lastDir
); );
} }
if (BrowserUIUtils.closeShortcutDisabled) {
document.getElementById("key_close").remove();
document.getElementById("menu_close").removeAttribute("key");
document.getElementById("key_closeWindow").remove();
document.getElementById("menu_closeWindow").removeAttribute("key");
}
}, },
reset() { reset() {
this._lastDir = null; this._lastDir = null;

View File

@@ -110,6 +110,12 @@ var NonBrowserWindow = {
document.getElementById("key_quitApplication").remove(); document.getElementById("key_quitApplication").remove();
document.getElementById("menu_FileQuitItem").removeAttribute("key"); document.getElementById("menu_FileQuitItem").removeAttribute("key");
} }
if (BrowserUIUtils.closeShortcutDisabled) {
document.getElementById("key_close").remove();
document.getElementById("menu_close").removeAttribute("key");
document.getElementById("key_closeWindow").remove();
document.getElementById("menu_closeWindow").removeAttribute("key");
}
} }
this.delayedStartupTimeoutId = setTimeout(() => this.delayedStartup(), 0); this.delayedStartupTimeoutId = setTimeout(() => this.delayedStartup(), 0);

View File

@@ -0,0 +1,62 @@
/* Any copyright is dedicated to the Public Domain.
* http://creativecommons.org/publicdomain/zero/1.0/ */
add_task(async function test_appMenu_close_disabled() {
await SpecialPowers.pushPrefEnv({
set: [["browser.closeShortcut.disabled", true]],
});
let win = await BrowserTestUtils.openNewBrowserWindow();
let doc = win.document;
let menuButton = doc.getElementById("PanelUI-menu-button");
menuButton.click();
await BrowserTestUtils.waitForEvent(win.PanelUI.mainView, "ViewShown");
let closeButton = doc.querySelector(`[key="key_closeWindow"]`);
is(closeButton, null, "No close button with shortcut key");
await BrowserTestUtils.closeWindow(win);
await SpecialPowers.popPrefEnv();
});
add_task(async function test_close_shortcut_disabled() {
async function testCloseShortcut(shouldClose) {
let win = await BrowserTestUtils.openNewBrowserWindow();
let closeRequested = false;
let observer = {
observe(subject, topic, data) {
is(topic, "close-application-requested", "Right observer topic");
ok(shouldClose, "Close shortcut should NOT have worked");
// Don't actually close the browser when testing.
let cancelClose = subject.QueryInterface(Ci.nsISupportsPRBool);
cancelClose.data = true;
closeRequested = true;
},
};
Services.obs.addObserver(observer, "close-application-requested");
let modifiers = { accelKey: true };
if (AppConstants.platform == "win") {
modifiers.shiftKey = true;
}
EventUtils.synthesizeKey("w", modifiers, win);
await BrowserTestUtils.closeWindow(win);
Services.obs.removeObserver(observer, "close-application-requested");
is(closeRequested, shouldClose, "Expected close state");
}
// Close shortcut should work when pref is not set.
await testCloseShortcut(true);
await SpecialPowers.pushPrefEnv({
set: [["browser.closeShortcut.disabled", true]],
});
await testCloseShortcut(false);
});

View File

@@ -174,3 +174,10 @@ XPCOMUtils.defineLazyPreferenceGetter(
"browser.quitShortcut.disabled", "browser.quitShortcut.disabled",
false false
); );
XPCOMUtils.defineLazyPreferenceGetter(
BrowserUIUtils,
"closeShortcutDisabled",
"browser.closeShortcut.disabled",
false
);

View File

@@ -214,6 +214,9 @@ pref("browser.ml.chat.enabled", false); // Disable built-in ML chat features.
// Image Format Support // Image Format Support
pref("image.jxl.enabled", true); // Enable support for the JPEG XL image format. pref("image.jxl.enabled", true); // Enable support for the JPEG XL image format.
// Keyboard Shortcuts
pref("browser.closeShortcut.disabled", false); // false = Ctrl+W (or Cmd+W) closes tab/window (default behavior). true = disables this shortcut.
// Restart Menu (often provided by extensions or custom builds, not a standard Firefox feature) // Restart Menu (often provided by extensions or custom builds, not a standard Firefox feature)
pref("browser.restart_menu.showpanelmenubtn", true); // If a restart menu is present, show its button in a panel menu. pref("browser.restart_menu.showpanelmenubtn", true); // If a restart menu is present, show its button in a panel menu.
pref("browser.restart_menu.purgecache", false); // If true, purges caches upon restart initiated via this menu. pref("browser.restart_menu.purgecache", false); // If true, purges caches upon restart initiated via this menu.