[gnome-software] Do not require exactly correct size screenshots



commit 80f804e9b1413f432797b4abd5077e5eb92231d7
Author: Richard Hughes <richard hughsie com>
Date:   Mon Nov 11 17:36:04 2013 +0000

    Do not require exactly correct size screenshots
    
    If a distro does not provide all the sizes of screenshot required, choose the
    image with the size most similar, not just the first one.
    
    This fixes the 'huuuuuge eclipse screenshot' issue.

 src/gs-screenshot.c    |   30 +++++++++++++++++++++++-------
 src/gs-screenshot.h    |   12 ++++++------
 src/gs-shell-details.c |   32 ++++++++++++--------------------
 3 files changed, 41 insertions(+), 33 deletions(-)
---
diff --git a/src/gs-screenshot.c b/src/gs-screenshot.c
index 01f78e2..562314a 100644
--- a/src/gs-screenshot.c
+++ b/src/gs-screenshot.c
@@ -76,10 +76,10 @@ gs_screenshot_set_is_default (GsScreenshot *screenshot, gboolean is_default)
 }
 
 /**
- * gs_screenshot_get_item:
+ * gs_screenshot_get_item_exact:
  **/
 static GsScreenshotItem *
-gs_screenshot_get_item (GsScreenshot *screenshot, guint width, guint height)
+gs_screenshot_get_item_exact (GsScreenshot *screenshot, guint width, guint height)
 {
        GsScreenshotItem *item;
        guint i;
@@ -88,8 +88,7 @@ gs_screenshot_get_item (GsScreenshot *screenshot, guint width, guint height)
 
        for (i = 0; i < screenshot->priv->array->len; i++) {
                item = g_ptr_array_index (screenshot->priv->array, i);
-               if ((item->width == width || width == G_MAXUINT) &&
-                   (item->height == height || height == G_MAXUINT))
+               if (item->width == width && item->height == height)
                        return item;
        }
 
@@ -113,7 +112,7 @@ gs_screenshot_add_image (GsScreenshot *screenshot,
        g_return_if_fail (height > 0);
 
        /* check if already exists */
-       item = gs_screenshot_get_item (screenshot, width, height);
+       item = gs_screenshot_get_item_exact (screenshot, width, height);
        if (item != NULL) {
                g_debug ("replaced URL %s with %s for %ux%u",
                         item->url, url, width, height);
@@ -130,6 +129,9 @@ gs_screenshot_add_image (GsScreenshot *screenshot,
 
 /**
  * gs_screenshot_get_url:
+ *
+ * Gets the URL with the closest size to @width and @height.
+ *
  **/
 const gchar *
 gs_screenshot_get_url (GsScreenshot *screenshot,
@@ -137,13 +139,27 @@ gs_screenshot_get_url (GsScreenshot *screenshot,
                       guint height,
                       GtkRequisition *provided)
 {
-       GsScreenshotItem *item;
+       GsScreenshotItem *item_tmp;
+       GsScreenshotItem *item = NULL;
+       guint best_size = G_MAXUINT;
+       guint i;
+       guint tmp;
 
        g_return_val_if_fail (GS_IS_SCREENSHOT (screenshot), NULL);
        g_return_val_if_fail (width > 0, NULL);
        g_return_val_if_fail (height > 0, NULL);
 
-       item = gs_screenshot_get_item (screenshot, width, height);
+       /* find the image with the closest size */
+       for (i = 0; i < screenshot->priv->array->len; i++) {
+               item_tmp = g_ptr_array_index (screenshot->priv->array, i);
+               tmp = ABS ((gint64) (width * height) -
+                          (gint64) (item_tmp->width * item_tmp->height));
+               if (tmp < best_size) {
+                       best_size = tmp;
+                       item = item_tmp;
+               }
+       }
+
        if (item == NULL)
                return NULL;
 
diff --git a/src/gs-screenshot.h b/src/gs-screenshot.h
index 556fe27..e9a37c7 100644
--- a/src/gs-screenshot.h
+++ b/src/gs-screenshot.h
@@ -51,12 +51,12 @@ GType                gs_screenshot_get_type         (void);
 
 GsScreenshot   *gs_screenshot_new              (void);
 
-#define GS_SCREENSHOT_SIZE_SMALL_WIDTH         112
-#define GS_SCREENSHOT_SIZE_SMALL_HEIGHT                63
-#define GS_SCREENSHOT_SIZE_LARGE_WIDTH         624
-#define GS_SCREENSHOT_SIZE_LARGE_HEIGHT                351
-#define GS_SCREENSHOT_SIZE_LARGE2_WIDTH                752
-#define GS_SCREENSHOT_SIZE_LARGE2_HEIGHT       423
+#define GS_SCREENSHOT_SIZE_THUMBNAIL_WIDTH     112
+#define GS_SCREENSHOT_SIZE_THUMBNAIL_HEIGHT    63
+#define GS_SCREENSHOT_SIZE_MAIN_WIDTH          624
+#define GS_SCREENSHOT_SIZE_MAIN_HEIGHT         351
+#define GS_SCREENSHOT_SIZE_MAIN_ONLY_WIDTH     752
+#define GS_SCREENSHOT_SIZE_MAIN_ONLY_HEIGHT    423
 
 gboolean        gs_screenshot_get_is_default   (GsScreenshot           *screenshot);
 void            gs_screenshot_set_is_default   (GsScreenshot           *screenshot,
diff --git a/src/gs-shell-details.c b/src/gs-shell-details.c
index 5d15287..0b73786 100644
--- a/src/gs-shell-details.c
+++ b/src/gs-shell-details.c
@@ -250,7 +250,6 @@ gs_shell_details_screenshot_selected_cb (GtkListBox *list,
 static void
 gs_shell_details_refresh_screenshots (GsShellDetails *shell_details)
 {
-       const gchar *tmp;
        GPtrArray *screenshots;
        GsScreenshot *ss;
        GsShellDetailsPrivate *priv = shell_details->priv;
@@ -260,7 +259,6 @@ gs_shell_details_refresh_screenshots (GsShellDetails *shell_details)
        GtkWidget *widget;
        guint i;
        GtkRequisition provided;
-       guint width, height;
 
        /* treat screenshots differently */
        if (gs_app_get_id_kind (priv->app) == GS_APP_ID_KIND_FONT) {
@@ -319,24 +317,18 @@ gs_shell_details_refresh_screenshots (GsShellDetails *shell_details)
 
        /* use a slightly larger screenshot if it's the only screenshot */
        if (screenshots->len == 1) {
-               width = GS_SCREENSHOT_SIZE_LARGE2_WIDTH;
-               height = GS_SCREENSHOT_SIZE_LARGE2_HEIGHT;
+               gs_screenshot_get_url (ss,
+                                      GS_SCREENSHOT_SIZE_MAIN_ONLY_WIDTH,
+                                      GS_SCREENSHOT_SIZE_MAIN_ONLY_HEIGHT,
+                                      &provided);
        } else {
-               width = GS_SCREENSHOT_SIZE_LARGE_WIDTH;
-               height = GS_SCREENSHOT_SIZE_LARGE_HEIGHT;
-       }
-
-       /* do we have a screenshot of the right size? */
-       tmp = gs_screenshot_get_url (ss, width, height, &provided);
-       if (tmp != NULL) {
-               gs_screenshot_image_set_size (GS_SCREENSHOT_IMAGE (ssimg),
-                                             width, height);
-       } else {
-               /* use any size provided */
-               gs_screenshot_get_url (ss, G_MAXUINT, G_MAXUINT, &provided);
-               gs_screenshot_image_set_size (GS_SCREENSHOT_IMAGE (ssimg),
-                                             provided.width, provided.height);
+               gs_screenshot_get_url (ss,
+                                      GS_SCREENSHOT_SIZE_MAIN_WIDTH,
+                                      GS_SCREENSHOT_SIZE_MAIN_HEIGHT,
+                                      &provided);
        }
+       gs_screenshot_image_set_size (GS_SCREENSHOT_IMAGE (ssimg),
+                                     provided.width, provided.height);
 
        gs_screenshot_image_load_async (GS_SCREENSHOT_IMAGE (ssimg), NULL);
        gtk_box_pack_start (GTK_BOX (widget), ssimg, FALSE, FALSE, 0);
@@ -360,8 +352,8 @@ gs_shell_details_refresh_screenshots (GsShellDetails *shell_details)
                                                  g_get_user_cache_dir ());
                gs_screenshot_image_set_screenshot (GS_SCREENSHOT_IMAGE (ssimg), ss);
                gs_screenshot_image_set_size (GS_SCREENSHOT_IMAGE (ssimg),
-                                             GS_SCREENSHOT_SIZE_SMALL_WIDTH,
-                                             GS_SCREENSHOT_SIZE_SMALL_HEIGHT);
+                                             GS_SCREENSHOT_SIZE_THUMBNAIL_WIDTH,
+                                             GS_SCREENSHOT_SIZE_THUMBNAIL_HEIGHT);
                gs_screenshot_image_load_async (GS_SCREENSHOT_IMAGE (ssimg), NULL);
                gtk_list_box_insert (GTK_LIST_BOX (list), ssimg, -1);
                gtk_widget_set_visible (ssimg, TRUE);


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