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