Merge lp:~tintou/granite/avatar-pixbuf-hidpi into lp:~elementary-pantheon/granite/granite

Proposed by Corentin Noël
Status: Merged
Approved by: Daniel Fore
Approved revision: 1030
Merged at revision: 1030
Proposed branch: lp:~tintou/granite/avatar-pixbuf-hidpi
Merge into: lp:~elementary-pantheon/granite/granite
Diff against target: 74 lines (+17/-9)
1 file modified
lib/Widgets/Avatar.vala (+17/-9)
To merge this branch: bzr merge lp:~tintou/granite/avatar-pixbuf-hidpi
Reviewer Review Type Date Requested Status
Daniel Fore Approve
Review via email: mp+316155@code.launchpad.net

Commit message

avatar: allow HiDPI Pixbufs (X times bigger raw images or icons specifically rendered for @X size)

Description of the change

This is the proper patch allowing HiDPI Pixbufs (X times bigger raw images or icons specifically rendered for @X size)

To post a comment you must log in.
1030. By Corentin Noël on 2017-02-01

Allow HiDPI pixbufs

Revision history for this message
Daniel Fore (danrabbit) wrote :

As far as I can tell this does as advertised and doesn't affect 1x

review: Approve

Preview Diff

[H/L] Next/Prev Comment, [J/K] Next/Prev File, [N/P] Next/Prev Hunk
=== modified file 'lib/Widgets/Avatar.vala'
--- lib/Widgets/Avatar.vala 2016-12-31 19:48:54 +0000
+++ lib/Widgets/Avatar.vala 2017-02-01 19:34:50 +0000
@@ -55,7 +55,8 @@
55 */55 */
56 public Avatar.from_file (string filepath, int icon_size) {56 public Avatar.from_file (string filepath, int icon_size) {
57 try {57 try {
58 pixbuf = new Gdk.Pixbuf.from_file_at_scale (filepath, icon_size, icon_size, true);58 var size = icon_size * get_style_context ().get_scale ();
59 pixbuf = new Gdk.Pixbuf.from_file_at_size (filepath, size, size);
59 } catch (Error e) {60 } catch (Error e) {
60 show_default (icon_size);61 show_default (icon_size);
61 }62 }
@@ -74,7 +75,8 @@
74 valign = Gtk.Align.CENTER;75 valign = Gtk.Align.CENTER;
75 halign = Gtk.Align.CENTER;76 halign = Gtk.Align.CENTER;
76 visible_window = false;77 visible_window = false;
77 get_style_context ().add_class (DEFAULT_STYLE);78 var style_context = get_style_context ();
79 style_context.add_class (DEFAULT_STYLE);
7880
79 notify["pixbuf"].connect (refresh_size_request);81 notify["pixbuf"].connect (refresh_size_request);
80 }82 }
@@ -85,7 +87,8 @@
8587
86 private void refresh_size_request () {88 private void refresh_size_request () {
87 if (pixbuf != null) {89 if (pixbuf != null) {
88 set_size_request (pixbuf.width + EXTRA_MARGIN * 2, pixbuf.height + EXTRA_MARGIN * 2);90 var scale_factor = get_style_context ().get_scale ();
91 set_size_request (pixbuf.width / scale_factor + EXTRA_MARGIN * 2, pixbuf.height / scale_factor + EXTRA_MARGIN * 2);
89 draw_theme_background = true;92 draw_theme_background = true;
90 } else {93 } else {
91 set_size_request (0, 0);94 set_size_request (0, 0);
@@ -102,7 +105,7 @@
102 public void show_default (int icon_size) {105 public void show_default (int icon_size) {
103 Gtk.IconTheme icon_theme = Gtk.IconTheme.get_default ();106 Gtk.IconTheme icon_theme = Gtk.IconTheme.get_default ();
104 try {107 try {
105 pixbuf = icon_theme.load_icon (DEFAULT_ICON, icon_size, 0);108 pixbuf = icon_theme.load_icon_for_scale (DEFAULT_ICON, icon_size, get_style_context ().get_scale (), 0);
106 } catch (Error e) {109 } catch (Error e) {
107 stderr.printf ("Error setting default avatar icon: %s ", e.message);110 stderr.printf ("Error setting default avatar icon: %s ", e.message);
108 }111 }
@@ -118,21 +121,26 @@
118 unowned Gtk.StyleContext style_context = get_style_context ();121 unowned Gtk.StyleContext style_context = get_style_context ();
119 var width = get_allocated_width () - EXTRA_MARGIN * 2;122 var width = get_allocated_width () - EXTRA_MARGIN * 2;
120 var height = get_allocated_height () - EXTRA_MARGIN * 2;123 var height = get_allocated_height () - EXTRA_MARGIN * 2;
124 var scale_factor = style_context.get_scale ();
121125
122 if (draw_theme_background) {126 if (draw_theme_background) {
123 var border_radius = style_context.get_property (Gtk.STYLE_PROPERTY_BORDER_RADIUS, Gtk.StateFlags.NORMAL).get_int ();127 var border_radius = style_context.get_property (Gtk.STYLE_PROPERTY_BORDER_RADIUS, style_context.get_state ()).get_int ();
124 var crop_radius = int.min (width / 2, border_radius * width / 100);128 var crop_radius = int.min (width / 2, border_radius * width / 100);
125129
126 Granite.Drawing.Utilities.cairo_rounded_rectangle (cr, EXTRA_MARGIN, EXTRA_MARGIN, width, height, crop_radius);130 Granite.Drawing.Utilities.cairo_rounded_rectangle (cr, EXTRA_MARGIN, EXTRA_MARGIN, width, height, crop_radius);
127 Gdk.cairo_set_source_pixbuf (cr, pixbuf, EXTRA_MARGIN, EXTRA_MARGIN);131 cr.save ();
132 cr.scale (1.0 / scale_factor, 1.0 / scale_factor);
133 Gdk.cairo_set_source_pixbuf (cr, pixbuf, EXTRA_MARGIN * scale_factor, EXTRA_MARGIN * scale_factor);
128 cr.fill_preserve ();134 cr.fill_preserve ();
135 cr.restore ();
129 style_context.render_background (cr, EXTRA_MARGIN, EXTRA_MARGIN, width, height);136 style_context.render_background (cr, EXTRA_MARGIN, EXTRA_MARGIN, width, height);
130 style_context.render_frame (cr, EXTRA_MARGIN, EXTRA_MARGIN, width, height);137 style_context.render_frame (cr, EXTRA_MARGIN, EXTRA_MARGIN, width, height);
131138
132 } else {139 } else {
133 Granite.Drawing.Utilities.cairo_rounded_rectangle (cr, EXTRA_MARGIN, EXTRA_MARGIN, width, height, 0);140 cr.save ();
134 Gdk.cairo_set_source_pixbuf (cr, pixbuf, EXTRA_MARGIN, EXTRA_MARGIN);141 cr.scale (1.0 / scale_factor, 1.0 / scale_factor);
135 cr.fill_preserve ();142 style_context.render_icon (cr, pixbuf, EXTRA_MARGIN, EXTRA_MARGIN);
143 cr.restore ();
136 }144 }
137145
138 return Gdk.EVENT_STOP;146 return Gdk.EVENT_STOP;

Subscribers

People subscribed via source and target branches