113 lines
3.9 KiB
Python
113 lines
3.9 KiB
Python
# window.py
|
|
#
|
|
# Copyright 2025 nihil
|
|
#
|
|
# This program is free software: you can redistribute it and/or modify
|
|
# it under the terms of the GNU Affero General Public License as published by
|
|
# the Free Software Foundation, either version 3 of the License, or
|
|
# (at your option) any later version.
|
|
#
|
|
# This program is distributed in the hope that it will be useful,
|
|
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
# GNU Affero General Public License for more details.
|
|
#
|
|
# You should have received a copy of the GNU Affero General Public License
|
|
# along with this program. If not, see <https://www.gnu.org/licenses/>.
|
|
#
|
|
# SPDX-License-Identifier: AGPL-3.0-or-later
|
|
|
|
import json
|
|
import os
|
|
from pathlib import Path
|
|
|
|
from .pages.home import HomePage
|
|
from .pages.settings import SettingsPage
|
|
from .pages.checklist import ChecklistPage
|
|
|
|
from gi.repository import Adw
|
|
from gi.repository import Gtk, Gio
|
|
from gi.repository import GLib
|
|
|
|
def get_config_dir(app_id: str) -> Path:
|
|
base = Path(GLib.get_user_config_dir())
|
|
cfg_dir = base / app_id
|
|
cfg_dir.mkdir(parents=True, exist_ok=True)
|
|
print(f"Found config dir at: {cfg_dir}")
|
|
return cfg_dir
|
|
|
|
APP_ID = "gay.valhrafnaz.Gnomeframe"
|
|
CONFIG_DIR = get_config_dir(APP_ID)
|
|
CONFIG_FILE = CONFIG_DIR / "profile.json"
|
|
|
|
|
|
def load_profile() -> dict[str, bool]:
|
|
try:
|
|
with CONFIG_FILE.open("r", encoding="utf-8") as f:
|
|
data = json.load(f)
|
|
return {k: bool(v) for k, v in data.items()}
|
|
except FileNotFoundError:
|
|
return {}
|
|
except (json.JSONDecodeError, OSError) as exc:
|
|
print(f"Could not load profile: {exc}")
|
|
return {}
|
|
|
|
def save_profile(state: dict[str, bool]) -> None:
|
|
try:
|
|
CONFIG_DIR.mkdir(parents=True, exist_ok=True)
|
|
with CONFIG_FILE.open("w", encoding="utf-8") as f:
|
|
json.dump(state, f, indent=2, sort_keys=True)
|
|
except OSError as exc:
|
|
print(f"Failed to write profile: {exc}")
|
|
|
|
|
|
|
|
|
|
@Gtk.Template(resource_path='/gay/valhrafnaz/Gnomeframe/ui/window.ui')
|
|
class GnomeframeWindow(Adw.ApplicationWindow):
|
|
__gtype_name__ = 'GnomeframeWindow'
|
|
|
|
viewstack = Gtk.Template.Child()
|
|
|
|
def __init__(self, **kwargs):
|
|
super().__init__(**kwargs)
|
|
|
|
self._profile: dict[str, bool] = load_profile()
|
|
self._load_page_templates()
|
|
|
|
reset_action = Gio.SimpleAction.new("reset-selections", None)
|
|
reset_action.connect("activate", self._reset_all)
|
|
self.get_application().add_action(reset_action)
|
|
|
|
self.connect("close-request", self._on_close_request)
|
|
|
|
# self.settings = Gio.Settings(schema_id="gay.valhrafnaz.Gnomeframe")
|
|
# self.settings.bind("window-width", self, "default-width", Gio.SettingsBindFlags.DEFAULT)
|
|
# self.settings.bind("window-height", self, "default-height", Gio.SettingsBindFlags.DEFAULT)
|
|
# self.settings.bind("window-maximized", self, "maximized", Gio.SettingsBindFlags.DEFAULT)
|
|
|
|
def _load_page_templates(self):
|
|
self.home_page = HomePage(parent=self.viewstack)
|
|
self.settings_page = SettingsPage(parent=self.viewstack)
|
|
self.checklist_page = ChecklistPage(parent=self.viewstack, window=self)
|
|
|
|
def _reset_all(self, action, param):
|
|
self._profile.clear()
|
|
button = self.checklist_page.btns_basic.get_first_child()
|
|
while button is not None:
|
|
if isinstance(button, Gtk.ToggleButton):
|
|
button.set_active(False)
|
|
button = button.get_next_sibling()
|
|
button = self.checklist_page.btns_prime.get_first_child()
|
|
while button is not None:
|
|
if isinstance(button, Gtk.ToggleButton):
|
|
button.set_active(False)
|
|
button = button.get_next_sibling()
|
|
save_profile(self._profile)
|
|
|
|
def _on_close_request(self, *args):
|
|
save_profile(self._profile)
|
|
return False
|
|
|
|
|