From 804e69ca4bf586bcec46e018630f94c1c4e0b7e7 Mon Sep 17 00:00:00 2001
From: JoaoCostaIFG <joaocosta.work@posteo.net>
Date: Thu, 8 Aug 2024 00:05:27 +0100
Subject: [PATCH 2/2] Add gaps support to snail layout

---
 dwl.c | 105 +++++++++++++++++++++++++++++++++-------------------------
 1 file changed, 59 insertions(+), 46 deletions(-)

diff --git a/dwl.c b/dwl.c
index 46bdca1..a158dd6 100644
--- a/dwl.c
+++ b/dwl.c
@@ -2659,8 +2659,8 @@ void
 snail(Monitor *m)
 {
 	int i = 0, n = 0;
-	unsigned int mw = m->w.width;
-	Client *c, *prev;
+	unsigned int mw = m->w.width, e = m->gaps, w = 0, h = 0, egappx = 0;
+	Client *c, *prev = NULL;
 	enum wlr_direction dir = WLR_DIRECTION_RIGHT;
 
 	wl_list_for_each(c, &clients, link)
@@ -2668,9 +2668,12 @@ snail(Monitor *m)
 			n++;
 	if (n == 0)
 		return;
+  if (smartgaps == n)
+		e = 0;
+	egappx = e * gappx;
 
 	if (n > m->nmaster)
-		mw = m->nmaster ? ROUND(m->w.width * m->mfact) : 0;
+		mw = m->nmaster ? (unsigned int)round((m->w.width + egappx) * m->mfact) : 0;
 
 	wl_list_for_each(c, &clients, link) {
 		if (!VISIBLEON(c, m) || c->isfloating || c->isfullscreen)
@@ -2681,8 +2684,8 @@ snail(Monitor *m)
 		 * master area with this window
 		 */
 		if (mw > 0 && i == 0) {
-			c->geom = (struct wlr_box){.x = m->w.x, .y = m->w.y,
-				.width = mw, .height = m->w.height};
+			c->geom = (struct wlr_box){.x = m->w.x + egappx, .y = m->w.y + egappx,
+				.width = mw - 2*egappx, .height = m->w.height - 2*egappx};
 			/*
 			 * If the first window in the master area is wide, split it
 			 * horizontally and put next one on its right; otherwise, split it
@@ -2694,55 +2697,65 @@ snail(Monitor *m)
 		 * m->nmaster-th window
 		 */
 		} else if (i == m->nmaster) {
-			c->geom = (struct wlr_box){.x = m->w.x + mw, .y = m->w.y,
-				.width = m->w.width - mw, .height = m->w.height};
+			c->geom = (struct wlr_box){.x = m->w.x + mw + egappx, .y = m->w.y + egappx,
+				.width = m->w.width - mw - 2*egappx, .height = m->w.height - 2*egappx};
 			/*
 			 * If the first window in the stack is wide, split it horizontally
 			 * and put next one on its right; otherwise, split it vertically and
 			 * put the next one below it
 			 */
 			dir = c->geom.width > m->w.height ? WLR_DIRECTION_RIGHT : WLR_DIRECTION_DOWN;
-		/*
-		 * Split the previous horizontally and put the current window on the right
-		 */
-		} else if (dir == WLR_DIRECTION_RIGHT) {
-			c->geom = (struct wlr_box){.x = prev->geom.x + prev->geom.width / 2, .y = prev->geom.y,
-				.width = prev->geom.width / 2, .height = prev->geom.height};
-			prev->geom = (struct wlr_box){.x = prev->geom.x, .y = prev->geom.y,
-				.width = prev->geom.width / 2, .height = prev->geom.height};
+		} else if (prev) {
 			/*
-			 * If it's a stack window or the first narrow window in the master
-			 * area, put the next one below it
+			 * Split the previous horizontally and put the current window on the right
 			 */
-			if (i >= m->nmaster || c->geom.width < m->w.height)
-				dir = WLR_DIRECTION_DOWN;
-		/*
-		 * Split the previous vertically and put the current window below it
-		 */
-		} else if (dir == WLR_DIRECTION_DOWN) {
-			c->geom = (struct wlr_box){.x = prev->geom.x, .y = prev->geom.y + prev->geom.height / 2,
-				.width = prev->geom.width, .height = prev->geom.height / 2};
-			prev->geom = (struct wlr_box){.x = prev->geom.x, .y = prev->geom.y,
-				.width = prev->geom.width, .height = prev->geom.height / 2};
-			dir = WLR_DIRECTION_LEFT;
-		/*
-		 * Split the previous horizontally and put the current window on the left
-		 */
-		} else if (dir == WLR_DIRECTION_LEFT) {
-			c->geom = (struct wlr_box){.x = prev->geom.x, .y = prev->geom.y,
-				.width = prev->geom.width / 2, .height = prev->geom.height};
-			prev->geom = (struct wlr_box){.x = prev->geom.x + prev->geom.width / 2, .y = prev->geom.y,
-				.width = prev->geom.width / 2, .height = prev->geom.height};
-			dir = WLR_DIRECTION_UP;
-		/*
-		 * Split the previous vertically and put the current window above it
-		 */
-		} else {
-			c->geom = (struct wlr_box){.x = prev->geom.x, .y = prev->geom.y,
-				.width = prev->geom.width, .height = prev->geom.height / 2};
-			prev->geom = (struct wlr_box){.x = prev->geom.x, .y = prev->geom.y + prev->geom.height / 2,
-				.width = prev->geom.width, .height = prev->geom.height / 2};
-			dir = WLR_DIRECTION_RIGHT;
+			if (dir == WLR_DIRECTION_RIGHT) {
+				w = prev->geom.width / 2 - egappx;
+				h = prev->geom.height;
+				c->geom = (struct wlr_box){.x = prev->geom.x + prev->geom.width / 2 + egappx, .y = prev->geom.y,
+					.width = w, .height = h};
+				prev->geom = (struct wlr_box){.x = prev->geom.x, .y = prev->geom.y,
+					.width = w, .height = h};
+				/*
+				 * If it's a stack window or the first narrow window in the master
+				 * area, put the next one below it
+				 */
+				if (i >= m->nmaster || c->geom.width < m->w.height)
+					dir = WLR_DIRECTION_DOWN;
+			/*
+			 * Split the previous vertically and put the current window below it
+			 */
+			} else if (dir == WLR_DIRECTION_DOWN) {
+				w = prev->geom.width;
+				h = prev->geom.height / 2 - egappx;
+				c->geom = (struct wlr_box){.x = prev->geom.x, .y = prev->geom.y + prev->geom.height / 2 + egappx,
+					.width = w, .height = h};
+				prev->geom = (struct wlr_box){.x = prev->geom.x, .y = prev->geom.y,
+					.width = w, .height = h};
+				dir = WLR_DIRECTION_LEFT;
+			/*
+			 * Split the previous horizontally and put the current window on the left
+			 */
+			} else if (dir == WLR_DIRECTION_LEFT) {
+				w = prev->geom.width / 2 - egappx;
+				h = prev->geom.height;
+				c->geom = (struct wlr_box){.x = prev->geom.x, .y = prev->geom.y,
+					.width = w, .height = h};
+				prev->geom = (struct wlr_box){.x = prev->geom.x + prev->geom.width / 2 + egappx, .y = prev->geom.y,
+					.width = w, .height = h};
+				dir = WLR_DIRECTION_UP;
+			/*
+			 * Split the previous vertically and put the current window above it
+			 */
+			} else {
+				w = prev->geom.width;
+				h = prev->geom.height / 2 - egappx;
+				c->geom = (struct wlr_box){.x = prev->geom.x, .y = prev->geom.y,
+					.width = w, .height = h};
+				prev->geom = (struct wlr_box){.x = prev->geom.x, .y = prev->geom.y + prev->geom.height / 2 + egappx,
+					.width = w, .height = h};
+				dir = WLR_DIRECTION_RIGHT;
+			}
 		}
 		i++;
 		prev = c;
-- 
2.46.0


