[gtk/inspector-display: 1/6] inspector: Make css editor respect inspected display



commit 174d8b72a5e86b26c71c9562fad0983a3139b8ed
Author: Matthias Clasen <mclasen redhat com>
Date:   Sat Dec 7 11:48:39 2019 -0500

    inspector: Make css editor respect inspected display
    
    Stop using gdk_display_get_default and use the
    inspected display instead.

 gtk/inspector/css-editor.c | 46 +++++++++++++++++++++++++++++++++++++---------
 gtk/inspector/css-editor.h |  4 ++--
 gtk/inspector/window.c     |  2 ++
 gtk/inspector/window.h     |  1 +
 gtk/inspector/window.ui    |  2 +-
 5 files changed, 43 insertions(+), 12 deletions(-)
---
diff --git a/gtk/inspector/css-editor.c b/gtk/inspector/css-editor.c
index 16b156a4e5..4cb20632d7 100644
--- a/gtk/inspector/css-editor.c
+++ b/gtk/inspector/css-editor.c
@@ -23,6 +23,7 @@
 #include "config.h"
 #include <glib/gi18n-lib.h>
 
+#include "window.h"
 #include "css-editor.h"
 
 #include "gtkcssprovider.h"
@@ -42,6 +43,7 @@ struct _GtkInspectorCssEditorPrivate
 {
   GtkWidget *view;
   GtkTextBuffer *text;
+  GdkDisplay *display;
   GtkCssProvider *provider;
   GtkToggleButton *disable_button;
   guint timeout;
@@ -157,11 +159,14 @@ static void
 disable_toggled (GtkToggleButton       *button,
                  GtkInspectorCssEditor *ce)
 {
+  if (!ce->priv->display)
+    return;
+
   if (gtk_toggle_button_get_active (button))
-    gtk_style_context_remove_provider_for_display (gdk_display_get_default (),
+    gtk_style_context_remove_provider_for_display (ce->priv->display,
                                                    GTK_STYLE_PROVIDER (ce->priv->provider));
   else
-    gtk_style_context_add_provider_for_display (gdk_display_get_default (),
+    gtk_style_context_add_provider_for_display (ce->priv->display,
                                                 GTK_STYLE_PROVIDER (ce->priv->provider),
                                                 GTK_STYLE_PROVIDER_PRIORITY_USER);
 }
@@ -326,22 +331,35 @@ static void
 create_provider (GtkInspectorCssEditor *ce)
 {
   ce->priv->provider = gtk_css_provider_new ();
-  gtk_style_context_add_provider_for_display (gdk_display_get_default (),
-                                              GTK_STYLE_PROVIDER (ce->priv->provider),
-                                              GTK_STYLE_PROVIDER_PRIORITY_USER);
-
   g_signal_connect (ce->priv->provider, "parsing-error",
                     G_CALLBACK (show_parsing_error), ce);
+
 }
 
 static void
 destroy_provider (GtkInspectorCssEditor *ce)
 {
-  gtk_style_context_remove_provider_for_display (gdk_display_get_default (),
-                                                 GTK_STYLE_PROVIDER (ce->priv->provider));
+  g_signal_handlers_disconnect_by_func (ce->priv->provider, show_parsing_error, ce);
   g_clear_object (&ce->priv->provider);
 }
 
+static void
+add_provider (GtkInspectorCssEditor *ce,
+              GdkDisplay *display)
+{
+  gtk_style_context_add_provider_for_display (display,
+                                              GTK_STYLE_PROVIDER (ce->priv->provider),
+                                              GTK_STYLE_PROVIDER_PRIORITY_USER);
+}
+
+static void
+remove_provider (GtkInspectorCssEditor *ce,
+                 GdkDisplay *display)
+{
+  gtk_style_context_remove_provider_for_display (display,
+                                                 GTK_STYLE_PROVIDER (ce->priv->provider));
+}
+
 static void
 gtk_inspector_css_editor_init (GtkInspectorCssEditor *ce)
 {
@@ -355,7 +373,6 @@ constructed (GObject *object)
   GtkInspectorCssEditor *ce = GTK_INSPECTOR_CSS_EDITOR (object);
 
   create_provider (ce);
-  set_initial_text (ce);
 }
 
 static void
@@ -366,6 +383,8 @@ finalize (GObject *object)
   if (ce->priv->timeout != 0)
     g_source_remove (ce->priv->timeout);
 
+  if (ce->priv->display)
+    remove_provider (ce, ce->priv->display);
   destroy_provider (ce);
 
   g_list_free_full (ce->priv->errors, css_error_free);
@@ -392,4 +411,13 @@ gtk_inspector_css_editor_class_init (GtkInspectorCssEditorClass *klass)
   gtk_widget_class_bind_template_callback (widget_class, query_tooltip_cb);
 }
 
+void
+gtk_inspector_css_editor_set_display (GtkInspectorCssEditor *ce,
+                                      GdkDisplay *display)
+{
+  ce->priv->display = display;
+  add_provider (ce, display);
+  set_initial_text (ce);
+}
+
 // vim: set et sw=2 ts=2:
diff --git a/gtk/inspector/css-editor.h b/gtk/inspector/css-editor.h
index ad95d6508a..af463dcb03 100644
--- a/gtk/inspector/css-editor.h
+++ b/gtk/inspector/css-editor.h
@@ -49,8 +49,8 @@ typedef struct _GtkInspectorCssEditorClass
 G_BEGIN_DECLS
 
 GType      gtk_inspector_css_editor_get_type   (void);
-void       gtk_inspector_css_editor_set_object (GtkInspectorCssEditor *ce,
-                                                GObject               *object);
+void       gtk_inspector_css_editor_set_display (GtkInspectorCssEditor *ce,
+                                                 GdkDisplay            *display);
 
 G_END_DECLS
 
diff --git a/gtk/inspector/window.c b/gtk/inspector/window.c
index 24c2f20515..b2a8a36e53 100644
--- a/gtk/inspector/window.c
+++ b/gtk/inspector/window.c
@@ -267,6 +267,7 @@ gtk_inspector_window_constructed (GObject *object)
   g_object_set_data (G_OBJECT (iw->inspected_display), "-gtk-inspector", iw);
 
   gtk_inspector_object_tree_set_display (GTK_INSPECTOR_OBJECT_TREE (iw->object_tree), iw->inspected_display);
+  gtk_inspector_css_editor_set_display (GTK_INSPECTOR_CSS_EDITOR (iw->css_editor), iw->inspected_display);
 }
 
 static void
@@ -416,6 +417,7 @@ gtk_inspector_window_class_init (GtkInspectorWindowClass *klass)
   gtk_widget_class_bind_template_child (widget_class, GtkInspectorWindow, controllers);
   gtk_widget_class_bind_template_child (widget_class, GtkInspectorWindow, magnifier);
   gtk_widget_class_bind_template_child (widget_class, GtkInspectorWindow, sidebar_revealer);
+  gtk_widget_class_bind_template_child (widget_class, GtkInspectorWindow, css_editor);
 
   gtk_widget_class_bind_template_callback (widget_class, gtk_inspector_on_inspect);
   gtk_widget_class_bind_template_callback (widget_class, on_object_activated);
diff --git a/gtk/inspector/window.h b/gtk/inspector/window.h
index f8ba6c5ef9..5616183c0b 100644
--- a/gtk/inspector/window.h
+++ b/gtk/inspector/window.h
@@ -71,6 +71,7 @@ typedef struct
   GtkWidget *controllers;
   GtkWidget *magnifier;
   GtkWidget *sidebar_revealer;
+  GtkWidget *css_editor;
 
   GtkWidget *selected_widget;
 
diff --git a/gtk/inspector/window.ui b/gtk/inspector/window.ui
index e55f36a8cb..265685e987 100644
--- a/gtk/inspector/window.ui
+++ b/gtk/inspector/window.ui
@@ -519,7 +519,7 @@
             <property name="name">css</property>
             <property name="title" translatable="yes">CSS</property>
             <property name="child">
-              <object class="GtkInspectorCssEditor"/>
+              <object class="GtkInspectorCssEditor" id="css_editor"/>
             </property>
           </object>
         </child>


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