summaryrefslogtreecommitdiffstats
path: root/comm/mail/base/content/shortcutsOverlay.js
blob: 85bf20b5ec02e0a6746ac52802060b3626dd01ac (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
/* 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/. */

"use strict";

// Wrap in a block to prevent leaking to window scope.
{
  var { XPCOMUtils } = ChromeUtils.importESModule(
    "resource://gre/modules/XPCOMUtils.sys.mjs"
  );

  XPCOMUtils.defineLazyModuleGetters(this, {
    ShortcutsManager: "resource:///modules/ShortcutsManager.jsm",
  });

  function setupShortcuts() {
    // Set up all dedicated shortcuts.
    setupSpacesShortcuts();

    // Set up the event listener.
    setupEventListener();
  }

  /**
   * Use the ShortcutManager to set up all keyboard shortcuts for the spaces
   * toolbar buttons.
   */
  async function setupSpacesShortcuts() {
    // Set up all shortcut strings for the various spaces buttons.
    let buttons = {
      "space-toggle": ["collapseButton", "spacesToolbarReveal"],
      "space-mail": ["mailButton"],
      "space-addressbook": ["addressBookButton"],
      "space-calendar": ["calendarButton"],
      "space-tasks": ["tasksButton"],
      "space-chat": ["chatButton"],
    };
    for (let [string, ids] of Object.entries(buttons)) {
      let shortcut = await ShortcutsManager.getShortcutStrings(string);
      if (!shortcut) {
        continue;
      }

      for (let id of ids) {
        let button = document.getElementById(id);
        button.setAttribute("aria-label", button.title);
        document.l10n.setAttributes(button, "button-shortcut-string", {
          title: button.title,
          shortcut: shortcut.localizedShortcut,
        });
        button.setAttribute("aria-keyshortcuts", shortcut.ariaKeyShortcuts);
      }
    }

    // Set up all shortcut strings for the various spaces menuitems.
    let menuitems = {
      "space-toggle": ["spacesPopupButtonReveal"],
      "space-mail": ["spacesPopupButtonMail"],
      "space-addressbook": ["spacesPopupButtonAddressBook"],
      "space-calendar": [
        "spacesPopupButtonCalendar",
        "calMenuSwitchToCalendar",
      ],
      "space-tasks": ["spacesPopupButtonTasks", "calMenuSwitchToTask"],
      "space-chat": ["spacesPopupButtonChat", "menu_goChat"],
    };
    for (let [string, ids] of Object.entries(menuitems)) {
      let shortcut = await ShortcutsManager.getShortcutStrings(string);
      if (!shortcut) {
        continue;
      }

      for (let id of ids) {
        let menuitem = document.getElementById(id);
        if (!menuitem.label) {
          await document.l10n.translateElements([menuitem]);
        }
        document.l10n.setAttributes(menuitem, "menuitem-shortcut-string", {
          label: menuitem.label,
          shortcut: shortcut.localizedShortcut,
        });
      }
    }
  }

  /**
   * Set up the keydown event to intercept shortcuts.
   */
  function setupEventListener() {
    let tabmail = document.getElementById("tabmail");

    window.addEventListener("keydown", event => {
      let shortcut = ShortcutsManager.matches(event);
      // FIXME: Temporarily ignore numbers coming from the Numpad to prevent
      // hijacking Alt characters typing in Windows. This can be removed once
      // we implement customizable shortcuts.
      if (!shortcut || event.location == 3) {
        return;
      }
      event.preventDefault();
      event.stopPropagation();

      switch (shortcut.id) {
        case "space-toggle":
          window.gSpacesToolbar.toggleToolbar(!window.gSpacesToolbar.isHidden);
          break;
        case "space-mail":
        case "space-addressbook":
        case "space-calendar":
        case "space-tasks":
        case "space-chat":
          let space = window.gSpacesToolbar.spaces.find(
            space => space.name == shortcut.id.replace("space-", "")
          );
          window.gSpacesToolbar.openSpace(tabmail, space);
          break;
      }
    });
  }

  window.addEventListener("load", setupShortcuts);
}