From 8f7985467935fba3582ef1549f3463ca4953aec6 Mon Sep 17 00:00:00 2001 From: Joshua Strobl Date: Wed, 7 Jul 2021 21:20:05 +0300 Subject: [PATCH] Fix inconsistent updating of KotoButton iconography noticed via KotoExpander in KotoNav. Turtles all the way down. --- src/koto-button.c | 43 +++++++++++++++++++++++++++++++++++-------- src/koto-expander.c | 42 ++++++++++++++++++++++++++++++------------ src/koto-expander.h | 4 ++-- 3 files changed, 67 insertions(+), 22 deletions(-) diff --git a/src/koto-button.c b/src/koto-button.c index 82caf3c..4c5b8df 100644 --- a/src/koto-button.c +++ b/src/koto-button.c @@ -309,6 +309,10 @@ void koto_button_flip(KotoButton * self) { } void koto_button_hide_image(KotoButton * self) { + if (!KOTO_IS_BUTTON(self)) { // Not a button + return; + } + if (GTK_IS_WIDGET(self->button_pic)) { // Is a widget gtk_widget_hide(self->button_pic); } @@ -318,6 +322,10 @@ void koto_button_set_badge_text( KotoButton * self, gchar * text ) { + if (!KOTO_IS_BUTTON(self)) { // Not a button + return; + } + if ((text == NULL) || (strcmp(text, "") == 0)) { // If the text is empty self->badge_text = g_strdup(""); } else { @@ -366,6 +374,14 @@ void koto_button_set_icon_name( gchar * icon_name, gboolean for_alt ) { + if (!KOTO_IS_BUTTON(self)) { // Not a button + return; + } + + if (!koto_utils_is_string_valid(icon_name)) { // Not a valid icon + return; + } + gchar * copied_icon_name = g_strdup(icon_name); if (for_alt) { // Is for the alternate icon @@ -390,12 +406,12 @@ void koto_button_set_icon_name( hide_image = TRUE; } - if (hide_image) { // Should hide the image - if (GTK_IS_PICTURE(self->button_pic)) { // If we already have a button image + if (GTK_IS_IMAGE(self->button_pic)) { // If we already have a button image + if (hide_image) { // Should hide the image gtk_widget_hide(self->button_pic); // Hide + } else { // Should be showing the image + gtk_image_set_from_icon_name(GTK_IMAGE(self->button_pic), self->icon_name); // Just update the existing image immediately } - - return; } g_object_notify_by_pspec(G_OBJECT(self), for_alt ? btn_props[PROP_ALT_ICON_NAME] : btn_props[PROP_ICON_NAME]); @@ -405,6 +421,10 @@ void koto_button_set_image_position( KotoButton * self, KotoButtonImagePosition pos ) { + if (!KOTO_IS_BUTTON(self)) { // Not a button + return; + } + if (self->image_position == pos) { // Is a different position that currently return; } @@ -424,6 +444,10 @@ void koto_button_set_pixbuf_size( KotoButton * self, guint size ) { + if (!KOTO_IS_BUTTON(self)) { + return; + } + if (size == self->pix_size) { return; } @@ -438,11 +462,15 @@ void koto_button_set_text( KotoButton * self, gchar * text ) { - if (text == NULL) { + if (!KOTO_IS_BUTTON(self)) { return; } - if (self->text != NULL) { // Text defined + if (!koto_utils_is_string_valid(text)) { // Invalid text + return; + } + + if (koto_utils_is_string_valid(self->text)) { // Text defined g_free(self->text); // Free existing text } @@ -492,7 +520,6 @@ void koto_button_show_image( gtk_box_prepend(GTK_BOX(self), self->button_pic); // Prepend to the box } } else { // From icon name - if (use_alt && ((self->alt_icon_name == NULL) || (strcmp(self->alt_icon_name, "") == 0))) { // Don't have an alt icon set return; } else if (!use_alt && ((self->icon_name == NULL) || (strcmp(self->icon_name, "") == 0))) { // Don't have icon set @@ -503,7 +530,7 @@ void koto_button_show_image( gchar * name = use_alt ? self->alt_icon_name : self->icon_name; if (GTK_IS_IMAGE(self->button_pic)) { - gtk_image_set_from_icon_name(GTK_IMAGE(self->button_pic), name); // Just update the existing iamge + gtk_image_set_from_icon_name(GTK_IMAGE(self->button_pic), name); // Just update the existing image } else { // Not an image self->button_pic = gtk_image_new_from_icon_name(name); // Get our new image gtk_box_prepend(GTK_BOX(self), self->button_pic); // Prepend to the box diff --git a/src/koto-expander.c b/src/koto-expander.c index 41000f0..e841e82 100644 --- a/src/koto-expander.c +++ b/src/koto-expander.c @@ -20,6 +20,7 @@ #include "config/config.h" #include "koto-button.h" #include "koto-expander.h" +#include "koto-utils.h" enum { PROP_EXP_0, @@ -146,20 +147,9 @@ static void koto_expander_set_property( ) { KotoExpander * self = KOTO_EXPANDER(obj); - if (!GTK_IS_WIDGET(self->header_button)) { // Header Button is not a widget - KotoButton * new_button = koto_button_new_with_icon(NULL, "emblem-favorite-symbolic", NULL, KOTO_BUTTON_PIXBUF_SIZE_SMALL); - - if (GTK_IS_WIDGET(new_button)) { // Created our widget successfully - self->header_button = new_button; - gtk_widget_set_hexpand(GTK_WIDGET(self->header_button), TRUE); - gtk_box_prepend(GTK_BOX(self->header), GTK_WIDGET(self->header_button)); - } - } - switch (prop_id) { case PROP_HEADER_ICON_NAME: - g_return_if_fail(GTK_IS_WIDGET(self->header_button)); - koto_button_set_icon_name(self->header_button, g_strdup(g_value_get_string(val)), FALSE); + koto_expander_set_icon_name(self, g_strdup(g_value_get_string(val))); break; case PROP_HEADER_LABEL: g_return_if_fail(GTK_IS_WIDGET(self->header_button)); @@ -193,6 +183,14 @@ static void koto_expander_init(KotoExpander * self) { gtk_revealer_set_reveal_child(GTK_REVEALER(self->revealer), TRUE); // Set to be revealed by default gtk_revealer_set_transition_type(GTK_REVEALER(self->revealer), GTK_REVEALER_TRANSITION_TYPE_SLIDE_DOWN); + KotoButton * new_button = koto_button_new_with_icon(NULL, "emblem-favorite-symbolic", NULL, KOTO_BUTTON_PIXBUF_SIZE_SMALL); + + if (KOTO_IS_BUTTON(new_button)) { // Created our widget successfully + self->header_button = new_button; + gtk_widget_set_hexpand(GTK_WIDGET(self->header_button), TRUE); + gtk_box_prepend(GTK_BOX(self->header), GTK_WIDGET(self->header_button)); + } + self->header_expand_button = koto_button_new_with_icon("", "pan-down-symbolic", "pan-up-symbolic", KOTO_BUTTON_PIXBUF_SIZE_SMALL); gtk_box_append(GTK_BOX(self->header), GTK_WIDGET(self->header_expand_button)); @@ -204,6 +202,26 @@ static void koto_expander_init(KotoExpander * self) { koto_button_add_click_handler(self->header_expand_button, KOTO_BUTTON_CLICK_TYPE_PRIMARY, G_CALLBACK(koto_expander_toggle_content), self); } +// koto_expander_set_icon_name will set the icon for our inner KotoButton for this Expander +void koto_expander_set_icon_name( + KotoExpander *self, + gchar * icon +) { + if (!KOTO_IS_EXPANDER(self)) { // Not a KotoExpander + return; + } + + if (!koto_utils_is_string_valid(icon)) { // Not a valid string + return; + } + + if (!KOTO_IS_BUTTON(self->header_button)) { // Don't have a header button for whatever reason + return; + } + + koto_button_set_icon_name(self->header_button, icon, FALSE); +} + void koto_expander_set_secondary_button( KotoExpander * self, KotoButton * new_button diff --git a/src/koto-expander.h b/src/koto-expander.h index 2677cd3..63618fb 100644 --- a/src/koto-expander.h +++ b/src/koto-expander.h @@ -36,8 +36,8 @@ KotoExpander * koto_expander_new_with_button( GtkWidget * koto_expander_get_content(KotoExpander * self); void koto_expander_set_icon_name( - KotoExpander * self, - const gchar* in + KotoExpander *self, + gchar * icon ); void koto_expander_set_label(