[gtk/matthiasc/for-master: 19/19] inspector: Add a focus overlay



commit a111cfa9754af9a92dcf3596ba0ce554610f504a
Author: Matthias Clasen <mclasen redhat com>
Date:   Fri Dec 27 12:16:14 2019 -0500

    inspector: Add a focus overlay
    
    It helps to see what is going on.

 gtk/inspector/focusoverlay.c | 104 +++++++++++++++++++++++++++++++++++++++++++
 gtk/inspector/focusoverlay.h |  36 +++++++++++++++
 gtk/inspector/meson.build    |   1 +
 gtk/inspector/visual.c       |  47 +++++++++++++++++++
 gtk/inspector/visual.ui      |  27 +++++++++++
 5 files changed, 215 insertions(+)
---
diff --git a/gtk/inspector/focusoverlay.c b/gtk/inspector/focusoverlay.c
new file mode 100644
index 0000000000..f577a217cb
--- /dev/null
+++ b/gtk/inspector/focusoverlay.c
@@ -0,0 +1,104 @@
+/*
+ * Copyright © 2018 Benjamin Otte
+ *
+ * This library is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 2.1 of the License, or (at your option) any later version.
+ *
+ * This library is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this library. If not, see <http://www.gnu.org/licenses/>.
+ *
+ * Authors: Benjamin Otte <otte gnome org>
+ */
+
+#include "config.h"
+
+#include "focusoverlay.h"
+
+#include "gtkintl.h"
+#include "gtkwidget.h"
+#include "gtkroot.h"
+
+struct _GtkFocusOverlay
+{
+  GtkInspectorOverlay parent_instance;
+
+  GdkRGBA color;
+};
+
+struct _GtkFocusOverlayClass
+{
+  GtkInspectorOverlayClass parent_class;
+};
+
+G_DEFINE_TYPE (GtkFocusOverlay, gtk_focus_overlay, GTK_TYPE_INSPECTOR_OVERLAY)
+
+static void
+gtk_focus_overlay_snapshot (GtkInspectorOverlay *overlay,
+                            GtkSnapshot         *snapshot,
+                            GskRenderNode       *node,
+                            GtkWidget           *widget)
+{
+  GtkFocusOverlay *self = GTK_FOCUS_OVERLAY (overlay);
+  GtkWidget *focus;
+  graphene_rect_t bounds;
+
+  if (!GTK_IS_ROOT (widget))
+    return;
+
+  focus = gtk_root_get_focus (GTK_ROOT (widget));
+  if (!focus)
+    return;
+
+  if (!gtk_widget_compute_bounds (focus, widget, &bounds))
+    return;
+
+  gtk_snapshot_append_color (snapshot, &self->color, &bounds);
+}
+
+static void
+gtk_focus_overlay_queue_draw (GtkInspectorOverlay *overlay)
+{
+}
+
+static void
+gtk_focus_overlay_class_init (GtkFocusOverlayClass *klass)
+{
+  GtkInspectorOverlayClass *overlay_class = GTK_INSPECTOR_OVERLAY_CLASS (klass);
+
+  overlay_class->snapshot = gtk_focus_overlay_snapshot;
+  overlay_class->queue_draw = gtk_focus_overlay_queue_draw;
+}
+
+static void
+gtk_focus_overlay_init (GtkFocusOverlay *self)
+{
+  self->color = (GdkRGBA) { 0.5, 0.0, 1.0, 0.2 };
+}
+
+GtkInspectorOverlay *
+gtk_focus_overlay_new (void)
+{
+  GtkFocusOverlay *self;
+
+  self = g_object_new (GTK_TYPE_FOCUS_OVERLAY, NULL);
+
+  return GTK_INSPECTOR_OVERLAY (self);
+}
+
+void
+gtk_focus_overlay_set_color (GtkFocusOverlay *self,
+                             const GdkRGBA   *color)
+{
+  if (gdk_rgba_equal (&self->color, color))
+    return;
+
+  self->color = *color;
+  gtk_inspector_overlay_queue_draw (GTK_INSPECTOR_OVERLAY (self));
+}
diff --git a/gtk/inspector/focusoverlay.h b/gtk/inspector/focusoverlay.h
new file mode 100644
index 0000000000..c2acd99947
--- /dev/null
+++ b/gtk/inspector/focusoverlay.h
@@ -0,0 +1,36 @@
+/*
+ * Copyright © 2018 Benjamin Otte
+ *
+ * This library is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 2.1 of the License, or (at your option) any later version.
+ *
+ * This library is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this library. If not, see <http://www.gnu.org/licenses/>.
+ *
+ * Authors: Benjamin Otte <otte gnome org>
+ */
+
+#ifndef __GTK_FOCUS_OVERLAY_H__
+#define __GTK_FOCUS_OVERLAY_H__
+
+#include "inspectoroverlay.h"
+
+G_BEGIN_DECLS
+
+#define GTK_TYPE_FOCUS_OVERLAY             (gtk_focus_overlay_get_type ())
+G_DECLARE_FINAL_TYPE (GtkFocusOverlay, gtk_focus_overlay, GTK, FOCUS_OVERLAY, GtkInspectorOverlay)
+
+GtkInspectorOverlay *   gtk_focus_overlay_new               (void);
+void                    gtk_focus_overlay_set_color         (GtkFocusOverlay    *self,
+                                                             const GdkRGBA      *color);
+
+G_END_DECLS
+
+#endif /* __GTK_FOCUS_OVERLAY_H__ */
diff --git a/gtk/inspector/meson.build b/gtk/inspector/meson.build
index a26d67ed87..dbcea03bb0 100644
--- a/gtk/inspector/meson.build
+++ b/gtk/inspector/meson.build
@@ -6,6 +6,7 @@ inspector_sources = files(
   'css-editor.c',
   'css-node-tree.c',
   'data-list.c',
+  'focusoverlay.c',
   'fpsoverlay.c',
   'general.c',
   'graphdata.c',
diff --git a/gtk/inspector/visual.c b/gtk/inspector/visual.c
index 183b30cf8c..5787f12756 100644
--- a/gtk/inspector/visual.c
+++ b/gtk/inspector/visual.c
@@ -23,6 +23,7 @@
 #include "fpsoverlay.h"
 #include "updatesoverlay.h"
 #include "layoutoverlay.h"
+#include "focusoverlay.h"
 #include "window.h"
 
 #include "gtkadjustment.h"
@@ -81,6 +82,7 @@ struct _GtkInspectorVisualPrivate
   GtkWidget *baselines_switch;
   GtkWidget *layout_switch;
   GtkWidget *resize_switch;
+  GtkWidget *focus_switch;
 
   GtkWidget *misc_box;
   GtkWidget *touchscreen_switch;
@@ -91,6 +93,7 @@ struct _GtkInspectorVisualPrivate
   GtkInspectorOverlay *fps_overlay;
   GtkInspectorOverlay *updates_overlay;
   GtkInspectorOverlay *layout_overlay;
+  GtkInspectorOverlay *focus_overlay;
 
   GdkDisplay *display;
 };
