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
// thumb.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;
use crate::data::*;
use crate::send;
use gtk::glib;
use gtk::glib::{Object, ParamSpec, Properties, Value};
use gtk::prelude::*;
use gtk::subclass::prelude::*;
use std::cell::RefCell;
use std::rc::Rc;
use tokio::sync::mpsc::Sender;
gtk::glib::wrapper! {
pub struct ThumbObject(ObjectSubclass<imp::ThumbObject>);
}
impl ThumbObject {
pub fn new(texture: Option<gtk::gdk::Texture>, post: Box<dyn Post>, viewed: bool) -> Self {
let self_: Self = gtk::glib::Object::new();
let image = match texture.as_ref() {
Some(t) => gtk::Image::from_paintable(Some(t)),
None => gtk::Image::from_icon_name("missing-symbolic"),
};
image.set_pixel_size(crate::DEFAULT_THUMB_SIZE_UI);
let s = imp::ThumbObject::from_obj(&self_);
{
let mut data = s.data.borrow_mut();
data.image = Some(image);
data.post = Some(post);
data.texture = texture;
}
*s.viewed.borrow_mut() = viewed;
self_
}
pub fn object_data(&self) -> Rc<RefCell<ThumbData>> {
let self_ = imp::ThumbObject::from_obj(self);
self_.data.clone()
}
}
#[derive(Default)]
pub struct ThumbData {
pub image: Option<gtk::Image>,
pub post: Option<Box<dyn Post>>,
pub texture: Option<gtk::gdk::Texture>,
}
mod imp {
use super::*;
// Object holding the state
#[derive(Default, Properties)]
#[properties(wrapper_type = super::ThumbObject)]
pub struct ThumbObject {
pub data: Rc<RefCell<ThumbData>>,
#[property(get, set)]
pub viewed: Rc<RefCell<bool>>,
}
// The central trait for subclassing a GObject
#[glib::object_subclass]
impl ObjectSubclass for ThumbObject {
const NAME: &'static str = "ThumbObject";
type Type = super::ThumbObject;
fn new() -> Self {
Self {
data: Rc::new(RefCell::new(ThumbData::default())),
viewed: Rc::new(RefCell::new(false)),
}
}
}
impl ObjectImpl for ThumbObject {
fn properties() -> &'static [ParamSpec] {
Self::derived_properties()
}
fn set_property(&self, id: usize, value: &Value, pspec: &ParamSpec) {
self.derived_set_property(id, value, pspec)
}
fn property(&self, id: usize, pspec: &ParamSpec) -> Value {
self.derived_property(id, pspec)
}
}
}
pub fn init_thumb(
thumb_object: &ThumbObject,
overlay: >k::Overlay,
sender: &Sender<Action>,
img: >k::Image,
post: Box<dyn Post>,
thumb_size: u32,
) {
// let img = thumb_from_binary(bytes, &post);
// TODO why is this called so often?
let button = gtk::Button::new();
let download_button = gtk::Button::new();
let download_button_img = gtk::Image::from_icon_name("document-save-symbolic");
download_button.add_css_class("flat");
download_button.set_child(Some(&download_button_img));
download_button.set_halign(gtk::Align::End);
download_button.set_valign(gtk::Align::Start);
let sender2 = sender.clone();
let download_post = post.clone_post();
download_button.connect_clicked(move |_| {
let cmd = Action::DownloadImage {
post: download_post.clone_post(),
};
send!(sender2, cmd);
});
let old_overlay = gtk::Box::new(gtk::Orientation::Vertical, 0);
old_overlay.add_css_class("old");
old_overlay.set_can_target(false);
old_overlay.set_visible(thumb_object.viewed());
overlay.add_overlay(&old_overlay);
thumb_object
.bind_property("viewed", &old_overlay, "visible")
.build();
thumb_object
.bind_property("viewed", &old_overlay, "visible")
.build();
// println!("BOUND FOR {}", thumb_object.object_data().borrow().post.as_ref().unwrap().id());
if !post.is_local() {
overlay.add_overlay(&download_button);
}
button.set_child(Some(img));
button.set_size_request(thumb_size as i32, thumb_size as i32);
button.set_hexpand(true);
button.set_has_frame(false);
button.set_tooltip_text(Some(&post.tags().join(" ")));
overlay.set_child(Some(&button));
let sender = sender.clone();
button.connect_clicked(move |_| {
let cmd = Action::OpenImage {
post: post.clone_post(),
};
send!(sender, cmd);
});
}
pub fn uninit_thumb(overlay: >k::Overlay) {
overlay.set_child(gtk::Widget::NONE);
let children = overlay.observe_children();
let cs: Vec<_> = children
.iter::<Object>()
.filter_map(|c| {
if let Ok(c) = c {
Some(c.downcast::<gtk::Widget>().unwrap().clone())
} else {
None
}
})
.collect();
for c in cs {
overlay.remove_overlay(&c);
}
}