merge upstream

This commit is contained in:
2025-05-31 00:00:37 -04:00
9 changed files with 465 additions and 80 deletions

27
modules/config.py Normal file
View File

@@ -0,0 +1,27 @@
# "modules/config.py", licensed under the MIT license
# Copyright 2024-2025 NinjaCheetah & Contributors
import os
import json
import pathlib
def get_config_file() -> pathlib.Path:
config_dir = pathlib.Path(os.path.join(
os.environ.get('APPDATA') or
os.environ.get('XDG_CONFIG_HOME') or
os.path.join(os.environ['HOME'], '.config'),
"NUSGet"
))
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, indent=4))
def update_setting(config_data: dict, setting: str, value: any) -> None:
config_data[setting] = value
save_config(config_data)

View File

@@ -1,15 +1,12 @@
# "modules/core.py", licensed under the MIT license
# Copyright 2024-2025 NinjaCheetah & Contributors
import os
import json
import pathlib
import requests
from dataclasses import dataclass
from typing import List
from PySide6.QtCore import Qt, QSize
from PySide6.QtWidgets import QStyledItemDelegate, QSizePolicy
from PySide6.QtWidgets import QStyledItemDelegate
# This is required to make the dropdown look correct with the custom styling. A little fuzzy on the why, but it has to
@@ -63,6 +60,13 @@ def connect_is_enabled_to_checkbox(items, chkbox):
item.setEnabled(False)
def fixup_qmenu_background(menu):
# These fixes are required to not have a square background poking out from behind the rounded corners of a QMenu.
menu.setWindowFlags(menu.windowFlags() | Qt.FramelessWindowHint)
menu.setWindowFlags(menu.windowFlags() | Qt.NoDropShadowWindowHint)
menu.setAttribute(Qt.WA_TranslucentBackground)
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)
@@ -81,25 +85,3 @@ def check_nusget_updates(app, current_version: str, progress_callback=None) -> s
return new_version
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
os.environ.get('XDG_CONFIG_HOME') or
os.path.join(os.environ['HOME'], '.config'),
"NUSGet"
))
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)

79
modules/language.py Normal file
View File

@@ -0,0 +1,79 @@
# "modules/language.py", licensed under the MIT license
# Copyright 2024-2025 NinjaCheetah & Contributors
from modules.config import update_setting
from PySide6.QtCore import QLocale, QTranslator
LANGS = {
"en": "English",
"es": "Español",
"de": "Deutsch",
"fr": "Français",
"it": "Italiano",
"no": "Norsk",
"ro": "Românǎ",
"ko": "한국어",
}
def set_language(config_data: dict, lang: str) -> None:
# Match the selected language. These names will NOT be translated since they represent each language in that
# language, but the "System (Default)" option will, so that will match the default case.
match lang:
case "English":
print("setting language to English")
update_setting(config_data, "language", "en")
case "Español":
print("setting language to Spanish")
update_setting(config_data, "language", "es")
case "Deutsch":
print("setting language to German")
update_setting(config_data, "language", "de")
case "Français":
print("setting language to French")
update_setting(config_data, "language", "fr")
case "Italiano":
print("setting language to Italian")
update_setting(config_data, "language", "it")
case "Norsk":
print("setting language to Norwegian")
update_setting(config_data, "language", "no")
case "Română":
print("setting language to Romanian")
update_setting(config_data, "language", "ro")
case "한국어":
print("setting language to Korean")
update_setting(config_data, "language", "ko")
case _:
print("setting language to system (default)")
update_setting(config_data, "language", "")
def get_language(translator: QTranslator, config_data: dict, path: str) -> QTranslator:
try:
lang = config_data["language"]
except KeyError:
lang = ""
# A specific language was set in the app's settings.
if lang != "":
# If the target language is English, then return an empty translator because that's the default.
if lang == "en":
return translator
if translator.load(QLocale(lang), 'nusget', '_', path):
return translator
else:
# If we get here, then the saved language is invalid, so clear it and run again to use the system language.
update_setting(config_data, "language", "")
return get_language(translator, config_data, path)
else:
# Unix-likes and Windows handle this differently, apparently. Unix-likes will try `nusget_xx_XX.qm` and then
# fall back on just `nusget_xx.qm` if the region-specific translation for the language can't be found. On
# Windows, no such fallback exists, and so this code manually implements that fallback, since for languages like
# Spanish NUSGet doesn't use region-specific translations.
locale = QLocale.system()
if not translator.load(QLocale.system(), 'nusget', '_', path):
base_locale = QLocale(locale.language())
translator.load(base_locale, 'nusget', '_', path)
return translator

View File

@@ -5,6 +5,9 @@ import os
import platform
import subprocess
from modules.config import update_setting
def is_dark_theme_windows():
# This has to be here so that Python doesn't try to import it on non-Windows.
import winreg
@@ -17,6 +20,7 @@ def is_dark_theme_windows():
except Exception:
return False
def is_dark_theme_macos():
# macOS is weird. If the dark theme is on, then `defaults read -g AppleInterfaceStyle` returns "Dark". If the light
# theme is on, then trying to read this key fails and returns an error instead.
@@ -29,6 +33,7 @@ def is_dark_theme_macos():
except Exception:
return False
def is_dark_theme_linux():
try:
import subprocess
@@ -43,7 +48,14 @@ def is_dark_theme_linux():
except Exception:
return False
def is_dark_theme():
def is_dark_theme(config_data: dict):
# Theming priority order:
# 1. `THEME` environment variable
# 2. Theme saved in config.json
# 3. The system theme
# 4. Light, if all else fails (though personally I'd prefer dark)
#
# First, check for an environment variable overriding the theme, and use that if it exists.
try:
if os.environ["THEME"].lower() == "light":
@@ -54,7 +66,18 @@ def is_dark_theme():
print(f"Unknown theme specified: \"{os.environ['THEME']}\"")
except KeyError:
pass
# If the theme wasn't overridden, then check the current system theme.
# If the theme wasn't overridden, read the user's preference in config.json.
try:
match config_data["theme"]:
case "light":
return False
case "dark":
return True
case _:
pass
except KeyError:
pass
# If a theme wasn't set (or the theme is "system"), then check for the system theme.
system = platform.system()
if system == "Windows":
return is_dark_theme_windows()
@@ -62,3 +85,16 @@ def is_dark_theme():
return is_dark_theme_macos()
else:
return is_dark_theme_linux()
def set_theme(config_data: dict, theme: str) -> None:
match theme:
case "light":
print("setting theme to light")
update_setting(config_data, "theme", "light")
case "dark":
print("setting theme to dark")
update_setting(config_data, "theme", "dark")
case _:
print("setting theme to system (default)")
update_setting(config_data, "theme", "")