theme-viewer: use the same GtkStyleContext as mutter usually renders

https://bugzilla.gnome.org/show_bug.cgi?id=690317
This commit is contained in:
Jasper St. Pierre 2012-12-16 17:54:33 -05:00
parent 22a3126a2b
commit 5c33b0d768
6 changed files with 82 additions and 45 deletions

View File

@ -44,6 +44,8 @@ struct _MetaPreview
{ {
GtkBin bin; GtkBin bin;
GtkStyleContext *style_context;
MetaTheme *theme; MetaTheme *theme;
char *title; char *title;
MetaFrameType type; MetaFrameType type;

View File

@ -185,38 +185,6 @@ prefs_changed_callback (MetaPreference pref,
} }
} }
static GtkStyleContext *
create_style_context (MetaFrames *frames,
const gchar *variant)
{
GtkStyleContext *style;
GdkScreen *screen;
char *theme_name;
screen = gtk_widget_get_screen (GTK_WIDGET (frames));
g_object_get (gtk_settings_get_for_screen (screen),
"gtk-theme-name", &theme_name,
NULL);
style = gtk_style_context_new ();
gtk_style_context_set_path (style,
gtk_widget_get_path (GTK_WIDGET (frames)));
if (theme_name && *theme_name)
{
GtkCssProvider *provider;
provider = gtk_css_provider_get_named (theme_name, variant);
gtk_style_context_add_provider (style,
GTK_STYLE_PROVIDER (provider),
GTK_STYLE_PROVIDER_PRIORITY_SETTINGS);
}
g_free (theme_name);
return style;
}
static GtkStyleContext * static GtkStyleContext *
meta_frames_get_theme_variant (MetaFrames *frames, meta_frames_get_theme_variant (MetaFrames *frames,
const gchar *variant) const gchar *variant)
@ -226,7 +194,7 @@ meta_frames_get_theme_variant (MetaFrames *frames,
style = g_hash_table_lookup (frames->style_variants, variant); style = g_hash_table_lookup (frames->style_variants, variant);
if (style == NULL) if (style == NULL)
{ {
style = create_style_context (frames, variant); style = meta_theme_create_style_context (gtk_widget_get_screen (GTK_WIDGET (frames)), variant);
g_hash_table_insert (frames->style_variants, g_strdup (variant), style); g_hash_table_insert (frames->style_variants, g_strdup (variant), style);
} }
@ -238,15 +206,18 @@ update_style_contexts (MetaFrames *frames)
{ {
GtkStyleContext *style; GtkStyleContext *style;
GList *variants, *variant; GList *variants, *variant;
GdkScreen *screen;
screen = gtk_widget_get_screen (GTK_WIDGET (frames));
if (frames->normal_style) if (frames->normal_style)
g_object_unref (frames->normal_style); g_object_unref (frames->normal_style);
frames->normal_style = create_style_context (frames, NULL); frames->normal_style = meta_theme_create_style_context (screen, NULL);
variants = g_hash_table_get_keys (frames->style_variants); variants = g_hash_table_get_keys (frames->style_variants);
for (variant = variants; variant; variant = variants->next) for (variant = variants; variant; variant = variants->next)
{ {
style = create_style_context (frames, (char *)variant->data); style = meta_theme_create_style_context (screen, (char *)variant->data);
g_hash_table_insert (frames->style_variants, g_hash_table_insert (frames->style_variants,
g_strdup (variant->data), style); g_strdup (variant->data), style);
} }

View File

@ -39,6 +39,8 @@ static void meta_preview_size_allocate (GtkWidget *widget,
GtkAllocation *allocation); GtkAllocation *allocation);
static gboolean meta_preview_draw (GtkWidget *widget, static gboolean meta_preview_draw (GtkWidget *widget,
cairo_t *cr); cairo_t *cr);
static void meta_preview_realize (GtkWidget *widget);
static void meta_preview_dispose (GObject *object);
static void meta_preview_finalize (GObject *object); static void meta_preview_finalize (GObject *object);
G_DEFINE_TYPE (MetaPreview, meta_preview, GTK_TYPE_BIN); G_DEFINE_TYPE (MetaPreview, meta_preview, GTK_TYPE_BIN);
@ -51,8 +53,10 @@ meta_preview_class_init (MetaPreviewClass *class)
widget_class = (GtkWidgetClass*) class; widget_class = (GtkWidgetClass*) class;
gobject_class->dispose = meta_preview_dispose;
gobject_class->finalize = meta_preview_finalize; gobject_class->finalize = meta_preview_finalize;
widget_class->realize = meta_preview_realize;
widget_class->draw = meta_preview_draw; widget_class->draw = meta_preview_draw;
widget_class->get_preferred_width = meta_preview_get_preferred_width; widget_class->get_preferred_width = meta_preview_get_preferred_width;
widget_class->get_preferred_height = meta_preview_get_preferred_height; widget_class->get_preferred_height = meta_preview_get_preferred_height;
@ -107,6 +111,16 @@ meta_preview_new (void)
return GTK_WIDGET (preview); return GTK_WIDGET (preview);
} }
static void
meta_preview_dispose (GObject *object)
{
MetaPreview *preview = META_PREVIEW (object);
g_clear_object (&preview->style_context);
G_OBJECT_CLASS (meta_preview_parent_class)->dispose (object);
}
static void static void
meta_preview_finalize (GObject *object) meta_preview_finalize (GObject *object)
{ {
@ -212,7 +226,7 @@ meta_preview_draw (GtkWidget *widget,
client_height = 1; client_height = 1;
meta_theme_draw_frame (preview->theme, meta_theme_draw_frame (preview->theme,
gtk_widget_get_style_context (widget), preview->style_context,
cr, cr,
preview->type, preview->type,
preview->flags, preview->flags,
@ -231,6 +245,17 @@ meta_preview_draw (GtkWidget *widget,
return GTK_WIDGET_CLASS (meta_preview_parent_class)->draw (widget, cr); return GTK_WIDGET_CLASS (meta_preview_parent_class)->draw (widget, cr);
} }
static void
meta_preview_realize (GtkWidget *widget)
{
MetaPreview *preview = META_PREVIEW (widget);
GTK_WIDGET_CLASS (meta_preview_parent_class)->realize (widget);
preview->style_context = meta_theme_create_style_context (gtk_widget_get_screen (widget),
NULL);
}
#define NO_CHILD_WIDTH 80 #define NO_CHILD_WIDTH 80
#define NO_CHILD_HEIGHT 20 #define NO_CHILD_HEIGHT 20

View File

@ -1022,6 +1022,9 @@ double meta_theme_get_title_scale (MetaTheme *theme,
MetaFrameType type, MetaFrameType type,
MetaFrameFlags flags); MetaFrameFlags flags);
GtkStyleContext * meta_theme_create_style_context (GdkScreen *screen,
const gchar *variant);
void meta_theme_draw_frame (MetaTheme *theme, void meta_theme_draw_frame (MetaTheme *theme,
GtkStyleContext *style_gtk, GtkStyleContext *style_gtk,
cairo_t *cr, cairo_t *cr,

View File

@ -854,7 +854,8 @@ main (int argc, char **argv)
G_CALLBACK (gtk_main_quit), NULL); G_CALLBACK (gtk_main_quit), NULL);
gtk_widget_realize (window); gtk_widget_realize (window);
style = gtk_widget_get_style_context (window);
style = meta_theme_create_style_context (gtk_widget_get_screen (window), NULL);
gtk_style_context_get (style, GTK_STATE_FLAG_NORMAL, "font", &font_desc, NULL); gtk_style_context_get (style, GTK_STATE_FLAG_NORMAL, "font", &font_desc, NULL);
g_assert (style); g_assert (style);
@ -927,13 +928,12 @@ get_flags (GtkWidget *widget)
} }
static int static int
get_text_height (GtkWidget *widget) get_text_height (GtkWidget *widget,
GtkStyleContext *style)
{ {
GtkStyleContext *style;
PangoFontDescription *font_desc; PangoFontDescription *font_desc;
int text_height; int text_height;
style = gtk_widget_get_style_context (widget);
gtk_style_context_get (style, GTK_STATE_FLAG_NORMAL, "font", &font_desc, NULL); gtk_style_context_get (style, GTK_STATE_FLAG_NORMAL, "font", &font_desc, NULL);
text_height = meta_pango_font_desc_get_text_height (font_desc, text_height = meta_pango_font_desc_get_text_height (font_desc,
gtk_widget_get_pango_context (widget)); gtk_widget_get_pango_context (widget));
@ -955,6 +955,7 @@ static void
run_theme_benchmark (void) run_theme_benchmark (void)
{ {
GtkWidget* widget; GtkWidget* widget;
GtkStyleContext *style_context;
cairo_surface_t *pixmap; cairo_surface_t *pixmap;
MetaFrameBorders borders; MetaFrameBorders borders;
MetaButtonState button_states[META_BUTTON_TYPE_LAST] = MetaButtonState button_states[META_BUTTON_TYPE_LAST] =
@ -979,9 +980,11 @@ run_theme_benchmark (void)
widget = gtk_window_new (GTK_WINDOW_TOPLEVEL); widget = gtk_window_new (GTK_WINDOW_TOPLEVEL);
gtk_widget_realize (widget); gtk_widget_realize (widget);
style_context = meta_theme_create_style_context (gtk_widget_get_screen (widget), NULL);
meta_theme_get_frame_borders (global_theme, meta_theme_get_frame_borders (global_theme,
META_FRAME_TYPE_NORMAL, META_FRAME_TYPE_NORMAL,
get_text_height (widget), get_text_height (widget, style_context),
get_flags (widget), get_flags (widget),
&borders); &borders);
@ -1024,13 +1027,13 @@ run_theme_benchmark (void)
cr = cairo_create (pixmap); cr = cairo_create (pixmap);
meta_theme_draw_frame (global_theme, meta_theme_draw_frame (global_theme,
gtk_widget_get_style_context (widget), style_context,
cr, cr,
META_FRAME_TYPE_NORMAL, META_FRAME_TYPE_NORMAL,
get_flags (widget), get_flags (widget),
client_width, client_height, client_width, client_height,
layout, layout,
get_text_height (widget), get_text_height (widget, style_context),
&button_layout, &button_layout,
button_states, button_states,
meta_preview_get_mini_icon (), meta_preview_get_mini_icon (),
@ -1058,6 +1061,7 @@ run_theme_benchmark (void)
g_timer_destroy (timer); g_timer_destroy (timer);
g_object_unref (G_OBJECT (layout)); g_object_unref (G_OBJECT (layout));
g_object_unref (style_context);
gtk_widget_destroy (widget); gtk_widget_destroy (widget);
#undef ITERATIONS #undef ITERATIONS

View File

@ -37,6 +37,7 @@
#include <config.h> #include <config.h>
#include "theme-private.h" #include "theme-private.h"
#include "frames.h" /* for META_TYPE_FRAMES */
#include <meta/util.h> #include <meta/util.h>
#include <meta/gradient.h> #include <meta/gradient.h>
#include <meta/prefs.h> #include <meta/prefs.h>
@ -5438,6 +5439,37 @@ meta_theme_get_title_scale (MetaTheme *theme,
return style->layout->title_scale; return style->layout->title_scale;
} }
GtkStyleContext *
meta_theme_create_style_context (GdkScreen *screen,
const gchar *variant)
{
GtkWidgetPath *path;
GtkStyleContext *style;
char *theme_name;
g_object_get (gtk_settings_get_for_screen (screen),
"gtk-theme-name", &theme_name,
NULL);
style = gtk_style_context_new ();
path = gtk_widget_path_new ();
gtk_widget_path_append_type (path, META_TYPE_FRAMES);
gtk_style_context_set_path (style, path);
gtk_widget_path_unref (path);
if (theme_name && *theme_name)
{
GtkCssProvider *provider;
provider = gtk_css_provider_get_named (theme_name, variant);
gtk_style_context_add_provider (style,
GTK_STYLE_PROVIDER (provider),
GTK_STYLE_PROVIDER_PRIORITY_SETTINGS);
}
return style;
}
void void
meta_theme_draw_frame (MetaTheme *theme, meta_theme_draw_frame (MetaTheme *theme,
GtkStyleContext *style_gtk, GtkStyleContext *style_gtk,