inspector: Draw baselines as an overlay

This commit is contained in:
Matthias Clasen 2019-05-13 17:36:46 +00:00
parent bc4637fff6
commit 4a978dc8c4
4 changed files with 152 additions and 7 deletions

View File

@ -0,0 +1,106 @@
#include "config.h"
#include "baselineoverlay.h"
#include "gtkwidgetprivate.h"
#include "gtkcssstyleprivate.h"
#include "gtkcssnodeprivate.h"
#include "gtkcssnumbervalueprivate.h"
#include "gtkcssboxesprivate.h"
struct _GtkBaselineOverlay
{
GtkInspectorOverlay parent_instance;
};
struct _GtkBaselineOverlayClass
{
GtkInspectorOverlayClass parent_class;
};
G_DEFINE_TYPE (GtkBaselineOverlay, gtk_baseline_overlay, GTK_TYPE_INSPECTOR_OVERLAY)
static void
recurse_child_widgets (GtkWidget *widget,
GtkSnapshot *snapshot)
{
int baseline;
GtkWidget *child;
GtkCssBoxes boxes;
if (!gtk_widget_get_mapped (widget))
return;
if (gtk_widget_get_overflow (widget) == GTK_OVERFLOW_HIDDEN)
{
gtk_css_boxes_init (&boxes, widget);
gtk_snapshot_push_rounded_clip (snapshot, gtk_css_boxes_get_padding_box (&boxes));
}
baseline = gtk_widget_get_allocated_baseline (widget);
if (baseline != -1)
{
GdkRGBA red = {1, 0, 0, 1};
graphene_rect_t bounds;
int width;
width = gtk_widget_get_width (widget);
/* Now do all the stuff */
gtk_snapshot_push_debug (snapshot, "Widget baseline debugging");
graphene_rect_init (&bounds,
0, baseline,
width, 1);
gtk_snapshot_append_color (snapshot, &red, &bounds);
gtk_snapshot_pop (snapshot);
}
/* Recurse into child widgets */
for (child = gtk_widget_get_first_child (widget);
child != NULL;
child = gtk_widget_get_next_sibling (child))
{
graphene_matrix_t matrix;
if (gtk_widget_compute_transform (child, widget, &matrix))
{
gtk_snapshot_save (snapshot);
gtk_snapshot_transform_matrix (snapshot, &matrix);
recurse_child_widgets (child, snapshot);
gtk_snapshot_restore (snapshot);
}
}
if (gtk_widget_get_overflow (widget) == GTK_OVERFLOW_HIDDEN)
gtk_snapshot_pop (snapshot);
}
static void
gtk_baseline_overlay_snapshot (GtkInspectorOverlay *overlay,
GtkSnapshot *snapshot,
GskRenderNode *node,
GtkWidget *widget)
{
recurse_child_widgets (widget, snapshot);
}
static void
gtk_baseline_overlay_init (GtkBaselineOverlay *self)
{
}
static void
gtk_baseline_overlay_class_init (GtkBaselineOverlayClass *klass)
{
GtkInspectorOverlayClass *overlay_class = (GtkInspectorOverlayClass *)klass;
overlay_class->snapshot = gtk_baseline_overlay_snapshot;
}
GtkInspectorOverlay *
gtk_baseline_overlay_new (void)
{
return g_object_new (GTK_TYPE_BASELINE_OVERLAY, NULL);
}

View File

@ -0,0 +1,17 @@
#ifndef __GTK_BASELINE_OVERLAY_H__
#define __GTK_BASELINE_OVERLAY_H__
#include "inspectoroverlay.h"
G_BEGIN_DECLS
#define GTK_TYPE_BASELINE_OVERLAY (gtk_baseline_overlay_get_type ())
G_DECLARE_FINAL_TYPE (GtkBaselineOverlay, gtk_baseline_overlay, GTK, BASELINE_OVERLAY, GtkInspectorOverlay)
GtkInspectorOverlay * gtk_baseline_overlay_new (void);
G_END_DECLS
#endif

View File

@ -1,6 +1,7 @@
inspector_sources = files(
'action-editor.c',
'actions.c',
'baselineoverlay.c',
'cellrenderergraph.c',
'controllers.c',
'css-editor.c',

View File

@ -24,6 +24,7 @@
#include "updatesoverlay.h"
#include "layoutoverlay.h"
#include "focusoverlay.h"
#include "baselineoverlay.h"
#include "window.h"
#include "gtkadjustment.h"
@ -94,6 +95,7 @@ struct _GtkInspectorVisualPrivate
GtkInspectorOverlay *updates_overlay;
GtkInspectorOverlay *layout_overlay;
GtkInspectorOverlay *focus_overlay;
GtkInspectorOverlay *baseline_overlay;
GdkDisplay *display;
};
@ -354,18 +356,37 @@ fallback_activate (GtkSwitch *sw,
}
static void
baselines_activate (GtkSwitch *sw)
baselines_activate (GtkSwitch *sw,
GParamSpec *pspec,
GtkInspectorVisual *vis)
{
guint flags;
GtkInspectorVisualPrivate *priv = vis->priv;
GtkInspectorWindow *iw;
gboolean baselines;
flags = gtk_get_debug_flags ();
baselines = gtk_switch_get_active (sw);
iw = GTK_INSPECTOR_WINDOW (gtk_widget_get_root (GTK_WIDGET (vis)));
if (iw == NULL)
return;
if (gtk_switch_get_active (sw))
flags |= GTK_DEBUG_BASELINES;
if (baselines)
{
if (priv->baseline_overlay == NULL)
{
priv->baseline_overlay = gtk_baseline_overlay_new ();
gtk_inspector_window_add_overlay (iw, priv->baseline_overlay);
g_object_unref (priv->baseline_overlay);
}
}
else
flags &= ~GTK_DEBUG_BASELINES;
{
if (priv->baseline_overlay != NULL)
{
gtk_inspector_window_remove_overlay (iw, priv->baseline_overlay);
priv->baseline_overlay = NULL;
}
}
gtk_set_debug_flags (flags);
redraw_everything ();
}