[gnome-software/wip/kalev/details-page-progress: 1/4] Break out the progress button as a separate class



commit 706bb64998d4205cc6594e18cc21780dce74fa37
Author: Kalev Lember <klember redhat com>
Date:   Mon Jul 13 18:05:37 2015 +0200

    Break out the progress button as a separate class

 src/Makefile.am          |    2 +
 src/gs-app-row.c         |   30 ++------------
 src/gs-app-row.ui        |    2 +-
 src/gs-progress-button.c |   98 ++++++++++++++++++++++++++++++++++++++++++++++
 src/gs-progress-button.h |   61 ++++++++++++++++++++++++++++
 5 files changed, 167 insertions(+), 26 deletions(-)
---
diff --git a/src/Makefile.am b/src/Makefile.am
index 840c539..330cf73 100644
--- a/src/Makefile.am
+++ b/src/Makefile.am
@@ -141,6 +141,8 @@ gnome_software_SOURCES =                            \
        gs-plugin.h                                     \
        gs-profile.c                                    \
        gs-profile.h                                    \
+       gs-progress-button.c                            \
+       gs-progress-button.h                            \
        gs-screenshot-image.c                           \
        gs-screenshot-image.h                           \
        gs-shell.c                                      \
diff --git a/src/gs-app-row.c b/src/gs-app-row.c
index ff516f8..bdca6f1 100644
--- a/src/gs-app-row.c
+++ b/src/gs-app-row.c
@@ -29,6 +29,7 @@
 #include "gs-cleanup.h"
 #include "gs-star-widget.h"
 #include "gs-markdown.h"
+#include "gs-progress-button.h"
 #include "gs-utils.h"
 #include "gs-folders.h"
 
@@ -44,7 +45,6 @@ struct _GsAppRowPrivate
        GtkWidget       *description_label;
        GtkWidget       *button_box;
        GtkWidget       *button;
-       GtkCssProvider  *button_css_provider;
        GtkWidget       *spinner;
        GtkWidget       *label;
        GtkWidget       *checkbox;
@@ -112,20 +112,6 @@ gs_app_row_get_description (GsAppRow *app_row)
 }
 
 /**
- * gs_app_row_get_button_css:
- **/
-static gchar *
-gs_app_row_get_button_css (gint percentage)
-{
-       if (percentage == 0)
-               return g_strdup (".button.install-progress { background: @theme_bg_color; }");
-       else if (percentage == 100)
-               return g_strdup (".button.install-progress { background: @theme_selected_bg_color; }");
-       else
-               return g_strdup_printf (".button.install-progress { background: linear-gradient(to right, 
@theme_selected_bg_color %d%%, @theme_bg_color %d%%); }", percentage, percentage + 1);
-}
-
-/**
  * gs_app_row_refresh:
  **/
 void
@@ -134,21 +120,19 @@ gs_app_row_refresh (GsAppRow *app_row)
        GsAppRowPrivate *priv = app_row->priv;
        GtkStyleContext *context;
        GString *str = NULL;
-       _cleanup_free_ gchar *button_css = NULL;
 
        if (app_row->priv->app == NULL)
                return;
 
        /* do a fill bar for the current progress */
-       context = gtk_widget_get_style_context (priv->button);
        switch (gs_app_get_state (priv->app)) {
        case AS_APP_STATE_INSTALLING:
-               button_css = gs_app_row_get_button_css (gs_app_get_progress (priv->app));
-               gtk_css_provider_load_from_data (priv->button_css_provider, button_css, -1, NULL);
-               gtk_style_context_add_class (context, "install-progress");
+               gs_progress_button_set_progress (GS_PROGRESS_BUTTON (priv->button),
+                                                gs_app_get_progress (priv->app));
+               gs_progress_button_set_show_progress (GS_PROGRESS_BUTTON (priv->button), TRUE);
                break;
        default:
-               gtk_style_context_remove_class (context, "install-progress");
+               gs_progress_button_set_show_progress (GS_PROGRESS_BUTTON (priv->button), FALSE);
                break;
        }
 
@@ -504,10 +488,6 @@ gs_app_row_init (GsAppRow *app_row)
        gtk_widget_init_template (GTK_WIDGET (app_row));
 
        priv->colorful = TRUE;
-       priv->button_css_provider = gtk_css_provider_new ();
-       gtk_style_context_add_provider (gtk_widget_get_style_context (priv->button),
-                                       GTK_STYLE_PROVIDER (priv->button_css_provider),
-                                       GTK_STYLE_PROVIDER_PRIORITY_APPLICATION);
 
        g_signal_connect (priv->button, "clicked",
                          G_CALLBACK (button_clicked), app_row);
diff --git a/src/gs-app-row.ui b/src/gs-app-row.ui
index bf65033..b4f0645 100644
--- a/src/gs-app-row.ui
+++ b/src/gs-app-row.ui
@@ -122,7 +122,7 @@
             <property name="halign">end</property>
             <property name="valign">center</property>
             <child>
-              <object class="GtkButton" id="button">
+              <object class="GsProgressButton" id="button">
                 <property name="margin_end">24</property>
                 <property name="width_request">100</property>
                 <property name="halign">end</property>
