Skip to content

Commit

Permalink
🤝 Add TooltipLabelProvider module for building tooltip texts using va…
Browse files Browse the repository at this point in the history
…rious inputs
  • Loading branch information
kierandrewett committed Feb 8, 2024
1 parent 425cec2 commit cbf5c31
Show file tree
Hide file tree
Showing 5 changed files with 64 additions and 21 deletions.
2 changes: 2 additions & 0 deletions components/moz.build
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,11 @@ DIRS += [
"settings",
"shortcuts",
"startpage",
"status",
"storage",
"tabs",
"toolbar",
"tooltips",
"widgets",
]

Expand Down
43 changes: 43 additions & 0 deletions components/tooltips/TooltipLabelProvider.sys.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
/* 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 tooltipL10n = new Localization(["dot/tooltips.ftl"], true);

export const TooltipLabelProvider = {
/**
* Constructs the tooltip text using the supplied values
* @param {string} label
* @param {object} [options]
* @param {string} [options.sublabel]
* @param {string} [options.shortcut]
*/
getTooltipText(label, options) {
let tooltipText = [];

if (options && options.shortcut) {
tooltipText.push(
tooltipL10n.formatValueSync("tooltip-shortcut-label", {
label,
shortcut: options.shortcut
})
);
} else {
tooltipText.push(
tooltipL10n.formatValueSync("tooltip-label", {
label
})
);
}

if (options && options.sublabel) {
tooltipText.push(
tooltipL10n.formatValueSync("tooltip-label", {
label: options.sublabel
})
);
}

return tooltipText.join("\n");
}
};
27 changes: 8 additions & 19 deletions components/widgets/content/browser-button.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@
* 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/. */

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

class BrowserButton extends BrowserContextualMixin(HTMLButtonElement) {
constructor() {
super();
Expand Down Expand Up @@ -103,8 +107,6 @@ class BrowserButton extends BrowserContextualMixin(HTMLButtonElement) {
* Updates the label of the browser button
*/
set label(newLabel) {
if (newLabel == this.label) return;

this.setAttribute("label", newLabel);
this.elements.label.textContent = newLabel;

Expand All @@ -122,8 +124,6 @@ class BrowserButton extends BrowserContextualMixin(HTMLButtonElement) {
* Updates the auxiliary label of the browser button
*/
set labelAuxiliary(newLabelAuxiliary) {
if (newLabelAuxiliary == this.labelAuxiliary) return;

this.setAttribute("labelauxiliary", newLabelAuxiliary);

this._updateTooltipText();
Expand Down Expand Up @@ -229,31 +229,20 @@ class BrowserButton extends BrowserContextualMixin(HTMLButtonElement) {
* Computes the tooltip text for this button
*/
getTooltipText() {
let tooltipText = "";

const label = this.getAttribute("label");
const labelAuxiliary = this.getAttribute("labelauxiliary");
const accelerator = this.getAttribute("accelerator");

if (labelAuxiliary) {
tooltipText = labelAuxiliary;
} else if (label) {
tooltipText = label;
}

if (accelerator) {
tooltipText += ` (${accelerator})`;
}

return tooltipText;
return TooltipLabelProvider.getTooltipText(
labelAuxiliary || label || "",
{ shortcut: accelerator }
);
}

/**
* Updates the button's tooltip text
*/
_updateTooltipText() {
if (this.elements.tooltip.state == "closed") return;

this.elements.tooltip.label = this.getTooltipText();
}

Expand Down
2 changes: 0 additions & 2 deletions locales/en-US/dot/browser.ftl
Original file line number Diff line number Diff line change
Expand Up @@ -45,5 +45,3 @@ browser-window-restore-button =
browser-window-close-button =
.title = Close
browser-keybind-label = { $label } ({ $keybind })
11 changes: 11 additions & 0 deletions locales/en-US/dot/tooltips.ftl
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
# 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/.

tooltip-label = { $label }
tooltip-shortcut-label =
{ PLATFORM() ->
[macos] { $label } { $shortcut }
*[other] { $label } ({ $shortcut })
}

0 comments on commit cbf5c31

Please sign in to comment.