Skip to content

Commit 7643a92

Browse files
committed
Add built-in menu structure and functions
Implement menu structure and related functions, preparing for clickable menu feature. Add new menus array to preserve current actions. Signed-off-by: LI Qingwu <[email protected]>
1 parent fe7f928 commit 7643a92

File tree

3 files changed

+148
-0
lines changed

3 files changed

+148
-0
lines changed

src/menu.c

Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -392,4 +392,89 @@ static gpointer context_menu_thread(gpointer data)
392392

393393
return NULL;
394394
}
395+
396+
397+
gboolean menu_init(struct notification *n)
398+
{
399+
if (n->actions == NULL) {
400+
return false;
401+
}
402+
403+
gpointer p_key;
404+
gpointer p_value;
405+
GHashTableIter iter;
406+
n->menus = g_array_sized_new(FALSE, FALSE, sizeof(struct menu),
407+
g_hash_table_size(n->actions));
408+
409+
g_hash_table_iter_init(&iter, n->actions);
410+
while (g_hash_table_iter_next(&iter, &p_key, &p_value)) {
411+
char *key = (char *)p_key;
412+
char *value = (char *)p_value;
413+
struct menu button = {.value = g_strdup(value),
414+
.key = g_strdup(key),
415+
.x = 0,
416+
.y = 0,
417+
.width = 0,
418+
.height = 0};
419+
420+
g_array_append_val(n->menus, button);
421+
}
422+
return true;
423+
}
424+
425+
int menu_get_count(struct notification *n)
426+
{
427+
if (!n->menus) {
428+
return 0;
429+
}
430+
return n->menus->len;
431+
}
432+
433+
char *menu_get_label(struct notification *n, int index)
434+
{
435+
if (index < 0 || index >= n->menus->len || !n->menus)
436+
return NULL;
437+
struct menu *button = &g_array_index(n->menus, struct menu, index);
438+
return button->value;
439+
}
440+
441+
void menu_set_position(struct notification *n, int index, int x, int y,
442+
int width, int height)
443+
{
444+
if (index < 0 || index >= n->menus->len || !n->menus)
445+
return;
446+
struct menu *button = &g_array_index(n->menus, struct menu, index);
447+
button->x = x;
448+
button->y = y;
449+
button->width = width;
450+
button->height = height;
451+
}
452+
453+
void menu_free_array(struct notification *n)
454+
{
455+
if (!n->menus)
456+
return;
457+
for (guint i = 0; i < n->menus->len; i++) {
458+
struct menu *button = &g_array_index(n->menus, struct menu, i);
459+
g_free(button->value);
460+
g_free(button->key);
461+
}
462+
g_array_free(n->menus, TRUE);
463+
n->menus = NULL;
464+
}
465+
466+
struct menu *menu_get_at(struct notification *n, int x, int y)
467+
{
468+
if (!n->menus)
469+
return NULL;
470+
for (guint i = 0; i < n->menus->len; i++) {
471+
struct menu *button = &g_array_index(n->menus, struct menu, i);
472+
if (x >= button->x && x <= button->x + button->width &&
473+
y >= button->y && y <= button->y + button->height) {
474+
return button;
475+
}
476+
}
477+
return NULL;
478+
}
479+
395480
/* vim: set ft=c tabstop=8 shiftwidth=8 expandtab textwidth=0: */

src/menu.h

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
#ifndef DUNST_MENU_H
33
#define DUNST_MENU_H
44

5+
#include "notification.h"
56
#include <glib.h>
67

78
/**
@@ -28,5 +29,65 @@ void context_menu(void);
2829
*/
2930
void context_menu_for(GList *notifications);
3031

32+
struct menu {
33+
char *key;
34+
char *value;
35+
int height;
36+
int width;
37+
int x;
38+
int y;
39+
};
40+
41+
/**
42+
* Initialize the menu structure for a given notification.
43+
* This function creates a new array of menu items based on the actions.
44+
* @param n The notification for which to initialize the menu
45+
* @return TRUE if the initialization was successful, FALSE otherwise
46+
*/
47+
gboolean menu_init(struct notification *n);
48+
49+
/**
50+
* Get the label (display text) for a menu item at a specific index.
51+
* @param n The notification containing the menu
52+
* @param index The index of the menu item
53+
* @return The label of the menu item at the specified index
54+
*/
55+
char *menu_get_label(struct notification *n, int index);
56+
57+
/**
58+
* Get the number of menu items for a given notification.
59+
* @param n The notification containing the menu
60+
* @return The number of menu items
61+
*/
62+
int menu_get_count(struct notification *n);
63+
64+
/**
65+
* Set the position and size of a menu item for a given notification.
66+
* @param n The notification containing the menu
67+
* @param index The index of the menu item to update
68+
* @param x The x-coordinate of the menu item
69+
* @param y The y-coordinate of the menu item
70+
* @param width The width of the menu item
71+
* @param height The height of the menu item
72+
*/
73+
void menu_set_position(struct notification *n, int index, int x, int y,
74+
int width, int height);
75+
76+
/**
77+
* Get the menu item at a specific coordinate.
78+
* @param n The notification containing the menu
79+
* @param x The x-coordinate to check
80+
* @param y The y-coordinate to check
81+
* @return A pointer to the menu item at the specified coordinates,
82+
* or NULL if not found.
83+
*/
84+
struct menu *menu_get_at(struct notification *n, int x, int y);
85+
86+
/**
87+
* Free the memory allocated for the menu array in a notification.
88+
* @param n The notification containing the menu array to be freed.
89+
*/
90+
void menu_free_array(struct notification *n);
91+
3192
#endif
3293
/* vim: set ft=c tabstop=8 shiftwidth=8 expandtab textwidth=0: */

src/notification.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -115,6 +115,8 @@ struct notification {
115115
char *msg; /**< formatted message */
116116
char *text_to_render; /**< formatted message (with age and action indicators) */
117117
char *urls; /**< urllist delimited by '\\n' */
118+
119+
GArray *menus;
118120
};
119121

120122
/**

0 commit comments

Comments
 (0)