From eb57d0d43148a985e3b1501285a0c5a36eb1435f Mon Sep 17 00:00:00 2001 From: Kovid Goyal Date: Sat, 1 Feb 2020 10:50:56 +0530 Subject: [PATCH] Remote control command to set background image --- kitty/cmds.py | 64 +++++++++++++++++++++++++++++++++++++++++++- kitty/config_data.py | 2 +- kitty/state.c | 2 +- 3 files changed, 65 insertions(+), 3 deletions(-) diff --git a/kitty/cmds.py b/kitty/cmds.py index 4df78d00c..4f4dceb68 100644 --- a/kitty/cmds.py +++ b/kitty/cmds.py @@ -12,7 +12,7 @@ ) from .config import parse_config, parse_send_text_bytes from .constants import appname -from .fast_data_types import focus_os_window +from .fast_data_types import focus_os_window, set_background_image as set_background_image_impl from .launch import ( launch as do_launch, options_spec as launch_options_spec, parse_launch_args @@ -1252,6 +1252,68 @@ def set_background_opacity(boss, window, payload): # }}} +# set_background_image {{{ +@cmd( + 'Set the background_image', + 'Set the background image for the specified OS windows.', + options_spec='''\ +--all -a +type=bool-set +By default, background image is only changed for the currently active OS window. This option will +cause the image to be changed in all windows. + + +--configured -c +type=bool-set +Change the configured background image which is used for new OS windows. + + +--layout +type=choices +choices=tiled,scaled,mirror-tiled,configured +How the image should be displayed. The value of configured will use the configured value. + + +''' + '\n\n' + MATCH_WINDOW_OPTION, + argspec='PATH_TO_PNG_IMAGE', + args_count=1 +) +def cmd_set_background_image(global_opts, opts, args): + ''' + path: Path to a PNG image + match: Window to change opacity in + layout: The image layout + all: Boolean indicating operate on all windows + configured: Boolean indicating if the configured value should be changed + ''' + if not args: + raise SystemExit('Must specify path to PNG image') + path = args[0] + import imghdr + if imghdr.what(path) != 'png': + raise SystemExit('{} is not a PNG image'.format(path)) + return { + 'path': path, 'match': opts.match, 'configured': opts.configured, + 'layout': opts.layout, 'all': opts.all, + } + + +def set_background_image(boss, window, payload): + pg = cmd_set_background_image.payload_get + windows = windows_for_payload(boss, window, payload) + os_windows = tuple({w.os_window_id for w in windows}) + layout = pg(payload, 'layout') + try: + if layout: + set_background_image_impl(pg(payload, 'path'), os_windows, pg(payload, 'configured'), layout) + else: + set_background_image_impl(pg(payload, 'path'), os_windows, pg(payload, 'configured')) + except ValueError as err: + err.hide_traceback = True + raise +# }}} + + # disable_ligatures {{{ @cmd( 'Control ligature rendering', diff --git a/kitty/config_data.py b/kitty/config_data.py index c53b91829..3d2c2ab93 100644 --- a/kitty/config_data.py +++ b/kitty/config_data.py @@ -861,7 +861,7 @@ def config_or_absolute_path(x): o('background_image', 'none', option_type=config_or_absolute_path, long_text=_(''' Path to a background image. Must be in PNG format.''')) -o('background_image_layout', 'tiled', option_type=choices('tiled', 'scaled', 'mirror_tiled'), long_text=_(''' +o('background_image_layout', 'tiled', option_type=choices('tiled', 'scaled', 'mirror-tiled'), long_text=_(''' Whether to tile or scale the background image.''')) o('background_image_linear', False, long_text=_(''' diff --git a/kitty/state.c b/kitty/state.c index 630fbc6d5..be916b7ef 100644 --- a/kitty/state.c +++ b/kitty/state.c @@ -908,7 +908,7 @@ pyset_background_image(PyObject *self UNUSED, PyObject *args) { PyObject *layout_name = NULL; PyObject *os_window_ids; int configured = 0; - PA("sO&|pU", &path, &PyTuple_Type, &os_window_ids, &configured, &layout_name); + PA("sO!|pU", &path, &PyTuple_Type, &os_window_ids, &configured, &layout_name); size_t size; BackgroundImageLayout layout = layout_name ? bglayout(layout_name) : OPT(background_image_layout); BackgroundImage *bgimage = calloc(1, sizeof(BackgroundImage));