centeroption-nodoc.patch (2123B)
1 diff --git a/dmenu.c b/dmenu.c 2 index 65f25ce..041c7f8 100644 3 --- a/dmenu.c 4 +++ b/dmenu.c 5 @@ -89,6 +89,16 @@ calcoffsets(void) 6 break; 7 } 8 9 +static int 10 +max_textw(void) 11 +{ 12 + int len = 0; 13 + struct item *item = items; 14 + for (item = items; item && item->text; item++) 15 + len = MAX(TEXTW(item->text), len); 16 + return len; 17 +} 18 + 19 static void 20 cleanup(void) 21 { 22 @@ -611,6 +620,7 @@ setup(void) 23 bh = drw->fonts->h + 2; 24 lines = MAX(lines, 0); 25 mh = (lines + 1) * bh; 26 + promptw = (prompt && *prompt) ? TEXTW(prompt) - lrpad / 4 : 0; 27 #ifdef XINERAMA 28 i = 0; 29 if (parentwin == root && (info = XineramaQueryScreens(dpy, &n))) { 30 @@ -637,9 +647,16 @@ setup(void) 31 if (INTERSECT(x, y, 1, 1, info[i])) 32 break; 33 34 - x = info[i].x_org; 35 - y = info[i].y_org + (topbar ? 0 : info[i].height - mh); 36 - mw = info[i].width; 37 + if (centered) { 38 + mw = MIN(MAX(max_textw() + promptw, min_width), info[i].width); 39 + x = info[i].x_org + ((info[i].width - mw) / 2); 40 + y = info[i].y_org + ((info[i].height - mh) / 2); 41 + } else { 42 + x = info[i].x_org; 43 + y = info[i].y_org + (topbar ? 0 : info[i].height - mh); 44 + mw = info[i].width; 45 + } 46 + 47 XFree(info); 48 } else 49 #endif 50 @@ -647,11 +664,17 @@ setup(void) 51 if (!XGetWindowAttributes(dpy, parentwin, &wa)) 52 die("could not get embedding window attributes: 0x%lx", 53 parentwin); 54 - x = 0; 55 - y = topbar ? 0 : wa.height - mh; 56 - mw = wa.width; 57 + 58 + if (centered) { 59 + mw = MIN(MAX(max_textw() + promptw, min_width), wa.width); 60 + x = (wa.width - mw) / 2; 61 + y = (wa.height - mh) / 2; 62 + } else { 63 + x = 0; 64 + y = topbar ? 0 : wa.height - mh; 65 + mw = wa.width; 66 + } 67 } 68 - promptw = (prompt && *prompt) ? TEXTW(prompt) - lrpad / 4 : 0; 69 inputw = MIN(inputw, mw/3); 70 match(); 71 72 @@ -709,6 +732,8 @@ main(int argc, char *argv[]) 73 topbar = 0; 74 else if (!strcmp(argv[i], "-f")) /* grabs keyboard before reading stdin */ 75 fast = 1; 76 + else if (!strcmp(argv[i], "-c")) /* centers dmenu on screen */ 77 + centered = 1; 78 else if (!strcmp(argv[i], "-i")) { /* case-insensitive item matching */ 79 fstrncmp = strncasecmp; 80 fstrstr = cistrstr; 81 -- 82 2.24.1 83