From anonymous, 1 Month ago, written in Python.
Embed
  1. class _DarkModeGenerator:
  2.  
  3.     """Get necessary blink settings to configure dark mode."""
  4.  
  5.     # Mapping from a colors.webpage.darkmode.algorithm setting value to
  6.     # Chromium's DarkModeInversionAlgorithm enum values.
  7.     _ALGORITHMS = {
  8.         # 0: kOff (not exposed)
  9.         # 1: kSimpleInvertForTesting (not exposed)
  10.         'brightness-rgb': 2,  # kInvertBrightness
  11.         'lightness-hsl': 3,  # kInvertLightness
  12.         'lightness-cielab': 4,  # kInvertLightnessLAB
  13.     }
  14.  
  15.     # Mapping from a colors.webpage.darkmode.policy.images setting value to
  16.     # Chromium's DarkModeImagePolicy enum values.
  17.     _IMAGE_POLICIES = {
  18.         'always': 0,  # kFilterAll
  19.         'never': 1,  # kFilterNone
  20.         'smart': 2,  # kFilterSmart
  21.     }
  22.  
  23.     # Mapping from a colors.webpage.darkmode.policy.page setting value to
  24.     # Chromium's DarkModePagePolicy enum values.
  25.     _PAGE_POLICIES = {
  26.         'always': 0,  # kFilterAll
  27.         'smart': 1,  # kFilterByBackground
  28.     }
  29.  
  30.     def _val(self, key: str, value: str) -> typing.Tuple[str, str]:
  31.         """Get a value tuple for the given dark mode setting."""
  32.         # FIXME: This is "forceDarkMode" starting with Chromium 83
  33.         prefix = 'darkMode'
  34.         return (prefix + key, value)
  35.  
  36.     def _get(self, setting: str) -> typing.Any:
  37.         """Get a darkmode setting from the config.
  38.  
  39.        To avoid blowing up the commandline length, we only pass modified
  40.        settings to Chromium, as our defaults line up with Chromium's.
  41.        """
  42.         return config.instance.get('colors.webpage.darkmode.' + setting,
  43.                                    fallback=False)
  44.  
  45.     def _is_set(self, value: typing.Any) -> bool:
  46.         """Check whether the given value is set."""
  47.         return not isinstance(value, usertypes.Unset)
  48.  
  49.     def generate(self) -> typing.Iterator[typing.Tuple[str, str]]:
  50.         if not config.val.colors.webpage.darkmode.enabled:
  51.             return
  52.  
  53.         if qtutils.version_check('5.15'):
  54.             yield self._val('Enabled', 'true')
  55.  
  56.         algorithm = self._get('algorithm')
  57.         contrast = self._get('contrast')
  58.         policy_images = self._get('policy.images')
  59.         policy_page = self._get('policy.page')
  60.         treshold_text = self._get('threshold.text')
  61.         treshold_background = self._get('threshold.background')
  62.         grayscale_all = self._get('grayscale_all')
  63.         grayscale_images = self._get('grayscale_images')
  64.  
  65.         if self._is_set(algorithm):
  66.             key = ('InversionAlgorithm' if qtutils.version_check('5.15')
  67.                    else '')
  68.             value = self._ALGORITHMS[algorithm]
  69.             yield self._val(key, value)
  70.  
  71.         if self._is_set(contrast):
  72.             yield self._val('Contrast', str(contrast))
  73.  
  74.         if self._is_set(policy_images):
  75.             value = self._IMAGE_POLICIES[policy_images]
  76.             yield self._val('ImagePolicy', value)
  77.  
  78.         if self._is_set(policy_page):
  79.             value = self._PAGE_POLICIES[policy_page]
  80.             yield self._val('PagePolicy', value)
  81.  
  82.         # [ ... ]