2022-08-22 13:56:45 +00:00
|
|
|
from gi.repository import Gtk, Gdk, Adw
|
|
|
|
|
|
|
|
from .constants import rootdir
|
2022-08-23 07:47:00 +00:00
|
|
|
from . import to_slug_case
|
|
|
|
import json
|
|
|
|
import os
|
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-22 13:56:45 +00:00
|
|
|
|
2022-08-23 07:47:00 +00:00
|
|
|
def __init__(self, name, toast_overlay, author="", **kwargs):
|
2022-08-22 13:56:45 +00:00
|
|
|
super().__init__(**kwargs)
|
2022-08-23 07:47:00 +00:00
|
|
|
|
|
|
|
self.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 07:47:00 +00:00
|
|
|
|
|
|
|
self.app = Gtk.Application.get_default()
|
|
|
|
|
|
|
|
self.toast_overlay = 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):
|
|
|
|
print("apply")
|
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 07:47:00 +00:00
|
|
|
|
2022-08-23 07:49:20 +00:00
|
|
|
def update_value(self):
|
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!"))
|
|
|
|
)
|