From ae6e082419f9c8cdbc01fecbd72aad3d93f346ac Mon Sep 17 00:00:00 2001 From: Kovid Goyal Date: Wed, 23 May 2018 08:48:59 +0530 Subject: [PATCH] Keyboard shortcuts to control background opacity --- README.asciidoc | 18 +++++++++++++----- kitty/boss.py | 29 +++++++++++++++++++++++------ kitty/config.py | 2 ++ kitty/kitty.conf | 4 ++++ kitty/state.c | 9 +++++++++ preprocess-readme.py | 2 +- 6 files changed, 52 insertions(+), 12 deletions(-) diff --git a/README.asciidoc b/README.asciidoc index 99d282c6a..a7156b5ff 100644 --- a/README.asciidoc +++ b/README.asciidoc @@ -13,6 +13,11 @@ :sc_fourth_window: pass:quotes[`ctrl+shift+4`] :sc_increase_font_size: pass:quotes[`ctrl+shift+equal`] :sc_input_unicode_character: pass:quotes[`ctrl+shift+u`] +:sc_kitten_hints: pass:quotes[`ctrl+shift+e`] +:sc_kitten_hints_type_line_program: pass:quotes[`ctrl+shift+p → l`] +:sc_kitten_hints_type_path: pass:quotes[`ctrl+shift+p → shift+f`] +:sc_kitten_hints_type_path_program: pass:quotes[`ctrl+shift+p → f`] +:sc_kitten_hints_type_word_program: pass:quotes[`ctrl+shift+p → w`] :sc_kitty_shell_window: pass:quotes[`ctrl+shift+escape`] :sc_move_tab_backward: pass:quotes[`ctrl+shift+,`] :sc_move_tab_forward: pass:quotes[`ctrl+shift+.`] @@ -32,11 +37,6 @@ :sc_previous_tab: pass:quotes[`ctrl+shift+left`] :sc_previous_window: pass:quotes[`ctrl+shift+[`] :sc_restore_font_size: pass:quotes[`ctrl+shift+backspace`] -:sc_run_kitten_text_hints: pass:quotes[`ctrl+shift+e`] -:sc_run_kitten_text_hints_type_line_program: pass:quotes[`ctrl+shift+p → l`] -:sc_run_kitten_text_hints_type_path: pass:quotes[`ctrl+shift+p → shift+f`] -:sc_run_kitten_text_hints_type_path_program: pass:quotes[`ctrl+shift+p → f`] -:sc_run_kitten_text_hints_type_word_program: pass:quotes[`ctrl+shift+p → w`] :sc_scroll_end: pass:quotes[`ctrl+shift+end`] :sc_scroll_home: pass:quotes[`ctrl+shift+home`] :sc_scroll_line_down: pass:quotes[`ctrl+shift+down` or `ctrl+shift+j`] @@ -44,6 +44,10 @@ :sc_scroll_page_down: pass:quotes[`ctrl+shift+page_down`] :sc_scroll_page_up: pass:quotes[`ctrl+shift+page_up`] :sc_second_window: pass:quotes[`ctrl+shift+2`] +:sc_set_background_opacity_0_1: pass:quotes[`ctrl+shift+a → shift+-`] +:sc_set_background_opacity_1: pass:quotes[`ctrl+shift+a → shift+1`] +:sc_set_background_opacity_default: pass:quotes[`ctrl+shift+a → shift+d`] +:sc_set_background_opacity_plus0_1: pass:quotes[`ctrl+shift+a → shift+=`] :sc_set_tab_title: pass:quotes[`ctrl+shift+alt+t`] :sc_seventh_window: pass:quotes[`ctrl+shift+7`] :sc_show_scrollback: pass:quotes[`ctrl+shift+h`] @@ -262,6 +266,10 @@ windows are: |Pass current selection to program | {sc_pass_selection_to_program} |Edit kitty config file | {sc_edit_config_file} |Open a kitty shell | {sc_kitty_shell} +|Increase background opacity | {sc_set_background_opacity_plus0_1} +|Decrease background opacity | {sc_set_background_opacity_0_1} +|Remove background opacity | {sc_set_background_opacity_1} +|Reset background opacity | {sc_set_background_opacity_default} |=== diff --git a/kitty/boss.py b/kitty/boss.py index b955982a5..24eb4c8ca 100644 --- a/kitty/boss.py +++ b/kitty/boss.py @@ -19,11 +19,12 @@ appname, config_dir, editor, set_boss, supports_primary_selection ) from .fast_data_types import ( - ChildMonitor, change_background_opacity, create_os_window, - current_os_window, destroy_global_data, destroy_sprite_map, - get_clipboard_string, glfw_post_empty_event, layout_sprite_map, - mark_os_window_for_close, set_clipboard_string, set_dpi_from_os_window, - set_in_sequence_mode, show_window, toggle_fullscreen, viewport_for_window + ChildMonitor, background_opacity_of, change_background_opacity, + create_os_window, current_os_window, destroy_global_data, + destroy_sprite_map, get_clipboard_string, glfw_post_empty_event, + layout_sprite_map, mark_os_window_for_close, set_clipboard_string, + set_dpi_from_os_window, set_in_sequence_mode, show_window, + toggle_fullscreen, viewport_for_window ) from .fonts.render import prerender, resize_fonts, set_font_family from .keys import get_shortcut, shortcut_matches @@ -362,7 +363,23 @@ def on_dpi_change(self, os_window_id): self._change_font_size() def _set_os_window_background_opacity(self, os_window_id, opacity): - change_background_opacity(os_window_id, opacity) + change_background_opacity(os_window_id, max(0.1, min(opacity, 1.0))) + + def set_background_opacity(self, opacity): + window = self.active_window + if window is None or not opacity: + return + os_window_id = window.os_window_id + if opacity[0] in '+-': + opacity = background_opacity_of(os_window_id) + if opacity is None: + return + opacity += float(opacity) + elif opacity == 'default': + opacity = self.opts.background_opacity + else: + opacity = float(opacity) + self._set_os_window_background_opacity(os_window_id, opacity) @property def active_tab_manager(self): diff --git a/kitty/config.py b/kitty/config.py index b2762faf5..4f496598b 100644 --- a/kitty/config.py +++ b/kitty/config.py @@ -124,6 +124,8 @@ def parse_key_action(action): func = 'kitten' elif func == 'goto_tab': args = (max(0, int(rest)), ) + elif func == 'set_background_opacity': + args = [rest] elif func == 'goto_layout' or func == 'kitty_shell': args = [rest] elif func == 'set_font_size': diff --git a/kitty/kitty.conf b/kitty/kitty.conf index 4372011c9..5cea679ec 100644 --- a/kitty/kitty.conf +++ b/kitty/kitty.conf @@ -501,6 +501,10 @@ map kitty_mod+u input_unicode_character map kitty_mod+f2 edit_config_file # Open the kitty shell in a new window/tab/overlay/os_window to control kitty using commands. map kitty_mod+escape kitty_shell window +map kitty_mod+a>shift+= set_background_opacity +0.1 +map kitty_mod+a>shift+- set_background_opacity -0.1 +map kitty_mod+a>shift+1 set_background_opacity 1 +map kitty_mod+a>shift+d set_background_opacity default # Sending arbitrary text on shortcut key presses # You can tell kitty to send arbitrary (UTF-8) encoded text to diff --git a/kitty/state.c b/kitty/state.c index be00f578c..85e1c7b79 100644 --- a/kitty/state.c +++ b/kitty/state.c @@ -517,6 +517,14 @@ PYWRAP1(change_background_opacity) { Py_RETURN_FALSE; } +PYWRAP1(background_opacity_of) { + id_type os_window_id = PyLong_AsUnsignedLongLong(args); + WITH_OS_WINDOW(os_window_id) + return PyFloat_FromDouble((double)os_window->background_opacity); + END_WITH_OS_WINDOW + Py_RETURN_NONE; +} + static inline bool fix_window_idx(Tab *tab, id_type window_id, unsigned int *window_idx) { for (id_type fix = 0; fix < tab->num_windows; fix++) { @@ -645,6 +653,7 @@ static PyMethodDef module_methods[] = { MW(set_titlebar_color, METH_VARARGS), MW(mark_tab_bar_dirty, METH_O), MW(change_background_opacity, METH_VARARGS), + MW(background_opacity_of, METH_O), MW(update_window_visibility, METH_VARARGS), MW(set_boss, METH_O), MW(set_display_state, METH_VARARGS), diff --git a/preprocess-readme.py b/preprocess-readme.py index a9759f6e7..ae5705fd0 100755 --- a/preprocess-readme.py +++ b/preprocess-readme.py @@ -17,7 +17,7 @@ if line.startswith('map '): _, sc, name = line.split(maxsplit=2) sc = sc.replace('kitty_mod', 'ctrl+shift') - name = name.rstrip().replace(' ', '_').replace('-', '_').replace('___', '_').replace('__', '_').strip('_') + name = name.rstrip().replace(' ', '_').replace('-', '_').replace('+', 'plus').replace('.', '_').replace('___', '_').replace('__', '_').strip('_') defns[name].append('`' + sc.replace('>', ' → ') + '`') defns = [