68 lines
1.8 KiB
Python
68 lines
1.8 KiB
Python
from textual.app import App, ComposeResult
|
|
from textual.containers import VerticalScroll
|
|
from textual.widgets import Checkbox, Input, Header, Footer
|
|
|
|
from base64_random import gen_random_base64
|
|
|
|
from codupoc.todo import Todo
|
|
from codupoc.todomanager import TodoManager
|
|
|
|
class NewTodo(Input):
|
|
def on_input_submitted(self, event: Input.Submitted) -> None:
|
|
self.main_app.save_todo(event.value)
|
|
|
|
def __init__(self, main_app: Codupoc, *args, **kwargs):
|
|
super().__init__(*args, **kwargs)
|
|
self.main_app = main_app
|
|
|
|
|
|
class TodoItem(Checkbox):
|
|
def __init__(self, main_app: Codupoc, todo: Todo, *args, **kwargs):
|
|
super().__init__(*args, **kwargs)
|
|
self.main_app = main_app
|
|
self.todo = todo
|
|
|
|
|
|
class Codupoc(App[None]):
|
|
CSS_PATH = "codupoc.tcss"
|
|
|
|
BINDINGS = [
|
|
("n", "new_todo", "Add a new Todo list item"),
|
|
]
|
|
|
|
def compose(self) -> ComposeResult:
|
|
yield Header()
|
|
yield VerticalScroll(id="list_items")
|
|
yield Footer()
|
|
|
|
def action_new_todo(self):
|
|
tinput = NewTodo(self, placeholder="What needs to be done?", id="newtodo")
|
|
scroller = self.query_one("#list_items")
|
|
scroller.mount(tinput)
|
|
tinput.focus()
|
|
|
|
def save_todo(self, value: str):
|
|
tinput = self.query_one("#newtodo")
|
|
tinput.remove()
|
|
todo = Checkbox(value)
|
|
scroller = self.query_one("#list_items")
|
|
scroller.mount(todo)
|
|
todo.scroll_visible()
|
|
todo.focus()
|
|
|
|
def insert_todo(self, title: str, scroller: VerticalScroll):
|
|
ntodo = Todo(title, False)
|
|
self.todos[ntodo.lid] = ntodo
|
|
todo = TodoItem(self, ntodo, title, id=ntodo.lid)
|
|
scroller.mount(todo)
|
|
todo.focus()
|
|
|
|
|
|
def __init__(self):
|
|
super().__init__()
|
|
self.todos = {}
|
|
|
|
|
|
if __name__ == "__main__":
|
|
app = Codupoc()
|
|
app.run() |