summaryrefslogtreecommitdiffstats
path: root/comm/mail/modules/TagUtils.jsm
blob: 05747e7b8cd1547322f6750afb49202ed536903d (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
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
/* 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 { MailServices } = ChromeUtils.import(
  "resource:///modules/MailServices.jsm"
);

const lazy = {};

ChromeUtils.defineESModuleGetters(lazy, {
  Color: "resource://gre/modules/Color.sys.mjs",
});

var EXPORTED_SYMBOLS = ["TagUtils"];

var TagUtils = {
  loadTagsIntoCSS,
  addTagToAllDocumentSheets,
  isColorContrastEnough,
};

function loadTagsIntoCSS(aDocument) {
  let tagSheet = findTagColorSheet(aDocument);
  let tagArray = MailServices.tags.getAllTags();
  for (let tag of tagArray) {
    // tag.key is the internal key, like "$label1" for "Important".
    // For user defined keys with non-ASCII characters, key is
    // the MUTF-7 encoded name.
    addTagToSheet(tag.key, tag.color, tagSheet);
  }
}

function addTagToAllDocumentSheets(aKey, aColor) {
  for (let nextWin of Services.wm.getEnumerator("mail:3pane", true)) {
    addTagToSheet(aKey, aColor, findTagColorSheet(nextWin.document));
  }

  for (let nextWin of Services.wm.getEnumerator("mailnews:search", true)) {
    addTagToSheet(aKey, aColor, findTagColorSheet(nextWin.document));
  }
}

function addTagToSheet(aKey, aColor, aSheet) {
  if (!aSheet) {
    return;
  }

  // Add rules to sheet.
  let ruleString1;
  let ruleString2;
  let ruleString3;
  let ruleString4;
  let selector = MailServices.tags.getSelectorForKey(aKey);
  if (!aColor) {
    ruleString1 =
      ":root[lwt-tree] treechildren::-moz-tree-row(" +
      selector +
      ", selected, focus) { background-color: " +
      "var(--sidebar-highlight-background-color) !important; }";
    ruleString2 =
      "treechildren::-moz-tree-cell-text(" +
      selector +
      ", selected, focus) { color: SelectedItemText !important; }";
    ruleString3 =
      "tree:-moz-lwtheme treechildren::-moz-tree-cell-text(" +
      selector +
      ", selected) { color: currentColor !important; }";
    ruleString4 =
      ":root[lwt-tree] treechildren::-moz-tree-cell-text(" +
      selector +
      ", selected, focus) { color: var(--sidebar-highlight-text-color, " +
      "var(--sidebar-text-color)) !important; }";
  } else {
    ruleString1 =
      "treechildren::-moz-tree-row(" +
      selector +
      ", selected, focus) { background-color: " +
      aColor +
      " !important; outline-color: color-mix(in srgb, " +
      aColor +
      ", black 25%); }";
    ruleString2 =
      "treechildren::-moz-tree-cell-text(" +
      selector +
      ") { color: " +
      aColor +
      "; }";
    let textColor = "black";
    if (!isColorContrastEnough(aColor)) {
      textColor = "white";
    }
    ruleString3 =
      "treechildren::-moz-tree-cell-text(" +
      selector +
      ", selected, focus) { color: " +
      textColor +
      " }";
    ruleString4 =
      "treechildren::-moz-tree-image(" +
      selector +
      ", selected, focus)," +
      "treechildren::-moz-tree-twisty(" +
      selector +
      ", selected, focus) { --select-focus-text-color: " +
      textColor +
      "; }";
  }
  try {
    aSheet.insertRule(ruleString1, aSheet.cssRules.length);
    aSheet.insertRule(ruleString2, aSheet.cssRules.length);
    aSheet.insertRule(ruleString3, aSheet.cssRules.length);
    aSheet.insertRule(ruleString4, aSheet.cssRules.length);
  } catch (ex) {
    aSheet.ownerNode.addEventListener(
      "load",
      () => addTagToSheet(aKey, aColor, aSheet),
      { once: true }
    );
  }
}

function findTagColorSheet(aDocument) {
  const cssUri = "chrome://messenger/skin/tagColors.css";
  let tagSheet = null;
  for (let sheet of aDocument.styleSheets) {
    if (sheet.href == cssUri) {
      tagSheet = sheet;
      break;
    }
  }
  if (!tagSheet) {
    console.error("TagUtils.findTagColorSheet: tagColors.css not found");
  }
  return tagSheet;
}

/* Checks if black writing on 'aColor' background has enough contrast */
function isColorContrastEnough(aColor) {
  // Is a color set? If not, return "true" to use the default color.
  if (!aColor) {
    return true;
  }
  // Zero-pad the number just to make sure that it is 8 digits.
  let colorHex = ("00000000" + aColor).substr(-8);
  let colorArray = colorHex.match(/../g);
  let [, cR, cG, cB] = colorArray.map(val => parseInt(val, 16));
  return new lazy.Color(cR, cG, cB).isContrastRatioAcceptable(
    new lazy.Color(0, 0, 0),
    "AAA"
  );
}