From 0471d58deca8013bc0fc47611aa661352f329bfe Mon Sep 17 00:00:00 2001 From: Hojun-Cho Date: Fri, 14 Aug 2026 21:13:49 +0900 Subject: [PATCH] popup: stabilize the input panel width --- dat.h | 3 ++- popup_layout.c | 17 +++-------------- tests/popup_test.c | 24 +++++++++++++++++------- 3 files changed, 22 insertions(+), 22 deletions(-) diff --git a/dat.h b/dat.h index fb9e978..9bb1482 100644 --- a/dat.h +++ b/dat.h @@ -21,6 +21,7 @@ enum PopupPad = 4, PopupSep = 1, PopupNumw = Fontsz, + PopupTextw = 12*Fontsz, Maxrunes = 64, Maxutf = Maxrunes * UTFmax + 1, }; @@ -30,7 +31,7 @@ enum Maxclients = 64, Maxkouho = 32, Maxdisp = 9, - Imgw = (Maxrunes + 3) * Fontsz, + Imgw = 2*PopupPad + PopupNumw + PopupTextw, Imgh = 2*PopupPad + (Maxdisp + 2)*Fontsz + PopupSep, Colfg = 0x000000, diff --git a/popup_layout.c b/popup_layout.c index a47b25a..1f5cc04 100644 --- a/popup_layout.c +++ b/popup_layout.c @@ -44,9 +44,8 @@ void popuplayout(Drawcmd *dc, int screenw, Popup *p) { char buf[32]; - vlong width; Str mark; - int i, maxw, nmark, npre, roww, y; + int nmark, npre, y; memset(p, 0, sizeof *p); p->prey = -1; @@ -60,19 +59,8 @@ popuplayout(Drawcmd *dc, int screenw, Popup *p) if(p->n == 0 && !npre) return; - maxw = npre ? textwidth(&dc->pre) : 0; - roww = 0; - for(i = 0; i < p->n; i++) - roww = max(roww, textwidth(&dc->kouho[i])); - if(p->n != 0) - maxw = max(maxw, PopupNumw + roww); + p->w = min(Imgw, max(screenw, 0)); nmark = pagemarker(buf, sizeof buf, dc->first, p->n, dc->total); - if(nmark != 0){ - sinit(&mark, buf, nmark); - maxw = max(maxw, textwidth(&mark)); - } - width = (vlong)maxw + 2*PopupPad; - p->w = min(min(max(width, 1), Imgw), max(screenw, 0)); y = PopupPad; if(npre){ @@ -96,6 +84,7 @@ popuplayout(Drawcmd *dc, int screenw, Popup *p) p->textx = PopupPad + PopupNumw; p->textw = max(p->w - PopupPad - p->textx, 0); if(nmark != 0){ + sinit(&mark, buf, nmark); p->markw = min(textwidth(&mark), max(p->w - 2*PopupPad, 0)); p->markx = p->w - PopupPad - p->markw; } diff --git a/tests/popup_test.c b/tests/popup_test.c index 40c8454..5f9cd2d 100644 --- a/tests/popup_test.c +++ b/tests/popup_test.c @@ -69,6 +69,8 @@ popup_layout(struct ct *t) CT_EQ_INT(t, strlen(markers[i].want), n); CT_CHECK(t, strcmp(markers[i].want, buf) == 0); } + CT_EQ_INT(t, 12*Fontsz, PopupTextw); + CT_EQ_INT(t, 2*PopupPad + PopupNumw + PopupTextw, Imgw); CT_EQ_INT(t, 2*PopupPad + (1 + Maxdisp + 1)*Fontsz + PopupSep, Imgh); @@ -137,7 +139,8 @@ popup_layout(struct ct *t) /* Preedit only. */ dc.pre = mkstr("preedit"); - popuplayout(&dc, Imgw, &p); + popuplayout(&dc, 2*Imgw, &p); + CT_EQ_INT(t, Imgw, p.w); CT_EQ_INT(t, PopupPad, p.prey); CT_EQ_INT(t, -1, p.sepy); CT_EQ_INT(t, -1, p.rowsy); @@ -150,7 +153,8 @@ popup_layout(struct ct *t) dc.nkouho = 2; dc.kouho[0] = mkstr("short"); dc.kouho[1] = mkstr("candidate"); - popuplayout(&dc, Imgw, &p); + popuplayout(&dc, 2*Imgw, &p); + CT_EQ_INT(t, Imgw, p.w); CT_EQ_INT(t, -1, p.prey); CT_EQ_INT(t, -1, p.sepy); CT_EQ_INT(t, PopupPad, p.rowsy); @@ -158,11 +162,13 @@ popup_layout(struct ct *t) CT_EQ_INT(t, 2*PopupPad + 2*Fontsz, p.h); CT_EQ_INT(t, PopupPad, p.numx); CT_EQ_INT(t, p.numx + PopupNumw, p.textx); + CT_EQ_INT(t, PopupTextw, p.textw); CT_EQ_INT(t, p.w - PopupPad, p.textx + p.textw); /* Preedit and candidates meet at one separator, with no empty rows. */ dc.pre = mkstr("preedit"); - popuplayout(&dc, Imgw, &p); + popuplayout(&dc, 2*Imgw, &p); + CT_EQ_INT(t, Imgw, p.w); CT_EQ_INT(t, PopupPad, p.prey); CT_EQ_INT(t, p.prey + Fontsz, p.sepy); CT_EQ_INT(t, p.sepy + PopupSep, p.rowsy); @@ -183,7 +189,7 @@ popup_layout(struct ct *t) for(i = 0; i < Maxdisp; i++) dc.kouho[i] = mkstr(i == dc.sel ? "M" : "candidate"); dc.kouho[0] = longrow; - popuplayout(&dc, Imgw, &p); + popuplayout(&dc, 2*Imgw, &p); CT_EQ_INT(t, Maxdisp, p.n); CT_EQ_INT(t, PopupPad, p.prey); CT_EQ_INT(t, p.prey + Fontsz, p.sepy); @@ -191,8 +197,7 @@ popup_layout(struct ct *t) CT_EQ_INT(t, p.rowsy + Maxdisp*Fontsz, p.marky); CT_EQ_INT(t, p.marky + Fontsz + PopupPad, p.h); CT_EQ_INT(t, Imgh, p.h); - CT_CHECK(t, p.w > 2*PopupPad + PopupNumw); - CT_CHECK(t, p.w <= Imgw); + CT_EQ_INT(t, Imgw, p.w); CT_CHECK(t, p.h <= Imgh); CT_EQ_INT(t, p.w - PopupPad, p.markx + p.markw); CT_EQ_INT(t, PopupPad, p.selx); @@ -255,7 +260,9 @@ popup_layout(struct ct *t) dc.nkouho = 1; dc.kouho[0] = longrow; n = textwidth(&longrow); - popuplayout(&dc, 6*Fontsz, &p); + popuplayout(&dc, 2*Imgw, &p); + CT_EQ_INT(t, Imgw, p.w); + CT_EQ_INT(t, PopupTextw, p.textw); CT_CHECK(t, n > p.textw); CT_EQ_INT(t, PopupPad, p.rowsy); CT_EQ_INT(t, p.w - PopupPad, p.textx + p.textw); @@ -273,5 +280,8 @@ popup_layout(struct ct *t) } CT_CHECK(t, ink > 0); CT_EQ_INT(t, n, textwidth(&longrow)); + + popuplayout(&dc, Imgw - 1, &p); + CT_EQ_INT(t, Imgw - 1, p.w); free(img); }