diff options
author | Benjamin Chausse <benjamin@chausse.xyz> | 2020-10-15 17:06:19 -0400 |
---|---|---|
committer | Benjamin Chausse <benjamin@chausse.xyz> | 2020-10-15 17:06:19 -0400 |
commit | 8116f551e3d98727e81199a908260021dc1d096e (patch) | |
tree | aa19a04d3cf7b585581b84a7672e47a24d7e65c7 | |
parent | 70aa9e0d8c9dce081e47d69deda26fc225f505dd (diff) |
Working centered dmenu
-rw-r--r-- | .gitignore | 7 | ||||
-rw-r--r-- | README.md | 3 | ||||
-rw-r--r-- | config.def.h (renamed from config.h) | 8 | ||||
-rw-r--r-- | dmenu.1 | 3 | ||||
-rw-r--r-- | dmenu.c | 39 |
5 files changed, 47 insertions, 13 deletions
@@ -1,8 +1,11 @@ -# C files +# c files *.o + +# dmenu files dmenu stest +config.h -# Patch files +# patch files *.orig *.rej @@ -1,4 +1,4 @@ -# Luke's dmenu +# My dmenu Extra stuff added to vanilla dmenu: @@ -7,6 +7,7 @@ Extra stuff added to vanilla dmenu: - can view color characters like emoji (libxft-bgra is required for this reason) - `-P` for password mode: hide user input - `-r` to reject non-matching input +- `-c` to reject non-matching input - dmenu options are mouse clickable ## Installation @@ -2,10 +2,12 @@ /* Default settings; can be overriden by command line. */ static int topbar = 1; /* -b option; if 0, dmenu appears at bottom */ +static int centered = 1; /* -c option; centers dmenu on screen */ +static int min_width = 500; /* minimum width when centered */ /* -fn option overrides fonts[0]; default X11 font or font set */ static const char *fonts[] = { - "monospace:size=10", - "JoyPixels:pixelsize=8:antialias=true:autohint=true" + "monospace:size=12", + "JoyPixels:pixelsize=11:antialias=true:autohint=true" }; static const unsigned int bgalpha = 0xe0; static const unsigned int fgalpha = OPAQUE; @@ -24,7 +26,7 @@ static const unsigned int alphas[SchemeLast][2] = { }; /* -l option; if nonzero, dmenu uses vertical list with given number of lines */ -static unsigned int lines = 0; +static unsigned int lines = 11; /* * Characters not considered part of a word while deleting words @@ -40,6 +40,9 @@ which lists programs in the user's $PATH and runs the result in their $SHELL. .B \-b dmenu appears at the bottom of the screen. .TP +.B \-c +dmenu appears non-centered on the screen. +.TP .B \-f dmenu grabs the keyboard before reading stdin if not reading from a tty. This is faster, but will lock up X until stdin reaches end\-of\-file. @@ -138,6 +138,15 @@ calcoffsets(void) break; } +static int +max_textw(void) +{ + int len = 0; + for (struct item *item = items; item && item->text; item++) + len = MAX(TEXTW(item->text), len); + return len; +} + static void cleanup(void) { @@ -796,6 +805,7 @@ setup(void) bh = drw->fonts->h + 2; lines = MAX(lines, 0); mh = (lines + 1) * bh; + promptw = (prompt && *prompt) ? TEXTW(prompt) - lrpad / 4 : 0; #ifdef XINERAMA i = 0; if (parentwin == root && (info = XineramaQueryScreens(dpy, &n))) { @@ -822,9 +832,16 @@ setup(void) if (INTERSECT(x, y, 1, 1, info[i])) break; - x = info[i].x_org; - y = info[i].y_org + (topbar ? 0 : info[i].height - mh); - mw = info[i].width; + if (centered) { + mw = MIN(MAX(max_textw() + promptw, min_width), info[i].width); + x = info[i].x_org + ((info[i].width - mw) / 2); + y = info[i].y_org + ((info[i].height - mh) / 2); + } else { + x = info[i].x_org; + y = info[i].y_org + (topbar ? 0 : info[i].height - mh); + mw = info[i].width; + } + XFree(info); } else #endif @@ -832,11 +849,17 @@ setup(void) if (!XGetWindowAttributes(dpy, parentwin, &wa)) die("could not get embedding window attributes: 0x%lx", parentwin); - x = 0; - y = topbar ? 0 : wa.height - mh; - mw = wa.width; + + if (centered) { + mw = MIN(MAX(max_textw() + promptw, min_width), wa.width); + x = (wa.width - mw) / 2; + y = (wa.height - mh) / 2; + } else { + x = 0; + y = topbar ? 0 : wa.height - mh; + mw = wa.width; + } } - promptw = (prompt && *prompt) ? TEXTW(prompt) - lrpad / 4 : 0; inputw = MIN(inputw, mw/3); match(); @@ -920,6 +943,8 @@ main(int argc, char *argv[]) topbar = 0; else if (!strcmp(argv[i], "-f")) /* grabs keyboard before reading stdin */ fast = 1; + else if (!strcmp(argv[i], "-c")) /* centers dmenu on screen */ + centered = 0; else if (!strcmp(argv[i], "-i")) { /* case-insensitive item matching */ fstrncmp = strncasecmp; fstrstr = cistrstr; |