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-09-07 16:00:33 +00:00
|
|
|
from gi.repository import Gtk, Adw
|
2022-08-24 19:29:11 +00:00
|
|
|
|
|
|
|
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-09-03 16:54:18 +00:00
|
|
|
def __init__(self, name, win, repo_name, 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-09-03 17:04:33 +00:00
|
|
|
|
2022-09-03 16:54:18 +00:00
|
|
|
self.prefix = to_slug_case(repo_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-09-07 15:52:50 +00:00
|
|
|
self.app.load_preset_from_file(
|
|
|
|
os.path.join(
|
|
|
|
os.environ.get("XDG_CONFIG_HOME",
|
|
|
|
os.environ["HOME"] + "/.config"),
|
|
|
|
"presets",
|
|
|
|
self.prefix,
|
|
|
|
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-09-03 16:54:18 +00:00
|
|
|
self.delete_preset = True
|
|
|
|
self.delete_toast = Adw.Toast(title=_("Preset removed"))
|
|
|
|
self.delete_toast.set_button_label(_("Undo"))
|
|
|
|
self.delete_toast.connect("dismissed", self.on_delete_toast_dismissed)
|
|
|
|
self.delete_toast.connect(
|
2022-09-07 15:52:50 +00:00
|
|
|
"button-clicked", self.on_undo_button_clicked)
|
2022-09-03 17:04:33 +00:00
|
|
|
|
2022-09-07 15:52:50 +00:00
|
|
|
self.toast_overlay.add_toast(self.delete_toast)
|
2022-08-23 10:12:22 +00:00
|
|
|
|
2022-08-25 10:33:09 +00:00
|
|
|
self.win.old_name = self.name
|
2022-09-04 14:26:13 +00:00
|
|
|
|
2022-09-04 14:08:37 +00:00
|
|
|
self.do_delete_preset()
|
2022-08-23 10:12:22 +00:00
|
|
|
|
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-09-10 08:58:34 +00:00
|
|
|
with open(
|
2022-09-07 15:52:50 +00:00
|
|
|
os.path.join(
|
|
|
|
os.environ.get("XDG_CONFIG_HOME",
|
|
|
|
os.environ["HOME"] + "/.config"),
|
|
|
|
"presets",
|
|
|
|
self.prefix,
|
|
|
|
to_slug_case(self.old_name) + ".json",
|
2022-09-10 08:58:34 +00:00
|
|
|
),
|
|
|
|
"r",
|
|
|
|
encoding="utf-8",
|
|
|
|
) as file:
|
|
|
|
preset_text = file.read()
|
|
|
|
preset = json.loads(preset_text)
|
2022-09-10 09:26:27 +00:00
|
|
|
|
2022-09-10 08:58:34 +00:00
|
|
|
preset_name = preset["name"]
|
|
|
|
variables = preset["variables"]
|
|
|
|
palette = preset["palette"]
|
|
|
|
if "custom_css" in preset:
|
|
|
|
custom_css = preset["custom_css"]
|
|
|
|
else:
|
|
|
|
custom_css = {
|
|
|
|
"gtk4": "",
|
|
|
|
"gtk3": "",
|
|
|
|
}
|
2022-09-10 09:26:27 +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",
|
2022-09-03 16:54:18 +00:00
|
|
|
self.prefix,
|
2022-08-23 07:47:00 +00:00
|
|
|
to_slug_case(self.name) + ".json",
|
|
|
|
),
|
|
|
|
"w",
|
|
|
|
encoding="utf-8",
|
|
|
|
) as file:
|
|
|
|
object_to_write = {
|
|
|
|
"name": self.name,
|
2022-09-10 08:58:34 +00:00
|
|
|
"variables": variables,
|
|
|
|
"palette": palette,
|
|
|
|
"custom_css": custom_css,
|
2022-08-23 07:47:00 +00:00
|
|
|
}
|
|
|
|
file.write(json.dumps(object_to_write, indent=4))
|
2022-09-07 15:52:50 +00:00
|
|
|
self.toast_overlay.add_toast(Adw.Toast(title=_("Preset renamed")))
|
2022-09-10 08:58:34 +00:00
|
|
|
os.remove(
|
|
|
|
os.path.join(
|
|
|
|
os.environ.get("XDG_CONFIG_HOME",
|
|
|
|
os.environ["HOME"] + "/.config"),
|
|
|
|
"presets",
|
|
|
|
self.prefix,
|
|
|
|
to_slug_case(self.old_name) + ".json",
|
|
|
|
)
|
|
|
|
)
|
2022-08-23 08:20:45 +00:00
|
|
|
self.old_name = self.name
|
2022-09-03 16:54:18 +00:00
|
|
|
|
2022-09-04 14:08:37 +00:00
|
|
|
def do_delete_preset(self):
|
|
|
|
if self.delete_preset:
|
|
|
|
try:
|
2022-09-07 15:52:50 +00:00
|
|
|
os.rename(
|
|
|
|
os.path.join(
|
|
|
|
os.environ.get(
|
|
|
|
"XDG_CONFIG_HOME", os.environ["HOME"] + "/.config"
|
|
|
|
),
|
|
|
|
"presets",
|
|
|
|
self.prefix,
|
|
|
|
to_slug_case(self.old_name) + ".json",
|
|
|
|
),
|
|
|
|
os.path.join(
|
|
|
|
os.environ.get(
|
|
|
|
"XDG_CONFIG_HOME", os.environ["HOME"] + "/.config"
|
|
|
|
),
|
|
|
|
"presets",
|
|
|
|
self.prefix,
|
|
|
|
to_slug_case(self.old_name) + ".json.to_delete",
|
|
|
|
),
|
|
|
|
)
|
2022-09-04 14:08:37 +00:00
|
|
|
except Exception as exception:
|
|
|
|
print(exception.with_traceback())
|
|
|
|
finally:
|
|
|
|
self.win.reload_pref_group()
|
2022-09-03 17:04:33 +00:00
|
|
|
|
2022-09-04 14:08:37 +00:00
|
|
|
self.delete_preset = True
|
2022-09-04 14:26:13 +00:00
|
|
|
|
2022-09-04 14:08:37 +00:00
|
|
|
def on_delete_toast_dismissed(self, widget):
|
2022-09-03 16:54:18 +00:00
|
|
|
if self.delete_preset:
|
|
|
|
try:
|
2022-09-07 15:52:50 +00:00
|
|
|
os.remove(
|
|
|
|
os.path.join(
|
|
|
|
os.environ.get(
|
|
|
|
"XDG_CONFIG_HOME", os.environ["HOME"] + "/.config"
|
|
|
|
),
|
|
|
|
"presets",
|
|
|
|
self.prefix,
|
|
|
|
to_slug_case(self.old_name) + ".json.to_delete",
|
|
|
|
)
|
|
|
|
)
|
2022-09-03 16:54:18 +00:00
|
|
|
except Exception as exception:
|
|
|
|
print(exception.with_traceback())
|
|
|
|
self.toast_overlay.add_toast(
|
|
|
|
Adw.Toast(title=_("Unable to delete preset"))
|
|
|
|
)
|
|
|
|
else:
|
|
|
|
self.toast_overlay.add_toast(
|
2022-09-07 15:52:50 +00:00
|
|
|
Adw.Toast(title=_("Preset removed")))
|
2022-09-03 16:54:18 +00:00
|
|
|
finally:
|
|
|
|
self.win.reload_pref_group()
|
2022-09-04 14:12:46 +00:00
|
|
|
else:
|
|
|
|
try:
|
2022-09-07 15:52:50 +00:00
|
|
|
os.rename(
|
|
|
|
os.path.join(
|
|
|
|
os.environ.get(
|
|
|
|
"XDG_CONFIG_HOME", os.environ["HOME"] + "/.config"
|
|
|
|
),
|
|
|
|
"presets",
|
|
|
|
self.prefix,
|
|
|
|
to_slug_case(self.old_name) + ".json.to_delete",
|
|
|
|
),
|
|
|
|
os.path.join(
|
|
|
|
os.environ.get(
|
|
|
|
"XDG_CONFIG_HOME", os.environ["HOME"] + "/.config"
|
|
|
|
),
|
|
|
|
"presets",
|
|
|
|
self.prefix,
|
|
|
|
to_slug_case(self.old_name) + ".json",
|
|
|
|
),
|
|
|
|
)
|
2022-09-04 14:12:46 +00:00
|
|
|
except Exception as exception:
|
|
|
|
print(exception.with_traceback())
|
|
|
|
finally:
|
|
|
|
self.win.reload_pref_group()
|
2022-09-03 16:54:18 +00:00
|
|
|
|
|
|
|
self.delete_preset = True
|
2022-09-03 17:04:33 +00:00
|
|
|
|
2022-09-03 16:54:18 +00:00
|
|
|
def on_undo_button_clicked(self, *_args):
|
|
|
|
self.delete_preset = False
|
|
|
|
self.delete_toast.dismiss()
|