diff --git a/example_config.json b/example_config.json index 2884911..6fcd6ed 100644 --- a/example_config.json +++ b/example_config.json @@ -20,5 +20,6 @@ "label": "example 5", "url": "http://example.com" } - ] + ], + "bg_color": "lime" } \ No newline at end of file diff --git a/menu.py b/menu.py index 84c0e14..ad2b43c 100644 --- a/menu.py +++ b/menu.py @@ -63,35 +63,35 @@ class Button: class MessageBoard(IBoard): - def __init__(self, screen_rect: pygame.Rect, text: str): + def __init__(self, screen_rect: pygame.Rect, text: str, bg_color: str): self.text = text self.font = pygame.freetype.SysFont(name="Sans", size=30) text_rect = self.font.get_rect(self.text) self.text_pos = pygame.Rect((screen_rect.left + (screen_rect.width - text_rect.width)/2), (screen_rect.top + (screen_rect.height - text_rect.height)/2), text_rect.width, text_rect.height) + self.bg_color = bg_color def handle_event(self, _): pass def draw(self, screen: pygame.Surface): - screen.fill("purple") + screen.fill(self.bg_color) self.font.render_to(screen, self.text_pos, self.text, fgcolor="black") class MenuBoard(IBoard): BUTTON_MARGINS = 10 - def __init__(self, screen_rect: pygame.Rect, buttons: List[ButtonDef]): + def __init__(self, screen_rect: pygame.Rect, buttons: List[ButtonDef], bg_color: str): self.rows = math.floor(math.sqrt(len(buttons))) self.cols = math.ceil(math.sqrt(len(buttons))) if (self.rows * self.cols) < len(buttons): # extra row if buttons don't fit self.rows += 1 button_positions = self.generate_button_positions(screen_rect) - self.buttons = list(map(lambda d: Button(next(button_positions), - f"Button {d['text']}", - d['cbk']), buttons)) + self.buttons = list(map(lambda d: Button(next(button_positions), d['text'], d['cbk']), buttons)) + self.bg_color = bg_color def generate_button_positions(self, screen_rect: pygame.Rect): current_button_row = 0 @@ -112,21 +112,22 @@ class MenuBoard(IBoard): b.handle_event(event) def draw(self, screen: pygame.Surface): - screen.fill("purple") + screen.fill(self.bg_color) for b in self.buttons: b.draw(screen) class App: - def __init__(self, urls: List[UrlDef]): + def __init__(self, urls: List[UrlDef], 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.append(ButtonDef(text="Exit", cbk=self.quit)) - self.board: IBoard = MenuBoard(self.get_screen_rect(), buttons) + self.board: IBoard = MenuBoard(self.get_screen_rect(), buttons, bg_color) self.task_q = Queue() def get_screen_rect(self) -> pygame.Rect: @@ -145,7 +146,7 @@ class App: process_thr = Thread(target=thr_fun) process_thr.start() - self.board = MessageBoard(self.get_screen_rect(), f"Fetching: {url}") + self.board = MessageBoard(self.get_screen_rect(), f"Fetching: {url}", bg_color=self.bg_color) return impl def loop(self): @@ -168,16 +169,14 @@ class App: pygame.quit() def quit(self): - self.board = MessageBoard(self.get_screen_rect(), "Exiting...") + self.board = MessageBoard(self.get_screen_rect(), "Exiting...", bg_color=self.bg_color) self.running = False -def get_config(path: str) -> List[UrlDef]: +def get_url_defs(config_data: dict) -> List[UrlDef]: url_defs = [] - with open(path, "rb") as f: - data = json.load(f) - for d in data['urls']: - url_defs.append(UrlDef(label=d['label'], url=d['url'])) + for d in data['urls']: + url_defs.append(UrlDef(label=d['label'], url=d['url'])) return url_defs @@ -185,5 +184,11 @@ if __name__ == '__main__': parser = argparse.ArgumentParser() parser.add_argument("config_path", help="Path to the config file.", type=str) args = parser.parse_args() - app = App(get_config(args.config_path)) + + url_defs: List[UrlDef] = [] + with open(args.config_path, "rb") as f: + data = json.load(f) + url_defs = get_url_defs(data) + + app = App(url_defs, bg_color=data["bg_color"]) app.loop()