From 27a646d2b9e204ffc7e5eb97efd00a7cc5ae7144 Mon Sep 17 00:00:00 2001 From: WuYongJie <549727955@qq.com> Date: Sun, 29 Nov 2026 04:26:30 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BF=AE=E5=A4=8D=E5=A4=9A=E4=B8=AA=E5=85=89?= =?UTF-8?q?=E6=A0=87=E6=A0=87=E7=AD=BE=E6=8C=A4=E5=9C=A8=E8=A7=92=E8=90=BD?= =?UTF-8?q?=E6=97=B6=E7=95=8C=E9=9D=A2=E7=BA=BF=E7=A8=8B=E5=8D=A1=E6=AD=BB?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit egui 的 Rect::intersects 把相贴也算相交。标签往下排时只挪到另一标签外扩 2 点的边上,会一直判为相交且不再移动,界面线程卡死在循环里(第二十二轮实测 04:08 起界面无响应)。改为越过外扩边距再放,并补测试。 --- .../src/jiansun/agent_presence/draw.rs | 31 ++++++++++++++++++- 1 file changed, 30 insertions(+), 1 deletion(-) diff --git a/apps/detail-photocraft/crates/ui-egui/src/jiansun/agent_presence/draw.rs b/apps/detail-photocraft/crates/ui-egui/src/jiansun/agent_presence/draw.rs index 711af110..886f33c8 100644 --- a/apps/detail-photocraft/crates/ui-egui/src/jiansun/agent_presence/draw.rs +++ b/apps/detail-photocraft/crates/ui-egui/src/jiansun/agent_presence/draw.rs @@ -137,7 +137,9 @@ pub(super) fn tags(p: &Painter, clip: Rect, tags: &[Tag]) -> Vec { None => { let mut rect = within(candidates[0], clip); while let Some(other) = placed.iter().find(|other| other.expand(2.0).intersects(rect)) { - rect = rect.translate(vec2(0.0, other.bottom() + 2.0 - rect.top())); + // Rect::intersects counts touching edges, so clear the 2-point margin + // completely; otherwise a label that just touches it never moves on. + rect = rect.translate(vec2(0.0, other.bottom() + 3.0 - rect.top())); } if rect.bottom() > clip.bottom() { continue; @@ -490,3 +492,30 @@ fn mark(p: &Painter, center: Pos2, mark: Mark, scale: f32, alpha: f32) { } } } + +#[cfg(test)] +mod tests { + use super::*; + + #[test] + fn labels_crowded_into_a_corner_stack_below_each_other() { + // At the top-left corner the left and upper positions fall outside the canvas, so every + // label after the first takes the stacking path. + let ctx = egui::Context::default(); + crate::theme::install_fonts(&ctx); + let clip = Rect::from_min_size(pos2(0.0, 0.0), vec2(400.0, 300.0)); + let label = |target: &str| Tag { + anchor: pos2(4.0, 4.0), tip: pos2(2.0, 2.0), color: Color32::LIGHT_BLUE, + name: None, phrase: Some(format!("生图 · {target}")), alpha: 1.0, + }; + let labels = [label("产品正面"), label("产品侧面"), label("细节特写")]; + let mut placed = Vec::new(); + ctx.run_ui(egui::RawInput { screen_rect: Some(clip), ..Default::default() }, |ui| { + placed = tags(ui.painter(), clip, &labels); + }).textures_delta.clear(); + assert_eq!(placed.len(), labels.len()); + for (index, rect) in placed.iter().enumerate() { + assert!(placed[index + 1..].iter().all(|other| !other.expand(2.0).intersects(*rect)), "{placed:?}"); + } + } +}