Skip to content

Commit b9a8b64

Browse files
committed
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.
1 parent 92ee52b commit b9a8b64

File tree

3 files changed

+56
-40
lines changed

3 files changed

+56
-40
lines changed

kittens/panel/main.py

Lines changed: 41 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@
3030
toggle_os_window_visibility,
3131
)
3232
from kitty.simple_cli_definitions import panel_options_spec
33-
from kitty.types import LayerShellConfig
33+
from kitty.types import LayerShellConfig, run_once
3434
from kitty.typing_compat import BossType
3535
from kitty.utils import log_error
3636

@@ -95,6 +95,46 @@ def layer_shell_config(opts: PanelCLIOptions) -> LayerShellConfig:
9595
output_name=opts.output_name or '')
9696

9797

98+
@run_once
99+
def cli_option_to_lsc_configs_map() -> dict[str, tuple[str, ...]]:
100+
return {
101+
'lines': ('y_size_in_cells', 'y_size_in_pixels'),
102+
'columns': ('x_size_in_cells', 'x_size_in_pixels'),
103+
'margin_top': ('requested_top_margin',),
104+
'margin_left': ('requested_left_margin',),
105+
'margin_bottom': ('requested_bottom_margin',),
106+
'margin_right': ('requested_right_margin',),
107+
'edge': ('edge',),
108+
'layer': ('type',),
109+
'output_name': ('output_name',),
110+
'focus_policy': ('focus_policy',),
111+
'exclusive_zone': ('requested_exclusive_zone',),
112+
'override_exclusive_zone': ('override_exclusive_zone',),
113+
'hide_on_focus_loss': ('hide_on_focus_loss',)
114+
}
115+
116+
117+
def incrementally_update_layer_shell_config(existing: dict[str, Any], cli_options: Iterable[str]) -> LayerShellConfig:
118+
seen_options: dict[str, Any] = {}
119+
try:
120+
try:
121+
opts, _ = parse_panel_args(list(cli_options), track_seen_options=seen_options)
122+
except SystemExit as e:
123+
raise ValueError(str(e))
124+
lsc = layer_shell_config(opts)
125+
except Exception as e:
126+
raise ValueError(f'Invalid panel options specified: {e}')
127+
lsc_cli_map = cli_option_to_lsc_configs_map()
128+
for option in seen_options:
129+
for config in lsc_cli_map[option]:
130+
existing[config] = getattr(lsc, config)
131+
if seen_options.get('edge') == 'background':
132+
existing['type'] = GLFW_LAYER_SHELL_BACKGROUND
133+
if existing['hide_on_focus_loss']:
134+
existing['focus_policy'] = GLFW_FOCUS_ON_DEMAND
135+
return LayerShellConfig(**existing)
136+
137+
98138
mtime_map: dict[str, float] = {}
99139

100140

kitty/launch.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -443,11 +443,11 @@ def get_env(opts: LaunchCLIOptions, active_child: Child | None = None, base_env:
443443
return env
444444

445445

446-
def layer_shell_config_from_panel_opts(panel_opts: Iterable[str], track_seen_options: dict[str, Any] | None = None) -> LayerShellConfig:
446+
def layer_shell_config_from_panel_opts(panel_opts: Iterable[str]) -> LayerShellConfig:
447447
from kittens.panel.main import layer_shell_config, parse_panel_args
448448
args = [('' if x.startswith('--') else '--') + x for x in panel_opts]
449449
try:
450-
opts, _ = parse_panel_args(args, track_seen_options=track_seen_options)
450+
opts, _ = parse_panel_args(args)
451451
except SystemExit as e:
452452
raise ValueError(str(e))
453453
return layer_shell_config(opts)

kitty/rc/resize_os_window.py

Lines changed: 13 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,7 @@
11
#!/usr/bin/env python
22
# License: GPLv3 Copyright: 2020, Kovid Goyal <kovid at kovidgoyal.net>
33

4-
from typing import TYPE_CHECKING, Any
5-
6-
from kitty.fast_data_types import (
7-
GLFW_FOCUS_ON_DEMAND,
8-
GLFW_LAYER_SHELL_BACKGROUND,
9-
)
10-
from kitty.types import LayerShellConfig
4+
from typing import TYPE_CHECKING
115

126
from .base import (
137
MATCH_WINDOW_OPTION,
@@ -124,41 +118,23 @@ def response_from_kitty(self, boss: Boss, window: Window | None, payload_get: Pa
124118
f'The OS Window {os_window_id} is not a panel you should not use the --action=resize option to resize it')
125119
if not panels:
126120
raise RemoteControlErrorWithoutTraceback('Must specify at least one panel setting')
127-
from kitty.launch import layer_shell_config_from_panel_opts
128-
seen_options: dict[str, Any] = {}
129-
try:
130-
lsc = layer_shell_config_from_panel_opts(panels, track_seen_options=seen_options)
131-
except Exception as e:
132-
raise RemoteControlErrorWithoutTraceback(
133-
f'Invalid panel options specified: {e}')
134121
if payload_get('incremental'):
135-
cli_option_to_lsc_configs_map = {
136-
'lines': ('y_size_in_cells', 'y_size_in_pixels'),
137-
'columns': ('x_size_in_cells', 'x_size_in_pixels'),
138-
'margin_top': ('requested_top_margin',),
139-
'margin_left': ('requested_left_margin',),
140-
'margin_bottom': ('requested_bottom_margin',),
141-
'margin_right': ('requested_right_margin',),
142-
'edge': ('edge',),
143-
'layer': ('type',),
144-
'output_name': ('output_name',),
145-
'focus_policy': ('focus_policy',),
146-
'exclusive_zone': ('requested_exclusive_zone',),
147-
'override_exclusive_zone': ('override_exclusive_zone',),
148-
'hide_on_focus_loss': ('hide_on_focus_loss',)
149-
}
150122
existing = layer_shell_config_for_os_window(os_window_id)
151123
if existing is None:
152124
raise RemoteControlErrorWithoutTraceback(
153125
f'The OS Window {os_window_id} has no panel configuration')
154-
for option in seen_options:
155-
for config in cli_option_to_lsc_configs_map[option]:
156-
existing[config] = getattr(lsc, config)
157-
if seen_options.get('edge') == 'background':
158-
existing['type'] = GLFW_LAYER_SHELL_BACKGROUND
159-
if existing['hide_on_focus_loss']:
160-
existing['focus_policy'] = GLFW_FOCUS_ON_DEMAND
161-
lsc = LayerShellConfig(**existing)
126+
from kittens.panel.main import incrementally_update_layer_shell_config
127+
try:
128+
lsc = incrementally_update_layer_shell_config(existing, panels)
129+
except Exception as e:
130+
raise RemoteControlErrorWithoutTraceback(str(e))
131+
else:
132+
from kitty.launch import layer_shell_config_from_panel_opts
133+
try:
134+
lsc = layer_shell_config_from_panel_opts(panels)
135+
except Exception as e:
136+
raise RemoteControlErrorWithoutTraceback(
137+
f'Invalid panel options specified: {e}')
162138
if not set_layer_shell_config(os_window_id, lsc):
163139
raise RemoteControlErrorWithoutTraceback(f'Failed to change panel configuration for OS Window {os_window_id}')
164140
elif ac == 'toggle-visibility':

0 commit comments

Comments
 (0)