Skip to content

Commit

Permalink
refactor: move everything to handle_keypress
Browse files Browse the repository at this point in the history
fix: Avoid conflict with built-in handle_key
  • Loading branch information
kraanzu committed Sep 24, 2024
1 parent 81d2a07 commit 4c946e1
Show file tree
Hide file tree
Showing 5 changed files with 20 additions and 12 deletions.
14 changes: 10 additions & 4 deletions dooit/ui/api/dooit_api.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
from collections import defaultdict
from typing import TYPE_CHECKING, List, Callable
from typing import TYPE_CHECKING, List, Callable, Optional

from dooit.ui.api.plug import PluginManager
from dooit.ui.api.components import TodoLayout, WorkspaceLayout
Expand All @@ -14,13 +14,15 @@
if TYPE_CHECKING:
from ..tui import Dooit

KeyBindType = defaultdict[str, defaultdict[str, Optional[Callable]]]


class DooitAPI:
def __init__(self, app: "Dooit") -> None:
self.app = app
self.plugin_manager = PluginManager()
self.plugin_manager.scan()
self.keybinds = defaultdict(lambda: defaultdict(lambda: lambda: None))
self.keybinds: KeyBindType = defaultdict(lambda: defaultdict(lambda: None))
self.css_manager = CssManager()

self.css_manager.refresh_css()
Expand All @@ -37,8 +39,12 @@ def __set_key(self, mode: str, key: str, callback: Callable) -> None:
def set_key_normal(self, key: str, callback: Callable) -> None:
self.__set_key("NORMAL", key, callback)

def handle_key(self, key: str) -> None:
self.keybinds[self.bar_mode][key]()
async def handle_key(self, key: str) -> None:
func = self.keybinds[self.bar_mode][key]
if func is not None:
func()
else:
await self.focused.handle_keypress(key)

def trigger_event(self, event: DooitEvent):
event_name = event.snake_case
Expand Down
2 changes: 1 addition & 1 deletion dooit/ui/screens/index.py
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ async def handle_key(self, event: events.Key) -> bool:
return True

key = self.resolve_key(event)
self.api.handle_key(key)
await self.api.handle_key(key)
return True

async def clear_right(self) -> None:
Expand Down
4 changes: 2 additions & 2 deletions dooit/ui/widgets/renderers/base_renderer.py
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,6 @@ def stop_edit(self):
getattr(self, self.editing).stop_edit()
self.editing = ""

def handle_key(self, event: events.Key) -> bool:
getattr(self, self.editing).keypress(event.key)
def handle_keypress(self, key: str) -> bool:
getattr(self, self.editing).keypress(key)
return True
4 changes: 4 additions & 0 deletions dooit/ui/widgets/trees/base_tree.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
from collections import defaultdict
from typing import TYPE_CHECKING, Union
from textual.app import events
from textual.widgets import OptionList
from textual.widgets.option_list import Option

Expand Down Expand Up @@ -41,3 +42,6 @@ def action_cursor_up(self) -> None:
return

return super().action_cursor_up()

async def handle_keypress(self, key: str) -> bool:
return True
8 changes: 3 additions & 5 deletions dooit/ui/widgets/trees/model_tree.py
Original file line number Diff line number Diff line change
Expand Up @@ -128,13 +128,12 @@ def reset_state(self):
"""
self.set_filter("")

async def handle_key(self, event: events.Key) -> bool:
key = event.key
async def handle_keypress(self, key: str) -> bool:
if self.is_editing:
if key == "escape":
self.stop_edit()
else:
self.current.handle_key(event)
self.current.handle_keypress(key)

self.update_current_prompt()
self.refresh_options()
Expand All @@ -143,12 +142,11 @@ async def handle_key(self, event: events.Key) -> bool:
if key == "escape":
self.reset_state()

return await super().handle_key(event)
return await super().handle_keypress(key)

def refresh_options(self) -> None:
for i in self._options:
assert i.id is not None

i.set_prompt(self._renderers[i.id].prompt)

self._refresh_lines()
Expand Down

0 comments on commit 4c946e1

Please sign in to comment.