class _DarkModeGenerator:
"""Get necessary blink settings to configure dark mode."""
# Mapping from a colors.webpage.darkmode.algorithm setting value to
# Chromium's DarkModeInversionAlgorithm enum values.
_ALGORITHMS = {
# 0: kOff (not exposed)
# 1: kSimpleInvertForTesting (not exposed)
'brightness-rgb': 2, # kInvertBrightness
'lightness-hsl': 3, # kInvertLightness
'lightness-cielab': 4, # kInvertLightnessLAB
}
# Mapping from a colors.webpage.darkmode.policy.images setting value to
# Chromium's DarkModeImagePolicy enum values.
_IMAGE_POLICIES = {
'always': 0, # kFilterAll
'never': 1, # kFilterNone
'smart': 2, # kFilterSmart
}
# Mapping from a colors.webpage.darkmode.policy.page setting value to
# Chromium's DarkModePagePolicy enum values.
_PAGE_POLICIES = {
'always': 0, # kFilterAll
'smart': 1, # kFilterByBackground
}
def _val(self, key: str, value: str) -> typing.Tuple[str, str]:
"""Get a value tuple for the given dark mode setting."""
# FIXME: This is "forceDarkMode" starting with Chromium 83
prefix = 'darkMode'
return (prefix + key, value)
def _get(self, setting: str) -> typing.Any:
"""Get a darkmode setting from the config.
To avoid blowing up the commandline length, we only pass modified
settings to Chromium, as our defaults line up with Chromium's.
"""
return config.instance.get('colors.webpage.darkmode.' + setting,
fallback=False)
def _is_set(self, value: typing.Any) -> bool:
"""Check whether the given value is set."""
return not isinstance(value, usertypes.Unset)
def generate(self) -> typing.Iterator[typing.Tuple[str, str]]:
if not config.val.colors.webpage.darkmode.enabled:
return
if qtutils.version_check('5.15'):
yield self._val('Enabled', 'true')
algorithm = self._get('algorithm')
contrast = self._get('contrast')
policy_images = self._get('policy.images')
policy_page = self._get('policy.page')
treshold_text = self._get('threshold.text')
treshold_background = self._get('threshold.background')
grayscale_all = self._get('grayscale_all')
grayscale_images = self._get('grayscale_images')
if self._is_set(algorithm):
key = ('InversionAlgorithm' if qtutils.version_check('5.15')
else '')
value = self._ALGORITHMS[algorithm]
yield self._val(key, value)
if self._is_set(contrast):
yield self._val('Contrast', str(contrast))
if self._is_set(policy_images):
value = self._IMAGE_POLICIES[policy_images]
yield self._val('ImagePolicy', value)
if self._is_set(policy_page):
value = self._PAGE_POLICIES[policy_page]
yield self._val('PagePolicy', value)
# [ ... ]