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 self.push_scroll(id, rect, delta, false);
143 }
144
145 pub(crate) fn scroll_children_damage(&mut self, id: ComponentId, rect: RectF, delta: crate::PointF) {
150 self.push_scroll(id, rect, delta, true);
151 }
152
153 fn push_scroll(&mut self, id: ComponentId, rect: RectF, delta: crate::PointF, children: bool) {
154 if delta.x == 0.0 && delta.y == 0.0 {
156 return;
157 }
158 self.repaint_requested = true;
159 self.scroll_moves.borrow_mut().push((id, rect, delta, children));
160 }
161
162 fn movable(&self, root: ComponentId, id: ComponentId, rect: RectF, children: bool) -> Option<RectF> {
170 if self.scaled(id) {
172 return None;
173 }
174 let abs = self.damage_rect(id)?;
175 let entry = self.entry(id)?;
176 let cb = self.control(id)?;
177 let mut backdrop = {
179 let v = entry.visual.borrow();
180 let style = v.shown.as_ref()?;
181 let see_through = children && style.fill.a == 0;
182 if v.shift != cb.translate() || (style.fill.a != 255 && !see_through) || style.material != crate::theme::Material::None || style.opacity < 1.0 || cb.opacity() < 1.0 {
183 return None;
184 }
185 if children && style.radius > 0.0 {
187 return None;
188 }
189 see_through
190 };
191 let mut region = rect.offset(abs.x, abs.y);
193 let covers = |c: ComponentId, region: RectF| {
194 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))
195 };
196 if !children && self.children(id).iter().any(|&c| covers(c, region)) {
197 return None;
198 }
199 let mut node = id;
200 while let Some(parent) = self.parent(node) {
201 let pcb = self.control(parent)?;
202 if pcb.opacity() < 1.0 {
203 return None;
204 }
205 if pcb.clip_children() {
206 region = intersection(region, self.damage_rect(parent)?)?;
207 }
208 let siblings = self.children(parent);
209 let at = siblings.iter().position(|&c| c == node)?;
210 if siblings[at + 1..].iter().any(|&c| covers(c, region)) {
211 return None;
212 }
213 if backdrop {
214 let below = |c: ComponentId| {
218 let own = self.entry(c).and_then(|e| e.visual.borrow().ink);
219 self.control(c).is_some_and(|ccb| ccb.visible()) && [own, self.subtree_ink(c)].into_iter().flatten().any(|r| r.intersects(region))
220 };
221 if siblings[..at].iter().any(|&c| below(c)) {
222 return None;
223 }
224 let plain = self.entry(parent)?.visual.borrow().plain;
225 match plain {
226 crate::ink::Plain::Nothing => {}
227 crate::ink::Plain::Fill(r, _) if r.contains_rect(region) => backdrop = false,
228 _ => return None,
229 }
230 }
231 node = parent;
232 }
233 let form = self.control(root).map(|f| RectF::new(0.0, 0.0, f.width(), f.height()))?;
235 intersection(region, form)
236 }
237
238 pub fn take_frame(&mut self, root: ComponentId) -> FrameDamage {
242 self.update_layout();
244 self.form_painted(root);
245 let mut content = std::mem::take(&mut *self.frame_ids.borrow_mut());
248 let mut placed = std::mem::take(&mut *self.placed_ids.borrow_mut());
249 for list in [&mut content, &mut placed] {
252 let mut seen = std::collections::HashSet::with_capacity(list.len());
253 list.retain(|id| seen.insert(*id));
254 }
255 let parts = std::mem::take(&mut *self.part_ids.borrow_mut());
256 let scrolls = std::mem::take(&mut *self.scroll_moves.borrow_mut());
257 let anything = !(content.is_empty() && placed.is_empty() && parts.is_empty() && scrolls.is_empty());
258 let mut areas = Vec::new();
259 let mut moves = Vec::new();
260 let mut whole = false;
261 let (mut others, mut others_placed, mut other_parts, mut other_scrolls) = (Vec::new(), Vec::new(), Vec::new(), Vec::new());
262 let mut changed: Vec<ComponentId> = Vec::new();
264 for (id, inside) in content.into_iter().map(|id| (id, true)).chain(placed.into_iter().map(|id| (id, false))) {
265 if !self.contains(id) {
266 continue;
267 }
268 if self.root_of(id) != root {
269 if inside { others.push(id) } else { others_placed.push(id) }
270 continue;
271 }
272 if inside {
273 changed.push(id);
274 }
275 self.bump_versions(id, inside);
276 match self.damage_rect(id).and_then(|r| self.extent(id, r)) {
277 Some(mut r) => {
278 let now = self.control(id).map_or(crate::PointF::ZERO, |cb| cb.translate());
282 let (before, own) = self.entry(id).map_or((now, None), |e| {
283 let v = e.visual.borrow();
284 (v.shift, v.ink)
285 });
286 if now != before {
287 let (dx, dy) = (now.x - before.x, now.y - before.y);
288 for ink in [own, self.subtree_ink(id)].into_iter().flatten() {
289 r = r.union(ink.inflate(EDGE)).union(ink.offset(dx, dy).inflate(EDGE));
290 }
291 }
292 areas.push(r);
293 }
294 None => whole = true,
295 }
296 }
297 for (id, rect) in parts {
298 if !self.contains(id) {
299 continue;
300 }
301 if self.root_of(id) != root {
302 other_parts.push((id, rect));
303 continue;
304 }
305 self.bump_versions(id, true);
306 match self.damage_rect(id) {
307 Some(abs) if self.scaled(id) => areas.push(self.extent(id, abs).unwrap_or(abs)),
309 Some(abs) => areas.push(rect.offset(abs.x, abs.y).inflate(EDGE)),
310 None => whole = true,
311 }
312 }
313 let mut scrolled: Vec<(ComponentId, RectF, crate::PointF, bool)> = Vec::new();
315 for (id, rect, delta, children) in scrolls {
316 if !self.contains(id) {
317 continue;
318 }
319 if self.root_of(id) != root {
320 other_scrolls.push((id, rect, delta, children));
321 continue;
322 }
323 match scrolled.iter_mut().find(|(i, ..)| *i == id) {
324 Some(s) => {
325 s.1 = rect;
326 s.2 = crate::PointF::new(s.2.x + delta.x, s.2.y + delta.y);
327 s.3 &= children;
328 }
329 None => scrolled.push((id, rect, delta, children)),
330 }
331 }
332 let floating = self.visible_hint(root).is_some() || self.drag_view(root).is_some();
334 for (id, rect, d, children) in scrolled {
335 self.bump_versions(id, true);
336 let region = if floating || changed.contains(&id) { None } else { self.movable(root, id, rect, children) };
337 match region {
338 Some(r) if d.x.abs() < r.width && d.y.abs() < r.height && (d.x != 0.0 || d.y != 0.0) => {
339 let Some(dest) = intersection(r.offset(d.x, d.y), r) else {
341 areas.push(r);
342 continue;
343 };
344 moves.push(ScrollMove { rect: dest.offset(-d.x, -d.y), delta: d });
345 if d.y > 0.0 {
346 areas.push(RectF::new(r.x, r.y, r.width, d.y));
347 } else if d.y < 0.0 {
348 areas.push(RectF::new(r.x, r.bottom() + d.y, r.width, -d.y));
349 }
350 if d.x > 0.0 {
351 areas.push(RectF::new(r.x, r.y, d.x, r.height));
352 } else if d.x < 0.0 {
353 areas.push(RectF::new(r.right() + d.x, r.y, -d.x, r.height));
354 }
355 }
356 Some(r) => areas.push(r),
357 None => match self.damage_rect(id) {
358 Some(abs) if self.scaled(id) => areas.push(self.extent(id, abs).unwrap_or(abs)),
359 Some(abs) => areas.push(rect.offset(abs.x, abs.y).inflate(EDGE)),
360 None => whole = true,
361 },
362 }
363 }
364 if let Some(mark) = self.middle_scroll_mark(root).filter(|_| !moves.is_empty()) {
367 let origin = self.absolute_rect(root).map_or(crate::PointF::ZERO, |r| r.origin());
368 let mark = mark.offset(origin.x, origin.y);
369 areas.push(mark);
370 areas.extend(moves.iter().map(|m| mark.offset(m.delta.x, m.delta.y)));
371 }
372 self.frame_ids.borrow_mut().extend(others);
374 self.placed_ids.borrow_mut().extend(others_placed);
375 self.part_ids.borrow_mut().extend(other_parts);
376 self.scroll_moves.borrow_mut().extend(other_scrolls);
377 let epoch = self.damage_epoch;
378 let full = self.painted_epoch.insert(root, epoch) != Some(epoch);
379 if full || whole || !anything {
380 return FrameDamage::Whole;
381 }
382 let areas = merge_areas(areas);
383 if moves.is_empty() && areas.is_empty() {
384 return FrameDamage::Whole;
385 }
386 FrameDamage::Parts { moves, areas }
387 }
388
389 pub fn take_due_frames(&mut self, now: f64) -> bool {
393 let due: Vec<ComponentId> = {
394 let mut timed = self.timed_frames.borrow_mut();
395 let due: Vec<ComponentId> = timed.iter().filter(|(_, t)| **t <= now).map(|(id, _)| *id).collect();
396 for id in &due {
397 timed.remove(id);
398 }
399 due
400 };
401 let mut any = false;
402 for id in due {
403 if self.contains(id) {
404 self.damage(id);
405 any = true;
406 }
407 }
408 any
409 }
410
411 pub fn forms_to_paint(&self, windows: &[ComponentId]) -> Vec<ComponentId> {
421 let mut forms = self.forms_with_changes(windows);
422 if !self.frames.painted.is_empty() {
423 forms.retain(|&f| self.paint_held_until(f).is_none());
424 }
425 forms
426 }
427
428 fn forms_with_changes(&self, windows: &[ComponentId]) -> Vec<ComponentId> {
429 if self.layout_dirty || self.is_animating() {
430 return windows.to_vec();
431 }
432 let mut marked: std::collections::HashSet<ComponentId> = self.dirty_forms.iter().copied().collect();
433 marked.extend(self.dirty_parts.iter().filter(|&&p| self.contains(p)).map(|&p| self.root_of(p)));
434 let ids = self.frame_ids.borrow();
435 let placed = self.placed_ids.borrow();
436 let parts = self.part_ids.borrow();
437 let scrolls = self.scroll_moves.borrow();
438 let all = ids.iter().chain(placed.iter()).copied().chain(parts.iter().map(|(id, _)| *id)).chain(scrolls.iter().map(|(id, ..)| *id));
439 for id in all {
440 if self.contains(id) {
441 marked.insert(self.root_of(id));
442 }
443 }
444 if marked.iter().any(|root| !windows.contains(root)) {
445 return windows.to_vec();
446 }
447 let epoch = self.damage_epoch;
448 windows.iter().copied().filter(|w| marked.contains(w) || self.painted_epoch.get(w) != Some(&epoch)).collect()
449 }
450
451 pub fn take_damage(&mut self, root: ComponentId) -> Option<RectF> {
455 self.take_frame(root).bounds()
456 }
457}
458
459#[derive(Clone, Copy, Debug, PartialEq)]
462pub struct ScrollMove {
463 pub rect: RectF,
464 pub delta: crate::PointF,
465}
466
467#[derive(Clone, Debug, PartialEq)]
469pub enum FrameDamage {
470 Whole,
472 Parts { moves: Vec<ScrollMove>, areas: Vec<RectF> },
475}
476
477impl FrameDamage {
478 pub fn bounds(&self) -> Option<RectF> {
481 match self {
482 FrameDamage::Whole => None,
483 FrameDamage::Parts { moves, areas } => {
484 let all = moves.iter().flat_map(|m| [m.rect, m.rect.offset(m.delta.x, m.delta.y)]).chain(areas.iter().copied());
485 all.reduce(|a, b| a.union(b))
486 }
487 }
488 }
489
490 pub fn snapped(self, scale: f32) -> FrameDamage {
493 let FrameDamage::Parts { moves, mut areas } = self else { return self };
494 let whole = |v: f32| ((v * scale) - (v * scale).round()).abs() < 0.01;
495 let mut kept = Vec::new();
496 for m in moves {
497 let r = m.rect;
498 if [r.x, r.y, r.right(), r.bottom(), m.delta.x, m.delta.y].into_iter().all(whole) {
499 kept.push(m);
500 } else {
501 areas.push(r.union(r.offset(m.delta.x, m.delta.y)));
502 }
503 }
504 FrameDamage::Parts { moves: kept, areas }
505 }
506
507 pub fn without_moves(self) -> FrameDamage {
510 let FrameDamage::Parts { moves, mut areas } = self else { return self };
511 areas.extend(moves.iter().map(|m| m.rect.union(m.rect.offset(m.delta.x, m.delta.y))));
512 FrameDamage::Parts { moves: Vec::new(), areas }
513 }
514}
515
516fn intersection(a: RectF, b: RectF) -> Option<RectF> {
518 let (x, y) = (a.x.max(b.x), a.y.max(b.y));
519 let (r, bottom) = (a.right().min(b.right()), a.bottom().min(b.bottom()));
520 (r > x && bottom > y).then(|| RectF::new(x, y, r - x, bottom - y))
521}
522
523fn merge_areas(mut rects: Vec<RectF>) -> Vec<RectF> {
527 if rects.len() > 256 {
528 return rects.into_iter().reduce(|a, b| a.union(b)).into_iter().collect();
529 }
530 let size = |r: RectF| r.width.max(0.0) * r.height.max(0.0);
531 loop {
532 let mut joined = None;
533 'search: for i in 0..rects.len() {
534 for j in i + 1..rects.len() {
535 let (a, b) = (rects[i], rects[j]);
536 let u = a.union(b);
537 if size(u) <= (size(a) + size(b)) * 1.25 {
540 joined = Some((i, j, u));
541 break 'search;
542 }
543 }
544 }
545 match joined {
546 Some((i, j, u)) => {
547 rects[i] = u;
548 rects.swap_remove(j);
549 }
550 None => return rects,
551 }
552 }
553}