Gradience/src/presets_manager_window.py

265 lines
9.9 KiB
Python
Raw Normal View History

# presets_manager_window.py
2022-08-15 13:40:21 +00:00
#
2022-08-20 23:17:40 +00:00
# Change the look of Adwaita, with ease
# Copyright (C) 2022 Gradience Team
2022-08-15 13:40:21 +00:00
#
2022-08-20 23:17:40 +00:00
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
2022-08-15 13:40:21 +00:00
#
2022-08-20 23:17:40 +00:00
# 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 General Public License for more details.
2022-08-15 13:40:21 +00:00
#
2022-08-20 23:17:40 +00:00
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <https://www.gnu.org/licenses/>.
2022-08-15 13:40:21 +00:00
import os
2022-08-23 09:46:51 +00:00
import shutil
2022-08-15 13:40:21 +00:00
import json
from gi.repository import Gtk, Adw, Gio, Gdk
2022-08-22 13:56:56 +00:00
from .preset_row import GradiencePresetRow
2022-08-23 09:11:54 +00:00
from .builtin_preset_row import GradienceBuiltinPresetRow
from .explore_preset_row import GradienceExplorePresetRow
from .modules.custom_presets import fetch_presets
from .constants import rootdir, build_type
from .modules.utils import to_slug_case
2022-09-03 11:02:36 +00:00
from .repo_row import GradienceRepoRow
2022-08-17 21:35:59 +00:00
2022-08-24 18:19:01 +00:00
PRESETS_LIST_URL = "https://github.com/GradienceTeam/Community/raw/main/presets.json"
2022-08-22 13:58:30 +00:00
2022-08-15 13:40:21 +00:00
@Gtk.Template(resource_path=f"{rootdir}/ui/presets_manager_window.ui")
2022-08-19 19:14:05 +00:00
class GradiencePresetWindow(Adw.Window):
__gtype_name__ = "GradiencePresetWindow"
2022-08-15 13:40:21 +00:00
2022-08-24 15:25:24 +00:00
installed = Gtk.Template.Child()
2022-09-03 11:02:36 +00:00
repos = Gtk.Template.Child()
2022-08-15 13:40:21 +00:00
main_view = Gtk.Template.Child()
toast_overlay = Gtk.Template.Child()
2022-08-22 13:58:30 +00:00
2022-08-22 13:56:56 +00:00
import_button = Gtk.Template.Child("import_button")
remove_button = Gtk.Template.Child("remove_button")
file_manager_button = Gtk.Template.Child("file_manager_button")
2022-08-24 18:19:22 +00:00
2022-08-24 15:25:24 +00:00
search_entry = Gtk.Template.Child("search_entry")
search_stack = Gtk.Template.Child("search_stack")
search_results = Gtk.Template.Child("search_results")
search_spinner = Gtk.Template.Child("search_spinner")
2022-08-15 13:40:21 +00:00
custom_presets = {}
2022-09-03 14:22:54 +00:00
official_repositories = {
"Official": "https://github.com/GradienceTeam/Community/raw/main/presets.json"
}
2022-08-15 13:40:21 +00:00
def __init__(self, **kwargs):
super().__init__(**kwargs)
2022-08-23 11:01:42 +00:00
self.setup_explore()
2022-09-03 14:22:54 +00:00
self.repositories = {
"Official 2": "https://github.com/GradienceTeam/Community/raw/main/presets.json"
}
2022-08-23 09:46:51 +00:00
self.builtin_preset_list = Adw.PreferencesGroup()
self.builtin_preset_list.set_title(_("Builtin Presets"))
self.installed.add(self.builtin_preset_list)
self.preset_list = Adw.PreferencesGroup()
self.preset_list.set_title(_("User Presets"))
self.installed.add(self.preset_list)
2022-09-03 11:07:18 +00:00
self.reload_pref_group()
2022-09-03 11:07:50 +00:00
2022-09-03 11:02:36 +00:00
self.repos_list = Adw.PreferencesGroup()
self.repos_list.set_title(_("Repositories"))
self.repos.add(self.repos_list)
2022-09-03 11:07:18 +00:00
self.reload_repos_group()
2022-08-23 09:46:51 +00:00
2022-08-23 08:36:20 +00:00
self.app = Gtk.Application.get_default()
2022-08-23 09:46:51 +00:00
self.setup_import()
2022-08-25 10:24:20 +00:00
self.connect_signals()
2022-08-25 13:28:38 +00:00
self.delete_toast = Adw.Toast(title=_("Preset removed"))
2022-08-25 10:45:30 +00:00
# self.delete_toast.set_action_name("on_undo_button_clicked")
self.delete_preset = True
2022-08-25 10:24:20 +00:00
self.delete_toast.set_button_label(_("Undo"))
self.delete_toast.connect("dismissed", self.on_delete_toast_dismissed)
2022-08-25 10:45:30 +00:00
self.delete_toast.connect(
"button-clicked",
self.on_undo_button_clicked)
2022-09-03 11:07:50 +00:00
2022-09-03 11:02:36 +00:00
def remove_repo(self, repo_name):
self.repositories.pop(repo_name)
def reload_repos_group(self):
self.repos_list = Adw.PreferencesGroup()
2022-09-03 14:25:09 +00:00
self.repos_list.set_title(_("Repositories"))
2022-09-03 14:26:28 +00:00
for repo_name, repo in self.official_repositories.items():
2022-09-03 14:25:33 +00:00
row = GradienceRepoRow(repo, repo_name, self, deletable=False)
self.repos_list.add(row)
2022-09-03 14:26:28 +00:00
for repo_name, repo in self.repositories.items():
2022-09-03 14:24:17 +00:00
row = GradienceRepoRow(repo, repo_name, self)
2022-09-03 11:02:36 +00:00
self.repos_list.add(row)
2022-09-03 14:25:33 +00:00
2022-09-03 11:02:36 +00:00
self.repos.add(self.repos_list)
2022-09-03 11:07:50 +00:00
def setup_explore(self):
self.explore_presets, urls = fetch_presets()
if not self.explore_presets: # offline
self.search_spinner.props.visible = False
self.search_stack.set_visible_child_name("page_offline")
2022-08-25 10:24:20 +00:00
else:
self.search_spinner.props.visible = False
2022-08-25 12:14:02 +00:00
2022-08-25 10:24:20 +00:00
for (preset, preset_name), preset_url in zip(
self.explore_presets.items(), urls):
row = GradienceExplorePresetRow(preset_name, preset_url, self)
2022-08-25 10:24:20 +00:00
self.search_results.append(row)
2022-08-23 09:46:51 +00:00
def setup_import(self):
self.file_chooser_dialog = Gtk.FileChooserNative()
2022-08-23 09:46:51 +00:00
self.file_chooser_dialog.set_transient_for(self)
self.file_chooser_dialog.connect(
"response", self.on_file_chooser_response
)
2022-08-24 18:19:22 +00:00
2022-08-24 15:25:24 +00:00
def connect_signals(self):
self.search_entry.connect("search-changed", self.on_search_changed)
self.search_entry.connect("realize", self.on_search_realize)
2022-08-25 10:24:20 +00:00
def on_delete_toast_dismissed(self, widget):
if self.delete_preset:
2022-08-25 10:40:52 +00:00
try:
os.remove(os.path.join(
os.environ.get("XDG_CONFIG_HOME",
2022-08-25 10:45:30 +00:00
os.environ["HOME"] + "/.config"),
2022-08-25 10:40:52 +00:00
"presets",
to_slug_case(self.old_name) + ".json",
))
except Exception:
self.toast_overlay.add_toast(
2022-08-25 10:45:30 +00:00
Adw.Toast(title=_("Unable to delete preset"))
)
2022-08-25 10:40:52 +00:00
else:
self.toast_overlay.add_toast(
2022-08-25 13:28:38 +00:00
Adw.Toast(title=_("Preset removed"))
2022-08-25 10:45:30 +00:00
)
2022-08-25 10:41:51 +00:00
finally:
self.reload_pref_group()
2022-08-25 10:40:52 +00:00
self.delete_preset = True
def on_undo_button_clicked(self, *_args):
self.delete_preset = False
2022-08-25 10:40:52 +00:00
self.delete_toast.dismiss()
2022-08-24 18:19:22 +00:00
2022-08-25 10:33:48 +00:00
def on_search_changed(self, widget):
2022-08-24 15:25:24 +00:00
print("search changed")
2022-08-24 18:19:22 +00:00
2022-08-24 15:25:24 +00:00
def on_search_realize(self, widget):
print("search realized")
2022-08-24 18:19:22 +00:00
@Gtk.Template.Callback()
2022-08-23 08:36:20 +00:00
def on_file_manager_button_clicked(self, *_args):
self.app.open_preset_directory()
2022-08-23 09:12:16 +00:00
@Gtk.Template.Callback()
2022-08-23 09:46:51 +00:00
def on_import_button_clicked(self, *_args):
self.file_chooser_dialog.show()
def on_file_chooser_response(self, widget, response):
if response == Gtk.ResponseType.ACCEPT:
self.preset_path = self.file_chooser_dialog.get_file()
preset_file = self.preset_path.get_basename()
2022-08-23 09:46:51 +00:00
self.file_chooser_dialog.hide()
if response == Gtk.ResponseType.ACCEPT:
if preset_file.endswith(".json"):
2022-08-23 09:54:39 +00:00
if preset_file.strip(".json") in self.custom_presets:
self.toast_overlay.add_toast(
Adw.Toast(title=_("Preset already exists"))
)
else:
shutil.copy(self.preset_path.get_path(), os.path.join(
os.environ.get("XDG_CONFIG_HOME",
os.environ["HOME"] + "/.config"),
"presets",
preset_file
))
self.toast_overlay.add_toast(
2022-08-25 13:28:38 +00:00
Adw.Toast(title=_("Preset imported"))
2022-08-23 09:54:39 +00:00
)
else:
2022-08-23 11:01:42 +00:00
self.toast_overlay.add_toast(Adw.Toast(
title=_("Unsupported file format, must be .json")))
2022-08-23 09:54:39 +00:00
2022-08-23 09:46:51 +00:00
self.reload_pref_group()
def reload_pref_group(self):
preset_directory = os.path.join(
2022-08-15 13:40:21 +00:00
os.environ.get("XDG_CONFIG_HOME", os.environ["HOME"] + "/.config"),
"presets",
)
if not os.path.exists(preset_directory):
os.makedirs(preset_directory)
self.custom_presets.clear()
self.builtin_presets = {
2022-08-23 09:11:54 +00:00
"adwaita-dark": "Adwaita Dark",
"adwaita": "Adwaita",
"pretty-purple": "Pretty Purple",
}
2022-08-15 13:40:21 +00:00
for file_name in os.listdir(preset_directory):
if file_name.endswith(".json"):
try:
with open(
os.path.join(preset_directory, file_name), "r", encoding="utf-8"
) as file:
preset_text = file.read()
preset = json.loads(preset_text)
2022-08-15 13:40:21 +00:00
if preset.get("variables") is None:
raise KeyError("variables")
if preset.get("palette") is None:
raise KeyError("palette")
2022-08-17 21:35:59 +00:00
self.custom_presets[file_name.replace(
".json", "")] = preset["name"]
2022-08-15 13:40:21 +00:00
except Exception:
2022-08-23 07:47:11 +00:00
self.toast_overlay.add_toast(
2022-08-15 13:40:21 +00:00
Adw.Toast(title=_("Failed to load preset"))
)
2022-08-23 09:46:51 +00:00
self.installed.remove(self.preset_list)
self.installed.remove(self.builtin_preset_list)
2022-08-23 09:54:39 +00:00
self.builtin_preset_list = Adw.PreferencesGroup()
2022-08-23 09:27:21 +00:00
self.builtin_preset_list.set_title(_("Builtin Presets"))
for preset, preset_name in self.builtin_presets.items():
row = GradienceBuiltinPresetRow(preset_name, self.toast_overlay)
2022-08-23 09:27:21 +00:00
self.builtin_preset_list.add(row)
self.installed.add(self.builtin_preset_list)
2022-08-23 09:46:51 +00:00
self.preset_list = Adw.PreferencesGroup()
2022-08-23 09:27:21 +00:00
self.preset_list.set_title(_("User Presets"))
2022-08-23 11:01:42 +00:00
self.preset_list.set_description(
_("See <a href=\"https://github.com/GradienceTeam/Community\">GradienceTeam/Community</a> on Github for more presets"))
2022-08-23 10:25:30 +00:00
if self.custom_presets:
for preset, preset_name in self.custom_presets.items():
row = GradiencePresetRow(preset_name, self)
2022-08-23 10:25:30 +00:00
self.preset_list.add(row)
2022-08-23 10:14:17 +00:00
else:
self.preset_empty = Adw.ActionRow()
2022-08-23 11:01:42 +00:00
self.preset_empty.set_title(
_("No preset found! Use the import button to import one or search one on the Explore tab"))
2022-08-23 10:25:30 +00:00
self.preset_list.add(self.preset_empty)
2022-08-22 13:56:56 +00:00
self.installed.add(self.preset_list)