From 70ccc1cf6d822c86decf0b48e49c89d2d95a9b5f Mon Sep 17 00:00:00 2001 From: Kovid Goyal Date: Thu, 7 May 2020 12:18:12 +0530 Subject: [PATCH] Port Grid layout to new groups API --- kitty/layout/grid.py | 60 +++++++++++++++++++------------------------- 1 file changed, 26 insertions(+), 34 deletions(-) diff --git a/kitty/layout/grid.py b/kitty/layout/grid.py index 89577bcd2..12061c95d 100644 --- a/kitty/layout/grid.py +++ b/kitty/layout/grid.py @@ -8,12 +8,11 @@ from typing import Callable, Dict, Generator, List, Optional, Sequence, Tuple from kitty.constants import Edges from kitty.typing import WindowType -from kitty.window_list import WindowList +from kitty.window_list import WindowGroup, WindowList from .base import ( - Borders, NeighborsMap, Layout, LayoutData, LayoutDimension, - ListOfWindows, all_borders, layout_dimension, lgd, no_borders, - variable_bias + Layout, LayoutData, LayoutDimension, ListOfWindows, NeighborsMap, + layout_dimension, lgd, variable_bias ) 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: 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 - num_windows = len(top_level_windows) + num_windows = all_windows.num_groups ncols, nrows, special_rows, special_col = calc_grid_size(num_windows) def position_for_window_idx(idx: int) -> Tuple[int, int]: @@ -134,17 +133,17 @@ class Grid(Layout): pos += rows on_col_done(col_windows) - def do_layout(self, windows: WindowList, active_window_idx: int) -> None: - n = len(windows) + def do_layout(self, all_windows: WindowList) -> None: + n = all_windows.num_groups if n == 1: - self.layout_single_window(windows[0]) + self.layout_single_window_group(next(all_windows.iter_all_layoutable_groups())) return ncols, nrows, special_rows, special_col = calc_grid_size(n) - - win_col_map = [] + groups = tuple(all_windows.iter_all_layoutable_groups()) + win_col_map: List[List[WindowGroup]] = [] 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) 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) def position_window_in_grid_cell(window_idx: int, xl: LayoutData, yl: LayoutData) -> None: - w = windows[window_idx] - bw = w.effective_border() + wg = groups[window_idx] edges = Edges( - w.effective_margin('left') + w.effective_padding('left') + bw, - 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, + wg.decoration('left'), wg.decoration('top'), wg.decoration('right'), wg.decoration('bottom') ) xl = layout(xl, lgd.cell_width, edges.left, edges.right) 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( n, nrows, ncols, special_rows, special_col, on_col_done): 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]: - for w in windows: - 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) + def window_independent_borders(self, all_windows: WindowList, active_window: Optional[WindowType] = None) -> Generator[Edges, None, None]: + n = all_windows.num_groups if not lgd.draw_minimal_borders or n < 2: return ncols, nrows, special_rows, special_col = calc_grid_size(n) row_borders: List[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: left = xl.content_pos + xl.content_size + xl.space_after - bw // 2 @@ -213,14 +202,17 @@ class Grid(Layout): for border in rows[:-1]: yield border - def neighbors_for_window(self, window: WindowType, windows: WindowList) -> NeighborsMap: - n = len(windows) + def neighbors_for_window(self, window: WindowType, all_windows: WindowList) -> NeighborsMap: + n = all_windows.num_groups 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) blank_row: List[Optional[int]] = [None for i in range(ncols)] 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]] = {} col_counts: List[int] = [] for col in range(ncols): @@ -230,7 +222,7 @@ class Grid(Layout): matrix[row][col] = wid = w.id pos_map[wid] = row, col col_counts.append(rows) - row, col = pos_map[window.id] + row, col = pos_map[wg.id] def neighbors(row: int, col: int) -> List[int]: try: