2020-05-29 12:19:35 +00:00
|
|
|
using Gtk;
|
|
|
|
using Gdk;
|
|
|
|
|
|
|
|
public class Tootle.Widgets.Avatar : EventBox {
|
|
|
|
|
|
|
|
public string? url { get; set; }
|
|
|
|
public int size { get; set; default = 48; }
|
2020-06-29 21:43:45 +00:00
|
|
|
|
2020-05-29 12:19:35 +00:00
|
|
|
private Cache.Reference? cached;
|
|
|
|
|
|
|
|
construct {
|
|
|
|
get_style_context ().add_class ("avatar");
|
|
|
|
notify["url"].connect (on_url_updated);
|
|
|
|
notify["size"].connect (on_redraw);
|
2020-07-10 14:22:38 +00:00
|
|
|
// Screen.get_default ().monitors_changed.connect (on_redraw);
|
2020-05-29 12:19:35 +00:00
|
|
|
on_url_updated ();
|
|
|
|
}
|
|
|
|
|
|
|
|
public Avatar (int size = this.size) {
|
|
|
|
Object (size: size);
|
|
|
|
on_redraw ();
|
|
|
|
}
|
2020-06-29 21:43:45 +00:00
|
|
|
|
2020-05-29 12:19:35 +00:00
|
|
|
~Avatar () {
|
|
|
|
notify["url"].disconnect (on_url_updated);
|
2020-07-10 14:22:38 +00:00
|
|
|
// Screen.get_default ().monitors_changed.disconnect (on_redraw);
|
2020-05-29 12:19:35 +00:00
|
|
|
cache.unload (cached);
|
|
|
|
}
|
2020-06-29 21:43:45 +00:00
|
|
|
|
2020-05-29 12:19:35 +00:00
|
|
|
private void on_url_updated () {
|
2020-07-10 14:22:38 +00:00
|
|
|
if (cached != null)
|
|
|
|
cache.unload (cached);
|
2020-05-29 12:19:35 +00:00
|
|
|
cached = null;
|
|
|
|
on_redraw ();
|
|
|
|
cache.load (url, on_cache_result);
|
|
|
|
}
|
2020-06-29 21:43:45 +00:00
|
|
|
|
2020-05-29 12:19:35 +00:00
|
|
|
private void on_cache_result (Cache.Reference? result) {
|
|
|
|
cached = result;
|
|
|
|
on_redraw ();
|
|
|
|
}
|
2020-06-29 21:43:45 +00:00
|
|
|
|
2020-05-29 12:19:35 +00:00
|
|
|
public int get_scaled_size () {
|
2020-07-10 14:22:38 +00:00
|
|
|
return size; //return size * get_scale_factor ();
|
2020-06-29 21:43:45 +00:00
|
|
|
|
2020-05-29 12:19:35 +00:00
|
|
|
private void on_redraw () {
|
|
|
|
set_size_request (get_scaled_size (), get_scaled_size ());
|
|
|
|
queue_draw_area (0, 0, size, size);
|
|
|
|
}
|
2020-06-29 21:43:45 +00:00
|
|
|
|
2020-05-29 12:19:35 +00:00
|
|
|
public override bool draw (Cairo.Context ctx) {
|
|
|
|
var w = get_allocated_width ();
|
|
|
|
var h = get_allocated_height ();
|
|
|
|
var style = get_style_context ();
|
|
|
|
var border_radius = style.get_property (Gtk.STYLE_PROPERTY_BORDER_RADIUS, style.get_state ()).get_int ();
|
|
|
|
Pixbuf pixbuf;
|
|
|
|
|
|
|
|
Drawing.draw_rounded_rect (ctx, 0, 0, w, h, border_radius);
|
|
|
|
if (cached != null && !cached.loading) {
|
|
|
|
pixbuf = cached.data.scale_simple (get_scaled_size (), get_scaled_size (), InterpType.BILINEAR);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
pixbuf = IconTheme.get_default ()
|
2020-07-10 14:30:57 +00:00
|
|
|
.load_icon_for_scale ("avatar-default", get_scaled_size (), get_scale_factor (), IconLookupFlags.GENERIC_FALLBACK);
|
2020-05-29 12:19:35 +00:00
|
|
|
}
|
|
|
|
Gdk.cairo_set_source_pixbuf (ctx, pixbuf, 0, 0);
|
|
|
|
ctx.fill ();
|
|
|
|
|
|
|
|
return Gdk.EVENT_STOP;
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|