From 60ee969028ee5119cd8a92998b48bdb5b7b40c62 Mon Sep 17 00:00:00 2001 From: Kovid Goyal Date: Sun, 22 Feb 2026 06:31:13 +0530 Subject: [PATCH] Cleanup previous PR --- kitty/fast_data_types.pyi | 2 +- kitty/glfw.c | 30 ++++++++++++++++-------------- kitty/tabs.py | 12 ++++++++---- 3 files changed, 25 insertions(+), 19 deletions(-) diff --git a/kitty/fast_data_types.pyi b/kitty/fast_data_types.pyi index d4a52cdcd..08a13bbbf 100644 --- a/kitty/fast_data_types.pyi +++ b/kitty/fast_data_types.pyi @@ -1818,7 +1818,7 @@ def start_drag_with_data( operations: int = GLFW_DRAG_OPERATION_MOVE ) -> None: ... -def draw_drag_icon_title(os_window_id: int, text: str, fg: int, bg: int, width: int) -> bytes: ... +def draw_single_line_of_text(os_window_id: int, text: str, fg: int, bg: int, width: int, padding_y: int = 2) -> bytes: ... def set_tab_being_dragged(tab_id: int = 0, drag_started: bool = False, x: float = 0, y: float = 0) -> None: ... def get_tab_being_dragged() -> tuple[int, bool, float, float]: ... def request_callback_with_thumbnail( diff --git a/kitty/glfw.c b/kitty/glfw.c index 68f9ecf62..74c4b38c8 100644 --- a/kitty/glfw.c +++ b/kitty/glfw.c @@ -2760,25 +2760,27 @@ grab_keyboard(PyObject *self UNUSED, PyObject *action) { } static PyObject* -draw_drag_icon_title(PyObject *self UNUSED, PyObject *args) { +draw_single_line_of_text(PyObject *self UNUSED, PyObject *args) { unsigned long long os_window_id; const char *text; unsigned int fg, bg; - int width; - if (!PyArg_ParseTuple(args, "KsIIi", &os_window_id, &text, &fg, &bg, &width)) return NULL; + int width, padding_y = 2; + if (!PyArg_ParseTuple(args, "KsIIi|i", &os_window_id, &text, &fg, &bg, &width, &padding_y)) return NULL; OSWindow *w = os_window_for_id(os_window_id); - if (!w || !w->fonts_data) { PyErr_SetString(PyExc_KeyError, "OS Window with specified id does not exist or has no fonts data"); return NULL; } - double font_sz_pts = w->fonts_data->font_sz_in_pts; - double ydpi = w->fonts_data->logical_dpi_y; - size_t height = (size_t)w->fonts_data->fcm.cell_height + 2; // extra 2px for visual padding - size_t buf_sz = (size_t)width * height * 4; - RAII_ALLOC(uint8_t, buf, calloc(buf_sz, 1)); - if (!buf) return PyErr_NoMemory(); - if (!draw_window_title(font_sz_pts, ydpi, text, fg, bg, buf, width, height)) { - if (!PyErr_Occurred()) PyErr_SetString(PyExc_RuntimeError, "Failed to render drag icon title"); + if (!w || !w->fonts_data) { + PyErr_SetString(PyExc_KeyError, "OS Window with specified id does not exist or has no fonts data"); return NULL; } - return PyBytes_FromStringAndSize((char*)buf, buf_sz); + double font_sz_pts = w->fonts_data->font_sz_in_pts; + double ydpi = w->fonts_data->logical_dpi_y; + size_t height = (size_t)w->fonts_data->fcm.cell_height + padding_y; + size_t buf_sz = (size_t)width * height * 4; + RAII_PyObject(ans, PyBytes_FromStringAndSize(NULL, buf_sz)); if (!ans) return NULL; + if (!draw_window_title(font_sz_pts, ydpi, text, fg, bg, (uint8_t*)PyBytes_AS_STRING(ans), width, height)) { + if (!PyErr_Occurred()) PyErr_SetString(PyExc_RuntimeError, "Failed to render text"); + return NULL; + } + return Py_NewRef(ans); } static PyObject* @@ -2825,7 +2827,7 @@ static PyMethodDef module_methods[] = { METHODB(pointer_name_to_css_name, METH_O), {"create_os_window", (PyCFunction)(void (*) (void))(create_os_window), METH_VARARGS | METH_KEYWORDS, NULL}, {"start_drag_with_data", (PyCFunction)(void (*) (void))(start_drag_with_data), METH_VARARGS | METH_KEYWORDS, NULL}, - METHODB(draw_drag_icon_title, METH_VARARGS), + METHODB(draw_single_line_of_text, METH_VARARGS), METHODB(set_default_window_icon, METH_VARARGS), METHODB(set_os_window_icon, METH_VARARGS), METHODB(set_clipboard_data_types, METH_VARARGS), diff --git a/kitty/tabs.py b/kitty/tabs.py index f2ada74d2..4dd3ea93e 100644 --- a/kitty/tabs.py +++ b/kitty/tabs.py @@ -28,7 +28,7 @@ buffer_keys_in_window, current_focused_os_window_id, detach_window, - draw_drag_icon_title, + draw_single_line_of_text, get_boss, get_click_interval, get_options, @@ -1642,9 +1642,13 @@ def start_tab_drag(self, pixels: bytes, width: int, height: int) -> None: title = replace_c0_codes_except_nl_space_tab(title.encode()).decode() title = re.sub(r'\n', ' ', title) opts = get_options() - fg = 0xff000000 | color_as_int(opts.active_tab_foreground) - bg = 0xff000000 | color_as_int(opts.active_tab_background) - title_pixels = draw_drag_icon_title(self.os_window_id, title, fg, bg, width) + if td.is_active: + fg = color_as_int(opts.active_tab_foreground) + bg = color_as_int(opts.active_tab_background) + else: + fg = color_as_int(opts.inactive_tab_foreground) + bg = color_as_int(opts.inactive_tab_background) + title_pixels = draw_single_line_of_text(self.os_window_id, title, 0xff000000 | fg, 0xff000000 | bg, width) title_height = len(title_pixels) // (width * 4) drag_data = { f'application/net.kovidgoyal.kitty-tab-{os.getpid()}': str(tab.id).encode(),