Skip to content

Commit

Permalink
Merge pull request #41 from darrenburns/arrow-between-widgets
Browse files Browse the repository at this point in the history
More intuitive cursor movement
  • Loading branch information
darrenburns authored May 13, 2024
2 parents e67f8d1 + abee323 commit b41b2fe
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 3 deletions.
4 changes: 4 additions & 0 deletions elia_chat/screens/home_screen.py
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,10 @@ async def open_chat_screen(self, event: ChatList.ChatOpened):
chat = await self.chats_manager.get_chat(chat_id)
await self.app.push_screen(ChatScreen(chat))

@on(ChatList.CursorEscapingTop)
def cursor_escaping_top(self):
self.query_one(HomePromptInput).focus()

@on(PromptInput.PromptSubmitted)
async def create_new_chat(self, event: PromptInput.PromptSubmitted) -> None:
text = event.text
Expand Down
4 changes: 4 additions & 0 deletions elia_chat/widgets/chat.py
Original file line number Diff line number Diff line change
Expand Up @@ -264,6 +264,10 @@ async def user_chat_message_submitted(
async def on_cursor_up_from_prompt(self) -> None:
self.focus_latest_message()

@on(Chatbox.CursorEscapingBottom)
def move_focus_to_prompt(self) -> None:
self.query_one(ChatPromptInput).focus()

def get_latest_chatbox(self) -> Chatbox:
return self.query(Chatbox).last()

Expand Down
16 changes: 14 additions & 2 deletions elia_chat/widgets/chat_list.py
Original file line number Diff line number Diff line change
Expand Up @@ -61,9 +61,9 @@ class ChatList(OptionList):
),
Binding("j,down", "cursor_down", "Down", show=False),
Binding("k,up", "cursor_up", "Up", show=False),
Binding("G,end", "last", "Last", show=False),
Binding("l,enter", "select", "Select", show=False),
Binding("l,right,enter", "select", "Select", show=False),
Binding("g,home", "first", "First", show=False),
Binding("G,end", "last", "Last", show=False),
Binding("pagedown", "page_down", "Page Down", show=False),
Binding("pageup", "page_up", "Page Up", show=False),
]
Expand All @@ -72,6 +72,12 @@ class ChatList(OptionList):
class ChatOpened(Message):
chat: ChatData

class CursorEscapingTop(Message):
"""Cursor attempting to move out-of-bounds at top of list."""

class CursorEscapingBottom(Message):
"""Cursor attempting to move out-of-bounds at bottom of list."""

async def on_mount(self) -> None:
await self.reload_and_refresh()

Expand Down Expand Up @@ -131,3 +137,9 @@ def create_chat(self, chat_data: ChatData) -> None:
option_list.add_options(self.options)
option_list.highlighted = 0
self.refresh()

def action_cursor_up(self) -> None:
if self.highlighted == 0:
self.post_message(self.CursorEscapingTop())
else:
return super().action_cursor_up()
8 changes: 7 additions & 1 deletion elia_chat/widgets/chatbox.py
Original file line number Diff line number Diff line change
Expand Up @@ -151,6 +151,9 @@ class Chatbox(Widget, can_focus=True):
),
]

class CursorEscapingBottom(Message):
"""Sent when the cursor moves down from the bottom message."""

selection_mode = reactive(False, init=False)

def __init__(
Expand Down Expand Up @@ -185,7 +188,10 @@ def action_up(self) -> None:
self.screen.focus_previous(Chatbox)

def action_down(self) -> None:
self.screen.focus_next(Chatbox)
if self.parent and self is self.parent.children[-1]:
self.post_message(self.CursorEscapingBottom())
else:
self.screen.focus_next(Chatbox)

def action_select(self) -> None:
self.selection_mode = not self.selection_mode
Expand Down

0 comments on commit b41b2fe

Please sign in to comment.