-
-
Notifications
You must be signed in to change notification settings - Fork 51
/
Image.vala
132 lines (107 loc) · 3.41 KB
/
Image.vala
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
public class Tuba.Widgets.Attachment.Image : Widgets.Attachment.Item {
public Gtk.Picture pic { get; private set; }
protected Gtk.Overlay media_overlay;
private bool _spoiler = false;
public bool spoiler {
get {
return _spoiler;
}
set {
_spoiler = value;
if (value) {
pic.add_css_class ("spoilered-attachment");
} else {
pic.remove_css_class ("spoilered-attachment");
}
if (media_icon != null) media_icon.visible = !value;
}
}
void update_pic_content_fit () {
pic.content_fit = settings.letterbox_media ? Gtk.ContentFit.CONTAIN : Gtk.ContentFit.COVER;
}
construct {
pic = new Gtk.Picture () {
hexpand = true,
vexpand = true,
can_shrink = true,
keep_aspect_ratio = true,
css_classes = {"attachment-picture"}
};
update_pic_content_fit ();
settings.notify["letterbox-media"].connect (update_pic_content_fit);
media_overlay = new Gtk.Overlay ();
media_overlay.child = pic;
button.child = media_overlay;
}
protected Gtk.Image? media_icon = null;
ulong pic_paintable_id = 0;
protected override void on_rebind () {
base.on_rebind ();
pic.alternative_text = entity == null ? null : entity.description;
if (pic_paintable_id != 0) {
pic.disconnect (pic_paintable_id);
pic_paintable_id = 0;
}
if (media_kind.is_video ()) {
media_icon = new Gtk.Image () {
valign = Gtk.Align.CENTER,
halign = Gtk.Align.CENTER
};
if (media_kind != Tuba.Attachment.MediaType.AUDIO) {
media_icon.css_classes = { "osd", "circular", "attachment-overlay-icon" };
media_icon.icon_name = "media-playback-start-symbolic";
} else {
pic_paintable_id = pic.notify["paintable"].connect (on_audio_paintable_notify);
media_icon.icon_name = "tuba-music-note-symbolic";
}
media_overlay.add_overlay (media_icon);
// Doesn't get applied sometimes when set above
media_icon.icon_size = Gtk.IconSize.LARGE;
}
Tuba.Helper.Image.request_paintable (entity.preview_url, entity.blurhash, on_cache_response);
copy_media_simple_action.set_enabled (media_kind.can_copy ());
}
private void on_audio_paintable_notify () {
if (media_icon == null) return;
// toggle icon size so it applies
media_icon.icon_size = Gtk.IconSize.NORMAL;
if (pic.paintable == null) {
media_icon.css_classes = {};
} else {
media_icon.css_classes = { "osd", "circular", "attachment-overlay-icon" };
}
media_icon.icon_size = Gtk.IconSize.LARGE;
}
protected override void copy_media () {
debug ("Begin copy-media action");
Host.download.begin (entity.url, (obj, res) => {
try {
string path = Host.download.end (res);
Gdk.Texture texture = Gdk.Texture.from_filename (path);
if (texture == null) return;
Gdk.Clipboard clipboard = Gdk.Display.get_default ().get_clipboard ();
clipboard.set_texture (texture);
app.toast (_("Copied image to clipboard"));
} catch (Error e) {
app.toast ("%s: %s".printf (_("Error"), e.message));
}
debug ("End copy-media action");
});
}
protected virtual void on_cache_response (Gdk.Paintable? data) {
pic.paintable = data;
}
public signal void spoiler_revealed ();
protected override void on_click () {
if (pic.has_css_class ("spoilered-attachment")) {
spoiler_revealed ();
return;
}
if (media_kind != Tuba.Attachment.MediaType.UNKNOWN) {
on_any_attachment_click (entity.url);
} else { // Fallback
base.on_click ();
}
}
public signal void on_any_attachment_click (string url) {}
}