1
0
Fork 0

Do not hardcode quitting action, make in a configurable action instead

This commit is contained in:
Michał Rudowicz 2024-09-10 17:50:10 +02:00
parent 0ecf532464
commit 0c21a3e34a
3 changed files with 22 additions and 12 deletions

View File

@ -19,3 +19,7 @@ Shows a message `action_message` for `action_param` number of seconds.
## `GET` ## `GET`
Performs a `GET` on the `action_param` URL while displaying `action_message`. Performs a `GET` on the `action_param` URL while displaying `action_message`.
## `QUIT`
Quits an application.

View File

@ -1,5 +1,5 @@
{ {
"urls": [ "actions": [
{ {
"label": "example 1", "label": "example 1",
"action_type": "MSG", "action_type": "MSG",
@ -29,6 +29,10 @@
"action_type": "GET", "action_type": "GET",
"action_param": "http://example.com/", "action_param": "http://example.com/",
"action_message": "Doing GET example" "action_message": "Doing GET example"
},
{
"label": "Quit",
"action_type": "QUIT"
} }
], ],
"color_scheme": { "color_scheme": {

24
menu.py
View File

@ -7,7 +7,7 @@ from enum import Enum, auto
from time import sleep from time import sleep
from threading import Thread from threading import Thread
from queue import Queue, Empty 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
import pygame.freetype import pygame.freetype
@ -23,15 +23,16 @@ class ButtonDef(TypedDict):
class ActionType(Enum): class ActionType(Enum):
MSG = auto() # Shows a message for about 5 seconds MSG = auto() # Shows a message for about 5 seconds
GET = auto() # Performs a HTTP GET GET = auto() # Performs a HTTP GET
QUIT = auto() # Quits an application
class Action(TypedDict): class Action(TypedDict):
label: str label: str
action_type: ActionType action_type: ActionType
action_param: str action_param: Optional[str]
action_message: str action_message: Optional[str]
class Button: class Button:
@ -162,7 +163,7 @@ class MenuBoard(IBoard):
class App: class App:
FPS = 30 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() pygame.init()
info = pygame.display.Info() info = pygame.display.Info()
flags = 0 flags = 0
@ -172,8 +173,7 @@ class App:
self.clock = pygame.time.Clock() self.clock = pygame.time.Clock()
self.running = False self.running = False
self.color_scheme = color_scheme self.color_scheme = color_scheme
buttons = list(map(lambda u: ButtonDef(text=u['label'], cbk=self.button_press_handler(u)), urls)) buttons = list(map(lambda u: ButtonDef(text=u['label'], cbk=self.button_press_handler(u)), actions))
buttons.append(ButtonDef(text="Exit", cbk=self.quit))
self.board: IBoard = MenuBoard(self.get_screen_rect(), buttons, color_scheme) self.board: IBoard = MenuBoard(self.get_screen_rect(), buttons, color_scheme)
self.task_q = Queue() self.task_q = Queue()
self.screensaver = ClockScreensaver() self.screensaver = ClockScreensaver()
@ -232,6 +232,8 @@ class App:
return self.show_message_handler(action) return self.show_message_handler(action)
elif action['action_type'] == ActionType.GET: elif action['action_type'] == ActionType.GET:
return self.get_handler(action) return self.get_handler(action)
elif action['action_type'] == ActionType.QUIT:
return self.quit
raise NotImplementedError(action['action_type']) raise NotImplementedError(action['action_type'])
def loop(self): def loop(self):
@ -269,11 +271,11 @@ class App:
def get_url_defs(config_data: dict) -> List[Action]: def get_url_defs(config_data: dict) -> List[Action]:
url_defs = [] url_defs = []
for d in data['urls']: for d in data['actions']:
url_defs.append(Action(label=d['label'], url_defs.append(Action(label=d['label'],
action_type=ActionType[d['action_type']], action_type=ActionType[d['action_type']],
action_param=d['action_param'], action_param=d.get('action_param'),
action_message=d['action_message'])) action_message=d.get('action_message')))
return url_defs return url_defs