Skip to content

Commit

Permalink
Add emulation for inverted-colors to web inspector
Browse files Browse the repository at this point in the history
https://bugs.webkit.org/show_bug.cgi?id=259418

Reviewed by NOBODY (OOPS!).

Web inspector now allows overriding the inverted-colors media query.

* LayoutTests/inspector/page/overrideUserPreference-expected.txt:
* LayoutTests/inspector/page/overrideUserPreference.html:
* Source/JavaScriptCore/inspector/protocol/Page.json:
* Source/WebCore/inspector/agents/InspectorPageAgent.cpp:
(WebCore::InspectorPageAgent::disable):
(WebCore::InspectorPageAgent::overrideUserPreference):
(WebCore::InspectorPageAgent::overrideInvertedColors):
(WebCore::InspectorPageAgent::defaultUserPreferencesDidChange):
* Source/WebCore/inspector/agents/InspectorPageAgent.h:
* Source/WebInspectorUI/Localizations/en.lproj/localizedStrings.js:
* Source/WebInspectorUI/UserInterface/Views/OverrideUserPreferencesPopover.js:
(WI.OverrideUserPreferencesPopover.prototype.createContentElement):
(WI.OverrideUserPreferencesPopover.prototype._userPreferenceValueLabel):
  • Loading branch information
lukewarlow committed Jul 22, 2023
1 parent bd0d59d commit 08cc52f
Show file tree
Hide file tree
Showing 7 changed files with 94 additions and 3 deletions.
13 changes: 13 additions & 0 deletions LayoutTests/inspector/page/overrideUserPreference-expected.txt
Original file line number Diff line number Diff line change
Expand Up @@ -41,3 +41,16 @@ Removing PrefersColorScheme override
PASS: (prefers-color-scheme: dark) media query does not match.
PASS: --test-prefers-color-scheme: light

-- Running test case: Page.overrideUserPreference.InvertedColors
PASS: (inverted-colors: inverted) media query does not match.
PASS: --test-inverted-colors: none
Overriding InvertedColors value to Inverted
PASS: (inverted-colors: inverted) media query matches.
PASS: --test-inverted-colors: inverted
Overriding InvertedColors value to None
PASS: (inverted-colors: inverted) media query does not match.
PASS: --test-inverted-colors: none
Removing InvertedColors override
PASS: (inverted-colors: inverted) media query does not match.
PASS: --test-inverted-colors: none

35 changes: 35 additions & 0 deletions LayoutTests/inspector/page/overrideUserPreference.html
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,41 @@
},
});

suite.addTestCase({
name: "Page.overrideUserPreference.InvertedColors",
description: "",
async test() {
let cssPropertyName = "--test-inverted-colors";
let mediaQuery = "(inverted-colors: inverted)";
let testCases = [
{
expectedValue: "none",
expectedMatchMedia: false,
},
{
preferenceName: InspectorBackend.Enum.Page.UserPreferenceName.InvertedColors,
preferenceValue: InspectorBackend.Enum.Page.UserPreferenceValue.Inverted,
expectedValue: "inverted",
expectedMatchMedia: true,
},
{
preferenceName: InspectorBackend.Enum.Page.UserPreferenceName.InvertedColors,
preferenceValue: InspectorBackend.Enum.Page.UserPreferenceValue.None,
expectedValue: "none",
expectedMatchMedia: false,
},
{
preferenceName: InspectorBackend.Enum.Page.UserPreferenceName.InvertedColors,
preferenceValue: null,
expectedValue: "none",
expectedMatchMedia: false,
},
];

await testOverridePreference({cssPropertyName, mediaQuery, testCases});
},
});

suite.runTestCasesAndFinish();
}

