From f4ef68182bf9d01c1b2e9ef309129cdf30fddd70 Mon Sep 17 00:00:00 2001 From: IBBoard Date: Sat, 30 Jan 2016 19:41:02 +0000 Subject: [PATCH 1/5] Initial scaling work Keep 4:3 proportions where we can. Currently doesn't: * crop image height * middle-align the cropping * work correctly with multiple images --- src/widgets/MediaButton.vala | 22 +++++++++++++--------- 1 file changed, 13 insertions(+), 9 deletions(-) diff --git a/src/widgets/MediaButton.vala b/src/widgets/MediaButton.vala index 32f9d7c65..33c2600b3 100644 --- a/src/widgets/MediaButton.vala +++ b/src/widgets/MediaButton.vala @@ -99,15 +99,19 @@ private class MediaButton : Gtk.Widget { return; } - width = this.get_allocated_width (); - height = this.get_allocated_height (); - double scale_x = (double)width / this._media.width; - double scale_y = (double)height / this._media.height; - - scale = double.min (double.min (scale_x, scale_y), 1.0); - - width = (int)(this._media.width * scale); - height = (int)(this._media.height * scale); + if (this._media.width > this.get_allocated_width ()) { + width = this.get_allocated_width (); + int maxHeight = (int) Math.floor((width / 4.0) * 3); + height = int.min(this._media.height, maxHeight); + scale = this.get_allocated_width () / (double) this._media.width; + stderr.printf("Scale %s: %d x %d => %d x %d (%f)\n", this._media.url,this._media.width,this._media.height,width,height, scale); + } else { + width = this._media.width; + int maxHeight = (int)Math.floor((this._media.width / 4.0) * 3); + height = int.min(this._media.height, maxHeight); + scale = 1; + stderr.printf("No scale %s: %d x %d => %d x %d (%f)\n", this._media.url,this._media.width,this._media.height,width,height, scale); + } } public override bool draw (Cairo.Context ct) { From 34875c31ec30a3b342b40d4a4219e6e5da128f68 Mon Sep 17 00:00:00 2001 From: IBBoard Date: Sun, 31 Jan 2016 21:01:01 +0000 Subject: [PATCH 2/5] Fix height calculation (a bit) Images that are greater than app width and portrait are cropped correctly Images narrower than app width but portrait aren't cropped correctly Images that are small enough are okay Images that are wider than app width but not 4:3 tall have spacing --- src/widgets/MediaButton.vala | 36 ++++++++++++++++++++++++------------ 1 file changed, 24 insertions(+), 12 deletions(-) diff --git a/src/widgets/MediaButton.vala b/src/widgets/MediaButton.vala index 33c2600b3..c2f1a67b1 100644 --- a/src/widgets/MediaButton.vala +++ b/src/widgets/MediaButton.vala @@ -99,18 +99,17 @@ private class MediaButton : Gtk.Widget { return; } + int maxHeight = (int) Math.floor((this.get_allocated_width () / 4.0) * 3); + height = int.min(this._media.height, maxHeight); + if (this._media.width > this.get_allocated_width ()) { width = this.get_allocated_width (); - int maxHeight = (int) Math.floor((width / 4.0) * 3); - height = int.min(this._media.height, maxHeight); - scale = this.get_allocated_width () / (double) this._media.width; - stderr.printf("Scale %s: %d x %d => %d x %d (%f)\n", this._media.url,this._media.width,this._media.height,width,height, scale); + scale = width / (double) this._media.width; + stderr.printf("Scale %s: %d x %d => %d x %d (%f) in %d x %d\n", this._media.url,this._media.width,this._media.height,width,height, scale, this.get_allocated_width (), this.get_allocated_height ()); } else { width = this._media.width; - int maxHeight = (int)Math.floor((this._media.width / 4.0) * 3); - height = int.min(this._media.height, maxHeight); scale = 1; - stderr.printf("No scale %s: %d x %d => %d x %d (%f)\n", this._media.url,this._media.width,this._media.height,width,height, scale); + stderr.printf("No scale %s: %d x %d => %d x %d (%f) in %d x %d\n", this._media.url,this._media.width,this._media.height,width,height, scale, this.get_allocated_width (), this.get_allocated_height ()); } } @@ -193,7 +192,7 @@ private class MediaButton : Gtk.Widget { out int natural) { int media_width; int media_height; - + if (this._media == null || this._media.width == -1 || this._media.height == -1) { media_width = MIN_WIDTH; media_height = MAX_HEIGHT; @@ -202,9 +201,15 @@ private class MediaButton : Gtk.Widget { media_height = this._media.height; } - double width_ratio = (double)width / (double) media_width; - int height = int.min (media_height, (int)(media_height * width_ratio)); + int maxHeight = (int) Math.floor((int.min(media_width, width) / 4.0) * 3); + int height = int.min(media_height, maxHeight); minimum = natural = height; + if (this._media != null) { + //FIXME: The calls are height_for_width, width_for_height, height_for_width + //We need to make sure they're consistent, as currently width_for_height returns + //too wide a width! + stderr.printf("get_preferred_height_for_width(%d) for %s (%d x %d): %d\n", width, this._media.url, this._media.width, this._media.height, height); + } } public override void get_preferred_width_for_height (int height, @@ -221,9 +226,14 @@ private class MediaButton : Gtk.Widget { media_height = this._media.height; } - double height_ratio = (double)height / (double)media_height; - int width = int.min (media_width, (int)(media_width * height_ratio)); + //double height_ratio = (double)height / (double)media_height; + //int width = int.min (media_width, (int)(media_width * height_ratio)); + int maxWidth = (height / 3) * 4; + int width = int.min(media_height, maxWidth); minimum = natural = width; + if (this._media != null) { + stderr.printf("get_preferred_width_for_height(%d) for %s (%d x %d): %d\n", height, this._media.url, this._media.width, this._media.height, width); + } } public override void get_preferred_height (out int minimum, @@ -237,6 +247,7 @@ private class MediaButton : Gtk.Widget { minimum = int.min (media_height, MIN_HEIGHT); natural = media_height; + stderr.printf("get_preferred_height() for %s (%d x %d): %d or %d\n", this._media.url, this._media.width, this._media.height, minimum, natural); } public override void get_preferred_width (out int minimum, @@ -250,6 +261,7 @@ private class MediaButton : Gtk.Widget { minimum = int.min (media_width, MIN_WIDTH); natural = media_width; + stderr.printf("get_preferred_width() for %s (%d x %d): %d or %d\n", this._media.url, this._media.width, this._media.height, minimum, natural); } public override void realize () { From 3c757b1f38123d05b87838f3e464770c76538383 Mon Sep 17 00:00:00 2001 From: IBBoard Date: Wed, 3 Feb 2016 20:12:11 +0000 Subject: [PATCH 3/5] Fix calculations for 4:3 ratio max, including putting lines in the right place on hover --- src/widgets/MediaButton.vala | 23 ++++++++++++----------- 1 file changed, 12 insertions(+), 11 deletions(-) diff --git a/src/widgets/MediaButton.vala b/src/widgets/MediaButton.vala index c2f1a67b1..9f527101b 100644 --- a/src/widgets/MediaButton.vala +++ b/src/widgets/MediaButton.vala @@ -99,8 +99,10 @@ private class MediaButton : Gtk.Widget { return; } - int maxHeight = (int) Math.floor((this.get_allocated_width () / 4.0) * 3); - height = int.min(this._media.height, maxHeight); + int maxHeightAllocated = (int) Math.floor((this.get_allocated_width () / 4.0) * 3); + int maxHeightFromWidth = (int) Math.floor((this._media.width / 4.0) * 3); + height = int.min(this._media.height, maxHeightAllocated); + height = int.min (height, maxHeightFromWidth); if (this._media.width > this.get_allocated_width ()) { width = this.get_allocated_width (); @@ -202,13 +204,12 @@ private class MediaButton : Gtk.Widget { } int maxHeight = (int) Math.floor((int.min(media_width, width) / 4.0) * 3); - int height = int.min(media_height, maxHeight); + double width_scale = width / (double) media_width; + int scaled_height = (int) Math.floor(media_height * width_scale); + int height = int.min(int.min(media_height, maxHeight), scaled_height); minimum = natural = height; if (this._media != null) { - //FIXME: The calls are height_for_width, width_for_height, height_for_width - //We need to make sure they're consistent, as currently width_for_height returns - //too wide a width! - stderr.printf("get_preferred_height_for_width(%d) for %s (%d x %d): %d\n", width, this._media.url, this._media.width, this._media.height, height); + stderr.printf("get_preferred_height_for_width(%d) for %s (%d x %d): %d (max %d)\n", width, this._media.url, this._media.width, this._media.height, height, maxHeight); } } @@ -228,21 +229,21 @@ private class MediaButton : Gtk.Widget { //double height_ratio = (double)height / (double)media_height; //int width = int.min (media_width, (int)(media_width * height_ratio)); - int maxWidth = (height / 3) * 4; + int maxWidth = (int) Math.floor((height / 3.0) * 4); int width = int.min(media_height, maxWidth); minimum = natural = width; if (this._media != null) { - stderr.printf("get_preferred_width_for_height(%d) for %s (%d x %d): %d\n", height, this._media.url, this._media.width, this._media.height, width); + stderr.printf("get_preferred_width_for_height(%d) for %s (%d x %d): %d (max %d)\n", height, this._media.url, this._media.width, this._media.height, width, maxWidth); } } public override void get_preferred_height (out int minimum, out int natural) { int media_height; - if (this._media == null || this._media.width == -1) { + if (this._media == null || this._media.height == -1) { media_height = 1; } else { - media_height = this._media.width; + media_height = this._media.height; } minimum = int.min (media_height, MIN_HEIGHT); From 6d50a3a833112fae3c34f9970a8acb69f4398d3d Mon Sep 17 00:00:00 2001 From: IBBoard Date: Sun, 7 Feb 2016 21:06:11 +0000 Subject: [PATCH 4/5] Simplify scaling and fix vertical alignment We now show the middle of the image Height is max 4:3 proportions of the widget --- src/widgets/MediaButton.vala | 47 +++++++++++++++--------------------- 1 file changed, 19 insertions(+), 28 deletions(-) diff --git a/src/widgets/MediaButton.vala b/src/widgets/MediaButton.vala index 9f527101b..956b716db 100644 --- a/src/widgets/MediaButton.vala +++ b/src/widgets/MediaButton.vala @@ -99,19 +99,14 @@ private class MediaButton : Gtk.Widget { return; } - int maxHeightAllocated = (int) Math.floor((this.get_allocated_width () / 4.0) * 3); - int maxHeightFromWidth = (int) Math.floor((this._media.width / 4.0) * 3); - height = int.min(this._media.height, maxHeightAllocated); - height = int.min (height, maxHeightFromWidth); - - if (this._media.width > this.get_allocated_width ()) { - width = this.get_allocated_width (); - scale = width / (double) this._media.width; - stderr.printf("Scale %s: %d x %d => %d x %d (%f) in %d x %d\n", this._media.url,this._media.width,this._media.height,width,height, scale, this.get_allocated_width (), this.get_allocated_height ()); - } else { - width = this._media.width; + width = int.min (this._media.width, this.get_allocated_width ()); + scale = this.get_allocated_width () / (double) this._media.width; + + if (scale > 1) { + height = int.min (this._media.height, (int) Math.floor((this.get_allocated_width () / 4.0) * 3)); scale = 1; - stderr.printf("No scale %s: %d x %d => %d x %d (%f) in %d x %d\n", this._media.url,this._media.width,this._media.height,width,height, scale, this.get_allocated_width (), this.get_allocated_height ()); + } else { + height = (int) Math.floor (double.min (this._media.height * scale, (this._media.width * scale / 4.0) * 3)); } } @@ -133,7 +128,7 @@ private class MediaButton : Gtk.Widget { int draw_x = (widget_width / 2) - (draw_width / 2); ct.scale (scale, scale); - ct.set_source_surface (media.surface, draw_x / scale, 0); + ct.set_source_surface (media.surface, draw_x / scale, -(((media.height * scale) - draw_height) / 2) / scale); ct.fill (); ct.restore (); @@ -203,14 +198,17 @@ private class MediaButton : Gtk.Widget { media_height = this._media.height; } - int maxHeight = (int) Math.floor((int.min(media_width, width) / 4.0) * 3); - double width_scale = width / (double) media_width; - int scaled_height = (int) Math.floor(media_height * width_scale); - int height = int.min(int.min(media_height, maxHeight), scaled_height); - minimum = natural = height; - if (this._media != null) { - stderr.printf("get_preferred_height_for_width(%d) for %s (%d x %d): %d (max %d)\n", width, this._media.url, this._media.width, this._media.height, height, maxHeight); + double scale = width / (double) media_width; + + int height = 0; + + if (scale > 1) { + height = int.min (media_height, (int) Math.floor((width / 4.0) * 3)); + } else { + height = (int) Math.floor (double.min (media_height * scale, (media_width * scale / 4.0) * 3)); } + + minimum = natural = height; } public override void get_preferred_width_for_height (int height, @@ -227,14 +225,9 @@ private class MediaButton : Gtk.Widget { media_height = this._media.height; } - //double height_ratio = (double)height / (double)media_height; - //int width = int.min (media_width, (int)(media_width * height_ratio)); int maxWidth = (int) Math.floor((height / 3.0) * 4); - int width = int.min(media_height, maxWidth); + int width = int.min(media_width, maxWidth); minimum = natural = width; - if (this._media != null) { - stderr.printf("get_preferred_width_for_height(%d) for %s (%d x %d): %d (max %d)\n", height, this._media.url, this._media.width, this._media.height, width, maxWidth); - } } public override void get_preferred_height (out int minimum, @@ -248,7 +241,6 @@ private class MediaButton : Gtk.Widget { minimum = int.min (media_height, MIN_HEIGHT); natural = media_height; - stderr.printf("get_preferred_height() for %s (%d x %d): %d or %d\n", this._media.url, this._media.width, this._media.height, minimum, natural); } public override void get_preferred_width (out int minimum, @@ -262,7 +254,6 @@ private class MediaButton : Gtk.Widget { minimum = int.min (media_width, MIN_WIDTH); natural = media_width; - stderr.printf("get_preferred_width() for %s (%d x %d): %d or %d\n", this._media.url, this._media.width, this._media.height, minimum, natural); } public override void realize () { From 3a55a9f232e9a81e9d65a419ecfc03075d42a869 Mon Sep 17 00:00:00 2001 From: IBBoard Date: Mon, 8 Feb 2016 20:01:54 +0000 Subject: [PATCH 5/5] Tidy up code before pull request * Fix code formatting * Improve readability by extracting variable * Match change in 2b7d76 --- src/widgets/MediaButton.vala | 14 ++++++++------ 1 file changed, 8 insertions(+), 6 deletions(-) diff --git a/src/widgets/MediaButton.vala b/src/widgets/MediaButton.vala index 956b716db..58649af8c 100644 --- a/src/widgets/MediaButton.vala +++ b/src/widgets/MediaButton.vala @@ -103,7 +103,7 @@ private class MediaButton : Gtk.Widget { scale = this.get_allocated_width () / (double) this._media.width; if (scale > 1) { - height = int.min (this._media.height, (int) Math.floor((this.get_allocated_width () / 4.0) * 3)); + height = int.min (this._media.height, (int) Math.floor ((this.get_allocated_width () / 4.0) * 3)); scale = 1; } else { height = (int) Math.floor (double.min (this._media.height * scale, (this._media.width * scale / 4.0) * 3)); @@ -128,7 +128,8 @@ private class MediaButton : Gtk.Widget { int draw_x = (widget_width / 2) - (draw_width / 2); ct.scale (scale, scale); - ct.set_source_surface (media.surface, draw_x / scale, -(((media.height * scale) - draw_height) / 2) / scale); + double draw_y = -(((media.height * scale) - draw_height) / 2); + ct.set_source_surface (media.surface, draw_x / scale, draw_y / scale); ct.fill (); ct.restore (); @@ -203,7 +204,7 @@ private class MediaButton : Gtk.Widget { int height = 0; if (scale > 1) { - height = int.min (media_height, (int) Math.floor((width / 4.0) * 3)); + height = int.min (media_height, (int) Math.floor ((width / 4.0) * 3)); } else { height = (int) Math.floor (double.min (media_height * scale, (media_width * scale / 4.0) * 3)); } @@ -225,9 +226,10 @@ private class MediaButton : Gtk.Widget { media_height = this._media.height; } - int maxWidth = (int) Math.floor((height / 3.0) * 4); - int width = int.min(media_width, maxWidth); - minimum = natural = width; + int max_width = (int) Math.floor ((height / 3.0) * 4); + int width = int.min (media_width, max_width); + minimum = int.min (media_width, MIN_WIDTH); + natural = width; } public override void get_preferred_height (out int minimum,