chore: import official kitty sources from upstream

Bring the kovidgoyal/kitty codebase in as the base of FWS-Kitty,
vendored at upstream commit 91fb38a without its git history.
This commit is contained in:
BlackAngelTV committed 2026-11-04 11:20:47 +01:00
commit 7e7d249099
1134 files changed
+433629

No files matched your search

View File
Whitespace-only changes.
+860
View File
@@ -0,0 +1,860 @@
#!/usr/bin/env python
# License: GPLv3 Copyright: 2020, Kovid Goyal <kovid at kovidgoyal.net>
from collections.abc import Callable, Generator, Iterable, Iterator, Sequence
from copy import copy, deepcopy
from enum import Enum
from functools import partial
from itertools import repeat
from math import ceil, isfinite
from typing import Any, ClassVar, NamedTuple, cast
from kitty.borders import BorderColor
from kitty.fast_data_types import BOTTOM_EDGE, RIGHT_EDGE, Region, get_options, set_active_window, viewport_for_window
from kitty.options.types import Options
from kitty.types import DockEdge, DockSpec, Edges, NeighborsMap, WindowGeometry, WindowMapper, WindowResizeDragData, dock_edges, opposite_dock_edge
from kitty.typing_compat import EdgeLiteral, WindowType
from kitty.window_list import WindowGroup, WindowList, collapsed_margin
class BorderLine(NamedTuple):
edges: Edges = Edges()
color: BorderColor = BorderColor.inactive
window_id: int = 0
horizontal: bool = False
class LayoutOpts:
def __init__(self, data: dict[str, str]):
pass
def serialized(self) -> dict[str, Any]:
return {}
class LayoutData(NamedTuple):
content_pos: int = 0
cells_per_window: int = 0
space_before: int = 0
space_after: int = 0
content_size: int = 0
# The portion of space_before/space_after that is compensatory padding
# arising from the window size not being an exact multiple of the cell size
# (as opposed to the intentional margin/border/padding decoration).
compensatory_before: int = 0
compensatory_after: int = 0
DecorationPairs = Sequence[tuple[int, int]]
LayoutDimension = Generator[LayoutData, None, None]
ListOfWindows = list[WindowType]
class LayoutGlobalData:
draw_minimal_borders: bool = True
draw_active_borders: bool = True
fill_padding_with_neighboring_cell: bool = False
alignment_x: int = 0
alignment_y: int = 0
central: Region = Region((0, 0, 199, 199, 200, 200))
cell_width: int = 20
cell_height: int = 20
lgd = LayoutGlobalData()
def fractional_resize_steps(remaining: int, fraction: float) -> int:
return max(1, ceil(remaining * fraction - 1e-9)) if remaining > 0 else 0
def idx_for_id(win_id: int, windows: Iterable[WindowType]) -> int | None:
for i, w in enumerate(windows):
if w.id == win_id:
return i
return None
def effective_draw_minimal_borders(opts: Options, has_more_than_one_visible_group: bool = True) -> bool:
ans = opts.draw_minimal_borders and sum(opts.window_margin_width) == 0
if not has_more_than_one_visible_group and opts.draw_window_borders_for_single_window:
ans = False
return ans
def set_layout_options(opts: Options) -> None:
lgd.draw_minimal_borders = effective_draw_minimal_borders(opts)
lgd.draw_active_borders = opts.active_border_color is not None
lgd.fill_padding_with_neighboring_cell = opts.padding_fill_strategy == 'neighboring_cell'
lgd.alignment_x = -1 if opts.placement_strategy.endswith('left') else 1 if opts.placement_strategy.endswith('right') else 0
lgd.alignment_y = -1 if opts.placement_strategy.startswith('top') else 1 if opts.placement_strategy.startswith('bottom') else 0
def convert_bias_map(bias: dict[int, float], number_of_windows: int, number_of_cells: int) -> Sequence[float]:
cells_per_window, extra = divmod(number_of_cells, number_of_windows)
cell_map = list(repeat(cells_per_window, number_of_windows))
cell_map[-1] += extra
base_bias = [x / number_of_cells for x in cell_map]
return distribute_indexed_bias(base_bias, bias)
def calculate_cells_map(bias: None | Sequence[float] | dict[int, float], number_of_windows: int, number_of_cells: int) -> list[int]:
if isinstance(bias, dict):
b: dict[int, float] = cast(dict[int, float], bias)
bias = convert_bias_map(b, number_of_windows, number_of_cells)
cells_per_window = number_of_cells // number_of_windows
if bias is not None and number_of_windows > 1 and number_of_windows == len(bias) and cells_per_window > 5:
cells_map = [int(b * number_of_cells) for b in bias]
while min(cells_map) < 5:
maxi, mini = map(cells_map.index, (max(cells_map), min(cells_map)))
if maxi == mini:
break
cells_map[mini] += 1
cells_map[maxi] -= 1
else:
cells_map = list(repeat(cells_per_window, number_of_windows))
extra = number_of_cells - sum(cells_map)
if extra > 0:
cells_map[-1] += extra
return cells_map
def layout_dimension(
start_at: int, length: int, cell_length: int, decoration_pairs: DecorationPairs, alignment: int = 0, bias: None | Sequence[float] | dict[int, float] = None
) -> LayoutDimension:
number_of_windows = len(decoration_pairs)
number_of_cells = max(0, length // cell_length)
dec_vals: Iterable[int] = map(sum, decoration_pairs)
space_needed_for_decorations = sum(dec_vals)
extra = length - number_of_cells * cell_length
while extra < space_needed_for_decorations and number_of_cells > 0:
number_of_cells -= 1
extra = length - number_of_cells * cell_length
cells_map = calculate_cells_map(bias, number_of_windows, number_of_cells) if number_of_cells > 0 else [0] * number_of_windows
assert sum(cells_map) == number_of_cells
extra = length - number_of_cells * cell_length - space_needed_for_decorations
pos = start_at # start
if alignment > 0: # end
pos += extra
elif alignment == 0: # center
pos += extra // 2
last_i = len(cells_map) - 1
for i, cells_per_window in enumerate(cells_map):
before_dec, after_dec = decoration_pairs[i]
pos += before_dec
if i == 0:
before_space = pos - start_at
else:
before_space = before_dec
content_size = cells_per_window * cell_length
if i == last_i:
after_space = (start_at + length) - (pos + content_size)
else:
after_space = after_dec
# Whatever space is present beyond the intentional decoration is the
# compensatory padding from the size mismatch (only ever non-zero for
# the first/last windows, which absorb the leading/trailing remainder).
yield LayoutData(pos, cells_per_window, before_space, after_space, content_size, before_space - before_dec, after_space - after_dec)
pos += content_size + after_space
class Rect(NamedTuple):
left: int
top: int
right: int
bottom: int
def blank_rects_for_window(wg: WindowGeometry) -> Generator[Rect, None, None]:
left, top, right, bottom = wg.left, wg.top, wg.right, wg.bottom
left_width, top_height, right_width, bottom_height = wg.spaces
if lgd.fill_padding_with_neighboring_cell:
# The compensatory padding is the innermost slice of the spaces, adjacent
# to the cells. It is drawn separately by the padding shader so that it
# matches its neighboring cell. Exclude it here by expanding the content
# box over it and only painting the remaining outer decoration frame in
# the background color.
c = wg.compensatory
left -= c.left
top -= c.top
right += c.right
bottom += c.bottom
left_width -= c.left
top_height -= c.top
right_width -= c.right
bottom_height -= c.bottom
if left_width > 0:
yield Rect(left - left_width, top - top_height, left, bottom + bottom_height)
if top_height > 0:
yield Rect(left, top - top_height, right + right_width, top)
if right_width > 0:
yield Rect(right, top, right + right_width, bottom + bottom_height)
if bottom_height > 0:
yield Rect(left, bottom, right, bottom + bottom_height)
def window_geometry(
xstart: int,
xnum: int,
ystart: int,
ynum: int,
left: int,
top: int,
right: int,
bottom: int,
compensatory: Edges = Edges(),
) -> WindowGeometry:
return WindowGeometry(
left=xstart,
top=ystart,
xnum=max(0, xnum),
ynum=max(0, ynum),
right=xstart + lgd.cell_width * xnum,
bottom=ystart + lgd.cell_height * ynum,
spaces=Edges(left, top, right, bottom),
compensatory=compensatory,
)
def window_geometry_from_layouts(x: LayoutData, y: LayoutData) -> WindowGeometry:
return window_geometry(
x.content_pos,
x.cells_per_window,
y.content_pos,
y.cells_per_window,
x.space_before,
y.space_before,
x.space_after,
y.space_after,
Edges(x.compensatory_before, y.compensatory_before, x.compensatory_after, y.compensatory_after),
)
def layout_single_window(
xdecoration_pairs: DecorationPairs,
ydecoration_pairs: DecorationPairs,
xalignment: int = 0,
yalignment: int = 0,
) -> WindowGeometry:
x = next(layout_dimension(lgd.central.left, lgd.central.width, lgd.cell_width, xdecoration_pairs, alignment=xalignment))
y = next(layout_dimension(lgd.central.top, lgd.central.height, lgd.cell_height, ydecoration_pairs, alignment=yalignment))
return window_geometry_from_layouts(x, y)
def region(left: int, top: int, width: int, height: int) -> Region:
width, height = max(0, width), max(0, height)
return Region((left, top, left + width - 1, top + height - 1, width, height))
def outer_region_of(geom: WindowGeometry) -> Region:
"The region occupied by a window including its decorations"
s = geom.spaces
return region(geom.left - s.left, geom.top - s.top, geom.right - geom.left + s.left + s.right, geom.bottom - geom.top + s.top + s.bottom)
def carve_docks(area: Region, docks: Iterable[WindowGroup], cell_width: int, cell_height: int) -> tuple[Region, list[tuple[WindowGroup, Region]]]:
"""
Give each docked group a slice of area at its edge, in order, so earlier
docks are further out and span the full extent of whatever area remains when
they are placed. Returns the area left over for the content and the regions of
the docks. When space runs out, later docks are shrunk first.
"""
left, top, right, bottom = area.left, area.top, area.left + area.width, area.top + area.height
placements: list[tuple[WindowGroup, Region]] = []
for g in docks:
d = g.dock
if d is None:
continue
if d.is_horizontal:
total, available, cell, dec = area.height, bottom - top, cell_height, g.decoration('top') + g.decoration('bottom')
else:
total, available, cell, dec = area.width, right - left, cell_width, g.decoration('left') + g.decoration('right')
wanted = int(total * d.size / 100) if d.size_is_percent else int(d.size) * cell + dec
size = max(0, min(wanted, available))
match d.edge:
case 'top':
placements.append((g, region(left, top, right - left, size)))
top += size
case 'bottom':
bottom -= size
placements.append((g, region(left, bottom, right - left, size)))
case 'left':
placements.append((g, region(left, top, size, bottom - top)))
left += size
case 'right':
right -= size
placements.append((g, region(right, top, size, bottom - top)))
return region(left, top, right - left, bottom - top), placements
def safe_increment_bias(old_val: float, increment: float = 0) -> float:
return max(0.1, min(old_val + increment, 0.9))
def normalize_biases(biases: list[float]) -> list[float]:
s = sum(biases)
if s == 1.0:
return biases
return [x / s for x in biases]
def distribute_indexed_bias(base_bias: Sequence[float], index_bias_map: dict[int, float]) -> Sequence[float]:
if not index_bias_map:
return base_bias
ans = list(base_bias)
limit = len(ans)
for row, increment in index_bias_map.items():
if row >= limit or not increment:
continue
other_increment = -increment / (limit - 1)
ans = [safe_increment_bias(b, increment if i == row else other_increment) for i, b in enumerate(ans)]
return normalize_biases(ans)
def is_focusable_dock(g: WindowGroup) -> bool:
return g.dock is not None and g.dock.focusable and g.is_visible_in_layout
def innermost_focusable_dock(docks: Sequence[WindowGroup], edge: DockEdge) -> WindowGroup | None:
for d in reversed(docks):
if d.dock is not None and d.dock.edge == edge and is_focusable_dock(d):
return d
return None
def set_neighbors(ans: NeighborsMap, edge: DockEdge, val: list[int]) -> None:
# NeighborsMap is a TypedDict so it cannot be indexed by a variable
match edge:
case 'left':
ans['left'] = val
case 'top':
ans['top'] = val
case 'right':
ans['right'] = val
case 'bottom':
ans['bottom'] = val
def create_window_id_map_for_unserialize(all_windows: WindowList) -> dict[int, int]:
window_id_map = {}
for w in all_windows:
if w.serialized_id:
window_id_map[w.serialized_id] = w.id
return window_id_map
class DragOverlayMode(Enum):
"Controls drag-and-drop overlay display and valid direction axis for body drops"
full = 'full' # full-window overlay, positional swap (Stack and any unrecognised layout)
axis_x = 'axis_x' # top/bottom halves only (Vertical, Tall, Grid)
axis_y = 'axis_y' # left/right halves only (Horizontal, Fat)
free = 'free' # 4-way free direction (Splits; handled by its own insert_window_next_to override)
class Layout:
name: str = ''
needs_window_borders = True
must_draw_borders = False # can be overridden to customize behavior from kittens
layout_opts = LayoutOpts({})
only_active_window_visible = False
drag_overlay_mode: ClassVar[DragOverlayMode] = DragOverlayMode.full
def __init__(self, os_window_id: int, tab_id: int, layout_opts: str = '') -> None:
self.set_owner(os_window_id, tab_id)
# A set of rectangles corresponding to the blank spaces at the edges of
# this layout, i.e. spaces that are not covered by any window
self.blank_rects: list[Rect] = []
self.tab_dock_placements: list[tuple[WindowGroup, Region]] = []
# The blank strips at the edges of the layout area when margins are collapsed
self.collapsed_margin_rects: list[Rect] = []
self.layout_opts = self.parse_layout_opts(layout_opts)
assert self.name is not None
self.full_name = f'{self.name}:{layout_opts}' if layout_opts else self.name
self.remove_all_biases()
def set_owner(self, os_window_id: int, tab_id: int) -> None:
# Useful when moving a layout from one tab to another typically a detached tab being re-attached
self.os_window_id = os_window_id
self.tab_id = tab_id
self.set_active_window_in_os_window = partial(set_active_window, os_window_id, tab_id)
def bias_increment_for_cell(self, all_windows: WindowList, is_horizontal: bool) -> float:
self._set_dimensions(all_windows)
return self.calculate_bias_increment_for_a_single_cell(all_windows, is_horizontal)
def calculate_bias_increment_for_a_single_cell(self, all_windows: WindowList, is_horizontal: bool) -> float:
if is_horizontal:
return (lgd.cell_width + 1) / lgd.central.width
return (lgd.cell_height + 1) / lgd.central.height
def apply_bias(self, window_id: int, increment: float, all_windows: WindowList, is_horizontal: bool = True) -> bool:
return False
def remove_all_biases(self) -> bool:
return False
def modify_size_of_window(self, all_windows: WindowList, window_id: int, increment: float, is_horizontal: bool = True) -> bool:
idx = all_windows.group_idx_for_window(window_id)
if idx is None or not increment:
return False
return self.apply_bias(idx, increment, all_windows, is_horizontal)
def apply_fractional_bias(self, window_idx: int, increment: float, all_windows: WindowList, is_horizontal: bool) -> bool:
return self.apply_bias(window_idx, increment, all_windows, is_horizontal)
def modify_size_of_window_by_fraction(self, all_windows: WindowList, window_id: int, increment: float, is_horizontal: bool, fraction: float) -> bool:
if window_id not in all_windows.id_map:
return False
idx = all_windows.group_idx_for_window(window_id)
if idx is None or all_windows.num_groups < 2 or not increment or not isfinite(increment) or not 0 < fraction <= 1:
return False
# Probe private layout state, without changing windows or rendering every
# intermediate step. This follows each layout's ordinary resize rules.
probe = copy(self)
state = deepcopy(self.layout_state())
state['opts'] = self.layout_opts.serialized()
if not probe.set_layout_state(state, lambda group_id: group_id):
return False
# The state after each step, so that the chosen number of steps can be
# adopted without repeating them.
states: list[dict[str, Any]] = []
# Biases span at most one unit; allow two for clamping/normalization.
# The bound also prevents future layout implementations from looping.
for _ in range(max(1, ceil(2 / abs(increment)))):
if not probe.apply_fractional_bias(idx, increment, all_windows, is_horizontal):
break
states.append(deepcopy(probe.layout_state()))
steps = fractional_resize_steps(len(states), fraction)
if not steps:
return False
state = states[steps - 1]
state['opts'] = self.layout_opts.serialized()
return self.set_layout_state(state, lambda group_id: group_id)
def drag_resize_window(self, all_windows: WindowList, window_id: int, increment: float, is_horizontal: bool = True) -> float:
"""Resize by a number of cells, returning the number of cells actually applied.
Layouts that can apply an increment only partially should override this and
report what they really did, so that the caller can keep the dragged divider
locked to the pointer. This generic implementation has no way to tell how much
apply_bias() clamped, so it reports all or nothing.
"""
increment_as_percent = self.bias_increment_for_cell(all_windows, is_horizontal) * increment
return increment if self.modify_size_of_window(all_windows, window_id, increment_as_percent, is_horizontal) else 0.0
def parse_layout_opts(self, layout_opts: str | None = None) -> LayoutOpts:
data: dict[str, str] = {}
if layout_opts:
for x in layout_opts.split(';'):
k, v = x.partition('=')[::2]
if k and v:
data[k] = v
return type(self.layout_opts)(data)
def nth_window(self, all_windows: WindowList, num: int) -> WindowType | None:
return all_windows.active_window_in_nth_group(num, clamp=True)
def activate_nth_window(self, all_windows: WindowList, num: int) -> None:
all_windows.set_active_group_idx(num)
def next_window(self, all_windows: WindowList, delta: int = 1) -> None:
all_windows.activate_next_window_group(delta)
def neighbors(self, all_windows: WindowList) -> NeighborsMap:
w = all_windows.active_window
assert w is not None
return self.neighbors_for_any_window(w, all_windows)
def neighbors_for_any_window(self, window: WindowType, all_windows: WindowList) -> NeighborsMap:
"""
Like neighbors_for_window() but also works for docked windows and takes
docks into account. A docked group is a neighbor of the group it is
docked to and of the docks next to it at the same edge.
"""
group = all_windows.group_for_window(window)
if group is None:
return {}
if group.dock is None:
return self._neighbors_with_docks(window, group, all_windows)
owner = all_windows.dock_owner(group)
siblings = [d for d in (all_windows.tab_docks if owner is None else owner.docks) if d.dock is not None and d.dock.edge == group.dock.edge]
idx = siblings.index(group)
outer = [d for d in siblings[:idx] if is_focusable_dock(d)]
inner = [d for d in siblings[idx + 1 :] if is_focusable_dock(d)]
ans: NeighborsMap = {}
if owner is not None and (w := all_windows.id_map.get(owner.active_window_id)) is not None:
# The rest of the neighbors are those of the owner
ans = self._neighbors_with_docks(w, owner, all_windows, skip_own_docks=True)
inwards: list[int] = []
if inner:
inwards = [inner[0].id]
elif owner is not None:
inwards = [owner.id]
elif (mg := all_windows.active_main_group) is not None:
inwards = [mg.id]
set_neighbors(ans, opposite_dock_edge[group.dock.edge], inwards)
if outer:
set_neighbors(ans, group.dock.edge, [outer[-1].id])
return ans
def _neighbors_with_docks(self, window: WindowType, group: WindowGroup, all_windows: WindowList, skip_own_docks: bool = False) -> NeighborsMap:
ans: NeighborsMap = {}
ans.update(self.neighbors_for_window(window, all_windows))
for edge in dock_edges:
# The innermost dock at an edge is next to the group, so it takes
# precedence. The tab docks are next to whichever groups have no
# neighbors in that direction.
if not skip_own_docks and (d := innermost_focusable_dock(group.docks, edge)) is not None:
set_neighbors(ans, edge, [d.id])
elif not ans.get(edge) and (d := innermost_focusable_dock(all_windows.tab_docks, edge)) is not None:
set_neighbors(ans, edge, [d.id])
return ans
def move_window(self, all_windows: WindowList, delta: int = 1) -> bool:
if all_windows.num_groups < 2 or not delta or all_windows.active_dock is not None:
return False
return all_windows.move_window_group(by=delta)
def move_window_to_group(self, all_windows: WindowList, group: int) -> bool:
return all_windows.move_window_group(to_group=group)
def insert_window_next_to(
self,
all_windows: WindowList,
window: WindowType,
next_to: WindowType,
horizontal: bool,
after: bool,
) -> None:
"""
Reposition window as a linear neighbour of next_to.
For axis_x/axis_y layouts this performs a positional insert that preserves
the order of all other groups. For 'full' layouts it falls back to a swap.
The Splits layout overrides this with tree-based logic.
"""
src_wg = all_windows.group_for_window(window)
dest_wg = all_windows.group_for_window(next_to)
if src_wg is None or dest_wg is None or src_wg.id == dest_wg.id or src_wg.dock is not None or dest_wg.dock is not None:
return
all_windows.set_active_window_group_for(window)
if self.drag_overlay_mode in (DragOverlayMode.axis_x, DragOverlayMode.axis_y):
all_windows.insert_window_group_next_to(dest_wg.id, after)
else:
# 'full' fallback: swap (preserves existing behaviour for Stack etc.)
self.move_window_to_group(all_windows, dest_wg.id)
def add_window(
self,
all_windows: WindowList,
window: WindowType,
location: str | None = None,
overlay_for: int | None = None,
put_overlay_behind: bool = False,
bias: float | None = None,
next_to: WindowType | None = None,
dock: DockSpec | None = None,
dock_owner: WindowGroup | None = None,
) -> WindowType | None:
if dock is not None:
all_windows.add_window(window, dock=dock, dock_owner=dock_owner)
return None
if overlay_for is not None:
underlay = all_windows.id_map.get(overlay_for)
if underlay is not None:
window.margin, window.padding = underlay.margin.copy(), underlay.padding.copy()
all_windows.add_window(window, group_of=overlay_for, head_of_group=put_overlay_behind)
return underlay
if location == 'neighbor':
location = 'after'
self.add_non_overlay_window(all_windows, window, location, bias, next_to)
return None
def add_non_overlay_window(
self, all_windows: WindowList, window: WindowType, location: str | None, bias: float | None = None, next_to: WindowType | None = None
) -> None:
before = False
next_to = next_to or all_windows.active_window
if next_to is not None:
# New windows are never placed next to docked windows
next_to = all_windows.main_window_for(next_to)
if location is not None:
if location in ('after', 'vsplit', 'hsplit'):
pass
elif location == 'before':
before = True
elif location == 'first':
before = True
next_to = None
elif location == 'last':
next_to = None
all_windows.add_window(window, next_to=next_to, before=before)
if bias is not None:
idx = all_windows.group_idx_for_window(window)
if idx is not None:
self._set_dimensions(all_windows)
self._bias_slot(all_windows, idx, bias)
def _bias_slot(self, all_windows: WindowList, idx: int, bias: float) -> bool:
fractional_bias = max(10, min(abs(bias), 90)) / 100
h, v = self.calculate_bias_increment_for_a_single_cell(all_windows, True), self.calculate_bias_increment_for_a_single_cell(all_windows, False)
nh, nv = lgd.central.width / lgd.cell_width, lgd.central.height / lgd.cell_height
f = max(-90, min(bias, 90)) / 100.0
return self.bias_slot(all_windows, idx, fractional_bias, h * nh * f, v * nv * f)
def bias_slot(self, all_windows: WindowList, idx: int, fractional_bias: float, cell_increment_bias_h: float, cell_increment_bias_v: float) -> bool:
return False
def update_visibility(self, all_windows: WindowList) -> None:
active_window = all_windows.active_main_window
for window, is_group_leader in all_windows.iter_windows_with_visibility():
is_visible = window is active_window or (is_group_leader and not self.only_active_window_visible)
window.set_visible_in_layout(is_visible)
# Docked groups are visible when their owner is, tab docks are always visible
for dock in all_windows.tab_docks:
for window in dock:
window.set_visible_in_layout(window.id == dock.active_window_id)
for group in all_windows.groups:
owner_is_visible = group.is_visible_in_layout
for dock in group.docks:
for window in dock:
window.set_visible_in_layout(owner_is_visible and window.id == dock.active_window_id)
for dock in all_windows.orphaned_docks:
for window in dock:
window.set_visible_in_layout(False)
def _set_dimensions(self, all_windows: WindowList) -> None:
full_area, tab_bar, vw, vh, lgd.cell_width, lgd.cell_height = viewport_for_window(self.os_window_id)
opts = get_options()
self.collapsed_margin_rects = []
if opts.collapse_window_margins and (w := next(iter(all_windows), None)) is not None:
# Windows use only part of their margins, see collapsed_margin(),
# so supply the rest at the edges of the layout area
def inset(edge: EdgeLiteral) -> int:
m = w.default_margin(edge)
return m - collapsed_margin(m, edge)
ml, mt, mr, mb = inset('left'), inset('top'), inset('right'), inset('bottom')
left, top, right, bottom = full_area.left, full_area.top, full_area.left + full_area.width, full_area.top + full_area.height
full_area = region(left + ml, top + mt, full_area.width - ml - mr, full_area.height - mt - mb)
il, it = full_area.left, full_area.top
ir, ib = il + full_area.width, it + full_area.height
self.collapsed_margin_rects = [
r
for r in (Rect(left, top, right, it), Rect(left, ib, right, bottom), Rect(left, it, il, ib), Rect(ir, it, right, ib))
if r.right > r.left and r.bottom > r.top
]
# The tab docks reduce the area available to the layout
lgd.central, self.tab_dock_placements = carve_docks(full_area, all_windows.tab_docks, lgd.cell_width, lgd.cell_height)
# Update lgd.draw_minimal_borders based on the current number of visible windows
# and the draw_window_borders_for_single_window option
lgd.draw_minimal_borders = effective_draw_minimal_borders(opts, all_windows.has_more_than_one_visible_group)
def __call__(self, all_windows: WindowList) -> None:
self._set_dimensions(all_windows)
self.update_visibility(all_windows)
# Set show_title_bar flag on each visible window before layout
min_windows = get_options().window_title_bar_min_windows
visible_groups = tuple(all_windows.iter_all_layoutable_groups(only_visible=True))
force_show = all_windows.force_show_title_bars
show_title_bar = force_show or (min_windows > 0 and len(visible_groups) >= min_windows)
for wg in visible_groups:
for w in wg.windows:
w.show_title_bar = show_title_bar
for wg in all_windows.iter_dock_groups():
for w in wg.windows:
w.show_title_bar = False
if all_windows.groups:
self.do_layout(all_windows)
self.layout_docks(all_windows)
# Computed from the final geometry of the windows, as it can differ
# from what the layout gave them, see WindowGroup.set_content_geometry()
self.blank_rects = list(self.collapsed_margin_rects)
for group in all_windows.iter_visible_groups():
if (geom := group.geometry) is not None:
self.blank_rects.extend(blank_rects_for_window(geom))
def layout_docks(self, all_windows: WindowList) -> None:
for dock, area in self.tab_dock_placements:
self.layout_docked_group(dock, area)
for group in all_windows.groups:
if group.docks and (geom := group.allocated_geometry) is not None:
self.dock_into_group(group, geom)
def dock_into_group(self, owner: WindowGroup, geom: WindowGeometry) -> None:
"""
Make room for the docks of owner within the area the layout gave it. The
decorations the layout chose for owner are preserved.
"""
content, placements = carve_docks(outer_region_of(geom), owner.docks, lgd.cell_width, lgd.cell_height)
s, c = geom.spaces, geom.compensatory
x = next(layout_dimension(content.left, content.width, lgd.cell_width, ((s.left - c.left, s.right - c.right),), alignment=lgd.alignment_x))
y = next(layout_dimension(content.top, content.height, lgd.cell_height, ((s.top - c.top, s.bottom - c.bottom),), alignment=lgd.alignment_y))
owner.set_content_geometry(window_geometry_from_layouts(x, y))
for dock, area in placements:
self.layout_docked_group(dock, area)
def layout_docked_group(self, dock: WindowGroup, area: Region) -> None:
xdecoration_pairs = ((dock.decoration('left'), dock.decoration('right')),)
ydecoration_pairs = ((dock.decoration('top'), dock.decoration('bottom')),)
x = next(layout_dimension(area.left, area.width, lgd.cell_width, xdecoration_pairs, alignment=lgd.alignment_x))
y = next(layout_dimension(area.top, area.height, lgd.cell_height, ydecoration_pairs, alignment=lgd.alignment_y))
dock.set_geometry(window_geometry_from_layouts(x, y))
def layout_single_window_group(self, wg: WindowGroup) -> None:
bw = 1 if self.must_draw_borders else 0
xdecoration_pairs = (
(
wg.decoration('left', border_mult=bw, is_single_window=True),
wg.decoration('right', border_mult=bw, is_single_window=True),
),
)
ydecoration_pairs = (
(
wg.decoration('top', border_mult=bw, is_single_window=True),
wg.decoration('bottom', border_mult=bw, is_single_window=True),
),
)
wg.set_geometry(layout_single_window(xdecoration_pairs, ydecoration_pairs, xalignment=lgd.alignment_x, yalignment=lgd.alignment_y))
def xlayout(
self,
groups: Iterator[WindowGroup],
bias: None | Sequence[float] | dict[int, float] = None,
start: int | None = None,
size: int | None = None,
offset: int = 0,
border_mult: int = 1,
) -> LayoutDimension:
decoration_pairs = tuple(
(g.decoration('left', border_mult=border_mult), g.decoration('right', border_mult=border_mult)) for i, g in enumerate(groups) if i >= offset
)
if start is None:
start = lgd.central.left
if size is None:
size = lgd.central.width
return layout_dimension(start, size, lgd.cell_width, decoration_pairs, bias=bias, alignment=lgd.alignment_x)
def ylayout(
self,
groups: Iterator[WindowGroup],
bias: None | Sequence[float] | dict[int, float] = None,
start: int | None = None,
size: int | None = None,
offset: int = 0,
border_mult: int = 1,
) -> LayoutDimension:
decoration_pairs = tuple(
(g.decoration('top', border_mult=border_mult), g.decoration('bottom', border_mult=border_mult)) for i, g in enumerate(groups) if i >= offset
)
if start is None:
start = lgd.central.top
if size is None:
size = lgd.central.height
return layout_dimension(start, size, lgd.cell_height, decoration_pairs, bias=bias, alignment=lgd.alignment_y)
def set_window_group_geometry(self, wg: WindowGroup, xl: LayoutData, yl: LayoutData) -> WindowGeometry:
geom = window_geometry_from_layouts(xl, yl)
wg.set_geometry(geom)
return geom
def do_layout(self, windows: WindowList) -> None:
raise NotImplementedError()
def neighbors_for_window(self, window: WindowType, windows: WindowList) -> NeighborsMap:
return {}
def compute_needs_borders_map(self, all_windows: WindowList) -> dict[int, bool]:
return all_windows.compute_needs_borders_map(lgd.draw_active_borders)
def get_minimal_borders(self, windows: WindowList) -> Iterator[BorderLine]:
self._set_dimensions(windows)
yield from self.minimal_borders(windows)
if lgd.draw_minimal_borders:
yield from self.dock_borders(windows)
def dock_borders(self, windows: WindowList) -> Iterator[BorderLine]:
"Borders on the inner edges of docked groups, separating them from the content they are docked to"
needs_borders_map = windows.compute_needs_borders_map(lgd.draw_active_borders)
active_group = windows.active_group
for g in windows.iter_dock_groups(only_visible=True):
if g.dock is None or (geom := g.geometry) is None or not (bw := g.effective_border()):
continue
outer = outer_region_of(geom)
left, top, right, bottom = outer.left, outer.top, outer.left + outer.width, outer.top + outer.height
edge = opposite_dock_edge[g.dock.edge]
margin = max(0, g.decoration(edge) - bw - g.effective_padding(edge))
wid = g.active_window_id
# A positive window id means the border is on the right or bottom of the window
match edge:
case 'bottom':
edges, horizontal = Edges(left, bottom - margin - bw, right, bottom - margin), True
case 'top':
edges, horizontal, wid = Edges(left, top + margin, right, top + margin + bw), True, -wid
case 'right':
edges, horizontal = Edges(right - margin - bw, top, right - margin, bottom), False
case _:
edges, horizontal, wid = Edges(left + margin, top, left + margin + bw, bottom), False, -wid
color = BorderColor.inactive
if needs_borders_map.get(g.id):
color = BorderColor.active if g is active_group else BorderColor.bell
yield BorderLine(edges, color, wid, horizontal)
def minimal_borders(self, windows: WindowList) -> Iterator[BorderLine]:
yield from ()
def layout_action(self, action_name: str, args: Sequence[str], all_windows: WindowList) -> bool | None:
pass
def on_window_removed(self, all_windows: WindowList) -> bool:
return False
def layout_state(self) -> dict[str, Any]:
return {}
def set_layout_state(self, layout_state: dict[str, Any], map_group_id: WindowMapper) -> bool:
return True
def drag_resize_target_windows(
self,
click_window: WindowType,
x: float,
y: float,
edges: int,
all_windows: WindowList,
) -> WindowResizeDragData:
return WindowResizeDragData(click_window.id, bool(edges & RIGHT_EDGE), click_window.id, bool(edges & BOTTOM_EDGE))
def serialize(self, all_windows: WindowList) -> dict[str, Any]:
ans = self.layout_state()
ans['opts'] = self.layout_opts.serialized()
ans['class'] = self.__class__.__name__
ans['all_windows'] = all_windows.serialize_layout_state()
return ans
def unserialize(
self,
s: dict[str, Any],
all_windows: WindowList,
window_id_mapper: Callable[[WindowList], dict[int, int]] = create_window_id_map_for_unserialize,
apply_to_window_list: bool = True,
) -> bool:
"""
apply_to_window_list=False computes the group id mapping without changing the
window list. Needed when restoring the state of more than one layout, since
only the layout being made current should order the windows.
"""
if s.get('class') != self.__class__.__name__:
return False
if 'all_windows' not in s:
return False
window_id_map = window_id_mapper(all_windows)
m = all_windows.unserialize_layout_state(s['all_windows'], window_id_map, apply=apply_to_window_list)
if m is None:
return False
return self.set_layout_state(s, m.get)
+333
View File
@@ -0,0 +1,333 @@
#!/usr/bin/env python
# License: GPLv3 Copyright: 2020, Kovid Goyal <kovid at kovidgoyal.net>
from collections.abc import Callable, Generator, Iterator, Sequence
from functools import lru_cache
from itertools import repeat
from math import ceil, floor
from typing import Any
from kitty.borders import BorderColor
from kitty.types import Edges, NeighborsMap, WindowMapper
from kitty.typing_compat import WindowType
from kitty.window_list import WindowGroup, WindowList
from .base import BorderLine, DragOverlayMode, Layout, LayoutData, LayoutDimension, ListOfWindows, layout_dimension, lgd
from .tall import neighbors_for_tall_window
@lru_cache
def calc_grid_size(n: int) -> tuple[int, int, int, int]:
if n <= 5:
ncols = 1 if n == 1 else 2
else:
for ncols in range(3, (n // 2) + 1):
if ncols * ncols >= n:
break
nrows = n // ncols
special_rows = n - (nrows * (ncols - 1))
special_col = 0 if special_rows < nrows else ncols - 1
return ncols, nrows, special_rows, special_col
class Grid(Layout):
name: str = 'grid'
no_minimal_window_borders = True
drag_overlay_mode = DragOverlayMode.axis_y
def remove_all_biases(self) -> bool:
self.biased_rows: dict[int, float] = {}
self.biased_cols: dict[int, float] = {}
return True
def column_layout(
self,
num: int,
bias: Sequence[float] | None = None,
) -> LayoutDimension:
decoration_pairs = tuple(repeat((0, 0), num))
return layout_dimension(lgd.central.left, lgd.central.width, lgd.cell_width, decoration_pairs, bias=bias, alignment=lgd.alignment_x)
def row_layout(
self,
num: int,
bias: Sequence[float] | None = None,
) -> LayoutDimension:
decoration_pairs = tuple(repeat((0, 0), num))
return layout_dimension(lgd.central.top, lgd.central.height, lgd.cell_height, decoration_pairs, bias=bias, alignment=lgd.alignment_y)
def variable_layout(self, layout_func: Callable[..., LayoutDimension], num_windows: int, biased_map: dict[int, float]) -> LayoutDimension:
return layout_func(num_windows, bias=biased_map if num_windows > 1 else None)
def position_for_window_idx(self, idx: int, num_windows: int, ncols: int, nrows: int, special_rows: int, special_col: int) -> tuple[int, int]:
row_num = col_num = 0
def on_col_done(col_windows: list[int]) -> None:
nonlocal col_num, row_num
row_num = 0
col_num += 1
for window_idx, xl, yl in self.layout_windows(num_windows, nrows, ncols, special_rows, special_col, on_col_done):
if idx == window_idx:
return row_num, col_num
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, window_id: 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(window_id, num_windows, ncols, nrows, special_rows, special_col)
if is_horizontal:
b = self.biased_cols
if ncols < 2:
return False
bias_idx = col_num
attr = 'biased_cols'
def layout_func(windows: ListOfWindows, bias: Sequence[float] | None = None) -> LayoutDimension:
return self.column_layout(num_windows, bias=bias)
else:
b = self.biased_rows
if max(nrows, special_rows) < 2:
return False
bias_idx = row_num
attr = 'biased_rows'
def layout_func(windows: ListOfWindows, bias: Sequence[float] | None = None) -> LayoutDimension:
return self.row_layout(num_windows, bias=bias)
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 == tuple(self.variable_layout(layout_func, num_windows, candidate)):
return False
setattr(self, attr, candidate)
return True
def apply_fractional_bias(self, window_idx: int, increment: float, all_windows: WindowList, is_horizontal: bool) -> bool:
num = all_windows.num_groups
ncols, nrows, special_rows, special_col = calc_grid_size(num)
def geometry() -> tuple[tuple[LayoutData, LayoutData], ...]:
return tuple((x, y) for _, x, y in self.layout_windows(num, nrows, ncols, special_rows, special_col))
before = geometry()
before_rows, before_cols = self.biased_rows, self.biased_cols
if not self.apply_bias(window_idx, increment, all_windows, is_horizontal):
return False
if geometry() == before:
self.biased_rows, self.biased_cols = before_rows, before_cols
return False
return True
def layout_windows(
self, num_windows: int, nrows: int, ncols: int, special_rows: int, special_col: int, on_col_done: Callable[[list[int]], None] = lambda col_windows: None
) -> Generator[tuple[int, LayoutData, LayoutData], None, None]:
# Distribute windows top-to-bottom, left-to-right (i.e. in columns)
xlayout = self.variable_layout(self.column_layout, ncols, self.biased_cols)
yvals_normal = tuple(self.variable_layout(self.row_layout, nrows, self.biased_rows))
yvals_special = yvals_normal if special_rows == nrows else tuple(self.variable_layout(self.row_layout, special_rows, self.biased_rows))
pos = 0
for col in range(ncols):
rows = special_rows if col == special_col else nrows
yls = yvals_special if col == special_col else yvals_normal
xl = next(xlayout)
col_windows = []
for i, yl in enumerate(yls):
window_idx = pos + i
yield window_idx, xl, yl
col_windows.append(window_idx)
pos += rows
on_col_done(col_windows)
def do_layout(self, windows: WindowList) -> None:
n = windows.num_groups
if n == 1:
self.layout_single_window_group(next(windows.iter_all_layoutable_groups()))
return
ncols, nrows, special_rows, special_col = calc_grid_size(n)
groups = tuple(windows.iter_all_layoutable_groups())
win_col_map: list[list[WindowGroup]] = []
def on_col_done(col_windows: list[int]) -> None:
col_windows_w = [groups[i] for i in col_windows]
win_col_map.append(col_windows_w)
def extents(ld: LayoutData) -> tuple[int, int]:
start = ld.content_pos - ld.space_before
size = ld.space_before + ld.space_after + ld.content_size
return start, size
def layout(ld: LayoutData, cell_length: int, before_dec: int, after_dec: int, alignment: int) -> LayoutData:
start, size = extents(ld)
space_needed_for_decorations = before_dec + after_dec
content_size = size - space_needed_for_decorations
number_of_cells = content_size // cell_length
cell_area = number_of_cells * cell_length
extra = content_size - cell_area
compensatory_before = 0
if alignment == 0: # center
compensatory_before = extra // 2
elif alignment > 0: # end
compensatory_before = extra
space_before = before_dec + compensatory_before
space_after = size - cell_area - space_before
return LayoutData(start + space_before, number_of_cells, space_before, space_after, cell_area, compensatory_before, space_after - after_dec)
def position_window_in_grid_cell(window_idx: int, xl: LayoutData, yl: LayoutData) -> None:
wg = groups[window_idx]
edges = Edges(wg.decoration('left'), wg.decoration('top'), wg.decoration('right'), wg.decoration('bottom'))
xl = layout(xl, lgd.cell_width, edges.left, edges.right, lgd.alignment_x)
yl = layout(yl, lgd.cell_height, edges.top, edges.bottom, lgd.alignment_y)
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) -> Iterator[BorderLine]:
n = windows.num_groups
if not lgd.draw_minimal_borders or n < 2:
return
needs_borders_map = windows.compute_needs_borders_map(lgd.draw_active_borders)
ncols, nrows, special_rows, special_col = calc_grid_size(n)
is_first_row: set[int] = set()
is_last_row: set[int] = set()
is_first_column: set[int] = set()
is_last_column: set[int] = set()
groups = tuple(windows.iter_all_layoutable_groups())
bw = groups[0].effective_border()
if not bw:
return
xl: LayoutData = LayoutData()
yl: LayoutData = LayoutData()
prev_col_windows: list[int] = []
layout_data_map: dict[int, tuple[LayoutData, LayoutData]] = {}
def on_col_done(col_windows: list[int]) -> None:
nonlocal prev_col_windows, is_first_column
if col_windows:
is_first_row.add(groups[col_windows[0]].id)
is_last_row.add(groups[col_windows[-1]].id)
if not prev_col_windows:
is_first_column = {groups[x].id for x in col_windows}
prev_col_windows = col_windows
all_groups_in_order: list[WindowGroup] = []
for window_idx, xl, yl in self.layout_windows(n, nrows, ncols, special_rows, special_col, on_col_done):
wg = groups[window_idx]
all_groups_in_order.append(wg)
layout_data_map[wg.id] = xl, yl
is_last_column = {groups[x].id for x in prev_col_windows}
active_group = windows.active_group
def ends(yl: LayoutData) -> tuple[int, int]:
return yl.content_pos - yl.space_before, yl.content_pos + yl.content_size + yl.space_after
def borders_for_window(gid: int, color: BorderColor, wid: int) -> Iterator[BorderLine]:
xl, yl = layout_data_map[gid]
left, right = ends(xl)
top, bottom = ends(yl)
first_row, last_row = gid in is_first_row, gid in is_last_row
first_column, last_column = gid in is_first_column, gid in is_last_column
# Horizontal
if not first_row:
yield BorderLine(Edges(left, top, right, top + bw), color, -wid, True)
if not last_row:
yield BorderLine(Edges(left, bottom - bw, right, bottom), color, wid, True)
# Vertical
if not first_column:
yield BorderLine(Edges(left, top, left + bw, bottom), color, -wid, False)
if not last_column:
yield BorderLine(Edges(right - bw, top, right, bottom), color, wid, False)
for wg in all_groups_in_order:
color = BorderColor.inactive
if needs_borders_map.get(wg.id):
color = BorderColor.active if wg is active_group else BorderColor.bell
wid = wg.active_window_id
yield from borders_for_window(wg.id, color, wid)
def neighbors_for_window(self, window: WindowType, windows: WindowList) -> NeighborsMap:
n = windows.num_groups
if n < 4:
return neighbors_for_tall_window(1, window, windows)
wg = windows.group_for_window(window)
assert wg is not None
ncols, nrows, special_rows, special_col = calc_grid_size(n)
blank_row: list[int | None] = [None for i in range(ncols)]
matrix = tuple(blank_row[:] for j in range(max(nrows, special_rows)))
wi = windows.iter_all_layoutable_groups()
pos_map: dict[int, tuple[int, int]] = {}
col_counts: list[int] = []
for col in range(ncols):
rows = special_rows if col == special_col else nrows
for row in range(rows):
w = next(wi)
matrix[row][col] = wid = w.id
pos_map[wid] = row, col
col_counts.append(rows)
row, col = pos_map[wg.id]
def neighbors(row: int, col: int) -> list[int]:
try:
ans = matrix[row][col]
except IndexError:
ans = None
return [] if ans is None else [ans]
def side(row: int, col: int, delta: int) -> list[int]:
neighbor_col = col + delta
neighbor_nrows = col_counts[neighbor_col]
nrows = col_counts[col]
if neighbor_nrows == nrows:
return neighbors(row, neighbor_col)
start_row = floor(neighbor_nrows * row / nrows)
end_row = ceil(neighbor_nrows * (row + 1) / nrows)
xs = []
for neighbor_row in range(start_row, end_row):
xs.extend(neighbors(neighbor_row, neighbor_col))
return xs
ans: NeighborsMap = {}
if row:
ans['top'] = neighbors(row - 1, col)
if bottom := neighbors(row + 1, col):
ans['bottom'] = bottom
if col and (left := side(row, col, -1)):
ans['left'] = left
if col < ncols - 1:
ans['right'] = side(row, col, 1)
return ans
def layout_state(self) -> dict[str, Any]:
return {'biased_cols': self.biased_cols, 'biased_rows': self.biased_rows}
def set_layout_state(self, layout_state: dict[str, Any], map_group_id: WindowMapper) -> bool:
self.biased_rows = {int(k): v for k, v in layout_state['biased_rows'].items()}
self.biased_cols = {int(k): v for k, v in layout_state['biased_cols'].items()}
return True
+43
View File
@@ -0,0 +1,43 @@
#!/usr/bin/env python
# License: GPLv3 Copyright: 2020, Kovid Goyal <kovid at kovidgoyal.net>
from .base import Layout
from .grid import Grid
from .splits import Splits
from .stack import Stack
from .tall import Fat, Tall
from .vertical import Horizontal, Vertical
all_layouts: dict[str, type[Layout]] = {
Stack.name: Stack,
Tall.name: Tall,
Fat.name: Fat,
Vertical.name: Vertical,
Horizontal.name: Horizontal,
Grid.name: Grid,
Splits.name: Splits,
}
KeyType = tuple[str, int, int, str]
class CreateLayoutObjectFor:
cache: dict[KeyType, Layout] = {}
def __call__(self, name: str, os_window_id: int, tab_id: int, layout_opts: str = '') -> Layout:
key = name, os_window_id, tab_id, layout_opts
ans = create_layout_object_for.cache.get(key)
if ans is None:
name, layout_opts = name.partition(':')[::2]
ans = create_layout_object_for.cache[key] = all_layouts[name](os_window_id, tab_id, layout_opts)
return ans
create_layout_object_for = CreateLayoutObjectFor()
def evict_cached_layouts(tab_id: int) -> None:
remove = [key for key in create_layout_object_for.cache if key[2] == tab_id]
for key in remove:
del create_layout_object_for.cache[key]
File diff suppressed because it is too large. Load diff
+27
View File
@@ -0,0 +1,27 @@
#!/usr/bin/env python
# License: GPLv3 Copyright: 2020, Kovid Goyal <kovid at kovidgoyal.net>
from kitty.types import NeighborsMap
from kitty.typing_compat import WindowType
from kitty.window_list import WindowList
from .base import Layout
class Stack(Layout):
name = 'stack'
needs_window_borders = False
only_active_window_visible = True
def do_layout(self, windows: WindowList) -> None:
for group in windows.iter_all_layoutable_groups():
self.layout_single_window_group(group)
def neighbors_for_window(self, window: WindowType, windows: WindowList) -> NeighborsMap:
wg = windows.group_for_window(window)
assert wg is not None
groups = tuple(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]
return {'top': before, 'left': before, 'right': after, 'bottom': after}
+403
View File
@@ -0,0 +1,403 @@
#!/usr/bin/env python
# License: GPLv3 Copyright: 2020, Kovid Goyal <kovid at kovidgoyal.net>
import sys
from collections.abc import Generator, Iterator, Sequence
from itertools import islice, repeat
from typing import Any
from kitty.borders import BorderColor
from kitty.conf.utils import to_bool
from kitty.fast_data_types import BOTTOM_EDGE, RIGHT_EDGE
from kitty.types import Edges, NeighborsMap, WindowMapper, WindowResizeDragData
from kitty.typing_compat import EdgeLiteral, WindowType
from kitty.window_list import WindowGroup, WindowList
from .base import (
BorderLine,
DragOverlayMode,
Layout,
LayoutData,
LayoutDimension,
LayoutOpts,
lgd,
normalize_biases,
safe_increment_bias,
)
from .vertical import borders
def drag_resize_target_windows(
click_window: WindowType, edges: int, x: float, y: float, num_full_size_windows: int, all_windows: WindowList, main_is_horizontal: bool = True
) -> WindowResizeDragData:
groups = tuple(all_windows.iter_all_layoutable_groups())
horizontal = vertical = click_window
min_dist = float(sys.maxsize)
height_increases_downwards = bool(edges & BOTTOM_EDGE)
width_increases_rightwards = bool(edges & RIGHT_EDGE)
for gr in groups[num_full_size_windows:]:
if gr.windows:
w = gr.windows[-1]
g = w.geometry
if main_is_horizontal:
if (dist := min(abs(g.top - y), abs(g.bottom - y))) < min_dist:
min_dist = dist
vertical = w
height_increases_downwards = y > g.top + (g.bottom - g.top) / 2
else:
if (dist := min(abs(g.left - x), abs(g.right - x))) < min_dist:
min_dist = dist
horizontal = w
width_increases_rightwards = x > g.left + (g.right - g.left) / 2
return WindowResizeDragData(horizontal.id, width_increases_rightwards, vertical.id, height_increases_downwards)
def neighbors_for_tall_window(
num_full_size_windows: int, window: WindowType, all_windows: WindowList, mirrored: bool = False, main_is_horizontal: bool = True
) -> 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)
prev = None if idx == 0 else groups[idx - 1]
nxt = None if idx == len(groups) - 1 else groups[idx + 1]
ans: NeighborsMap = {}
main_before: EdgeLiteral = 'left' if main_is_horizontal else 'top'
main_after: EdgeLiteral = 'right' if main_is_horizontal else 'bottom'
cross_before: EdgeLiteral = 'top' if main_is_horizontal else 'left'
cross_after: EdgeLiteral = 'bottom' if main_is_horizontal else 'right'
if mirrored:
main_before, main_after = main_after, main_before
if prev is not None:
ans[main_before] = [prev.id]
if idx < num_full_size_windows - 1:
if nxt is not None:
ans[main_after] = [nxt.id]
elif idx == num_full_size_windows - 1:
ans[main_after] = [w.id for w in groups[idx + 1 :]]
else:
ans[main_before] = [groups[num_full_size_windows - 1].id]
if idx > num_full_size_windows and prev is not None:
ans[cross_before] = [prev.id]
if nxt is not None:
ans[cross_after] = [nxt.id]
return ans
class TallLayoutOpts(LayoutOpts):
bias: int = 50
full_size: int = 1
mirrored: bool = False
def __init__(self, data: dict[str, str]):
try:
self.full_size = int(data.get('full_size', 1))
except Exception:
self.full_size = 1
self.full_size = max(1, min(self.full_size, 100))
try:
self.bias = int(data.get('bias', 50))
except Exception:
self.bias = 50
rv: bool | str = data.get('mirrored', 'false')
self.mirrored = to_bool(rv) if isinstance(rv, str) else bool(rv)
def serialized(self) -> dict[str, Any]:
return {'full_size': self.full_size, 'bias': self.bias, 'mirrored': 'y' if self.mirrored else 'n'}
def build_bias_list(self) -> tuple[float, ...]:
b = self.bias / 100
b = max(0.1, min(b, 0.9))
return tuple(repeat(b / self.full_size, self.full_size)) + (1.0 - b,)
def set_bias(biases: Sequence[float], idx: int, target: float) -> list[float]:
remainder = 1 - target
previous_remainder = sum(x for i, x in enumerate(biases) if i != idx)
ans = [1.0 for i in range(len(biases))]
for i in range(len(biases)):
if i == idx:
ans[i] = target
else:
ans[i] = remainder * biases[i] / previous_remainder
return ans
class Tall(Layout):
name = 'tall'
main_is_horizontal = True
no_minimal_window_borders = True
drag_overlay_mode = DragOverlayMode.axis_y
layout_opts: TallLayoutOpts = TallLayoutOpts({})
main_axis_layout = Layout.xlayout
perp_axis_layout = Layout.ylayout
@property
def num_full_size_windows(self) -> int:
return self.layout_opts.full_size
def remove_all_biases(self) -> bool:
self.main_bias: list[float] = list(self.layout_opts.build_bias_list())
self.biased_map: dict[int, float] = {}
return True
def variable_layout(self, all_windows: WindowList, biased_map: dict[int, float]) -> LayoutDimension:
num = all_windows.num_groups - self.num_full_size_windows
bias = biased_map if num > 1 else None
return self.perp_axis_layout(all_windows.iter_all_layoutable_groups(), bias=bias, offset=self.num_full_size_windows)
def bias_slot(self, all_windows: WindowList, idx: int, fractional_bias: float, cell_increment_bias_h: float, cell_increment_bias_v: float) -> bool:
if idx < len(self.main_bias):
before_main_bias = self.main_bias
self.main_bias = set_bias(self.main_bias, idx, fractional_bias)
return self.main_bias != before_main_bias
before_layout = tuple(self.variable_layout(all_windows, self.biased_map))
self.biased_map[idx - self.num_full_size_windows] = cell_increment_bias_v if self.main_is_horizontal else cell_increment_bias_h
after_layout = tuple(self.variable_layout(all_windows, self.biased_map))
return before_layout == after_layout
def apply_bias(self, window_id: int, increment: float, all_windows: WindowList, is_horizontal: bool = True) -> bool:
num_windows = all_windows.num_groups
if self.main_is_horizontal == is_horizontal:
before_main_bias = self.main_bias
ncols = self.num_full_size_windows + 1
biased_col = window_id if window_id < self.num_full_size_windows else (ncols - 1)
self.main_bias = [safe_increment_bias(self.main_bias[i], increment * (1 if i == biased_col else -1)) for i in range(ncols)]
self.main_bias = normalize_biases(self.main_bias)
return self.main_bias != before_main_bias
num_of_short_windows = num_windows - self.num_full_size_windows
if window_id < self.num_full_size_windows or num_of_short_windows < 2:
return False
window_id -= self.num_full_size_windows
before_layout = tuple(self.variable_layout(all_windows, self.biased_map))
before = self.biased_map.get(window_id, 0.0)
candidate = self.biased_map.copy()
candidate[window_id] = after = before + increment
if before_layout == tuple(self.variable_layout(all_windows, candidate)):
return False
self.biased_map = candidate
return before != after
def apply_fractional_bias(self, window_idx: int, increment: float, all_windows: WindowList, is_horizontal: bool) -> bool:
# Main-axis biases can keep changing after the visible size stops
# changing. Do not count those changes as remaining resize steps.
layout = self.simple_layout if all_windows.num_groups <= self.num_full_size_windows + 1 else self.full_layout
before_layout = tuple((x, y) for _, x, y, _ in layout(all_windows))
before_bias, before_map = self.main_bias, self.biased_map
if not self.apply_bias(window_idx, increment, all_windows, is_horizontal):
return False
if before_layout == tuple((x, y) for _, x, y, _ in layout(all_windows)):
self.main_bias, self.biased_map = before_bias, before_map
return False
return True
def simple_layout(self, all_windows: WindowList) -> Generator[tuple[WindowGroup, LayoutData, LayoutData, bool], None, None]:
num = all_windows.num_groups
is_fat = not self.main_is_horizontal
mirrored = self.layout_opts.mirrored
groups = tuple(all_windows.iter_all_layoutable_groups())
main_bias = self.main_bias[::-1] if mirrored else self.main_bias
if mirrored:
groups = tuple(reversed(groups))
main_bias = normalize_biases(main_bias[:num])
xlayout = self.main_axis_layout(iter(groups), bias=main_bias)
for wg, xl in zip(groups, xlayout):
yl = next(self.perp_axis_layout(iter((wg,))))
if is_fat:
xl, yl = yl, xl
yield wg, xl, yl, True
def full_layout(self, all_windows: WindowList) -> Generator[tuple[WindowGroup, LayoutData, LayoutData, bool], None, None]:
is_fat = not self.main_is_horizontal
mirrored = self.layout_opts.mirrored
groups = tuple(all_windows.iter_all_layoutable_groups())
main_bias = self.main_bias[::-1] if mirrored else self.main_bias
start = lgd.central.top if is_fat else lgd.central.left
size = 0
if mirrored:
fsg = groups[: self.num_full_size_windows + 1]
xlayout = self.main_axis_layout(reversed(fsg), bias=main_bias)
for i, wg in enumerate(reversed(fsg)):
xl = next(xlayout)
if i == 0:
size = xl.content_size + xl.space_before + xl.space_after
continue
yl = next(self.perp_axis_layout(iter((wg,))))
if is_fat:
xl, yl = yl, xl
yield wg, xl, yl, True
else:
xlayout = self.main_axis_layout(islice(groups, self.num_full_size_windows + 1), bias=main_bias)
for i, wg in enumerate(groups):
if i >= self.num_full_size_windows:
break
xl = next(xlayout)
yl = next(self.perp_axis_layout(iter((wg,))))
start = xl.content_pos + xl.content_size + xl.space_after
if is_fat:
xl, yl = yl, xl
yield wg, xl, yl, True
size = 1 + (lgd.central.bottom if is_fat else lgd.central.right) - start
ylayout = self.variable_layout(all_windows, self.biased_map)
for i, wg in enumerate(all_windows.iter_all_layoutable_groups()):
if i < self.num_full_size_windows:
continue
yl = next(ylayout)
xl = next(self.main_axis_layout(iter((wg,)), start=start, size=size))
if is_fat:
xl, yl = yl, xl
yield wg, xl, yl, False
def do_layout(self, windows: WindowList) -> None:
num = windows.num_groups
if num == 1:
self.layout_single_window_group(next(windows.iter_all_layoutable_groups()))
return
layouts = (self.simple_layout if num <= self.num_full_size_windows + 1 else self.full_layout)(windows)
for wg, xl, yl, is_full_size in layouts:
self.set_window_group_geometry(wg, xl, yl)
def neighbors_for_window(self, window: WindowType, windows: WindowList) -> NeighborsMap:
return neighbors_for_tall_window(self.num_full_size_windows, window, windows, self.layout_opts.mirrored, self.main_is_horizontal)
def layout_action(self, action_name: str, args: Sequence[str], all_windows: WindowList) -> bool | None:
if action_name == 'increase_num_full_size_windows':
self.layout_opts.full_size += 1
self.main_bias = list(self.layout_opts.build_bias_list())
return True
if action_name == 'decrease_num_full_size_windows':
if self.layout_opts.full_size > 1:
self.layout_opts.full_size -= 1
self.main_bias = list(self.layout_opts.build_bias_list())
return True
if action_name == 'mirror':
action = (args or ('toggle',))[0]
ok = False
if action == 'toggle':
self.layout_opts.mirrored = not self.layout_opts.mirrored
ok = True
else:
new_val = to_bool(action)
if new_val != self.layout_opts.mirrored:
self.layout_opts.mirrored = new_val
ok = True
return ok
if action_name == 'bias':
if len(args) == 0:
raise ValueError('layout_action bias must contain at least one number between 10 and 90')
biases = args[0].split()
if len(biases) == 1:
biases.append('50')
try:
i = biases.index(str(self.layout_opts.bias)) + 1
except ValueError:
i = 0
try:
self.layout_opts.bias = int(biases[i % len(biases)])
self.remove_all_biases()
return True
except Exception:
return False
return None
def minimal_borders(self, windows: WindowList) -> Iterator[BorderLine]:
num = windows.num_groups
if num < 2 or not lgd.draw_minimal_borders:
return
try:
bw = next(windows.iter_all_layoutable_groups()).effective_border()
except StopIteration:
bw = 0
if not bw:
return
if num <= self.num_full_size_windows + 1:
layout = (x[:3] for x in self.simple_layout(windows))
yield from borders(layout, self.main_is_horizontal, windows)
return
main_layouts: list[tuple[WindowGroup, LayoutData, LayoutData]] = []
perp_borders: list[BorderLine] = []
layouts = (self.simple_layout if num <= self.num_full_size_windows else self.full_layout)(windows)
needs_borders_map = windows.compute_needs_borders_map(lgd.draw_active_borders)
active_group = windows.active_group
mirrored = self.layout_opts.mirrored
for wg, xl, yl, is_full_size in layouts:
if is_full_size:
main_layouts.append((wg, xl, yl))
else:
color = BorderColor.inactive
if needs_borders_map.get(wg.id):
color = BorderColor.active if wg is active_group else BorderColor.bell
wid = wg.active_window_id
mult = 1 if mirrored else -1
def h(left: int, right: int, top: int, mult: int) -> None:
e = Edges(left, top, right, top + bw)
perp_borders.append(BorderLine(e, color, mult * wid, True))
def v(top: int, bottom: int, left: int, mult: int) -> None:
e = Edges(left, top, left + bw, bottom)
perp_borders.append(BorderLine(e, color, mult * wid, False))
if self.main_is_horizontal:
h(xl.content_pos - xl.space_before, xl.content_pos + xl.content_size + xl.space_after, yl.content_pos - yl.space_before, -1)
v(
yl.content_pos - yl.space_before,
yl.content_pos + yl.content_size + yl.space_after,
xl.content_pos + ((xl.content_size + xl.space_after - bw) if mirrored else -xl.space_before),
mult,
)
h(
xl.content_pos - xl.space_before,
xl.content_pos + xl.content_size + xl.space_after,
yl.content_pos + yl.content_size + yl.space_after - bw,
1,
)
else:
v(yl.content_pos - yl.space_before, yl.content_pos + yl.content_size + yl.space_after, xl.content_pos - xl.space_before, -1)
h(
xl.content_pos - xl.space_before,
xl.content_pos + xl.content_size + xl.space_after,
yl.content_pos + ((yl.content_size + yl.space_after - bw) if mirrored else -yl.space_before),
mult,
)
v(
yl.content_pos - yl.space_before,
yl.content_pos + yl.content_size + yl.space_after,
xl.content_pos + xl.content_size + xl.space_after - bw,
1,
)
mirrored = self.layout_opts.mirrored
yield from borders(main_layouts, self.main_is_horizontal, windows, start_offset=int(not mirrored), end_offset=int(mirrored))
yield from perp_borders[1:-1]
def layout_state(self) -> dict[str, Any]:
return {'main_bias': self.main_bias, 'biased_map': self.biased_map}
def set_layout_state(self, layout_state: dict[str, Any], map_group_id: WindowMapper) -> bool:
self.main_bias = layout_state['main_bias']
self.biased_map = {int(k): v for k, v in layout_state['biased_map'].items()}
self.layout_opts = TallLayoutOpts(layout_state['opts'])
return True
def drag_resize_target_windows(
self,
click_window: WindowType,
x: float,
y: float,
edges: int,
all_windows: WindowList,
) -> WindowResizeDragData:
return drag_resize_target_windows(click_window, edges, x, y, self.num_full_size_windows, all_windows, self.main_is_horizontal)
class Fat(Tall):
name = 'fat'
main_is_horizontal = False
drag_overlay_mode = DragOverlayMode.axis_x
main_axis_layout = Layout.ylayout
perp_axis_layout = Layout.xlayout
+165
View File
@@ -0,0 +1,165 @@
#!/usr/bin/env python
# License: GPLv3 Copyright: 2020, Kovid Goyal <kovid at kovidgoyal.net>
from collections.abc import Generator, Iterable
from typing import Any
from kitty.borders import BorderColor
from kitty.types import Edges, NeighborsMap, WindowMapper
from kitty.typing_compat import EdgeLiteral, WindowType
from kitty.window_list import WindowGroup, WindowList
from .base import BorderLine, DragOverlayMode, Layout, LayoutData, LayoutDimension, lgd
def borders(
data: Iterable[tuple[WindowGroup, LayoutData, LayoutData]], is_horizontal: bool, all_windows: WindowList, start_offset: int = 1, end_offset: int = 1
) -> Generator[BorderLine, None, None]:
borders: list[BorderLine] = []
active_group = all_windows.active_group
needs_borders_map = all_windows.compute_needs_borders_map(lgd.draw_active_borders)
try:
bw = next(all_windows.iter_all_layoutable_groups()).effective_border()
except StopIteration:
bw = 0
if not bw:
return
for wg, xl, yl in data:
if is_horizontal:
e1 = Edges(
xl.content_pos - xl.space_before,
yl.content_pos - yl.space_before,
xl.content_pos - xl.space_before + bw,
yl.content_pos + yl.content_size + yl.space_after,
)
e2 = Edges(
xl.content_pos + xl.content_size + xl.space_after - bw,
yl.content_pos - yl.space_before,
xl.content_pos + xl.content_size + xl.space_after,
yl.content_pos + yl.content_size + yl.space_after,
)
else:
e1 = Edges(
xl.content_pos - xl.space_before,
yl.content_pos - yl.space_before,
xl.content_pos + xl.content_size + xl.space_after,
yl.content_pos - yl.space_before + bw,
)
e2 = Edges(
xl.content_pos - xl.space_before,
yl.content_pos + yl.content_size + yl.space_after - bw,
xl.content_pos + xl.content_size + xl.space_after,
yl.content_pos + yl.content_size + yl.space_after,
)
color = BorderColor.inactive
if needs_borders_map.get(wg.id):
color = BorderColor.active if wg is active_group else BorderColor.bell
borders.append(BorderLine(e1, color, -wg.active_window_id, not is_horizontal))
borders.append(BorderLine(e2, color, wg.active_window_id, not is_horizontal))
last_idx = len(borders) - 1 - end_offset
for i, x in enumerate(borders):
if start_offset <= i <= last_idx:
yield x
class Vertical(Layout):
name = 'vertical'
main_is_horizontal = False
no_minimal_window_borders = True
drag_overlay_mode = DragOverlayMode.axis_y
main_axis_layout = Layout.ylayout
perp_axis_layout = Layout.xlayout
def variable_layout(self, all_windows: WindowList, biased_map: dict[int, float]) -> LayoutDimension:
num_windows = all_windows.num_groups
bias = biased_map if num_windows > 1 else None
return self.main_axis_layout(all_windows.iter_all_layoutable_groups(), bias=bias)
def fixed_layout(self, wg: WindowGroup) -> LayoutDimension:
return self.perp_axis_layout(iter((wg,)), border_mult=0 if lgd.draw_minimal_borders else 1)
def remove_all_biases(self) -> bool:
self.biased_map: dict[int, float] = {}
return True
def apply_bias(self, window_id: int, increment: float, all_windows: WindowList, is_horizontal: bool = True) -> bool:
if self.main_is_horizontal != is_horizontal:
return False
num_windows = all_windows.num_groups
if num_windows < 2:
return False
before_layout = list(self.variable_layout(all_windows, self.biased_map))
candidate = self.biased_map.copy()
before = candidate.get(window_id, 0)
candidate[window_id] = before + increment
if before_layout == list(self.variable_layout(all_windows, candidate)):
return False
self.biased_map = candidate
return True
def bias_slot(self, all_windows: WindowList, idx: int, fractional_bias: float, cell_increment_bias_h: float, cell_increment_bias_v: float) -> bool:
before_layout = tuple(self.variable_layout(all_windows, self.biased_map))
self.biased_map[idx] = cell_increment_bias_h if self.main_is_horizontal else cell_increment_bias_v
after_layout = tuple(self.variable_layout(all_windows, self.biased_map))
return before_layout == after_layout
def generate_layout_data(self, all_windows: WindowList) -> Generator[tuple[WindowGroup, LayoutData, LayoutData], None, None]:
ylayout = self.variable_layout(all_windows, self.biased_map)
for wg, yl in zip(all_windows.iter_all_layoutable_groups(), ylayout):
xl = next(self.fixed_layout(wg))
if self.main_is_horizontal:
xl, yl = yl, xl
yield wg, xl, yl
def do_layout(self, windows: WindowList) -> None:
window_count = windows.num_groups
if window_count == 1:
self.layout_single_window_group(next(windows.iter_all_layoutable_groups()))
return
for wg, xl, yl in self.generate_layout_data(windows):
self.set_window_group_geometry(wg, xl, yl)
def minimal_borders(self, windows: WindowList) -> Generator[BorderLine, None, None]:
window_count = windows.num_groups
if window_count < 2 or not lgd.draw_minimal_borders:
return
yield from borders(self.generate_layout_data(windows), self.main_is_horizontal, windows)
def neighbors_for_window(self, window: WindowType, windows: WindowList) -> NeighborsMap:
wg = windows.group_for_window(window)
assert wg is not None
groups = tuple(windows.iter_all_layoutable_groups())
idx = groups.index(wg)
lg = len(groups)
if lg > 1:
after = [groups[(idx - 1 + lg) % lg].id]
before = [groups[(idx + 1) % lg].id]
else:
before, after = [], []
ans: NeighborsMap = {}
akey: EdgeLiteral = 'top'
bkey: EdgeLiteral = 'bottom'
if self.main_is_horizontal:
akey, bkey = 'left', 'right'
if before:
ans[bkey] = before
if after:
ans[akey] = after
return ans
def layout_state(self) -> dict[str, Any]:
return {'biased_map': self.biased_map}
def set_layout_state(self, layout_state: dict[str, Any], map_group_id: WindowMapper) -> bool:
self.biased_map = {int(k): v for k, v in layout_state['biased_map'].items()}
return True
class Horizontal(Vertical):
name = 'horizontal'
main_is_horizontal = True
drag_overlay_mode = DragOverlayMode.axis_x
main_axis_layout = Layout.xlayout
perp_axis_layout = Layout.ylayout