dmenu-center-20191105-f1ca0d0.diff (3411B)
1 From f1ca0d0c35769f3197781eb875f2359a9d33007d Mon Sep 17 00:00:00 2001 2 From: "nihal@nihaljere.xyz" <Nihal Jere> 3 Date: Tue, 5 Nov 2019 18:33:00 -0600 4 Subject: [PATCH] Improved center patch. Can now be enabled using a flag 5 6 --- 7 config.def.h | 1 + 8 dmenu.1 | 3 +++ 9 dmenu.c | 39 ++++++++++++++++++++++++++++++++------- 10 3 files changed, 36 insertions(+), 7 deletions(-) 11 12 diff --git a/config.def.h b/config.def.h 13 index 1edb647..7ee3247 100644 14 --- a/config.def.h 15 +++ b/config.def.h 16 @@ -2,6 +2,7 @@ 17 /* Default settings; can be overriden by command line. */ 18 19 static int topbar = 1; /* -b option; if 0, dmenu appears at bottom */ 20 +static int centered = 0; /* -c option; centers dmenu on screen */ 21 /* -fn option overrides fonts[0]; default X11 font or font set */ 22 static const char *fonts[] = { 23 "monospace:size=10" 24 diff --git a/dmenu.1 b/dmenu.1 25 index 323f93c..c036baa 100644 26 --- a/dmenu.1 27 +++ b/dmenu.1 28 @@ -40,6 +40,9 @@ which lists programs in the user's $PATH and runs the result in their $SHELL. 29 .B \-b 30 dmenu appears at the bottom of the screen. 31 .TP 32 +.B \-c 33 +dmenu appears centered on the screen. 34 +.TP 35 .B \-f 36 dmenu grabs the keyboard before reading stdin if not reading from a tty. This 37 is faster, but will lock up X until stdin reaches end\-of\-file. 38 diff --git a/dmenu.c b/dmenu.c 39 index 65f25ce..bc7d087 100644 40 --- a/dmenu.c 41 +++ b/dmenu.c 42 @@ -89,6 +89,15 @@ calcoffsets(void) 43 break; 44 } 45 46 +static int 47 +max_textw(void) 48 +{ 49 + int len = 0; 50 + for (struct item *item = items; item && item->text; item++) 51 + len = MAX(TEXTW(item->text), len); 52 + return len; 53 +} 54 + 55 static void 56 cleanup(void) 57 { 58 @@ -611,6 +620,7 @@ setup(void) 59 bh = drw->fonts->h + 2; 60 lines = MAX(lines, 0); 61 mh = (lines + 1) * bh; 62 + promptw = (prompt && *prompt) ? TEXTW(prompt) - lrpad / 4 : 0; 63 #ifdef XINERAMA 64 i = 0; 65 if (parentwin == root && (info = XineramaQueryScreens(dpy, &n))) { 66 @@ -637,9 +647,16 @@ setup(void) 67 if (INTERSECT(x, y, 1, 1, info[i])) 68 break; 69 70 - x = info[i].x_org; 71 - y = info[i].y_org + (topbar ? 0 : info[i].height - mh); 72 - mw = info[i].width; 73 + if (centered) { 74 + mw = MIN(MAX(max_textw() + promptw, 100), info[i].width); 75 + x = info[i].x_org + ((info[i].width - mw) / 2); 76 + y = info[i].y_org + ((info[i].height - mh) / 2); 77 + } else { 78 + x = info[i].x_org; 79 + y = info[i].y_org + (topbar ? 0 : info[i].height - mh); 80 + mw = info[i].width; 81 + } 82 + 83 XFree(info); 84 } else 85 #endif 86 @@ -647,11 +664,17 @@ setup(void) 87 if (!XGetWindowAttributes(dpy, parentwin, &wa)) 88 die("could not get embedding window attributes: 0x%lx", 89 parentwin); 90 - x = 0; 91 - y = topbar ? 0 : wa.height - mh; 92 - mw = wa.width; 93 + 94 + if (centered) { 95 + mw = MIN(MAX(max_textw() + promptw, 100), wa.width); 96 + x = (wa.width - mw) / 2; 97 + y = (wa.height - mh) / 2; 98 + } else { 99 + x = 0; 100 + y = topbar ? 0 : wa.height - mh; 101 + mw = wa.width; 102 + } 103 } 104 - promptw = (prompt && *prompt) ? TEXTW(prompt) - lrpad / 4 : 0; 105 inputw = MIN(inputw, mw/3); 106 match(); 107 108 @@ -709,6 +732,8 @@ main(int argc, char *argv[]) 109 topbar = 0; 110 else if (!strcmp(argv[i], "-f")) /* grabs keyboard before reading stdin */ 111 fast = 1; 112 + else if (!strcmp(argv[i], "-c")) /* centers dmenu on screen */ 113 + centered = 1; 114 else if (!strcmp(argv[i], "-i")) { /* case-insensitive item matching */ 115 fstrncmp = strncasecmp; 116 fstrstr = cistrstr; 117 -- 118 2.23.0 119