feat: fair layout

This commit is contained in:
DreamMaoMao 2026-05-16 08:23:48 +08:00
parent f9e17b6d76
commit 14d2827bae
5 changed files with 160 additions and 0 deletions

View file

@ -281,4 +281,80 @@ void vertical_grid(Monitor *m) {
i++;
}
}
}
void vertical_fair(Monitor *m) {
int32_t i, n = 0;
Client *c = NULL;
n = m->visible_tiling_clients;
if (n == 0)
return;
// 间隙参数处理
int32_t cur_gappiv = enablegaps ? m->gappiv : 0;
int32_t cur_gappih = enablegaps ? m->gappih : 0;
int32_t cur_gappov = enablegaps ? m->gappov : 0;
int32_t cur_gappoh = enablegaps ? m->gappoh : 0;
// 智能间隙
cur_gappiv = config.smartgaps && n == 1 ? 0 : cur_gappiv;
cur_gappih = config.smartgaps && n == 1 ? 0 : cur_gappih;
cur_gappov = config.smartgaps && n == 1 ? 0 : cur_gappov;
cur_gappoh = config.smartgaps && n == 1 ? 0 : cur_gappoh;
// 计算最佳行数 rows = ceil(sqrt(n))
int32_t rows;
for (rows = 0; rows <= n; rows++) {
if (rows * rows >= n)
break;
}
int32_t base_cols = n / rows; // 每行的基础列数
int32_t remainder = n % rows; // 多出来的窗口,分配给前 remainder 行
// 计算标准行高
int32_t row_height =
(m->w.height - 2 * cur_gappov - (rows - 1) * cur_gappiv) / rows;
i = 0;
wl_list_for_each(c, &clients, link) {
if (!VISIBLEON(c, m) || !ISTILED(c))
continue;
int32_t row_idx, col_idx, cols_in_this_row;
// 判断当前窗口属于哪一行、哪一列
if (i < remainder * (base_cols + 1)) {
row_idx = i / (base_cols + 1);
col_idx = i % (base_cols + 1);
cols_in_this_row = base_cols + 1;
} else {
int32_t offset = i - remainder * (base_cols + 1);
row_idx = remainder + offset / base_cols;
col_idx = offset % base_cols;
cols_in_this_row = base_cols;
}
// 计算 Y 坐标和高度 (最后一行吃掉剩余像素)
int32_t cy = m->w.y + cur_gappov + row_idx * (row_height + cur_gappiv);
int32_t ch = (row_idx == rows - 1)
? (m->w.height - 2 * cur_gappov -
row_idx * (row_height + cur_gappiv))
: row_height;
// 计算 X 坐标和宽度 (最后一列吃掉剩余像素,防止缝隙)
int32_t base_cw = (m->w.width - 2 * cur_gappoh -
(cols_in_this_row - 1) * cur_gappih) /
cols_in_this_row;
int32_t cx = m->w.x + cur_gappoh + col_idx * (base_cw + cur_gappih);
int32_t cw = (col_idx == cols_in_this_row - 1)
? (m->w.width - 2 * cur_gappoh -
col_idx * (base_cw + cur_gappih))
: base_cw;
resize(c, (struct wlr_box){.x = cx, .y = cy, .width = cw, .height = ch},
0);
i++;
}
}