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 let mut content = std::mem::take(&mut *self.frame_ids.borrow_mut());
247 let mut placed = std::mem::take(&mut *self.placed_ids.borrow_mut());
248 for list in [&mut content, &mut placed] {
251 let mut seen = std::collections::HashSet::with_capacity(list.len());
252 list.retain(|id| seen.insert(*id));
253 }
254 let parts = std::mem::take(&mut *self.part_ids.borrow_mut());
255 let scrolls = std::mem::take(&mut *self.scroll_moves.borrow_mut());
256 let anything = !(content.is_empty() && placed.is_empty() && parts.is_empty() && scrolls.is_empty());
257 let mut areas = Vec::new();
258 let mut moves = Vec::new();
259 let mut whole = false;
260 let (mut others, mut others_placed, mut other_parts, mut other_scrolls) = (Vec::new(), Vec::new(), Vec::new(), Vec::new());
261 let mut changed: Vec<ComponentId> = Vec::new();
263 for (id, inside) in content.into_iter().map(|id| (id, true)).chain(placed.into_iter().map(|id| (id, false))) {
264 if !self.contains(id) {
265 continue;
266 }
267 if self.root_of(id) != root {
268 if inside { others.push(id) } else { others_placed.push(id) }
269 continue;
270 }
271 if inside {
272 changed.push(id);
273 }
274 self.bump_versions(id, inside);
275 match self.damage_rect(id).and_then(|r| self.extent(id, r)) {
276 Some(mut r) => {
277 let now = self.control(id).map_or(crate::PointF::ZERO, |cb| cb.translate());
281 let (before, own) = self.entry(id).map_or((now, None), |e| {
282 let v = e.visual.borrow();
283 (v.shift, v.ink)
284 });
285 if now != before {
286 let (dx, dy) = (now.x - before.x, now.y - before.y);
287 for ink in [own, self.subtree_ink(id)].into_iter().flatten() {
288 r = r.union(ink.inflate(EDGE)).union(ink.offset(dx, dy).inflate(EDGE));
289 }
290 }
291 areas.push(r);
292 }
293 None => whole = true,
294 }
295 }
296 for (id, rect) in parts {
297 if !self.contains(id) {
298 continue;
299 }
300 if self.root_of(id) != root {
301 other_parts.push((id, rect));
302 continue;
303 }
304 self.bump_versions(id, true);
305 match self.damage_rect(id) {
306 Some(abs) if self.scaled(id) => areas.push(self.extent(id, abs).unwrap_or(abs)),
308 Some(abs) => areas.push(rect.offset(abs.x, abs.y).inflate(EDGE)),
309 None => whole = true,
310 }
311 }
312 let mut scrolled: Vec<(ComponentId, RectF, crate::PointF, bool)> = Vec::new();
314 for (id, rect, delta, children) in scrolls {
315 if !self.contains(id) {
316 continue;
317 }
318 if self.root_of(id) != root {
319 other_scrolls.push((id, rect, delta, children));
320 continue;
321 }
322 match scrolled.iter_mut().find(|(i, ..)| *i == id) {
323 Some(s) => {
324 s.1 = rect;
325 s.2 = crate::PointF::new(s.2.x + delta.x, s.2.y + delta.y);
326 s.3 &= children;
327 }
328 None => scrolled.push((id, rect, delta, children)),
329 }
330 }
331 let floating = self.visible_hint(root).is_some() || self.drag_view(root).is_some();
333 for (id, rect, d, children) in scrolled {
334 self.bump_versions(id, true);
335 let region = if floating || changed.contains(&id) { None } else { self.movable(root, id, rect, children) };
336 match region {
337 Some(r) if d.x.abs() < r.width && d.y.abs() < r.height && (d.x != 0.0 || d.y != 0.0) => {
338 let Some(dest) = intersection(r.offset(d.x, d.y), r) else {
340 areas.push(r);
341 continue;
342 };
343 moves.push(ScrollMove { rect: dest.offset(-d.x, -d.y), delta: d });
344 if d.y > 0.0 {
345 areas.push(RectF::new(r.x, r.y, r.width, d.y));
346 } else if d.y < 0.0 {
347 areas.push(RectF::new(r.x, r.bottom() + d.y, r.width, -d.y));
348 }
349 if d.x > 0.0 {
350 areas.push(RectF::new(r.x, r.y, d.x, r.height));
351 } else if d.x < 0.0 {
352 areas.push(RectF::new(r.right() + d.x, r.y, -d.x, r.height));
353 }
354 }
355 Some(r) => areas.push(r),
356 None => match self.damage_rect(id) {
357 Some(abs) if self.scaled(id) => areas.push(self.extent(id, abs).unwrap_or(abs)),
358 Some(abs) => areas.push(rect.offset(abs.x, abs.y).inflate(EDGE)),
359 None => whole = true,
360 },
361 }
362 }
363 self.frame_ids.borrow_mut().extend(others);
365 self.placed_ids.borrow_mut().extend(others_placed);
366 self.part_ids.borrow_mut().extend(other_parts);
367 self.scroll_moves.borrow_mut().extend(other_scrolls);
368 let epoch = self.damage_epoch;
369 let full = self.painted_epoch.insert(root, epoch) != Some(epoch);
370 if full || whole || !anything {
371 return FrameDamage::Whole;
372 }
373 let areas = merge_areas(areas);
374 if moves.is_empty() && areas.is_empty() {
375 return FrameDamage::Whole;
376 }
377 FrameDamage::Parts { moves, areas }
378 }
379
380 pub fn take_due_frames(&mut self, now: f64) -> bool {
384 let due: Vec<ComponentId> = {
385 let mut timed = self.timed_frames.borrow_mut();
386 let due: Vec<ComponentId> = timed.iter().filter(|(_, t)| **t <= now).map(|(id, _)| *id).collect();
387 for id in &due {
388 timed.remove(id);
389 }
390 due
391 };
392 let mut any = false;
393 for id in due {
394 if self.contains(id) {
395 self.damage(id);
396 any = true;
397 }
398 }
399 any
400 }
401
402 pub fn forms_to_paint(&self, windows: &[ComponentId]) -> Vec<ComponentId> {
408 if self.layout_dirty || self.is_animating() {
409 return windows.to_vec();
410 }
411 let mut marked: std::collections::HashSet<ComponentId> = self.dirty_forms.iter().copied().collect();
412 marked.extend(self.dirty_parts.iter().filter(|&&p| self.contains(p)).map(|&p| self.root_of(p)));
413 let ids = self.frame_ids.borrow();
414 let placed = self.placed_ids.borrow();
415 let parts = self.part_ids.borrow();
416 let scrolls = self.scroll_moves.borrow();
417 let all = ids.iter().chain(placed.iter()).copied().chain(parts.iter().map(|(id, _)| *id)).chain(scrolls.iter().map(|(id, ..)| *id));
418 for id in all {
419 if self.contains(id) {
420 marked.insert(self.root_of(id));
421 }
422 }
423 if marked.iter().any(|root| !windows.contains(root)) {
424 return windows.to_vec();
425 }
426 let epoch = self.damage_epoch;
427 windows.iter().copied().filter(|w| marked.contains(w) || self.painted_epoch.get(w) != Some(&epoch)).collect()
428 }
429
430 pub fn take_damage(&mut self, root: ComponentId) -> Option<RectF> {
434 self.take_frame(root).bounds()
435 }
436}
437
438#[derive(Clone, Copy, Debug, PartialEq)]
441pub struct ScrollMove {
442 pub rect: RectF,
443 pub delta: crate::PointF,
444}
445
446#[derive(Clone, Debug, PartialEq)]
448pub enum FrameDamage {
449 Whole,
451 Parts { moves: Vec<ScrollMove>, areas: Vec<RectF> },
454}
455
456impl FrameDamage {
457 pub fn bounds(&self) -> Option<RectF> {
460 match self {
461 FrameDamage::Whole => None,
462 FrameDamage::Parts { moves, areas } => {
463 let all = moves.iter().flat_map(|m| [m.rect, m.rect.offset(m.delta.x, m.delta.y)]).chain(areas.iter().copied());
464 all.reduce(|a, b| a.union(b))
465 }
466 }
467 }
468
469 pub fn snapped(self, scale: f32) -> FrameDamage {
472 let FrameDamage::Parts { moves, mut areas } = self else { return self };
473 let whole = |v: f32| ((v * scale) - (v * scale).round()).abs() < 0.01;
474 let mut kept = Vec::new();
475 for m in moves {
476 let r = m.rect;
477 if [r.x, r.y, r.right(), r.bottom(), m.delta.x, m.delta.y].into_iter().all(whole) {
478 kept.push(m);
479 } else {
480 areas.push(r.union(r.offset(m.delta.x, m.delta.y)));
481 }
482 }
483 FrameDamage::Parts { moves: kept, areas }
484 }
485
486 pub fn without_moves(self) -> FrameDamage {
489 let FrameDamage::Parts { moves, mut areas } = self else { return self };
490 areas.extend(moves.iter().map(|m| m.rect.union(m.rect.offset(m.delta.x, m.delta.y))));
491 FrameDamage::Parts { moves: Vec::new(), areas }
492 }
493}
494
495fn intersection(a: RectF, b: RectF) -> Option<RectF> {
497 let (x, y) = (a.x.max(b.x), a.y.max(b.y));
498 let (r, bottom) = (a.right().min(b.right()), a.bottom().min(b.bottom()));
499 (r > x && bottom > y).then(|| RectF::new(x, y, r - x, bottom - y))
500}
501
502fn merge_areas(mut rects: Vec<RectF>) -> Vec<RectF> {
506 if rects.len() > 256 {
507 return rects.into_iter().reduce(|a, b| a.union(b)).into_iter().collect();
508 }
509 let size = |r: RectF| r.width.max(0.0) * r.height.max(0.0);
510 loop {
511 let mut joined = None;
512 'search: for i in 0..rects.len() {
513 for j in i + 1..rects.len() {
514 let (a, b) = (rects[i], rects[j]);
515 let u = a.union(b);
516 if size(u) <= (size(a) + size(b)) * 1.25 {
519 joined = Some((i, j, u));
520 break 'search;
521 }
522 }
523 }
524 match joined {
525 Some((i, j, u)) => {
526 rects[i] = u;
527 rects.swap_remove(j);
528 }
529 None => return rects,
530 }
531 }
532}