8000 Render media in max 4:3 proportions box by IBBoard · Pull Request #605 · baedert/corebird · GitHub
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content

Render media in max 4:3 proportions box #605

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 7 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
56 changes: 38 additions & 18 deletions src/widgets/MediaButton.vala
Original file line number Diff line number Diff line change
Expand Up @@ -156,15 +156,15 @@ 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;
width = int.min (this._media.width, this.get_allocated_width ());
scale = this.get_allocated_width () / (double) this._media.width;

scale = double.min (double.min (scale_x, scale_y), 1.0);

width = (int)(this._media.width * scale);
height = (int)(this._media.height * scale);
if (scale > 1) {
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));
}
}

public override bool draw (Cairo.Context ct) {
Expand All @@ -185,11 +185,27 @@ private class MediaButton : Gtk.Widget {
ct.save ();
ct.rectangle (0, 0, widget_width, widget_height);
ct.scale (scale, scale);
ct.set_source_surface (media.surface, draw_x / scale, 0);
double draw_y = -(((media.height * scale) - draw_height) / 2);
ct.set_source_surface (media.surface, draw_x / scale, draw_y / scale);
ct.paint_with_alpha (this.media_alpha);
ct.restore ();
ct.new_path ();

/*
* If image got moved off the top, we cropped it. Indicate that.
* Currently trying a gradient overlay top and bottom
*/
if (draw_y < 0) {
Cairo.Pattern pattern = new Cairo.Pattern.linear (0.0, 0.0, 0, widget_height);
pattern.add_color_stop_rgba (0.01, 0.3, 0.3, 0.3, 1);
pattern.add_color_stop_rgba (0.1, 0.7, 0.7, 0.7, 0);
pattern.add_color_stop_rgba (0.9, 0.7, 0.7, 0.7, 0);
pattern.add_color_stop_rgba (0.99, 0.3, 0.3, 0.3, 1);
ct.rectangle (0, 0, widget_width, widget_height);
ct.set_source (pattern);
ct.fill ();
}

/* Draw play indicator */
if (_media.is_video ()) {
int x = (widget_width / 2) - (PLAY_ICON_SIZE / 2);
Expand Down Expand Up @@ -265,7 +281,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;
Expand All @@ -274,15 +290,19 @@ 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));
double scale = width / (double) media_width;

int height = 0;

if (restrict_height) {
minimum = int.min (media_height, MAX_HEIGHT);
natural = minimum;
height = int.min (media_height, MAX_HEIGHT);
} else if (scale > 1) {
height = int.min (media_height, (int) Math.floor ((width / 4.0) * 3));
} else {
minimum = height;
natural = height;
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,
Expand All @@ -299,8 +319,8 @@ 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 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;
}
Expand Down
0