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 os
|
2022-08-22 13:56:45 +00:00
|
|
|
|
2022-09-15 20:33:03 +00:00
|
|
|
from gi.repository import Gtk, Adw, Xdp, XdpGtk4
|
2022-08-24 19:29:11 +00:00
|
|
|
|
2022-09-14 14:55:35 +00:00
|
|
|
from gradience.modules.custom_presets import PRESET_DIR
|
|
|
|
|
2022-08-24 19:29:11 +00:00
|
|
|
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-09-14 14:56:35 +00:00
|
|
|
|
2022-08-22 13:56:45 +00:00
|
|
|
@Gtk.Template(resource_path=f"{rootdir}/ui/preset_row.ui")
|
2022-09-15 18:38:54 +00:00
|
|
|
class GradiencePresetRow(Adw.ExpanderRow):
|
2022-08-22 13:56:45 +00:00
|
|
|
__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-09-28 12:05:32 +00:00
|
|
|
btn_report = Gtk.Template.Child("btn_report")
|
|
|
|
star_button = Gtk.Template.Child("star_button")
|
2022-08-22 13:56:45 +00:00
|
|
|
|
2022-09-25 21:22:55 +00:00
|
|
|
def __init__(self, name, win, repo_name, file_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-09-25 21:22:55 +00:00
|
|
|
self.file_name = file_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-09-15 17:58:45 +00:00
|
|
|
self.set_subtitle(repo_name)
|
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-09-15 20:35:03 +00:00
|
|
|
self.btn_report.connect("clicked", self.on_report_btn_clicked)
|
2022-09-28 12:05:32 +00:00
|
|
|
self.star_button.connect("clicked", self.on_star_button_clicked)
|
|
|
|
|
2022-09-28 12:18:15 +00:00
|
|
|
if name in self.win.app.favourite:
|
|
|
|
self.star_button.set_icon_name("starred-symbolic")
|
|
|
|
else:
|
|
|
|
self.star_button.set_icon_name("non-starred-symbolic")
|
|
|
|
|
2022-09-28 12:05:32 +00:00
|
|
|
def on_star_button_clicked(self, *_args):
|
|
|
|
buglog("star")
|
|
|
|
if self.name in self.win.app.favourite:
|
|
|
|
self.win.app.favourite.remove(self.name)
|
2022-09-28 12:18:15 +00:00
|
|
|
self.star_button.set_icon_name("non-starred-symbolic")
|
2022-09-28 12:05:32 +00:00
|
|
|
else:
|
|
|
|
self.win.app.favourite.add(self.name)
|
2022-09-28 12:18:15 +00:00
|
|
|
self.star_button.set_icon_name("starred-symbolic")
|
2022-09-28 12:05:32 +00:00
|
|
|
self.win.app.save_favourite()
|
|
|
|
self.win.reload_pref_group()
|
2022-09-15 20:33:03 +00:00
|
|
|
|
2022-08-22 13:56:45 +00:00
|
|
|
@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
|
|
|
|
2022-09-22 19:54:33 +00:00
|
|
|
def on_undo_button_clicked(self, *_args):
|
|
|
|
buglog("undo")
|
|
|
|
self.delete_preset = False
|
|
|
|
self.delete_toast.dismiss()
|
|
|
|
|
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-09-15 20:33:03 +00:00
|
|
|
def on_report_btn_clicked(self, *_args):
|
|
|
|
buglog("report")
|
|
|
|
|
2022-09-15 20:37:17 +00:00
|
|
|
parent = XdpGtk4.parent_new_gtk(self.win)
|
2022-09-15 20:33:03 +00:00
|
|
|
|
|
|
|
def open_dir_callback(_, result):
|
|
|
|
self.app.portal.open_uri_finish(result)
|
|
|
|
|
|
|
|
self.app.portal.open_uri(
|
|
|
|
parent,
|
|
|
|
"https://github.com/GradienceTeam/Community/issues/new?assignees=daudix-UFO&labels=bug&template=preset_issue.yml&title=preset%3A+",
|
|
|
|
Xdp.OpenUriFlags.NONE,
|
|
|
|
None,
|
|
|
|
open_dir_callback,
|
|
|
|
)
|
|
|
|
|
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)
|
2022-09-22 22:02:15 +00:00
|
|
|
self.delete_toast.connect(
|
2022-09-23 11:14:21 +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-09-14 14:55:35 +00:00
|
|
|
try:
|
|
|
|
os.rename(
|
|
|
|
os.path.join(
|
2022-09-14 14:56:35 +00:00
|
|
|
os.environ.get("XDG_CONFIG_HOME",
|
|
|
|
os.environ["HOME"] + "/.config"),
|
2022-09-14 14:55:35 +00:00
|
|
|
"presets",
|
|
|
|
self.prefix,
|
2022-09-25 21:22:55 +00:00
|
|
|
self.file_name + ".json",
|
2022-09-14 14:55:35 +00:00
|
|
|
),
|
|
|
|
os.path.join(
|
2022-09-14 14:56:35 +00:00
|
|
|
os.environ.get("XDG_CONFIG_HOME",
|
|
|
|
os.environ["HOME"] + "/.config"),
|
2022-09-14 14:55:35 +00:00
|
|
|
"presets",
|
|
|
|
self.prefix,
|
2022-09-25 21:22:55 +00:00
|
|
|
self.file_name + ".json.to_delete",
|
2022-09-14 14:55:35 +00:00
|
|
|
),
|
|
|
|
)
|
2022-09-25 21:22:55 +00:00
|
|
|
|
2022-09-14 14:56:35 +00:00
|
|
|
self.set_name(self.name + "(" + _("Pending deletion") + ")")
|
2022-09-14 14:55:35 +00:00
|
|
|
except Exception as exception:
|
|
|
|
buglog(exception)
|
2022-09-22 12:48:29 +00:00
|
|
|
else:
|
|
|
|
self.props.visible = False
|
2022-09-22 19:54:33 +00:00
|
|
|
finally:
|
|
|
|
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,
|
2022-09-25 21:22:55 +00:00
|
|
|
self.file_name + ".json",
|
2022-09-10 08:58:34 +00:00
|
|
|
)
|
2022-09-14 14:55:35 +00:00
|
|
|
)
|
2022-09-03 16:54:18 +00:00
|
|
|
|
2022-09-04 14:08:37 +00:00
|
|
|
def on_delete_toast_dismissed(self, widget):
|
2022-09-22 19:54:33 +00:00
|
|
|
buglog("dismissed")
|
2022-09-03 16:54:18 +00:00
|
|
|
if self.delete_preset:
|
2022-09-22 19:54:33 +00:00
|
|
|
buglog("delete")
|
2022-09-03 16:54:18 +00:00
|
|
|
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,
|
2022-09-25 21:22:55 +00:00
|
|
|
self.file_name + ".json.to_delete",
|
2022-09-07 15:52:50 +00:00
|
|
|
)
|
|
|
|
)
|
2022-09-03 16:54:18 +00:00
|
|
|
except Exception as exception:
|
2022-09-10 17:34:50 +00:00
|
|
|
buglog(exception)
|
2022-09-03 16:54:18 +00:00
|
|
|
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:
|
2022-09-22 19:54:33 +00:00
|
|
|
buglog("undo")
|
2022-09-04 14:12:46 +00:00
|
|
|
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,
|
2022-09-25 21:22:55 +00:00
|
|
|
self.file_name + ".json.to_delete",
|
2022-09-07 15:52:50 +00:00
|
|
|
),
|
|
|
|
os.path.join(
|
|
|
|
os.environ.get(
|
|
|
|
"XDG_CONFIG_HOME", os.environ["HOME"] + "/.config"
|
|
|
|
),
|
|
|
|
"presets",
|
|
|
|
self.prefix,
|
2022-09-25 21:22:55 +00:00
|
|
|
self.file_name + ".json",
|
2022-09-07 15:52:50 +00:00
|
|
|
),
|
|
|
|
)
|
2022-09-04 14:12:46 +00:00
|
|
|
except Exception as exception:
|
2022-09-10 17:34:50 +00:00
|
|
|
buglog(exception)
|
2022-09-04 14:12:46 +00:00
|
|
|
finally:
|
|
|
|
self.win.reload_pref_group()
|
2022-09-03 16:54:18 +00:00
|
|
|
|
|
|
|
self.delete_preset = True
|