Simplify display width/height code

With this commit, display width/height is only grabbed once. This means
we don't need to deal with all that X11 and Xinerama code when we're
simply resizing the window.
This commit is contained in:
speedie 2023-06-24 17:13:17 +02:00
parent bd49d876cd
commit 336bdd5830
4 changed files with 33 additions and 86 deletions

View file

@ -34,11 +34,14 @@ void handle_wl(void) {
create_layer(&state, "spmenu"); create_layer(&state, "spmenu");
sp.mw = (menuwidth > 0 ? menuwidth : output_width); mo.output_width = output_width;
mo.output_height = output_height;
sp.mw = (menuwidth > 0 ? menuwidth : mo.output_width);
get_mh(); get_mh();
if (menuposition == 2) { if (menuposition == 2) {
sp.mw = MIN(MAX(max_textw() + sp.promptw, minwidth), output_width); sp.mw = MIN(MAX(max_textw() + sp.promptw, minwidth), mo.output_width);
} }
state.width = sp.mw; state.width = sp.mw;

View file

@ -72,14 +72,6 @@ void set_prop_x11(void) {
void resizeclient_x11(void) { void resizeclient_x11(void) {
int mh = sp.mh; int mh = sp.mh;
int x, y; int x, y;
#if USEXINERAMA
int j, di, a, n, area = 0;
XineramaScreenInfo *info;
Window pw;
unsigned int du;
Window w, dw, *dws;
#endif
XWindowAttributes wa;
struct item *item; struct item *item;
int ic = 0; // item count int ic = 0; // item count
@ -102,63 +94,14 @@ void resizeclient_x11(void) {
sp.mh -= sp.bh; sp.mh -= sp.bh;
} }
// init xinerama screens if (menuposition == 2) { // centered
#if USEXINERAMA sp.mw = MIN(MAX(max_textw() + sp.promptw, minwidth), mo.output_width);
int i = 0; x = (mo.output_width - sp.mw) / 2 + xpos;
if (parentwin == root && (info = XineramaQueryScreens(dpy, &n))) { y = (mo.output_height - sp.mh) / 2 - ypos;
XGetInputFocus(dpy, &w, &di); } else { // top or bottom
if (mon >= 0 && mon < n) { x = 0;
i = mon; y = menuposition ? 0 : mo.output_width - sp.mh - ypos;
} else if (w != root && w != PointerRoot && w != None) { sp.mw = (menuwidth > 0 ? menuwidth : mo.output_width);
do {
if (XQueryTree(dpy, (pw = w), &dw, &w, &dws, &du) && dws)
XFree(dws);
} while (w != root && w != pw);
if (XGetWindowAttributes(dpy, pw, &wa)) {
for (j = 0; j < n; j++) {
if ((a = INTERSECT(wa.x, wa.y, wa.width, wa.height, info[j])) > area) {
area = a;
i = j;
}
}
}
if (mon < 0 && !area && XQueryPointer(dpy, root, &dw, &dw, &x, &y, &di, &di, &du)) {
for (i = 0; i < n; i++) {
if (INTERSECT(x, y, 1, 1, info[i])) {
break;
}
}
}
}
// calculate x/y position
if (menuposition == 2) { // centered
sp.mw = MIN(MAX(max_textw() + sp.promptw, minwidth), info[i].width);
x = info[i].x_org + ((info[i].width - sp.mw) / 2);
y = info[i].y_org + ((info[i].height - sp.mh) / 2);
} else { // top or bottom
x = info[i].x_org + xpos;
y = info[i].y_org + (menuposition ? 0 : info[i].height - sp.mh - ypos);
sp.mw = (menuwidth>0 ? menuwidth : info[i].width);
}
XFree(info);
} else
#endif
{
if (!XGetWindowAttributes(dpy, parentwin, &wa))
die("spmenu: could not get embedding window attributes: 0x%lx",
parentwin); // die because unable to get attributes for the parent window
if (menuposition == 2) { // centered
sp.mw = MIN(MAX(max_textw() + sp.promptw, minwidth), wa.width);
x = (wa.width - sp.mw) / 2;
y = (wa.height - sp.mh) / 2;
} else { // top or bottom
x = 0;
y = menuposition ? 0 : wa.height - sp.mh - ypos;
sp.mw = (menuwidth > 0 ? menuwidth : wa.width);
}
} }
// no window/invalid window or menu height we had before is the same as the current window height // no window/invalid window or menu height we had before is the same as the current window height

View file

@ -49,16 +49,8 @@ void setupdisplay_x11(void) {
if (INTERSECT(x, y, 1, 1, info[i])) if (INTERSECT(x, y, 1, 1, info[i]))
break; break;
// calculate x/y position mo.output_width = info[i].width;
if (menuposition == 2) { // centered mo.output_height = info[i].height;
sp.mw = MIN(MAX(max_textw() + sp.promptw, minwidth), info[i].width);
x = info[i].x_org + xpos + ((info[i].width - sp.mw) / 2);
y = info[i].y_org - ypos + ((info[i].height - sp.mh) / 2);
} else { // top or bottom
x = info[i].x_org + xpos;
y = info[i].y_org + (menuposition ? 0 : info[i].height - sp.mh - ypos);
sp.mw = (menuwidth > 0 ? menuwidth : info[i].width);
}
XFree(info); XFree(info);
} else } else
@ -68,15 +60,18 @@ void setupdisplay_x11(void) {
die("spmenu: could not get embedding window attributes: 0x%lx", die("spmenu: could not get embedding window attributes: 0x%lx",
parentwin); // die because unable to get attributes for the parent window parentwin); // die because unable to get attributes for the parent window
if (menuposition == 2) { // centered mo.output_width = wa.width;
sp.mw = MIN(MAX(max_textw() + sp.promptw, minwidth), wa.width); mo.output_height = wa.height;
x = (wa.width - sp.mw) / 2 + xpos; }
y = (wa.height - sp.mh) / 2 - ypos;
} else { // top or bottom if (menuposition == 2) { // centered
x = 0; sp.mw = MIN(MAX(max_textw() + sp.promptw, minwidth), mo.output_width);
y = menuposition ? 0 : wa.width - sp.mh - ypos; x = (mo.output_width - sp.mw) / 2 + xpos;
sp.mw = (menuwidth > 0 ? menuwidth : wa.width); y = (mo.output_height - sp.mh) / 2 - ypos;
} } else { // top or bottom
x = 0;
y = menuposition ? 0 : mo.output_width - sp.mh - ypos;
sp.mw = (menuwidth > 0 ? menuwidth : mo.output_width);
} }
// create menu window and set properties for it // create menu window and set properties for it

View file

@ -138,6 +138,11 @@ struct sp {
int ignoreglobalmouse; // same for mouse int ignoreglobalmouse; // same for mouse
}; };
struct mo {
int output_width; // output width
int output_height; // output height
};
#if USEIMAGE #if USEIMAGE
struct img { struct img {
int setlines; // actual lines int setlines; // actual lines
@ -170,6 +175,7 @@ struct x11 {
static struct sp sp = {0}; static struct sp sp = {0};
static struct tx tx = {0}; static struct tx tx = {0};
static struct mo mo = {0};
#if USEIMAGE #if USEIMAGE
static struct img img = {0}; static struct img img = {0};
#endif #endif