From anonymous, 2 Months ago, written in Python.
Embed
  1. def _darkmode_settings() -> typing.Iterator[typing.Tuple[str, str]]:
  2.     """Get necessary blink settings to configure dark mode for QtWebEngine."""
  3.     if not config.val.colors.webpage.darkmode.enabled:
  4.         return
  5.     assert qtutils.version_check('5.14', compiled=False)
  6.  
  7.     # Mapping from a colors.webpage.darkmode.algorithm setting value to
  8.     # Chromium's DarkModeInversionAlgorithm enum values.
  9.     algorithms = {
  10.         # 0: kOff (not exposed)
  11.         # 1: kSimpleInvertForTesting (not exposed)
  12.         'brightness-rgb': 2,  # kInvertBrightness
  13.         'lightness-hsl': 3,  # kInvertLightness
  14.         'lightness-cielab': 4,  # kInvertLightnessLAB
  15.     }
  16.  
  17.     # Mapping from a colors.webpage.darkmode.policy.images setting value to
  18.     # Chromium's DarkModeImagePolicy enum values.
  19.     image_policies = {
  20.         'always': 0,  # kFilterAll
  21.         'never': 1,  # kFilterNone
  22.         'smart': 2,  # kFilterSmart
  23.     }
  24.  
  25.     # Mapping from a colors.webpage.darkmode.policy.page setting value to
  26.     # Chromium's DarkModePagePolicy enum values.
  27.     page_policies = {
  28.         'always': 0,  # kFilterAll
  29.         'smart': 1,  # kFilterByBackground
  30.     }
  31.  
  32.     bools = {
  33.         True: 'true',
  34.         False: 'false',
  35.     }
  36.  
  37.     _setting_description_type = typing.Tuple[
  38.         str,  # qutebrowser option name
  39.         str,  # darkmode setting name
  40.         # Mapping from the config value to a string (or something convertable
  41.         # to a string) which gets passed to Chromium.
  42.         typing.Optional[typing.Mapping[typing.Any, typing.Union[str, int]]],
  43.     ]
  44.     if qtutils.version_check('5.15', compiled=False):
  45.         settings = [
  46.             ('enabled', 'Enabled', bools),
  47.             ('algorithm', 'InversionAlgorithm', algorithms),
  48.         ]  # type: typing.List[_setting_description_type]
  49.     else:
  50.         settings = [
  51.             ('algorithm', '', algorithms),
  52.         ]
  53.  
  54.     settings += [
  55.         ('contrast', 'Contrast', None),
  56.         ('policy.images', 'ImagePolicy', image_policies),
  57.         ('policy.page', 'PagePolicy', page_policies),
  58.         ('threshold.text', 'TextBrightnessThreshold', None),
  59.         ('threshold.background', 'BackgroundBrightnessThreshold', None),
  60.         ('grayscale.all', 'Grayscale', bools),
  61.         ('grayscale.images', 'ImageGrayscale', None),
  62.     ]
  63.  
  64.     for setting, key, mapping in settings:
  65.         # To avoid blowing up the commandline length, we only pass modified
  66.         # settings to Chromium, as our defaults line up with Chromium's.
  67.         # However, we always pass enabled/algorithm to make sure dark mode gets
  68.         # actually turned on.
  69.         value = config.instance.get('colors.webpage.darkmode.' + setting,
  70.                                     fallback=setting in ['enabled', 'algorithm'])
  71.         if isinstance(value, usertypes.Unset):
  72.             continue
  73.  
  74.         if mapping is not None:
  75.             value = mapping[value]
  76.  
  77.         # FIXME: This is "forceDarkMode" starting with Chromium 83
  78.         prefix = 'darkMode'
  79.  
  80.         yield prefix + key, str(value)
  81.