diff --git a/src/gs-progress-button.c b/src/gs-progress-button.c
new file mode 100644
index 0000000..aea60aa
--- /dev/null
+++ b/src/gs-progress-button.c
@@ -0,0 +1,98 @@
+/* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*-
+ *
+ * Copyright (C) 2013-2014 Richard Hughes <richard hughsie com>
+ *
+ * Licensed under the GNU General Public License Version 2
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2 of the License, or
+ * (at your option) any later version.
+ *
+ * This program 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 General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
+ */
+
+#include "config.h"
+
+#include "gs-progress-button.h"
+
+struct _GsProgressButtonPrivate
+{
+       GtkCssProvider  *css_provider;
+};
+
+G_DEFINE_TYPE_WITH_PRIVATE (GsProgressButton, gs_progress_button, GTK_TYPE_BUTTON)
+
+void
+gs_progress_button_set_progress (GsProgressButton *button, guint percentage)
+{
+       GsProgressButtonPrivate *priv = gs_progress_button_get_instance_private (button);
+       gchar *css;
+
+       if (percentage == 0)
+               css = g_strdup (".button.install-progress { background: @theme_bg_color; }");
+       else if (percentage == 100)
+               css = g_strdup (".button.install-progress { background: @theme_selected_bg_color; }");
+       else
+               css = g_strdup_printf (".button.install-progress { background: linear-gradient(to right, 
@theme_selected_bg_color %d%%, @theme_bg_color %d%%); }", percentage, percentage + 1);
+
+       gtk_css_provider_load_from_data (priv->css_provider, css, -1, NULL);
+       g_free (css);
+}
+
+void
+gs_progress_button_set_show_progress (GsProgressButton *button, gboolean show_progress)
+{
+       GtkStyleContext *context;
+
+       context = gtk_widget_get_style_context (GTK_WIDGET (button));
+       if (show_progress)
+               gtk_style_context_add_class (context, "install-progress");
+       else
+               gtk_style_context_remove_class (context, "install-progress");
+}
+
+static void
+gs_progress_button_dispose (GObject *object)
+{
+       GsProgressButton *button = GS_PROGRESS_BUTTON (object);
+       GsProgressButtonPrivate *priv = gs_progress_button_get_instance_private (button);
+
+       g_clear_object (&priv->css_provider);
+
+       G_OBJECT_CLASS (gs_progress_button_parent_class)->dispose (object);
+}
+
+static void
+gs_progress_button_init (GsProgressButton *button)
+{
+       GsProgressButtonPrivate *priv = gs_progress_button_get_instance_private (button);
+
+       priv->css_provider = gtk_css_provider_new ();
+       gtk_style_context_add_provider (gtk_widget_get_style_context (GTK_WIDGET (button)),
+                                       GTK_STYLE_PROVIDER (priv->css_provider),
+                                       GTK_STYLE_PROVIDER_PRIORITY_APPLICATION);
+}
+
+static void
+gs_progress_button_class_init (GsProgressButtonClass *klass)
+{
+       GObjectClass *object_class = G_OBJECT_CLASS (klass);
+
+       object_class->dispose = gs_progress_button_dispose;
+}
+
+GtkWidget *
+gs_progress_button_new (void)
+{
+       return g_object_new (GS_TYPE_PROGRESS_BUTTON, NULL);
+}
+
+/* vim: set noexpandtab: */
diff --git a/src/gs-progress-button.h b/src/gs-progress-button.h
new file mode 100644
index 0000000..ba10d21
--- /dev/null
+++ b/src/gs-progress-button.h
@@ -0,0 +1,61 @@
+/* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*-
+ *
+ * Copyright (C) 2013-2014 Richard Hughes <richard hughsie com>
+ *
+ * Licensed under the GNU General Public License Version 2
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2 of the License, or
+ * (at your option) any later version.
+ *
+ * This program 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 General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
+ */
+
+#ifndef GS_PROGRESS_BUTTON_H
+#define GS_PROGRESS_BUTTON_H
+
+#include <gtk/gtk.h>
+
+#define GS_TYPE_PROGRESS_BUTTON                        (gs_progress_button_get_type())
+#define GS_PROGRESS_BUTTON(obj)                        (G_TYPE_CHECK_INSTANCE_CAST((obj), 
GS_TYPE_PROGRESS_BUTTON, GsProgressButton))
+#define GS_PROGRESS_BUTTON_CLASS(cls)          (G_TYPE_CHECK_CLASS_CAST((cls), GS_TYPE_PROGRESS_BUTTON, 
GsProgressButtonClass))
+#define GS_IS_PROGRESS_BUTTON(obj)             (G_TYPE_CHECK_INSTANCE_TYPE((obj), GS_TYPE_PROGRESS_BUTTON))
+#define GS_IS_PROGRESS_BUTTON_CLASS(cls)       (G_TYPE_CHECK_CLASS_TYPE((cls), GS_TYPE_PROGRESS_BUTTON))
+#define GS_PROGRESS_BUTTON_GET_CLASS(obj)      (G_TYPE_INSTANCE_GET_CLASS((obj), GS_TYPE_PROGRESS_BUTTON, 
GsProgressButtonClass))
+
+G_BEGIN_DECLS
+
+typedef struct _GsProgressButton               GsProgressButton;
+typedef struct _GsProgressButtonClass          GsProgressButtonClass;
+typedef struct _GsProgressButtonPrivate                GsProgressButtonPrivate;
+
+struct _GsProgressButton
+{
+       GtkButton        parent;
+};
+
+struct _GsProgressButtonClass
+{
+       GtkButtonClass   parent_class;
+};
+
+GType           gs_progress_button_get_type            (void);
+GtkWidget      *gs_progress_button_new                 (void);
+void            gs_progress_button_set_progress        (GsProgressButton       *button,
+                                                        guint                   percentage);
+void            gs_progress_button_set_show_progress   (GsProgressButton       *button,
+                                                        gboolean                show_progress);
+
+G_END_DECLS
+
+#endif /* GS_PROGRESS_BUTTON_H */
+
+/* vim: set noexpandtab: */


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