Skip to content

Commit

Permalink
fix: closing an application now works even if the application is not …
Browse files Browse the repository at this point in the history
…the root of its hierarchy
  • Loading branch information
sassanh committed Dec 1, 2024
1 parent f721137 commit ac4c312
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 23 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
# Changelog

## Upcoming

- fix: closing an application now works even if the application is not the root of its hierarchy

## Version 0.13.9

- refactor: set parent of `StackApplicationItem`s like `StackMenuItem`s
Expand Down
37 changes: 18 additions & 19 deletions ubo_gui/menu/menu_widget.py
Original file line number Diff line number Diff line change
Expand Up @@ -601,30 +601,29 @@ def close_application(self: MenuWidget, application: PageWidget) -> None:
# If any of these applications are the top of the stack, remove it with `pop` to
# ensure the animation is played.
with self.stack_lock:
if any(
isinstance(item.root, StackApplicationItem)
and item.root.application is application
to_be_removed = [
cast(StackApplicationItem, item)
for item in self.stack
):
to_be_removed = [
cast(StackApplicationItem, item)
for item in self.stack
if isinstance(item.root, StackApplicationItem)
and item.root.application is application
and item is not self.top
]

for item in to_be_removed:
if any(
isinstance(item, StackApplicationItem)
and item.application is application
for item in item.lineage
)
]

for item in to_be_removed:
if item is not self.top:
item.clear_subscriptions()
item.application.dispatch('on_close')

self.stack = [item for item in self.stack if item not in to_be_removed]
self.stack = [
item
for item in self.stack
if item not in to_be_removed or item is self.top
]

if (
isinstance(self.top.root, StackApplicationItem)
and self.top.root.application is application
):
self._pop()
if self.top in to_be_removed:
self._pop()

@property
def root(self: MenuWidget) -> StackMenuItem:
Expand Down
16 changes: 12 additions & 4 deletions ubo_gui/menu/stack_item.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@

from __future__ import annotations

from collections.abc import Sequence
from collections.abc import Generator, Sequence
from dataclasses import dataclass, field
from typing import TYPE_CHECKING, Self, TypeVar, cast
from typing import TYPE_CHECKING, Self, TypeVar

if TYPE_CHECKING:
from collections.abc import Callable
Expand All @@ -31,11 +31,19 @@ def clear_subscriptions(self: BaseStackItem) -> None:
unsubscribe()

@property
def root(self: BaseStackItem) -> StackMenuItem:
def root(self: BaseStackItem) -> BaseStackItem:
"""Return the root item."""
if self.parent:
return self.parent.root
return cast(StackMenuItem, self)
return self

@property
def lineage(self: BaseStackItem) -> Generator[BaseStackItem, None, None]:
"""A generator iterating from the current item to its root."""
current = self
while current:
yield current
current = current.parent

@property
def title(self: Self) -> str:
Expand Down

0 comments on commit ac4c312

Please sign in to comment.