修复多个光标标签挤在角落时界面线程卡死
egui 的 Rect::intersects 把相贴也算相交。标签往下排时只挪到另一标签外扩 2 点的边上,会一直判为相交且不再移动,界面线程卡死在循环里(第二十二轮实测 04:08 起界面无响应)。改为越过外扩边距再放,并补测试。
This commit is contained in:
parent
6749900c89
commit
27a646d2b9
@ -137,7 +137,9 @@ pub(super) fn tags(p: &Painter, clip: Rect, tags: &[Tag]) -> Vec<Rect> {
|
||||
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:?}");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user