Skip to content
This repository has been archived by the owner on Jan 23, 2023. It is now read-only.

Commit

Permalink
Re #142: Move some responsibilities to Media
Browse files Browse the repository at this point in the history
The loader shouldn't decide whether special cases are met.

Also, a helper function means that you don't have to understand logic of
loading hires texture to get the right suface
  • Loading branch information
IBBoard committed Aug 27, 2020
1 parent bc1a696 commit 5bed822
Show file tree
Hide file tree
Showing 4 changed files with 48 additions and 38 deletions.
31 changes: 31 additions & 0 deletions src/CbMedia.c
Expand Up @@ -143,9 +143,34 @@ cb_media_loading_finished (CbMedia *media)
{
g_return_if_fail (CB_IS_MEDIA (media));

media->thumb_width = cairo_image_surface_get_width(media->surface);
media->thumb_height = cairo_image_surface_get_height(media->surface);

// Take these sizes as full size if full size isn't set.
// This happens when loading third-party images which don't have
// Twitter's scaling variants.
if (media->width == -1) {
media->width = media->thumb_width;
}

if (media->height == -1) {
media->height = media->thumb_height;
}

media->invalid = FALSE;
media->loaded = TRUE;
media->loading = FALSE;

if (media->height == media->thumb_height && media->width == media->thumb_width) {
// There is no higher res to load so pretend we did.
// The get_highest_res_surface() function then deals with what is available
media->loaded_hires = TRUE;
}
else if (cb_media_is_video (media)) {
// Video doesn't have a hires, it runs the URL through GStreamer, so pretend we loaded the hires image
media->loaded_hires = TRUE;
}

cb_media_update_progress (media, 1.0);
}

Expand Down Expand Up @@ -195,4 +220,10 @@ cb_media_loading_hires_finished (CbMedia *media)
media->loading_hires = FALSE;

cb_media_update_hires_progress (media, 1.0);
}

cairo_surface_t *
cb_media_get_highest_res_surface (CbMedia *media)
{
return media->surface_hires == NULL ? media->surface : media->surface_hires;
}
1 change: 1 addition & 0 deletions src/CbMedia.h
Expand Up @@ -77,6 +77,7 @@ void cb_media_loading_hires_finished (CbMedia *media);
void cb_media_update_hires_progress (CbMedia *media,
double progress);
CbMediaType cb_media_type_from_url (const char *url);
cairo_surface_t * cb_media_get_highest_res_surface (CbMedia *media);

G_END_DECLS

Expand Down
17 changes: 0 additions & 17 deletions src/CbMediaDownloader.c
Expand Up @@ -390,23 +390,6 @@ cb_media_downloader_load_threaded (CbMediaDownloader *downloader,
return;
}

media->thumb_width = cairo_image_surface_get_width(media->surface);
media->thumb_height = cairo_image_surface_get_height(media->surface);

// Take these sizes as full size if full size isn't set.
// This happens when loading third-party images which don't have
// Twitter's scaling variants.
if (media->width == -1) {
media->width = media->thumb_width;
}

if (media->height == -1) {
media->height = media->thumb_height;
}

media->loaded = TRUE;
media->invalid = FALSE;

cb_media_loading_finished (media);
}

Expand Down
37 changes: 16 additions & 21 deletions src/CbMediaImageWidget.c
Expand Up @@ -133,32 +133,27 @@ cb_media_image_widget_new (CbMedia *media, GdkRectangle *max_dimensions)
if (media->type == CB_MEDIA_TYPE_GIF) {
gtk_image_set_from_animation (GTK_IMAGE (self->image), media->animation);
}
else if (media->surface_hires != NULL) {
gtk_image_set_from_surface (GTK_IMAGE (self->image), media->surface_hires);
else if (media->loaded_hires) {
gtk_image_set_from_surface (GTK_IMAGE (self->image), cb_media_get_highest_res_surface(media));
}
else {
double scale_width = media->width * 1.0 / media->thumb_width;
double scale_height = media->height * 1.0 / media->thumb_height;

if (scale_width != 1 || scale_height != 1) {
data = g_new0 (LoadingData, 1);
data->widget = g_object_ref (self);
data->media = g_object_ref (media);
g_signal_connect(media, "hires-progress", G_CALLBACK(hires_progress), data);
self->image_surface = cairo_image_surface_create(cairo_image_surface_get_format(media->surface), media->width, media->height);
cairo_t *ct = cairo_create(self->image_surface);
cairo_scale(ct, scale_width, scale_height);
cairo_set_source_surface (ct, media->surface, 0, 0);
cairo_paint(ct);
cairo_destroy(ct);
gtk_image_set_from_surface (GTK_IMAGE (self->image), self->image_surface);
if (!media->loading) {
// NULL callback because we should pick it up from the earlier g_signal_connect
cb_media_downloader_load_hires_async (cb_media_downloader_get_default(), media, NULL, NULL);
}
}
else {
gtk_image_set_from_surface (GTK_IMAGE (self->image), media->surface);
data = g_new0 (LoadingData, 1);
data->widget = g_object_ref (self);
data->media = g_object_ref (media);
g_signal_connect(media, "hires-progress", G_CALLBACK(hires_progress), data);
self->image_surface = cairo_image_surface_create(cairo_image_surface_get_format(media->surface), media->width, media->height);
cairo_t *ct = cairo_create(self->image_surface);
cairo_scale(ct, scale_width, scale_height);
cairo_set_source_surface (ct, media->surface, 0, 0);
cairo_paint(ct);
cairo_destroy(ct);
gtk_image_set_from_surface (GTK_IMAGE (self->image), self->image_surface);
if (!media->loading) {
// NULL callback because we should pick it up from the earlier g_signal_connect
cb_media_downloader_load_hires_async (cb_media_downloader_get_default(), media, NULL, NULL);
}
}

Expand Down

0 comments on commit 5bed822

Please sign in to comment.