summaryrefslogtreecommitdiffstats
path: root/comm/mail/components/unifiedtoolbar/modules/CustomizationState.mjs
blob: 75c0b390be618e47f881b197a9faaffeeb7c1cef (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
/* 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 MAIN_WINDOW_DOCUMENT = "chrome://messenger/content/messenger.xhtml";
const UNIFIED_TOOLBAR_ID = "unifiedToolbar";
const CUSTOMIZATION_ATTRIBUTE_NAME = "state";

/**
 * @typedef {object} UnifiedToolbarCustomizationState
 * @property {string[]} (spaceName) - Each space has a key on the object,
 *   containing an ordered array of item IDs.
 */

/**
 * Store the customization state for the unified toolbar. Sends a global
 * observer notification.
 *
 * @param {UnifiedToolbarCustomizationState} state
 */
export function storeState(state) {
  Services.xulStore.setValue(
    MAIN_WINDOW_DOCUMENT,
    UNIFIED_TOOLBAR_ID,
    CUSTOMIZATION_ATTRIBUTE_NAME,
    JSON.stringify(state)
  );
  Services.obs.notifyObservers(null, "unified-toolbar-state-change");
}

/**
 * Retrieve the customization state of the unified toolbar.
 *
 * @returns {UnifiedToolbarCustomizationState} A partial representation of the
 *   customization state of the unified toolbar. Missing spaces are in their
 *   default states.
 */
export function getState() {
  let state = {};
  if (
    Services.xulStore.hasValue(
      MAIN_WINDOW_DOCUMENT,
      UNIFIED_TOOLBAR_ID,
      CUSTOMIZATION_ATTRIBUTE_NAME
    )
  ) {
    const rawState = Services.xulStore.getValue(
      MAIN_WINDOW_DOCUMENT,
      UNIFIED_TOOLBAR_ID,
      CUSTOMIZATION_ATTRIBUTE_NAME
    );
    state = JSON.parse(rawState);
  }
  return state;
}