Skip to content

Commit

Permalink
✨ Add add-tab action and command to link to add-tab-button
Browse files Browse the repository at this point in the history
  • Loading branch information
kierandrewett committed Nov 25, 2023
1 parent c12c8dc commit 01d93ba
Show file tree
Hide file tree
Showing 5 changed files with 96 additions and 2 deletions.
1 change: 1 addition & 0 deletions components/actions/ActionRegistry.sys.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ const { Action } = ChromeUtils.importESModule(

const ALL_ACTIONS = [
// browser.tabs
"browser.tabs.add_tab",
"browser.tabs.go_back",
"browser.tabs.go_forward",
"browser.tabs.reload_page",
Expand Down
50 changes: 50 additions & 0 deletions components/actions/content/browser/tabs/add_tab.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */

/**
* @typedef {import("components/actions/Action.sys.mjs").ActionDispatchEvent<T>} ActionDispatchEvent
* @template T
*/

const { Action } = ChromeUtils.importESModule(
"resource://gre/modules/Action.sys.mjs"
);

export class BrowserTabsAddTabAction extends Action {
static id = "browser.tabs.add_tab";

constructor() {
super();
}

/**
* The name of this action
*/
get name() {
return "Create tab";
}

/**
* Performs this action
* @param {ActionDispatchEvent<{ url: string | string[] }>} event
*/
run(event) {
const { args } = event.detail;

if (!args.url) throw new Error("No 'url' argument supplied!");

const win = event.target.ownerGlobal;
const { gDot } = win;

const params = {
triggeringPrincipal:
Services.scriptSecurityManager.getSystemPrincipal()
};

gDot.tabs.createTabs(
Array.isArray(args.url) ? args.url : [args.url],
params
);
}
}
2 changes: 1 addition & 1 deletion components/commands/BrowserCommands.sys.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ const { CommandSubscription } = ChromeUtils.importESModule(
"resource://gre/modules/CommandSubscription.sys.mjs"
);

const ALL_COMMANDS = ["go-back", "go-forward", "reload-tab"];
const ALL_COMMANDS = ["add-tab", "go-back", "go-forward", "reload-tab"];

export class BrowserCommands {
/** @type {Window} */
Expand Down
42 changes: 42 additions & 0 deletions components/commands/content/add-tab.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */

const { Command } = ChromeUtils.importESModule(
"resource://gre/modules/Command.sys.mjs"
);

const { StartPage } = ChromeUtils.importESModule(
"resource:///modules/StartPage.sys.mjs"
);

export class AddTabCommand extends Command {
constructor(subscription, area) {
super(subscription, area);

this.label = "New Tab";
this.label_auxiliary = "Open a new tab";
this.icon = "add";
}

/**
* Performs this command
*
* @param {{ urls?: string[], url?: string }} args
*/
run(args) {
let urls = [];

if (args.url || args.urls) {
if (args.url) urls.push(args.url);

urls = urls.concat(args.urls || []);
} else {
urls = StartPage.getHomePage();
}

for (const url of urls) {
this.actions.run("browser.tabs.add_tab", { url });
}
}
}
3 changes: 2 additions & 1 deletion components/tabs/content/browser-add-tab-button.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@ class BrowserAddTabButton extends BrowserToolbarButton {
constructor() {
super();

this.routineId = "new-tab";
this.buttonId = "add-tab-button";
this.commandId = "add-tab";
}

connectedCallback() {
Expand Down

0 comments on commit 01d93ba

Please sign in to comment.