From 0bf24230a00ebaeb87071e3d059a465207f8f245 Mon Sep 17 00:00:00 2001 From: Max Erenberg Date: Wed, 8 Sep 2021 04:10:21 +0000 Subject: [PATCH] add global quit button --- ceo/tui/CeoFrame.py | 32 ++++++++++++++++++++++++++++++-- ceo/tui/ConfirmView.py | 1 + ceo/tui/ResultView.py | 1 + ceo/tui/WelcomeView.py | 12 ++++++------ 4 files changed, 38 insertions(+), 8 deletions(-) diff --git a/ceo/tui/CeoFrame.py b/ceo/tui/CeoFrame.py index 8084c62..c18e0c4 100644 --- a/ceo/tui/CeoFrame.py +++ b/ceo/tui/CeoFrame.py @@ -1,5 +1,8 @@ -from asciimatics.exceptions import NextScene -from asciimatics.widgets import Frame, Layout, Divider, Button, Label +from asciimatics.event import KeyboardEvent +from asciimatics.exceptions import NextScene, StopApplication +from asciimatics.screen import Screen +from asciimatics.widgets import Frame, Layout, Divider, Button, Label, \ + PopUpDialog class CeoFrame(Frame): @@ -14,6 +17,7 @@ class CeoFrame(Frame): title=None, save_data=False, # whether to save widget state for resizing has_dynamic_layouts=False, # whether layouts are created on load + escape_on_q=False, # whether to quit when 'q' is pressed ): super().__init__( screen, @@ -30,6 +34,9 @@ class CeoFrame(Frame): self._name = name self._loaded = False self._has_dynamic_layouts = has_dynamic_layouts + self._quit_keys = [Screen.KEY_ESCAPE] + if escape_on_q: + self._quit_keys.append(ord('q')) # sanity check if save_data: assert name in model.viewdata @@ -138,3 +145,24 @@ class CeoFrame(Frame): def clear_flash_message(self): self.flash_message('') + + def process_event(self, event): + if not isinstance(event, KeyboardEvent): + return super().process_event(event) + c = event.key_code + # Stop on 'q' or 'Esc' + if c in self._quit_keys: + self._scene.add_effect(PopUpDialog( + self.screen, + 'Are you sure you want to quit?', + ['Yes', 'No'], + has_shadow=True, + on_close=self._quit_on_yes, + )) + return super().process_event(event) + + @staticmethod + def _quit_on_yes(selected): + # Yes is the first button + if selected == 0: + raise StopApplication("User terminated app") diff --git a/ceo/tui/ConfirmView.py b/ceo/tui/ConfirmView.py index be313f5..d3af5a9 100644 --- a/ceo/tui/ConfirmView.py +++ b/ceo/tui/ConfirmView.py @@ -9,6 +9,7 @@ class ConfirmView(CeoFrame): screen, height, width, model, 'Confirm', on_load=self._confirmview_on_load, title='Confirmation', has_dynamic_layouts=True, + escape_on_q=True, ) def _add_line(self, text: str = ''): diff --git a/ceo/tui/ResultView.py b/ceo/tui/ResultView.py index dcbf163..0a414bb 100644 --- a/ceo/tui/ResultView.py +++ b/ceo/tui/ResultView.py @@ -11,6 +11,7 @@ class ResultView(CeoFrame): screen, height, width, model, 'Result', on_load=self._resultview_on_load, title='Result', has_dynamic_layouts=True, + escape_on_q=True, ) # TODO: deduplicate this from ConfirmView diff --git a/ceo/tui/WelcomeView.py b/ceo/tui/WelcomeView.py index b4ff240..5271199 100644 --- a/ceo/tui/WelcomeView.py +++ b/ceo/tui/WelcomeView.py @@ -1,18 +1,18 @@ import functools -from asciimatics.widgets import Frame, ListBox, Layout, Divider, Button, Label +from asciimatics.widgets import ListBox, Layout, Divider, Button, Label from asciimatics.exceptions import NextScene, StopApplication +from .CeoFrame import CeoFrame -class WelcomeView(Frame): + +class WelcomeView(CeoFrame): def __init__(self, screen, width, height, model): super().__init__( - screen, - height, - width, + screen, height, width, model, 'Welcome', title='CSC Electronic Office', + escape_on_q=True, ) - self._model = model members_menu_items = [ ('Add member', 'AddUser'), ('Add club rep', 'AddUser'),