Expand Down
4 changes: 2 additions & 2 deletions Source/JavaScriptCore/inspector/protocol/Page.json
Original file line number Diff line number Diff line change
Expand Up @@ -35,13 +35,13 @@
{
"id": "UserPreferenceName",
"type": "string",
"enum": ["PrefersReducedMotion", "PrefersContrast", "PrefersColorScheme"],
"enum": ["PrefersReducedMotion", "PrefersContrast", "PrefersColorScheme", "InvertedColors"],
"description": "User preference name."
},
{
"id": "UserPreferenceValue",
"type": "string",
"enum": ["NoPreference", "Reduce", "More", "Light", "Dark"],
"enum": ["NoPreference", "Reduce", "More", "Light", "Dark", "Inverted", "None"],
"description": "User preference value."
},
{
Expand Down
27 changes: 27 additions & 0 deletions Source/WebCore/inspector/agents/InspectorPageAgent.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -391,6 +391,7 @@ Protocol::ErrorStringOr<void> InspectorPageAgent::disable()
inspectedPageSettings.setWebSecurityEnabledInspectorOverride(std::nullopt);
inspectedPageSettings.setForcedPrefersReducedMotionAccessibilityValue(ForcedAccessibilityValue::System);
inspectedPageSettings.setForcedPrefersContrastAccessibilityValue(ForcedAccessibilityValue::System);
inspectedPageSettings.setForcedColorsAreInvertedAccessibilityValue(ForcedAccessibilityValue::System);

m_client->setDeveloperPreferenceOverride(InspectorClient::DeveloperPreference::PrivateClickMeasurementDebugModeEnabled, std::nullopt);
m_client->setDeveloperPreferenceOverride(InspectorClient::DeveloperPreference::ITPDebugModeEnabled, std::nullopt);
Expand Down Expand Up @@ -516,12 +517,29 @@ Protocol::ErrorStringOr<void> InspectorPageAgent::overrideUserPreference(Protoco
case Protocol::Page::UserPreferenceName::PrefersColorScheme:
overridePrefersColorScheme(WTFMove(value));
return { };

case Protocol::Page::UserPreferenceName::InvertedColors:
overrideInvertedColors(WTFMove(value));
return { };
}

ASSERT_NOT_REACHED();
return { };
}

void InspectorPageAgent::overrideInvertedColors(std::optional<Protocol::Page::UserPreferenceValue>&& value)
{
ForcedAccessibilityValue forcedValue = ForcedAccessibilityValue::System;

if (value == Protocol::Page::UserPreferenceValue::Inverted)
forcedValue = ForcedAccessibilityValue::On;
else if (value == Protocol::Page::UserPreferenceValue::None)
forcedValue = ForcedAccessibilityValue::Off;

m_inspectedPage.settings().setForcedColorsAreInvertedAccessibilityValue(forcedValue);
m_inspectedPage.accessibilitySettingsDidChange();
}

void InspectorPageAgent::overridePrefersReducedMotion(std::optional<Protocol::Page::UserPreferenceValue>&& value)
{
ForcedAccessibilityValue forcedValue = ForcedAccessibilityValue::System;
Expand Down Expand Up @@ -1011,6 +1029,15 @@ void InspectorPageAgent::defaultUserPreferencesDidChange()

defaultUserPreferences->addItem(WTFMove(prefersContrastUserPreference));

bool invertedColors = screenHasInvertedColors();

auto invertedColorsUserPreference = Protocol::Page::UserPreference::create()
.setName(Protocol::Page::UserPreferenceName::InvertedColors)
.setValue(invertedColors ? Protocol::Page::UserPreferenceValue::Inverted : Protocol::Page::UserPreferenceValue::None)
.release();

defaultUserPreferences->addItem(WTFMove(invertedColorsUserPreference));

#if ENABLE(DARK_MODE_CSS) || HAVE(OS_DARK_MODE_SUPPORT)
auto prefersColorSchemeUserPreference = Protocol::Page::UserPreference::create()
.setName(Protocol::Page::UserPreferenceName::PrefersColorScheme)
Expand Down
1 change: 1 addition & 0 deletions Source/WebCore/inspector/agents/InspectorPageAgent.h
Original file line number Diff line number Diff line change
Expand Up @@ -161,6 +161,7 @@ class InspectorPageAgent final : public InspectorAgentBase, public Inspector::Pa
void overridePrefersReducedMotion(std::optional<Inspector::Protocol::Page::UserPreferenceValue>&&);
void overridePrefersContrast(std::optional<Inspector::Protocol::Page::UserPreferenceValue>&&);
void overridePrefersColorScheme(std::optional<Inspector::Protocol::Page::UserPreferenceValue>&&);
void overrideInvertedColors(std::optional<Inspector::Protocol::Page::UserPreferenceValue>&&);

Ref<Inspector::Protocol::Page::Frame> buildObjectForFrame(LocalFrame*);
Ref<Inspector::Protocol::Page::FrameResourceTree> buildObjectForFrameTree(LocalFrame*);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -843,6 +843,8 @@ localizedStrings["Historical @ Font Details Sidebar Property Value"] = "Historic
localizedStrings["Historical Forms @ Font Details Sidebar Property Value"] = "Historical Forms";
localizedStrings["Host"] = "Host";
localizedStrings["ICO"] = "ICO";
/* Label for input to override the preference for inverted colors. */
localizedStrings["Invert colors @ User Preferences Overrides"] = "Invert colors";
localizedStrings["IP"] = "IP";
localizedStrings["IP Address"] = "IP Address";
localizedStrings["ITML Context"] = "ITML Context";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ WI.OverrideUserPreferencesPopover = class OverrideUserPreferencesPopover extends
});
}

