kitten @ set-background-opacity --toggle

Fixes #6691
This commit is contained in:
Kovid Goyal
2023-10-10 05:04:50 +05:30
parent 70f3909cdc
commit 16e7ca3a95
2 changed files with 20 additions and 6 deletions

View File

@@ -52,6 +52,8 @@ Detailed list of changes
- macOS: When running the default shell with the login program fix :file:`~/.hushlogin` not being respected when opening windows not in the home directory (:iss:`6689`) - macOS: When running the default shell with the login program fix :file:`~/.hushlogin` not being respected when opening windows not in the home directory (:iss:`6689`)
- ``kitten @ set-background-opacity`` - add a new ``--toggle`` flag to easily switch opacity between the specified value and the default (:iss:`6691`)
0.30.1 [2023-10-05] 0.30.1 [2023-10-05]
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

View File

@@ -29,6 +29,7 @@ class SetBackgroundOpacity(RemoteCommand):
match_window/str: Window to change opacity in match_window/str: Window to change opacity in
match_tab/str: Tab to change opacity in match_tab/str: Tab to change opacity in
all/bool: Boolean indicating operate on all windows all/bool: Boolean indicating operate on all windows
toggle/bool: Boolean indicating if opacity should be toggled between the default and the specified value
''' '''
short_desc = 'Set the background opacity' short_desc = 'Set the background opacity'
@@ -41,26 +42,37 @@ class SetBackgroundOpacity(RemoteCommand):
options_spec = '''\ options_spec = '''\
--all -a --all -a
type=bool-set type=bool-set
By default, background opacity are only changed for the currently active window. This option will By default, background opacity are only changed for the currently active OS window. This option will
cause background opacity to be changed in all windows. cause background opacity to be changed in all windows.
--toggle
type=bool-set
When specified, the background opacity for the matching OS windows will be reset to default if it is currently
equal to the specified value, otherwise it will be set to the specified value.
''' + '\n\n' + MATCH_WINDOW_OPTION + '\n\n' + MATCH_TAB_OPTION.replace('--match -m', '--match-tab -t') ''' + '\n\n' + MATCH_WINDOW_OPTION + '\n\n' + MATCH_TAB_OPTION.replace('--match -m', '--match-tab -t')
args = RemoteCommand.Args(spec='OPACITY', count=1, json_field='opacity', special_parse='parse_opacity(args[0])') args = RemoteCommand.Args(spec='OPACITY', count=1, json_field='opacity', special_parse='parse_opacity(args[0])')
def message_to_kitty(self, global_opts: RCOptions, opts: 'CLIOptions', args: ArgsType) -> PayloadType: def message_to_kitty(self, global_opts: RCOptions, opts: 'CLIOptions', args: ArgsType) -> PayloadType:
opacity = max(0.1, min(float(args[0]), 1.0)) opacity = max(0.1, min(float(args[0]), 1.0))
return { return {
'opacity': opacity, 'match_window': opts.match, 'opacity': opacity, 'match_window': opts.match,
'all': opts.all, 'match_tab': opts.match_tab 'all': opts.all, 'match_tab': opts.match_tab, 'toggle': opts.toggle,
} }
def response_from_kitty(self, boss: Boss, window: Optional[Window], payload_get: PayloadGetType) -> ResponseType: def response_from_kitty(self, boss: Boss, window: Optional[Window], payload_get: PayloadGetType) -> ResponseType:
from kitty.fast_data_types import get_options from kitty.fast_data_types import background_opacity_of, get_options
if not get_options().dynamic_background_opacity: opts = get_options()
if not opts.dynamic_background_opacity:
raise OpacityError('You must turn on the dynamic_background_opacity option in kitty.conf to be able to set background opacity') raise OpacityError('You must turn on the dynamic_background_opacity option in kitty.conf to be able to set background opacity')
windows = self.windows_for_payload(boss, window, payload_get) windows = self.windows_for_payload(boss, window, payload_get)
for os_window_id in {w.os_window_id for w in windows if w}: for os_window_id in {w.os_window_id for w in windows if w}:
boss._set_os_window_background_opacity(os_window_id, payload_get('opacity')) val: float = payload_get('opacity')
if payload_get('toggle'):
current = background_opacity_of(os_window_id)
if current == val:
val = opts.background_opacity
boss._set_os_window_background_opacity(os_window_id, val)
return None return None