feat(interface): 组件自动聚焦与键盘操作改进

This commit is contained in:
2026-04-22 22:54:25 +08:00
parent c2a1867c49
commit f50c19ba82
24 changed files with 173 additions and 12258 deletions

View File

@@ -5,6 +5,7 @@ from typing import TypedDict
from textual.containers import ScrollableContainer
from textual.widget import Widget
from textual.widgets import Button, Label, Markdown
from textual.events import Key
import heurams.kernel.particles as pt
import heurams.kernel.puzzles as pz
@@ -50,6 +51,7 @@ class ClozePuzzle(BasePuzzleWidget):
self.hashtable = {}
self.alia = alia
self._load()
self.btn_shortcuts = {}
self.hashmap = dict()
def _load(self):
@@ -67,15 +69,24 @@ class ClozePuzzle(BasePuzzleWidget):
yield Label(self.puzzle.wording, id="sentence")
yield Markdown(f"> {self.listprint(self.inputlist)}", id="inputpreview")
# 渲染当前问题的选项
with ScrollableContainer(id="btn-container"):
with ScrollableContainer(id="btn-container") as s:
c = 0
for i in self.ans:
h = str(hash(i))
if hash(i) in self.hashmap.keys():
continue
c += 1
self.hashmap[h] = i
btnid = f"sel000-{h}"
logger.debug(f"建立按钮 {btnid}")
yield Button(i, id=f"{btnid}")
self.btn_shortcuts[f'{c}'] = btnid
yield Button(f'[{c}] {i}', id=f"{btnid}")
s.focus()
yield Button("退格", id="delete")
self.btn_shortcuts[f'0'] = 'delete'
self.btn_shortcuts[f'backspace'] = 'delete'
self.btn_shortcuts[f'delete'] = 'delete'
def listprint(self, lst):
s = ""
@@ -117,3 +128,10 @@ class ClozePuzzle(BasePuzzleWidget):
pass
else:
self.atom.minimize(rating)
def on_key(self, event: Key) -> None:
self.notify(event.key)
if event.key in self.btn_shortcuts:
btn_id = self.btn_shortcuts.get(event.key)
btn_id = '#' + btn_id
self.query_one(btn_id, Button).press()