Gradience/src/main.py

521 lines
20 KiB
Python
Raw Normal View History

# main.py
#
2022-08-11 08:16:44 +00:00
# Change the look of Adwaita, with ease
# Copyright (C) 2022 Adwaita Manager Team
#
2022-08-11 08:16:44 +00:00
# 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/>.
import sys
import json
2022-07-17 17:07:40 +00:00
import os
2022-07-18 10:51:25 +00:00
import re
import traceback
2022-07-25 11:05:17 +00:00
from anyascii import anyascii
2022-07-29 11:56:36 +00:00
import gi
from gi.repository import Gtk, Gdk, Gio, Adw, GLib, Xdp, XdpGtk4
2022-08-11 12:22:08 +00:00
from material_color_utilities_python import *
2022-07-29 11:56:36 +00:00
from .settings_schema import settings_schema
from .window import AdwcustomizerMainWindow
from .palette_shades import AdwcustomizerPaletteShades
from .option import AdwcustomizerOption
from .app_type_dialog import AdwcustomizerAppTypeDialog
2022-07-24 17:46:37 +00:00
from .custom_css_group import AdwcustomizerCustomCSSGroup
2022-08-10 15:51:55 +00:00
from .plugins_list import AdwcustomizerPluginsList
2022-08-10 08:46:52 +00:00
2022-07-18 10:51:25 +00:00
def to_slug_case(non_slug):
2022-07-25 11:30:16 +00:00
return re.sub(r"[^0-9a-z]+", "-", anyascii(non_slug).lower()).strip("-")
2022-08-10 08:46:52 +00:00
class AdwcustomizerApplication(Adw.Application):
"""The main application singleton class."""
2022-07-20 10:18:55 +00:00
def __init__(self, version):
2022-08-10 08:46:52 +00:00
super().__init__(
application_id="com.github.AdwCustomizerTeam.AdwCustomizer",
flags=Gio.ApplicationFlags.FLAGS_NONE,
)
2022-07-20 10:18:55 +00:00
self.version = version
self.portal = Xdp.Portal()
2022-07-29 11:56:36 +00:00
self.preset_name = ""
2022-08-01 19:42:42 +00:00
self.is_dirty = False
self.variables = {}
2022-07-29 11:56:36 +00:00
self.pref_variables = {}
self.palette = {}
2022-07-29 11:56:36 +00:00
self.pref_palette_shades = {}
2022-07-24 17:46:37 +00:00
self.custom_css = {}
2022-07-29 11:56:36 +00:00
self.custom_css_group = None
self.custom_presets = {}
self.global_errors = []
2022-07-24 17:46:37 +00:00
self.current_css_provider = None
2022-07-29 11:56:36 +00:00
self.is_ready = False
2022-07-29 11:56:36 +00:00
def do_activate(self):
"""Called when the application is activated.
We raise the application's main window, creating it if
necessary.
"""
2022-08-10 15:11:39 +00:00
self.win = self.props.active_window
if not self.win:
self.win = AdwcustomizerMainWindow(application=self)
self.create_action("open_preset_directory", self.open_preset_directory)
2022-08-10 08:46:52 +00:00
self.create_stateful_action(
"load_preset",
GLib.VariantType.new("s"),
GLib.Variant("s", "adwaita"),
self.load_preset_action,
)
self.create_action("apply_color_scheme",
self.show_apply_color_scheme_dialog)
self.create_action("reset_color_scheme",
self.show_reset_color_scheme_dialog)
2022-07-18 10:51:25 +00:00
self.create_action("save_preset", self.show_save_preset_dialog)
self.create_action("about", self.show_about_window)
self.reload_user_defined_presets()
2022-08-10 08:46:52 +00:00
self.load_preset_from_resource(
"/com/github/AdwCustomizerTeam/AdwCustomizer/presets/adwaita.json"
)
2022-08-10 15:11:39 +00:00
self.win.present()
def reload_user_defined_presets(self):
if self.props.active_window.presets_menu.get_n_items() > 1:
self.props.active_window.presets_menu.remove(1)
2022-08-10 08:46:52 +00:00
preset_directory = os.path.join(
os.environ.get("XDG_CONFIG_HOME", os.environ["HOME"] + "/.config" ), "presets")
2022-08-01 08:51:39 +00:00
if not os.path.exists(preset_directory):
os.makedirs(preset_directory)
self.custom_presets.clear()
2022-08-01 08:51:39 +00:00
for file_name in os.listdir(preset_directory):
2022-07-25 06:11:02 +00:00
if file_name.endswith(".json"):
2022-07-18 11:27:31 +00:00
try:
2022-08-10 08:46:52 +00:00
with open(
os.path.join(preset_directory, file_name), "r", encoding="utf-8"
) as file:
2022-07-24 18:43:32 +00:00
preset_text = file.read()
2022-07-18 11:27:31 +00:00
preset = json.loads(preset_text)
2022-08-10 08:46:52 +00:00
if preset.get("variables") is None:
raise KeyError("variables")
if preset.get("palette") is None:
raise KeyError("palette")
self.custom_presets[file_name.replace(
".json", "")] = preset["name"]
2022-08-01 09:43:42 +00:00
except Exception:
2022-08-10 08:46:52 +00:00
self.global_errors.append(
{
"error": _("Failed to load preset"),
"element": file_name,
"line": traceback.format_exc().strip(),
}
)
2022-08-10 15:11:39 +00:00
self.win.toast_overlay.add_toast(Adw.Toast(title=_("Failed to load preset")))
2022-08-10 15:05:47 +00:00
self.props.active_window.update_errors(self.global_errors)
2022-07-18 11:27:31 +00:00
custom_menu_section = Gio.Menu()
2022-07-24 18:43:32 +00:00
for preset, preset_name in self.custom_presets.items():
2022-07-18 11:27:31 +00:00
menu_item = Gio.MenuItem()
2022-07-24 18:43:32 +00:00
menu_item.set_label(preset_name)
if not preset.startswith("error"):
2022-08-10 08:46:52 +00:00
menu_item.set_action_and_target_value(
"app.load_preset", GLib.Variant("s", "custom-" + preset)
)
else:
menu_item.set_action_and_target_value("")
2022-07-18 11:27:31 +00:00
custom_menu_section.append_item(menu_item)
open_in_file_manager_item = Gio.MenuItem()
open_in_file_manager_item.set_label(_("Open in File Manager"))
2022-08-10 08:46:52 +00:00
open_in_file_manager_item.set_action_and_target_value(
"app.open_preset_directory"
)
custom_menu_section.append_item(open_in_file_manager_item)
2022-08-10 08:46:52 +00:00
self.props.active_window.presets_menu.append_section(
_("User Defined Presets"), custom_menu_section
)
def open_preset_directory(self, *_args):
parent = XdpGtk4.parent_new_gtk(self.props.active_window)
2022-08-10 08:46:52 +00:00
2022-07-24 18:43:32 +00:00
def open_dir_callback(_, result):
self.portal.open_uri_finish(result)
2022-08-10 08:46:52 +00:00
self.portal.open_uri(
parent,
"file://" + os.path.join(os.environ.get("XDG_CONFIG_HOME", os.environ["HOME"] + "/.config" ), "presets"),
Xdp.OpenUriFlags.NONE,
None,
2022-08-10 08:46:52 +00:00
open_dir_callback,
)
def load_preset_from_file(self, preset_path):
preset_text = ""
2022-08-10 08:46:52 +00:00
with open(preset_path, "r", encoding="utf-8") as file:
2022-07-24 18:43:32 +00:00
preset_text = file.read()
self.load_preset_variables(json.loads(preset_text))
def load_preset_from_resource(self, preset_path):
2022-08-10 08:46:52 +00:00
preset_text = Gio.resources_lookup_data(
preset_path, 0).get_data().decode()
self.load_preset_variables(json.loads(preset_text))
def load_preset_variables(self, preset):
2022-08-01 19:42:42 +00:00
self.is_ready = False
self.preset_name = preset["name"]
self.variables = preset["variables"]
self.palette = preset["palette"]
2022-07-24 17:46:37 +00:00
if "custom_css" in preset:
self.custom_css = preset["custom_css"]
else:
for app_type in settings_schema["custom_css_app_types"]:
self.custom_css[app_type] = ""
for key in self.variables.keys():
if key in self.pref_variables:
self.pref_variables[key].update_value(self.variables[key])
for key in self.palette.keys():
if key in self.pref_palette_shades:
self.pref_palette_shades[key].update_shades(self.palette[key])
2022-07-24 17:46:37 +00:00
self.custom_css_group.load_custom_css(self.custom_css)
2022-08-01 19:42:42 +00:00
self.clear_dirty()
self.reload_variables()
2022-08-11 10:55:12 +00:00
def update_theme_from_monet(self, theme, tone):
2022-08-11 12:22:08 +00:00
palettes = theme["palettes"]
palette = {}
i = 0
for color in palettes.values():
i += 1
2022-08-11 12:23:31 +00:00
palette[str(i)] = hexFromArgb(color.tone(int(tone.get_string())))
2022-08-11 12:22:08 +00:00
2022-08-11 12:24:16 +00:00
self.pref_palette_shades["monet"].update_shades(palette)
2022-08-11 10:55:12 +00:00
2022-07-24 17:46:37 +00:00
def generate_gtk_css(self, app_type):
final_css = ""
2022-07-24 17:46:37 +00:00
for key in self.variables.keys():
2022-07-29 12:04:18 +00:00
final_css += f"@define-color {key} {self.variables[key]};\n"
2022-07-24 17:46:37 +00:00
for prefix_key in self.palette.keys():
for key in self.palette[prefix_key].keys():
2022-07-29 12:04:18 +00:00
final_css += f"@define-color {prefix_key + key} {self.palette[prefix_key][key]};\n"
2022-07-24 17:46:37 +00:00
final_css += self.custom_css.get(app_type, "")
return final_css
2022-08-01 19:42:42 +00:00
def mark_as_dirty(self):
self.is_dirty = True
2022-08-10 13:05:54 +00:00
self.props.active_window.save_preset_button.get_child().set_icon_name(
2022-08-10 15:15:46 +00:00
"disk-unsaved-symbolic"
2022-08-10 13:05:54 +00:00
)
self.props.active_window.save_preset_button.get_child().set_tooltip_text(
2022-08-10 08:46:52 +00:00
_("Unsaved changes")
)
2022-08-01 19:42:42 +00:00
def clear_dirty(self):
self.is_dirty = False
2022-08-10 15:15:46 +00:00
self.props.active_window.save_preset_button.get_child().set_icon_name(
"disk-saved-symbolic"
)
2022-08-01 19:42:42 +00:00
self.props.active_window.save_preset_button.get_child().set_label("")
def reload_variables(self):
2022-07-18 19:20:02 +00:00
parsing_errors = []
2022-07-24 17:46:37 +00:00
gtk_css = self.generate_gtk_css("gtk4")
css_provider = Gtk.CssProvider()
2022-08-10 08:46:52 +00:00
2022-07-24 18:43:32 +00:00
def on_error(_, section, error):
2022-07-18 19:20:02 +00:00
start_location = section.get_start_location().chars
end_location = section.get_end_location().chars
line_number = section.get_end_location().lines
2022-08-10 08:46:52 +00:00
parsing_errors.append(
{
"error": error.message,
"element": gtk_css[start_location:end_location].strip(),
"line": gtk_css.splitlines()[line_number]
if line_number < len(gtk_css.splitlines())
else "<last line>",
}
)
css_provider.connect("parsing-error", on_error)
2022-07-22 15:10:52 +00:00
css_provider.load_from_data(gtk_css.encode())
2022-08-10 08:46:52 +00:00
self.props.active_window.update_errors(
self.global_errors + parsing_errors)
# loading with the priority above user to override the applied config
2022-07-24 17:46:37 +00:00
if self.current_css_provider is not None:
2022-08-10 08:46:52 +00:00
Gtk.StyleContext.remove_provider_for_display(
Gdk.Display.get_default(), self.current_css_provider
)
Gtk.StyleContext.add_provider_for_display(
Gdk.Display.get_default(),
css_provider,
Gtk.STYLE_PROVIDER_PRIORITY_USER + 1,
)
2022-07-24 17:46:37 +00:00
self.current_css_provider = css_provider
2022-08-01 19:42:42 +00:00
self.is_ready = True
def load_preset_action(self, _unused, *args):
if args[0].get_string().startswith("custom-"):
2022-08-10 08:46:52 +00:00
self.load_preset_from_file(
os.path.join(
os.environ.get("XDG_CONFIG_HOME", os.environ["HOME"] + "/.config" ),
2022-08-10 08:46:52 +00:00
"presets",
args[0].get_string().replace("custom-", "", 1) + ".json",
)
)
else:
2022-08-10 08:46:52 +00:00
self.load_preset_from_resource(
"/com/github/AdwCustomizerTeam/AdwCustomizer/presets/"
+ args[0].get_string()
+ ".json"
)
Gio.SimpleAction.set_state(self.lookup_action("load_preset"), args[0])
def show_apply_color_scheme_dialog(self, *_args):
2022-08-10 08:46:52 +00:00
dialog = AdwcustomizerAppTypeDialog(
_("Apply this color scheme?"),
_(
"Warning: any custom CSS files for those app types will be irreversibly overwritten!"
),
"apply",
_("Apply"),
Adw.ResponseAppearance.SUGGESTED,
transient_for=self.props.active_window,
)
2022-07-22 15:10:52 +00:00
dialog.connect("response", self.apply_color_scheme)
dialog.present()
def show_reset_color_scheme_dialog(self, *_args):
2022-08-10 08:46:52 +00:00
dialog = AdwcustomizerAppTypeDialog(
_("Reset applied color scheme?"),
_("Make sure you have the current settings saved as a preset."),
"reset",
_("Reset"),
Adw.ResponseAppearance.DESTRUCTIVE,
transient_for=self.props.active_window,
)
2022-07-22 15:10:52 +00:00
dialog.connect("response", self.reset_color_scheme)
dialog.present()
def show_save_preset_dialog(self, *_args):
2022-08-10 08:46:52 +00:00
dialog = Adw.MessageDialog(
transient_for=self.props.active_window,
heading=_("Save preset as..."),
body=_(
"Saving preset to <tt>{0}</tt>. If that preset already exists, it will be overwritten!"
).format(
os.path.join(
os.environ.get("XDG_CONFIG_HOME", os.environ["HOME"] + "/.config" ),
2022-08-10 08:46:52 +00:00
"presets",
to_slug_case(self.preset_name) + ".json",
)
),
body_use_markup=True,
)
2022-07-18 10:51:25 +00:00
dialog.add_response("cancel", _("Cancel"))
dialog.add_response("save", _("Save"))
2022-08-10 08:46:52 +00:00
dialog.set_response_appearance(
"save", Adw.ResponseAppearance.SUGGESTED)
2022-07-18 10:51:25 +00:00
dialog.set_default_response("cancel")
dialog.set_close_response("cancel")
preset_entry = Gtk.Entry(placeholder_text="Preset Name")
preset_entry.set_text(self.preset_name)
2022-08-10 08:46:52 +00:00
def on_preset_entry_change(*_args):
2022-07-18 10:51:25 +00:00
if len(preset_entry.get_text()) == 0:
2022-08-10 08:46:52 +00:00
dialog.set_body(
_(
"Saving preset to <tt>{0}</tt>. If that preset already exists, it will be overwritten!"
).format(os.path.join(os.environ.get("XDG_CONFIG_HOME", os.environ["HOME"] + "/.config" ), "presets"))
2022-08-10 08:46:52 +00:00
)
2022-07-18 10:51:25 +00:00
dialog.set_response_enabled("save", False)
else:
2022-08-10 08:46:52 +00:00
dialog.set_body(
_(
"Saving preset to <tt>{0}</tt>. If that preset already exists, it will be overwritten!"
).format(
os.path.join(
os.environ.get("XDG_CONFIG_HOME", os.environ["HOME"] + "/.config" ),
2022-08-10 08:46:52 +00:00
"presets",
to_slug_case(preset_entry.get_text()) + ".json",
)
)
)
2022-07-18 10:51:25 +00:00
dialog.set_response_enabled("save", True)
2022-08-10 08:46:52 +00:00
2022-07-18 10:51:25 +00:00
preset_entry.connect("changed", on_preset_entry_change)
dialog.set_extra_child(preset_entry)
dialog.connect("response", self.save_preset, preset_entry)
dialog.present()
def save_preset(self, _unused, response, entry):
2022-07-18 10:51:25 +00:00
if response == "save":
2022-08-10 08:46:52 +00:00
with open(
os.path.join(
os.environ.get("XDG_CONFIG_HOME", os.environ["HOME"] + "/.config" ),
2022-08-10 08:46:52 +00:00
"presets",
to_slug_case(entry.get_text()) + ".json",
),
"w",
encoding="utf-8",
) as file:
2022-07-18 10:51:25 +00:00
object_to_write = {
"name": entry.get_text(),
"variables": self.variables,
2022-07-24 17:46:37 +00:00
"palette": self.palette,
2022-08-10 08:46:52 +00:00
"custom_css": self.custom_css,
2022-07-18 10:51:25 +00:00
}
2022-07-24 18:43:32 +00:00
file.write(json.dumps(object_to_write, indent=4))
2022-08-01 19:42:42 +00:00
self.clear_dirty()
2022-08-10 15:11:39 +00:00
self.win.toast_overlay.add_toast(Adw.Toast(title=_("Scheme successfully saved!")))
2022-07-18 10:51:25 +00:00
2022-07-22 15:10:52 +00:00
def apply_color_scheme(self, widget, response):
if response == "apply":
if widget.get_app_types()["gtk4"]:
2022-08-10 08:46:52 +00:00
gtk4_dir = os.path.join(
os.environ.get("XDG_CONFIG_HOME", os.environ["HOME"] + "/.config" ), "gtk-4.0")
if not os.path.exists(gtk4_dir):
os.makedirs(gtk4_dir)
2022-07-24 17:46:37 +00:00
gtk4_css = self.generate_gtk_css("gtk4")
2022-08-10 08:46:52 +00:00
with open(
os.path.join(gtk4_dir, "gtk.css"), "w", encoding="utf-8"
) as file:
2022-07-24 18:43:32 +00:00
file.write(gtk4_css)
if widget.get_app_types()["gtk3"]:
2022-08-10 08:46:52 +00:00
gtk3_dir = os.path.join(
os.environ.get("XDG_CONFIG_HOME", os.environ["HOME"] + "/.config" ), "gtk-3.0")
if not os.path.exists(gtk3_dir):
os.makedirs(gtk3_dir)
2022-07-24 17:46:37 +00:00
gtk3_css = self.generate_gtk_css("gtk3")
2022-08-10 08:46:52 +00:00
with open(
os.path.join(gtk3_dir, "gtk.css"), "w", encoding="utf-8"
) as file:
2022-07-24 18:43:32 +00:00
file.write(gtk3_css)
2022-08-10 15:11:39 +00:00
self.win.toast_overlay.add_toast(Adw.Toast(title=_("Scheme set successfully!")))
2022-07-22 15:10:52 +00:00
def reset_color_scheme(self, widget, response):
if response == "reset":
if widget.get_app_types()["gtk4"]:
2022-08-10 08:46:52 +00:00
file = Gio.File.new_for_path(
os.path.join(
2022-08-10 17:23:10 +00:00
os.environ.get("XDG_CONFIG_HOME", os.environ["HOME"] + "/.config" ), "gtk-4.0/gtk.css")
2022-08-10 08:46:52 +00:00
)
try:
file.delete()
2022-08-01 09:43:42 +00:00
except Exception:
pass
2022-08-10 17:23:10 +00:00
if widget.get_app_types()["gtk3"]:
2022-08-10 08:46:52 +00:00
file = Gio.File.new_for_path(
os.path.join(
2022-08-10 17:23:10 +00:00
os.environ.get("XDG_CONFIG_HOME", os.environ["HOME"] + "/.config" ), "gtk-3.0/gtk.css")
2022-08-10 08:46:52 +00:00
)
try:
file.delete()
2022-08-01 09:43:42 +00:00
except Exception:
pass
2022-08-11 10:38:57 +00:00
self.win.toast_overlay.add_toast(Adw.Toast(title=_("Reset successfully!")))
def show_about_window(self, *_args):
2022-08-10 08:46:52 +00:00
about = Adw.AboutWindow(
transient_for=self.props.active_window,
application_name=_("Adwaita Manager"),
application_icon="com.github.AdwCustomizerTeam.AdwCustomizer",
developer_name=_("Adwaita Manager Team"),
2022-08-10 12:18:38 +00:00
website="https://github.com/AdwCustomizerTeam/AdwCustomizer",
support_url="https://github.com/orgs/AdwCustomizerTeam/discussions",
issue_url="https://github.com/AdwCustomizerTeam/AdwCustomizer/issues",
2022-08-10 08:46:52 +00:00
developers=[
'Artyom "ArtyIF" Fomin https://github.com/ArtyIF',
2022-08-10 12:18:38 +00:00
"0xMRTT https://github.com/0xMRTT",
2022-08-11 10:57:29 +00:00
"Verantor https://github.com/Verantor",
2022-08-10 12:18:38 +00:00
],
artists=[
'David "Daudix UFO" Lapshin https://github.com/daudix-UFO'
],
designers=[
'David "Daudix UFO" Lapshin https://github.com/daudix-UFO'
2022-08-10 08:46:52 +00:00
],
# Translators: This is a place to put your credits (formats: "Name https://example.com" or "Name <email@example.com>", no quotes) and is not meant to be translated literally.
2022-08-10 12:18:38 +00:00
translator_credits="""Maxime V https://www.transifex.com/user/profile/Adaoh/
FineFindus https://github.com/FineFindus
Karol Lademan https://www.transifex.com/user/profile/karlod/
Monty Monteusz https://www.transifex.com/user/profile/MontyQIQI/
Renato Corrêa https://www.transifex.com/user/profile/renatocrrs/
Aggelos Tselios https://www.transifex.com/user/profile/AndroGR/
David "Daudix UFO" Lapshin https://github.com/daudix-UFO'
2022-08-11 10:57:29 +00:00
0xMRTT https://github.com/0xMRTT
2022-08-10 12:18:38 +00:00
Juanjo Cillero https://www.transifex.com/user/profile/renux918/
Taylan Tatlı https://www.transifex.com/user/profile/TaylanTatli34/""",
2022-08-10 08:46:52 +00:00
copyright="© 2022 Adwaita Manager Team",
license_type=Gtk.License.MIT_X11,
)
about.present()
2022-07-24 17:46:37 +00:00
def update_custom_css_text(self, app_type, new_value):
self.custom_css[app_type] = new_value
self.reload_variables()
def create_action(self, name, callback, shortcuts=None):
"""Add an application action.
Args:
name: the name of the action
callback: the function to be called when the action is
activated
shortcuts: an optional list of accelerators
"""
action = Gio.SimpleAction.new(name, None)
action.connect("activate", callback)
self.add_action(action)
if shortcuts:
self.set_accels_for_action(f"app.{name}", shortcuts)
2022-08-10 08:46:52 +00:00
def create_stateful_action(
self, name, parameter_type, initial_state, callback, shortcuts=None
):
"""Add a stateful application action."""
action = Gio.SimpleAction.new_stateful(
name, parameter_type, initial_state)
action.connect("activate", callback)
self.add_action(action)
if shortcuts:
self.set_accels_for_action(f"app.{name}", shortcuts)
def main(version):
"""The application's entry point."""
2022-07-20 10:18:55 +00:00
app = AdwcustomizerApplication(version)
return app.run(sys.argv)