Skip to content

Commit

Permalink
🔨 Improve the way we scroll to the end of the document
Browse files Browse the repository at this point in the history
  • Loading branch information
davep committed Apr 29, 2024
1 parent 33b1798 commit c1ee262
Showing 1 changed file with 13 additions and 16 deletions.
29 changes: 13 additions & 16 deletions natter/screens/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -68,27 +68,24 @@ class Main(Screen[None]):
_conversation: var[list[Message]] = var(list)
"""The ongoing conversation."""

def compose(self) -> ComposeResult:
yield VerticalScroll()
yield UserInput()
def __init__(self) -> None:
"""Initialise the main screen."""
super().__init__()
if (source := (conversations_dir() / self._CONVERSATION_FILE)).exists():
self._conversation = loads(source.read_text())

async def on_mount(self) -> None:
"""Reload any previous conversation."""
if not (source := (conversations_dir() / self._CONVERSATION_FILE)).exists():
return
self._conversation = loads(source.read_text())
await self.query_one(VerticalScroll).mount_all(
[
def compose(self) -> ComposeResult:
yield VerticalScroll(
*[
{User.ROLE: User, Agent.ROLE: Agent}[part["role"]](part["content"])
for part in self._conversation
]
)
# Scrolling to the end feels like it should work here, but even with
# call_later it doesn't work; it needs a timer for some reason.
# TODO: look into if this is supposed to be the case or not.
self.set_timer(
0.05, lambda: self.query_one(VerticalScroll).scroll_end(animate=False)
)
yield UserInput()

async def on_mount(self) -> None:
"""Settle the UI on startup."""
self.query_one(VerticalScroll).scroll_end(animate=False)

@on(UserInput.Submitted)
async def handle_input(self, event: UserInput.Submitted) -> None:
Expand Down

0 comments on commit c1ee262

Please sign in to comment.