mirror of
https://github.com/1j01/textual-paint.git
synced 2024-12-22 06:11:37 +03:00
Half-implement menu item hotkeys
This commit is contained in:
parent
031a0a3e72
commit
edc0048ecc
13
menus.py
13
menus.py
@ -5,7 +5,7 @@ from textual.containers import Container
|
|||||||
from textual.reactive import var
|
from textual.reactive import var
|
||||||
from textual.widgets import Button, Static
|
from textual.widgets import Button, Static
|
||||||
from rich.text import Text
|
from rich.text import Text
|
||||||
from localization.i18n import markup_hotkey, get_direction
|
from localization.i18n import markup_hotkey, get_hotkey, get_direction
|
||||||
|
|
||||||
def to_snake_case(name: str) -> str:
|
def to_snake_case(name: str) -> str:
|
||||||
name = re.sub('(.)([A-Z][a-z]+)', r'\1_\2', name)
|
name = re.sub('(.)([A-Z][a-z]+)', r'\1_\2', name)
|
||||||
@ -52,6 +52,15 @@ class Menu(Container):
|
|||||||
self.close()
|
self.close()
|
||||||
if self.parent_menu:
|
if self.parent_menu:
|
||||||
self.parent_menu.focus()
|
self.parent_menu.focus()
|
||||||
|
elif event.is_printable:
|
||||||
|
# TODO: alt+hotkey for top level menus, globally.
|
||||||
|
# This is pretty useless when you have to click a menu first.
|
||||||
|
for item in self.items:
|
||||||
|
if isinstance(item, MenuItem) and item.hotkey and event.character:
|
||||||
|
if item.hotkey.lower() == event.character.lower():
|
||||||
|
item.press()
|
||||||
|
break
|
||||||
|
|
||||||
|
|
||||||
def on_button_pressed(self, event: Button.Pressed) -> None:
|
def on_button_pressed(self, event: Button.Pressed) -> None:
|
||||||
"""Called when a button is clicked or activated with the keyboard."""
|
"""Called when a button is clicked or activated with the keyboard."""
|
||||||
@ -184,6 +193,7 @@ class MenuItem(Button):
|
|||||||
) -> None:
|
) -> None:
|
||||||
"""Initialize a menu item."""
|
"""Initialize a menu item."""
|
||||||
super().__init__(markup_hotkey(name), **kwargs)
|
super().__init__(markup_hotkey(name), **kwargs)
|
||||||
|
self.hotkey: str|None = get_hotkey(name)
|
||||||
self.disabled = grayed
|
self.disabled = grayed
|
||||||
self.action = action
|
self.action = action
|
||||||
self.submenu = submenu
|
self.submenu = submenu
|
||||||
@ -203,6 +213,7 @@ class Separator(Static):
|
|||||||
def __init__(self, **kwargs: Any) -> None:
|
def __init__(self, **kwargs: Any) -> None:
|
||||||
"""Initialize a separator."""
|
"""Initialize a separator."""
|
||||||
super().__init__(mid_line, **kwargs)
|
super().__init__(mid_line, **kwargs)
|
||||||
|
self.hotkey = None
|
||||||
# self.disabled = True # This breaks scroll wheel over the separator, as of Textual 0.20.1
|
# self.disabled = True # This breaks scroll wheel over the separator, as of Textual 0.20.1
|
||||||
self.disabled = False
|
self.disabled = False
|
||||||
self.action = None
|
self.action = None
|
||||||
|
Loading…
Reference in New Issue
Block a user