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
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
// image_page.rs
//
// Copyright 2020-2024 nee <nee-git@hidamari.blue>
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program. If not, see <http://www.gnu.org/licenses/>.
//
// SPDX-License-Identifier: GPL-3.0-or-later
use crate::booru::{Post, TagType};
use crate::data::*;
use crate::send;
use adw::prelude::*;
use gtk::glib;
use gtk::glib::Properties;
use gtk::glib::clone;
use gtk::subclass::prelude::*;
use std::cell::Cell;
use std::cell::RefCell;
use tokio::sync::mpsc::Sender;
mod imp {
use super::*;
#[derive(Debug, Default, gtk::CompositeTemplate, Properties)]
#[properties(wrapper_type = super::ImagePage)]
#[template(resource = "/blue/hidamari/boorus/ui/image_page.ui")]
pub struct ImagePage {
#[template_child]
pub split: TemplateChild<adw::OverlaySplitView>,
#[template_child]
pub window: TemplateChild<gtk::ScrolledWindow>,
#[template_child]
pub download: TemplateChild<gtk::Button>,
#[template_child]
pub tags_window: TemplateChild<adw::Bin>,
#[template_child]
pub create_date: TemplateChild<gtk::Label>,
#[template_child]
pub web_button: TemplateChild<gtk::Button>,
#[property(get, set)]
pub narrow: Cell<bool>,
pub video: RefCell<Option<gtk::Video>>,
}
#[glib::object_subclass]
impl ObjectSubclass for ImagePage {
const NAME: &'static str = "ImagePage";
type Type = super::ImagePage;
type ParentType = gtk::Box;
fn class_init(klass: &mut Self::Class) {
klass.bind_template();
}
// You must call `Widget`'s `init_template()` within `instance_init()`.
fn instance_init(obj: &glib::subclass::InitializingObject<Self>) {
obj.init_template();
}
}
#[glib::derived_properties]
impl ObjectImpl for ImagePage {
fn constructed(&self) {
self.parent_constructed();
}
}
impl WidgetImpl for ImagePage {}
impl BoxImpl for ImagePage {}
}
glib::wrapper! {
pub struct ImagePage(ObjectSubclass<imp::ImagePage>)
@extends gtk::Widget, gtk::Box,
@implements gtk::Accessible, gtk::Buildable, gtk::ConstraintTarget;
}
impl ImagePage {
pub fn new(state: &State, post: &dyn Post) -> ImagePage {
let widget: Self = glib::Object::new();
let tags_window = make_tags_sidebar(state, post);
widget.imp().tags_window.set_child(Some(&tags_window));
widget
.bind_property("narrow", &widget.imp().split.get(), "collapsed")
.flags(glib::BindingFlags::SYNC_CREATE)
.build();
let post = post.clone_post();
widget.imp().web_button.connect_clicked(move |_| {
post.open_web();
});
widget
}
pub fn got_image(&self, sender: &Sender<Action>, post: &dyn Post, bytes: bytes::Bytes) {
// TODO support gif
if let Ok(texture) = texture_from_bytes(&bytes) {
let picture = gtk::Picture::for_paintable(&texture);
self.imp().window.set_child(Some(&picture));
} else if let Ok(video) = make_video(&bytes) {
self.imp().window.set_child(Some(&video));
self.imp().video.replace(Some(video));
} else {
self.error_image();
}
self.setup_download_button(sender.clone(), post.clone_post(), bytes);
}
pub fn clear_video(&self) {
self.stop_playback();
self.imp().video.replace(None);
self.imp().window.set_child(gtk::Widget::NONE);
}
fn stop_playback(&self) {
let video = self.imp().video.borrow();
if let Some(video) = video.as_ref()
&& let Some(stream) = video.media_stream()
{
stream.set_playing(false);
}
}
fn setup_download_button(
&self,
sender: Sender<Action>,
post: std::boxed::Box<dyn Post>,
bytes: bytes::Bytes,
) {
if post.is_local() {
self.imp().download.set_visible(false);
return;
}
let filename = crate::download::download_read_path(post.filename());
if !filename.exists() {
let download_button_img = gtk::Image::from_icon_name("document-save-symbolic");
self.imp().download.set_child(Some(&download_button_img));
let download_button = self.imp().download.clone();
// download_button.add_css_class("suggested-action");
self.imp().download.connect_clicked(move |_| {
// crate::download::save_bytes(&filename, &bytes);
// TODO FIXME STOP CLONING THE BYTES THIS IS BIG, maybe arc them???
send!(
sender,
Action::DownloadDone {
bytes: bytes.clone(),
post: post.clone_post()
}
);
download_button.set_icon_name("emblem-ok-symbolic");
// download_button.remove_css_class("suggested-action");
download_button.add_css_class("success");
});
} else {
// already downloaded
self.imp().download.set_icon_name("emblem-ok-symbolic");
self.imp().download.add_css_class("success");
}
}
pub fn error_image(&self) {
let status_page = adw::StatusPage::builder()
.icon_name("image-missing-symbolic")
.title("Failed to open image/video. 😿")
.build();
self.imp().window.set_child(Some(&status_page));
}
}
fn make_video(bytes: &bytes::Bytes) -> Result<gtk::Video, Box<dyn std::error::Error>> {
// memory streaming is currently not supported
// let stream = gtk::gio::MemoryInputStream::from_bytes(>k::glib::Bytes::from(bytes));
// let stream = gtk::gio::DataInputStream::new(&stream);
// let file = gtk::MediaFile::for_input_stream(&stream);
// tmp file workaround
use std::fs::File;
use std::io::Write;
let mut path = glib::tmp_dir();
path.push("boorus-last-video");
let mut file = File::create(&path)?;
file.write_all(bytes)?;
file.sync_all()?;
let video = gtk::Video::builder()
.autoplay(true)
.loop_(true)
.file(>k::gio::File::for_path(path))
.graphics_offload(gtk::GraphicsOffloadEnabled::Enabled)
.build();
Ok(video)
}
fn texture_from_bytes(
bytes: &bytes::Bytes,
) -> Result<gtk::gdk::Texture, Box<dyn std::error::Error>> {
let strm: gtk::gio::MemoryInputStream =
gtk::gio::MemoryInputStream::from_bytes(>k::glib::Bytes::from(bytes));
let cancellable: Option<>k::gio::Cancellable> = None;
let pixbuf = gtk::gdk_pixbuf::Pixbuf::from_stream(&strm, cancellable)?;
// let pixbuf = gtk::gdk_pixbuf::Pixbuf::from_stream_at_scale(&strm, THUMB_SIZE, THUMB_SIZE, true, cancellable)?;
let texture = gtk::gdk::Texture::for_pixbuf(&pixbuf);
Ok(texture)
}
fn make_tag_button(sender: &Sender<Action>, t: &str, tag_type: TagType) -> gtk::Box {
let b = gtk::Box::new(gtk::Orientation::Horizontal, 3);
let tag1 = t.to_owned();
let add_to_search = gtk::Button::new();
let add_to_search_img = gtk::Image::from_icon_name("list-add-symbolic");
add_to_search.set_child(Some(&add_to_search_img));
add_to_search.add_css_class("flat");
add_to_search.connect_clicked(clone!(
#[strong]
sender,
move |_| {
let cmd = Action::AddToSearch {
tag: tag1.to_string(),
};
send!(sender, cmd);
}
));
let tag_label = gtk::Label::new(Some(crate::booru::display_tag(t).as_str()));
tag_label.set_halign(gtk::Align::Start);
tag_label.set_wrap(true);
tag_label.set_wrap_mode(gtk::pango::WrapMode::WordChar);
tag_label.set_max_width_chars(30);
let replace_search = gtk::Button::new();
replace_search.add_css_class("flat");
replace_search.set_hexpand(true);
replace_search.set_child(Some(&tag_label));
replace_search.set_action_name(Some("app.open-browse-search"));
replace_search.set_action_target_value(Some(&glib::Variant::from(&t)));
init_tag_context_menu(replace_search.clone(), t, tag_type);
b.append(&add_to_search);
b.append(&replace_search);
// b.append(&fav);
b
}
fn init_tag_context_menu(button: gtk::Button, t: &str, tag_type: TagType) {
let tag: String = t.to_string();
let on_rightclick = clone!(
#[weak]
button,
move |(x, y)| {
let menu = gtk::gio::Menu::new();
let wiki_item;
if tag_type == TagType::Artist {
wiki_item = gtk::gio::MenuItem::new(Some("View Artist Profile"), None);
wiki_item.set_action_and_target_value(
Some("app.open-artist-page"),
Some(&glib::Variant::from(&tag)),
);
} else {
wiki_item = gtk::gio::MenuItem::new(Some("View Wiki"), None);
wiki_item.set_action_and_target_value(
Some("app.open-wiki-page"),
Some(&glib::Variant::from(&tag)),
);
}
menu.append_item(&wiki_item);
let popover = gtk::PopoverMenu::from_model(Some(&menu));
popover.set_parent(&button);
popover.set_pointing_to(Some(>k::gdk::Rectangle::new(x as i32, y as i32, 1, 1)));
popover.set_halign(gtk::Align::Start);
popover.set_has_arrow(false);
popover.popup();
}
);
let on_long_press = on_rightclick.clone();
let long_press = gtk::GestureLongPress::new();
long_press.connect_pressed(move |_, x, y| {
on_long_press((x, y));
});
let right_click = gtk::GestureClick::builder()
.button(gtk::gdk::BUTTON_SECONDARY)
.build();
right_click.connect_pressed(move |_, _, x, y| {
on_rightclick((x, y));
});
button.add_controller(long_press.clone());
button.add_controller(right_click);
}
fn make_headlined_tag_list(
sender: &Sender<Action>,
b: >k::Box,
tags: Vec<&str>,
tag_type: TagType,
) {
if tags.is_empty() {
return;
}
let label = gtk::Label::new(Some(tag_type.css_class()));
label.add_css_class("tag_headline");
label.add_css_class(tag_type.css_class());
b.append(&label);
for t in tags {
let button = make_tag_button(sender, t, tag_type);
b.append(&button);
}
}
fn make_tags_sidebar(state: &State, post: &dyn Post) -> gtk::ScrolledWindow {
let b = gtk::Box::new(gtk::Orientation::Vertical, 3);
let sender = &state.sender;
make_headlined_tag_list(sender, &b, post.tags_artist(&state.tagdb), TagType::Artist);
make_headlined_tag_list(
sender,
&b,
post.tags_character(&state.tagdb),
TagType::Character,
);
make_headlined_tag_list(
sender,
&b,
post.tags_copyright(&state.tagdb),
TagType::Copyright,
);
make_headlined_tag_list(
sender,
&b,
post.tags_unknown(&state.tagdb),
TagType::Unknown,
);
make_headlined_tag_list(
sender,
&b,
post.tags_general(&state.tagdb),
TagType::General,
);
make_headlined_tag_list(sender, &b, post.tags_meta(&state.tagdb), TagType::Meta);
// make_headlined_tag_list(&b, post.tags_deprecated(&state.tagdb), TagType::Deprecated);
let tag_scroll = gtk::ScrolledWindow::new();
tag_scroll.set_policy(gtk::PolicyType::Never, gtk::PolicyType::Automatic);
tag_scroll.set_child(Some(&b));
tag_scroll.set_hexpand(false);
tag_scroll
}