[gnome-builder] minimap: use sourceview style scheme to generate overlay color



commit bfb9089a5ddf6a29e4cd7aeb20d384332e910e08
Author: Christian Hergert <christian hergert me>
Date:   Fri Apr 24 17:32:56 2015 -0700

    minimap: use sourceview style scheme to generate overlay color
    
    This should make the color a bit more visible for various themes.
    solarized dark and light look much better, for example.

 data/theme/Adwaita-shared.css |    9 ---
 data/theme/shared.css         |    5 --
 libide/ide-source-map.c       |  146 ++++++++++++++++++++++++++++++++++------
 3 files changed, 124 insertions(+), 36 deletions(-)
---
diff --git a/data/theme/Adwaita-shared.css b/data/theme/Adwaita-shared.css
index ab17625..283d7c1 100644
--- a/data/theme/Adwaita-shared.css
+++ b/data/theme/Adwaita-shared.css
@@ -38,12 +38,3 @@ GbNewProjectDialog GtkFileChooserButton.linked-on-right .button {
   border-radius: 3px 0 0 3px;
 }
 
-
-/*
- * Minimap highlight.
- */
-IdeSourceMap GtkEventBox {
-  background-color: #a0a3a3;
-  border-top: 1px solid #2e3436;
-  border-bottom: 1px solid #2e3436;
-}
diff --git a/data/theme/shared.css b/data/theme/shared.css
index 222ad09..e782993 100644
--- a/data/theme/shared.css
+++ b/data/theme/shared.css
@@ -98,8 +98,3 @@ GbDocumentStack .button {
   transition: none;
 }
 
-
-IdeSourceMap GtkEventBox {
-  background-color: @theme_selected_bg_color;
-  opacity: 0.5;
-}
diff --git a/libide/ide-source-map.c b/libide/ide-source-map.c
index 2283c48..e01177d 100644
--- a/libide/ide-source-map.c
+++ b/libide/ide-source-map.c
@@ -34,7 +34,9 @@ struct _IdeSourceMap
   GtkOverlay            parent_instance;
 
   PangoFontDescription *font_desc;
-  GtkCssProvider       *css_provider;
+
+  GtkCssProvider       *view_css_provider;
+  GtkCssProvider       *box_css_provider;
 
   GtkSourceView        *child_view;
   GtkEventBox          *overlay_box;
@@ -59,6 +61,64 @@ enum {
 
 static GParamSpec *gParamSpecs [LAST_PROP];
 
+static void
+ide_source_map_rebuild_css (IdeSourceMap *self)
+{
+  g_assert (IDE_IS_SOURCE_MAP (self));
+
+  if (self->font_desc != NULL)
+    {
+      gchar *css;
+      gchar *tmp;
+
+      tmp = ide_pango_font_description_to_css (self->font_desc);
+      css = g_strdup_printf ("GtkSourceView { %s }\n", tmp ?: "");
+      gtk_css_provider_load_from_data (self->view_css_provider, css, -1, NULL);
+      g_free (css);
+      g_free (tmp);
+    }
+
+  if (self->view != NULL)
+    {
+      GtkSourceStyleScheme *style_scheme;
+      GtkTextBuffer *buffer;
+
+      buffer = gtk_text_view_get_buffer (GTK_TEXT_VIEW (self->view));
+      style_scheme = gtk_source_buffer_get_style_scheme (GTK_SOURCE_BUFFER (buffer));
+
+      if (style_scheme != NULL)
+        {
+          GtkSourceStyle *style;
+
+          style = gtk_source_style_scheme_get_style (style_scheme, "selection");
+
+          if (style != NULL)
+            {
+              g_autofree gchar *background = NULL;
+
+              g_object_get (style,
+                            "background", &background,
+                            NULL);
+
+              if (background != NULL)
+                {
+                  gchar *css;
+
+                  css = g_strdup_printf ("IdeSourceMap GtkEventBox { "
+                                         "  background-color: %s;"
+                                         "  opacity: 0.75;"
+                                         "  border-top: 1px solid shade(%s,0.9); "
+                                         "  border-bottom: 1px solid shade(%s,0.9); "
+                                         "}\n",
+                                         background, background, background);
+                  gtk_css_provider_load_from_data (self->box_css_provider, css, -1, NULL);
+                  g_free (css);
+                }
+            }
+        }
+    }
+}
+
 /**
  * ide_source_map_get_view:
  *
@@ -195,6 +255,35 @@ transform_font_desc (GBinding     *binding,
   return TRUE;
 }
 
+static void
+ide_source_map__buffer_notify_style_scheme (IdeSourceMap  *self,
+                                            GParamSpec    *pspec,
+                                            GtkTextBuffer *buffer)
+{
+  g_assert (IDE_IS_SOURCE_MAP (self));
+  g_assert (GTK_IS_TEXT_BUFFER (buffer));
+
+  ide_source_map_rebuild_css (self);
+}
+
+static void
+ide_source_map__view_notify_buffer (IdeSourceMap  *self,
+                                    GParamSpec    *pspec,
+                                    GtkSourceView *view)
+{
+  GtkTextBuffer *buffer;
+
+  g_assert (IDE_IS_SOURCE_MAP (self));
+  g_assert (GTK_SOURCE_IS_VIEW (view));
+
+  buffer = gtk_text_view_get_buffer (GTK_TEXT_VIEW (view));
+  g_signal_connect_object (buffer,
+                           "notify::style-scheme",
+                           G_CALLBACK (ide_source_map__buffer_notify_style_scheme),
+                           self,
+                           G_CONNECT_SWAPPED);
+}
+
 void
 ide_source_map_set_view (IdeSourceMap  *self,
                          GtkSourceView *view)
@@ -207,6 +296,7 @@ ide_source_map_set_view (IdeSourceMap  *self,
       if (view != NULL)
         {
           GtkAdjustment *vadj;
+          GtkTextBuffer *buffer;
 
           g_object_bind_property (self->view, "buffer",
                                   self->child_view, "buffer",
@@ -218,6 +308,15 @@ ide_source_map_set_view (IdeSourceMap  *self,
                                   self->child_view, "tab-width",
                                   G_BINDING_SYNC_CREATE);
 
+          g_signal_connect_object (view,
+                                   "notify::buffer",
+                                   G_CALLBACK (ide_source_map__view_notify_buffer),
+                                   self,
+                                   G_CONNECT_SWAPPED);
+
+          buffer = gtk_text_view_get_buffer (GTK_TEXT_VIEW (view));
+          ide_source_map__buffer_notify_style_scheme (self, NULL, buffer);
+
           /*
            * TODO: Not sure what we should do about this in terms of abstraction.
            */
@@ -241,7 +340,10 @@ ide_source_map_set_view (IdeSourceMap  *self,
                                    G_CALLBACK (ide_source_map__view_vadj_notify_upper),
                                    self,
                                    G_CONNECT_SWAPPED);
+
+          ide_source_map_rebuild_css (self);
         }
