UrlbarProviderActionsSearchMode.sys.mjs (3224B)
1 /* This Source Code Form is subject to the terms of the Mozilla Public 2 * License, v. 2.0. If a copy of the MPL was not distributed with this 3 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ 4 5 /** 6 * This module exports a provider that returns all the available 7 * actions for an actions search mode. 8 */ 9 10 import { 11 UrlbarProvider, 12 UrlbarUtils, 13 } from "moz-src:///browser/components/urlbar/UrlbarUtils.sys.mjs"; 14 15 const lazy = {}; 16 17 // Default icon shown for actions if no custom one is provided. 18 const DEFAULT_ICON = "chrome://global/skin/icons/settings.svg"; 19 const DYNAMIC_TYPE_NAME = "actions"; 20 21 ChromeUtils.defineESModuleGetters(lazy, { 22 ActionsProviderQuickActions: 23 "moz-src:///browser/components/urlbar/ActionsProviderQuickActions.sys.mjs", 24 UrlbarResult: "moz-src:///browser/components/urlbar/UrlbarResult.sys.mjs", 25 }); 26 27 /** 28 * A provider that lets the user view all available actions while in searchMode. 29 */ 30 export class UrlbarProviderActionsSearchMode extends UrlbarProvider { 31 /** 32 * @returns {Values<typeof UrlbarUtils.PROVIDER_TYPE>} 33 */ 34 get type() { 35 return UrlbarUtils.PROVIDER_TYPE.PROFILE; 36 } 37 38 async isActive(queryContext) { 39 return queryContext.searchMode?.source == UrlbarUtils.RESULT_SOURCE.ACTIONS; 40 } 41 42 /** 43 * Starts querying. 44 * 45 * @param {UrlbarQueryContext} queryContext 46 * @param {(provider: UrlbarProvider, result: UrlbarResult) => void} addCallback 47 * Callback invoked by the provider to add a new result. 48 */ 49 async startQuery(queryContext, addCallback) { 50 let input = queryContext.trimmedLowerCaseSearchString; 51 let results = await lazy.ActionsProviderQuickActions.getActions({ 52 input, 53 includesExactMatch: true, 54 }); 55 results.forEach(resultKey => { 56 let result = new lazy.UrlbarResult({ 57 type: UrlbarUtils.RESULT_TYPE.DYNAMIC, 58 source: UrlbarUtils.RESULT_SOURCE.ACTIONS, 59 payload: { 60 key: resultKey, 61 dynamicType: DYNAMIC_TYPE_NAME, 62 }, 63 }); 64 addCallback(this, result); 65 }); 66 } 67 68 onEngagement(queryContext, controller, details) { 69 lazy.ActionsProviderQuickActions.pickAction( 70 queryContext, 71 controller, 72 details.element 73 ); 74 } 75 76 getViewTemplate(result) { 77 let action = lazy.ActionsProviderQuickActions.getAction(result.payload.key); 78 let inActive = "isActive" in action && !action.isActive(); 79 return { 80 children: [ 81 { 82 tag: "span", 83 classList: ["urlbarView-action-btn"], 84 attributes: { 85 "data-action": result.payload.key, 86 "data-input-length": result.payload.inputLength, 87 role: inActive ? "" : "button", 88 disabled: inActive, 89 }, 90 children: [ 91 { 92 tag: "img", 93 attributes: { 94 src: action.icon || DEFAULT_ICON, 95 }, 96 }, 97 { 98 name: `label`, 99 tag: "span", 100 }, 101 ], 102 }, 103 ], 104 }; 105 } 106 107 getViewUpdate(result) { 108 let action = lazy.ActionsProviderQuickActions.getAction(result.payload.key); 109 110 return { 111 label: { 112 l10n: { id: action.label }, 113 }, 114 }; 115 } 116 }