mirror of https://git.sr.ht/~michalr/menu
Potential multiple different action types
This commit is contained in:
parent
a16e689ec5
commit
c704b137a4
|
@ -2,23 +2,28 @@
|
||||||
"urls": [
|
"urls": [
|
||||||
{
|
{
|
||||||
"label": "example 1",
|
"label": "example 1",
|
||||||
"url": "http://example.com"
|
"action_type": "MSG",
|
||||||
|
"action_param": "This is example 1"
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
"label": "example 2",
|
"label": "example 2",
|
||||||
"url": "http://example.com"
|
"action_type": "MSG",
|
||||||
|
"action_param": "This is example 2"
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
"label": "example 3",
|
"label": "example 3",
|
||||||
"url": "http://example.com"
|
"action_type": "MSG",
|
||||||
|
"action_param": "This is example 3"
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
"label": "example 4",
|
"label": "example 4",
|
||||||
"url": "http://example.com"
|
"action_type": "MSG",
|
||||||
|
"action_param": "This is example 4"
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
"label": "example 5",
|
"label": "example 5",
|
||||||
"url": "http://example.com"
|
"action_type": "MSG",
|
||||||
|
"action_param": "This is example 5"
|
||||||
}
|
}
|
||||||
],
|
],
|
||||||
"bg_color": "lime"
|
"bg_color": "lime"
|
||||||
|
|
31
menu.py
31
menu.py
|
@ -3,6 +3,7 @@
|
||||||
import math
|
import math
|
||||||
import argparse
|
import argparse
|
||||||
import json
|
import json
|
||||||
|
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
|
||||||
|
@ -18,9 +19,14 @@ class ButtonDef(TypedDict):
|
||||||
cbk: Callable[[], None]
|
cbk: Callable[[], None]
|
||||||
|
|
||||||
|
|
||||||
class UrlDef(TypedDict):
|
class ActionType(Enum):
|
||||||
|
MSG = auto()
|
||||||
|
|
||||||
|
|
||||||
|
class Action(TypedDict):
|
||||||
label: str
|
label: str
|
||||||
url: str
|
action_type: ActionType
|
||||||
|
param: str
|
||||||
|
|
||||||
|
|
||||||
class IBoard:
|
class IBoard:
|
||||||
|
@ -118,14 +124,14 @@ class MenuBoard(IBoard):
|
||||||
|
|
||||||
|
|
||||||
class App:
|
class App:
|
||||||
def __init__(self, urls: List[UrlDef], bg_color: str):
|
def __init__(self, urls: List[Action], bg_color: str):
|
||||||
pygame.init()
|
pygame.init()
|
||||||
info = pygame.display.Info()
|
info = pygame.display.Info()
|
||||||
self.screen = pygame.display.set_mode((info.current_w, info.current_h), flags=pygame.FULLSCREEN)
|
self.screen = pygame.display.set_mode((info.current_w, info.current_h), flags=pygame.FULLSCREEN)
|
||||||
self.clock = pygame.time.Clock()
|
self.clock = pygame.time.Clock()
|
||||||
self.running = False
|
self.running = False
|
||||||
self.bg_color = bg_color
|
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))
|
buttons.append(ButtonDef(text="Exit", cbk=self.quit))
|
||||||
self.board: IBoard = MenuBoard(self.get_screen_rect(), buttons, bg_color)
|
self.board: IBoard = MenuBoard(self.get_screen_rect(), buttons, bg_color)
|
||||||
self.task_q = Queue()
|
self.task_q = Queue()
|
||||||
|
@ -133,7 +139,7 @@ class App:
|
||||||
def get_screen_rect(self) -> pygame.Rect:
|
def get_screen_rect(self) -> pygame.Rect:
|
||||||
return pygame.Rect(0, 0, self.screen.get_width(), self.screen.get_height())
|
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():
|
def impl():
|
||||||
previous_board = self.board
|
previous_board = self.board
|
||||||
|
|
||||||
|
@ -146,9 +152,14 @@ class App:
|
||||||
|
|
||||||
process_thr = Thread(target=thr_fun)
|
process_thr = Thread(target=thr_fun)
|
||||||
process_thr.start()
|
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
|
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):
|
def loop(self):
|
||||||
self.running = True
|
self.running = True
|
||||||
while self.running:
|
while self.running:
|
||||||
|
@ -173,10 +184,12 @@ class App:
|
||||||
self.running = False
|
self.running = False
|
||||||
|
|
||||||
|
|
||||||
def get_url_defs(config_data: dict) -> List[UrlDef]:
|
def get_url_defs(config_data: dict) -> List[Action]:
|
||||||
url_defs = []
|
url_defs = []
|
||||||
for d in data['urls']:
|
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
|
return url_defs
|
||||||
|
|
||||||
|
|
||||||
|
@ -185,7 +198,7 @@ if __name__ == '__main__':
|
||||||
parser.add_argument("config_path", help="Path to the config file.", type=str)
|
parser.add_argument("config_path", help="Path to the config file.", type=str)
|
||||||
args = parser.parse_args()
|
args = parser.parse_args()
|
||||||
|
|
||||||
url_defs: List[UrlDef] = []
|
url_defs: List[Action] = []
|
||||||
with open(args.config_path, "rb") as f:
|
with open(args.config_path, "rb") as f:
|
||||||
data = json.load(f)
|
data = json.load(f)
|
||||||
url_defs = get_url_defs(data)
|
url_defs = get_url_defs(data)
|
||||||
|
|
Loading…
Reference in New Issue