Skip to content

Commit

Permalink
Better empty state for the collection tree
Browse files Browse the repository at this point in the history
  • Loading branch information
darrenburns committed Aug 11, 2024
1 parent 4a77aaf commit d877cbf
Showing 1 changed file with 42 additions and 7 deletions.
49 changes: 42 additions & 7 deletions src/posting/widgets/collection/browser.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
from textual.geometry import Region
from textual.message import Message
from textual.reactive import Reactive, reactive
from textual.widgets import Static, Tree
from textual.widgets import Label, Static, Tree
from textual.widgets.tree import TreeNode

from posting.collection import Collection, RequestModel
Expand Down Expand Up @@ -66,6 +66,16 @@ def __init__(
)
self.cached_base_urls: set[str] = set()

@dataclass
class RequestAdded(Message):
request: RequestModel
node: TreeNode[CollectionNode]
tree: "CollectionTree"

@property
def control(self) -> "CollectionTree":
return self.tree

@dataclass
class RequestSelected(Message):
request: RequestModel
Expand Down Expand Up @@ -319,12 +329,23 @@ def add_request(
parent_node: TreeNode[CollectionNode],
after: TreeNode[CollectionNode] | int | None = None,
before: TreeNode[CollectionNode] | int | None = None,
) -> TreeNode[CollectionNode]:
) -> TreeNode[CollectionNode] | None:
"""Add a new request to the tree, and cache data from it."""
self.cache_request(request)
return parent_node.add_leaf(
request.name, data=request, after=after, before=before
)
try:
added_node = parent_node.add_leaf(
request.name, data=request, after=after, before=before
)
except Exception as e:
self.notify(
title="Error adding request.",
message=f"{e}",
timeout=3,
)
return None
else:
self.post_message(self.RequestAdded(request, parent_node, self))
return added_node

def cache_request(self, request: RequestModel) -> None:
def get_base_url(url: str) -> str | None:
Expand Down Expand Up @@ -393,6 +414,11 @@ class CollectionBrowser(Vertical):
background: transparent;
}
#empty-collection-label {
color: $text-muted;
padding: 1 2;
}
}
"""
Expand All @@ -412,8 +438,11 @@ def compose(self) -> ComposeResult:
self.border_title = "Collection"
self.add_class("section")
collection = self.collection
if collection is None:
return

yield Static(
"[i]Collection is empty.[/]\nPress [b]ctrl+s[/b] to save the current request.",
id="empty-collection-label",
)

tree = CollectionTree(
label=collection.name,
Expand Down Expand Up @@ -447,6 +476,12 @@ def add_collection_to_tree(
yield tree
yield RequestPreview()

@on(CollectionTree.RequestAdded)
def on_request_added(self, event: CollectionTree.RequestAdded) -> None:
self.query_one("#empty-collection-label").display = (
len(event.tree.root.children) == 0
)

@on(CollectionTree.RequestSelected)
def on_request_selected(self, event: CollectionTree.RequestSelected) -> None:
if isinstance(event.node.data, RequestModel):
Expand Down

0 comments on commit d877cbf

Please sign in to comment.