Archived
0
0

组件消息现代化

This commit is contained in:
2025-11-09 12:04:01 +08:00
parent 330846a4a5
commit 35ed4e63be
4 changed files with 70 additions and 56 deletions

View File

@@ -1,18 +1,14 @@
from textual.widgets import (
Markdown,
Label,
Static,
Button,
)
from textual.containers import Container, Horizontal, Center
from textual.screen import Screen
from textual.widget import Widget
import heurams.kernel.particles as pt
import heurams.kernel.puzzles as pz
from .base_puzzle_widget import BasePuzzleWidget
import copy
import random
from .. import shim
from textual.message import Message
class ClozePuzzle(BasePuzzleWidget):
@@ -28,31 +24,60 @@ class ClozePuzzle(BasePuzzleWidget):
self.ans = copy.copy(self.puzzle.answer)
random.shuffle(self.ans)
class RatingChanged(Message):
def __init__(self, atom: pt.Atom, rating: int, is_correct: bool) -> None:
self.atom = atom
self.rating = rating # 评分
self.is_correct = is_correct # 是否正确
super().__init__()
class InputChanged(Message):
"""输入变化消息"""
def __init__(self, current_input: list, max_length: int) -> None:
self.current_input = current_input # 当前输入
self.max_length = max_length # 最大长度
self.progress = len(current_input) / max_length # 进度
super().__init__()
def compose(self):
yield Label(self.puzzle.wording, id="sentence")
yield Label(f"当前输入: {self.inputlist}", id="inputpreview")
for i in self.ans:
self.hashtable[str(hash(i))] = i
yield Button(i, id=f"{hash(i)}")
yield Button("退格", id=f"delete")
yield Button("退格", id="delete")
def handler(self, event, type_):
# TODO: 改动:在线错误纠正
if type_ == "button":
if event.button.id == "delete":
if len(self.inputlist) > 0:
self.inputlist.pop()
else:
return 1
else:
self.inputlist.append(self.hashtable[event.button.id[6:]])
if len(self.inputlist) < len(self.puzzle.answer):
return 1
else:
if self.inputlist == self.puzzle.answer:
shim.report_to_staging(self.atom, 4)
return 0
else:
def update_preview(self):
preview = self.query_one("#inputpreview")
preview.update(f"当前输入: {self.inputlist}") # type: ignore
self.post_message(self.InputChanged(
current_input=self.inputlist.copy(),
max_length=len(self.puzzle.answer)
))
def on_button_pressed(self, event: Button.Pressed) -> None:
button_id = event.button.id
if button_id == "delete":
if len(self.inputlist) > 0:
self.inputlist.pop()
self.update_preview()
else:
answer_text = self.hashtable[button_id]
self.inputlist.append(answer_text)
self.update_preview()
if len(self.inputlist) >= len(self.puzzle.answer):
is_correct = self.inputlist == self.puzzle.answer
rating = 4 if is_correct else 2
self.post_message(self.RatingChanged(
atom=self.atom,
rating=rating,
is_correct=is_correct
))
if not is_correct:
self.inputlist = []
shim.report_to_staging(self.atom, 2)
return 2
self.update_preview()