Skip to content

Commit

Permalink
refactor(layout): _reset() -> _init(); embrace qtile clone-init flow …
Browse files Browse the repository at this point in the history
…to help us work towards RAII of Tree classes
  • Loading branch information
aravinda0 committed Feb 13, 2024
1 parent e4f0919 commit a6a295a
Showing 1 changed file with 13 additions and 11 deletions.
24 changes: 13 additions & 11 deletions src/qtile_bonsai/layout.py
Original file line number Diff line number Diff line change
Expand Up @@ -193,6 +193,8 @@ def __init__(self, **config) -> None:
super().__init__(**config)
self.add_defaults(self.defaults)

# We declare everything here, but things are initialized in `self._init()`. See
# docs for `self.clone()`.
self._tree: Tree
self._focused_window: Window | None
self._windows_to_panes: dict[Window, BonsaiPane]
Expand All @@ -203,8 +205,6 @@ def __init__(self, **config) -> None:
)
self._restoration_window_id_to_pane_id: dict[int, int] = {}

self._reset()

@property
def focused_window(self) -> Window | None:
return self._focused_window
Expand All @@ -221,18 +221,20 @@ def clone(self, group):
This is a bit different from traditional copying/cloning of any 'current' state
of the layout instance. In qtile, the config file holds the 'first' instance of
the layout, then each Group instance 'clones' of that original instance (which
likely remains in its initial state) and uses the new instance for all future
operations.
the layout. Then, as each `Group` is created, it is initialized with 'clones' of
that original instance.
All the built-in layouts perform a state-resetting in their `clone()`
implementations.
All the qtile-provided built-in layouts perform a state-resetting in their
`clone()` implementations.
So in practice, qtile treats the `Layout.clone()` method more like a factory
method, to create fresh instances.
So in practice, it seems qtile treats `Layout.clone()` sort of like an 'init'
function.
Here, we lean into this fully. We can instantiate a `Bonsai` layout instance,
but we can only use it after `_init()` is called, which happens via `clone()`
when qtile is ready to provide us with the associated `Group` instance.
"""
pseudo_clone = super().clone(group)
pseudo_clone._reset()
pseudo_clone._init()
return pseudo_clone

def layout(self, windows: Sequence[Window], screen_rect: ScreenRect):
Expand Down Expand Up @@ -788,7 +790,7 @@ def info(self):
def _handle_default_next_window(self) -> BonsaiPane:
return self._tree.tab()

def _reset(self):
def _init(self):
config = self.parse_multi_level_config()

# We initialize the tree with arbitrary dimensions. These get reset soon as this
Expand Down

0 comments on commit a6a295a

Please sign in to comment.