From d6796bc5a52bdbd3d2045131ce43e592dc216e22 Mon Sep 17 00:00:00 2001 From: DYefremov Date: Tue, 17 Aug 2021 11:00:13 +0300 Subject: [PATCH] locale setting refactoring --- app/settings.py | 3 +++ app/ui/dialogs.py | 4 +-- app/ui/mac_style.css | 43 ++++++++++++++++++++++++++++++ app/ui/uicommons.py | 63 ++++++++++++++++++++++++++++++++------------ 4 files changed, 94 insertions(+), 19 deletions(-) create mode 100644 app/ui/mac_style.css diff --git a/app/settings.py b/app/settings.py index 29e081ac..e7984ccf 100644 --- a/app/settings.py +++ b/app/settings.py @@ -13,8 +13,11 @@ HOME_PATH = str(Path.home()) CONFIG_PATH = HOME_PATH + "/.config/demon-editor/" CONFIG_FILE = CONFIG_PATH + "config.json" DATA_PATH = HOME_PATH + "/DemonEditor/data/" +GTK_PATH = os.environ.get("GTK_PATH", None) IS_DARWIN = sys.platform == "darwin" +IS_WIN = sys.platform == "win32" +IS_LINUX = sys.platform == "linux" class Defaults(Enum): diff --git a/app/ui/dialogs.py b/app/ui/dialogs.py index 1c7a2330..e02f6aa3 100644 --- a/app/ui/dialogs.py +++ b/app/ui/dialogs.py @@ -1,5 +1,5 @@ """ Common module for showing dialogs """ -import locale +import gettext from enum import Enum from functools import lru_cache from pathlib import Path @@ -176,7 +176,7 @@ def get_dialog_from_xml(dialog_type, transient, use_header=0, title=""): def get_message(message): """ returns translated message """ - return locale.dgettext(TEXT_DOMAIN, message) + return gettext.dgettext(TEXT_DOMAIN, message) @lru_cache(maxsize=5) diff --git a/app/ui/mac_style.css b/app/ui/mac_style.css new file mode 100644 index 00000000..12fac8b4 --- /dev/null +++ b/app/ui/mac_style.css @@ -0,0 +1,43 @@ +* { + -GtkDialog-action-area-border: 5em; +} + +entry { + min-height: 2em; +} + +button { + min-height: 1.5em; + padding-left: 0.5em; + padding-right: 0.5em; + padding-top: 0.1em; + padding-bottom: 0.1em; +} + +spinbutton { + min-height: 1.5em; +} + +toolbutton { + padding: 0.1em; +} + +spinner { + padding-left: 1em; + padding-right: 1em; +} + +infobar { + min-height: 2em; +} + +switch slider { + min-height: 1.5em; + min-width: 1.5em; +} + +paned > separator { + background-repeat: no-repeat; + background-position: center; + background-size: 1px 24px; +} diff --git a/app/ui/uicommons.py b/app/ui/uicommons.py index 8fb2b515..c88d038b 100644 --- a/app/ui/uicommons.py +++ b/app/ui/uicommons.py @@ -7,17 +7,16 @@ import gi gi.require_version("Gtk", "3.0") gi.require_version("Gdk", "3.0") -gi.require_version("Notify", "0.7") -from gi.repository import Gtk, Gdk, Notify +from gi.repository import Gtk, Gdk -from app.settings import Settings, SettingsException, IS_DARWIN +from app.settings import Settings, SettingsException, IS_DARWIN, GTK_PATH, IS_LINUX -# Init notify -Notify.init("DemonEditor") -# Setting mod mask for the keyboard depending on the platform. +# Setting mod mask for keyboard depending on platform MOD_MASK = Gdk.ModifierType.MOD2_MASK if IS_DARWIN else Gdk.ModifierType.CONTROL_MASK -# Path to *.glade files. -UI_RESOURCES_PATH = "app/ui/" if os.path.exists("app/ui/") else "/usr/share/demoneditor/app/ui/" +# Path to *.glade files +UI_RESOURCES_PATH = "app/ui/" if os.path.exists("app/ui/") else "ui/" +LANG_PATH = UI_RESOURCES_PATH + "lang" +NOTIFY_IS_INIT = False IS_GNOME_SESSION = int(bool(os.environ.get("GNOME_DESKTOP_SESSION_ID"))) # Translation. TEXT_DOMAIN = "demon-editor" @@ -29,18 +28,44 @@ except SettingsException: pass else: os.environ["LANGUAGE"] = settings.language - if UI_RESOURCES_PATH == "app/ui/": - locale.bindtextdomain(TEXT_DOMAIN, UI_RESOURCES_PATH + "lang") - st = Gtk.Settings().get_default() APP_FONT = st.get_property("gtk-font-name") - if not settings.list_font: - settings.list_font = APP_FONT + st.set_property("gtk-application-prefer-dark-theme", settings.dark_mode) if settings.is_themes_support: st.set_property("gtk-theme-name", settings.theme) st.set_property("gtk-icon-theme-name", settings.icon_theme) + else: + if IS_DARWIN: + style_provider = Gtk.CssProvider() + s_path = "{}mac_style.css".format(GTK_PATH + "/" + UI_RESOURCES_PATH if GTK_PATH else UI_RESOURCES_PATH) + style_provider.load_from_path(s_path) + Gtk.StyleContext.add_provider_for_screen(Gdk.Screen.get_default(), style_provider, + Gtk.STYLE_PROVIDER_PRIORITY_APPLICATION) +if IS_LINUX: + locale.bindtextdomain(TEXT_DOMAIN, LANG_PATH) + # Init notify + try: + gi.require_version("Notify", "0.7") + from gi.repository import Notify + except ImportError: + pass + else: + NOTIFY_IS_INIT = Notify.init("DemonEditor") +elif IS_DARWIN: + import gettext + + if GTK_PATH: + LANG_PATH = GTK_PATH + "/share/locale" + gettext.bindtextdomain(TEXT_DOMAIN, LANG_PATH) + # For launching from the bundle. + if os.getcwd() == "/" and GTK_PATH: + os.chdir(GTK_PATH) +else: + locale.setlocale(locale.LC_NUMERIC, "C") + +# Icons. theme = Gtk.IconTheme.get_default() theme.append_search_path(UI_RESOURCES_PATH + "icons") @@ -84,10 +109,14 @@ def show_notification(message, timeout=10000, urgency=1): @param timeout: milliseconds @param urgency: 0 - low, 1 - normal, 2 - critical """ - notify = Notify.Notification.new("DemonEditor", message, "demon-editor") - notify.set_urgency(urgency) - notify.set_timeout(timeout) - notify.show() + if IS_DARWIN: + # Since NSUserNotification has been deprecated, osascript will be used. + os.system("""osascript -e 'display notification "{}" with title "DemonEditor"'""".format(message)) + elif NOTIFY_IS_INIT: + notify = Notify.Notification.new("DemonEditor", message, "demon-editor") + notify.set_urgency(urgency) + notify.set_timeout(timeout) + notify.show() class Page(Enum):