From c704b137a4fb83b14169f9e16643ad988e71c6ef Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Micha=C5=82=20Rudowicz?= Date: Sun, 8 Sep 2024 19:14:34 +0200 Subject: [PATCH] Potential multiple different action types --- example_config.json | 15 ++++++++++----- menu.py | 31 ++++++++++++++++++++++--------- 2 files changed, 32 insertions(+), 14 deletions(-) diff --git a/example_config.json b/example_config.json index 6fcd6ed..043fd50 100644 --- a/example_config.json +++ b/example_config.json @@ -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" diff --git a/menu.py b/menu.py index ad2b43c..1dfa935 100644 --- a/menu.py +++ b/menu.py @@ -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)