Bug 1944627 - update sidebar button checked state for non-revamped sidebar cases...
[gecko.git] / browser / components / urlbar / UrlbarProviderUnitConversion.sys.mjs
blob13b18a525c2c90ee625aae7e591b0149d1dc3a10
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/. */
5 /**
6  * Provide unit converter.
7  */
9 import { XPCOMUtils } from "resource://gre/modules/XPCOMUtils.sys.mjs";
11 import { UnitConverterSimple } from "resource:///modules/UnitConverterSimple.sys.mjs";
12 import { UnitConverterTemperature } from "resource:///modules/UnitConverterTemperature.sys.mjs";
13 import { UnitConverterTimezone } from "resource:///modules/UnitConverterTimezone.sys.mjs";
14 import {
15   UrlbarProvider,
16   UrlbarUtils,
17 } from "resource:///modules/UrlbarUtils.sys.mjs";
19 const lazy = {};
21 ChromeUtils.defineESModuleGetters(lazy, {
22   UrlbarPrefs: "resource:///modules/UrlbarPrefs.sys.mjs",
23   UrlbarResult: "resource:///modules/UrlbarResult.sys.mjs",
24   UrlbarView: "resource:///modules/UrlbarView.sys.mjs",
25 });
27 XPCOMUtils.defineLazyServiceGetter(
28   lazy,
29   "ClipboardHelper",
30   "@mozilla.org/widget/clipboardhelper;1",
31   "nsIClipboardHelper"
34 const CONVERTERS = [
35   new UnitConverterSimple(),
36   new UnitConverterTemperature(),
37   new UnitConverterTimezone(),
40 const DYNAMIC_RESULT_TYPE = "unitConversion";
41 const VIEW_TEMPLATE = {
42   attributes: {
43     selectable: true,
44   },
45   children: [
46     {
47       name: "content",
48       tag: "span",
49       classList: ["urlbarView-no-wrap"],
50       children: [
51         {
52           name: "icon",
53           tag: "img",
54           classList: ["urlbarView-favicon"],
55           attributes: {
56             src: "chrome://global/skin/icons/edit-copy.svg",
57           },
58         },
59         {
60           name: "output",
61           tag: "strong",
62         },
63         {
64           name: "action",
65           tag: "span",
66         },
67       ],
68     },
69   ],
72 /**
73  * Provide a feature that converts given units.
74  */
75 class ProviderUnitConversion extends UrlbarProvider {
76   constructor() {
77     super();
78     lazy.UrlbarResult.addDynamicResultType(DYNAMIC_RESULT_TYPE);
79     lazy.UrlbarView.addDynamicViewTemplate(DYNAMIC_RESULT_TYPE, VIEW_TEMPLATE);
80   }
82   /**
83    * Returns the name of this provider.
84    *
85    * @returns {string} the name of this provider.
86    */
87   get name() {
88     return "UnitConversion";
89   }
91   /**
92    * Returns the type of this provider.
93    *
94    * @returns {integer} one of the types from UrlbarUtils.PROVIDER_TYPE.*
95    */
96   get type() {
97     return UrlbarUtils.PROVIDER_TYPE.PROFILE;
98   }
100   /**
101    * Whether the provider should be invoked for the given context.  If this
102    * method returns false, the providers manager won't start a query with this
103    * provider, to save on resources.
104    *
105    * @param {UrlbarQueryContext} queryContext
106    *   The query context object.
107    * @returns {boolean}
108    *   Whether this provider should be invoked for the search.
109    */
110   isActive({ searchString }) {
111     if (!lazy.UrlbarPrefs.get("unitConversion.enabled")) {
112       return false;
113     }
115     for (const converter of CONVERTERS) {
116       const result = converter.convert(searchString);
117       if (result) {
118         this._activeResult = result;
119         return true;
120       }
121     }
123     this._activeResult = null;
124     return false;
125   }
127   /**
128    * This is called only for dynamic result types, when the urlbar view updates
129    * the view of one of the results of the provider.  It should return an object
130    * describing the view update.
131    *
132    * @param {UrlbarResult} result The result whose view will be updated.
133    * @returns {object} An object describing the view update.
134    */
135   getViewUpdate(result) {
136     return {
137       output: {
138         textContent: result.payload.output,
139       },
140       action: {
141         l10n: { id: "urlbar-result-action-copy-to-clipboard" },
142       },
143     };
144   }
146   /**
147    * This method is called by the providers manager when a query starts to fetch
148    * each extension provider's results.  It fires the resultsRequested event.
149    *
150    * @param {UrlbarQueryContext} queryContext
151    *   The query context object.
152    * @param {Function} addCallback
153    *   The callback invoked by this method to add each result.
154    */
155   startQuery(queryContext, addCallback) {
156     const result = new lazy.UrlbarResult(
157       UrlbarUtils.RESULT_TYPE.DYNAMIC,
158       UrlbarUtils.RESULT_SOURCE.OTHER_LOCAL,
159       {
160         dynamicType: DYNAMIC_RESULT_TYPE,
161         output: this._activeResult,
162         input: queryContext.searchString,
163       }
164     );
165     result.suggestedIndex = lazy.UrlbarPrefs.get(
166       "unitConversion.suggestedIndex"
167     );
169     addCallback(this, result);
170   }
172   onEngagement(queryContext, controller, details) {
173     let { element } = details;
174     const { textContent } = element.querySelector(
175       ".urlbarView-dynamic-unitConversion-output"
176     );
177     lazy.ClipboardHelper.copyString(textContent);
178   }
181 export const UrlbarProviderUnitConversion = new ProviderUnitConversion();