Skip to content

Commit

Permalink
Added patch to highlight an entry when mouse-hovered.
Browse files Browse the repository at this point in the history
  • Loading branch information
happyzleaf authored and bakkeby committed Jul 16, 2024
1 parent d045957 commit 4a8b71c
Show file tree
Hide file tree
Showing 4 changed files with 129 additions and 2 deletions.
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@ Browsing patches? There is a [map of patches](https://coggle.it/diagram/YjT2DD6j
### Changelog:
2024-07-16 - Added the mouse motion support patch
2023-06-15 - Added the caret width patch
2022-09-05 - Removed the json patch due to maintenance and compatibility reasons, added the
Expand Down
8 changes: 8 additions & 0 deletions dmenu.c
Original file line number Diff line number Diff line change
Expand Up @@ -1527,6 +1527,11 @@ run(void)
case ButtonPress:
buttonpress(&ev);
break;
#if MOTION_SUPPORT_PATCH
case MotionNotify:
motionevent(&ev.xbutton);
break;
#endif // MOTION_SUPPORT_PATCH
#endif // MOUSE_SUPPORT_PATCH
case DestroyNotify:
if (ev.xdestroywindow.window != win)
Expand Down Expand Up @@ -1749,6 +1754,9 @@ setup(void)
swa.event_mask = ExposureMask | KeyPressMask | VisibilityChangeMask
#if MOUSE_SUPPORT_PATCH
| ButtonPressMask
#if MOTION_SUPPORT_PATCH
| PointerMotionMask
#endif // MOTION_SUPPORT_PATCH
#endif // MOUSE_SUPPORT_PATCH
;
win = XCreateWindow(
Expand Down
116 changes: 114 additions & 2 deletions patch/mousesupport.c
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@ buttonpress(XEvent *e)
struct item *item;
XButtonPressedEvent *ev = &e->xbutton;
int x = 0, y = 0, h = bh, w;
#if GRID_PATCH
int i, cols;
#endif // GRID_PATCH

if (ev->window != win)
return;
Expand Down Expand Up @@ -66,8 +69,40 @@ buttonpress(XEvent *e)
if (ev->state & ~ControlMask)
return;
if (lines > 0) {
#if GRID_PATCH
cols = columns ? columns : 1;
for (i = 0, item = curr; item != next; item = item->right, i++) {
if (
(ev->y >= y + ((i % lines) + 1) * bh) && // line y start
(ev->y <= y + ((i % lines) + 2) * bh) && // line y end
(ev->x >= x + ((i / lines) * (w / cols))) && // column x start
(ev->x <= x + ((i / lines + 1) * (w / cols))) // column x end
) {
#if !MULTI_SELECTION_PATCH
puts(item->text);
#endif // MULTI_SELECTION_PATCH
if (!(ev->state & ControlMask)) {
#if MULTI_SELECTION_PATCH
sel = item;
selsel();
printsel(ev->state);
#endif // MULTI_SELECTION_PATCH
exit(0);
}
sel = item;
if (sel) {
#if MULTI_SELECTION_PATCH
selsel();
#else
sel->out = 1;
#endif // MULTI_SELECTION_PATCH
drawmenu();
}
return;
}
}
#else
/* vertical list: (ctrl)left-click on item */
w = mw - x;
for (item = curr; item != next; item = item->right) {
y += h;
if (ev->y >= y && ev->y <= (y + h)) {
Expand All @@ -94,6 +129,7 @@ buttonpress(XEvent *e)
return;
}
}
#endif // GRID_PATCH
} else if (matches) {
/* left-click on left arrow */
x += inputw;
Expand Down Expand Up @@ -156,4 +192,80 @@ buttonpress(XEvent *e)
return;
}
}
}
}

#if MOTION_SUPPORT_PATCH
static void
motionevent(XButtonEvent *ev)
{
struct item *item;
int x = 0, y = 0, w;
#if GRID_PATCH
int i, cols;
#endif // GRID_PATCH

if (ev->window != win || matches == 0)
return;

if (prompt && *prompt)
x += promptw;

if (lines > 0) {
/* input field */
w = mw - x;
#if GRID_PATCH
cols = columns ? columns : 1;
/* grid view or vertical list */
for (i = 0, item = curr; item != next; item = item->right, i++) {
if (
(ev->y >= y + ((i % lines) + 1) * bh) && // line y start
(ev->y <= y + ((i % lines) + 2) * bh) && // line y end
(ev->x >= x + ((i / lines) * (w / cols))) && // column x start
(ev->x <= x + ((i / lines + 1) * (w / cols))) // column x end
) {
sel = item;
calcoffsets();
drawmenu();
break;
}
}
#else
/* vertical list */
w = mw - x;
for (item = curr; item != next; item = item->right) {
y += bh;
if (ev->y >= y && ev->y <= (y + bh)) {
sel = item;
calcoffsets();
drawmenu();
break;
}
}
#endif // GRID_PATCH
return;
}

/* left-click on left arrow */
x += inputw;
#if SYMBOLS_PATCH
w = TEXTW(symbol_1);
#else
w = TEXTW("<");
#endif // SYMBOLS_PATCH
/* horizontal list */
for (item = curr; item != next; item = item->right) {
x += w;
#if SYMBOLS_PATCH
w = MIN(TEXTW(item->text), mw - x - TEXTW(symbol_2));
#else
w = MIN(TEXTW(item->text), mw - x - TEXTW(">"));
#endif // SYMBOLS_PATCH
if (ev->x >= x && ev->x <= x + w) {
sel = item;
calcoffsets();
drawmenu();
break;
}
}
}
#endif
5 changes: 5 additions & 0 deletions patches.def.h
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,11 @@
*/
#define MOUSE_SUPPORT_PATCH 0

/* Expands the above to support mouse hovering.
* https://tools.suckless.org/dmenu/patches/mouse-support/
*/
#define MOTION_SUPPORT_PATCH 0

/* Without this patch when you press Ctrl+Enter dmenu just outputs current item and it is not
* possible to undo that.
* With this patch dmenu will output all selected items only on exit. It is also possible to
Expand Down

0 comments on commit 4a8b71c

Please sign in to comment.