Allow middle click on notification to invoke action

A middle click on an notification with a single or default action will
invoke it. If there are multiple actions and no default, the context
menu is opened. If there are no actions, proceed similarly with URLs.
This commit is contained in:
Reto Schnyder 2017-06-28 20:18:19 +02:00
parent aec3533a0b
commit e04bfe0a58
4 changed files with 42 additions and 3 deletions

View File

@ -614,6 +614,11 @@ The "context" keybinding is used to interact with these actions, by showing a
menu of possible actions. This feature requires "dmenu" or a dmenu drop-in
replacement present.
Alternatively, you can invoke an action with a middle click on the notification.
If there is exactly one associated action, or one is marked as default, that one
is invoked. If there are multiple, the context menu is shown. The same applies
to URLs when there are no actions.
=head1 MISCELLANEOUS
Dunst can be paused by sending a notification with a summary of

View File

@ -588,4 +588,32 @@ int notification_get_ttl(notification *n) {
int notification_get_age(notification *n) {
return time(NULL) - n->timestamp;
}
/*
* If the notification has exactly one action, or one is marked as default,
* invoke it. If there are multiple and no default, open the context menu. If
* there are no actions, proceed similarly with urls.
*/
void notification_do_action(notification *n) {
if (n->actions) {
if (n->actions->count == 2) {
action_invoked(n, n->actions->actions[0]);
return;
}
for (int i = 0; i < n->actions->count; i += 2) {
if (strcmp(n->actions->actions[i], "default") == 0) {
action_invoked(n, n->actions->actions[i]);
return;
}
}
context_menu();
} else if (n->urls) {
if (strstr(n->urls, "\n") == NULL)
open_browser(n->urls);
else
context_menu();
}
}
/* vim: set tabstop=8 shiftwidth=8 expandtab textwidth=0: */

View File

@ -73,6 +73,7 @@ char *notification_replace_format(const char *needle, const char *replacement, c
void notification_update_text_to_render(notification *n);
int notification_get_ttl(notification *n);
int notification_get_age(notification *n);
void notification_do_action(notification *n);
#endif
/* vim: set tabstop=8 shiftwidth=8 expandtab textwidth=0: */

View File

@ -896,7 +896,7 @@ static void x_handle_click(XEvent ev)
return;
}
if (ev.xbutton.button == Button1) {
if (ev.xbutton.button == Button1 || ev.xbutton.button == Button2) {
int y = settings.separator_height;
notification *n = NULL;
int first = true;
@ -910,8 +910,13 @@ static void x_handle_click(XEvent ev)
if (first)
y += settings.frame_width;
}
if (n)
notification_close(n, 2);
if (n) {
if (ev.xbutton.button == Button1)
notification_close(n, 2);
else
notification_do_action(n);
}
}
}