sites

public wiki contents of suckless.org
git clone git://git.suckless.org/sites
Log | Files | Refs

dmenu-center-20240616-36c3d68.diff (3640B)


      1 From 36c3d68f185f47cbb754569775b7888728be4711 Mon Sep 17 00:00:00 2001
      2 From: elbachir-one <bachiralfa@gmail.com>
      3 Date: Sun, 16 Jun 2024 21:28:41 +0100
      4 Subject: [PATCH] Adding an option to change the height as well
      5 
      6 ---
      7  config.def.h |  3 +++
      8  dmenu.1      |  3 +++
      9  dmenu.c      | 38 ++++++++++++++++++++++++++++++++------
     10  3 files changed, 38 insertions(+), 6 deletions(-)
     11 
     12 diff --git a/config.def.h b/config.def.h
     13 index 1edb647..832896f 100644
     14 --- a/config.def.h
     15 +++ b/config.def.h
     16 @@ -2,6 +2,9 @@
     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 = 1;                    /* -c option; centers dmenu on screen */
     21 +static int min_width = 500;                    /* minimum width when centered */
     22 +static const float menu_height_ratio = 4.0f;  /* This is the ratio used in the original calculation */
     23  /* -fn option overrides fonts[0]; default X11 font or font set */
     24  static const char *fonts[] = {
     25  	"monospace:size=10"
     26 diff --git a/dmenu.1 b/dmenu.1
     27 index 323f93c..c036baa 100644
     28 --- a/dmenu.1
     29 +++ b/dmenu.1
     30 @@ -40,6 +40,9 @@ which lists programs in the user's $PATH and runs the result in their $SHELL.
     31  .B \-b
     32  dmenu appears at the bottom of the screen.
     33  .TP
     34 +.B \-c
     35 +dmenu appears centered on the screen.
     36 +.TP
     37  .B \-f
     38  dmenu grabs the keyboard before reading stdin if not reading from a tty. This
     39  is faster, but will lock up X until stdin reaches end\-of\-file.
     40 diff --git a/dmenu.c b/dmenu.c
     41 index 40f93e0..32d6a9d 100644
     42 --- a/dmenu.c
     43 +++ b/dmenu.c
     44 @@ -95,6 +95,15 @@ calcoffsets(void)
     45  			break;
     46  }
     47  
     48 +static int
     49 +max_textw(void)
     50 +{
     51 +	int len = 0;
     52 +	for (struct item *item = items; item && item->text; item++)
     53 +		len = MAX(TEXTW(item->text), len);
     54 +	return len;
     55 +}
     56 +
     57  static void
     58  cleanup(void)
     59  {
     60 @@ -636,6 +645,7 @@ setup(void)
     61  	bh = drw->fonts->h + 2;
     62  	lines = MAX(lines, 0);
     63  	mh = (lines + 1) * bh;
     64 +	promptw = (prompt && *prompt) ? TEXTW(prompt) - lrpad / 4 : 0;
     65  #ifdef XINERAMA
     66  	i = 0;
     67  	if (parentwin == root && (info = XineramaQueryScreens(dpy, &n))) {
     68 @@ -662,9 +672,16 @@ setup(void)
     69  				if (INTERSECT(x, y, 1, 1, info[i]) != 0)
     70  					break;
     71  
     72 -		x = info[i].x_org;
     73 -		y = info[i].y_org + (topbar ? 0 : info[i].height - mh);
     74 -		mw = info[i].width;
     75 +		if (centered) {
     76 +			mw = MIN(MAX(max_textw() + promptw, min_width), info[i].width);
     77 +			x = info[i].x_org + ((info[i].width  - mw) / 2);
     78 +			y = info[i].y_org + ((info[i].height - mh) / menu_height_ratio);
     79 +		} else {
     80 +			x = info[i].x_org;
     81 +			y = info[i].y_org + (topbar ? 0 : info[i].height - mh);
     82 +			mw = info[i].width;
     83 +		}
     84 +
     85  		XFree(info);
     86  	} else
     87  #endif
     88 @@ -672,9 +689,16 @@ setup(void)
     89  		if (!XGetWindowAttributes(dpy, parentwin, &wa))
     90  			die("could not get embedding window attributes: 0x%lx",
     91  			    parentwin);
     92 -		x = 0;
     93 -		y = topbar ? 0 : wa.height - mh;
     94 -		mw = wa.width;
     95 +
     96 +		if (centered) {
     97 +			mw = MIN(MAX(max_textw() + promptw, min_width), wa.width);
     98 +			x = (wa.width  - mw) / 2;
     99 +			y = (wa.height - mh) / 2;
    100 +		} else {
    101 +			x = 0;
    102 +			y = topbar ? 0 : wa.height - mh;
    103 +			mw = wa.width;
    104 +		}
    105  	}
    106  	promptw = (prompt && *prompt) ? TEXTW(prompt) - lrpad / 4 : 0;
    107  	inputw = mw / 3; /* input width: ~33% of monitor width */
    108 @@ -734,6 +758,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.45.2
    119