2022-08-24 19:29:11 +00:00
|
|
|
# preset_row.py
|
|
|
|
#
|
|
|
|
# Change the look of Adwaita, with ease
|
|
|
|
# Copyright (C) 2022 Gradience Team
|
|
|
|
#
|
|
|
|
# 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.
|
|
|
|
#
|
|
|
|
# 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.
|
|
|
|
#
|
|
|
|
# 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-22 13:56:45 +00:00
|
|
|
|
2022-08-23 07:47:00 +00:00
|
|
|
import json
|
|
|
|
import os
|
2022-08-22 13:56:45 +00:00
|
|
|
|
2022-08-24 19:29:11 +00:00
|
|
|
from gi.repository import Gtk, Gdk, Adw
|
|
|
|
|
|
|
|
from .constants import rootdir
|
|
|
|
from .modules.utils import to_slug_case, buglog
|
|
|
|
|
2022-08-22 13:56:45 +00:00
|
|
|
|
|
|
|
@Gtk.Template(resource_path=f"{rootdir}/ui/preset_row.ui")
|
|
|
|
class GradiencePresetRow(Adw.ActionRow):
|
|
|
|
__gtype_name__ = "GradiencePresetRow"
|
2022-08-23 07:27:11 +00:00
|
|
|
|
2022-08-23 07:22:42 +00:00
|
|
|
name_entry = Gtk.Template.Child("name_entry")
|
|
|
|
value_stack = Gtk.Template.Child("value_stack")
|
|
|
|
name_entry_toggle = Gtk.Template.Child("name_entry_toggle")
|
|
|
|
apply_button = Gtk.Template.Child("apply_button")
|
2022-08-23 10:11:32 +00:00
|
|
|
remove_button = Gtk.Template.Child("remove_button")
|
2022-08-22 13:56:45 +00:00
|
|
|
|
2022-08-23 10:11:32 +00:00
|
|
|
def __init__(self, name, win, author="", **kwargs):
|
2022-08-22 13:56:45 +00:00
|
|
|
super().__init__(**kwargs)
|
2022-08-23 09:12:16 +00:00
|
|
|
|
2022-08-23 07:47:00 +00:00
|
|
|
self.name = name
|
2022-08-23 08:20:45 +00:00
|
|
|
self.old_name = name
|
2022-08-22 13:56:45 +00:00
|
|
|
|
2022-08-23 07:54:09 +00:00
|
|
|
self.set_name(name)
|
|
|
|
self.set_title(name)
|
2022-08-22 13:56:45 +00:00
|
|
|
self.set_subtitle(author)
|
2022-08-23 07:54:09 +00:00
|
|
|
self.name_entry.set_text(name)
|
2022-08-23 09:12:16 +00:00
|
|
|
|
2022-08-23 07:47:00 +00:00
|
|
|
self.app = Gtk.Application.get_default()
|
2022-08-23 10:11:32 +00:00
|
|
|
self.win = win
|
|
|
|
self.toast_overlay = self.win.toast_overlay
|
2022-08-22 13:56:45 +00:00
|
|
|
|
|
|
|
apply_button = Gtk.Template.Child("apply_button")
|
|
|
|
rename_button = Gtk.Template.Child("rename_button")
|
|
|
|
|
|
|
|
@Gtk.Template.Callback()
|
|
|
|
def on_apply_button_clicked(self, *_args):
|
2022-08-23 17:57:53 +00:00
|
|
|
buglog("apply")
|
2022-08-23 09:12:16 +00:00
|
|
|
|
2022-08-23 08:36:09 +00:00
|
|
|
self.app.load_preset_from_file(os.path.join(
|
2022-08-23 09:12:16 +00:00
|
|
|
os.environ.get("XDG_CONFIG_HOME",
|
|
|
|
os.environ["HOME"] + "/.config"),
|
|
|
|
"presets",
|
|
|
|
to_slug_case(self.name) + ".json",
|
|
|
|
))
|
2022-08-23 07:22:42 +00:00
|
|
|
|
|
|
|
@Gtk.Template.Callback()
|
|
|
|
def on_name_entry_changed(self, *_args):
|
2022-08-23 07:47:00 +00:00
|
|
|
self.name = self.name_entry.get_text()
|
|
|
|
self.set_name(self.name)
|
|
|
|
self.set_title(self.name)
|
2022-08-23 07:22:42 +00:00
|
|
|
|
|
|
|
@Gtk.Template.Callback()
|
|
|
|
def on_name_entry_toggled(self, *_args):
|
|
|
|
if self.name_entry_toggle.get_active():
|
|
|
|
self.value_stack.set_visible_child(self.name_entry)
|
|
|
|
else:
|
2022-08-23 07:49:20 +00:00
|
|
|
self.update_value()
|
2022-08-23 07:27:11 +00:00
|
|
|
self.value_stack.set_visible_child(self.apply_button)
|
2022-08-23 10:12:22 +00:00
|
|
|
|
2022-08-23 10:11:32 +00:00
|
|
|
@Gtk.Template.Callback()
|
|
|
|
def on_remove_button_clicked(self, *_args):
|
2022-08-23 10:12:22 +00:00
|
|
|
|
|
|
|
try:
|
|
|
|
os.remove(os.path.join(
|
|
|
|
os.environ.get("XDG_CONFIG_HOME",
|
|
|
|
os.environ["HOME"] + "/.config"),
|
|
|
|
"presets",
|
|
|
|
to_slug_case(self.old_name) + ".json",
|
|
|
|
))
|
|
|
|
except Exception:
|
|
|
|
|
|
|
|
self.toast_overlay.add_toast(
|
|
|
|
Adw.Toast(title=_("Scheme could not be removed!"))
|
|
|
|
)
|
|
|
|
else:
|
|
|
|
self.toast_overlay.add_toast(
|
|
|
|
Adw.Toast(title=_("Scheme successfully deleted!"))
|
|
|
|
)
|
|
|
|
|
2022-08-23 10:11:32 +00:00
|
|
|
self.win.reload_pref_group()
|
2022-08-23 10:12:22 +00:00
|
|
|
|
2022-08-23 09:12:16 +00:00
|
|
|
def update_value(self):
|
2022-08-23 08:20:45 +00:00
|
|
|
os.remove(os.path.join(
|
2022-08-23 09:12:16 +00:00
|
|
|
os.environ.get("XDG_CONFIG_HOME",
|
|
|
|
os.environ["HOME"] + "/.config"),
|
|
|
|
"presets",
|
|
|
|
to_slug_case(self.old_name) + ".json",
|
2022-08-23 10:11:32 +00:00
|
|
|
))
|
2022-08-23 07:47:00 +00:00
|
|
|
with open(
|
|
|
|
os.path.join(
|
|
|
|
os.environ.get("XDG_CONFIG_HOME",
|
|
|
|
os.environ["HOME"] + "/.config"),
|
|
|
|
"presets",
|
|
|
|
to_slug_case(self.name) + ".json",
|
|
|
|
),
|
|
|
|
"w",
|
|
|
|
encoding="utf-8",
|
|
|
|
) as file:
|
|
|
|
object_to_write = {
|
|
|
|
"name": self.name,
|
|
|
|
"variables": self.app.variables,
|
|
|
|
"palette": self.app.palette,
|
|
|
|
"custom_css": self.app.custom_css,
|
|
|
|
}
|
|
|
|
file.write(json.dumps(object_to_write, indent=4))
|
|
|
|
self.app.clear_dirty()
|
|
|
|
self.toast_overlay.add_toast(
|
|
|
|
Adw.Toast(title=_("Scheme successfully renamed!"))
|
|
|
|
)
|
2022-08-23 08:20:45 +00:00
|
|
|
self.old_name = self.name
|