From 6b15f69e007597c302002a875046096d7d2fdcac Mon Sep 17 00:00:00 2001 From: Dorota Czaplejewicz Date: Thu, 26 Sep 2019 10:34:24 +0000 Subject: [PATCH 1/2] style: Use path instead of class for key --- data/style.css | 4 ++-- eek/eek-renderer.c | 60 +++++++++++++++++++++++++++++++++------------- 2 files changed, 45 insertions(+), 19 deletions(-) diff --git a/data/style.css b/data/style.css index adc2d556..9c734c15 100644 --- a/data/style.css +++ b/data/style.css @@ -4,7 +4,7 @@ font-family: cantarell, sans-serif; } -.key { +button { color: #deddda; background: #464448; border-style: solid; @@ -13,7 +13,7 @@ border-radius: 3px; } -.key:active { +button:active { background: #545256; border-color: #716e78; } diff --git a/eek/eek-renderer.c b/eek/eek-renderer.c index 0c305f11..9ab1a458 100644 --- a/eek/eek-renderer.c +++ b/eek/eek-renderer.c @@ -39,7 +39,7 @@ typedef struct _EekRendererPrivate PangoContext *pcontext; GtkCssProvider *css_provider; GtkStyleContext *scontext; - GtkStyleContext *key_context; + GtkStyleContext *button_context; EekColor default_foreground_color; EekColor default_background_color; @@ -191,24 +191,24 @@ render_button_outline (EekRenderer *renderer, /* Set the name of the button on the widget path, using the name obtained from the button's symbol. */ g_autoptr (GtkWidgetPath) path = NULL; - path = gtk_widget_path_copy (gtk_style_context_get_path (priv->key_context)); + path = gtk_widget_path_copy (gtk_style_context_get_path (priv->button_context)); const char *name = squeek_button_get_name(button); gtk_widget_path_iter_set_name (path, -1, name); /* Update the style context with the updated widget path. */ - gtk_style_context_set_path (priv->key_context, path); + gtk_style_context_set_path (priv->button_context, path); /* Set the state to take into account whether the button is active (pressed) or normal. */ - gtk_style_context_set_state(priv->key_context, + gtk_style_context_set_state(priv->button_context, active ? GTK_STATE_FLAG_ACTIVE : GTK_STATE_FLAG_NORMAL); - gtk_render_background (priv->key_context, + gtk_render_background (priv->button_context, cr, 0, 0, bounds.width, bounds.height); - gtk_render_frame (priv->key_context, + gtk_render_frame (priv->button_context, cr, 0, 0, bounds.width, bounds.height); - gtk_style_context_set_state(priv->key_context, GTK_STATE_FLAG_NORMAL); + gtk_style_context_set_state(priv->button_context, GTK_STATE_FLAG_NORMAL); } static void @@ -263,7 +263,7 @@ render_button (EekRenderer *self, cairo_set_source_surface (cr, outline_surface, 0.0, 0.0); cairo_paint (cr); - eek_renderer_get_foreground_color (self, priv->key_context, &foreground); + eek_renderer_get_foreground_color (self, priv->button_context, &foreground); /* render icon (if any) */ const char *icon_name = squeek_button_get_icon_name(place->button); @@ -587,6 +587,33 @@ eek_renderer_class_init (EekRendererClass *klass) pspec); } + +static GType new_type(char *name) { + GTypeInfo info = {0}; + info.class_size = sizeof(GtkWidgetClass); + info.instance_size = sizeof(GtkWidget); + + return g_type_register_static(GTK_TYPE_WIDGET, name, &info, + G_TYPE_FLAG_ABSTRACT + ); +} + +static GType layout_type() { + static GType type = 0; + if (!type) { + type = new_type("layout"); + } + return type; +} + +static GType button_type() { + static GType type = 0; + if (!type) { + type = new_type("button"); + } + return type; +} + static void eek_renderer_init (EekRenderer *self) { @@ -628,17 +655,16 @@ eek_renderer_init (EekRenderer *self) "/sm/puri/squeekboard/style.css"); /* Create a style context for keys */ - priv->key_context = gtk_style_context_new (); - gtk_style_context_add_class (priv->key_context, "key"); - gtk_style_context_add_provider (priv->key_context, + GtkWidgetPath *path = gtk_widget_path_new(); + gtk_widget_path_append_type(path, layout_type()); + gtk_widget_path_append_type(path, button_type()); + priv->button_context = gtk_style_context_new (); + gtk_style_context_set_path(priv->button_context, path); + gtk_style_context_set_state (priv->button_context, GTK_STATE_FLAG_NORMAL); + gtk_style_context_add_provider (priv->button_context, GTK_STYLE_PROVIDER(priv->css_provider), GTK_STYLE_PROVIDER_PRIORITY_USER); - - g_autoptr (GtkWidgetPath) path = NULL; - path = gtk_widget_path_new (); - gtk_widget_path_append_type (path, GTK_TYPE_BUTTON); - gtk_style_context_set_path (priv->key_context, path); - gtk_style_context_set_state (priv->key_context, GTK_STATE_FLAG_NORMAL); + gtk_widget_path_unref(path); } static void From 2889e505077199e9f7df6f894e028804c127968a Mon Sep 17 00:00:00 2001 From: Dorota Czaplejewicz Date: Thu, 26 Sep 2019 11:03:08 +0000 Subject: [PATCH 2/2] style: Simplified layout styling Layout background is styled in the same place as button background, and obtains the path "layout". --- data/style.css | 2 +- eek/eek-gtk-keyboard.c | 21 ++------------------- eek/eek-renderer.c | 37 ++++++++++++++----------------------- eek/eek-renderer.h | 3 +-- 4 files changed, 18 insertions(+), 45 deletions(-) diff --git a/data/style.css b/data/style.css index 9c734c15..ab90eaf1 100644 --- a/data/style.css +++ b/data/style.css @@ -1,4 +1,4 @@ -.keyboard { +layout { background-color: rgba(0, 0, 0, 255); color: #ffffff; font-family: cantarell, sans-serif; diff --git a/eek/eek-gtk-keyboard.c b/eek/eek-gtk-keyboard.c index 448614e0..c846b082 100644 --- a/eek/eek-gtk-keyboard.c +++ b/eek/eek-gtk-keyboard.c @@ -52,8 +52,6 @@ typedef struct _EekGtkKeyboardPrivate { EekRenderer *renderer; LevelKeyboard *keyboard; - GtkCssProvider *css_provider; - GtkStyleContext *scontext; GdkEventSequence *sequence; // unowned reference } EekGtkKeyboardPrivate; @@ -98,7 +96,7 @@ eek_gtk_keyboard_real_draw (GtkWidget *self, if (!priv->renderer) { PangoContext *pcontext = gtk_widget_get_pango_context (self); - priv->renderer = eek_renderer_new (priv->keyboard, pcontext, priv->scontext); + priv->renderer = eek_renderer_new (priv->keyboard, pcontext); eek_renderer_set_allocation_size (priv->renderer, allocation.width, @@ -383,22 +381,7 @@ eek_gtk_keyboard_class_init (EekGtkKeyboardClass *klass) static void eek_gtk_keyboard_init (EekGtkKeyboard *self) -{ - EekGtkKeyboardPrivate *priv = eek_gtk_keyboard_get_instance_private (self); - - /* Create a default CSS provider and load a style sheet */ - priv->css_provider = gtk_css_provider_new (); - gtk_css_provider_load_from_resource (priv->css_provider, - "/sm/puri/squeekboard/style.css"); - - /* Apply the style to the widget */ - priv->scontext = gtk_widget_get_style_context (GTK_WIDGET(self)); - gtk_style_context_add_class (priv->scontext, "keyboard"); - gtk_style_context_add_provider (priv->scontext, - GTK_STYLE_PROVIDER(priv->css_provider), - GTK_STYLE_PROVIDER_PRIORITY_USER); - gtk_style_context_set_state (priv->scontext, GTK_STATE_FLAG_NORMAL); -} +{} /** * eek_gtk_keyboard_new: diff --git a/eek/eek-renderer.c b/eek/eek-renderer.c index 9ab1a458..70ca8e31 100644 --- a/eek/eek-renderer.c +++ b/eek/eek-renderer.c @@ -29,7 +29,6 @@ enum { PROP_0, PROP_PCONTEXT, - PROP_STYLE_CONTEXT, PROP_LAST }; @@ -38,7 +37,7 @@ typedef struct _EekRendererPrivate LevelKeyboard *keyboard; PangoContext *pcontext; GtkCssProvider *css_provider; - GtkStyleContext *scontext; + GtkStyleContext *layout_context; GtkStyleContext *button_context; EekColor default_foreground_color; @@ -140,7 +139,7 @@ render_keyboard_surface (EekRenderer *renderer, struct squeek_view *view) EekRendererPrivate *priv = eek_renderer_get_instance_private (renderer); EekColor foreground; - eek_renderer_get_foreground_color (renderer, priv->scontext, &foreground); + eek_renderer_get_foreground_color (renderer, priv->layout_context, &foreground); EekBounds bounds = squeek_view_get_bounds (level_keyboard_current(priv->keyboard)); @@ -151,11 +150,11 @@ render_keyboard_surface (EekRenderer *renderer, struct squeek_view *view) }; /* Paint the background covering the entire widget area */ - gtk_render_background (priv->scontext, + gtk_render_background (priv->layout_context, data.cr, 0, 0, priv->allocation_width, priv->allocation_height); - gtk_render_frame (priv->scontext, + gtk_render_frame (priv->layout_context, data.cr, 0, 0, priv->allocation_width, priv->allocation_height); @@ -496,10 +495,6 @@ eek_renderer_set_property (GObject *object, priv->pcontext = g_value_get_object (value); g_object_ref (priv->pcontext); break; - case PROP_STYLE_CONTEXT: - priv->scontext = g_value_get_object (value); - g_object_ref (priv->scontext); - break; default: G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec); break; @@ -576,15 +571,6 @@ eek_renderer_class_init (EekRendererClass *klass) g_object_class_install_property (gobject_class, PROP_PCONTEXT, pspec); - - pspec = g_param_spec_object ("style-context", - "GTK Style Context", - "GTK Style Context", - GTK_TYPE_STYLE_CONTEXT, - G_PARAM_CONSTRUCT_ONLY | G_PARAM_WRITABLE); - g_object_class_install_property (gobject_class, - PROP_STYLE_CONTEXT, - pspec); } @@ -657,6 +643,13 @@ eek_renderer_init (EekRenderer *self) /* Create a style context for keys */ GtkWidgetPath *path = gtk_widget_path_new(); gtk_widget_path_append_type(path, layout_type()); + + priv->layout_context = gtk_style_context_new(); + gtk_style_context_set_path(priv->layout_context, path); + gtk_style_context_add_provider (priv->layout_context, + GTK_STYLE_PROVIDER(priv->css_provider), + GTK_STYLE_PROVIDER_PRIORITY_USER); + gtk_widget_path_append_type(path, button_type()); priv->button_context = gtk_style_context_new (); gtk_style_context_set_path(priv->button_context, path); @@ -686,12 +679,10 @@ invalidate (EekRenderer *renderer) EekRenderer * eek_renderer_new (LevelKeyboard *keyboard, - PangoContext *pcontext, - GtkStyleContext *scontext) + PangoContext *pcontext) { EekRenderer *renderer = g_object_new (EEK_TYPE_RENDERER, "pango-context", pcontext, - "style-context", scontext, NULL); EekRendererPrivate *priv = eek_renderer_get_instance_private (renderer); priv->keyboard = keyboard; @@ -785,7 +776,7 @@ eek_renderer_get_button_bounds (EekRenderer *renderer, min = points[2]; max = points[0]; - for (uint i = 0; i < G_N_ELEMENTS(points); i++) { + for (unsigned i = 0; i < G_N_ELEMENTS(points); i++) { eek_point_rotate (&points[i], angle); if (points[i].x < min.x) min.x = points[i].x; @@ -953,7 +944,7 @@ eek_are_bounds_inside (EekBounds bounds, EekPoint point, EekPoint origin, int32_ points[3].x = points[0].x; points[3].y = points[2].y; - for (uint i = 0; i < G_N_ELEMENTS(points); i++) { + for (unsigned i = 0; i < G_N_ELEMENTS(points); i++) { eek_point_rotate (&points[i], angle); points[i].x += origin.x; points[i].y += origin.y; diff --git a/eek/eek-renderer.h b/eek/eek-renderer.h index 4a72e763..76d151f5 100644 --- a/eek/eek-renderer.h +++ b/eek/eek-renderer.h @@ -57,8 +57,7 @@ struct _EekRendererClass GType eek_renderer_get_type (void) G_GNUC_CONST; EekRenderer *eek_renderer_new (LevelKeyboard *keyboard, - PangoContext *pcontext, - GtkStyleContext *scontext); + PangoContext *pcontext); void eek_renderer_set_allocation_size (EekRenderer *renderer, gdouble width,