Files
2026-10-14 23:16:33 -03:00

130 lines
5.0 KiB
Rust

//! Fitted tab labels, overflow activation and animated-pill containment.
use egui::{Context, Event, Pos2, RawInput, Rect, Shape, pos2, vec2};
use notescraft_ui_egui::{
fonts, motion,
tabs::{self, Tabs, TabsResponse},
theme::{Tokens, color},
};
fn click(point: Pos2) -> Vec<Event> {
vec![
Event::PointerMoved(point),
Event::PointerButton { pos: point, button: egui::PointerButton::Primary, pressed: true, modifiers: egui::Modifiers::NONE },
Event::PointerButton { pos: point, button: egui::PointerButton::Primary, pressed: false, modifiers: egui::Modifiers::NONE },
]
}
fn frame(ctx: &Context, tokens: &Tokens, tabs: &Tabs, width: f32, events: Vec<Event>) -> (egui::FullOutput, TabsResponse) {
let mut response = TabsResponse::default();
let mut output = ctx.run_ui(RawInput { screen_rect: Some(Rect::from_min_size(Pos2::ZERO, vec2(width, 700.0))), events, ..Default::default() }, |ui| {
response = tabs::show(ui, tokens, tabs, &[]);
});
output.textures_delta.clear();
(output, response)
}
#[test]
fn overflow_selection_is_promoted_into_visible_strip() -> Result<(), String> {
let theme = notescraft_themes::builtin("catppuccin-mocha").ok_or("missing theme")?;
let tokens = Tokens::new(theme);
let ctx = Context::default();
fonts::install(&ctx);
motion::set_enabled(&ctx, false);
let mut tabs = Tabs::new();
for index in 0..8 {
let _ = tabs.open(&format!("Long note {index}.md"));
}
let _ = frame(&ctx, &tokens, &tabs, 520.0, vec![]);
let (output, _) = frame(&ctx, &tokens, &tabs, 520.0, vec![]);
let more = output
.shapes
.iter()
.find_map(|shape| match &shape.shape {
Shape::Text(text) if text.galley.text().contains("more") => Some(text.pos + text.galley.size() * 0.5),
_ => None,
})
.ok_or("missing overflow chip")?;
let _ = frame(&ctx, &tokens, &tabs, 520.0, click(more));
let (output, _) = frame(&ctx, &tokens, &tabs, 520.0, vec![]);
let hidden = output
.shapes
.iter()
.find_map(|shape| match &shape.shape {
Shape::Text(text) if text.galley.text() == "Long note 6" => Some(text.pos + text.galley.size() * 0.5),
_ => None,
})
.ok_or("hidden tab absent from menu")?;
let (_, response) = frame(&ctx, &tokens, &tabs, 520.0, click(hidden));
assert_eq!(response.selected.as_deref(), Some("Long note 6.md"));
assert!(tabs.activate(6));
let (output, _) = frame(&ctx, &tokens, &tabs, 520.0, vec![]);
let active = output
.shapes
.iter()
.find_map(|shape| match &shape.shape {
Shape::Rect(rect) if rect.fill == color(tokens.theme.colors.surface) => Some(rect.rect),
_ => None,
})
.ok_or("active tab pill absent")?;
assert!(active.right() <= 520.0 - 36.0);
Ok(())
}
#[test]
fn labels_elide_whole_glyphs_and_reserve_close_width() -> Result<(), String> {
let tokens = Tokens::new(notescraft_themes::builtin("catppuccin-mocha").ok_or("missing theme")?);
let ctx = Context::default();
fonts::install(&ctx);
motion::set_enabled(&ctx, false);
let mut tabs = Tabs::new();
for title in ["A résumé with long title.md", "NotesCraft settings.md", "Graph overview.md"] {
let _ = tabs.open(title);
}
let _ = frame(&ctx, &tokens, &tabs, 360.0, vec![]);
let (output, _) = frame(&ctx, &tokens, &tabs, 360.0, vec![]);
let elided: Vec<_> = output
.shapes
.iter()
.filter_map(|shape| match &shape.shape {
Shape::Text(text) if text.galley.elided => Some((shape.clip_rect, text)),
_ => None,
})
.collect();
assert_eq!(elided.len(), 3);
for (clip, text) in elided {
let glyphs: String = text.galley.rows.iter().flat_map(|row| row.glyphs.iter().map(|glyph| glyph.chr)).collect();
assert!(glyphs.ends_with('…'), "{glyphs}");
assert!(clip.right() < 360.0 - 36.0);
assert!(text.pos.x + text.galley.size().x <= clip.right() + 0.01);
}
Ok(())
}
#[test]
fn spring_stays_inside_strip_during_resize_and_activation() -> Result<(), String> {
let tokens = Tokens::new(notescraft_themes::builtin("catppuccin-mocha").ok_or("missing theme")?);
let ctx = Context::default();
fonts::install(&ctx);
let mut tabs = Tabs::new();
for index in 0..20 {
let _ = tabs.open(&format!("Long note {index}.md"));
}
for width in [900.0, 360.0, 520.0] {
for index in [0, 19, 2] {
assert!(tabs.activate(index));
for _ in 0..8 {
let (output, _) = frame(&ctx, &tokens, &tabs, width, vec![Event::PointerMoved(pos2(0.0, 200.0))]);
for shape in output.shapes {
if let Shape::Rect(rect) = shape.shape
&& rect.fill == color(tokens.theme.colors.surface)
{
assert!(rect.rect.left() >= 0.0 && rect.rect.right() <= width - 36.0);
}
}
}
}
}
Ok(())
}