|
| 1 | +from textual.app import App, on |
| 2 | +from textual.containers import Grid, Horizontal, Vertical |
| 3 | +from textual.screen import Screen |
| 4 | +from textual.widgets import ( |
| 5 | + Button, |
| 6 | + DataTable, |
| 7 | + Footer, |
| 8 | + Header, |
| 9 | + Input, |
| 10 | + Label, |
| 11 | + Static, |
| 12 | +) |
| 13 | + |
| 14 | + |
| 15 | +class ContactsApp(App): |
| 16 | + CSS_PATH = "rpcontacts.tcss" |
| 17 | + BINDINGS = [ |
| 18 | + ("m", "toggle_dark", "Toggle dark mode"), |
| 19 | + ("a", "add", "Add"), |
| 20 | + ("d", "delete", "Delete"), |
| 21 | + ("c", "clear_all", "Clear All"), |
| 22 | + ("q", "request_quit", "Quit"), |
| 23 | + ] |
| 24 | + |
| 25 | + def __init__(self, db): |
| 26 | + super().__init__() |
| 27 | + self.db = db |
| 28 | + |
| 29 | + def compose(self): |
| 30 | + yield Header() |
| 31 | + contacts_list = DataTable(classes="contacts-list") |
| 32 | + contacts_list.focus() |
| 33 | + contacts_list.add_columns("Name", "Phone", "Email") |
| 34 | + contacts_list.cursor_type = "row" |
| 35 | + contacts_list.zebra_stripes = True |
| 36 | + add_button = Button("Add", variant="success", id="add") |
| 37 | + add_button.focus() |
| 38 | + buttons_panel = Vertical( |
| 39 | + add_button, |
| 40 | + Button("Delete", variant="warning", id="delete"), |
| 41 | + Static(classes="separator"), |
| 42 | + Button("Clear All", variant="error", id="clear"), |
| 43 | + classes="buttons-panel", |
| 44 | + ) |
| 45 | + yield Horizontal(contacts_list, buttons_panel) |
| 46 | + yield Footer() |
| 47 | + |
| 48 | + def on_mount(self): |
| 49 | + self.title = "RP Contacts" |
| 50 | + self.sub_title = "A Contacts Book App With Textual & Python" |
| 51 | + self._load_contacts() |
| 52 | + |
| 53 | + def _load_contacts(self): |
| 54 | + contacts_list = self.query_one(DataTable) |
| 55 | + for contact_data in self.db.get_all_contacts(): |
| 56 | + id, *contact = contact_data |
| 57 | + contacts_list.add_row(*contact, key=id) |
| 58 | + |
| 59 | + def action_toggle_dark(self): |
| 60 | + self.dark = not self.dark |
| 61 | + |
| 62 | + def action_request_quit(self): |
| 63 | + def check_answer(accepted): |
| 64 | + if accepted: |
| 65 | + self.exit() |
| 66 | + |
| 67 | + self.push_screen(QuestionDialog("Do you want to quit?"), check_answer) |
| 68 | + |
| 69 | + @on(Button.Pressed, "#add") |
| 70 | + def action_add(self): |
| 71 | + def check_contact(contact_data): |
| 72 | + if contact_data: |
| 73 | + self.db.add_contact(contact_data) |
| 74 | + id, *contact = self.db.get_last_contact() |
| 75 | + self.query_one(DataTable).add_row(*contact, key=id) |
| 76 | + |
| 77 | + self.push_screen(InputDialog(), check_contact) |
| 78 | + |
| 79 | + |
| 80 | +class QuestionDialog(Screen): |
| 81 | + def __init__(self, message, *args, **kwargs): |
| 82 | + super().__init__(*args, **kwargs) |
| 83 | + self.message = message |
| 84 | + |
| 85 | + def compose(self): |
| 86 | + no_button = Button("No", variant="primary", id="no") |
| 87 | + no_button.focus() |
| 88 | + |
| 89 | + yield Grid( |
| 90 | + Label(self.message, id="question"), |
| 91 | + Button("Yes", variant="error", id="yes"), |
| 92 | + no_button, |
| 93 | + id="question-dialog", |
| 94 | + ) |
| 95 | + |
| 96 | + def on_button_pressed(self, event): |
| 97 | + if event.button.id == "yes": |
| 98 | + self.dismiss(True) |
| 99 | + else: |
| 100 | + self.dismiss(False) |
| 101 | + |
| 102 | + |
| 103 | +class InputDialog(Screen): |
| 104 | + def compose(self): |
| 105 | + yield Grid( |
| 106 | + Label("Add Contact", id="title"), |
| 107 | + Label("Name:", classes="label"), |
| 108 | + Input(placeholder="Contact Name", classes="input", id="name"), |
| 109 | + Label("Phone:", classes="label"), |
| 110 | + Input(placeholder="Contact Phone", classes="input", id="phone"), |
| 111 | + Label("Email:", classes="label"), |
| 112 | + Input(placeholder="Contact Email", classes="input", id="email"), |
| 113 | + Static(), |
| 114 | + Button("Cancel", variant="warning", id="cancel"), |
| 115 | + Button("Ok", variant="success", id="ok"), |
| 116 | + id="input-dialog", |
| 117 | + ) |
| 118 | + |
| 119 | + def on_button_pressed(self, event): |
| 120 | + if event.button.id == "ok": |
| 121 | + name = self.query_one("#name", Input).value |
| 122 | + phone = self.query_one("#phone", Input).value |
| 123 | + email = self.query_one("#email", Input).value |
| 124 | + self.dismiss((name, phone, email)) |
| 125 | + else: |
| 126 | + self.dismiss(()) |
0 commit comments