#!/usr/bin/env python3 from textual.app import ComposeResult from textual.widgets import Header, Footer, Label, Static, Button from textual.containers import Center from textual.screen import Screen from textual.reactive import reactive from enum import Enum, auto from heurams.context import config_var from heurams.kernel.reactor import * import heurams.kernel.particles as pt import heurams.kernel.puzzles as pz from .. import shim class AtomState(Enum): FAILED = auto() NORMAL = auto() class MemScreen(Screen): BINDINGS = [ ("q", "pop_screen", "返回"), ("p", "prev", "复习上一个"), ("d", "toggle_dark", "改变色调"), ("v", "play_voice", "朗读"), ] if config_var.get()["quick_pass"]: BINDINGS.append(("k", "quick_pass", "跳过")) def __init__(self, atoms: list, name: str | None = None, id: str | None = None, classes: str | None = None) -> None: super().__init__(name, id, classes) self.atoms = atoms self.phaser = Phaser(atoms) #print(self.phaser.state) self.procession: Procession = self.phaser.current_procession() # type: ignore #print(self.phaser.state) self.rating = reactive(0) def puzzle_widget(self): try: self.fission = Fission(self.procession.current_atom, self.phaser.state) puzzle_info = next(self.fission.generate()) print(puzzle_info) return shim.puzzle2widget[puzzle_info["puzzle"]](atom = self.procession.current_atom, alia = puzzle_info["alia"]) except (KeyError, StopIteration, AttributeError) as e: print(f"调度展开出错: {e}") return Static("无法生成谜题") #print(shim.puzzle2widget[puzzle_info["puzzle"]]) def compose(self) -> ComposeResult: yield Header(show_clock=True) with Center(): yield Static(f"当前进度: {self.procession.process()}/{self.procession.total_length()}") #self.mount(self.current_widget()) # type: ignore yield Container(id="puzzle-container") yield Button("重新学习此单元", id="re-recognize", variant="warning") yield Footer() def load_puzzle(self): container = self.query_one("puzzle-container") for i in puz_container.children: i.remove() container.mount(self.puzzle_widget()) def on_button_pressed(self, event): event.stop() def watch_rating(self, old_value, new_value) -> None: forwards = 1 if new_value >= 4 else 0 if forwards: self.procession.forward(1) self.load_puzzle() def action_play_voice(self): """朗读当前内容""" pass def action_toggle_dark(self): self.app.action_toggle_dark() def action_pop_screen(self): self.app.pop_screen()