Port vertical/horz layouts to new groups API

This commit is contained in:
Kovid Goyal 2020-05-07 11:41:35 +05:30
parent 01c0e8da93
commit f801c47d1e
No known key found for this signature in database
GPG Key ID: 06BC317B515ACE7C

View File

@ -2,14 +2,14 @@
# vim:fileencoding=utf-8
# License: GPLv3 Copyright: 2020, Kovid Goyal <kovid at kovidgoyal.net>
from typing import Dict, Generator, Optional
from typing import Dict, Generator
from kitty.typing import WindowType
from kitty.window_list import WindowList
from .base import (
Borders, NeighborsMap, Layout, LayoutDimension, ListOfWindows,
all_borders, no_borders, variable_bias
Borders, Layout, LayoutDimension, NeighborsMap, all_borders, no_borders,
variable_bias
)
@ -21,61 +21,65 @@ class Vertical(Layout):
main_axis_layout = Layout.ylayout
perp_axis_layout = Layout.xlayout
def variable_layout(self, windows: ListOfWindows, biased_map: Dict[int, float]) -> LayoutDimension:
num_windows = len(windows)
def variable_layout(self, all_windows: WindowList, biased_map: Dict[int, float]) -> LayoutDimension:
num_windows = all_windows.num_groups
bias = variable_bias(num_windows, biased_map) if num_windows else None
return self.main_axis_layout(windows, bias=bias)
return self.main_axis_layout(all_windows.iter_all_layoutable_groups(), bias=bias)
def remove_all_biases(self) -> bool:
self.biased_map: Dict[int, float] = {}
return True
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:
if self.main_is_horizontal != is_horizontal:
return False
num_windows = len(top_level_windows)
num_windows = all_windows.num_groups
if num_windows < 2:
return False
before_layout = list(self.variable_layout(top_level_windows, self.biased_map))
before_layout = list(self.variable_layout(all_windows, self.biased_map))
candidate = self.biased_map.copy()
before = candidate.get(idx, 0)
candidate[idx] = before + increment
if before_layout == list(self.variable_layout(top_level_windows, candidate)):
if before_layout == list(self.variable_layout(all_windows, candidate)):
return False
self.biased_map = candidate
return True
def do_layout(self, windows: WindowList, active_window_idx: int) -> None:
window_count = len(windows)
def do_layout(self, all_windows: WindowList) -> None:
window_count = all_windows.num_groups
if window_count == 1:
self.layout_single_window(windows[0])
self.layout_single_window_group(next(all_windows.iter_all_layoutable_groups()))
return
ylayout = self.variable_layout(windows, self.biased_map)
for i, (w, yl) in enumerate(zip(windows, ylayout)):
xl = next(self.perp_axis_layout([w]))
ylayout = self.variable_layout(all_windows, self.biased_map)
for i, (wg, yl) in enumerate(zip(all_windows.iter_all_layoutable_groups(), ylayout)):
xl = next(self.perp_axis_layout(iter((wg,))))
if self.main_is_horizontal:
xl, yl = yl, xl
self.set_window_geometry(w, i, xl, yl)
self.set_window_group_geometry(wg, xl, yl)
def minimal_borders(self, windows: WindowList, active_window: Optional[WindowType], needs_borders_map: Dict[int, bool]) -> Generator[Borders, None, None]:
last_i = len(windows) - 1
for i, w in enumerate(windows):
if needs_borders_map[w.id]:
def minimal_borders(self, all_windows: WindowList, needs_borders_map: Dict[int, bool]) -> Generator[Borders, None, None]:
last_i = all_windows.num_groups - 1
groups = tuple(all_windows.iter_all_layoutable_groups())
for i, wg in enumerate(groups):
if needs_borders_map[wg.id]:
yield all_borders
continue
if i == last_i:
yield no_borders
break
if needs_borders_map[windows[i+1].id]:
if needs_borders_map[groups[i+1].id]:
yield no_borders
else:
yield self.only_between_border
def neighbors_for_window(self, window: WindowType, windows: WindowList) -> NeighborsMap:
idx = windows.index(window)
before = [] if window is windows[0] else [windows[idx-1].id]
after = [] if window is windows[-1] else [windows[idx+1].id]
def neighbors_for_window(self, window: WindowType, all_windows: WindowList) -> NeighborsMap:
wg = all_windows.group_for_window(window)
assert wg is not None
groups = tuple(all_windows.iter_all_layoutable_groups())
idx = groups.index(wg)
before = [] if wg is groups[0] else [groups[idx-1].id]
after = [] if wg is groups[-1] else [groups[idx+1].id]
if self.main_is_horizontal:
return {'left': before, 'right': after, 'top': [], 'bottom': []}
return {'top': before, 'bottom': after, 'left': [], 'right': []}