This commit is contained in:
Alex Chernika 2026-04-15 13:08:50 +02:00 committed by GitHub
commit 310bf5f36f
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
12 changed files with 158 additions and 26 deletions

View file

@ -4,6 +4,7 @@
#include <cairo.h> #include <cairo.h>
#include <pango/pango-font.h> #include <pango/pango-font.h>
#include <stdbool.h>
struct lab_data_buffer; struct lab_data_buffer;
@ -43,10 +44,11 @@ void font_get_buffer_size(int max_width, const char *text, struct font *font,
* @font: font description * @font: font description
* @color: foreground color in rgba format * @color: foreground color in rgba format
* @bg_pattern: background pattern * @bg_pattern: background pattern
* @use_markup: flag to render pango markup
*/ */
void font_buffer_create(struct lab_data_buffer **buffer, int max_width, void font_buffer_create(struct lab_data_buffer **buffer, int max_width,
int height, const char *text, struct font *font, const float *color, int height, const char *text, struct font *font, const float *color,
cairo_pattern_t *bg_pattern, double scale); cairo_pattern_t *bg_pattern, double scale, bool use_markup);
/** /**
* font_finish - free some font related resources * font_finish - free some font related resources

View file

@ -23,9 +23,11 @@ struct menuitem {
char *text; char *text;
char *icon_name; char *icon_name;
const char *arrow; const char *arrow;
uint32_t accelerator;
struct menu *parent; struct menu *parent;
struct menu *submenu; struct menu *submenu;
bool selectable; bool selectable;
bool use_markup;
enum menuitem_type type; enum menuitem_type type;
int native_width; int native_width;
struct wlr_scene_tree *tree; struct wlr_scene_tree *tree;
@ -66,6 +68,19 @@ struct menu {
/* For keyboard support */ /* For keyboard support */
void menu_item_select_next(void); void menu_item_select_next(void);
void menu_item_select_previous(void); void menu_item_select_previous(void);
/**
* menu_item_select_by_accelerator - selects the next menu item with
* a matching accelerator, starting after the current selection
*
* @accelerator a shortcut to quickly select/open an item, defined in menu.xml
* with an underscore in the item label before the target letter.
*
* Return: a boolean value that represents whether the newly selected item
* needs to be executed.
*/
bool menu_item_select_by_accelerator(uint32_t accelerator);
void menu_submenu_enter(void); void menu_submenu_enter(void);
void menu_submenu_leave(void); void menu_submenu_leave(void);
bool menu_call_selected_actions(void); bool menu_call_selected_actions(void);
@ -100,7 +115,7 @@ void menu_open_root(struct menu *menu, int x, int y);
void menu_process_cursor_motion(struct wlr_scene_node *node); void menu_process_cursor_motion(struct wlr_scene_node *node);
/** /**
* menu_close_root- close root menu * menu_close_root - close root menu
* *
* This function will close server.menu_current and set it to NULL. * This function will close server.menu_current and set it to NULL.
* Asserts that server.input_mode is set to LAB_INPUT_STATE_MENU. * Asserts that server.input_mode is set to LAB_INPUT_STATE_MENU.

View file

@ -15,6 +15,7 @@ struct scaled_font_buffer {
/* Private */ /* Private */
char *text; char *text;
bool use_markup;
int max_width; int max_width;
float color[4]; float color[4];
float bg_color[4]; float bg_color[4];
@ -71,6 +72,6 @@ scaled_font_buffer_create_for_titlebar(struct wlr_scene_tree *parent,
*/ */
void scaled_font_buffer_update(struct scaled_font_buffer *self, const char *text, void scaled_font_buffer_update(struct scaled_font_buffer *self, const char *text,
int max_width, struct font *font, const float *color, int max_width, struct font *font, const float *color,
const float *bg_color); const float *bg_color, bool use_markup);
#endif /* LABWC_SCALED_FONT_BUFFER_H */ #endif /* LABWC_SCALED_FONT_BUFFER_H */

View file

@ -79,7 +79,7 @@ font_get_buffer_size(int max_width, const char *text, struct font *font,
void void
font_buffer_create(struct lab_data_buffer **buffer, int max_width, font_buffer_create(struct lab_data_buffer **buffer, int max_width,
int height, const char *text, struct font *font, const float *color, int height, const char *text, struct font *font, const float *color,
cairo_pattern_t *bg_pattern, double scale) cairo_pattern_t *bg_pattern, double scale, bool use_markup)
{ {
if (string_null_or_empty(text)) { if (string_null_or_empty(text)) {
return; return;
@ -123,7 +123,13 @@ font_buffer_create(struct lab_data_buffer **buffer, int max_width,
PangoLayout *layout = pango_cairo_create_layout(cairo); PangoLayout *layout = pango_cairo_create_layout(cairo);
pango_context_set_round_glyph_positions(pango_layout_get_context(layout), false); pango_context_set_round_glyph_positions(pango_layout_get_context(layout), false);
pango_layout_set_width(layout, width * PANGO_SCALE); pango_layout_set_width(layout, width * PANGO_SCALE);
pango_layout_set_text(layout, text, -1);
if (use_markup) {
pango_layout_set_markup(layout, text, -1);
} else {
pango_layout_set_text(layout, text, -1);
}
pango_layout_set_ellipsize(layout, PANGO_ELLIPSIZE_END); pango_layout_set_ellipsize(layout, PANGO_ELLIPSIZE_END);
if (!opaque_bg) { if (!opaque_bg) {

View file

@ -58,8 +58,8 @@ create_fields_scene(struct view *view,
struct scaled_font_buffer *font_buffer = struct scaled_font_buffer *font_buffer =
scaled_font_buffer_create(parent); scaled_font_buffer_create(parent);
scaled_font_buffer_update(font_buffer, scaled_font_buffer_update(font_buffer,
buf.data, field_width, buf.data, field_width, &rc.font_osd,
&rc.font_osd, text_color, bg_color); text_color, bg_color, false);
node = &font_buffer->scene_buffer->node; node = &font_buffer->scene_buffer->node;
height = font_height(&rc.font_osd); height = font_height(&rc.font_osd);
} }
@ -143,7 +143,7 @@ cycle_osd_classic_init(struct cycle_osd_output *osd_output)
wlr_scene_node_set_position(&font_buffer->scene_buffer->node, wlr_scene_node_set_position(&font_buffer->scene_buffer->node,
x, y + (switcher_theme->item_height - font_height(&font)) / 2); x, y + (switcher_theme->item_height - font_height(&font)) / 2);
scaled_font_buffer_update(font_buffer, workspace_name, 0, scaled_font_buffer_update(font_buffer, workspace_name, 0,
&font, text_color, bg_color); &font, text_color, bg_color, false);
y += switcher_theme->item_height; y += switcher_theme->item_height;
} }

View file

@ -116,7 +116,7 @@ create_label(struct wlr_scene_tree *parent, struct view *view,
scaled_font_buffer_create(parent); scaled_font_buffer_create(parent);
scaled_font_buffer_update(buffer, buf.data, scaled_font_buffer_update(buffer, buf.data,
switcher_theme->item_width - 2 * switcher_theme->item_padding, switcher_theme->item_width - 2 * switcher_theme->item_padding,
&rc.font_osd, text_color, bg_color); &rc.font_osd, text_color, bg_color, false);
buf_reset(&buf); buf_reset(&buf);
wlr_scene_node_set_position(&buffer->scene_buffer->node, wlr_scene_node_set_position(&buffer->scene_buffer->node,
(switcher_theme->item_width - buffer->width) / 2, y); (switcher_theme->item_width - buffer->width) / 2, y);

View file

@ -116,7 +116,7 @@ update_key_indicator_callback(void *data)
buf_add_fmt(&buf, "%s (%d), ", keyname, pressed.values[i]); buf_add_fmt(&buf, "%s (%d), ", keyname, pressed.values[i]);
} }
scaled_font_buffer_update(indicator_state.sfb_pressed, buf.data, scaled_font_buffer_update(indicator_state.sfb_pressed, buf.data,
-1, &rc.font_osd, black, white); -1, &rc.font_osd, black, white, false);
buf_clear(&buf); buf_clear(&buf);
buf_add(&buf, "bound="); buf_add(&buf, "bound=");
@ -126,7 +126,7 @@ update_key_indicator_callback(void *data)
buf_add_fmt(&buf, "%s (%d), ", keyname, bound.values[i]); buf_add_fmt(&buf, "%s (%d), ", keyname, bound.values[i]);
} }
scaled_font_buffer_update(indicator_state.sfb_bound, buf.data, scaled_font_buffer_update(indicator_state.sfb_bound, buf.data,
-1, &rc.font_osd, black, white); -1, &rc.font_osd, black, white, false);
buf_clear(&buf); buf_clear(&buf);
buf_add(&buf, "pressed_sent="); buf_add(&buf, "pressed_sent=");
@ -136,7 +136,7 @@ update_key_indicator_callback(void *data)
buf_add_fmt(&buf, "%s (%d), ", keyname, pressed_sent.values[i]); buf_add_fmt(&buf, "%s (%d), ", keyname, pressed_sent.values[i]);
} }
scaled_font_buffer_update(indicator_state.sfb_pressed_sent, buf.data, -1, scaled_font_buffer_update(indicator_state.sfb_pressed_sent, buf.data, -1,
&rc.font_osd, black, white); &rc.font_osd, black, white, false);
buf_clear(&buf); buf_clear(&buf);
buf_add(&buf, "modifiers="); buf_add(&buf, "modifiers=");
@ -148,7 +148,7 @@ update_key_indicator_callback(void *data)
} }
buf_add_fmt(&buf, "(%d)", all_modifiers); buf_add_fmt(&buf, "(%d)", all_modifiers);
scaled_font_buffer_update(indicator_state.sfb_modifiers, buf.data, -1, scaled_font_buffer_update(indicator_state.sfb_modifiers, buf.data, -1,
&rc.font_osd, black, white); &rc.font_osd, black, white, false);
buf_reset(&buf); buf_reset(&buf);
} }

View file

@ -444,15 +444,24 @@ handle_menu_keys(struct keysyms *syms)
break; break;
case XKB_KEY_Return: case XKB_KEY_Return:
case XKB_KEY_KP_Enter: case XKB_KEY_KP_Enter:
menu_call_selected_actions(); if (!menu_call_selected_actions()) {
menu_submenu_enter();
};
break; break;
case XKB_KEY_Escape: case XKB_KEY_Escape:
menu_close_root(); menu_close_root();
cursor_update_focus(); cursor_update_focus();
break; break;
default: default: {
continue; uint32_t accelerator = xkb_keysym_to_utf32(syms->syms[i]);
} if (accelerator == 0) {
continue;
}
if (menu_item_select_by_accelerator(accelerator)) {
menu_call_selected_actions();
}
break;
}}
break; break;
} }
} }

View file

@ -2,12 +2,15 @@
#define _POSIX_C_SOURCE 200809L #define _POSIX_C_SOURCE 200809L
#include "menu/menu.h" #include "menu/menu.h"
#include <assert.h> #include <assert.h>
#include <ctype.h>
#include <libxml/parser.h> #include <libxml/parser.h>
#include <signal.h> #include <signal.h>
#include <stdlib.h> #include <stdlib.h>
#include <string.h> #include <string.h>
#include <strings.h> #include <strings.h>
#include <uchar.h>
#include <unistd.h> #include <unistd.h>
#include <wctype.h>
#include <wlr/types/wlr_scene.h> #include <wlr/types/wlr_scene.h>
#include <wlr/types/wlr_xdg_shell.h> #include <wlr/types/wlr_xdg_shell.h>
#include <wlr/util/log.h> #include <wlr/util/log.h>
@ -130,7 +133,7 @@ validate(void)
} }
static struct menuitem * static struct menuitem *
item_create(struct menu *menu, const char *text, const char *icon_name, bool show_arrow) item_create(struct menu *menu, char *text, const char *icon_name, bool show_arrow)
{ {
assert(menu); assert(menu);
assert(text); assert(text);
@ -140,9 +143,52 @@ item_create(struct menu *menu, const char *text, const char *icon_name, bool sho
menuitem->parent = menu; menuitem->parent = menu;
menuitem->selectable = true; menuitem->selectable = true;
menuitem->type = LAB_MENU_ITEM; menuitem->type = LAB_MENU_ITEM;
menuitem->text = xstrdup(text);
menuitem->arrow = show_arrow ? "" : NULL; menuitem->arrow = show_arrow ? "" : NULL;
uint32_t accelerator = 0;
char *new_text = NULL;
char *it = text;
while (*it != '\0') {
if (*it == '_') {
char32_t codepoint = 0;
mbstate_t state = {0};
size_t bytes = mbrtoc32(&codepoint, it + 1,
MB_CUR_MAX, &state);
if (bytes > 0 && bytes <= 4) {
accelerator = (uint32_t)towlower((wint_t)codepoint);
}
if (*(it + 1) != '\0') {
int underscore_index = it - text;
new_text = strdup_printf("%.*s<u>%.*s</u>%s",
underscore_index, text, (int)bytes,
it + 1, it + 1 + bytes);
break;
}
}
it++;
}
/* Fallback to the first character of the label */
if (accelerator == 0 && text[0] != '\0') {
char32_t codepoint = 0;
mbstate_t state = {0};
size_t bytes = mbrtoc32(&codepoint, text, MB_CUR_MAX, &state);
if (bytes > 0 && bytes <= 4) {
accelerator = (uint32_t)towlower((wint_t)codepoint);
}
}
menuitem->accelerator = accelerator;
if (new_text) {
menuitem->text = new_text;
menuitem->use_markup = true;
} else {
menuitem->text = xstrdup(text);
menuitem->use_markup = false;
}
#if HAVE_LIBSFDO #if HAVE_LIBSFDO
if (rc.menu_show_icons && !string_null_or_empty(icon_name)) { if (rc.menu_show_icons && !string_null_or_empty(icon_name)) {
menuitem->icon_name = xstrdup(icon_name); menuitem->icon_name = xstrdup(icon_name);
@ -213,7 +259,7 @@ item_create_scene_for_state(struct menuitem *item, float *text_color,
struct scaled_font_buffer *label_buffer = scaled_font_buffer_create(tree); struct scaled_font_buffer *label_buffer = scaled_font_buffer_create(tree);
assert(label_buffer); assert(label_buffer);
scaled_font_buffer_update(label_buffer, item->text, label_max_width, scaled_font_buffer_update(label_buffer, item->text, label_max_width,
&rc.font_menuitem, text_color, bg_color); &rc.font_menuitem, text_color, bg_color, item->use_markup);
/* Vertically center and left-align label */ /* Vertically center and left-align label */
int x = theme->menu_items_padding_x + icon_width; int x = theme->menu_items_padding_x + icon_width;
int y = (theme->menu_item_height - label_buffer->height) / 2; int y = (theme->menu_item_height - label_buffer->height) / 2;
@ -227,7 +273,7 @@ item_create_scene_for_state(struct menuitem *item, float *text_color,
struct scaled_font_buffer *arrow_buffer = scaled_font_buffer_create(tree); struct scaled_font_buffer *arrow_buffer = scaled_font_buffer_create(tree);
assert(arrow_buffer); assert(arrow_buffer);
scaled_font_buffer_update(arrow_buffer, item->arrow, -1, scaled_font_buffer_update(arrow_buffer, item->arrow, -1,
&rc.font_menuitem, text_color, bg_color); &rc.font_menuitem, text_color, bg_color, false);
/* Vertically center and right-align arrow */ /* Vertically center and right-align arrow */
x += label_max_width + theme->menu_items_padding_x; x += label_max_width + theme->menu_items_padding_x;
y = (theme->menu_item_height - label_buffer->height) / 2; y = (theme->menu_item_height - label_buffer->height) / 2;
@ -366,7 +412,7 @@ title_create_scene(struct menuitem *menuitem, int *item_y)
scaled_font_buffer_create(menuitem->normal_tree); scaled_font_buffer_create(menuitem->normal_tree);
assert(title_font_buffer); assert(title_font_buffer);
scaled_font_buffer_update(title_font_buffer, menuitem->text, scaled_font_buffer_update(title_font_buffer, menuitem->text,
text_width, &rc.font_menuheader, text_color, bg_color); text_width, &rc.font_menuheader, text_color, bg_color, false);
int title_x = 0; int title_x = 0;
switch (theme->menu_title_text_justify) { switch (theme->menu_title_text_justify) {
@ -1460,6 +1506,57 @@ menu_item_select_previous(void)
menu_item_select(/* forward */ false); menu_item_select(/* forward */ false);
} }
bool
menu_item_select_by_accelerator(uint32_t accelerator)
{
struct menu *menu = get_selection_leaf();
if (!menu || wl_list_empty(&menu->menuitems)) {
return false;
}
bool needs_exec = false;
bool matched = false;
struct menuitem *selection = menu->selection.item;
struct wl_list *start = selection ? &selection->link : &menu->menuitems;
struct wl_list *current = start;
struct menuitem *item = NULL;
struct menuitem *next_selection = NULL;
do {
current = current->next;
if (current == &menu->menuitems) {
/* Allow wrap around */
continue;
}
item = wl_container_of(current, item, link);
if (item->accelerator == accelerator) {
if (!matched) {
/* Found first match */
next_selection = item;
needs_exec = true;
matched = true;
} else {
/*
* Found another match,
* cycle selection instead of executing
*/
needs_exec = false;
break;
}
}
} while (current != start);
if (next_selection) {
menu_process_item_selection(next_selection);
if (needs_exec && next_selection->submenu) {
/* Since we can't execute a submenu, enter it. */
needs_exec = false;
menu_submenu_enter();
}
}
return needs_exec;
}
bool bool
menu_call_selected_actions(void) menu_call_selected_actions(void)
{ {

View file

@ -26,7 +26,7 @@ _create_buffer(struct scaled_buffer *scaled_buffer, double scale)
/* Buffer gets free'd automatically along the backing wlr_buffer */ /* Buffer gets free'd automatically along the backing wlr_buffer */
font_buffer_create(&buffer, self->max_width, self->height, self->text, font_buffer_create(&buffer, self->max_width, self->height, self->text,
&self->font, self->color, bg_pattern, scale); &self->font, self->color, bg_pattern, scale, self->use_markup);
if (!buffer) { if (!buffer) {
wlr_log(WLR_ERROR, "font_buffer_create() failed"); wlr_log(WLR_ERROR, "font_buffer_create() failed");
@ -56,6 +56,7 @@ _equal(struct scaled_buffer *scaled_buffer_a,
struct scaled_font_buffer *b = scaled_buffer_b->data; struct scaled_font_buffer *b = scaled_buffer_b->data;
return str_equal(a->text, b->text) return str_equal(a->text, b->text)
&& a->use_markup == b->use_markup
&& a->max_width == b->max_width && a->max_width == b->max_width
&& str_equal(a->font.name, b->font.name) && str_equal(a->font.name, b->font.name)
&& a->font.size == b->font.size && a->font.size == b->font.size
@ -107,7 +108,7 @@ scaled_font_buffer_create_for_titlebar(struct wlr_scene_tree *parent,
void void
scaled_font_buffer_update(struct scaled_font_buffer *self, const char *text, scaled_font_buffer_update(struct scaled_font_buffer *self, const char *text,
int max_width, struct font *font, const float *color, int max_width, struct font *font, const float *color,
const float *bg_color) const float *bg_color, bool use_markup)
{ {
assert(self); assert(self);
assert(text); assert(text);
@ -120,6 +121,7 @@ scaled_font_buffer_update(struct scaled_font_buffer *self, const char *text,
/* Update internal state */ /* Update internal state */
self->text = xstrdup(text); self->text = xstrdup(text);
self->use_markup = use_markup;
self->max_width = max_width; self->max_width = max_width;
if (font->name) { if (font->name) {
self->font.name = xstrdup(font->name); self->font.name = xstrdup(font->name);

View file

@ -202,7 +202,7 @@ resize_indicator_update(struct view *view)
wlr_scene_node_set_position(&indicator->tree->node, x, y); wlr_scene_node_set_position(&indicator->tree->node, x, y);
scaled_font_buffer_update(indicator->text, text, width, &rc.font_osd, scaled_font_buffer_update(indicator->text, text, width, &rc.font_osd,
rc.theme->osd_label_text_color, rc.theme->osd_bg_color); rc.theme->osd_label_text_color, rc.theme->osd_bg_color, false);
} }
void void

View file

@ -473,7 +473,7 @@ ssd_update_title(struct ssd *ssd)
const float bg_color[4] = {0, 0, 0, 0}; /* ignored */ const float bg_color[4] = {0, 0, 0, 0}; /* ignored */
scaled_font_buffer_update(subtree->title, view->title, scaled_font_buffer_update(subtree->title, view->title,
title_bg_width, font, title_bg_width, font,
text_color, bg_color); text_color, bg_color, false);
/* And finally update the cache */ /* And finally update the cache */
dstate->width = subtree->title->width; dstate->width = subtree->title->width;