1use std::cell::RefCell;
6use std::path::PathBuf;
7use std::rc::Rc;
8use std::sync::atomic::{AtomicU64, Ordering};
9
10use crate::animation::{self, Transition};
11use crate::tree::{Change, ImageNode, NodeId, NodeKind, Tree, borrow, replace};
12use crate::widgets::{Fit, IconProps, ImageFilters, ImageProps};
13use crate::window::Window;
14
15static NEXT_IMAGE: AtomicU64 = AtomicU64::new(0);
18
19const MAX_DROPPED: usize = 4096;
22
23thread_local! {
24 static DROPPED: RefCell<Vec<u64>> = const { RefCell::new(Vec::new()) };
26}
27
28pub fn dropped_images() -> Vec<u64> {
32 DROPPED.with(|dropped| std::mem::take(&mut *dropped.borrow_mut()))
33}
34
35impl Drop for ImageNode {
36 fn drop(&mut self) {
37 let _ = DROPPED.try_with(|dropped| {
39 let mut dropped = dropped.borrow_mut();
40 if dropped.len() < MAX_DROPPED {
41 dropped.push(self.id);
42 }
43 });
44 }
45}
46
47impl ImageNode {
48 pub(crate) fn new() -> ImageNode {
49 ImageNode {
50 id: NEXT_IMAGE.fetch_add(1, Ordering::Relaxed),
51 source: None,
52 width: crate::Image::DEFAULT_SIZE,
53 height: crate::Image::DEFAULT_SIZE,
54 fit: Fit::default(),
55 filters: ImageFilters::default(),
56 playing: true,
57 transition: ImageTransition::None,
58 leaving: None,
59 transitions: 0,
60 }
61 }
62}
63
64#[derive(Clone, Copy, Debug, Default, PartialEq)]
76pub enum ImageTransition {
77 #[default]
79 None,
80 Crossfade(Transition),
82 Wipe(Transition, f32),
87 Circle(Transition),
90 Pixelate(Transition),
93}
94
95impl ImageTransition {
96 pub const fn none() -> ImageTransition {
98 ImageTransition::None
99 }
100
101 pub const fn crossfade(transition: Transition) -> ImageTransition {
103 ImageTransition::Crossfade(transition)
104 }
105
106 pub const fn wipe(transition: Transition, angle: f32) -> ImageTransition {
108 ImageTransition::Wipe(transition, angle)
109 }
110
111 pub const fn circle(transition: Transition) -> ImageTransition {
113 ImageTransition::Circle(transition)
114 }
115
116 pub const fn pixelate(transition: Transition) -> ImageTransition {
118 ImageTransition::Pixelate(transition)
119 }
120
121 pub const fn transition(self) -> Option<Transition> {
123 match self {
124 ImageTransition::None => None,
125 ImageTransition::Crossfade(transition)
126 | ImageTransition::Wipe(transition, _)
127 | ImageTransition::Circle(transition)
128 | ImageTransition::Pixelate(transition) => Some(transition),
129 }
130 }
131}
132
133pub(crate) struct Leaving {
135 pub(crate) source: PathBuf,
136 pub(crate) t: f32,
138 generation: u64,
140}
141
142fn filter_value(value: f32, unset: f32) -> f32 {
145 if !value.is_finite() {
146 unset
147 } else if value > 0.0 {
148 value
149 } else {
150 0.0
151 }
152}
153
154fn repaint<T: PartialEq>(slot: &mut T, value: T) -> Change {
156 match replace(slot, value) {
157 Change::None => Change::None,
158 _ => Change::Paint,
159 }
160}
161
162impl Tree {
163 pub(crate) fn bind_image(&mut self, id: NodeId, props: ImageProps) {
164 self.images.push(id);
165 self.bind(id, props.source, "image", |tree, id, value| {
166 let mut started = None;
167 tree.update(id, |node| {
169 let drawn = node.rect.is_some();
170 let NodeKind::Image(image) = &mut node.kind else {
171 return Change::None;
172 };
173 if image.source.as_ref() == Some(&value) {
174 return Change::None;
175 }
176 let old = image.source.replace(value);
177 let transition = image
178 .transition
179 .transition()
180 .filter(|transition| !transition.is_instant());
181 let old = match image.leaving.take() {
183 Some(leaving) if leaving.t < 0.5 => Some(leaving.source),
184 _ => old,
185 };
186 let old = old.filter(|old| image.source.as_ref() != Some(old));
189 if let (Some(old), Some(transition), true) = (old, transition, drawn) {
190 image.transitions += 1;
191 image.leaving = Some(Leaving {
192 source: old,
193 t: 0.0,
194 generation: image.transitions,
195 });
196 started = Some((image.transitions, transition));
197 }
198 Change::Paint
199 });
200 if let Some((generation, transition)) = started {
201 tree.start_image_transition(id, generation, transition);
202 }
203 });
204 self.bind_opt(id, props.width, "width", |tree, id, value| {
205 tree.update_image(id, |image| replace(&mut image.width, value));
206 });
207 self.bind_opt(id, props.height, "height", |tree, id, value| {
208 tree.update_image(id, |image| replace(&mut image.height, value));
209 });
210 self.bind_opt(id, props.fit, "fit", |tree, id, value| {
211 tree.update_image(id, |image| repaint(&mut image.fit, value));
212 });
213 self.bind_opt(id, props.tint, "tint", |tree, id, value| {
214 tree.update_image(id, |image| repaint(&mut image.filters.tint, value));
215 });
216 self.bind_opt(id, props.brightness, "brightness", |tree, id, value| {
217 let value = filter_value(value, 1.0);
218 tree.update_image(id, |image| repaint(&mut image.filters.brightness, value));
219 });
220 self.bind_opt(id, props.contrast, "contrast", |tree, id, value| {
221 let value = filter_value(value, 1.0);
222 tree.update_image(id, |image| repaint(&mut image.filters.contrast, value));
223 });
224 self.bind_opt(id, props.saturation, "saturation", |tree, id, value| {
225 let value = filter_value(value, 1.0);
226 tree.update_image(id, |image| repaint(&mut image.filters.saturation, value));
227 });
228 self.bind_opt(id, props.blur, "blur", |tree, id, value| {
229 let value = filter_value(value, 0.0);
230 tree.update_image(id, |image| repaint(&mut image.filters.blur, value));
231 });
232 self.bind_opt(id, props.playing, "playing", |tree, id, value| {
233 tree.update_image(id, |image| repaint(&mut image.playing, value));
234 });
235 self.bind_opt(id, props.transition, "transition", |tree, id, value| {
236 tree.update_image(id, |image| repaint(&mut image.transition, value));
237 });
238 }
239
240 fn start_image_transition(&self, id: NodeId, generation: u64, transition: Transition) {
244 let tree = self.this.clone();
245 let mut started = animation::start_time();
246 let label = self.node(id).map_or_else(
247 || Rc::from("an image transition"),
248 |node| format!("{}.transition", node.path).into(),
249 );
250 animation::start(
251 label,
252 Box::new(move |now| {
253 let Some(tree) = tree.upgrade() else {
254 return false;
255 };
256 let Ok(mut tree) = tree.try_borrow_mut() else {
257 return true;
258 };
259 let begun = *started.get_or_insert(now);
260 let waiting = transition.waiting(begun, now);
261 let (t, ended) = transition.progress(begun, now);
262 let mut mine = false;
263 tree.update_image(id, |image| {
264 let Some(leaving) = image
265 .leaving
266 .as_mut()
267 .filter(|leaving| leaving.generation == generation)
268 else {
269 return Change::None;
270 };
271 mine = true;
272 if ended {
273 image.leaving = None;
274 } else if waiting {
275 return Change::None;
277 } else {
278 leaving.t = t;
279 }
280 Change::Paint
281 });
282 mine && !ended
283 }),
284 );
285 }
286
287 pub(crate) fn bind_icon(&mut self, id: NodeId, props: IconProps) {
288 self.bind(id, props.source, "icon", |tree, id, value| {
289 tree.update_icon(id, |icon| repaint(&mut icon.source, Some(value)));
291 });
292 self.bind_opt(id, props.size, "size", |tree, id, value| {
293 tree.update_icon(id, |icon| replace(&mut icon.size, value));
294 });
295 self.bind_opt(id, props.tint, "tint", |tree, id, value| {
296 tree.update_icon(id, |icon| repaint(&mut icon.tint, value));
297 });
298 }
299}
300
301impl Window {
302 pub fn repaint_image(&self, id: u64) -> bool {
306 let mut tree = borrow(&self.tree);
307 let found = tree.images.iter().copied().find(|&node| {
308 matches!(tree.node(node), Some(node) if node.rect.is_some()
309 && matches!(&node.kind, NodeKind::Image(image) if image.id == id))
310 });
311 let Some(node) = found else {
312 return false;
313 };
314 tree.invalidate(node, Change::Paint);
315 true
316 }
317}