if (InspectorBackend.Enum.Page?.UserPreferenceName?.PrefersReducedMotion || InspectorBackend.Enum.Page?.UserPreferenceName?.PrefersContrast) {
if (InspectorBackend.Enum.Page?.UserPreferenceName?.PrefersReducedMotion || InspectorBackend.Enum.Page?.UserPreferenceName?.PrefersContrast || InspectorBackend.Enum.Page?.UserPreference?.Inverted) {
let accessibilityHeader = contentElement.appendChild(document.createElement("h1"));
accessibilityHeader.textContent = WI.UIString("Accessibility", "Accessibility @ User Preferences Overrides", "Header for section with accessibility user preferences.");
}
Expand All @@ -113,6 +113,17 @@ WI.OverrideUserPreferencesPopover = class OverrideUserPreferencesPopover extends
defaultValue: WI.CSSManager.UserPreferenceDefaultValue,
});

// COMPATIBILITY (macOS 13.0, iOS 16.0): `InvertedColors` value for `Page.UserPreferenceName` did not exist yet.
if (InspectorBackend.Enum.Page?.UserPreferenceName?.InvertedColors)
this._createSelectElement({
contentElement,
id: "override-inverted-colors",
label: WI.UIString("Inverte colors", "Invert colors @ User Preferences Overrides", "Label for input to override the preference for inverted colors."),
preferenceName: InspectorBackend.Enum.Page.UserPreferenceName.InvertedColors,
preferenceValues: [InspectorBackend.Enum.Page.UserPreferenceValue.Inverted, InspectorBackend.Enum.Page.UserPreferenceValue.None],
defaultValue: WI.CSSManager.UserPreferenceDefaultValue,
});

return contentElement;
}

Expand Down Expand Up @@ -164,8 +175,10 @@ WI.OverrideUserPreferencesPopover = class OverrideUserPreferencesPopover extends
return WI.UIString("System (%s)", "System @ User Preferences Overrides", "Label for a preference that matches the default system value. The system value is shown in parentheses.").format(this._userPreferenceValueLabel(systemValue));
case InspectorBackend.Enum.Page.UserPreferenceValue?.Reduce:
case InspectorBackend.Enum.Page.UserPreferenceValue?.More:
case InspectorBackend.Enum.Page.UserPreferenceValue?.Inverted:
return WI.UIString("On", "On @ User Preferences Overrides", "Label for a preference that is turned on.");
case InspectorBackend.Enum.Page.UserPreferenceValue?.NoPreference:
case InspectorBackend.Enum.Page.UserPreferenceValue?.None:
return WI.UIString("Off", "Off @ User Preferences Overrides", "Label for a preference that is turned off.");
case InspectorBackend.Enum.Page.UserPreferenceValue?.Light:
case WI.CSSManager.Appearance.Light:
Expand Down

0 comments on commit 08cc52f

Please sign in to comment.