diff --git a/docs/changelog.rst b/docs/changelog.rst index 3bd8215e4..13573ecb6 100644 --- a/docs/changelog.rst +++ b/docs/changelog.rst @@ -457,6 +457,12 @@ Detailed list of changes on the user. Shown in the tab bar via a new :code:`tab.program_status` field for :opt:`tab_title_template`, see :ref:`program_status` +- Remote control: Fix :ref:`at-set-colors` and :option:`launch --color` clearing + :opt:`transparent_background_colors` when changing other colors, fix an error + when setting them with :option:`kitten @ set-colors --configured` and fix + :option:`kitten @ set-colors --reset` not restoring them and not resetting all + windows and the configured colors when used from the command line + 0.49.2 [2026-10-01] ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ diff --git a/kitty/boss.py b/kitty/boss.py index f8158c3b9..c75296dc5 100644 --- a/kitty/boss.py +++ b/kitty/boss.py @@ -429,6 +429,7 @@ class Boss: self.update_check_process: Optional['PopenType[bytes]'] = None self.window_id_map: WeakValueDictionary[int, Window] = WeakValueDictionary() self.color_settings_at_startup: dict[str, Color | None] = {k: opts[k] for k in opts if isinstance(opts[k], Color) or k in nullable_colors} + self.transparent_background_colors_at_startup = opts.transparent_background_colors self.current_visual_select: VisualSelect | None = None # A list of events received so far that are potentially part of a sequence keybinding. self.cached_values = cached_values diff --git a/kitty/colors.c b/kitty/colors.c index 6c2d886c7..cd27c8f26 100644 --- a/kitty/colors.c +++ b/kitty/colors.c @@ -428,8 +428,11 @@ patch_color_profiles(PyObject *module UNUSED, PyObject *args) { PyObject *spec, *transparent_background_colors, *profiles, *v; ColorProfile *self; int change_configured; - if (!PyArg_ParseTuple(args, "O!O!O!p", &PyDict_Type, &spec, &PyTuple_Type, &transparent_background_colors, &PyTuple_Type, &profiles, &change_configured)) + if (!PyArg_ParseTuple(args, "O!OO!p", &PyDict_Type, &spec, &transparent_background_colors, &PyTuple_Type, &profiles, &change_configured)) return NULL; + if (transparent_background_colors != Py_None && !PyTuple_Check(transparent_background_colors)) { + PyErr_SetString(PyExc_TypeError, "transparent_background_colors must be a tuple or None"); return NULL; + } char key[32] = {0}; bool has_null_values = false; for (size_t i = 0; i < arraysz(FG_BG_256); i++) { @@ -476,10 +479,13 @@ patch_color_profiles(PyObject *module UNUSED, PyObject *args) { S(visual_bell_color, visual_bell_color); #undef SI #undef S - for (Py_ssize_t i = 0; i < PyTuple_GET_SIZE(profiles); i++) { - self = (ColorProfile *)PyTuple_GET_ITEM(profiles, i); - set_transparent_background_colors(self->overriden_transparent_colors, transparent_background_colors); - if (change_configured) set_transparent_background_colors(self->configured_transparent_colors, transparent_background_colors); + // None means the transparent colors were not specified, so leave them unchanged + if (transparent_background_colors != Py_None) { + for (Py_ssize_t i = 0; i < PyTuple_GET_SIZE(profiles); i++) { + self = (ColorProfile *)PyTuple_GET_ITEM(profiles, i); + set_transparent_background_colors(self->overriden_transparent_colors, transparent_background_colors); + if (change_configured) set_transparent_background_colors(self->configured_transparent_colors, transparent_background_colors); + } } if (has_null_values) { diff --git a/kitty/colors.py b/kitty/colors.py index 1296d2c29..984fffdf8 100644 --- a/kitty/colors.py +++ b/kitty/colors.py @@ -19,6 +19,9 @@ ColorsSpec = dict[str, Optional[int]] TransparentBackgroundColors = tuple[tuple[Color, float], ...] ColorSchemes = Literal['light', 'dark', 'no_preference'] Colors = tuple[ColorsSpec, TransparentBackgroundColors] +# None for the transparent background colors means they were not specified and +# should be left unchanged, as opposed to () which means clear them. +ParsedColors = tuple[ColorsSpec, TransparentBackgroundColors | None] class BackgroundImageOptions(TypedDict, total=False): @@ -70,7 +73,7 @@ class ThemeColors: spec, tbc = parse_colors((f,), background_image_options) ans = dc_spec.copy() ans.update(spec) - return ans, tbc + return ans, (() if tbc is None else tbc) def refresh(self) -> bool: found = False @@ -209,13 +212,13 @@ def parse_colors( args: Iterable[str | Iterable[str]], background_image_options: BackgroundImageOptions | None = None, allow_reading_conf_files: bool = True, -) -> Colors: +) -> ParsedColors: from kitty.options.parse import parse_conf_item colors: dict[str, Color | None | int] = {} nullable_color_map: dict[str, int | None] = {} special_color_map: dict[str, int] = {} - transparent_background_colors: TransparentBackgroundColors = () + transparent_background_colors: TransparentBackgroundColors | None = None allowed = all_color_related_conf_keys() for spec in args: conf: dict[str, Any] = {} @@ -253,7 +256,10 @@ def parse_colors( def patch_options_with_color_spec( - opts: Options, spec: ColorsSpec, transparent_background_colors: TransparentBackgroundColors, background_image_options: BackgroundImageOptions | None = None + opts: Options, + spec: ColorsSpec, + transparent_background_colors: TransparentBackgroundColors | None, + background_image_options: BackgroundImageOptions | None = None, ) -> None: for k, v in spec.items(): if hasattr(opts, k): @@ -265,7 +271,8 @@ def patch_options_with_color_spec( setattr(opts, k, v) else: setattr(opts, k, color_from_int(v)) - opts.transparent_background_colors = transparent_background_colors + if transparent_background_colors is not None: + opts.transparent_background_colors = transparent_background_colors if background_image_options is not None: for k, bv in background_image_options.items(): if hasattr(opts, k): @@ -274,7 +281,7 @@ def patch_options_with_color_spec( def patch_colors( spec: ColorsSpec, - transparent_background_colors: TransparentBackgroundColors = (), + transparent_background_colors: TransparentBackgroundColors | None = None, configured: bool = False, windows: Sequence[WindowType] | None = None, notify_on_bg_change: bool = True, diff --git a/kitty/fast_data_types.pyi b/kitty/fast_data_types.pyi index 7ca46c862..69daa427b 100644 --- a/kitty/fast_data_types.pyi +++ b/kitty/fast_data_types.pyi @@ -804,7 +804,10 @@ class ColorProfile: def set_transparent_background_color(self, index: int, color: Color | None = None, opacity: float | None = None) -> None: ... def patch_color_profiles( - spec: Dict[str, Optional[int]], transparent_background_colors: tuple[tuple[Color, float], ...], profiles: Tuple[ColorProfile, ...], change_configured: bool + spec: Dict[str, Optional[int]], + transparent_background_colors: Optional[tuple[tuple[Color, float], ...]], + profiles: Tuple[ColorProfile, ...], + change_configured: bool, ) -> None: pass diff --git a/kitty/rc/set_colors.py b/kitty/rc/set_colors.py index 86963b84c..bdf7fa63d 100644 --- a/kitty/rc/set_colors.py +++ b/kitty/rc/set_colors.py @@ -5,7 +5,6 @@ from typing import TYPE_CHECKING from kitty.cli import emph -from kitty.fast_data_types import Color from .base import ( MATCH_TAB_OPTION, @@ -17,17 +16,19 @@ from .base import ( PayloadType, RCOptions, RemoteCommand, + RemoteControlErrorWithoutTraceback, ResponseType, Window, ) if TYPE_CHECKING: from kitty.cli_stub import SetColorsRCOptions as CLIOptions + from kitty.colors import TransparentBackgroundColors class SetColors(RemoteCommand): protocol_spec = __doc__ = """ - colors+/dict.colors: An object mapping names to colors as 24-bit RGB integers or null for nullable colors. Or a string for transparent_background_colors. + colors+/dict.colors: Map of names to 24-bit RGB ints or null for nullable colors. transparent_background_colors is a string, empty to clear, absent to keep. match_window/str: Window to change colors in match_tab/str: Tab to change colors in all/bool: Boolean indicating change colors everywhere or not @@ -76,7 +77,7 @@ this option, any color arguments are ignored and :option:`kitten @ set-colors -- def message_to_kitty(self, global_opts: RCOptions, opts: 'CLIOptions', args: ArgsType) -> PayloadType: final_colors: dict[str, int | None | str] = {} - transparent_background_colors: tuple[tuple[Color, float], ...] = () + transparent_background_colors: 'TransparentBackgroundColors | None' = None from kitty.colors import parse_colors if not opts.reset: @@ -87,7 +88,7 @@ this option, any color arguments are ignored and :option:`kitten @ set-colors -- except Exception as err: raise ParsingOfArgsFailed(str(err)) from err final_colors.update(fc) - if transparent_background_colors: + if transparent_background_colors is not None: final_colors['transparent_background_colors'] = ' '.join(f'{c.as_sharp}@{f}' for c, f in transparent_background_colors) ans = { 'match_window': opts.match, @@ -102,19 +103,36 @@ this option, any color arguments are ignored and :option:`kitten @ set-colors -- def response_from_kitty(self, boss: Boss, window: Window | None, payload_get: PayloadGetType) -> ResponseType: from kitty.colors import patch_colors - windows = self.windows_for_payload(boss, window, payload_get) - colors: dict[str, int | None] = payload_get('colors') or {} - if payload_get('reset'): + colors: dict[str, int | None] = {} + tbc: TransparentBackgroundColors | None = None + # reset implies all and configured, enforce it here as not all clients set them + reset = bool(payload_get('reset')) + configured = reset or bool(payload_get('configured')) + windows = list(boss.all_windows) if reset else self.windows_for_payload(boss, window, payload_get) + if reset: colors = {k: None if v is None else int(v) for k, v in boss.color_settings_at_startup.items()} - tbc = colors.get('transparent_background_colors') - if tbc: - from kitty.options.utils import transparent_background_colors - - parsed_tbc = transparent_background_colors(str(tbc)) + tbc = boss.transparent_background_colors_at_startup else: - parsed_tbc = () - patch_colors(colors, parsed_tbc, bool(payload_get('configured')), windows=windows) + payload_colors: dict[str, int | str | None] = payload_get('colors') or {} + for k, v in payload_colors.items(): + if k == 'transparent_background_colors': + tbc = self.parse_transparent_background_colors(v) + elif isinstance(v, str): + raise RemoteControlErrorWithoutTraceback(f'The color {k} must be an integer or null, not a string') + else: + colors[k] = v + patch_colors(colors, tbc, configured, windows=windows) return None + def parse_transparent_background_colors(self, spec: int | str | None) -> 'TransparentBackgroundColors': + from kitty.options.utils import transparent_background_colors + + if not isinstance(spec, str): + raise RemoteControlErrorWithoutTraceback('transparent_background_colors must be a string') + try: + return transparent_background_colors(spec) + except ValueError as err: + raise RemoteControlErrorWithoutTraceback(f'Invalid transparent_background_colors: {spec!r} with error: {err}') from err + set_colors = SetColors() diff --git a/kitty_tests/options.py b/kitty_tests/options.py index 98ee9f42c..8fab9aa6a 100644 --- a/kitty_tests/options.py +++ b/kitty_tests/options.py @@ -127,6 +127,15 @@ class TestConfParsing(BaseTest): self.ae(actual.get('transparent_background_colors', ()), expected) self.ae(actual['foreground'], 0xFF0000) + # colors that do not mention transparent_background_colors must leave existing ones unchanged + window = Window.__new__(Window) + window.screen = self.create_screen() + apply_colors(window, ('transparent_background_colors=#ffffff@0.5',)) + apply_colors(window, ('foreground=red',)) + self.ae(window.screen.color_profile.as_dict().get('transparent_background_colors', ()), ((Color(255, 255, 255), 0.5),)) + apply_colors(window, ('transparent_background_colors=',)) + self.ae(window.screen.color_profile.as_dict().get('transparent_background_colors', ()), ()) + def cli_parsing(self): from kitty.cli import CLIOptions, Options, parse_cmdline, parse_option_spec diff --git a/kitty_tests/remote_control.py b/kitty_tests/remote_control.py index aa16ab5f9..6ed6c02b9 100644 --- a/kitty_tests/remote_control.py +++ b/kitty_tests/remote_control.py @@ -57,6 +57,47 @@ class TestRemoteControl(BaseTest): setter.assert_called_once_with(11, '') + def test_set_colors_transparent_background_colors(self): + from kitty.fast_data_types import Color + from kitty.rc.base import RemoteControlErrorWithoutTraceback, parse_subcommand_cli + from kitty.remote_control import parse_rc_args + + cmd = command_for_name('set-colors') + + def payload(*args: str): + opts, items = parse_subcommand_cli(cmd, ['set-colors'] + list(args)) + return cmd.message_to_kitty(parse_rc_args([])[0], opts, items) + + # omitted means leave unchanged, explicitly empty means clear + self.assertNotIn('transparent_background_colors', payload('foreground=red')['colors']) + self.ae(payload('transparent_background_colors=')['colors']['transparent_background_colors'], '') + self.ae(payload('transparent_background_colors=red@0.5')['colors']['transparent_background_colors'], '#ff0000@0.5') + + windows = [Window(1, 11)] + boss = Boss(windows) + boss.color_settings_at_startup = {'foreground': Color(1, 2, 3)} + boss.transparent_background_colors_at_startup = ((Color(0, 0, 0), 0.25),) + + def response(p): + with patch('kitty.colors.patch_colors') as patcher: + cmd.response_from_kitty(boss, windows[0], PayloadGetter(cmd, p)) + patcher.assert_called_once() + return patcher.call_args.args + + self.ae(response({'colors': {'foreground': 0xFF0000}}), ({'foreground': 0xFF0000}, None, False)) + colors = {'foreground': 0xFF0000, 'transparent_background_colors': 'red@0.5'} + self.ae(response({'colors': colors, 'configured': True}), ({'foreground': 0xFF0000}, ((Color(255, 0, 0), 0.5),), True)) + self.ae(colors['transparent_background_colors'], 'red@0.5') # payload must not be mutated + self.ae(response({'colors': {'transparent_background_colors': ''}}), ({}, (), False)) + # reset implies configured even when the client does not set it + self.ae( + response({'colors': {'transparent_background_colors': 'red@0.5'}, 'reset': True}), + ({'foreground': int(Color(1, 2, 3))}, ((Color(0, 0, 0), 0.25),), True), + ) + for bad in (0, 'not-a-color@0.5'): + with self.subTest(bad=bad), self.assertRaises(RemoteControlErrorWithoutTraceback): + response({'colors': {'transparent_background_colors': bad}}) + TALK_THREAD_SURVIVES_ACCEPT_ERROR = """ import os, socket, sys, time