mirror of https://git.sr.ht/~michalr/menu
Display network request result on screen
This commit is contained in:
parent
8fb38ee0fd
commit
c20557cc32
24
menu.py
24
menu.py
|
@ -4,7 +4,6 @@ import math
|
|||
import argparse
|
||||
import json
|
||||
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, Optional
|
||||
|
@ -162,6 +161,7 @@ class MenuBoard(IBoard):
|
|||
|
||||
class App:
|
||||
FPS = 30
|
||||
SHOW_REQUEST_MESSAGE_FOR_AT_LEAST_S = 5
|
||||
|
||||
def __init__(self, actions: List[Action], theme: Dict, fullscreen: bool, screensaver_delay: int):
|
||||
pygame.init()
|
||||
|
@ -183,6 +183,11 @@ class App:
|
|||
def get_screen_rect(self) -> pygame.Rect:
|
||||
return pygame.Rect(0, 0, self.screen.get_width(), self.screen.get_height())
|
||||
|
||||
def show_message(self, msg: str) -> Callable[[], None]:
|
||||
def x():
|
||||
self.board = MessageBoard(self.get_screen_rect(), msg, theme=self.theme)
|
||||
return x
|
||||
|
||||
def show_message_handler(self, action: Action) -> Callable[[], None]:
|
||||
def impl():
|
||||
previous_board = self.board
|
||||
|
@ -193,15 +198,10 @@ class App:
|
|||
|
||||
try:
|
||||
sleep_time = int(action['action_param'])
|
||||
sleep(sleep_time)
|
||||
pygame.time.wait(sleep_time * 1000)
|
||||
except Exception as e:
|
||||
def f(ex):
|
||||
def x():
|
||||
self.board = MessageBoard(self.get_screen_rect(), f"Exception caught: {ex}",
|
||||
theme=self.theme)
|
||||
return x
|
||||
self.task_q.put(f(e))
|
||||
sleep(5)
|
||||
self.task_q.put(self.show_message(f"Exception caught: {e}"))
|
||||
pygame.time.wait(5 * 1000)
|
||||
finally:
|
||||
self.task_q.put(end_thr)
|
||||
|
||||
|
@ -217,14 +217,16 @@ class App:
|
|||
def thr_fun():
|
||||
def end_thr():
|
||||
self.board = previous_board
|
||||
|
||||
req_start_ts = pygame.time.get_ticks()
|
||||
ret = requests.get(action['action_param'])
|
||||
print(f"GET {action['action_param']}: {ret}")
|
||||
self.task_q.put(self.show_message(f"Request done: {ret}"))
|
||||
pygame.time.wait(max(self.SHOW_REQUEST_MESSAGE_FOR_AT_LEAST_S * 1000 - (pygame.time.get_ticks() - req_start_ts), 0))
|
||||
self.task_q.put(end_thr)
|
||||
|
||||
self.board = MessageBoard(self.get_screen_rect(), action['action_message'], theme=self.theme)
|
||||
process_thr = Thread(target=thr_fun)
|
||||
process_thr.start()
|
||||
self.board = MessageBoard(self.get_screen_rect(), action['action_message'], theme=self.theme)
|
||||
return impl
|
||||
|
||||
def button_press_handler(self, action: Action) -> Callable[[], None]:
|
||||
|
|
Loading…
Reference in New Issue