|
| 1 | +#include "eel-theme-utils.h" |
| 2 | +#include "eel-string.h" |
| 3 | + |
| 4 | +static GtkCssProvider *mandatory_css_provider = NULL; |
| 5 | + |
| 6 | +static gboolean |
| 7 | +css_provider_load_from_resource (GtkCssProvider *provider, |
| 8 | + const char *resource_path, |
| 9 | + GError **error) |
| 10 | +{ |
| 11 | + GBytes *data; |
| 12 | + gboolean retval; |
| 13 | + |
| 14 | + data = g_resources_lookup_data (resource_path, 0, error); |
| 15 | + if (!data) |
| 16 | + return FALSE; |
| 17 | + |
| 18 | + retval = gtk_css_provider_load_from_data (provider, |
| 19 | + g_bytes_get_data (data, NULL), |
| 20 | + g_bytes_get_size (data), |
| 21 | + error); |
| 22 | + g_bytes_unref (data); |
| 23 | + |
| 24 | + return retval; |
| 25 | +} |
| 26 | + |
| 27 | +static gchar * |
| 28 | +load_file_contents_from_resource (const char *resource_path, |
| 29 | + GError **error) |
| 30 | +{ |
| 31 | + GBytes *data; |
| 32 | + gchar *retval; |
| 33 | + |
| 34 | + data = g_resources_lookup_data (resource_path, 0, error); |
| 35 | + |
| 36 | + if (!data) { |
| 37 | + return NULL; |
| 38 | + } |
| 39 | + |
| 40 | + retval = g_strdup ((gchar *) g_bytes_get_data (data, NULL)); |
| 41 | + |
| 42 | + g_bytes_unref (data); |
| 43 | + |
| 44 | + return retval; |
| 45 | +} |
| 46 | + |
| 47 | +static void |
| 48 | +add_css_provider_at_priority (const gchar *rpath, guint priority) |
| 49 | +{ |
| 50 | + GtkCssProvider *provider; |
| 51 | + GError *error = NULL; |
| 52 | + |
| 53 | + provider = gtk_css_provider_new (); |
| 54 | + |
| 55 | + if (!css_provider_load_from_resource (provider, rpath, &error)) |
| 56 | + { |
| 57 | + g_warning ("Failed to load css file: %s", error->message); |
| 58 | + g_clear_error (&error); |
| 59 | + goto out; |
| 60 | + } |
| 61 | + |
| 62 | + gtk_style_context_add_provider_for_screen (gdk_screen_get_default (), |
| 63 | + GTK_STYLE_PROVIDER (provider), |
| 64 | + priority); |
| 65 | + |
| 66 | +out: |
| 67 | + g_object_unref (provider); |
| 68 | +} |
| 69 | + |
| 70 | +static void |
| 71 | +add_fallback_mandatory_css_provider (const gchar *theme_name) |
| 72 | +{ |
| 73 | + GtkCssProvider *current_provider; |
| 74 | + g_autofree gchar *css = NULL; |
| 75 | + g_autofree gchar *init_fallback_css = NULL; |
| 76 | + g_autofree gchar *final_fallback_css = NULL; |
| 77 | + GError *error = NULL; |
| 78 | + |
| 79 | + current_provider = gtk_css_provider_get_named (theme_name, NULL); |
| 80 | + |
| 81 | + css = gtk_css_provider_to_string (current_provider); |
| 82 | + |
| 83 | + if (!g_strstr_len (css, -1, "nemo")) { |
| 84 | + g_warning ("The theme appears to have no nemo support. Adding some..."); |
| 85 | + |
| 86 | + init_fallback_css = load_file_contents_from_resource ("/org/nemo/nemo-style-fallback-mandatory.css", |
| 87 | + &error); |
| 88 | + |
| 89 | + if (!init_fallback_css) { |
| 90 | + g_warning ("Failed to load fallback mandatory css file: %s", error->message); |
| 91 | + g_clear_error (&error); |
| 92 | + |
| 93 | + goto out; |
| 94 | + } |
| 95 | + } else { |
| 96 | + goto out; |
| 97 | + } |
| 98 | + |
| 99 | + /* Our fallback uses @theme_ prefixed names for colors. If the active theme does also, skip |
| 100 | + * to apply */ |
| 101 | + if (g_strstr_len (css, -1, "theme_selected_bg_color")) { |
| 102 | + final_fallback_css = g_strdup (init_fallback_css); |
| 103 | + |
| 104 | + goto apply; |
| 105 | + } |
| 106 | + |
| 107 | + /* Some themes don't prefix colors with theme_ - remove this from our fallback css */ |
| 108 | + if (g_strstr_len (css, -1, "@define-color selected_bg_color")) { |
| 109 | + g_warning ("Replacing theme_selected_bg_color with selected_bg_color"); |
| 110 | + final_fallback_css = eel_str_replace_substring (init_fallback_css, |
| 111 | + "@theme_", |
| 112 | + "@"); |
| 113 | + } else { |
| 114 | + /* If we can find neither, just bail out */ |
| 115 | + goto out; |
| 116 | + } |
| 117 | + |
| 118 | +apply: |
| 119 | + mandatory_css_provider = gtk_css_provider_new (); |
| 120 | + |
| 121 | + gtk_css_provider_load_from_data (mandatory_css_provider, |
| 122 | + final_fallback_css, |
| 123 | + -1, |
| 124 | + &error); |
| 125 | + |
| 126 | + if (error) { |
| 127 | + g_warning ("Failed to create a fallback provider: %s", error->message); |
| 128 | + g_clear_error (&error); |
| 129 | + goto out; |
| 130 | + } |
| 131 | + |
| 132 | + gtk_style_context_add_provider_for_screen (gdk_screen_get_default (), |
| 133 | + GTK_STYLE_PROVIDER (mandatory_css_provider), |
| 134 | + GTK_STYLE_PROVIDER_PRIORITY_APPLICATION); |
| 135 | +out: |
| 136 | + ; |
| 137 | +} |
| 138 | + |
| 139 | +static const char *supported_theme_hints[] = { |
| 140 | + "mint", |
| 141 | + "arc", |
| 142 | + "numix", |
| 143 | + "matcha" |
| 144 | +}; |
| 145 | + |
| 146 | +static gboolean |
| 147 | +is_known_supported_theme (const gchar *theme_name) |
| 148 | +{ |
| 149 | + gint i; |
| 150 | + gchar *name; |
| 151 | + gboolean ret; |
| 152 | + |
| 153 | + name = g_utf8_casefold (theme_name, -1); |
| 154 | + ret = FALSE; |
| 155 | + |
| 156 | + for (i = 0; i < G_N_ELEMENTS (supported_theme_hints); i++) { |
| 157 | + gchar *hint; |
| 158 | + |
| 159 | + hint = g_utf8_casefold (supported_theme_hints[i], -1); |
| 160 | + |
| 161 | + if (g_strstr_len (name, -1, hint)) { |
| 162 | + ret = TRUE; |
| 163 | + } |
| 164 | + |
| 165 | + g_free (hint); |
| 166 | + |
| 167 | + if (ret) { |
| 168 | + break; |
| 169 | + } |
| 170 | + } |
| 171 | + |
| 172 | + g_free (name); |
| 173 | + |
| 174 | + return ret; |
| 175 | +} |
| 176 | + |
| 177 | +static void |
| 178 | +process_theme (GtkSettings *gtk_settings) |
| 179 | +{ |
| 180 | + gchar *theme_name; |
| 181 | + |
| 182 | + if (mandatory_css_provider != NULL) { |
| 183 | + gtk_style_context_remove_provider_for_screen (gdk_screen_get_default (), |
| 184 | + GTK_STYLE_PROVIDER (mandatory_css_provider)); |
| 185 | + g_clear_object (&mandatory_css_provider); |
| 186 | + } |
| 187 | + |
| 188 | + g_object_get (gtk_settings, |
| 189 | + "gtk-theme-name", &theme_name, |
| 190 | + NULL); |
| 191 | + |
| 192 | + if (!is_known_supported_theme (theme_name)) { |
| 193 | + g_warning ("Current gtk theme is not known to have nemo support (%s) - checking...", theme_name); |
| 194 | + add_fallback_mandatory_css_provider (theme_name); |
| 195 | + } |
| 196 | + |
| 197 | + gtk_style_context_reset_widgets (gdk_screen_get_default ()); |
| 198 | + g_free (theme_name); |
| 199 | +} |
| 200 | + |
| 201 | +void |
| 202 | +eel_theme_utils_init_styles (void) |
| 203 | +{ |
| 204 | + add_css_provider_at_priority ("/org/nemo/nemo-style-fallback.css", |
| 205 | + GTK_STYLE_PROVIDER_PRIORITY_FALLBACK); |
| 206 | + |
| 207 | + add_css_provider_at_priority ("/org/nemo/nemo-style-application.css", |
| 208 | + GTK_STYLE_PROVIDER_PRIORITY_APPLICATION); |
| 209 | + |
| 210 | + GtkSettings *gtk_settings = gtk_settings_get_default (); |
| 211 | + |
| 212 | + g_signal_connect_swapped (gtk_settings, "notify::gtk-theme-name", |
| 213 | + G_CALLBACK (process_theme), gtk_settings); |
| 214 | + |
| 215 | + process_theme (gtk_settings); |
| 216 | +} |
0 commit comments