diff --git a/README.md b/README.md index 7361018..49cf46a 100644 --- a/README.md +++ b/README.md @@ -19,3 +19,7 @@ Shows a message `action_message` for `action_param` number of seconds. ## `GET` Performs a `GET` on the `action_param` URL while displaying `action_message`. + +## `QUIT` + +Quits an application. diff --git a/example_config.json b/example_config.json index 2df9329..36ec1be 100644 --- a/example_config.json +++ b/example_config.json @@ -1,5 +1,5 @@ { - "urls": [ + "actions": [ { "label": "example 1", "action_type": "MSG", @@ -29,6 +29,10 @@ "action_type": "GET", "action_param": "http://example.com/", "action_message": "Doing GET example" + }, + { + "label": "Quit", + "action_type": "QUIT" } ], "color_scheme": { diff --git a/menu.py b/menu.py index 30bc96b..af931c9 100644 --- a/menu.py +++ b/menu.py @@ -7,7 +7,7 @@ from enum import Enum, auto from time import sleep from threading import Thread from queue import Queue, Empty -from typing import Tuple, Callable, TypedDict, List, Dict +from typing import Tuple, Callable, TypedDict, List, Dict, Optional import pygame import pygame.freetype @@ -23,15 +23,16 @@ class ButtonDef(TypedDict): class ActionType(Enum): - MSG = auto() # Shows a message for about 5 seconds - GET = auto() # Performs a HTTP GET + MSG = auto() # Shows a message for about 5 seconds + GET = auto() # Performs a HTTP GET + QUIT = auto() # Quits an application class Action(TypedDict): label: str action_type: ActionType - action_param: str - action_message: str + action_param: Optional[str] + action_message: Optional[str] class Button: @@ -162,7 +163,7 @@ class MenuBoard(IBoard): class App: FPS = 30 - def __init__(self, urls: List[Action], color_scheme: Dict, fullscreen: bool, screensaver_delay: int): + def __init__(self, actions: List[Action], color_scheme: Dict, fullscreen: bool, screensaver_delay: int): pygame.init() info = pygame.display.Info() flags = 0 @@ -172,8 +173,7 @@ class App: self.clock = pygame.time.Clock() self.running = False self.color_scheme = color_scheme - buttons = list(map(lambda u: ButtonDef(text=u['label'], cbk=self.button_press_handler(u)), urls)) - buttons.append(ButtonDef(text="Exit", cbk=self.quit)) + buttons = list(map(lambda u: ButtonDef(text=u['label'], cbk=self.button_press_handler(u)), actions)) self.board: IBoard = MenuBoard(self.get_screen_rect(), buttons, color_scheme) self.task_q = Queue() self.screensaver = ClockScreensaver() @@ -232,6 +232,8 @@ class App: return self.show_message_handler(action) elif action['action_type'] == ActionType.GET: return self.get_handler(action) + elif action['action_type'] == ActionType.QUIT: + return self.quit raise NotImplementedError(action['action_type']) def loop(self): @@ -269,11 +271,11 @@ class App: def get_url_defs(config_data: dict) -> List[Action]: url_defs = [] - for d in data['urls']: + for d in data['actions']: url_defs.append(Action(label=d['label'], action_type=ActionType[d['action_type']], - action_param=d['action_param'], - action_message=d['action_message'])) + action_param=d.get('action_param'), + action_message=d.get('action_message'))) return url_defs