Gradience/gradience/preset_row.py

190 lines
6.2 KiB
Python
Raw Normal View History

# 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 os
2022-08-22 13:56:45 +00:00
2022-09-07 16:00:33 +00:00
from gi.repository import Gtk, Adw
2022-09-14 14:55:35 +00:00
from gradience.modules.custom_presets import PRESET_DIR
from .constants import rootdir
from .modules.utils import to_slug_case, buglog
2022-09-14 14:55:35 +00:00
from .modules.preset import Preset
2022-08-22 13:56:45 +00:00
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
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
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
2022-09-14 14:55:35 +00:00
self.preset = Preset(name, repo_name)
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):
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):
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)
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
self.win.old_name = self.name
2022-09-04 14:26:13 +00:00
2022-09-14 14:55:35 +00:00
try:
os.rename(
os.path.join(
os.environ.get("XDG_CONFIG_HOME",
os.environ["HOME"] + "/.config"),
2022-09-14 14:55:35 +00:00
"presets",
self.prefix,
to_slug_case(self.old_name) + ".json",
),
os.path.join(
os.environ.get("XDG_CONFIG_HOME",
os.environ["HOME"] + "/.config"),
2022-09-14 14:55:35 +00:00
"presets",
self.prefix,
to_slug_case(self.old_name) + ".json.to_delete",
),
)
print("rename")
self.set_name(self.name + "(" + _("Pending deletion") + ")")
2022-09-14 14:55:35 +00:00
print("renamed")
except Exception as exception:
buglog(exception)
self.delete_preset = True
2022-08-23 10:12:22 +00:00
2022-08-23 09:12:16 +00:00
def update_value(self):
2022-09-14 14:55:35 +00:00
self.preset.preset_name = self.name
self.preset.name = to_slug_case(self.name)
self.preset.save_preset()
os.remove(
2022-09-07 15:52:50 +00:00
os.path.join(
2022-09-14 14:55:35 +00:00
PRESET_DIR,
2022-09-07 15:52:50 +00:00
self.prefix,
to_slug_case(self.old_name) + ".json",
2022-09-10 08:58:34 +00:00
)
2022-09-14 14:55:35 +00:00
)
2022-08-23 08:20:45 +00:00
self.old_name = self.name
2022-09-04 14:08:37 +00:00
def on_delete_toast_dismissed(self, widget):
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",
)
)
except Exception as exception:
buglog(exception)
self.toast_overlay.add_toast(
Adw.Toast(title=_("Unable to delete preset"))
)
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:
buglog(exception)
2022-09-04 14:12:46 +00:00
finally:
self.win.reload_pref_group()
self.delete_preset = True
2022-09-03 17:04:33 +00:00
def on_undo_button_clicked(self, *_args):
self.delete_preset = False
self.delete_toast.dismiss()