From bfb87854df3a292a9e83cb237e954ed15b52b526 Mon Sep 17 00:00:00 2001 From: Kovid Goyal Date: Fri, 3 Dec 2021 13:38:42 +0530 Subject: [PATCH] Python API to change window logo --- kitty/fast_data_types.pyi | 6 ++++++ kitty/state.c | 38 ++++++++++++++++++++++++++++---------- 2 files changed, 34 insertions(+), 10 deletions(-) diff --git a/kitty/fast_data_types.pyi b/kitty/fast_data_types.pyi index d24b0787f..d8bc5253f 100644 --- a/kitty/fast_data_types.pyi +++ b/kitty/fast_data_types.pyi @@ -1271,9 +1271,11 @@ def set_window_padding(os_window_id: int, tab_id: int, window_id: int, left: int def click_mouse_url(os_window_id: int, tab_id: int, window_id: int) -> bool: pass + def click_mouse_cmd_output(os_window_id: int, tab_id: int, window_id: int, select_cmd_output: bool) -> bool: pass + def move_cursor_to_mouse_if_in_prompt(os_window_id: int, tab_id: int, window_id: int) -> bool: pass @@ -1282,6 +1284,10 @@ def mouse_selection(os_window_id: int, tab_id: int, window_id: int, code: int, b pass +def set_window_logo(os_window_id: int, tab_id: int, window_id: int, path: str, position: str) -> None: + pass + + def apply_options_update() -> None: pass diff --git a/kitty/state.c b/kitty/state.c index 195e05c8e..383cde582 100644 --- a/kitty/state.c +++ b/kitty/state.c @@ -224,21 +224,22 @@ release_gpu_resources_for_window(Window *w) { w->render_data.gvao_idx = -1; } -static void -set_default_window_logo(Window *w) { - if (OPT(default_window_logo)) { - WindowLogo *wl = find_or_create_window_logo(&global_state.all_window_logos, OPT(default_window_logo)); +static bool +set_window_logo(Window *w, const char *path, const ImageAnchorPosition pos, bool is_default) { + bool ok = false; + if (path && path[0]) { + WindowLogo *wl = find_or_create_window_logo(&global_state.all_window_logos, path); if (wl) { w->window_logo.instance = wl; - w->window_logo.position = OPT(window_logo_position); - } else if (PyErr_Occurred()) { - log_error("Failed to load default window logo: %s", OPT(default_window_logo)); - PyErr_Print(); + w->window_logo.position = pos; + ok = true; } } else { decref_window_logo(&global_state.all_window_logos, &w->window_logo.instance); + ok = true; } - w->window_logo.using_default = true; + w->window_logo.using_default = is_default; + return ok; } static void @@ -247,7 +248,10 @@ initialize_window(Window *w, PyObject *title, bool init_gpu_resources) { w->visible = true; w->title = title; Py_XINCREF(title); - set_default_window_logo(w); + if (!set_window_logo(w, OPT(default_window_logo), OPT(window_logo_position), true)) { + log_error("Failed to load default window logo: %s", OPT(default_window_logo)); + if (PyErr_Occurred()) PyErr_Print(); + } if (init_gpu_resources) create_gpu_resources_for_window(w); else { w->render_data.vao_idx = -1; @@ -1139,6 +1143,19 @@ pymouse_selection(PyObject *self UNUSED, PyObject *args) { Py_RETURN_NONE; } +PYWRAP1(set_window_logo) { + id_type os_window_id, tab_id, window_id; + const char *path; PyObject *position; + PA("KKKsU", &os_window_id, &tab_id, &window_id, &path, &position); + bool ok = false; + WITH_WINDOW(os_window_id, tab_id, window_id); + ok = set_window_logo(window, path, bganchor(position), false); + END_WITH_WINDOW; + if (ok) Py_RETURN_TRUE; + Py_RETURN_FALSE; +} + + PYWRAP1(click_mouse_url) { id_type a, b, c; PA("KKK", &a, &b, &c); if (click_mouse_url(a, b, c)) { Py_RETURN_TRUE; } @@ -1190,6 +1207,7 @@ static PyMethodDef module_methods[] = { MW(move_cursor_to_mouse_if_in_prompt, METH_VARARGS), MW(redirect_mouse_handling, METH_O), MW(mouse_selection, METH_VARARGS), + MW(set_window_logo, METH_VARARGS), MW(set_in_sequence_mode, METH_O), MW(resolve_key_mods, METH_VARARGS), MW(handle_for_window_id, METH_VARARGS),