Skip to content

Commit

Permalink
Bug 1725430: disable screenshot shortcut for extension when screensho…
Browse files Browse the repository at this point in the history
…ts.browser.component.enabled is true. r=emalysz,fluent-reviewers,flod

Differential Revision: https://phabricator.services.mozilla.com/D125850
  • Loading branch information
Emma Malysz committed Sep 21, 2021
1 parent 6c11c9c commit 6fede10
Show file tree
Hide file tree
Showing 11 changed files with 44 additions and 42 deletions.
3 changes: 3 additions & 0 deletions browser/base/content/browser-sets.inc
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,8 @@
<command id="Tools:Sanitize" oncommand="Sanitizer.showUI(window);"/>
<command id="Tools:PrivateBrowsing"
oncommand="OpenBrowserWindow({private: true});"/>
<command id="Browser:Screenshot" oncommand="ScreenshotsUtils.notify(window, 'shortcut')"/>

#ifdef NIGHTLY_BUILD
<command id="Tools:FissionWindow"
oncommand="OpenBrowserWindow({fission: true, private: !!window?.browsingContext?.usePrivateBrowsing});"
Expand Down Expand Up @@ -303,6 +305,7 @@

<key id="key_privatebrowsing" command="Tools:PrivateBrowsing" data-l10n-id="private-browsing-shortcut"
modifiers="accel,shift" reserved="true"/>
<key id="key_screenshot" data-l10n-id="screenshot-shortcut" command="Browser:Screenshot" modifiers="accel,shift"/>
<key id="key_sanitize" command="Tools:Sanitize" keycode="VK_DELETE" modifiers="accel,shift"/>
#ifdef XP_MACOSX
<key id="key_sanitize_mac" command="Tools:Sanitize" keycode="VK_BACK" modifiers="accel,shift"/>
Expand Down
1 change: 1 addition & 0 deletions browser/base/content/browser.js
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,7 @@ XPCOMUtils.defineLazyModuleGetters(this, {
SafeBrowsing: "resource://gre/modules/SafeBrowsing.jsm",
Sanitizer: "resource:///modules/Sanitizer.jsm",
SaveToPocket: "chrome://pocket/content/SaveToPocket.jsm",
ScreenshotsUtils: "resource:///modules/ScreenshotsUtils.jsm",
SessionStartup: "resource:///modules/sessionstore/SessionStartup.jsm",
SessionStore: "resource:///modules/sessionstore/SessionStore.jsm",
ShortcutUtils: "resource://gre/modules/ShortcutUtils.jsm",
Expand Down
1 change: 1 addition & 0 deletions browser/base/content/browser.xhtml
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,7 @@
<link rel="localization" href="browser/tabbrowser.ftl"/>
<link rel="localization" href="preview/firefoxSuggest.ftl"/>
<link rel="localization" href="browser/toolbarContextMenu.ftl"/>
<link rel="localization" href="browser/screenshots.ftl"/>

<title data-l10n-id="browser-main-window-title"></title>

Expand Down
6 changes: 5 additions & 1 deletion browser/base/content/nsContextMenu.js
Original file line number Diff line number Diff line change
Expand Up @@ -1300,7 +1300,11 @@ class nsContextMenu {
if (SCREENSHOT_BROWSER_COMPONENT) {
Services.obs.notifyObservers(window, "menuitem-screenshot", true);
} else {
Services.obs.notifyObservers(null, "menuitem-screenshot-extension", true);
Services.obs.notifyObservers(
null,
"menuitem-screenshot-extension",
"contextMenu"
);
}
}

Expand Down
7 changes: 6 additions & 1 deletion browser/components/customizableui/CustomizableWidgets.jsm
Original file line number Diff line number Diff line change
Expand Up @@ -525,6 +525,7 @@ if (Services.prefs.getBoolPref("identity.fxaccounts.enabled")) {
if (!screenshotsDisabled) {
CustomizableWidgets.push({
id: "screenshot-button",
shortcutId: "key_screenshot",
l10nId: "screenshot-toolbarbutton",
onCommand(aEvent) {
if (SCREENSHOT_BROWSER_COMPONENT) {
Expand All @@ -533,7 +534,11 @@ if (!screenshotsDisabled) {
"menuitem-screenshot"
);
} else {
Services.obs.notifyObservers(null, "menuitem-screenshot-extension");
Services.obs.notifyObservers(
null,
"menuitem-screenshot-extension",
"toolbar"
);
}
},
onCreated(aNode) {
Expand Down
14 changes: 14 additions & 0 deletions browser/components/screenshots/ScreenshotsUtils.jsm
Original file line number Diff line number Diff line change
Expand Up @@ -26,4 +26,18 @@ var ScreenshotsUtils = {
{ features: "resizable=no", sizeTo: "available" }
);
},
notify(window, type) {
if (window.document.getElementById("screenshot-button").disabled) {
return;
}

if (Services.prefs.getBoolPref("screenshots.browser.component.enabled")) {
Services.obs.notifyObservers(
window.event.currentTarget.ownerGlobal,
"menuitem-screenshot"
);
} else {
Services.obs.notifyObservers(null, "menuitem-screenshot-extension", type);
}
},
};
2 changes: 1 addition & 1 deletion browser/extensions/screenshots/background/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ this.main = (function() {
_startShotFlow(tab, "context-menu");
});

exports.onCommand = catcher.watchFunction(tab => {
exports.onShortcut = catcher.watchFunction(tab => {
_startShotFlow(tab, "keyboard-shortcut");
});

Expand Down
37 changes: 8 additions & 29 deletions browser/extensions/screenshots/background/startBackground.js
Original file line number Diff line number Diff line change
Expand Up @@ -64,47 +64,26 @@ this.startBackground = (function() {
];

browser.experiments.screenshots.onScreenshotCommand.addListener(
async isContextMenuClick => {
async type => {
try {
let [[tab]] = await Promise.all([
browser.tabs.query({ currentWindow: true, active: true }),
loadIfNecessary(),
]);
zoomFactor = await browser.tabs.getZoom(tab.id);
isContextMenuClick
? main.onClickedContextMenu(tab)
: main.onClicked(tab);
if (type === "contextMenu") {
main.onClickedContextMenu(tab);
} else if (type === "toolbar") {
main.onClicked(tab);
} else if (type === "shortcut") {
main.onShortcut(tab);
}
} catch (error) {
console.error("Error loading Screenshots:", error);
}
}
);

browser.commands.onCommand.addListener(cmd => {
if (cmd !== "take-screenshot") {
return;
}
loadIfNecessary()
.then(() => {
browser.tabs
.query({ currentWindow: true, active: true })
.then(async tabs => {
const activeTab = tabs[0];
zoomFactor = await browser.tabs.getZoom(activeTab.id);
main.onCommand(activeTab);
})
.catch(error => {
throw error;
});
})
.catch(error => {
console.error(
"Error toggling Screenshots via keyboard shortcut: ",
error
);
});
});

browser.runtime.onMessage.addListener((req, sender, sendResponse) => {
loadIfNecessary()
.then(() => {
Expand Down
4 changes: 2 additions & 2 deletions browser/extensions/screenshots/experiments/screenshots/api.js
Original file line number Diff line number Diff line change
Expand Up @@ -47,8 +47,8 @@ this.screenshots = class extends ExtensionAPI {
name: "experiments.screenshots.onScreenshotCommand",
register: fire => {
let observer = (subject, topic, data) => {
let isContexMenuClick = data;
fire.sync(isContexMenuClick);
let type = data;
fire.sync(type);
};
Services.obs.addObserver(observer, TOPIC);
return () => {
Expand Down
8 changes: 0 additions & 8 deletions browser/extensions/screenshots/manifest.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,14 +18,6 @@
"background/startBackground.js"
]
},
"commands": {
"take-screenshot": {
"suggested_key": {
"default": "Ctrl+Shift+S"
},
"description": "Open the Firefox Screenshots UI"
}
},
"content_scripts": [
{
"matches": ["https://screenshots.firefox.com/*"],
Expand Down
3 changes: 3 additions & 0 deletions browser/locales/en-US/browser/screenshots.ftl
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,9 @@ screenshot-toolbarbutton =
.label = Screenshot
.tooltiptext = Take a screenshot
screenshot-shortcut =
.key = S
screenshots-instructions = Drag or click on the page to select a region. Press ESC to cancel.
screenshots-cancel-button = Cancel
screenshots-save-visible-button = Save visible
Expand Down

0 comments on commit 6fede10

Please sign in to comment.