Skip to content

Commit

Permalink
feat(core/tree): add Tree.adjacent_node() and use it in existing func…
Browse files Browse the repository at this point in the history
…tions
  • Loading branch information
aravinda0 committed Jun 14, 2024
1 parent c3529b0 commit 833c3ae
Showing 1 changed file with 26 additions and 13 deletions.
39 changes: 26 additions & 13 deletions src/qtile_bonsai/core/tree.py
Original file line number Diff line number Diff line change
Expand Up @@ -434,6 +434,30 @@ def focus(self, pane: Pane):
node = node.parent
pane.recency = self.next_recency_value()

def adjacent_node(
self, node: Node, direction: DirectionParam, *, wrap: bool = True
) -> Node:
"""Return the node that is adjacent to the provided `node` in the specified
direction. This may be a 'supernode' that is a container for multiple child
nodes.
Returns the node itself when there are no suitable sibling nodes.
NOTES:
- Tab bars are ignored. Two panes can be adjacent even if a subtab bar
appears between them.
"""
direction = Direction(direction)

supernode = self.find_border_encompassing_supernode(node, direction)
if supernode is None:
return node

supernode_sibling = supernode.sibling(direction.axis_unit, wrap=wrap)
if supernode_sibling is None or supernode_sibling is supernode:
return node

return supernode_sibling

def adjacent_pane(
self, pane: Pane, direction: DirectionParam, *, wrap: bool = True
) -> Pane:
Expand All @@ -450,22 +474,11 @@ def adjacent_panes(
) -> list[Pane]:
"""Returns all panes that are adjacent to the provided `node` in the specified
`direction`.
NOTES:
- 'Adjacent' here means that panes may partially or wholly share a border
with the provided `node`.
- A pane is not adjacent to itself.
- Tab bars are ignored. Two panes can be adjacent even if a subtab bar
appears between them.
"""
direction = Direction(direction)

supernode = self.find_border_encompassing_supernode(node, direction)
if supernode is None:
return []

supernode_sibling = supernode.sibling(direction.axis_unit, wrap=wrap)
if supernode_sibling is None or supernode_sibling is supernode:
supernode_sibling = self.adjacent_node(node, direction, wrap=wrap)
if supernode_sibling is node:
return []

adjacent = []
Expand Down

0 comments on commit 833c3ae

Please sign in to comment.