1use crate::component::ComponentId;
10use crate::geometry::RectF;
11use crate::ui::Ui;
12
13const EDGE: f32 = 3.0;
15
16const RECENT: usize = 16;
18
19fn push_recent(list: &mut Vec<ComponentId>, id: ComponentId) {
25 if !list.iter().rev().take(RECENT).any(|&x| x == id) {
26 list.push(id);
27 }
28}
29
30impl Ui {
31 fn extent(&self, id: ComponentId, rect: RectF) -> Option<RectF> {
37 let visual = self.entry(id)?.visual.borrow();
38 let r = rect.inflate(visual.reach.unwrap_or(0.0) + EDGE);
39 Some(visual.ink.map_or(r, |ink| r.union(ink.inflate(EDGE))))
40 }
41
42 pub fn damage(&mut self, id: ComponentId) {
46 self.repaint_requested = true;
47 push_recent(&mut self.frame_ids.borrow_mut(), id);
48 }
49
50 pub fn damage_placement(&mut self, id: ComponentId) {
54 self.repaint_requested = true;
55 push_recent(&mut self.placed_ids.borrow_mut(), id);
56 }
57
58 fn bump_versions(&self, id: ComponentId, itself: bool) {
61 let mut current = if itself { Some(id) } else { self.parent(id) };
62 while let Some(c) = current {
63 if let Some(entry) = self.entry(c) {
64 entry.visual.borrow_mut().version += 1;
65 }
66 current = self.parent(c);
67 }
68 }
69
70 fn damage_rect(&self, id: ComponentId) -> Option<RectF> {
73 let root = self.root_of(id);
74 if self.overlay_users.borrow().contains(&id) || !self.open_forms().contains(&root) || self.flips_content(id) {
75 return None;
76 }
77 let mut chain: Vec<(crate::PointF, f32)> = Vec::new();
80 let mut current = Some(id);
81 while let Some(c) = current {
82 let cb = self.control(c)?;
83 if cb.rotation() != 0.0 {
84 return None;
85 }
86 chain.push((cb.translate(), cb.scale()));
87 current = self.parent(c);
88 }
89 let mut shift = crate::PointF::ZERO;
90 let mut factor = 1.0;
91 for i in (0..chain.len()).rev() {
92 let (t, s) = chain[i];
93 shift = crate::PointF::new(shift.x + t.x * factor, shift.y + t.y * factor);
94 factor *= s;
95 }
96 self.absolute_rect(id).map(|r| r.offset(shift.x, shift.y))
97 }
98
99 fn scaled(&self, id: ComponentId) -> bool {
102 let mut current = Some(id);
103 while let Some(c) = current {
104 if self.control(c).is_some_and(|cb| cb.scale() != 1.0) {
105 return true;
106 }
107 current = self.parent(c);
108 }
109 false
110 }
111
112 fn subtree_ink(&self, id: ComponentId) -> Option<RectF> {
115 let mut ink: Option<RectF> = None;
116 let mut stack: Vec<ComponentId> = self.children(id).to_vec();
117 while let Some(c) = stack.pop() {
118 if let Some(entry) = self.entry(c) {
119 if let Some(r) = entry.visual.borrow().ink {
120 ink = Some(ink.map_or(r, |i| i.union(r)));
121 }
122 }
123 stack.extend_from_slice(self.children(c));
124 }
125 ink
126 }
127
128 pub fn damage_part(&mut self, id: ComponentId, rect: RectF) {
132 self.repaint_requested = true;
133 self.part_ids.borrow_mut().push((id, rect));
134 }
135
136 pub fn scroll_damage(&mut self, id: ComponentId, rect: RectF, delta: crate::PointF) {
142 if delta.x == 0.0 && delta.y == 0.0 {
144 return;
145 }
146 self.repaint_requested = true;
147 self.scroll_moves.borrow_mut().push((id, rect, delta));
148 }
149
150 fn movable(&self, root: ComponentId, id: ComponentId, rect: RectF) -> Option<RectF> {
155 if self.scaled(id) {
157 return None;
158 }
159 let abs = self.damage_rect(id)?;
160 let entry = self.entry(id)?;
161 let cb = self.control(id)?;
162 {
163 let v = entry.visual.borrow();
164 let style = v.shown.as_ref()?;
165 if v.shift != cb.translate() || style.fill.a != 255 || style.material != crate::theme::Material::None || style.opacity < 1.0 || cb.opacity() < 1.0 {
166 return None;
167 }
168 }
169 let mut region = rect.offset(abs.x, abs.y);
171 let covers = |c: ComponentId, region: RectF| {
172 self.control(c).is_some_and(|ccb| ccb.visible()) && self.damage_rect(c).and_then(|r| self.extent(c, r)).is_none_or(|r| r.intersects(region))
173 };
174 if self.children(id).iter().any(|&c| covers(c, region)) {
175 return None;
176 }
177 let mut node = id;
178 while let Some(parent) = self.parent(node) {
179 let pcb = self.control(parent)?;
180 if pcb.opacity() < 1.0 {
181 return None;
182 }
183 if pcb.clip_children() {
184 region = intersection(region, self.damage_rect(parent)?)?;
185 }
186 let siblings = self.children(parent);
187 let at = siblings.iter().position(|&c| c == node)?;
188 if siblings[at + 1..].iter().any(|&c| covers(c, region)) {
189 return None;
190 }
191 node = parent;
192 }
193 let form = self.control(root).map(|f| RectF::new(0.0, 0.0, f.width(), f.height()))?;
194 intersection(region, form)
195 }
196
197 pub fn take_frame(&mut self, root: ComponentId) -> FrameDamage {
201 self.update_layout();
203 let mut content = std::mem::take(&mut *self.frame_ids.borrow_mut());
206 let mut placed = std::mem::take(&mut *self.placed_ids.borrow_mut());
207 for list in [&mut content, &mut placed] {
210 let mut seen = std::collections::HashSet::with_capacity(list.len());
211 list.retain(|id| seen.insert(*id));
212 }
213 let parts = std::mem::take(&mut *self.part_ids.borrow_mut());
214 let scrolls = std::mem::take(&mut *self.scroll_moves.borrow_mut());
215 let anything = !(content.is_empty() && placed.is_empty() && parts.is_empty() && scrolls.is_empty());
216 let mut areas = Vec::new();
217 let mut moves = Vec::new();
218 let mut whole = false;
219 let (mut others, mut others_placed, mut other_parts, mut other_scrolls) = (Vec::new(), Vec::new(), Vec::new(), Vec::new());
220 let mut changed: Vec<ComponentId> = Vec::new();
222 for (id, inside) in content.into_iter().map(|id| (id, true)).chain(placed.into_iter().map(|id| (id, false))) {
223 if !self.contains(id) {
224 continue;
225 }
226 if self.root_of(id) != root {
227 if inside { others.push(id) } else { others_placed.push(id) }
228 continue;
229 }
230 if inside {
231 changed.push(id);
232 }
233 self.bump_versions(id, inside);
234 match self.damage_rect(id).and_then(|r| self.extent(id, r)) {
235 Some(mut r) => {
236 let now = self.control(id).map_or(crate::PointF::ZERO, |cb| cb.translate());
240 let (before, own) = self.entry(id).map_or((now, None), |e| {
241 let v = e.visual.borrow();
242 (v.shift, v.ink)
243 });
244 if now != before {
245 let (dx, dy) = (now.x - before.x, now.y - before.y);
246 for ink in [own, self.subtree_ink(id)].into_iter().flatten() {
247 r = r.union(ink.inflate(EDGE)).union(ink.offset(dx, dy).inflate(EDGE));
248 }
249 }
250 areas.push(r);
251 }
252 None => whole = true,
253 }
254 }
255 for (id, rect) in parts {
256 if !self.contains(id) {
257 continue;
258 }
259 if self.root_of(id) != root {
260 other_parts.push((id, rect));
261 continue;
262 }
263 self.bump_versions(id, true);
264 match self.damage_rect(id) {
265 Some(abs) if self.scaled(id) => areas.push(self.extent(id, abs).unwrap_or(abs)),
267 Some(abs) => areas.push(rect.offset(abs.x, abs.y).inflate(EDGE)),
268 None => whole = true,
269 }
270 }
271 let mut scrolled: Vec<(ComponentId, RectF, crate::PointF)> = Vec::new();
273 for (id, rect, delta) in scrolls {
274 if !self.contains(id) {
275 continue;
276 }
277 if self.root_of(id) != root {
278 other_scrolls.push((id, rect, delta));
279 continue;
280 }
281 match scrolled.iter_mut().find(|(i, _, _)| *i == id) {
282 Some(s) => {
283 s.1 = rect;
284 s.2 = crate::PointF::new(s.2.x + delta.x, s.2.y + delta.y);
285 }
286 None => scrolled.push((id, rect, delta)),
287 }
288 }
289 let floating = self.visible_hint(root).is_some() || self.drag_view(root).is_some();
291 for (id, rect, d) in scrolled {
292 self.bump_versions(id, true);
293 let region = if floating || changed.contains(&id) { None } else { self.movable(root, id, rect) };
294 match region {
295 Some(r) if d.x.abs() < r.width && d.y.abs() < r.height && (d.x != 0.0 || d.y != 0.0) => {
296 let Some(dest) = intersection(r.offset(d.x, d.y), r) else {
298 areas.push(r);
299 continue;
300 };
301 moves.push(ScrollMove { rect: dest.offset(-d.x, -d.y), delta: d });
302 if d.y > 0.0 {
303 areas.push(RectF::new(r.x, r.y, r.width, d.y));
304 } else if d.y < 0.0 {
305 areas.push(RectF::new(r.x, r.bottom() + d.y, r.width, -d.y));
306 }
307 if d.x > 0.0 {
308 areas.push(RectF::new(r.x, r.y, d.x, r.height));
309 } else if d.x < 0.0 {
310 areas.push(RectF::new(r.right() + d.x, r.y, -d.x, r.height));
311 }
312 }
313 Some(r) => areas.push(r),
314 None => match self.damage_rect(id) {
315 Some(abs) if self.scaled(id) => areas.push(self.extent(id, abs).unwrap_or(abs)),
316 Some(abs) => areas.push(rect.offset(abs.x, abs.y).inflate(EDGE)),
317 None => whole = true,
318 },
319 }
320 }
321 self.frame_ids.borrow_mut().extend(others);
323 self.placed_ids.borrow_mut().extend(others_placed);
324 self.part_ids.borrow_mut().extend(other_parts);
325 self.scroll_moves.borrow_mut().extend(other_scrolls);
326 let epoch = self.damage_epoch;
327 let full = self.painted_epoch.insert(root, epoch) != Some(epoch);
328 if full || whole || !anything {
329 return FrameDamage::Whole;
330 }
331 let areas = merge_areas(areas);
332 if moves.is_empty() && areas.is_empty() {
333 return FrameDamage::Whole;
334 }
335 FrameDamage::Parts { moves, areas }
336 }
337
338 pub fn take_due_frames(&mut self, now: f64) -> bool {
342 let due: Vec<ComponentId> = {
343 let mut timed = self.timed_frames.borrow_mut();
344 let due: Vec<ComponentId> = timed.iter().filter(|(_, t)| **t <= now).map(|(id, _)| *id).collect();
345 for id in &due {
346 timed.remove(id);
347 }
348 due
349 };
350 let mut any = false;
351 for id in due {
352 if self.contains(id) {
353 self.damage(id);
354 any = true;
355 }
356 }
357 any
358 }
359
360 pub fn forms_to_paint(&self, windows: &[ComponentId]) -> Vec<ComponentId> {
366 if self.layout_dirty || self.is_animating() {
367 return windows.to_vec();
368 }
369 let mut marked: std::collections::HashSet<ComponentId> = self.dirty_forms.iter().copied().collect();
370 let ids = self.frame_ids.borrow();
371 let placed = self.placed_ids.borrow();
372 let parts = self.part_ids.borrow();
373 let scrolls = self.scroll_moves.borrow();
374 let all = ids.iter().chain(placed.iter()).copied().chain(parts.iter().map(|(id, _)| *id)).chain(scrolls.iter().map(|(id, _, _)| *id));
375 for id in all {
376 if self.contains(id) {
377 marked.insert(self.root_of(id));
378 }
379 }
380 if marked.iter().any(|root| !windows.contains(root)) {
381 return windows.to_vec();
382 }
383 let epoch = self.damage_epoch;
384 windows.iter().copied().filter(|w| marked.contains(w) || self.painted_epoch.get(w) != Some(&epoch)).collect()
385 }
386
387 pub fn take_damage(&mut self, root: ComponentId) -> Option<RectF> {
391 self.take_frame(root).bounds()
392 }
393}
394
395#[derive(Clone, Copy, Debug, PartialEq)]
398pub struct ScrollMove {
399 pub rect: RectF,
400 pub delta: crate::PointF,
401}
402
403#[derive(Clone, Debug, PartialEq)]
405pub enum FrameDamage {
406 Whole,
408 Parts { moves: Vec<ScrollMove>, areas: Vec<RectF> },
411}
412
413impl FrameDamage {
414 pub fn bounds(&self) -> Option<RectF> {
417 match self {
418 FrameDamage::Whole => None,
419 FrameDamage::Parts { moves, areas } => {
420 let all = moves.iter().flat_map(|m| [m.rect, m.rect.offset(m.delta.x, m.delta.y)]).chain(areas.iter().copied());
421 all.reduce(|a, b| a.union(b))
422 }
423 }
424 }
425
426 pub fn snapped(self, scale: f32) -> FrameDamage {
429 let FrameDamage::Parts { moves, mut areas } = self else { return self };
430 let whole = |v: f32| ((v * scale) - (v * scale).round()).abs() < 0.01;
431 let mut kept = Vec::new();
432 for m in moves {
433 let r = m.rect;
434 if [r.x, r.y, r.right(), r.bottom(), m.delta.x, m.delta.y].into_iter().all(whole) {
435 kept.push(m);
436 } else {
437 areas.push(r.union(r.offset(m.delta.x, m.delta.y)));
438 }
439 }
440 FrameDamage::Parts { moves: kept, areas }
441 }
442
443 pub fn without_moves(self) -> FrameDamage {
446 let FrameDamage::Parts { moves, mut areas } = self else { return self };
447 areas.extend(moves.iter().map(|m| m.rect.union(m.rect.offset(m.delta.x, m.delta.y))));
448 FrameDamage::Parts { moves: Vec::new(), areas }
449 }
450}
451
452fn intersection(a: RectF, b: RectF) -> Option<RectF> {
454 let (x, y) = (a.x.max(b.x), a.y.max(b.y));
455 let (r, bottom) = (a.right().min(b.right()), a.bottom().min(b.bottom()));
456 (r > x && bottom > y).then(|| RectF::new(x, y, r - x, bottom - y))
457}
458
459fn merge_areas(mut rects: Vec<RectF>) -> Vec<RectF> {
463 if rects.len() > 256 {
464 return rects.into_iter().reduce(|a, b| a.union(b)).into_iter().collect();
465 }
466 let size = |r: RectF| r.width.max(0.0) * r.height.max(0.0);
467 loop {
468 let mut joined = None;
469 'search: for i in 0..rects.len() {
470 for j in i + 1..rects.len() {
471 let (a, b) = (rects[i], rects[j]);
472 let u = a.union(b);
473 if size(u) <= (size(a) + size(b)) * 1.25 {
476 joined = Some((i, j, u));
477 break 'search;
478 }
479 }
480 }
481 match joined {
482 Some((i, j, u)) => {
483 rects[i] = u;
484 rects.swap_remove(j);
485 }
486 None => return rects,
487 }
488 }
489}