[gnome-terminal] app: Add CSS loading infrastructure



commit f436efd459f2464206f8dbc0538cc8d1ad9dbe98
Author: Christian Persch <chpe gnome org>
Date:   Tue Jul 14 19:37:28 2015 +0200

    app: Add CSS loading infrastructure
    
    At application startup, load common CSS, and CSS for the specific theme, if it exists.

 src/terminal-app.c         |   59 +++++++++++++++++++++++++++++++++++++++++++-
 src/terminal.common.css    |    1 +
 src/terminal.gresource.xml |    1 +
 3 files changed, 60 insertions(+), 1 deletions(-)
---
diff --git a/src/terminal-app.c b/src/terminal-app.c
index 1ca532d..0d2a0e6 100644
--- a/src/terminal-app.c
+++ b/src/terminal-app.c
@@ -3,7 +3,7 @@
  * Copyright © 2002 Red Hat, Inc.
  * Copyright © 2002 Sun Microsystems
  * Copyright © 2003 Mariano Suarez-Alvarez
- * Copyright © 2008, 2010, 2011 Christian Persch
+ * Copyright © 2008, 2010, 2011, 2015 Christian Persch
  *
  * 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
@@ -152,6 +152,60 @@ maybe_migrate_settings (TerminalApp *app)
 #endif /* ENABLE_MIGRATION */
 }
 
+static gboolean
+load_css_from_resource (GApplication *application,
+                        GtkCssProvider *provider,
+                        gboolean theme)
+{
+  const char *base_path;
+  gs_free char *uri;
+  gs_unref_object GFile *file;
+  gs_free_error GError *error = NULL;
+
+  base_path = g_application_get_resource_base_path (application);
+
+  if (theme) {
+    gs_free char *str, *theme_name;
+
+    g_object_get (gtk_settings_get_default (), "gtk-theme-name", &str, NULL);
+    theme_name = g_ascii_strdown (str, -1);
+    uri = g_strdup_printf ("resource://%s/css/%s/terminal.css", base_path, theme_name);
+  } else {
+    uri = g_strdup_printf ("resource://%s/css/terminal.css", base_path);
+  }
+
+  file = g_file_new_for_uri (uri);
+  if (!g_file_query_exists (file, NULL /* cancellable */))
+    return FALSE;
+
+  if (!gtk_css_provider_load_from_file (provider, file, &error))
+    g_assert_no_error (error);
+
+  return TRUE;
+}
+
+static void
+add_css_provider (GApplication *application,
+                  gboolean theme)
+{
+  gs_unref_object GtkCssProvider *provider;
+
+  provider = gtk_css_provider_new ();
+  if (!load_css_from_resource (application, provider, theme))
+    return;
+
+  gtk_style_context_add_provider_for_screen (gdk_screen_get_default (),
+                                             GTK_STYLE_PROVIDER (provider),
+                                             GTK_STYLE_PROVIDER_PRIORITY_APPLICATION);
+}
+
+static void
+app_load_css (GApplication *application)
+{
+  add_css_provider (application, FALSE);
+  add_css_provider (application, TRUE);
+}
+
 void
 terminal_app_new_profile (TerminalApp *app,
                           GSettings   *base_profile,
@@ -332,6 +386,9 @@ terminal_app_startup (GApplication *application)
                                    app_menu_actions, G_N_ELEMENTS (app_menu_actions),
                                    application);
 
+
+  app_load_css (application);
+
   _terminal_debug_print (TERMINAL_DEBUG_SERVER, "Startup complete\n");
 }
 
diff --git a/src/terminal.common.css b/src/terminal.common.css
new file mode 100644
index 0000000..9ac6ed5
--- /dev/null
+++ b/src/terminal.common.css
@@ -0,0 +1 @@
+/* Generic theme-independent CSS file */
diff --git a/src/terminal.gresource.xml b/src/terminal.gresource.xml
index abab042..5ead95f 100644
--- a/src/terminal.gresource.xml
+++ b/src/terminal.gresource.xml
@@ -24,5 +24,6 @@
     <file alias="ui/profile-preferences.ui" compressed="true" 
preprocess="xml-stripblanks">profile-preferences.ui</file>
     <file alias="ui/window.ui" compressed="true" preprocess="xml-stripblanks">terminal-window.ui</file>
     <file alias="gtk/menus.ui" compressed="true" preprocess="xml-stripblanks">terminal-menus.ui</file>
+    <file alias="css/terminal.css" compressed="true">terminal.common.css</file>
   </gresource>
 </gresources>


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]