add gappless grid layout

This commit is contained in:
Jakub 2025-01-24 10:17:48 -05:00
parent c4ab8a48ad
commit ed5debbb62
4 changed files with 93 additions and 0 deletions

View File

@ -121,12 +121,14 @@ static const int nmaster = 1; /* number of clients in master area */
static const int resizehints = 1; /* 1 means respect size hints in tiled resizals */
static const int lockfullscreen = 1; /* 1 will force focus on the fullscreen window */
#include "gaplessgrid.c"
static const Layout layouts[] = {
/* symbol arrange function */
{ "[]=", tile }, /* first entry is default */
{ "><>", NULL }, /* no layout function means floating behavior */
{ "[M]", monocle },
{ "[D]", deck },
{ "###", gaplessgrid },
};
/* key definitions */

47
gaplessgrid.c Normal file
View File

@ -0,0 +1,47 @@
void
gaplessgrid(Monitor *m)
{
unsigned int i, n;
int x, y, cols, rows, ch, cw, cn, rn, rrest, crest; // counters
Client *c;
for (n = 0, c = nexttiled(m->clients); c; c = nexttiled(c->next), n++);
if ( n == 0 )
return;
/* grid dimensions */
for (cols = 0; cols <= n/2; cols++)
if (cols*cols >= n)
break;
if (n == 5) /* set layout against the general calculation: not 1:2:2, but 2:3 */
cols = 2;
rows = n/cols;
cn = rn = 0; // reset column no, row no, client count
ch = (m->wh - 2*gappx - gappx * (rows - 1)) / rows;
cw = (m->ww - 2*gappx - gappx * (cols - 1)) / cols;
rrest = (m->wh - 2*gappx - gappx * (rows - 1)) - ch * rows;
crest = (m->ww - 2*gappx - gappx * (cols - 1)) - cw * cols;
x = m->wx + gappx;
y = m->wy + gappx;
for (i = 0, c = nexttiled(m->clients); c; i++, c = nexttiled(c->next)) {
if (i/rows + 1 > cols - n%cols) {
rows = n/cols + 1;
ch = (m->wh - 2*gappx - gappx * (rows - 1)) / rows;
rrest = (m->wh - 2*gappx - gappx * (rows - 1)) - ch * rows;
}
resize(c,
x,
y + rn*(ch + gappx) + MIN(rn, rrest),
cw + (cn < crest ? 1 : 0) - 2*c->bw,
ch + (rn < rrest ? 1 : 0) - 2*c->bw,
0);
rn++;
if (rn >= rows) {
rn = 0;
x += cw + gappx + (cn < crest ? 1 : 0);
cn++;
}
}
}

View File

@ -0,0 +1,43 @@
URL: http://dwm.suckless.org/patches/gapless_grid
Add gapless grid layout.
Index: dwm/gaplessgrid.c
===================================================================
--- /dev/null
+++ dwm/gaplessgrid.c
@@ -0,0 +1,35 @@
+void
+gaplessgrid(Monitor *m) {
+ unsigned int n, cols, rows, cn, rn, i, cx, cy, cw, ch;
+ Client *c;
+
+ for(n = 0, c = nexttiled(m->clients); c; c = nexttiled(c->next), n++) ;
+ if(n == 0)
+ return;
+
+ /* grid dimensions */
+ for(cols = 0; cols <= n/2; cols++)
+ if(cols*cols >= n)
+ break;
+ if(n == 5) /* set layout against the general calculation: not 1:2:2, but 2:3 */
+ cols = 2;
+ rows = n/cols;
+
+ /* window geometries */
+ cw = cols ? m->ww / cols : m->ww;
+ cn = 0; /* current column number */
+ rn = 0; /* current row number */
+ for(i = 0, c = nexttiled(m->clients); c; i++, c = nexttiled(c->next)) {
+ if(i/rows + 1 > cols - n%cols)
+ rows = n/cols + 1;
+ ch = rows ? m->wh / rows : m->wh;
+ cx = m->wx + cn*cw;
+ cy = m->wy + rn*ch;
+ resize(c, cx, cy, cw - 2 * c->bw, ch - 2 * c->bw, False);
+ rn++;
+ if(rn >= rows) {
+ rn = 0;
+ cn++;
+ }
+ }
+}

View File

@ -5,4 +5,5 @@ cat <<EOF | xmenu
><> Floating Layout 1
[M] Monocle Layout 2
[D] Deck Layout 3
### Grid Layout 4
EOF