Implement --bias for the grid layout

This commit is contained in:
Kovid Goyal 2024-07-20 13:11:06 +05:30
parent 92385f6db7
commit 5ab484cac2
No known key found for this signature in database
GPG key ID: 06BC317B515ACE7C
2 changed files with 28 additions and 3 deletions

View file

@ -213,6 +213,14 @@ def options_spec() -> str:
* Fat layout: Same as tall layout except it goes by rows instead of columns.
* Grid layout: The bias is interpreted the same way as for the Vertical and Horizontal
layouts, as something to be added/subtracted to the normal size. However, the
since in a grid layout there are rows *and* columns, the bias on the first window in a column
operates on the columns. Any later windows in that column operate on the row.
So, for example, if you bias the first window in a grid layout it will change the width
of the first column, the second window, the width of the second column, the third window,
the height of the second row and so on.
The bias option was introduced in kitty version 0.36.0.

View file

@ -73,10 +73,27 @@ def on_col_done(col_windows: List[int]) -> None:
row_num += 1
return 0, 0
def bias_slot(self, all_windows: WindowList, idx: int, fractional_bias: float, cell_increment_bias_h: float, cell_increment_bias_v: float) -> bool:
num_windows = all_windows.num_groups
ncols, nrows, special_rows, special_col = calc_grid_size(num_windows)
row_num, col_num = self.position_for_window_idx(idx, num_windows, ncols, nrows, special_rows, special_col)
if row_num == 0:
b = self.biased_cols
layout_func = self.column_layout
bias_idx = col_num
increment = cell_increment_bias_h
else:
b = self.biased_rows
layout_func = self.row_layout
bias_idx = row_num
increment = cell_increment_bias_v
before_layout = tuple(self.variable_layout(layout_func, num_windows, b))
b[bias_idx] = increment
return tuple(self.variable_layout(layout_func, num_windows, b)) == before_layout
def apply_bias(self, idx: int, increment: float, all_windows: WindowList, is_horizontal: bool = True) -> bool:
num_windows = all_windows.num_groups
ncols, nrows, special_rows, special_col = calc_grid_size(num_windows)
row_num, col_num = self.position_for_window_idx(idx, num_windows, ncols, nrows, special_rows, special_col)
if is_horizontal:
@ -99,11 +116,11 @@ def layout_func(windows: ListOfWindows, bias: Optional[Sequence[float]] = None)
def layout_func(windows: ListOfWindows, bias: Optional[Sequence[float]] = None) -> LayoutDimension:
return self.row_layout(num_windows, bias=bias)
before_layout = list(self.variable_layout(layout_func, num_windows, b))
before_layout = tuple(self.variable_layout(layout_func, num_windows, b))
candidate = b.copy()
before = candidate.get(bias_idx, 0)
candidate[bias_idx] = before + increment
if before_layout == list(self.variable_layout(layout_func, num_windows, candidate)):
if before_layout == tuple(self.variable_layout(layout_func, num_windows, candidate)):
return False
setattr(self, attr, candidate)
return True