commit 5ca7cb9f16c4f1aff15f1eeb694275729fb5271f
parent df40719ca86475cfc9c2383a2aaf350994076021
Author: awy <awy@awy.one>
Date: Fri, 19 Dec 2025 19:20:06 +0300
center patch
Diffstat:
3 files changed, 52 insertions(+), 10 deletions(-)
diff --git a/config.def.h b/config.def.h
@@ -1,9 +1,12 @@
/* See LICENSE file for copyright and license details. */
/* Default settings; can be overriden by command line. */
-static int top = 1; /* -b option; if 0, appear at bottom */
-static const char *fonts[] = { "monospace:size=12" }; /* -f option overrides fonts[0] */
-static const char *prompt = NULL; /* -p option; prompt to the left of input field */
+static int top = 1; /* -b option; if 0, appear at bottom */
+static int centered = 0; /* -c option; centers mew on screen */
+static int min_width = 300; /* minimum width when centered */
+static const float menu_height_ratio = 4.0f; /* This is the ratio used in the original calculation */
+static const char *fonts[] = { "monospace:size=12" }; /* -f option overrides fonts[0] */
+static const char *prompt = NULL; /* -p option; prompt to the left of input field */
/* fg bg */
/* NORD */
diff --git a/mew.1 b/mew.1
@@ -37,6 +37,9 @@ is a script that lists programs in the user's $PATH and runs the result with
.B \-b
appear at the bottom of the screen.
.TP
+.B \-c
+mew appears centered on the screen.
+.TP
.B \-e
execute input or selection with
.BR /bin/sh ,
diff --git a/mew.c b/mew.c
@@ -51,6 +51,8 @@ static struct {
static char text[BUFSIZ] = "";
static int bh, mw, mh;
+static int out_w = 0;
+static int out_h = 0;
static int inputw = 0, promptw, passwd = 0;
static int32_t scale = 1;
static int lrpad; /* sum of left and right padding */
@@ -220,6 +222,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)
{
@@ -816,7 +827,12 @@ surface_handle_preferred_scale(void *data,
loadfonts();
/* the scale of the surface is only known after an initial draw, not before :( */
- zwlr_layer_surface_v1_set_size(layer_surface, 0, mh / scale);
+ if (centered == 1) {
+ promptw = (prompt && *prompt) ? TEXTW(prompt) - lrpad / 4 : 0;
+ mw = MIN(MAX(max_textw() + promptw, min_width * scale), out_w);
+ zwlr_layer_surface_v1_set_size(layer_surface, (mw / scale) - 1, mh / scale);
+ } else
+ zwlr_layer_surface_v1_set_size(layer_surface, 0, mh / scale);
redraw();
}
@@ -848,9 +864,19 @@ output_handle_name(void *data, struct wl_output *wl_output, const char *name)
wl_output_destroy(wl_output);
}
+static void
+output_handle_mode(void *data, struct wl_output *wl_output, uint32_t flags,
+ int32_t width, int32_t height, int32_t refresh)
+{
+ if (flags & WL_OUTPUT_MODE_CURRENT) {
+ out_w = width;
+ out_h = height;
+ }
+}
+
static const struct wl_output_listener output_listener = {
.geometry = noop,
- .mode = noop,
+ .mode = output_handle_mode,
.done = noop,
.scale = noop,
.name = output_handle_name,
@@ -999,10 +1025,18 @@ setup(void)
layer_surface = zwlr_layer_shell_v1_get_layer_surface(layer_shell,
surface, output, ZWLR_LAYER_SHELL_V1_LAYER_OVERLAY, "mew");
- zwlr_layer_surface_v1_set_size(layer_surface, 0, mh);
- zwlr_layer_surface_v1_set_anchor(layer_surface,
- (top ? ZWLR_LAYER_SURFACE_V1_ANCHOR_TOP : ZWLR_LAYER_SURFACE_V1_ANCHOR_BOTTOM ) |
- ZWLR_LAYER_SURFACE_V1_ANCHOR_LEFT | ZWLR_LAYER_SURFACE_V1_ANCHOR_RIGHT);
+ if (centered == 1) {
+ promptw = (prompt && *prompt) ? TEXTW(prompt) - lrpad / 4 : 0;
+ mw = MIN(MAX(max_textw() + promptw, min_width * scale), out_w);
+ zwlr_layer_surface_v1_set_size(layer_surface, mw - 1, mh);
+ zwlr_layer_surface_v1_set_margin(layer_surface, (out_h - mh) / menu_height_ratio, 0, 0, 0);
+ zwlr_layer_surface_v1_set_anchor(layer_surface, ZWLR_LAYER_SURFACE_V1_ANCHOR_TOP);
+ } else {
+ zwlr_layer_surface_v1_set_size(layer_surface, 0, mh);
+ zwlr_layer_surface_v1_set_anchor(layer_surface,
+ (top ? ZWLR_LAYER_SURFACE_V1_ANCHOR_TOP : ZWLR_LAYER_SURFACE_V1_ANCHOR_BOTTOM ) |
+ ZWLR_LAYER_SURFACE_V1_ANCHOR_LEFT | ZWLR_LAYER_SURFACE_V1_ANCHOR_RIGHT);
+ }
zwlr_layer_surface_v1_set_exclusive_zone(layer_surface, -1);
zwlr_layer_surface_v1_set_keyboard_interactivity(layer_surface, 1);
zwlr_layer_surface_v1_add_listener(layer_surface, &layer_surface_listener, NULL);
@@ -1013,7 +1047,7 @@ setup(void)
static void
usage(void)
{
- die("usage: mew [-beivP] [-l lines] [-p prompt] [-f font] [-o output]\n"
+ die("usage: mew [-beivcP] [-l lines] [-p prompt] [-f font] [-o output]\n"
" [-nb color] [-nf color] [-sb color] [-sf color]");
}
@@ -1036,6 +1070,8 @@ main(int argc, char *argv[])
fstrstr = cistrstr;
} else if (!strcmp(argv[i], "-P")) /* is the input a password */
passwd = 1;
+ else if (!strcmp(argv[i], "-c"))
+ centered = 1;
else if (i + 1 == argc)
usage();
else if (!strcmp(argv[i], "-l"))