From b9a8b64b32e00497567c349594e9a52a947fc79c Mon Sep 17 00:00:00 2001 From: Kovid Goyal Date: Mon, 22 Sep 2025 11:54:23 +0530 Subject: [PATCH] Refactor previous PR Move code to incrementally update lsc config into the kitten module do that it is more likely to stay in sync with any future changes to the kitten cli. --- kittens/panel/main.py | 42 +++++++++++++++++++++++++++++- kitty/launch.py | 4 +-- kitty/rc/resize_os_window.py | 50 ++++++++++-------------------------- 3 files changed, 56 insertions(+), 40 deletions(-) diff --git a/kittens/panel/main.py b/kittens/panel/main.py index a1f059796..8464b9d32 100644 --- a/kittens/panel/main.py +++ b/kittens/panel/main.py @@ -30,7 +30,7 @@ from kitty.fast_data_types import ( toggle_os_window_visibility, ) from kitty.simple_cli_definitions import panel_options_spec -from kitty.types import LayerShellConfig +from kitty.types import LayerShellConfig, run_once from kitty.typing_compat import BossType from kitty.utils import log_error @@ -95,6 +95,46 @@ def layer_shell_config(opts: PanelCLIOptions) -> LayerShellConfig: output_name=opts.output_name or '') +@run_once +def cli_option_to_lsc_configs_map() -> dict[str, tuple[str, ...]]: + return { + 'lines': ('y_size_in_cells', 'y_size_in_pixels'), + 'columns': ('x_size_in_cells', 'x_size_in_pixels'), + 'margin_top': ('requested_top_margin',), + 'margin_left': ('requested_left_margin',), + 'margin_bottom': ('requested_bottom_margin',), + 'margin_right': ('requested_right_margin',), + 'edge': ('edge',), + 'layer': ('type',), + 'output_name': ('output_name',), + 'focus_policy': ('focus_policy',), + 'exclusive_zone': ('requested_exclusive_zone',), + 'override_exclusive_zone': ('override_exclusive_zone',), + 'hide_on_focus_loss': ('hide_on_focus_loss',) + } + + +def incrementally_update_layer_shell_config(existing: dict[str, Any], cli_options: Iterable[str]) -> LayerShellConfig: + seen_options: dict[str, Any] = {} + try: + try: + opts, _ = parse_panel_args(list(cli_options), track_seen_options=seen_options) + except SystemExit as e: + raise ValueError(str(e)) + lsc = layer_shell_config(opts) + except Exception as e: + raise ValueError(f'Invalid panel options specified: {e}') + lsc_cli_map = cli_option_to_lsc_configs_map() + for option in seen_options: + for config in lsc_cli_map[option]: + existing[config] = getattr(lsc, config) + if seen_options.get('edge') == 'background': + existing['type'] = GLFW_LAYER_SHELL_BACKGROUND + if existing['hide_on_focus_loss']: + existing['focus_policy'] = GLFW_FOCUS_ON_DEMAND + return LayerShellConfig(**existing) + + mtime_map: dict[str, float] = {} diff --git a/kitty/launch.py b/kitty/launch.py index 7fe321d01..948fcb05e 100644 --- a/kitty/launch.py +++ b/kitty/launch.py @@ -443,11 +443,11 @@ def get_env(opts: LaunchCLIOptions, active_child: Child | None = None, base_env: return env -def layer_shell_config_from_panel_opts(panel_opts: Iterable[str], track_seen_options: dict[str, Any] | None = None) -> LayerShellConfig: +def layer_shell_config_from_panel_opts(panel_opts: Iterable[str]) -> LayerShellConfig: from kittens.panel.main import layer_shell_config, parse_panel_args args = [('' if x.startswith('--') else '--') + x for x in panel_opts] try: - opts, _ = parse_panel_args(args, track_seen_options=track_seen_options) + opts, _ = parse_panel_args(args) except SystemExit as e: raise ValueError(str(e)) return layer_shell_config(opts) diff --git a/kitty/rc/resize_os_window.py b/kitty/rc/resize_os_window.py index 1cd8927bc..587974b41 100644 --- a/kitty/rc/resize_os_window.py +++ b/kitty/rc/resize_os_window.py @@ -1,13 +1,7 @@ #!/usr/bin/env python # License: GPLv3 Copyright: 2020, Kovid Goyal -from typing import TYPE_CHECKING, Any - -from kitty.fast_data_types import ( - GLFW_FOCUS_ON_DEMAND, - GLFW_LAYER_SHELL_BACKGROUND, -) -from kitty.types import LayerShellConfig +from typing import TYPE_CHECKING from .base import ( MATCH_WINDOW_OPTION, @@ -124,41 +118,23 @@ using this option means that you will not be notified of failures. f'The OS Window {os_window_id} is not a panel you should not use the --action=resize option to resize it') if not panels: raise RemoteControlErrorWithoutTraceback('Must specify at least one panel setting') - from kitty.launch import layer_shell_config_from_panel_opts - seen_options: dict[str, Any] = {} - try: - lsc = layer_shell_config_from_panel_opts(panels, track_seen_options=seen_options) - except Exception as e: - raise RemoteControlErrorWithoutTraceback( - f'Invalid panel options specified: {e}') if payload_get('incremental'): - cli_option_to_lsc_configs_map = { - 'lines': ('y_size_in_cells', 'y_size_in_pixels'), - 'columns': ('x_size_in_cells', 'x_size_in_pixels'), - 'margin_top': ('requested_top_margin',), - 'margin_left': ('requested_left_margin',), - 'margin_bottom': ('requested_bottom_margin',), - 'margin_right': ('requested_right_margin',), - 'edge': ('edge',), - 'layer': ('type',), - 'output_name': ('output_name',), - 'focus_policy': ('focus_policy',), - 'exclusive_zone': ('requested_exclusive_zone',), - 'override_exclusive_zone': ('override_exclusive_zone',), - 'hide_on_focus_loss': ('hide_on_focus_loss',) - } existing = layer_shell_config_for_os_window(os_window_id) if existing is None: raise RemoteControlErrorWithoutTraceback( f'The OS Window {os_window_id} has no panel configuration') - for option in seen_options: - for config in cli_option_to_lsc_configs_map[option]: - existing[config] = getattr(lsc, config) - if seen_options.get('edge') == 'background': - existing['type'] = GLFW_LAYER_SHELL_BACKGROUND - if existing['hide_on_focus_loss']: - existing['focus_policy'] = GLFW_FOCUS_ON_DEMAND - lsc = LayerShellConfig(**existing) + from kittens.panel.main import incrementally_update_layer_shell_config + try: + lsc = incrementally_update_layer_shell_config(existing, panels) + except Exception as e: + raise RemoteControlErrorWithoutTraceback(str(e)) + else: + from kitty.launch import layer_shell_config_from_panel_opts + try: + lsc = layer_shell_config_from_panel_opts(panels) + except Exception as e: + raise RemoteControlErrorWithoutTraceback( + f'Invalid panel options specified: {e}') if not set_layer_shell_config(os_window_id, lsc): raise RemoteControlErrorWithoutTraceback(f'Failed to change panel configuration for OS Window {os_window_id}') elif ac == 'toggle-visibility':