Port Grid layout to new groups API

This commit is contained in:
Kovid Goyal 2020-05-07 12:18:12 +05:30
parent f801c47d1e
commit 70ccc1cf6d
No known key found for this signature in database
GPG Key ID: 06BC317B515ACE7C

View File

@ -8,12 +8,11 @@ from typing import Callable, Dict, Generator, List, Optional, Sequence, Tuple
from kitty.constants import Edges from kitty.constants import Edges
from kitty.typing import WindowType from kitty.typing import WindowType
from kitty.window_list import WindowList from kitty.window_list import WindowGroup, WindowList
from .base import ( from .base import (
Borders, NeighborsMap, Layout, LayoutData, LayoutDimension, Layout, LayoutData, LayoutDimension, ListOfWindows, NeighborsMap,
ListOfWindows, all_borders, layout_dimension, lgd, no_borders, layout_dimension, lgd, variable_bias
variable_bias
) )
from .tall import neighbors_for_tall_window from .tall import neighbors_for_tall_window
@ -60,9 +59,9 @@ class Grid(Layout):
def variable_layout(self, layout_func: Callable[..., LayoutDimension], num_windows: int, biased_map: Dict[int, float]) -> LayoutDimension: def variable_layout(self, layout_func: Callable[..., LayoutDimension], num_windows: int, biased_map: Dict[int, float]) -> LayoutDimension:
return layout_func(num_windows, bias=variable_bias(num_windows, biased_map) if num_windows > 1 else None) return layout_func(num_windows, bias=variable_bias(num_windows, biased_map) if num_windows > 1 else None)
def apply_bias(self, idx: int, increment: float, top_level_windows: ListOfWindows, is_horizontal: bool = True) -> bool: def apply_bias(self, idx: int, increment: float, all_windows: WindowList, is_horizontal: bool = True) -> bool:
b = self.biased_cols if is_horizontal else self.biased_rows b = self.biased_cols if is_horizontal else self.biased_rows
num_windows = len(top_level_windows) num_windows = all_windows.num_groups
ncols, nrows, special_rows, special_col = calc_grid_size(num_windows) ncols, nrows, special_rows, special_col = calc_grid_size(num_windows)
def position_for_window_idx(idx: int) -> Tuple[int, int]: def position_for_window_idx(idx: int) -> Tuple[int, int]:
@ -134,17 +133,17 @@ class Grid(Layout):
pos += rows pos += rows
on_col_done(col_windows) on_col_done(col_windows)
def do_layout(self, windows: WindowList, active_window_idx: int) -> None: def do_layout(self, all_windows: WindowList) -> None:
n = len(windows) n = all_windows.num_groups
if n == 1: if n == 1:
self.layout_single_window(windows[0]) self.layout_single_window_group(next(all_windows.iter_all_layoutable_groups()))
return return
ncols, nrows, special_rows, special_col = calc_grid_size(n) ncols, nrows, special_rows, special_col = calc_grid_size(n)
groups = tuple(all_windows.iter_all_layoutable_groups())
win_col_map = [] win_col_map: List[List[WindowGroup]] = []
def on_col_done(col_windows: List[int]) -> None: def on_col_done(col_windows: List[int]) -> None:
col_windows_w = [windows[i] for i in col_windows] col_windows_w = [groups[i] for i in col_windows]
win_col_map.append(col_windows_w) win_col_map.append(col_windows_w)
def extents(ld: LayoutData) -> Tuple[int, int]: def extents(ld: LayoutData) -> Tuple[int, int]:
@ -164,37 +163,27 @@ class Grid(Layout):
return LayoutData(start + before_dec, number_of_cells, before_dec, size - cell_area - before_dec, cell_area) return LayoutData(start + before_dec, number_of_cells, before_dec, size - cell_area - before_dec, cell_area)
def position_window_in_grid_cell(window_idx: int, xl: LayoutData, yl: LayoutData) -> None: def position_window_in_grid_cell(window_idx: int, xl: LayoutData, yl: LayoutData) -> None:
w = windows[window_idx] wg = groups[window_idx]
bw = w.effective_border()
edges = Edges( edges = Edges(
w.effective_margin('left') + w.effective_padding('left') + bw, wg.decoration('left'), wg.decoration('top'), wg.decoration('right'), wg.decoration('bottom')
w.effective_margin('right') + w.effective_padding('right') + bw,
w.effective_margin('top') + w.effective_padding('top') + bw,
w.effective_margin('bottom') + w.effective_padding('bottom') + bw,
) )
xl = layout(xl, lgd.cell_width, edges.left, edges.right) xl = layout(xl, lgd.cell_width, edges.left, edges.right)
yl = layout(yl, lgd.cell_height, edges.top, edges.bottom) yl = layout(yl, lgd.cell_height, edges.top, edges.bottom)
self.set_window_geometry(w, window_idx, xl, yl) self.set_window_group_geometry(wg, xl, yl)
for window_idx, xl, yl in self.layout_windows( for window_idx, xl, yl in self.layout_windows(
n, nrows, ncols, special_rows, special_col, on_col_done): n, nrows, ncols, special_rows, special_col, on_col_done):
position_window_in_grid_cell(window_idx, xl, yl) position_window_in_grid_cell(window_idx, xl, yl)
def minimal_borders(self, windows: WindowList, active_window: Optional[WindowType], needs_borders_map: Dict[int, bool]) -> Generator[Borders, None, None]: def window_independent_borders(self, all_windows: WindowList, active_window: Optional[WindowType] = None) -> Generator[Edges, None, None]:
for w in windows: n = all_windows.num_groups
if needs_borders_map[w.id]:
yield all_borders
else:
yield no_borders
def window_independent_borders(self, windows: WindowList, active_window: Optional[WindowType] = None) -> Generator[Edges, None, None]:
n = len(windows)
if not lgd.draw_minimal_borders or n < 2: if not lgd.draw_minimal_borders or n < 2:
return return
ncols, nrows, special_rows, special_col = calc_grid_size(n) ncols, nrows, special_rows, special_col = calc_grid_size(n)
row_borders: List[List[Edges]] = [[]] row_borders: List[List[Edges]] = [[]]
col_borders: List[Edges] = [] col_borders: List[Edges] = []
bw = windows[0].effective_border() groups = tuple(all_windows.iter_all_layoutable_groups())
bw = groups[0].effective_border()
def on_col_done(col_windows: List[int]) -> None: def on_col_done(col_windows: List[int]) -> None:
left = xl.content_pos + xl.content_size + xl.space_after - bw // 2 left = xl.content_pos + xl.content_size + xl.space_after - bw // 2
@ -213,14 +202,17 @@ class Grid(Layout):
for border in rows[:-1]: for border in rows[:-1]:
yield border yield border
def neighbors_for_window(self, window: WindowType, windows: WindowList) -> NeighborsMap: def neighbors_for_window(self, window: WindowType, all_windows: WindowList) -> NeighborsMap:
n = len(windows) n = all_windows.num_groups
if n < 4: if n < 4:
return neighbors_for_tall_window(1, window, windows) return neighbors_for_tall_window(1, window, all_windows)
wg = all_windows.group_for_window(window)
assert wg is not None
ncols, nrows, special_rows, special_col = calc_grid_size(n) ncols, nrows, special_rows, special_col = calc_grid_size(n)
blank_row: List[Optional[int]] = [None for i in range(ncols)] blank_row: List[Optional[int]] = [None for i in range(ncols)]
matrix = tuple(blank_row[:] for j in range(max(nrows, special_rows))) matrix = tuple(blank_row[:] for j in range(max(nrows, special_rows)))
wi = iter(windows) wi = all_windows.iter_all_layoutable_groups()
pos_map: Dict[int, Tuple[int, int]] = {} pos_map: Dict[int, Tuple[int, int]] = {}
col_counts: List[int] = [] col_counts: List[int] = []
for col in range(ncols): for col in range(ncols):
@ -230,7 +222,7 @@ class Grid(Layout):
matrix[row][col] = wid = w.id matrix[row][col] = wid = w.id
pos_map[wid] = row, col pos_map[wid] = row, col
col_counts.append(rows) col_counts.append(rows)
row, col = pos_map[window.id] row, col = pos_map[wg.id]
def neighbors(row: int, col: int) -> List[int]: def neighbors(row: int, col: int) -> List[int]:
try: try: