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`
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",
"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": {

24
menu.py
View File

@ -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