From 4a08bd47cd7f27e21ba10e36105d99fadd410aaa Mon Sep 17 00:00:00 2001 From: NinjaCheetah <58050615+NinjaCheetah@users.noreply.github.com> Date: Wed, 21 May 2025 12:53:32 -0400 Subject: [PATCH] Allow overriding the color scheme with the THEME environment variable --- NUSGet.py | 16 ++++++++++++---- modules/core.py | 5 +++++ 2 files changed, 17 insertions(+), 4 deletions(-) diff --git a/NUSGet.py b/NUSGet.py index afed5e8..c3f5208 100644 --- a/NUSGet.py +++ b/NUSGet.py @@ -656,10 +656,18 @@ if __name__ == "__main__": # Load Fusion because that's objectively the best base theme, and then load the fancy stylesheet on top to make # NUSGet look nice and pretty. app.setStyle("fusion") - if is_dark_theme(): - stylesheet = open(os.path.join(os.path.dirname(__file__), "resources", "style_dark.qss")).read() - else: - stylesheet = open(os.path.join(os.path.dirname(__file__), "resources", "style_light.qss")).read() + theme_sheet = "style_dark.qss" + try: + # Check for an environment variable overriding the theme. This is mostly for theme testing but would also allow + # you to force a theme. + if os.environ["THEME"].lower() == "light": + theme_sheet = "style_light.qss" + except KeyError: + if is_dark_theme(): + theme_sheet = "style_dark.qss" + else: + theme_sheet = "style_light.qss" + stylesheet = open(os.path.join(os.path.dirname(__file__), "resources", theme_sheet)).read() image_path_prefix = pathlib.Path(os.path.join(os.path.dirname(__file__), "resources")).resolve().as_posix() stylesheet = stylesheet.replace("{IMAGE_PREFIX}", image_path_prefix) app.setStyleSheet(stylesheet) diff --git a/modules/core.py b/modules/core.py index e579075..d149293 100644 --- a/modules/core.py +++ b/modules/core.py @@ -54,6 +54,7 @@ def connect_label_to_checkbox(label, checkbox): checkbox.toggle() label.mousePressEvent = toggle_checkbox + def connect_is_enabled_to_checkbox(items, chkbox): for item in items: if chkbox.isChecked(): @@ -61,6 +62,7 @@ def connect_is_enabled_to_checkbox(items, chkbox): else: item.setEnabled(False) + def check_nusget_updates(app, current_version: str, progress_callback=None) -> str | None: # Simple function to make a request to the GitHub API and then check if the latest available version is newer. gh_api_request = requests.get(url="https://api.github.com/repos/NinjaCheetah/NUSGet/releases/latest", stream=True) @@ -80,6 +82,7 @@ def check_nusget_updates(app, current_version: str, progress_callback=None) -> s progress_callback.emit(app.translate("MainWindow", "\n\nYou're running the latest release of NUSGet.")) return None + def get_config_file() -> pathlib.Path: config_dir = pathlib.Path(os.path.join( os.environ.get('APPDATA') or @@ -90,11 +93,13 @@ def get_config_file() -> pathlib.Path: config_dir.mkdir(exist_ok=True) return config_dir.joinpath("config.json") + def save_config(config_data: dict) -> None: config_file = get_config_file() print(f"writing data: {config_data}") open(config_file, "w").write(json.dumps(config_data)) + def update_setting(config_data: dict, setting: str, value: any) -> None: config_data[setting] = value save_config(config_data)