diff --git a/data/ui/window.blp b/data/ui/window.blp index a811b30de..732c558fc 100644 --- a/data/ui/window.blp +++ b/data/ui/window.blp @@ -118,6 +118,10 @@ template GradienceMainWindow : Adw.ApplicationWindow { menu main-menu { section { + item { + label: _("Restore Applied Color Scheme"); + action: "app.restore_color_scheme"; + } item { label: _("Reset Applied Color Scheme"); action: "app.reset_color_scheme"; diff --git a/src/main.py b/src/main.py index ecbeeaaca..4d966a56f 100644 --- a/src/main.py +++ b/src/main.py @@ -110,6 +110,8 @@ def do_activate(self): "show_gtk4_demo", self.show_gtk4_demo) + self.create_action("restore_color_scheme", + self.show_restore_color_scheme_dialog) self.create_action( "reset_color_scheme", self.show_reset_color_scheme_dialog) @@ -411,6 +413,18 @@ def show_apply_color_scheme_dialog(self, *_args): dialog.connect("response", self.apply_color_scheme) dialog.present() + def show_restore_color_scheme_dialog(self, *_args): + dialog = GradienceAppTypeDialog( + _("Restore applied color scheme?"), + _("Make sure you have the current settings saved as a preset."), + "restore", + _("Restore"), + Adw.ResponseAppearance.DESTRUCTIVE, + transient_for=self.props.active_window, + ) + dialog.connect("response", self.restore_color_scheme) + dialog.present() + def show_reset_color_scheme_dialog(self, *_args): dialog = GradienceAppTypeDialog( _("Reset applied color scheme?"), @@ -556,6 +570,15 @@ def apply_color_scheme(self, widget, response): if not os.path.exists(gtk4_dir): os.makedirs(gtk4_dir) gtk4_css = self.generate_gtk_css("gtk4") + contents = "" + with open( + os.path.join(gtk4_dir, "gtk.css"), "r", encoding="utf-8" + ) as file: + contents = file.read() + with open( + os.path.join(gtk4_dir, "gtk.css.bak"), "w", encoding="utf-8" + ) as file: + file.write(contents) with open( os.path.join(gtk4_dir, "gtk.css"), "w", encoding="utf-8" ) as file: @@ -578,6 +601,48 @@ def apply_color_scheme(self, widget, response): Adw.Toast(title=_("Preset set sucessfully")) ) + def restore_color_scheme(self, widget, response): + if response == "restore": + if widget.get_app_types()["gtk4"]: + file = Gio.File.new_for_path( + os.path.join( + os.environ.get( + "XDG_CONFIG_HOME", os.environ["HOME"] + "/.config" + ), + "gtk-4.0/gtk.css.bak", + ) + ) + try: + backup = open( + os.path.join( + os.environ.get( + "XDG_CONFIG_HOME", os.environ["HOME"] + "/.config" + ), + "gtk-4.0/gtk.css.bak", + ), + "r", + encoding="utf-8", + ) + contents = backup.read() + backup.close() + gtk4css = open( + os.path.join( + os.environ.get( + "XDG_CONFIG_HOME", os.environ["HOME"] + "/.config" + ), + "gtk-4.0/gtk.css", + ), + "w", + encoding="utf-8", + ) + gtk4css.write(contents) + gtk4css.close() + except FileNotFoundError: + self.win.toast_overlay.add_toast( + Adw.Toast(title=_("Could not restore GTK4 backup")) + ) + pass + def reset_color_scheme(self, widget, response): if response == "reset": if widget.get_app_types()["gtk4"]: