From 3f1ff03b947ddcfbfa98aba09207ccf74f1c73ea Mon Sep 17 00:00:00 2001 From: Hojun-Cho Date: Sun, 16 Aug 2026 18:27:24 +0900 Subject: [PATCH] popup: keep only computed geometry in Popup; one fallback for slivers numx, textx and selx were compile-time constants stored per layout; the number column starts at PopupPad and text at PopupPad+PopupNumw, which dat.h now says once. The height fallback tried dropping the marker, then the preedit, then the padding as three separate refits; below one padded row it now shows as many bare rows as fit, at least one, which is what those steps added up to. --- dat.h | 10 ++++++---- popup_layout.c | 26 ++++++++++---------------- tests/popup_test.c | 23 +++++++++-------------- 3 files changed, 25 insertions(+), 34 deletions(-) diff --git a/dat.h b/dat.h index 0dc8e73..f6dfcf7 100644 --- a/dat.h +++ b/dat.h @@ -142,6 +142,11 @@ struct Drawcmd Caret caret; }; +/* + * Popup geometry from popuplayout: n rows from row0 of the candidates, + * each Fontsz tall; a section's y is -1 when it is absent. Numbers + * start at PopupPad, text at PopupPad+PopupNumw. + */ typedef struct Popup Popup; struct Popup { @@ -152,14 +157,11 @@ struct Popup int prey; int sepy; int rowsy; - int numx; - int textx; int textw; - int markx; int marky; + int markx; int markw; Str mark; - int selx; int sely; int selw; }; diff --git a/popup_layout.c b/popup_layout.c index 06b55d5..376fc41 100644 --- a/popup_layout.c +++ b/popup_layout.c @@ -123,7 +123,6 @@ popuplayout(Drawcmd *dc, int areaw, int areah, Popup *p) p->prey = -1; p->sepy = -1; p->rowsy = -1; - p->markx = -1; p->marky = -1; p->sely = -1; pad = PopupPad; @@ -132,6 +131,8 @@ popuplayout(Drawcmd *dc, int areaw, int areah, Popup *p) if((nall == 0 && !npre) || areaw <= 0 || areah <= 0) return; + /* Rows first; a page marker when they do not all fit; and in a + * sliver of a work area the selected row alone, unpadded. */ total = max(dc->total, dc->first + nall); markrow = dc->first > 0 || nall < total; p->n = fitrows(areah, npre, markrow, nall); @@ -143,13 +144,10 @@ popuplayout(Drawcmd *dc, int areaw, int areah, Popup *p) markrow = 0; p->n = fitrows(areah, npre, markrow, nall); } - if(p->n == 0 && npre && nall != 0){ - npre = 0; - p->n = fitrows(areah, npre, markrow, nall); - } if(p->n == 0 && nall != 0){ - p->n = 1; + npre = 0; pad = 0; + p->n = max(min(nall, areah / Fontsz), 1); } if(p->n != 0 && dc->sel >= p->n) p->row0 = min(dc->sel - p->n + 1, nall - p->n); @@ -189,14 +187,11 @@ popuplayout(Drawcmd *dc, int areaw, int areah, Popup *p) y += Fontsz; } p->h = min(y + pad, areah); - p->numx = PopupPad; - p->textx = PopupPad + PopupNumw; - p->textw = max(p->w - PopupPad - p->textx, 0); + p->textw = max(p->w - 2*PopupPad - PopupNumw, 0); if(markrow){ p->markw = min(p->markw, max(p->w - 2*PopupPad, 0)); p->markx = p->w - PopupPad - p->markw; } - p->selx = PopupPad; p->selw = max(p->w - 2*PopupPad, 0); if(dc->sel >= p->row0 && dc->sel < p->row0 + p->n) p->sely = p->rowsy + (dc->sel - p->row0)*Fontsz; @@ -221,18 +216,17 @@ popupdraw(u32int *img, Drawcmd *dc, Popup *p) fillrect(img, p->w, p->h, PopupPad, p->sepy, p->w - 2*PopupPad, PopupSep, Colsep); if(p->sely >= 0) - fillrect(img, p->w, p->h, p->selx, p->sely, + fillrect(img, p->w, p->h, PopupPad, p->sely, p->selw, Fontsz, Colsel); for(i = 0, y = p->rowsy; i < p->n; i++, y += Fontsz){ j = p->row0 + i; color = j == dc->sel ? Colselfg : Colfg; sclear(&num); sputr(&num, '1' + j + Asciitofull); - textdraw(img, p->w, p->h, p->numx, y, - min(PopupNumw, max(p->w - PopupPad - p->numx, 0)), - color, &num); - textdraw(img, p->w, p->h, p->textx, y, p->textw, color, - &dc->kouho[j]); + textdraw(img, p->w, p->h, PopupPad, y, + min(PopupNumw, max(p->w - 2*PopupPad, 0)), color, &num); + textdraw(img, p->w, p->h, PopupPad + PopupNumw, y, p->textw, + color, &dc->kouho[j]); } if(p->marky >= 0) textdraw(img, p->w, p->h, p->markx, p->marky, p->markw, diff --git a/tests/popup_test.c b/tests/popup_test.c index 0043869..a257163 100644 --- a/tests/popup_test.c +++ b/tests/popup_test.c @@ -166,10 +166,8 @@ popup_layout(struct ct *t) CT_EQ_INT(t, PopupPad, p.rowsy); CT_EQ_INT(t, -1, p.marky); 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); + CT_EQ_INT(t, p.w - PopupPad, PopupPad + PopupNumw + p.textw); /* Preedit and candidates meet at one separator, with no empty rows. */ dc.pre = mkstr("preedit"); @@ -212,12 +210,9 @@ popup_layout(struct ct *t) CT_EQ_INT(t, 2*PopupBasew, 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); CT_EQ_INT(t, p.w - 2*PopupPad, p.selw); CT_EQ_INT(t, p.rowsy + dc.sel*Fontsz, p.sely); - CT_EQ_INT(t, PopupPad, p.numx); - CT_EQ_INT(t, p.numx + PopupNumw, p.textx); - CT_EQ_INT(t, p.w - PopupPad, p.textx + p.textw); + CT_EQ_INT(t, p.w - PopupPad, PopupPad + PopupNumw + p.textw); img = emalloc(2*PopupBasew*Imgh*sizeof img[0]); popupdraw(img, &dc, &p); @@ -232,7 +227,7 @@ popup_layout(struct ct *t) for(y = p.sely; y < p.sely + Fontsz; y++){ for(x = 0; x < p.w; x++){ c = pixel(img, &p, x, y); - if(x < p.selx || x >= p.selx + p.selw){ + if(x < PopupPad || x >= PopupPad + p.selw){ CT_EQ_UINT(t, Colbg, c); continue; } @@ -241,9 +236,9 @@ popup_layout(struct ct *t) continue; } CT_CHECK(t, - (x >= p.numx && x < p.numx + PopupNumw) || - (x >= p.textx && x < p.textx + p.textw)); - if(x < p.numx + PopupNumw) + (x >= PopupPad && x < PopupPad + PopupNumw) || + (x >= (PopupPad + PopupNumw) && x < (PopupPad + PopupNumw) + p.textw)); + if(x < PopupPad + PopupNumw) numink++; else textink++; @@ -277,7 +272,7 @@ popup_layout(struct ct *t) CT_CHECK(t, p.textw > PopupTextw); CT_CHECK(t, n > p.textw); CT_EQ_INT(t, PopupPad, p.rowsy); - CT_EQ_INT(t, p.w - PopupPad, p.textx + p.textw); + CT_EQ_INT(t, p.w - PopupPad, PopupPad + PopupNumw + p.textw); popupdraw(img, &dc, &p); ink = 0; for(y = p.rowsy; y < p.rowsy + Fontsz; y++){ @@ -285,7 +280,7 @@ popup_layout(struct ct *t) c = pixel(img, &p, x, y); if(x < PopupPad || x >= p.w - PopupPad) CT_EQ_UINT(t, Colbg, c); - if(x >= p.textx && x < p.textx + p.textw && + if(x >= (PopupPad + PopupNumw) && x < (PopupPad + PopupNumw) + p.textw && c != (Colbg & Rgbmask)) ink++; } @@ -312,7 +307,7 @@ popup_layout(struct ct *t) popupdraw(img, &dc, &p); numink = 0; for(y = p.rowsy; y < p.rowsy + Fontsz; y++) - for(x = p.numx; x < p.numx + PopupNumw; x++) + for(x = PopupPad; x < PopupPad + PopupNumw; x++) if(pixel(img, &p, x, y) != (Colsel & Rgbmask)) numink++; markink = 0;