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/. */
6 * Provide unit converter.
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";
17 } from "resource:///modules/UrlbarUtils.sys.mjs";
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",
27 XPCOMUtils.defineLazyServiceGetter(
30 "@mozilla.org/widget/clipboardhelper;1",
35 new UnitConverterSimple(),
36 new UnitConverterTemperature(),
37 new UnitConverterTimezone(),
40 const DYNAMIC_RESULT_TYPE = "unitConversion";
41 const VIEW_TEMPLATE = {
49 classList: ["urlbarView-no-wrap"],
54 classList: ["urlbarView-favicon"],
56 src: "chrome://global/skin/icons/edit-copy.svg",
73 * Provide a feature that converts given units.
75 class ProviderUnitConversion extends UrlbarProvider {
78 lazy.UrlbarResult.addDynamicResultType(DYNAMIC_RESULT_TYPE);
79 lazy.UrlbarView.addDynamicViewTemplate(DYNAMIC_RESULT_TYPE, VIEW_TEMPLATE);
83 * Returns the name of this provider.
85 * @returns {string} the name of this provider.
88 return "UnitConversion";
92 * Returns the type of this provider.
94 * @returns {integer} one of the types from UrlbarUtils.PROVIDER_TYPE.*
97 return UrlbarUtils.PROVIDER_TYPE.PROFILE;
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.
105 * @param {UrlbarQueryContext} queryContext
106 * The query context object.
108 * Whether this provider should be invoked for the search.
110 isActive({ searchString }) {
111 if (!lazy.UrlbarPrefs.get("unitConversion.enabled")) {
115 for (const converter of CONVERTERS) {
116 const result = converter.convert(searchString);
118 this._activeResult = result;
123 this._activeResult = null;
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.
132 * @param {UrlbarResult} result The result whose view will be updated.
133 * @returns {object} An object describing the view update.
135 getViewUpdate(result) {
138 textContent: result.payload.output,
141 l10n: { id: "urlbar-result-action-copy-to-clipboard" },
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.
150 * @param {UrlbarQueryContext} queryContext
151 * The query context object.
152 * @param {Function} addCallback
153 * The callback invoked by this method to add each result.
155 startQuery(queryContext, addCallback) {
156 const result = new lazy.UrlbarResult(
157 UrlbarUtils.RESULT_TYPE.DYNAMIC,
158 UrlbarUtils.RESULT_SOURCE.OTHER_LOCAL,
160 dynamicType: DYNAMIC_RESULT_TYPE,
161 output: this._activeResult,
162 input: queryContext.searchString,
165 result.suggestedIndex = lazy.UrlbarPrefs.get(
166 "unitConversion.suggestedIndex"
169 addCallback(this, result);
172 onEngagement(queryContext, controller, details) {
173 let { element } = details;
174 const { textContent } = element.querySelector(
175 ".urlbarView-dynamic-unitConversion-output"
177 lazy.ClipboardHelper.copyString(textContent);
181 export const UrlbarProviderUnitConversion = new ProviderUnitConversion();