You've already forked HeurAMS-legacy
144 lines
5.1 KiB
Python
144 lines
5.1 KiB
Python
from textual.app import App, ComposeResult
|
|
from textual.events import Event
|
|
from textual.widgets import Collapsible, Header, Footer, Markdown, ListView, ListItem, Label, Static, Button
|
|
from textual.containers import Container, Horizontal, Center
|
|
from textual.screen import Screen
|
|
from textual.widget import Widget
|
|
import pathlib
|
|
from typing import Tuple, Dict
|
|
import particles as pt
|
|
import uuid
|
|
from functools import wraps
|
|
|
|
class Composition():
|
|
def __init__(self, screen: Screen, reactor, atom: Tuple[pt.Electron, pt.Nucleon, Dict]):
|
|
self.screen = screen
|
|
self.atom = atom
|
|
from reactor import Reactor
|
|
self.reactor: Reactor = reactor
|
|
self.reg = dict()
|
|
def regid(self, id_):
|
|
self.reg[id_] = id_ + str(uuid.uuid4())
|
|
return self.reg[id_]
|
|
def getid(self, id_):
|
|
if id_ not in self.reg.keys():
|
|
return "None"
|
|
return self.reg[id_]
|
|
def compose(self):
|
|
yield Label("示例标签", id="testlabel")
|
|
yield Button("示例按钮", id="testbtn")
|
|
def handler(self, event):
|
|
print(event.button.id)
|
|
self.screen.query_one("#testlabel", Label).update("hi")
|
|
|
|
class Placeholder(Composition):
|
|
def __init__(self, screen: Screen):
|
|
self.screen = screen
|
|
def compose(self):
|
|
yield Label("示例标签", id="testlabel")
|
|
yield Button("示例按钮", id="testbtn", classes="choice")
|
|
def handler(self, event):
|
|
print(event.button.id)
|
|
self.screen.query_one("#testlabel", Label).update("hi")
|
|
|
|
class Recognition(Composition):
|
|
def __init__(self, screen: Screen, reactor, atom: Tuple[pt.Electron, pt.Nucleon, Dict]):
|
|
super().__init__(screen, reactor, atom)
|
|
def compose(self):
|
|
with Center():
|
|
yield Static(f"[dim]{self.atom[1]['translation']}[/]")
|
|
yield Label(f"")
|
|
with Center():
|
|
yield Label(f"[b][b]{self.atom[1]['content']}[/][/]", id=self.regid("sentence")) # 致敬传奇去重串 uuid
|
|
#with Collapsible(title="附加信息", collapsed=True):
|
|
for i in self.atom[2]["testdata"]["additional_inf"]:
|
|
if self.atom[1][i]:
|
|
print(type(self.atom[1][i]))
|
|
print(self.atom[1][i])
|
|
if isinstance(self.atom[1][i], list):
|
|
for j in self.atom[1][i]:
|
|
print(999)
|
|
yield Markdown(f"### {self.atom[2]["keydata"][i]}: {j}")
|
|
continue
|
|
if isinstance(self.atom[1][i], Dict):
|
|
t = ""
|
|
for j, k in self.atom[1][i].items():
|
|
t += f"> **{j}**: {k} \n"
|
|
yield Markdown(t, id=self.regid("tran"))
|
|
with Center():
|
|
yield Button("我已知晓", id=self.regid("ok"))
|
|
def handler(self, event):
|
|
if event.button.id == self.getid("ok"):
|
|
print(1)
|
|
""""""#assessment = self.reactor.report(self.reactor.current_atom, -1)
|
|
return 0
|
|
|
|
class BasicEvaluation(Composition):
|
|
def __init__(self, screen: Screen, reactor, atom: Tuple[pt.Electron, pt.Nucleon, Dict]):
|
|
super().__init__(screen, reactor, atom)
|
|
def compose(self):
|
|
yield Label(self.atom[1]["content"], id="sentence")
|
|
for i in self.atom[2]["testdata"]["additional_inf"]:
|
|
yield Label(f"{self.atom[2]["keydata"][i]}: {self.atom[1][i]}", id=f"label_{i}")
|
|
yield Button("我已知晓", id="ok")
|
|
def handler(self, event):
|
|
if event.button.id == "ok":
|
|
return 1
|
|
|
|
class FillBlank(Composition):
|
|
def __init__(self, screen: Screen, atom: Tuple[pt.Electron, pt.Nucleon, Dict]):
|
|
""""""#super().__init__(screen, atom)
|
|
def compose(self):
|
|
yield Label(self.atom[1]["content"], id="sentence")
|
|
for i in self.atom[2]["testdata"]["additional_inf"]:
|
|
yield Label(f"{self.atom[2]["keydata"][i]}: {self.atom[1][i]}", id=f"label_{i}")
|
|
yield Button("我已知晓", id="ok")
|
|
def handler(self, event):
|
|
if event.button.id == "ok":
|
|
return 1
|
|
|
|
|
|
registry = {
|
|
"sample": Composition,
|
|
"recognition": Recognition,
|
|
"fill_blank_test": FillBlank,
|
|
"draw_card_test": BasicEvaluation,
|
|
"basic_evaluation": BasicEvaluation,
|
|
}
|
|
|
|
|
|
# TEST
|
|
|
|
class TestScreen(Screen):
|
|
def __init__(self):
|
|
super().__init__(name=None, id=None, classes=None)
|
|
self.comp = Recognition(self, None, pt.Atom.advanced_placeholder())
|
|
def compose(self) -> ComposeResult:
|
|
yield Header(show_clock=True)
|
|
yield from self.comp.compose()
|
|
yield Footer()
|
|
def on_mount(self) -> None:
|
|
pass
|
|
|
|
def on_button_pressed(self, event: Event) -> None:
|
|
self.comp.handler(event)
|
|
|
|
def action_quit_app(self) -> None:
|
|
self.app.exit()
|
|
|
|
class AppLauncher(App):
|
|
CSS_PATH = "styles.tcss"
|
|
TITLE = '测试布局'
|
|
BINDINGS = [("escape", "quit", "退出"), ("d", "toggle_dark", "改变色调")]
|
|
SCREENS = {
|
|
"testscreen": TestScreen,
|
|
}
|
|
|
|
def on_mount(self) -> None:
|
|
self.action_toggle_dark()
|
|
self.push_screen("testscreen")
|
|
|
|
if __name__ == "__main__":
|
|
app = AppLauncher()
|
|
app.run()
|