diff options
author | P. F. Chimento <philip.chimento@gmail.com> | 2013-08-07 18:08:49 -0700 |
---|---|---|
committer | P. F. Chimento <philip.chimento@gmail.com> | 2013-08-07 18:08:49 -0700 |
commit | d8835f8c28167cb6777baa8c5c30fed3e8aa2db0 (patch) | |
tree | a29ddefb3bd30545081ed51719f4c8d5f2cb748d | |
parent | 6d8f6e781ac14a3fd7ab25cdef9e6ac4ec724445 (diff) | |
parent | 87c5c2726c130aab12c6c1808dca00c01c4c970a (diff) |
Merge pull request #251 from endlessm/issues/247
Fixed the pixbuf sizing on the category images #247
-rw-r--r-- | wikipedia/utils.js | 57 | ||||
-rw-r--r-- | wikipedia/views/category_button.js | 24 | ||||
-rw-r--r-- | wikipedia/views/title_label_view.js | 31 |
3 files changed, 34 insertions, 78 deletions
diff --git a/wikipedia/utils.js b/wikipedia/utils.js index 404f412..f08deed 100644 --- a/wikipedia/utils.js +++ b/wikipedia/utils.js @@ -162,39 +162,30 @@ const array_contains = function (arr, obj, same_type) { return false; }; -// Convenience function to parse a clutter color from a string -function parse_clutter_color(color_string) { - let [res, color] = Clutter.Color.from_string(color_string); - return color; -} - -// Convenience function to load a gresource image into a Clutter.Image -function load_clutter_image_from_resource(resource_path) { - let pixbuf = GdkPixbuf.Pixbuf.new_from_resource(resource_path); - let image = new Clutter.Image(); - if (pixbuf != null) { - image.set_data(pixbuf.get_pixels(), - pixbuf.get_has_alpha() - ? Cogl.PixelFormat.RGBA_8888 - : Cogl.PixelFormat.RGB_888, - pixbuf.get_width(), - pixbuf.get_height(), - pixbuf.get_rowstride()); - } - return image; -} - -// Private function to format a clutter actors allocation and print it -function _clutter_allocation_printer(actor, box, flag) { - print("Allocation for", actor); - print(" Xs:", box.x1, box.x2); - print(" Ys:", box.y1, box.y2); -} - -// Call this function on a clutter actor to have it log it's allocation to -// console -function print_clutter_actor_allocation(actor) { - actor.connect('allocation-changed', _clutter_allocation_printer); +/* + * Loads a pixbuf sized to cover the dest_width and dest_height with the + * image in res_path, while mataining the aspect ratio of the image + */ +function load_pixbuf_cover(res_path, dest_width, dest_height) { + let [load_width, load_height] = [dest_width, dest_height]; + // TODO: We need to get the size of the source image, so right now we + // are loading the image twice, once to get the size, and the again at + // the proper size. We should eventually use a GdkPixbuf.Loader and + // connect to the size-prepared signal, as described in the + // documentation + let temp_pixbuf = GdkPixbuf.Pixbuf.new_from_resource(res_path); + let source_aspect = temp_pixbuf.width / temp_pixbuf.height; + let dest_aspect = dest_width / dest_height; + if(dest_aspect > source_aspect) + load_height = -1; + else + load_width = -1; + let source_pixbuf = GdkPixbuf.Pixbuf.new_from_resource_at_scale(res_path, + load_width, load_height, true); + let cropped_pixbuf = source_pixbuf; + if(dest_width < source_pixbuf.width || dest_height < source_pixbuf.height) + cropped_pixbuf = source_pixbuf.new_subpixbuf(0, 0, dest_width, dest_height); + return cropped_pixbuf; } // Convenience function to convert a resource URI to a resource path, for diff --git a/wikipedia/views/category_button.js b/wikipedia/views/category_button.js index 0761a58..7fde6a1 100644 --- a/wikipedia/views/category_button.js +++ b/wikipedia/views/category_button.js @@ -112,9 +112,10 @@ const CategoryButton = new Lang.Class({ set image_uri(value) { this._image_uri = value; if(this._image) { - let res_path = Utils.resourceUriToPath(value); let allocation = this.get_allocation(); - this._updateImage(res_path, allocation.width, allocation.height); + let new_pixbuf = Utils.load_pixbuf_cover(Utils.resourceUriToPath(this._image_uri), + allocation.width, allocation.height); + this._image.set_from_pixbuf(new_pixbuf); } }, @@ -132,29 +133,14 @@ const CategoryButton = new Lang.Class({ vfunc_size_allocate: function(allocation) { this.parent(allocation); - this._updateImage(Utils.resourceUriToPath(this._image_uri), + let new_pixbuf = Utils.load_pixbuf_cover(Utils.resourceUriToPath(this._image_uri), allocation.width, allocation.height); + this._image.set_from_pixbuf(new_pixbuf); }, // HANDLERS _onButtonPress: function(widget, event) { this.emit('clicked') - }, - - // PRIVATE - - _updateImage: function(res_path, width, height) { - let [source_width, source_height] = [width, height]; - if(width > height) - source_height = -1; - else - source_width = -1; - let source_pixbuf = GdkPixbuf.Pixbuf.new_from_resource_at_scale(res_path, - source_width, source_height, true); - let cropped_pixbuf = source_pixbuf; - if(width < source_pixbuf.width || height < source_pixbuf.height) - cropped_pixbuf = source_pixbuf.new_subpixbuf(0, 0, width, height); - this._image.set_from_pixbuf(cropped_pixbuf); } }); diff --git a/wikipedia/views/title_label_view.js b/wikipedia/views/title_label_view.js index 8ea821b..479747d 100644 --- a/wikipedia/views/title_label_view.js +++ b/wikipedia/views/title_label_view.js @@ -60,8 +60,9 @@ const TitleLabelView = new Lang.Class({ vfunc_size_allocate: function(allocation) { this.parent(allocation); if(this._image_uri !== "" && this._image_uri != null) { - this._updateImage(Utils.resourceUriToPath(this._image_uri), + let new_pixbuf = Utils.load_pixbuf_cover(Utils.resourceUriToPath(this._image_uri), allocation.width, allocation.height); + this._image.set_from_pixbuf(new_pixbuf); } }, @@ -86,31 +87,9 @@ const TitleLabelView = new Lang.Class({ if(this._image) { let res_path = Utils.resourceUriToPath(value); let allocation = this.get_allocation(); - this._updateImage(res_path, allocation.width, allocation.height); + let new_pixbuf = Utils.load_pixbuf_cover(Utils.resourceUriToPath(this._image_uri), + allocation.width, allocation.height); + this._image.set_from_pixbuf(new_pixbuf); } - }, - - // PRIVATE - - _updateImage: function(res_path, dest_width, dest_height) { - let [source_width, source_height] = [dest_width, dest_height]; - // TODO: We need to get the size of the source image, so right now we - // are loading the image twice, once to get the size, and the again at - // the proper size. We should eventually use a GdkPixbuf.Loader and - // connect to the size-prepared signal, as described in the - // documentation - let temp_pixbuf = GdkPixbuf.Pixbuf.new_from_resource(res_path); - let source_aspect = temp_pixbuf.width / temp_pixbuf.height; - let dest_aspect = dest_width / dest_height; - if(dest_aspect > source_aspect) - source_height = -1; - else - source_width = -1; - let source_pixbuf = GdkPixbuf.Pixbuf.new_from_resource_at_scale(res_path, - source_width, source_height, true); - let cropped_pixbuf = source_pixbuf; - if(dest_width < source_pixbuf.width || dest_height < source_pixbuf.height) - cropped_pixbuf = source_pixbuf.new_subpixbuf(0, 0, dest_width, dest_height); - this._image.set_from_pixbuf(cropped_pixbuf); } }); |