@@ -414,6 +417,41 @@ widget_resize_activate (GtkSwitch *sw)
   gtk_set_debug_flags (flags);
 }
 
+static void
+focus_activate (GtkSwitch          *sw,
+                GParamSpec         *pspec,
+                GtkInspectorVisual *vis)
+{
+  GtkInspectorVisualPrivate *priv = vis->priv;
+  GtkInspectorWindow *iw;
+  gboolean focus;
+
+  focus = gtk_switch_get_active (sw);
+  iw = GTK_INSPECTOR_WINDOW (gtk_widget_get_root (GTK_WIDGET (vis)));
+  if (iw == NULL)
+    return;
+
+  if (focus)
+    {
+      if (priv->focus_overlay == NULL)
+        {
+          priv->focus_overlay = gtk_focus_overlay_new ();
+          gtk_inspector_window_add_overlay (iw, priv->focus_overlay);
+          g_object_unref (priv->focus_overlay);
+        }
+    }
+  else
+    {
+      if (priv->focus_overlay != NULL)
+        {
+          gtk_inspector_window_remove_overlay (iw, priv->focus_overlay);
+          priv->focus_overlay = NULL;
+        }
+    }
+
+  redraw_everything ();
+}
+
 static void
 fill_gtk (const gchar *path,
           GHashTable  *t)
