1
0
Fork 0

Potential multiple different action types

This commit is contained in:
Michał Rudowicz 2024-09-08 19:14:34 +02:00
parent a16e689ec5
commit c704b137a4
2 changed files with 32 additions and 14 deletions

View File

@ -2,23 +2,28 @@
"urls": [
{
"label": "example 1",
"url": "http://example.com"
"action_type": "MSG",
"action_param": "This is example 1"
},
{
"label": "example 2",
"url": "http://example.com"
"action_type": "MSG",
"action_param": "This is example 2"
},
{
"label": "example 3",
"url": "http://example.com"
"action_type": "MSG",
"action_param": "This is example 3"
},
{
"label": "example 4",
"url": "http://example.com"
"action_type": "MSG",
"action_param": "This is example 4"
},
{
"label": "example 5",
"url": "http://example.com"
"action_type": "MSG",
"action_param": "This is example 5"
}
],
"bg_color": "lime"

31
menu.py
View File

@ -3,6 +3,7 @@
import math
import argparse
import json
from enum import Enum, auto
from time import sleep
from threading import Thread
from queue import Queue, Empty
@ -18,9 +19,14 @@ class ButtonDef(TypedDict):
cbk: Callable[[], None]
class UrlDef(TypedDict):
class ActionType(Enum):
MSG = auto()
class Action(TypedDict):
label: str
url: str
action_type: ActionType
param: str
class IBoard:
@ -118,14 +124,14 @@ class MenuBoard(IBoard):
class App:
def __init__(self, urls: List[UrlDef], bg_color: str):
def __init__(self, urls: List[Action], bg_color: str):
pygame.init()
info = pygame.display.Info()
self.screen = pygame.display.set_mode((info.current_w, info.current_h), flags=pygame.FULLSCREEN)
self.clock = pygame.time.Clock()
self.running = False
self.bg_color = bg_color
buttons = list(map(lambda u: ButtonDef(text=u['label'], cbk=self.button_press_handler(u['url'])), urls))
buttons = list(map(lambda u: ButtonDef(text=u['label'], cbk=self.button_press_handler(u)), urls))
buttons.append(ButtonDef(text="Exit", cbk=self.quit))
self.board: IBoard = MenuBoard(self.get_screen_rect(), buttons, bg_color)
self.task_q = Queue()
@ -133,7 +139,7 @@ class App:
def get_screen_rect(self) -> pygame.Rect:
return pygame.Rect(0, 0, self.screen.get_width(), self.screen.get_height())
def button_press_handler(self, url: str) -> Callable[[], None]:
def show_message_handler(self, action: Action) -> Callable[[], None]:
def impl():
previous_board = self.board
@ -146,9 +152,14 @@ class App:
process_thr = Thread(target=thr_fun)
process_thr.start()
self.board = MessageBoard(self.get_screen_rect(), f"Fetching: {url}", bg_color=self.bg_color)
self.board = MessageBoard(self.get_screen_rect(), action['param'], bg_color=self.bg_color)
return impl
def button_press_handler(self, action: Action) -> Callable[[], None]:
if action['action_type'] == ActionType.MSG:
return self.show_message_handler(action)
raise NotImplementedError(action['action_type'])
def loop(self):
self.running = True
while self.running:
@ -173,10 +184,12 @@ class App:
self.running = False
def get_url_defs(config_data: dict) -> List[UrlDef]:
def get_url_defs(config_data: dict) -> List[Action]:
url_defs = []
for d in data['urls']:
url_defs.append(UrlDef(label=d['label'], url=d['url']))
url_defs.append(Action(label=d['label'],
action_type=ActionType[d['action_type']],
param=d['action_param']))
return url_defs
@ -185,7 +198,7 @@ if __name__ == '__main__':
parser.add_argument("config_path", help="Path to the config file.", type=str)
args = parser.parse_args()
url_defs: List[UrlDef] = []
url_defs: List[Action] = []
with open(args.config_path, "rb") as f:
data = json.load(f)
url_defs = get_url_defs(data)