From 45282eef7de2dadd31a0c31be145b6e5bdca4e03 Mon Sep 17 00:00:00 2001 From: Manuel Villanueva <1mm.villanueva1@gmail.com> Date: Thu, 29 Oct 2026 15:35:48 -0600 Subject: [PATCH] Wayland: inherit GNOME titlebar preferences through the desktop portal --- docs/wayland-gnome-csd.md | 68 ++++++++----- glfw/glfw3.h | 5 +- glfw/input.c | 6 +- glfw/linux_desktop_settings.c | 152 ++++++++++++++++++++++++++---- glfw/linux_desktop_settings.h | 13 ++- glfw/tests/desktop_appearance.c | 80 +++++++++++++++- glfw/tests/gnome_csd.c | 26 +++++ glfw/wl_client_side_decorations.c | 79 ++++++++++++---- glfw/wl_window.c | 14 +++ kitty/glfw-wrapper.h | 2 +- kitty/glfw.c | 23 ++++- 11 files changed, 398 insertions(+), 70 deletions(-) diff --git a/docs/wayland-gnome-csd.md b/docs/wayland-gnome-csd.md index 1ca9ced8c..bfc699b17 100644 --- a/docs/wayland-gnome-csd.md +++ b/docs/wayland-gnome-csd.md @@ -14,27 +14,49 @@ python3 setup.py build -o wayland_titlebar_color=system ``` -The renderer draws a 38 logical-pixel header, centered bold title, 24-pixel -circular controls spaced 36 pixels apart, antialiased upper corners and matching -shadows. Maximized/tiled headers have square corners. Narrow headers prioritize -the close control. A control activates only after a matching press/release. -Existing movement, resizing, maximize/restore, menu and fullscreen behavior is -preserved. +The renderer draws a header of at least 38 logical pixels, centered title, +24-pixel circular controls spaced 36 pixels apart, antialiased upper corners +and matching shadows. The header grows to accommodate the selected font and +text scale. Maximized/tiled headers have square corners. Narrow headers hide +minimize/maximize controls before close, when those controls are configured. +A control activates only after a matching press/release. With `wayland_titlebar_color system`, the palette follows the desktop's color -scheme, including live light/dark transitions. When the desktop expresses no -preference, the palette follows the terminal background's luminance. An explicit -`wayland_titlebar_color` takes precedence, with contrasting text and controls. +scheme, including live light/dark transitions. Without a desktop preference, +the palette follows the terminal background's luminance. Explicit colors still +take precedence, with contrasting text and controls. -The interface font family and point size come from the existing asynchronous -portal settings query and are rendered bold through FreeType/Fontconfig. Font -changes redraw the header. No settings subprocess or additional blocking query -is added at startup. If no font is exposed, the fallback is sans-serif 11. +The existing asynchronous portal query also reads these GNOME settings: -This approximates GNOME's appearance; it does not interpret GTK/Yaru CSS, arbitrary -Pango font-description styling, GNOME button layout or text scaling settings. -The lower terminal corners remain square. Visual review used Ubuntu 26.10 / -GNOME Shell 51.0; Ubuntu 24.10 has not been tested. +| Namespace | Settings used | +| --- | --- | +| `org.gnome.desktop.interface` | `font-name`, `text-scaling-factor` | +| `org.gnome.desktop.wm.preferences` | `titlebar-font`, `titlebar-uses-system-font`, `button-layout` | +| `org.gnome.desktop.wm.preferences` | `action-double-click-titlebar`, `action-middle-click-titlebar`, `action-right-click-titlebar` | + +These affect the titlebar, not terminal text. Font preferences select either +GNOME's interface font (bold for the title) or the configured title font. +Family, point size and trailing Bold/Italic/Oblique/Regular/Normal styles are +supported through FreeType/Fontconfig. Text scale is validated between 0.5 and +4.0. Font or scale changes rebuild the cached font/header as needed. Normal +windows keep their terminal content size; docked windows keep their exterior +size while the header changes height. + +Button layout respects left/right placement, ordering and omitted controls. +Duplicate or unsupported control names are ignored; malformed layouts retain +the last valid layout. Supported titlebar actions are menu, toggle-maximize, +minimize and none. Unsupported actions such as lower, shade or single-axis +maximize are treated as no action; no equivalent xdg-shell request is used. + +Updates are applied live through portal SettingChanged signals, without a new +settings subprocess or blocking query. On backends that omit these GNOME keys, +fallbacks are sans-serif 11, scale 1, controls minimize/maximize/close on the +right, double-click maximize, right-click menu and no middle-click action. + +This approximates GNOME appearance. GTK/Yaru CSS, arbitrary Pango font styling, +theme-specific decoration palettes, icon artwork and shadow geometry are not +interpreted. Lower terminal corners remain square. Visual review used Ubuntu +26.10 / GNOME Shell 51.0; Ubuntu 24.10 has not been tested. The rounded header cutouts contain the same shadow field as the adjacent surfaces, including inactive opacity. A smooth outer envelope removes the finite @@ -45,9 +67,11 @@ full-window atlas. The renderer test checks buffer bounds at 100/125/150/200% scale, tiny/narrow/normal widths, focused/unfocused/maximized states, dark/light/dark repainting, explicit -custom colors, control spacing and header/shadow continuity. A separate test -feeds real D-Bus appearance signal messages to the portal handler without -changing desktop settings. +custom colors, control spacing, split/empty layouts, narrow-window priorities, +header growth and header/shadow continuity. A separate test feeds real D-Bus +settings signal messages through the handler to validate appearance, font +selection, text scale, invalid values, duplicate controls and titlebar actions, +without changing desktop settings. From the repository root: @@ -60,8 +84,8 @@ gcc -D_GLFW_WAYLAND -Iglfw $(pkg-config --cflags dbus-1) \ gcc -D_GLFW_WAYLAND -Iglfw $(pkg-config --cflags dbus-1) \ -Wall -Wextra -Werror -Wno-missing-field-initializers -std=c11 -pedantic-errors \ - -ffunction-sections -fdata-sections glfw/tests/desktop_appearance.c \ - -Wl,--gc-sections $(pkg-config --libs dbus-1) -o /tmp/kitty-appearance-test + -ffunction-sections -fdata-sections glfw/tests/desktop_appearance.c glfw/dbus_glfw.c \ + -Wl,--gc-sections $(pkg-config --libs dbus-1) -lm -o /tmp/kitty-appearance-test /tmp/kitty-appearance-test ``` diff --git a/glfw/glfw3.h b/glfw/glfw3.h index 3ac755f8e..4deb3840d 100644 --- a/glfw/glfw3.h +++ b/glfw/glfw3.h @@ -4218,8 +4218,9 @@ GLFWAPI GLFWsystemcolorthemechangefun glfwSetSystemColorThemeChangeCallback(GLFW GLFWAPI GLFWclipboardlostfun glfwSetClipboardLostCallback(GLFWclipboardlostfun callback); GLFWAPI GLFWColorScheme glfwGetCurrentSystemColorTheme(bool query_if_unintialized); -/*! @brief Returns the desktop interface font description, or NULL if unavailable. */ -GLFWAPI const char* glfwGetSystemFont(void); +/*! @brief Returns the selected desktop titlebar font, or NULL if unavailable. + * Optional outputs give text scale and the default bold style. */ +GLFWAPI const char* glfwGetSystemFont(double *text_scale, bool *bold); /*! @brief Sets the refresh callback for the specified window. * diff --git a/glfw/input.c b/glfw/input.c index 0ab2bd93b..b74db6415 100644 --- a/glfw/input.c +++ b/glfw/input.c @@ -1599,10 +1599,12 @@ glfwGetTime(void) { } GLFWAPI const char * -glfwGetSystemFont(void) { +glfwGetSystemFont(double *text_scale, bool *bold) { #if defined(_GLFW_WAYLAND) || defined(_GLFW_X11) - return glfw_current_system_font(); + return glfw_current_system_font(text_scale, bold); #else + if (text_scale) *text_scale = 1.; + if (bold) *bold = true; return NULL; #endif } diff --git a/glfw/linux_desktop_settings.c b/glfw/linux_desktop_settings.c index 1c3131301..70ca804df 100644 --- a/glfw/linux_desktop_settings.c +++ b/glfw/linux_desktop_settings.c @@ -9,6 +9,7 @@ #include #include #include +#include #ifdef _GLFW_WAYLAND GLFWAPI void glfwWaylandRedrawCSDWindowTitle(GLFWwindow *window); #endif @@ -17,18 +18,137 @@ GLFWAPI void glfwWaylandRedrawCSDWindowTitle(GLFWwindow *window); #define DESKTOP_PATH "/org/freedesktop/portal/desktop" #define DESKTOP_INTERFACE "org.freedesktop.portal.Settings" #define GNOME_DESKTOP_NAMESPACE "org.gnome.desktop.interface" +#define GNOME_WM_NAMESPACE "org.gnome.desktop.wm.preferences" #define FDO_DESKTOP_NAMESPACE "org.freedesktop.appearance" -static const char *supported_namespaces[2] = {FDO_DESKTOP_NAMESPACE, GNOME_DESKTOP_NAMESPACE}; +static const char *supported_namespaces[3] = {FDO_DESKTOP_NAMESPACE, GNOME_DESKTOP_NAMESPACE, GNOME_WM_NAMESPACE}; #define FDO_APPEARANCE_KEY "color-scheme" static char theme_name[128] = {0}; static char interface_font[256] = "sans-serif 11"; -const char * -glfw_current_system_font(void) { - return interface_font; +static char titlebar_font[256]; +static bool titlebar_uses_system_font = true; +static double titlebar_text_scale = 1.; +static GLFWTitlebarAction titlebar_actions[] = {TITLEBAR_ACTION_MAXIMIZE, TITLEBAR_ACTION_NONE, TITLEBAR_ACTION_MENU}; +GLFWTitlebarAction glfw_current_titlebar_action(unsigned gesture) { + return gesture < arraysz(titlebar_actions) ? titlebar_actions[gesture] : TITLEBAR_ACTION_NONE; } +static GLFWTitlebarButtonLayout button_layout = {.right = {3, 2, 1}, .right_count = 3}; + +const GLFWTitlebarButtonLayout * +glfw_current_titlebar_button_layout(void) { return &button_layout; } + +double +glfw_current_titlebar_text_scale(void) { return titlebar_text_scale; } + +const char * +glfw_current_system_font(double *text_scale, bool *bold) { + bool system_font = titlebar_uses_system_font || !titlebar_font[0]; + if (text_scale) *text_scale = titlebar_text_scale; + if (bold) *bold = system_font; + return system_font ? interface_font : titlebar_font; +} + +unsigned +glfw_current_titlebar_height(void) { + const char *font = glfw_current_system_font(NULL, NULL); + const char *size = strrchr(font, ' '); + double points = 11; + if (size) { + char *end; + double parsed = strtod(size + 1, &end); + if (end != size + 1 && !*end && isfinite(parsed) && parsed >= 1 && parsed <= 200) points = parsed; + } + return (unsigned)ceil(fmax(38., points * 96. / 72. + 18) * fmax(1, titlebar_text_scale)); +} + +static void +redraw_titlebars(void) { +#ifdef _GLFW_WAYLAND + for (_GLFWwindow *w = _glfw.windowListHead; w; w = w->next) { + w->wl.decorations.pressed_button = 0; + w->wl.decorations.close.hovered = w->wl.decorations.maximize.hovered = w->wl.decorations.minimize.hovered = false; + glfwWaylandRedrawCSDWindowTitle((GLFWwindow *)w); + } +#endif +} + +static bool +parse_button_layout(const char *value, GLFWTitlebarButtonLayout *out) { + if (!value || strlen(value) >= 256) return false; + char copy[256]; + snprintf(copy, sizeof(copy), "%s", value); + char *colon = strchr(copy, ':'); + if (!colon || strchr(colon + 1, ':')) return false; + *colon = 0; + memset(out, 0, sizeof(*out)); + bool seen[4] = {false}; + for (int side = 0; side < 2; side++) { + char *token = side ? colon + 1 : copy; + while (token && *token) { + char *next = strchr(token, ','); + if (next) *next++ = 0; + while (*token == ' ') token++; + char *end = token + strlen(token); + while (end > token && end[-1] == ' ') *--end = 0; + int id = strcmp(token, "close") == 0 ? 1 : strcmp(token, "maximize") == 0 ? 2 : strcmp(token, "minimize") == 0 ? 3 : 0; + if (id && !seen[id]) { + seen[id] = true; + if (side) out->right[out->right_count++] = id; + else out->left[out->left_count++] = id; + } + token = next; + } + } + return true; +} + +static bool +read_string_setting(DBusMessageIter *value, char *dest, size_t capacity) { + if (dbus_message_iter_get_arg_type(value) != DBUS_TYPE_STRING) return false; + const char *text; + dbus_message_iter_get_basic(value, &text); + if (!text || !text[0] || strlen(text) >= capacity || strcmp(dest, text) == 0) return false; + snprintf(dest, capacity, "%s", text); + return true; +} + +static void +process_wm_setting(const char *key, DBusMessageIter *value) { + bool changed = false; + if (strcmp(key, "titlebar-font") == 0) changed = read_string_setting(value, titlebar_font, sizeof(titlebar_font)); + else if (strcmp(key, "titlebar-uses-system-font") == 0 && dbus_message_iter_get_arg_type(value) == DBUS_TYPE_BOOLEAN) { + dbus_bool_t flag; + dbus_message_iter_get_basic(value, &flag); + changed = titlebar_uses_system_font != (flag != 0); + titlebar_uses_system_font = flag != 0; + } else if (strcmp(key, "button-layout") == 0 && dbus_message_iter_get_arg_type(value) == DBUS_TYPE_STRING) { + const char *text; + dbus_message_iter_get_basic(value, &text); + GLFWTitlebarButtonLayout parsed; + if (parse_button_layout(text, &parsed) && memcmp(&parsed, &button_layout, sizeof(parsed)) != 0) { + button_layout = parsed; + changed = true; + } + } + static const char *action_keys[] = {"action-double-click-titlebar", "action-middle-click-titlebar", "action-right-click-titlebar"}; + for (unsigned i = 0; i < arraysz(action_keys); i++) { + if (strcmp(key, action_keys[i]) == 0 && dbus_message_iter_get_arg_type(value) == DBUS_TYPE_STRING) { + const char *name; + dbus_message_iter_get_basic(value, &name); + // xdg-shell has no lower/shade or one-axis maximize request. + GLFWTitlebarAction action = strcmp(name, "menu") == 0 ? TITLEBAR_ACTION_MENU : + strcmp(name, "toggle-maximize") == 0 ? TITLEBAR_ACTION_MAXIMIZE : + strcmp(name, "minimize") == 0 ? TITLEBAR_ACTION_MINIMIZE : TITLEBAR_ACTION_NONE; + changed |= action != titlebar_actions[i]; + titlebar_actions[i] = action; + break; + } + } + if (changed) redraw_titlebars(); +} + static int theme_size = -1; static GLFWColorScheme appearance = GLFW_COLOR_SCHEME_NO_PREFERENCE; static bool cursor_theme_changed = false, appearance_initialized = false; @@ -158,16 +278,13 @@ process_gnome_setting(const char *key, DBusMessageIter *value) { } } } else if (strcmp(key, "font-name") == 0) { - if (dbus_message_iter_get_arg_type(value) == DBUS_TYPE_STRING) { - const char *name; - dbus_message_iter_get_basic(value, &name); - if (name && name[0] && strcmp(name, interface_font) != 0) { - snprintf(interface_font, sizeof(interface_font), "%s", name); -#ifdef _GLFW_WAYLAND - for (_GLFWwindow *w = _glfw.windowListHead; w; w = w->next) - glfwWaylandRedrawCSDWindowTitle((GLFWwindow *)w); -#endif - } + if (read_string_setting(value, interface_font, sizeof(interface_font))) redraw_titlebars(); + } else if (strcmp(key, "text-scaling-factor") == 0 && dbus_message_iter_get_arg_type(value) == DBUS_TYPE_DOUBLE) { + double factor; + dbus_message_iter_get_basic(value, &factor); + if (isfinite(factor) && factor >= .5 && factor <= 4 && factor != titlebar_text_scale) { + titlebar_text_scale = factor; + redraw_titlebars(); } } else if (strcmp(key, "cursor-theme") == 0) { if (dbus_message_iter_get_arg_type(value) == DBUS_TYPE_STRING) { @@ -220,6 +337,8 @@ while (dbus_message_iter_get_arg_type(&array) == DBUS_TYPE_DICT_ENTRY) { process_settings_dict(&settings, process_fdo_setting); } else if (strcmp(namespace, GNOME_DESKTOP_NAMESPACE) == 0) { process_settings_dict(&settings, process_gnome_setting); + } else if (strcmp(namespace, GNOME_WM_NAMESPACE) == 0) { + process_settings_dict(&settings, process_wm_setting); } } } @@ -304,12 +423,13 @@ setting_changed(DBusConnection *conn UNUSED, DBusMessage *msg, void *user_data U DBUS_TYPE_INVALID)) { if (strcmp(namespace, FDO_DESKTOP_NAMESPACE) == 0) { if (strcmp(key, FDO_APPEARANCE_KEY) == 0) { on_color_scheme_change(msg); } - } else if (strcmp(namespace, GNOME_DESKTOP_NAMESPACE) == 0 && strcmp(key, "font-name") == 0) { + } else if (strcmp(namespace, GNOME_DESKTOP_NAMESPACE) == 0 || strcmp(namespace, GNOME_WM_NAMESPACE) == 0) { DBusMessageIter iter, value; if (dbus_message_iter_init(msg, &iter) && dbus_message_iter_next(&iter) && dbus_message_iter_next(&iter) && dbus_message_iter_get_arg_type(&iter) == DBUS_TYPE_VARIANT) { dbus_message_iter_recurse(&iter, &value); - process_gnome_setting(key, &value); + if (strcmp(namespace, GNOME_WM_NAMESPACE) == 0) process_wm_setting(key, &value); + else process_gnome_setting(key, &value); } } } diff --git a/glfw/linux_desktop_settings.h b/glfw/linux_desktop_settings.h index 4ec25c90d..05453a262 100644 --- a/glfw/linux_desktop_settings.h +++ b/glfw/linux_desktop_settings.h @@ -14,4 +14,15 @@ void glfw_initialize_desktop_settings(void); void glfw_current_cursor_theme(const char **theme, int *size); GLFWColorScheme glfw_current_system_color_theme(bool); -const char *glfw_current_system_font(void); +typedef struct { + int left[3], right[3]; // close=1, maximize=2, minimize=3 + unsigned left_count, right_count; +} GLFWTitlebarButtonLayout; + +typedef enum { TITLEBAR_ACTION_NONE, TITLEBAR_ACTION_MENU, TITLEBAR_ACTION_MAXIMIZE, TITLEBAR_ACTION_MINIMIZE } GLFWTitlebarAction; +// Gesture: 0=double click, 1=middle click, 2=right click. +GLFWTitlebarAction glfw_current_titlebar_action(unsigned gesture); +const GLFWTitlebarButtonLayout *glfw_current_titlebar_button_layout(void); +double glfw_current_titlebar_text_scale(void); +unsigned glfw_current_titlebar_height(void); +const char *glfw_current_system_font(double *text_scale, bool *bold); diff --git a/glfw/tests/desktop_appearance.c b/glfw/tests/desktop_appearance.c index 5cc6a4277..69991b8cb 100644 --- a/glfw/tests/desktop_appearance.c +++ b/glfw/tests/desktop_appearance.c @@ -2,7 +2,12 @@ /* Exercise portal appearance replies/signals without changing desktop settings. */ #include "../linux_desktop_settings.c" #include +#include +_GLFWlibrary _glfw; +static unsigned redraws; +GLFWAPI void glfwWaylandRedrawCSDWindowTitle(GLFWwindow *window) { (void)window; redraws++; } +void _glfwInputError(int code, const char *format, ...) { (void)code; (void)format; abort(); } static unsigned notifications; static GLFWColorScheme received; void @@ -24,7 +29,23 @@ change_scheme(uint32_t value) { assert(dbus_message_iter_open_container(&iter, DBUS_TYPE_VARIANT, "u", &variant)); assert(dbus_message_iter_append_basic(&variant, DBUS_TYPE_UINT32, &value)); assert(dbus_message_iter_close_container(&iter, &variant)); - on_color_scheme_change(msg); + setting_changed(NULL, msg, NULL); + dbus_message_unref(msg); +} + +static void +change_setting(const char *namespace, const char *key, int type, const void *data) { + DBusMessage *msg = dbus_message_new_signal(DESKTOP_PATH, DESKTOP_INTERFACE, "SettingChanged"); + assert(msg); + DBusMessageIter iter, variant; + char signature[] = {(char)type, 0}; + dbus_message_iter_init_append(msg, &iter); + assert(dbus_message_iter_append_basic(&iter, DBUS_TYPE_STRING, &namespace)); + assert(dbus_message_iter_append_basic(&iter, DBUS_TYPE_STRING, &key)); + assert(dbus_message_iter_open_container(&iter, DBUS_TYPE_VARIANT, signature, &variant)); + assert(dbus_message_iter_append_basic(&variant, type, data)); + assert(dbus_message_iter_close_container(&iter, &variant)); + setting_changed(NULL, msg, NULL); dbus_message_unref(msg); } @@ -39,6 +60,61 @@ main(void) { assert(notifications == 3 && received == GLFW_COLOR_SCHEME_DARK); change_scheme(1); assert(notifications == 3); - puts("Desktop portal appearance: dark/light/dark signals passed"); + _GLFWwindow window = {0}; + _glfw.windowListHead = &window; + double factor; + bool bold; + assert(strcmp(glfw_current_system_font(&factor, &bold), "sans-serif 11") == 0 && factor == 1 && bold); + const char *font = "Ubuntu Sans Bold Italic 13"; + change_setting(GNOME_WM_NAMESPACE, "titlebar-font", DBUS_TYPE_STRING, &font); + dbus_bool_t flag = false; + change_setting(GNOME_WM_NAMESPACE, "titlebar-uses-system-font", DBUS_TYPE_BOOLEAN, &flag); + assert(strcmp(glfw_current_system_font(&factor, &bold), font) == 0 && !bold); + flag = true; + change_setting(GNOME_WM_NAMESPACE, "titlebar-uses-system-font", DBUS_TYPE_BOOLEAN, &flag); + const char *interface = "Ubuntu Sans 12"; + change_setting(GNOME_DESKTOP_NAMESPACE, "font-name", DBUS_TYPE_STRING, &interface); + assert(strcmp(glfw_current_system_font(&factor, &bold), interface) == 0 && bold); + double scale = 2.; + change_setting(GNOME_DESKTOP_NAMESPACE, "text-scaling-factor", DBUS_TYPE_DOUBLE, &scale); + assert(glfw_current_titlebar_text_scale() == 2.); + assert(glfw_current_titlebar_height() == 76); + const char *large = "Ubuntu Sans 32"; + change_setting(GNOME_DESKTOP_NAMESPACE, "font-name", DBUS_TYPE_STRING, &large); + assert(glfw_current_titlebar_height() == 122); + change_setting(GNOME_DESKTOP_NAMESPACE, "font-name", DBUS_TYPE_STRING, &interface); + unsigned previous = redraws; + scale = NAN; + change_setting(GNOME_DESKTOP_NAMESPACE, "text-scaling-factor", DBUS_TYPE_DOUBLE, &scale); + scale = -1.; + change_setting(GNOME_DESKTOP_NAMESPACE, "text-scaling-factor", DBUS_TYPE_DOUBLE, &scale); + scale = 9.; + change_setting(GNOME_DESKTOP_NAMESPACE, "text-scaling-factor", DBUS_TYPE_DOUBLE, &scale); + assert(glfw_current_titlebar_text_scale() == 2. && redraws == previous); + const char *buttons = "close:maximize,minimize,close,appmenu"; + change_setting(GNOME_WM_NAMESPACE, "button-layout", DBUS_TYPE_STRING, &buttons); + assert(button_layout.left_count == 1 && button_layout.left[0] == 1); + assert(button_layout.right_count == 2 && button_layout.right[0] == 2 && button_layout.right[1] == 3); + previous = redraws; + buttons = "close::maximize"; + change_setting(GNOME_WM_NAMESPACE, "button-layout", DBUS_TYPE_STRING, &buttons); + assert(button_layout.left_count == 1 && redraws == previous); + buttons = ":"; + change_setting(GNOME_WM_NAMESPACE, "button-layout", DBUS_TYPE_STRING, &buttons); + assert(button_layout.left_count == 0 && button_layout.right_count == 0); + const char *action = "minimize"; + change_setting(GNOME_WM_NAMESPACE, "action-double-click-titlebar", DBUS_TYPE_STRING, &action); + assert(glfw_current_titlebar_action(0) == TITLEBAR_ACTION_MINIMIZE); + action = "none"; + change_setting(GNOME_WM_NAMESPACE, "action-right-click-titlebar", DBUS_TYPE_STRING, &action); + assert(glfw_current_titlebar_action(2) == TITLEBAR_ACTION_NONE); + action = "menu"; + change_setting(GNOME_WM_NAMESPACE, "action-middle-click-titlebar", DBUS_TYPE_STRING, &action); + assert(glfw_current_titlebar_action(1) == TITLEBAR_ACTION_MENU); + action = "lower"; + change_setting(GNOME_WM_NAMESPACE, "action-middle-click-titlebar", DBUS_TYPE_STRING, &action); + assert(glfw_current_titlebar_action(1) == TITLEBAR_ACTION_NONE); + assert(redraws > 0); + puts("Desktop portal: appearance, font preferences, scale validation and button layout signals passed"); return 0; } diff --git a/glfw/tests/gnome_csd.c b/glfw/tests/gnome_csd.c index d72c3da34..8005a7777 100644 --- a/glfw/tests/gnome_csd.c +++ b/glfw/tests/gnome_csd.c @@ -9,6 +9,11 @@ _glfwWindowForId(GLFWid id) { (void)id; abort(); } +static GLFWTitlebarButtonLayout layout = {.right = {3, 2, 1}, .right_count = 3}; +static double text_scale = 1.; +const GLFWTitlebarButtonLayout *glfw_current_titlebar_button_layout(void) { return &layout; } +double glfw_current_titlebar_text_scale(void) { return text_scale; } +unsigned glfw_current_titlebar_height(void) { return (unsigned)ceil(38 * fmax(1, text_scale)); } static GLFWColorScheme scheme = GLFW_COLOR_SCHEME_DARK; GLFWAPI GLFWColorScheme glfwGetCurrentSystemColorTheme(bool query) { @@ -96,6 +101,27 @@ main(void) { assert(custom_pixels[19 * 128] == (0xff000000 | custom_colors[i])); } } + w.wl.decorations.use_custom_titlebar_color = false; + w.wl.decorations.titlebar.buffer.width = 128; + w.wl.decorations.titlebar.buffer.height = 38; + layout = (GLFWTitlebarButtonLayout){.left = {1}, .right = {3, 2}, .left_count = 1, .right_count = 2}; + render_titlebar_pixels(&w, (uint8_t *)custom_pixels); + assert(w.wl.decorations.close.left == 6); + assert(w.wl.decorations.minimize.left == 50 && w.wl.decorations.maximize.left == 86); + layout = (GLFWTitlebarButtonLayout){0}; + render_titlebar_pixels(&w, (uint8_t *)custom_pixels); + assert(!w.wl.decorations.close.width && !w.wl.decorations.maximize.width && !w.wl.decorations.minimize.width); + layout = (GLFWTitlebarButtonLayout){.left = {1, 2, 3}, .left_count = 3}; + w.wl.decorations.titlebar.buffer.width = 40; + render_titlebar_pixels(&w, (uint8_t *)custom_pixels); + assert(w.wl.decorations.close.width == 36); + assert(!w.wl.decorations.maximize.width && !w.wl.decorations.minimize.width); + text_scale = 2.; + csd_initialize_metrics(&w); + assert(w.wl.decorations.metrics.visible_titlebar_height == 76); + text_scale = .75; + csd_initialize_metrics(&w); + assert(w.wl.decorations.metrics.visible_titlebar_height == 38); kernel_type *square = create_shadow_mask(84, 84, 12, 25, .7f, 384, 0); kernel_type *rounded = create_shadow_mask(84, 84, 12, 25, .7f, 384, 12); assert(square && rounded); diff --git a/glfw/wl_client_side_decorations.c b/glfw/wl_client_side_decorations.c index 0517742ed..ae2b642ed 100644 --- a/glfw/wl_client_side_decorations.c +++ b/glfw/wl_client_side_decorations.c @@ -6,6 +6,7 @@ */ #include "wl_client_side_decorations.h" +#include "linux_desktop_settings.h" #include "backend_utils.h" #include @@ -232,7 +233,7 @@ alloc_buffer_pair(uintptr_t window_id, _GLFWWaylandBufferPair *pair, struct wl_s void csd_initialize_metrics(_GLFWwindow *window) { decs.metrics.width = 12; - decs.metrics.top = decs.metrics.width + 38; + decs.metrics.top = decs.metrics.width + glfw_current_titlebar_height(); decs.metrics.visible_titlebar_height = decs.metrics.top - decs.metrics.width; decs.metrics.horizontal = 2 * decs.metrics.width; decs.metrics.vertical = decs.metrics.width + decs.metrics.top; @@ -280,16 +281,39 @@ render_titlebar_pixels(_GLFWwindow *window, uint8_t *output) { // Keep controls inside narrow windows, prioritizing the close button. int slot = MAX(1, (int)round(36 * scale)); int pad = MAX(0, MIN((int)round(6 * scale), (width - slot) / 2)); - int count = 1 + window->wl.wm_capabilities.maximize + window->wl.wm_capabilities.minimize; - count = MIN(count, MAX(0, (width - 2 * pad) / slot)); - int margin = pad + count * slot; + const GLFWTitlebarButtonLayout *layout = glfw_current_titlebar_button_layout(); + bool visible[4] = {false}; + for (unsigned side = 0; side < 2; side++) { + const int *ids = side ? layout->right : layout->left; + unsigned n = side ? layout->right_count : layout->left_count; + for (unsigned i = 0; i < n; i++) { + int id = ids[i]; + if (id == 1 || (id == 2 && window->wl.wm_capabilities.maximize) || (id == 3 && window->wl.wm_capabilities.minimize)) visible[id] = true; + } + } + int count = visible[1] + visible[2] + visible[3]; + int capacity = MAX(0, (width - 2 * pad) / slot); + for (int id = 3; id >= 1 && count > capacity; id--) { + if (visible[id]) { visible[id] = false; count--; } + } + int ids[3], positions[3], used = 0, side_count[2] = {0}; + for (unsigned side = 0; side < 2; side++) { + const int *buttons = side ? layout->right : layout->left; + unsigned n = side ? layout->right_count : layout->left_count; + for (unsigned i = 0; i < n; i++) if (visible[buttons[i]]) side_count[side]++; + int left = side ? width - pad - side_count[side] * slot : pad; + for (unsigned i = 0; i < n; i++) if (visible[buttons[i]]) { + ids[used] = buttons[i]; + positions[used++] = left; + left += slot; + } + } + int margin = pad + MAX(side_count[0], side_count[1]) * slot; if (width > 2 * margin && window->wl.title && _glfw.callbacks.draw_text) _glfw.callbacks.draw_text((GLFWwindow *)window, window->wl.title, fg, bg, output, width, height, margin, 0, margin, false); - int left = width - pad - slot; - for (int button = 0, drawn = 0; button < 3 && drawn < count; button++) { - if (button == 1 && !window->wl.wm_capabilities.maximize) continue; - if (button == 2 && !window->wl.wm_capabilities.minimize) continue; + for (int i = 0; i < used; i++) { + int button = ids[i] - 1, left = positions[i]; bool hovered = button == 0 ? decs.close.hovered : button == 1 ? decs.maximize.hovered : decs.minimize.hovered; uint32_t fill = hovered ? hover : circle; if (hovered && decs.pressed_button == button + 1) fill = dark ? 0xff707070 : 0xffaaaaaa; @@ -315,8 +339,6 @@ render_titlebar_pixels(_GLFWwindow *window, uint8_t *output) { else if (button == 1) { bounds(maximize); } else { bounds(minimize); } #undef bounds - left -= slot; - drawn++; } if (!decs.close.width) decs.close.hovered = false; if (!decs.maximize.width) decs.maximize.hovered = false; @@ -573,7 +595,8 @@ ensure_csd_resources(_GLFWwindow *window) { const double current_scale = _glfwWaylandWindowScale(window); const bool size_changed = (decs.for_window_state.width != window->wl.width || decs.for_window_state.height != window->wl.height || - decs.for_window_state.fscale != current_scale || !decs.mapping.data); + decs.for_window_state.fscale != current_scale || !decs.mapping.data || + (has_titlebar && decs.titlebar.buffer.viewport_height != decs.metrics.visible_titlebar_height)); const bool state_changed = decs.for_window_state.toplevel_states != window->wl.current.toplevel_states; const bool titlebar_state_changed = (has_titlebar && !decs.titlebar.surface) || (!has_titlebar && decs.titlebar.surface); const bool needs_update = focus_changed || size_changed || titlebar_state_changed || decs.buffer_destroyed || state_changed; @@ -842,6 +865,27 @@ handle_pointer_enter(_GLFWwindow *window, struct wl_surface *surface) { decs.dragging = false; } +static void +perform_titlebar_action(_GLFWwindow *window, GLFWTitlebarAction action) { + switch (action) { + case TITLEBAR_ACTION_MENU: + if (window->wl.xdg.toplevel && window->wl.wm_capabilities.window_menu) + xdg_toplevel_show_window_menu(window->wl.xdg.toplevel, _glfw.wl.seat, _glfw.wl.pointer_serial, + (int32_t)x, (int32_t)y - decs.metrics.top); + break; + case TITLEBAR_ACTION_MAXIMIZE: + if (window->wl.wm_capabilities.maximize) { + if (window->wl.current.toplevel_states & TOPLEVEL_STATE_MAXIMIZED) _glfwPlatformRestoreWindow(window); + else _glfwPlatformMaximizeWindow(window); + } + break; + case TITLEBAR_ACTION_MINIMIZE: + if (window->wl.wm_capabilities.minimize) _glfwPlatformIconifyWindow(window); + break; + case TITLEBAR_ACTION_NONE: break; + } +} + static void handle_pointer_button(_GLFWwindow *window, uint32_t button, uint32_t state) { uint32_t edges = XDG_TOPLEVEL_RESIZE_EDGE_NONE; @@ -872,8 +916,7 @@ handle_pointer_button(_GLFWwindow *window, uint32_t button, uint32_t state) { decs.last_click_on_top_decoration_at = monotonic(); if (decs.last_click_on_top_decoration_at - last_click_at <= _glfwPlatformGetDoubleClickInterval(window)) { decs.last_click_on_top_decoration_at = 0; - if (window->wl.current.toplevel_states & TOPLEVEL_STATE_MAXIMIZED) _glfwPlatformRestoreWindow(window); - else _glfwPlatformMaximizeWindow(window); + perform_titlebar_action(window, glfw_current_titlebar_action(0)); return; } } @@ -889,13 +932,9 @@ handle_pointer_button(_GLFWwindow *window, uint32_t button, uint32_t state) { case CSD_shadow_lower_right: edges = XDG_TOPLEVEL_RESIZE_EDGE_BOTTOM_RIGHT; break; } if (edges != XDG_TOPLEVEL_RESIZE_EDGE_NONE) xdg_toplevel_resize(window->wl.xdg.toplevel, _glfw.wl.seat, _glfw.wl.pointer_serial, edges); - } else if (button == BTN_RIGHT) { - if (decs.focus == CSD_titlebar && window->wl.xdg.toplevel) { - if (window->wl.wm_capabilities.window_menu) - xdg_toplevel_show_window_menu(window->wl.xdg.toplevel, _glfw.wl.seat, _glfw.wl.pointer_serial, (int32_t)x, (int32_t)y - decs.metrics.top); - else _glfwInputError(GLFW_PLATFORM_ERROR, "Wayland compositor does not support showing wndow menu"); - return; - } + } else if ((button == BTN_RIGHT || button == BTN_MIDDLE) && state == WL_POINTER_BUTTON_STATE_PRESSED) { + if (decs.focus == CSD_titlebar) + perform_titlebar_action(window, glfw_current_titlebar_action(button == BTN_MIDDLE ? 1 : 2)); } } diff --git a/glfw/wl_window.c b/glfw/wl_window.c index a5843c018..307826f1f 100644 --- a/glfw/wl_window.c +++ b/glfw/wl_window.c @@ -3128,6 +3128,20 @@ glfwWaylandSetTitlebarColor(GLFWwindow *handle, uint32_t color, bool use_system_ GLFWAPI void glfwWaylandRedrawCSDWindowTitle(GLFWwindow *handle) { _GLFWwindow *window = (_GLFWwindow *)handle; + unsigned old_height = window->wl.decorations.metrics.visible_titlebar_height; + csd_initialize_metrics(window); + bool height_changed = old_height != window->wl.decorations.metrics.visible_titlebar_height; + if (height_changed && csd_should_window_be_decorated(window) && !window->wl.decorations.titlebar_hidden) { + int32_t width = window->wl.width; + int32_t height = window->wl.height + (int)window->wl.decorations.metrics.visible_titlebar_height; + if (window->wl.current.toplevel_states & TOPLEVEL_STATE_DOCKED) height = window->wl.height + (int)old_height; + csd_set_window_geometry(window, &width, &height); + if (height > 0 && height != window->wl.height) { + window->wl.height = height; + resizeFramebuffer(window); + } + inform_compositor_of_window_geometry(window, "TitlebarSettings"); + } if (csd_change_title(window)) commit_window_surface_if_safe(window); } diff --git a/kitty/glfw-wrapper.h b/kitty/glfw-wrapper.h index 31d625cc8..8140abf70 100644 --- a/kitty/glfw-wrapper.h +++ b/kitty/glfw-wrapper.h @@ -2273,7 +2273,7 @@ typedef GLFWColorScheme (*glfwGetCurrentSystemColorTheme_func)(bool); GFW_EXTERN glfwGetCurrentSystemColorTheme_func glfwGetCurrentSystemColorTheme_impl; #define glfwGetCurrentSystemColorTheme glfwGetCurrentSystemColorTheme_impl -typedef const char* (*glfwGetSystemFont_func)(void); +typedef const char* (*glfwGetSystemFont_func)(double*, bool*); GFW_EXTERN glfwGetSystemFont_func glfwGetSystemFont_impl; #define glfwGetSystemFont glfwGetSystemFont_impl diff --git a/kitty/glfw.c b/kitty/glfw.c index c071286a2..0a1b99369 100644 --- a/kitty/glfw.c +++ b/kitty/glfw.c @@ -1523,7 +1523,9 @@ draw_text_callback( static char title[2048]; const bool gnome_title = global_state.is_wayland && !is_single_glyph; if (gnome_title) { - const char *description = glfwGetSystemFont(); + double text_scale = 1.; + bool bold = true, italic = false; + const char *description = glfwGetSystemFont(&text_scale, &bold); char family[256]; snprintf(family, sizeof(family), "%s", description && description[0] ? description : "sans-serif 11"); double points = 11; @@ -1536,19 +1538,32 @@ draw_text_callback( *size = 0; } } + // Common GNOME descriptions have optional trailing style words. + for (;;) { + char *style = strrchr(family, ' '); + if (!style) break; + if (strcmp(style + 1, "Bold") == 0) bold = true; + else if (strcmp(style + 1, "Italic") == 0 || strcmp(style + 1, "Oblique") == 0) italic = true; + else if (strcmp(style + 1, "Regular") == 0 || strcmp(style + 1, "Normal") == 0) bold = false; + else break; + *style = 0; + } static FreeTypeRenderCtx gnome_ctx = NULL; + static bool cached_bold, cached_italic; static char cached_family[256] = {0}; - if (!gnome_ctx || strcmp(cached_family, family) != 0) { - FreeTypeRenderCtx replacement = create_freetype_render_context(family, true, false); + if (!gnome_ctx || strcmp(cached_family, family) != 0 || cached_bold != bold || cached_italic != italic) { + FreeTypeRenderCtx replacement = create_freetype_render_context(family, bold, italic); if (replacement) { if (gnome_ctx) release_freetype_render_context(gnome_ctx); gnome_ctx = replacement; + cached_bold = bold; + cached_italic = italic; snprintf(cached_family, sizeof(cached_family), "%s", family); } else if (PyErr_Occurred()) PyErr_Print(); } ctx = gnome_ctx ? gnome_ctx : freetype_render_ctx(true); if (!ctx) return false; - px_sz = (unsigned)round(points * 96. / 72. * height / 38.); + px_sz = (unsigned)round(points * ydpi / 72. * text_scale); px_sz = MIN(px_sz, (unsigned)(height * 3 / 4)); size_t text_width = freetype_text_width_for_single_line(ctx, text, px_sz); size_t available = width > right_margin + (size_t)x_offset ? width - right_margin - (size_t)x_offset : 0;