Files
kitty-mirror/kitty/rc/set_float_spec.py

97 lines
4.2 KiB
Python

#!/usr/bin/env python
# License: GPLv3 Copyright: 2026, Kovid Goyal <kovid at kovidgoyal.net>
from typing import TYPE_CHECKING
from kitty.types import FloatSpec, iter_spec_settings, parse_float_spec
from .base import (
MATCH_WINDOW_OPTION,
ArgsType,
Boss,
PayloadGetType,
PayloadType,
RCOptions,
RemoteCommand,
RemoteControlErrorWithoutTraceback,
ResponseType,
Window,
)
if TYPE_CHECKING:
from kitty.cli_stub import SetFloatSpecRCOptions as CLIOptions
def split_hidden_toggle(settings: list[str]) -> tuple[list[str], bool]:
"""
Remove the hidden=toggle settings, which are not part of the syntax of launch --float-spec.
Returns the remaining settings and whether hidden should be toggled, which is the case if
the last hidden setting is hidden=toggle. Raises ValueError for settings that cannot be changed.
"""
remaining: list[str] = []
toggle = False
for key, val in iter_spec_settings('float', settings):
if key == 'opacity':
raise ValueError('The opacity of a floating window can only be set when it is created')
if key == 'modal':
raise ValueError('Whether a floating window is modal can only be set when it is created')
if key == 'hidden' and val.lower() == 'toggle':
toggle = True
continue
if key == 'hidden':
toggle = False
remaining.append(f'{key}={val}')
return remaining, toggle
class SetFloatSpec(RemoteCommand):
protocol_spec = __doc__ = """
settings+/list.str: The settings to change, each of the form key=value
match/str: Which windows to change
"""
short_desc = 'Move, resize, hide or show floating windows'
desc = (
'Change the placement and appearance of the specified :ref:`floating windows <floats>`. The settings use the'
' syntax of :option:`launch --float-spec` and are applied on top of the current settings of each window, for example::\n\n'
' kitten @ set-float-spec --match var:scratch size=100x30 anchor=top offset=0,1\n\n'
'All settings except :code:`opacity` and :code:`modal` can be changed. Additionally, :code:`hidden=toggle` hides the windows'
' if they are shown and shows them if they are hidden. Shown windows are given the keyboard focus, if they can be'
' focused. If you use the :option:`kitten @ set-float-spec --match` option, all matched floating windows are'
' changed, windows that are not floating are ignored. By default, only the window in which the command is run'
' is affected. It is an error if no floating windows are matched.'
)
options_spec = MATCH_WINDOW_OPTION
args = RemoteCommand.Args(json_field='settings', spec='SETTINGS ...', minimum_count=1)
def message_to_kitty(self, global_opts: RCOptions, opts: 'CLIOptions', args: ArgsType) -> PayloadType:
if not args:
self.fatal('You must specify at least one setting')
# defaults to the window this command is run in
return {'match': opts.match, 'settings': list(args), 'self': True}
def response_from_kitty(self, boss: Boss, window: Window | None, payload_get: PayloadGetType) -> ResponseType:
changes: list[tuple[Window, FloatSpec]] = []
try:
settings, toggle = split_hidden_toggle(payload_get('settings') or [])
for w in self.windows_for_match_payload(boss, window, payload_get):
if w is None or (tab := w.tabref()) is None or (group := tab.windows.float_for_window_id(w.id)) is None or group.floating is None:
continue
spec = parse_float_spec(*settings, base=group.floating)
if toggle:
spec = spec._replace(hidden=not group.floating.hidden)
changes.append((w, spec))
except ValueError as e:
# Invalid settings
raise RemoteControlErrorWithoutTraceback(str(e)) from e
if not changes:
raise RemoteControlErrorWithoutTraceback('No floating windows matched')
# All settings are validated before any window is changed
for w, spec in changes:
if (tab := w.tabref()) is not None:
tab.set_float_spec(w, spec)
return None
set_float_spec = SetFloatSpec()