Skip to content

Commit 2d16349

Browse files
jaredhirschjhirsch@mozilla.com
authored andcommitted
Bug 1988635 - Disable theme colors in forced-colors mode r=morgan,emilio,rpl
Differential Revision: https://phabricator.services.mozilla.com/D269692
1 parent 8da01c8 commit 2d16349

File tree

3 files changed

+65
-6
lines changed

3 files changed

+65
-6
lines changed

toolkit/modules/LightweightThemeConsumer.sys.mjs

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -227,6 +227,9 @@ export function LightweightThemeConsumer(aDocument) {
227227
this.darkThemeMediaQuery = this._win.matchMedia("(-moz-system-dark-theme)");
228228
this.darkThemeMediaQuery.addListener(this);
229229

230+
this.forcedColorsMediaQuery = this._win.matchMedia("(forced-colors)");
231+
this.forcedColorsMediaQuery.addListener(this);
232+
230233
const { LightweightThemeManager } = ChromeUtils.importESModule(
231234
"resource://gre/modules/LightweightThemeManager.sys.mjs"
232235
);
@@ -252,7 +255,10 @@ LightweightThemeConsumer.prototype = {
252255
},
253256

254257
handleEvent(aEvent) {
255-
if (aEvent.target == this.darkThemeMediaQuery) {
258+
if (
259+
aEvent.target == this.darkThemeMediaQuery ||
260+
aEvent.target == this.forcedColorsMediaQuery
261+
) {
256262
this._update(this._lastData);
257263
return;
258264
}
@@ -262,10 +268,10 @@ LightweightThemeConsumer.prototype = {
262268
Services.obs.removeObserver(this, "lightweight-theme-styling-update");
263269
Services.ppmm.sharedData.delete(`theme/${this._winId}`);
264270
this._win = this._doc = null;
265-
if (this.darkThemeMediaQuery) {
266-
this.darkThemeMediaQuery.removeListener(this);
267-
this.darkThemeMediaQuery = null;
268-
}
271+
this.darkThemeMediaQuery?.removeListener(this);
272+
this.darkThemeMediaQuery = null;
273+
this.forcedColorsMediaQuery?.removeListener(this);
274+
this.forcedColorsMediaQuery = null;
269275
break;
270276
}
271277
},
@@ -303,7 +309,7 @@ LightweightThemeConsumer.prototype = {
303309
})();
304310

305311
let theme = useDarkTheme ? themeData.darkTheme : themeData.theme;
306-
if (!theme) {
312+
if (!theme || this.forcedColorsMediaQuery?.matches) {
307313
theme = { id: DEFAULT_THEME_ID };
308314
}
309315
let builtinThemeConfig = lazy.BuiltInThemeConfig.get(theme.id);

toolkit/mozapps/extensions/test/browser/browser.toml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -163,6 +163,9 @@ skip-if = ["os == 'linux' && os_version == '24.04' && processor == 'x86_64' && d
163163

164164
["browser_task_next_test.js"]
165165

166+
["browser_theme_forced_colors.js"]
167+
skip-if = ["os != 'win'"] # Forced-colors is only supported on Windows
168+
166169
["browser_theme_undo.js"]
167170

168171
["browser_updateid.js"]
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
/* Any copyright is dedicated to the Public Domain.
2+
https://creativecommons.org/publicdomain/zero/1.0/ */
3+
4+
"use strict";
5+
6+
const { LightweightThemeManager } = ChromeUtils.importESModule(
7+
"resource://gre/modules/LightweightThemeManager.sys.mjs"
8+
);
9+
10+
add_task(async function test_forced_colors_themes() {
11+
await SpecialPowers.pushPrefEnv({
12+
set: [["ui.useAccessibilityTheme", 0]],
13+
});
14+
15+
const THEME_ID = "theme@mochi.test";
16+
let theme = ExtensionTestUtils.loadExtension({
17+
manifest: {
18+
browser_specific_settings: { gecko: { id: THEME_ID } },
19+
name: "test theme",
20+
theme: {
21+
colors: {
22+
frame: "red",
23+
tab_background_text: "blue",
24+
},
25+
},
26+
},
27+
useAddonManager: "temporary",
28+
});
29+
await theme.startup();
30+
31+
let docEl = window.document.documentElement;
32+
Assert.ok(docEl.hasAttribute("lwtheme"), "LWT attribute should be set");
33+
34+
await SpecialPowers.pushPrefEnv({
35+
set: [["ui.useAccessibilityTheme", 1]],
36+
});
37+
38+
Assert.equal(
39+
LightweightThemeManager.themeData.theme.id,
40+
THEME_ID,
41+
"The theme manager should indicate that the theme is still active"
42+
);
43+
Assert.ok(!docEl.hasAttribute("lwtheme"), "LWT attribute should not be set");
44+
Assert.ok(
45+
window.matchMedia("(forced-colors)").matches,
46+
"should be in forced-colors mode"
47+
);
48+
49+
await theme.unload();
50+
});

0 commit comments

Comments
 (0)