mirror of
https://github.com/GradienceTeam/Gradience.git
synced 2024-08-23 07:52:32 +00:00
fix: window freez when delete
This commit is contained in:
parent
94e7111c4e
commit
06b848a1c3
6 changed files with 151 additions and 121 deletions
|
@ -1,8 +1,54 @@
|
|||
using Gtk 4.0;
|
||||
using Adw 1;
|
||||
|
||||
Popover pop_actions {
|
||||
styles [
|
||||
"menu",
|
||||
]
|
||||
|
||||
Box {
|
||||
orientation: vertical;
|
||||
margin-top: 6;
|
||||
margin-bottom: 6;
|
||||
margin-start: 6;
|
||||
margin-end: 6;
|
||||
|
||||
|
||||
Button remove_button {
|
||||
valign: center;
|
||||
label: _("Remove preset");
|
||||
tooltip-text: _("Remove preset");
|
||||
clicked => on_remove_button_clicked();
|
||||
}
|
||||
|
||||
Separator {
|
||||
}
|
||||
|
||||
.GtkModelButton btn_report {
|
||||
text: _("Report a Bug…");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
template GradiencePresetRow : Adw.ActionRow {
|
||||
[suffix]
|
||||
Box {
|
||||
spacing: 6;
|
||||
|
||||
Label label_grade {
|
||||
valign: center;
|
||||
label: _("Unknown");
|
||||
|
||||
styles [
|
||||
"tag",
|
||||
"caption",
|
||||
]
|
||||
}
|
||||
|
||||
Separator {
|
||||
margin-top: 12;
|
||||
margin-bottom: 12;
|
||||
}
|
||||
|
||||
Stack value_stack {
|
||||
valign: center;
|
||||
hhomogeneous: false;
|
||||
|
@ -14,6 +60,9 @@ template GradiencePresetRow : Adw.ActionRow {
|
|||
icon-name: "checkmark-small-symbolic";
|
||||
tooltip-text: _("Apply preset");
|
||||
clicked => on_apply_button_clicked();
|
||||
styles [
|
||||
"flat",
|
||||
]
|
||||
}
|
||||
Entry name_entry {
|
||||
text: "Name";
|
||||
|
@ -21,21 +70,29 @@ template GradiencePresetRow : Adw.ActionRow {
|
|||
}
|
||||
}
|
||||
|
||||
[suffix]
|
||||
ToggleButton name_entry_toggle {
|
||||
valign: center;
|
||||
icon-name: "document-edit-symbolic";
|
||||
tooltip-text: _("Edit preset name");
|
||||
|
||||
toggled => on_name_entry_toggled();
|
||||
|
||||
styles [
|
||||
"flat",
|
||||
]
|
||||
}
|
||||
|
||||
[suffix]
|
||||
Button remove_button {
|
||||
MenuButton {
|
||||
valign: center;
|
||||
icon-name: "edit-delete-symbolic";
|
||||
tooltip-text: _("Remove preset");
|
||||
clicked => on_remove_button_clicked();
|
||||
popover: pop_actions;
|
||||
icon-name: "view-more-symbolic";
|
||||
|
||||
styles [
|
||||
"flat",
|
||||
]
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
@ -26,6 +26,11 @@ import aiohttp
|
|||
import asyncio
|
||||
|
||||
|
||||
PRESET_DIR = os.path.join(
|
||||
os.environ.get("XDG_CONFIG_HOME", os.environ["HOME"] + "/.config"),
|
||||
"presets",
|
||||
)
|
||||
|
||||
|
||||
async def main(repo):
|
||||
async with aiohttp.ClientSession() as session:
|
||||
|
@ -81,12 +86,16 @@ async def _download_preset(name, repo_name, url) -> None:
|
|||
|
||||
data = json.dumps(raw)
|
||||
|
||||
print(os.path.join(
|
||||
PRESET_DIR,
|
||||
repo_name,
|
||||
to_slug_case(name) + ".json",
|
||||
))
|
||||
|
||||
try:
|
||||
with open(
|
||||
os.path.join(
|
||||
os.environ.get("XDG_CONFIG_HOME",
|
||||
os.environ["HOME"] + "/.config"),
|
||||
"presets",
|
||||
PRESET_DIR,
|
||||
repo_name,
|
||||
to_slug_case(name) + ".json",
|
||||
),
|
||||
|
@ -94,7 +103,6 @@ async def _download_preset(name, repo_name, url) -> None:
|
|||
encoding="utf-8",
|
||||
) as f:
|
||||
f.write(data)
|
||||
f.close()
|
||||
except OSError as error:
|
||||
buglog(f"Failed to write data to a file. Exc: {error}")
|
||||
|
||||
|
|
|
@ -18,6 +18,8 @@ class Preset:
|
|||
"gtk3": "",
|
||||
}
|
||||
plugins = {}
|
||||
repo = "user"
|
||||
name = "new_preset"
|
||||
|
||||
def __init__(self, name=None, repo=None, preset_path=None, text=None, preset=None):
|
||||
if text: # load from ressource
|
||||
|
@ -26,13 +28,9 @@ class Preset:
|
|||
self.load_preset(preset=preset)
|
||||
else:
|
||||
self.preset_name = name
|
||||
if name is None:
|
||||
self.name = "new_preset"
|
||||
else:
|
||||
if name is not None:
|
||||
self.name = to_slug_case(name)
|
||||
if repo is None:
|
||||
self.repo = "user"
|
||||
else:
|
||||
if repo is not None:
|
||||
self.repo = repo
|
||||
if preset_path is None:
|
||||
self.preset_path = os.path.join(PRESET_DIR, repo, self.name + ".json")
|
||||
|
@ -70,6 +68,10 @@ class Preset:
|
|||
buglog(error, " -> preset : ", self.preset_path)
|
||||
|
||||
def save_preset(self, name=None, plugins_list=None, to=None):
|
||||
if to is None:
|
||||
self.preset_path = os.path.join(PRESET_DIR, self.repo, self.name + ".json")
|
||||
else:
|
||||
self.preset_path = to
|
||||
if not os.path.exists(
|
||||
os.path.join(
|
||||
PRESET_DIR,
|
||||
|
|
|
@ -21,9 +21,11 @@ import os
|
|||
|
||||
from gi.repository import Gtk, Adw
|
||||
|
||||
from gradience.modules.custom_presets import PRESET_DIR
|
||||
|
||||
from .constants import rootdir
|
||||
from .modules.utils import to_slug_case, buglog
|
||||
|
||||
from .modules.preset import Preset
|
||||
|
||||
@Gtk.Template(resource_path=f"{rootdir}/ui/preset_row.ui")
|
||||
class GradiencePresetRow(Adw.ActionRow):
|
||||
|
@ -52,6 +54,8 @@ class GradiencePresetRow(Adw.ActionRow):
|
|||
self.win = win
|
||||
self.toast_overlay = self.win.toast_overlay
|
||||
|
||||
self.preset = Preset(name, repo_name)
|
||||
|
||||
apply_button = Gtk.Template.Child("apply_button")
|
||||
rename_button = Gtk.Template.Child("rename_button")
|
||||
|
||||
|
@ -89,74 +93,11 @@ class GradiencePresetRow(Adw.ActionRow):
|
|||
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(
|
||||
"button-clicked", self.on_undo_button_clicked)
|
||||
|
||||
self.toast_overlay.add_toast(self.delete_toast)
|
||||
|
||||
self.win.old_name = self.name
|
||||
|
||||
self.do_delete_preset()
|
||||
|
||||
self.win.reload_pref_group()
|
||||
|
||||
def update_value(self):
|
||||
with open(
|
||||
os.path.join(
|
||||
os.environ.get("XDG_CONFIG_HOME",
|
||||
os.environ["HOME"] + "/.config"),
|
||||
"presets",
|
||||
self.prefix,
|
||||
to_slug_case(self.old_name) + ".json",
|
||||
),
|
||||
"r",
|
||||
encoding="utf-8",
|
||||
) as file:
|
||||
preset_text = file.read()
|
||||
preset = json.loads(preset_text)
|
||||
|
||||
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": "",
|
||||
}
|
||||
|
||||
with open(
|
||||
os.path.join(
|
||||
os.environ.get("XDG_CONFIG_HOME",
|
||||
os.environ["HOME"] + "/.config"),
|
||||
"presets",
|
||||
self.prefix,
|
||||
to_slug_case(self.name) + ".json",
|
||||
),
|
||||
"w",
|
||||
encoding="utf-8",
|
||||
) as file:
|
||||
object_to_write = {
|
||||
"name": self.name,
|
||||
"variables": variables,
|
||||
"palette": palette,
|
||||
"custom_css": custom_css,
|
||||
}
|
||||
file.write(json.dumps(object_to_write, indent=4))
|
||||
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",
|
||||
)
|
||||
)
|
||||
self.old_name = self.name
|
||||
|
||||
def do_delete_preset(self):
|
||||
if self.delete_preset:
|
||||
try:
|
||||
os.rename(
|
||||
os.path.join(
|
||||
|
@ -176,13 +117,29 @@ class GradiencePresetRow(Adw.ActionRow):
|
|||
to_slug_case(self.old_name) + ".json.to_delete",
|
||||
),
|
||||
)
|
||||
print("rename")
|
||||
self.set_name(self.name + "(" +_("Pending deletion") + ")")
|
||||
print("renamed")
|
||||
except Exception as exception:
|
||||
buglog(exception)
|
||||
finally:
|
||||
self.win.reload_pref_group()
|
||||
|
||||
self.delete_preset = True
|
||||
|
||||
#self.win.reload_pref_group()
|
||||
|
||||
def update_value(self):
|
||||
self.preset.preset_name = self.name
|
||||
self.preset.name = to_slug_case(self.name)
|
||||
self.preset.save_preset()
|
||||
os.remove(
|
||||
os.path.join(
|
||||
PRESET_DIR,
|
||||
self.prefix,
|
||||
to_slug_case(self.old_name) + ".json",
|
||||
)
|
||||
)
|
||||
self.old_name = self.name
|
||||
|
||||
def on_delete_toast_dismissed(self, widget):
|
||||
if self.delete_preset:
|
||||
try:
|
||||
|
|
|
@ -279,6 +279,7 @@ class GradiencePresetWindow(Adw.Window):
|
|||
self.reload_pref_group()
|
||||
|
||||
def reload_pref_group(self):
|
||||
print("reload")
|
||||
preset_directory = os.path.join(
|
||||
os.environ.get("XDG_CONFIG_HOME", os.environ["HOME"] + "/.config"),
|
||||
"presets",
|
||||
|
@ -348,6 +349,8 @@ class GradiencePresetWindow(Adw.Window):
|
|||
self.installed.remove(self.preset_list)
|
||||
self.installed.remove(self.builtin_preset_list)
|
||||
|
||||
print("reload 2")
|
||||
|
||||
self.builtin_preset_list = Adw.PreferencesGroup()
|
||||
self.builtin_preset_list.set_title(_("Builtin Presets"))
|
||||
for preset, preset_name in self.builtin_presets.items():
|
||||
|
@ -372,6 +375,8 @@ class GradiencePresetWindow(Adw.Window):
|
|||
)
|
||||
buglog(f"preset_check: {presets_check}")
|
||||
|
||||
print("reload 3")
|
||||
|
||||
if presets_check:
|
||||
for repo, presets in self.custom_presets.items():
|
||||
for preset, preset_name in presets.items():
|
||||
|
@ -387,6 +392,7 @@ class GradiencePresetWindow(Adw.Window):
|
|||
)
|
||||
self.preset_list.add(self.preset_empty)
|
||||
self.installed.add(self.preset_list)
|
||||
print("reload end")
|
||||
|
||||
def reload_repos_group(self):
|
||||
self.repos.remove(self.repos_list)
|
||||
|
|
Loading…
Reference in a new issue