Add context menu to receiver selector + other improvements

This commit is contained in:
hensm
2022-05-09 23:45:20 +01:00
parent b1100bd258
commit 5f96e6ef29
9 changed files with 444 additions and 315 deletions

View File

@@ -26,7 +26,6 @@ export interface ReceiverSelectionCast {
actionType: ReceiverSelectionActionType.Cast;
receiverDevice: ReceiverDevice;
mediaType: ReceiverSelectorMediaType;
filePath?: string;
}
export interface ReceiverSelectionStop {
actionType: ReceiverSelectionActionType.Stop;
@@ -39,6 +38,7 @@ interface ReceiverSelectorEvents {
error: string;
cancelled: void;
stop: ReceiverSelectionStop;
close: void;
}
/**
* Manages the receiver selector popup window and communication with the
@@ -268,6 +268,8 @@ export default class ReceiverSelector extends TypedEventTarget<ReceiverSelectorE
this.dispatchEvent(new CustomEvent("cancelled"));
}
this.dispatchEvent(new CustomEvent("close"));
// Cleanup
delete this.windowId;
delete this.messagePort;

View File

@@ -30,6 +30,9 @@ let menuIdCastMedia: MenuId;
let menuIdWhitelist: MenuId;
let menuIdWhitelistRecommended: MenuId;
export const menuIdPopupCast = "popup_cast";
export const menuIdPopupStop = "popup_stop";
/** Match patterns for the whitelist option menus. */
const whitelistChildMenuPatterns = new Map<MenuId, string>();
@@ -42,7 +45,9 @@ export async function initMenus() {
// Global "Cast..." menu item
menuIdCast = browser.menus.create({
contexts: ["browser_action", "page", "tools_menu"],
title: _("contextCast")
title: _("contextCast"),
documentUrlPatterns: ["http://*/*", "https://*/*"],
icons: { "16": "icons/icon.svg" } // browser_action context
});
// <video>/<audio> "Cast..." context menu item
@@ -71,6 +76,19 @@ export async function initMenus() {
parentId: menuIdWhitelist
});
// Popup context menus
const popupUrlPattern = `${browser.runtime.getURL("ui/popup")}/*`;
browser.menus.create({
id: menuIdPopupCast,
title: _("popupCastButtonTitle"),
documentUrlPatterns: [popupUrlPattern]
});
browser.menus.create({
id: menuIdPopupStop,
title: _("popupStopButtonTitle"),
documentUrlPatterns: [popupUrlPattern]
});
browser.menus.onShown.addListener(onMenuShown);
browser.menus.onClicked.addListener(onMenuClicked);

View File

@@ -126,12 +126,10 @@ async function getSelection(
function onSelectorSelected(ev: CustomEvent<ReceiverSelectionCast>) {
logger.info("Selected receiver", ev.detail);
removeListeners();
resolve({
actionType: ReceiverSelectionActionType.Cast,
receiverDevice: ev.detail.receiverDevice,
mediaType: ev.detail.mediaType,
filePath: ev.detail.filePath
mediaType: ev.detail.mediaType
});
}
function onSelectorStop(ev: CustomEvent<ReceiverSelectionStop>) {
@@ -139,7 +137,6 @@ async function getSelection(
deviceManager.stopReceiverApp(ev.detail.receiverDevice.id);
removeListeners();
resolve({
actionType: ReceiverSelectionActionType.Stop,
receiverDevice: ev.detail.receiverDevice
@@ -148,11 +145,9 @@ async function getSelection(
function onSelectorCancelled() {
logger.info("Cancelled receiver selection");
removeListeners();
resolve(null);
}
function onSelectorError(ev: CustomEvent<string>) {
removeListeners();
reject(ev.detail);
}
@@ -160,6 +155,7 @@ async function getSelection(
sharedSelector.addEventListener("stop", onSelectorStop);
sharedSelector.addEventListener("cancelled", onSelectorCancelled);
sharedSelector.addEventListener("error", onSelectorError);
sharedSelector.addEventListener("close", removeListeners);
function removeListeners() {
sharedSelector.removeEventListener("selected", onSelectorSelected);
@@ -169,6 +165,7 @@ async function getSelection(
onSelectorCancelled
);
sharedSelector.removeEventListener("error", onSelectorError);
sharedSelector.removeEventListener("close", removeListeners);
deviceManager.removeEventListener(
"receiverDeviceUp",