@@ -939,6 +977,11 @@ row_activated (GtkListBox         *box,
       GtkSwitch *sw = GTK_SWITCH (vis->priv->resize_switch);
       gtk_switch_set_active (sw, !gtk_switch_get_active (sw));
     }
+  else if (gtk_widget_is_ancestor (vis->priv->focus_switch, GTK_WIDGET (row)))
+    {
+      GtkSwitch *sw = GTK_SWITCH (vis->priv->focus_switch);
+      gtk_switch_set_active (sw, !gtk_switch_get_active (sw));
+    }
   else if (gtk_widget_is_ancestor (vis->priv->touchscreen_switch, GTK_WIDGET (row)))
     {
       GtkSwitch *sw = GTK_SWITCH (vis->priv->touchscreen_switch);
@@ -1028,6 +1071,8 @@ gtk_inspector_visual_finalize (GObject *object)
     gtk_inspector_window_remove_overlay (iw, vis->priv->updates_overlay);
   if (vis->priv->fps_overlay)
     gtk_inspector_window_remove_overlay (iw, vis->priv->fps_overlay);
+  if (vis->priv->focus_overlay)
+    gtk_inspector_window_remove_overlay (iw, vis->priv->focus_overlay);
 
   G_OBJECT_CLASS (gtk_inspector_visual_parent_class)->finalize (object);
 }
@@ -1104,6 +1149,7 @@ gtk_inspector_visual_class_init (GtkInspectorVisualClass *klass)
   gtk_widget_class_bind_template_child_private (widget_class, GtkInspectorVisual, baselines_switch);
   gtk_widget_class_bind_template_child_private (widget_class, GtkInspectorVisual, layout_switch);
   gtk_widget_class_bind_template_child_private (widget_class, GtkInspectorVisual, resize_switch);
+  gtk_widget_class_bind_template_child_private (widget_class, GtkInspectorVisual, focus_switch);
 
   gtk_widget_class_bind_template_callback (widget_class, fps_activate);
   gtk_widget_class_bind_template_callback (widget_class, updates_activate);
@@ -1112,6 +1158,7 @@ gtk_inspector_visual_class_init (GtkInspectorVisualClass *klass)
   gtk_widget_class_bind_template_callback (widget_class, baselines_activate);
   gtk_widget_class_bind_template_callback (widget_class, layout_activate);
   gtk_widget_class_bind_template_callback (widget_class, widget_resize_activate);
+  gtk_widget_class_bind_template_callback (widget_class, focus_activate);
   gtk_widget_class_bind_template_callback (widget_class, software_gl_activate);
 }
 
diff --git a/gtk/inspector/visual.ui b/gtk/inspector/visual.ui
index ead50b6384..d052c9d68c 100644
--- a/gtk/inspector/visual.ui
+++ b/gtk/inspector/visual.ui
@@ -529,6 +529,32 @@
                         </child>
                       </object>
                     </child>
+                    <child>
+                      <object class="GtkListBoxRow">
+                        <child>
+                          <object class="GtkBox">
+                            <property name="margin">10</property>
+                            <property name="spacing">40</property>
+                            <child>
+                              <object class="GtkLabel" id="focus_label">
+                                <property name="label" translatable="yes">Show Focus</property>
+                                <property name="halign">start</property>
+                                <property name="valign">baseline</property>
+                                <property name="xalign">0.0</property>
+                              </object>
+                            </child>
+                            <child>
+                              <object class="GtkSwitch" id="focus_switch">
+                                <property name="halign">end</property>
+                                <property name="valign">baseline</property>
+                                <property name="hexpand">1</property>
+                                <signal name="notify::active" handler="focus_activate"/>
+                              </object>
+                            </child>
+                          </object>
+                        </child>
+                      </object>
+                    </child>
                   </object>
                 </child>
               </object>
@@ -616,6 +642,7 @@
       <widget name="baselines_label"/>
       <widget name="layout_label"/>
       <widget name="resize_label"/>
+      <widget name="focus_label"/>
       <widget name="touchscreen_label"/>
       <widget name="software_gl_label"/>
     </widgets>


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