Skip to content

Commit

Permalink
Cycle over previously sent messages with arrow keys(#38)
Browse files Browse the repository at this point in the history
  • Loading branch information
douglascdev committed Mar 23, 2024
1 parent 8b1e686 commit 4e7ff0a
Show file tree
Hide file tree
Showing 3 changed files with 62 additions and 2 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
Changes:
* Add button to scroll down when chat is scrolled up(#56)
* Message timestamps(#55)
* Cycle over previously sent messages with arrow keys(#38)
3 changes: 3 additions & 0 deletions hasherino/__main__.py
Original file line number Diff line number Diff line change
Expand Up @@ -236,6 +236,9 @@ async def on_kb_event(self, e: ft.KeyboardEvent):
elif e.key == "U" and e.ctrl:
await self.new_message_row.user_completion()

elif e.key in ("Arrow Up", "Arrow Down"):
await self.new_message_row.cycle_messages(e.key)

async def run(self):
self.page.window_width = await self.persistent_storage.get("window_width")
self.page.window_height = await self.persistent_storage.get("window_height")
Expand Down
60 changes: 58 additions & 2 deletions hasherino/components/new_message_row.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
import logging
from difflib import SequenceMatcher
from random import choice
from typing import Awaitable
from typing import Awaitable, Literal

import flet as ft

Expand All @@ -13,6 +13,45 @@
from hasherino.storage import AsyncKeyValueStorage


class _CycleStatus:
"""
Cycle through sent messages with arrow up and down
"""

def __init__(self, text_field: ft.TextField):
self.items: list = []
self.index: int | None = None
self.text_field = text_field

async def add(self, item):
self.items.append(item)
self.index = None

async def up(self):
if self.index is None:
if len(self.items) > 0:
self.index = max(0, len(self.items) - 1)
await self._set_text_to_index_item()
else:
self.index = max(self.index - 1, 0)
await self._set_text_to_index_item()

async def down(self):
if self.index is not None:
self.index = min(self.index + 1, len(self.items) - 1)
if self.index + 1 == len(self.items):
self.text_field.value = ""
self.index = None
await self.text_field.update_async()
else:
await self._set_text_to_index_item()

async def _set_text_to_index_item(self):
if self.index is not None:
self.text_field.value = self.items[self.index]
await self.text_field.update_async()


class NewMessageRow(ft.Row):
def __init__(
self,
Expand All @@ -38,20 +77,28 @@ def __init__(
on_submit=self.send_message_click,
on_focus=self.new_message_focus,
on_blur=self.new_message_clear_error,
on_change=self.new_message_clear_error,
on_change=self.new_message_change,
)
self.send_message = ft.IconButton(
icon=ft.icons.SEND_ROUNDED,
tooltip="Send message",
on_click=self.send_message_click,
)

self.cycle_status = _CycleStatus(self.new_message)
super().__init__([self.new_message, self.send_message])

async def clear_cycle_status(self):
self.cycle_status.index = None

async def new_message_clear_error(self, e):
e.control.error_text = ""
await self.page.update_async()

async def new_message_change(self, e):
await self.new_message_clear_error(e)
await self.clear_cycle_status()

async def emote_completion(self):
if not self.new_message.value:
return
Expand Down Expand Up @@ -133,6 +180,14 @@ async def user_completion(self):
await self.new_message.update_async()
return

async def cycle_messages(
self, direction: Literal["Arrow Up"] | Literal["Arrow Down"]
):
if direction == "Arrow Up":
await self.cycle_status.up()
elif direction == "Arrow Down":
await self.cycle_status.down()

async def new_message_focus(self, e):
if await self.persistent_storage.get("user_name"):
e.control.prefix = ft.Text(
Expand Down Expand Up @@ -212,6 +267,7 @@ async def send_message_click(self, _):
emote_map,
)
await self.chat_container_on_message(message)
await self.cycle_status.add(self.new_message.value)

if not self.page.is_ctrl_pressed:
self.new_message.value = ""
Expand Down

0 comments on commit 4e7ff0a

Please sign in to comment.