From 04a3074fb4dddbe8bffb4e00b7d0932e65604d4c Mon Sep 17 00:00:00 2001 From: DreamMaoMao <2523610504@qq.com> Date: Sat, 16 May 2026 16:31:16 +0800 Subject: [PATCH] opt: make big one as the last open window in fair layout --- src/layout/horizontal.h | 23 ++++++++++++++--------- src/layout/vertical.h | 23 ++++++++++++++--------- 2 files changed, 28 insertions(+), 18 deletions(-) diff --git a/src/layout/horizontal.h b/src/layout/horizontal.h index 3ff8bd44..130d38f8 100644 --- a/src/layout/horizontal.h +++ b/src/layout/horizontal.h @@ -704,7 +704,11 @@ void fair(Monitor *m) { } int32_t base_rows = n / cols; // 每列的基础行数 - int32_t remainder = n % cols; // 多出来的窗口,分配给前 remainder 列 + int32_t remainder = n % cols; // 多出来的窗口 + + // 计算前半部分(大窗口)的列数和总窗口数 + int32_t first_group_cols = cols - remainder; + int32_t first_group_count = first_group_cols * base_rows; // 计算标准列宽 int32_t col_width = @@ -718,15 +722,16 @@ void fair(Monitor *m) { int32_t col_idx, row_idx, rows_in_this_col; // 判断当前窗口属于哪一列、哪一行 - if (i < remainder * (base_rows + 1)) { - col_idx = i / (base_rows + 1); - row_idx = i % (base_rows + 1); - rows_in_this_col = base_rows + 1; - } else { - int32_t offset = i - remainder * (base_rows + 1); - col_idx = remainder + offset / base_rows; - row_idx = offset % base_rows; + // 前半部分列拥有较少的行数(窗口更大),后半部分列承担余数(窗口更小) + if (i < first_group_count) { + col_idx = i / base_rows; + row_idx = i % base_rows; rows_in_this_col = base_rows; + } else { + int32_t offset = i - first_group_count; + col_idx = first_group_cols + (offset / (base_rows + 1)); + row_idx = offset % (base_rows + 1); + rows_in_this_col = base_rows + 1; } // 计算 X 坐标和宽度 (最后一列吃掉剩余像素,防止缝隙) diff --git a/src/layout/vertical.h b/src/layout/vertical.h index 484dfd85..74ed98a4 100644 --- a/src/layout/vertical.h +++ b/src/layout/vertical.h @@ -311,7 +311,11 @@ void vertical_fair(Monitor *m) { } int32_t base_cols = n / rows; // 每行的基础列数 - int32_t remainder = n % rows; // 多出来的窗口,分配给前 remainder 行 + int32_t remainder = n % rows; // 多出来的窗口 + + // 计算上半部分(大窗口)的行数和总窗口数 + int32_t first_group_rows = rows - remainder; + int32_t first_group_count = first_group_rows * base_cols; // 计算标准行高 int32_t row_height = @@ -325,15 +329,16 @@ void vertical_fair(Monitor *m) { 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; + // 上半部分行拥有较少的列数(窗口更宽),下半部分行承担余数(窗口更窄) + if (i < first_group_count) { + row_idx = i / base_cols; + col_idx = i % base_cols; cols_in_this_row = base_cols; + } else { + int32_t offset = i - first_group_count; + row_idx = first_group_rows + (offset / (base_cols + 1)); + col_idx = offset % (base_cols + 1); + cols_in_this_row = base_cols + 1; } // 计算 Y 坐标和高度 (最后一行吃掉剩余像素)