[gnome-software: 1/2] progress-button: Don't special-case 0% and 100%




commit 95a4280842089b6a9a90ad729199c492d77e9cdc
Author: Adrien Plazas <kekun plazas laposte net>
Date:   Tue Jun 22 10:33:06 2021 +0200

    progress-button: Don't special-case 0% and 100%
    
    We can simply handle them as all other value, so let's simplify the code
    a bit. This also uses the stack rather than the heap to store the string
    as its possible sizes are well known and small.

 src/gs-progress-button.c | 9 +++------
 1 file changed, 3 insertions(+), 6 deletions(-)
---
diff --git a/src/gs-progress-button.c b/src/gs-progress-button.c
index 8c8ddeca6..878292240 100644
--- a/src/gs-progress-button.c
+++ b/src/gs-progress-button.c
@@ -24,7 +24,7 @@ G_DEFINE_TYPE (GsProgressButton, gs_progress_button, GTK_TYPE_BUTTON)
 void
 gs_progress_button_set_progress (GsProgressButton *button, guint percentage)
 {
-       g_autofree gchar *tmp = NULL;
+       gchar tmp[64]; /* Large enough to hold the string below. */
        const gchar *css;
 
        if (percentage == GS_APP_PROGRESS_UNKNOWN) {
@@ -32,12 +32,9 @@ gs_progress_button_set_progress (GsProgressButton *button, guint percentage)
                      "  background-size: 25%;\n"
                      "  animation: install-progress-unknown-move infinite linear 2s;\n"
                      "}\n";
-       } else if (percentage == 0) {
-               css = ".install-progress { background-size: 0; }";
-       } else if (percentage == 100) {
-               css = ".install-progress { background-size: 100%; }";
        } else {
-               tmp = g_strdup_printf (".install-progress { background-size: %u%%; }", percentage);
+               percentage = MIN (percentage, 100); /* No need to clamp an unsigned to 0, it produces errors. 
*/
+               g_assert ((gsize) g_snprintf (tmp, sizeof (tmp), ".install-progress { background-size: %u%%; 
}", percentage) < sizeof (tmp));
                css = tmp;
        }
 


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