+
       g_object_notify_by_pspec (G_OBJECT (self), gParamSpecs [PROP_VIEW]);
     }
 }
@@ -253,29 +355,16 @@ ide_source_map_set_font_desc (IdeSourceMap               *self,
   g_assert (IDE_IS_SOURCE_MAP (self));
   g_assert (font_desc != NULL);
 
-  g_clear_pointer (&self->font_desc, pango_font_description_free);
-
-  if (!self->css_provider)
+  if (font_desc != self->font_desc)
     {
-      GtkStyleContext *style_context;
+      if (self->font_desc)
+        g_clear_pointer (&self->font_desc, pango_font_description_free);
 
-      self->css_provider = gtk_css_provider_new ();
-      style_context = gtk_widget_get_style_context (GTK_WIDGET (self->child_view));
-      gtk_style_context_add_provider (style_context,
-                                      GTK_STYLE_PROVIDER (self->css_provider),
-                                      GTK_STYLE_PROVIDER_PRIORITY_APPLICATION);
+      if (font_desc)
+        self->font_desc = pango_font_description_copy (font_desc);
     }
 
-  if (font_desc)
-    {
-      g_autofree gchar *str = NULL;
-      g_autofree gchar *css = NULL;
-
-      self->font_desc = pango_font_description_copy (font_desc);
-      str = ide_pango_font_description_to_css (font_desc);
-      css = g_strdup_printf ("GtkSourceView { %s }", str ?: "");
-      gtk_css_provider_load_from_data (self->css_provider, css, -1, NULL);
-    }
+  ide_source_map_rebuild_css (self);
 }
 
 static void
@@ -287,7 +376,7 @@ ide_source_map_set_font_name (IdeSourceMap *self,
   g_assert (IDE_IS_SOURCE_MAP (self));
 
   if (font_name == NULL)
-    font_name = "Monospace";
+    font_name = "Monospace 1";
 
   font_desc = pango_font_description_from_string (font_name);
   ide_source_map_set_font_desc (self, font_desc);
@@ -559,7 +648,8 @@ ide_source_map_finalize (GObject *object)
 {
   IdeSourceMap *self = (IdeSourceMap *)object;
 
-  g_clear_object (&self->css_provider);
+  g_clear_object (&self->box_css_provider);
+  g_clear_object (&self->view_css_provider);
   g_clear_pointer (&self->font_desc, pango_font_description_free);
   ide_clear_weak_pointer (&self->view);
 
@@ -647,8 +737,10 @@ ide_source_map_init (IdeSourceMap *self)
 {
   GtkSourceCompletion *completion;
   GtkSourceGutter *gutter;
+  GtkStyleContext *context;
   GtkSourceGutterRenderer *renderer;
 
+
   self->child_view = g_object_new (GTK_SOURCE_TYPE_VIEW,
                                    "auto-indent", FALSE,
                                    "can-focus", FALSE,
@@ -676,6 +768,11 @@ ide_source_map_init (IdeSourceMap *self)
                            G_CALLBACK (ide_source_map__child_view_realize_after),
                            self,
                            G_CONNECT_SWAPPED | G_CONNECT_AFTER);
+  self->view_css_provider = gtk_css_provider_new ();
+  context = gtk_widget_get_style_context (GTK_WIDGET (self->child_view));
+  gtk_style_context_add_provider (context,
+                                  GTK_STYLE_PROVIDER (self->view_css_provider),
+                                  GTK_STYLE_PROVIDER_PRIORITY_APPLICATION);
   gtk_container_add (GTK_CONTAINER (self), GTK_WIDGET (self->child_view));
 
   /*
@@ -720,6 +817,11 @@ ide_source_map_init (IdeSourceMap *self)
                            G_CALLBACK (ide_source_map__overlay_box_motion_notify_event),
                            self,
                            G_CONNECT_SWAPPED);
+  context = gtk_widget_get_style_context (GTK_WIDGET (self->overlay_box));
+  self->box_css_provider = gtk_css_provider_new ();
+  gtk_style_context_add_provider (context,
+                                  GTK_STYLE_PROVIDER (self->box_css_provider),
+                                  GTK_STYLE_PROVIDER_PRIORITY_APPLICATION);
 
   gtk_overlay_add_overlay (GTK_OVERLAY (self), GTK_WIDGET (self->overlay_box));
 


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