diff --git a/elia_chat/screens/home_screen.py b/elia_chat/screens/home_screen.py index c64df6d..5e928c6 100644 --- a/elia_chat/screens/home_screen.py +++ b/elia_chat/screens/home_screen.py @@ -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 diff --git a/elia_chat/widgets/chat.py b/elia_chat/widgets/chat.py index d5992a5..857f6a2 100644 --- a/elia_chat/widgets/chat.py +++ b/elia_chat/widgets/chat.py @@ -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() diff --git a/elia_chat/widgets/chat_list.py b/elia_chat/widgets/chat_list.py index 3d53614..4e33173 100644 --- a/elia_chat/widgets/chat_list.py +++ b/elia_chat/widgets/chat_list.py @@ -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), ] @@ -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() @@ -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() diff --git a/elia_chat/widgets/chatbox.py b/elia_chat/widgets/chatbox.py index 2d5c6c1..dbded9b 100644 --- a/elia_chat/widgets/chatbox.py +++ b/elia_chat/widgets/chatbox.py @@ -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__( @@ -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