centeroption.patch (2570B)
1 diff --git a/dmenu.1 b/dmenu.1 2 index 323f93c..c036baa 100644 3 --- a/dmenu.1 4 +++ b/dmenu.1 5 @@ -40,6 +40,9 @@ which lists programs in the user's $PATH and runs the result in their $SHELL. 6 .B \-b 7 dmenu appears at the bottom of the screen. 8 .TP 9 +.B \-c 10 +dmenu appears centered on the screen. 11 +.TP 12 .B \-f 13 dmenu grabs the keyboard before reading stdin if not reading from a tty. This 14 is faster, but will lock up X until stdin reaches end\-of\-file. 15 diff --git a/dmenu.c b/dmenu.c 16 index 65f25ce..041c7f8 100644 17 --- a/dmenu.c 18 +++ b/dmenu.c 19 @@ -89,6 +89,16 @@ calcoffsets(void) 20 break; 21 } 22 23 +static int 24 +max_textw(void) 25 +{ 26 + int len = 0; 27 + struct item *item = items; 28 + for (item = items; item && item->text; item++) 29 + len = MAX(TEXTW(item->text), len); 30 + return len; 31 +} 32 + 33 static void 34 cleanup(void) 35 { 36 @@ -611,6 +620,7 @@ setup(void) 37 bh = drw->fonts->h + 2; 38 lines = MAX(lines, 0); 39 mh = (lines + 1) * bh; 40 + promptw = (prompt && *prompt) ? TEXTW(prompt) - lrpad / 4 : 0; 41 #ifdef XINERAMA 42 i = 0; 43 if (parentwin == root && (info = XineramaQueryScreens(dpy, &n))) { 44 @@ -637,9 +647,16 @@ setup(void) 45 if (INTERSECT(x, y, 1, 1, info[i])) 46 break; 47 48 - x = info[i].x_org; 49 - y = info[i].y_org + (topbar ? 0 : info[i].height - mh); 50 - mw = info[i].width; 51 + if (centered) { 52 + mw = MIN(MAX(max_textw() + promptw, min_width), info[i].width); 53 + x = info[i].x_org + ((info[i].width - mw) / 2); 54 + y = info[i].y_org + ((info[i].height - mh) / 2); 55 + } else { 56 + x = info[i].x_org; 57 + y = info[i].y_org + (topbar ? 0 : info[i].height - mh); 58 + mw = info[i].width; 59 + } 60 + 61 XFree(info); 62 } else 63 #endif 64 @@ -647,11 +664,17 @@ setup(void) 65 if (!XGetWindowAttributes(dpy, parentwin, &wa)) 66 die("could not get embedding window attributes: 0x%lx", 67 parentwin); 68 - x = 0; 69 - y = topbar ? 0 : wa.height - mh; 70 - mw = wa.width; 71 + 72 + if (centered) { 73 + mw = MIN(MAX(max_textw() + promptw, min_width), wa.width); 74 + x = (wa.width - mw) / 2; 75 + y = (wa.height - mh) / 2; 76 + } else { 77 + x = 0; 78 + y = topbar ? 0 : wa.height - mh; 79 + mw = wa.width; 80 + } 81 } 82 - promptw = (prompt && *prompt) ? TEXTW(prompt) - lrpad / 4 : 0; 83 inputw = MIN(inputw, mw/3); 84 match(); 85 86 @@ -709,6 +732,8 @@ main(int argc, char *argv[]) 87 topbar = 0; 88 else if (!strcmp(argv[i], "-f")) /* grabs keyboard before reading stdin */ 89 fast = 1; 90 + else if (!strcmp(argv[i], "-c")) /* centers dmenu on screen */ 91 + centered = 1; 92 else if (!strcmp(argv[i], "-i")) { /* case-insensitive item matching */ 93 fstrncmp = strncasecmp; 94 fstrstr = cistrstr; 95 -- 96 2.24.1 97