tootle-linux-client/src/Widgets/Avatar.vala

86 lines
2.0 KiB
Vala
Raw Normal View History

2020-05-29 14:19:35 +02:00
using Gtk;
using Gdk;
2020-08-01 01:42:14 +02:00
public class Tootle.Widgets.Avatar : Button {
2020-05-29 14:19:35 +02:00
public string? url { get; set; }
public int size { get; set; default = 48; }
2020-06-29 23:43:45 +02:00
2020-08-01 01:42:14 +02:00
Cache.Reference? cached;
2020-05-29 14:19:35 +02:00
construct {
get_style_context ().add_class ("avatar");
notify["url"].connect (on_url_updated);
2020-08-01 01:42:14 +02:00
notify["size"].connect (on_size_changed);
2020-07-10 16:22:38 +02:00
// Screen.get_default ().monitors_changed.connect (on_redraw);
2020-05-29 14:19:35 +02:00
on_url_updated ();
}
public Avatar (int size = this.size) {
Object (size: size);
2020-08-01 01:42:14 +02:00
queue_draw ();
2020-05-29 14:19:35 +02:00
}
2020-06-29 23:43:45 +02:00
2020-05-29 14:19:35 +02:00
~Avatar () {
notify["url"].disconnect (on_url_updated);
2020-07-10 16:22:38 +02:00
// Screen.get_default ().monitors_changed.disconnect (on_redraw);
2020-05-29 14:19:35 +02:00
cache.unload (cached);
}
2020-06-29 23:43:45 +02:00
2020-08-01 01:42:14 +02:00
void on_url_updated () {
2020-07-10 16:22:38 +02:00
if (cached != null)
cache.unload (cached);
2020-08-01 01:42:14 +02:00
2020-05-29 14:19:35 +02:00
cached = null;
2020-08-01 01:42:14 +02:00
queue_draw ();
2020-05-29 14:19:35 +02:00
cache.load (url, on_cache_result);
}
2020-06-29 23:43:45 +02:00
2020-08-01 01:42:14 +02:00
void on_cache_result (Cache.Reference? result) {
2020-05-29 14:19:35 +02:00
cached = result;
2020-08-01 01:42:14 +02:00
queue_draw ();
}
void on_size_changed () {
set_size_request (get_scaled_size (), get_scaled_size ());
queue_draw ();
2020-05-29 14:19:35 +02:00
}
2020-06-29 23:43:45 +02:00
2020-05-29 14:19:35 +02:00
public int get_scaled_size () {
2020-07-10 16:22:38 +02:00
return size; //return size * get_scale_factor ();
2020-07-10 16:35:01 +02:00
}
2020-06-29 23:43:45 +02:00
2020-08-01 01:42:14 +02:00
public override void get_preferred_height (out int min_h, out int nat_h) {
min_h = nat_h = get_scaled_size ();
}
public override void get_preferred_width (out int min_w, out int nat_w) {
min_w = nat_w = get_scaled_size ();
2020-05-29 14:19:35 +02:00
}
2020-06-29 23:43:45 +02:00
2020-05-29 14:19:35 +02: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 {
2020-08-01 01:42:14 +02:00
pixbuf = IconTheme.get_default ().load_icon_for_scale (
"avatar-default",
get_scaled_size (),
get_scale_factor (),
IconLookupFlags.GENERIC_FALLBACK);
2020-05-29 14:19:35 +02:00
}
Gdk.cairo_set_source_pixbuf (ctx, pixbuf, 0, 0);
ctx.fill ();
return Gdk.EVENT_STOP;
}
}