e210b23a25
Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
102 lines
4.2 KiB
Rust
102 lines
4.2 KiB
Rust
//! Headless bookmark row, header star and command dispatch checks.
|
||
|
||
use egui::{Context, Event, FullOutput, Pos2, RawInput, Rect, Shape};
|
||
use notescraft_engine::Bookmark;
|
||
use notescraft_ui_egui::{bookmarks_view, motion, theme::Tokens};
|
||
|
||
fn context() -> Context {
|
||
let ctx = Context::default();
|
||
notescraft_ui_egui::fonts::install(&ctx);
|
||
egui_extras::install_image_loaders(&ctx);
|
||
motion::set_enabled(&ctx, false);
|
||
let mut output = ctx.run_ui(RawInput::default(), |_| {});
|
||
output.textures_delta.clear();
|
||
ctx
|
||
}
|
||
|
||
fn tokens() -> Tokens {
|
||
Tokens::new(notescraft_themes::builtin("catppuccin-mocha").expect("theme"))
|
||
}
|
||
|
||
fn input(events: Vec<Event>) -> RawInput {
|
||
RawInput { screen_rect: Some(Rect::from_min_size(Pos2::ZERO, egui::vec2(268.0, 600.0))), events, ..Default::default() }
|
||
}
|
||
|
||
fn click(position: Pos2) -> Vec<Event> {
|
||
vec![
|
||
Event::PointerMoved(position),
|
||
Event::PointerButton { pos: position, button: egui::PointerButton::Primary, pressed: true, modifiers: egui::Modifiers::NONE },
|
||
Event::PointerButton { pos: position, button: egui::PointerButton::Primary, pressed: false, modifiers: egui::Modifiers::NONE },
|
||
]
|
||
}
|
||
|
||
fn label(output: &FullOutput, wanted: &str) -> Rect {
|
||
output
|
||
.shapes
|
||
.iter()
|
||
.find_map(|shape| {
|
||
let Shape::Text(text) = &shape.shape else { return None };
|
||
(text.galley.text() == wanted).then(|| Rect::from_min_size(text.pos, text.galley.size()))
|
||
})
|
||
.unwrap_or_else(|| panic!("missing {wanted}"))
|
||
}
|
||
|
||
#[test]
|
||
fn bookmarks_use_full_width_36_point_rows_and_separate_remove_target() {
|
||
let ctx = context();
|
||
let tokens = tokens();
|
||
let rows = vec![
|
||
Bookmark { path: "folder/Alpha.md".into(), heading: None, title: Some("Saved note".into()) },
|
||
Bookmark { path: "Beta.md".into(), heading: Some("Heading".into()), title: None },
|
||
];
|
||
let draw = |events, writable| {
|
||
let mut action = None;
|
||
let mut output = ctx.run_ui(input(events), |ui| action = bookmarks_view::draw(ui, &tokens, &rows, Some("folder/Alpha.md"), writable));
|
||
output.textures_delta.clear();
|
||
(output, action)
|
||
};
|
||
let _ = draw(Vec::new(), true);
|
||
let (output, _) = draw(Vec::new(), true);
|
||
let heading = label(&output, "BOOKMARKS");
|
||
let first = label(&output, "Saved note");
|
||
let second = label(&output, "Beta › Heading");
|
||
assert!((heading.left() - 4.0).abs() < 0.1);
|
||
let active = output
|
||
.shapes
|
||
.iter()
|
||
.find_map(|shape| match &shape.shape {
|
||
Shape::Rect(shape) if (shape.rect.height() - 36.0).abs() < 0.01 && shape.fill.a() > 0 && shape.rect.contains(first.center()) => Some(shape.rect),
|
||
_ => None,
|
||
})
|
||
.expect("active bookmark row");
|
||
assert!((active.width() - 268.0).abs() < 0.1);
|
||
assert!((second.center().y - first.center().y - 40.0).abs() < 0.1);
|
||
let (_, action) = draw(click(second.center()), true);
|
||
assert_eq!(action, Some(bookmarks_view::BookmarkAction::Open(rows.get(1).expect("second").clone())));
|
||
let close = Pos2::new(active.right() - 20.0, active.center().y);
|
||
let _ = draw(vec![Event::PointerMoved(close)], true);
|
||
let (_, action) = draw(click(close), true);
|
||
assert_eq!(action, Some(bookmarks_view::BookmarkAction::Remove(rows.first().expect("first").clone())));
|
||
let _ = draw(Vec::new(), false);
|
||
let (_, action) = draw(click(close), false);
|
||
assert_eq!(action, Some(bookmarks_view::BookmarkAction::Open(rows.first().expect("first").clone())));
|
||
}
|
||
|
||
#[test]
|
||
fn header_star_has_28_point_hit_target_and_read_only_gating() {
|
||
let ctx = context();
|
||
let tokens = tokens();
|
||
let rect = Rect::from_min_size(Pos2::new(8.0, 8.0), egui::vec2(28.0, 28.0));
|
||
let draw = |events, selected, writable| {
|
||
let mut toggled = false;
|
||
let mut output = ctx.run_ui(input(events), |ui| toggled = bookmarks_view::star_toggle(ui, &tokens, rect, selected, writable));
|
||
output.textures_delta.clear();
|
||
toggled
|
||
};
|
||
assert!(!draw(Vec::new(), false, true));
|
||
assert!(draw(click(rect.center()), false, true));
|
||
assert!(draw(click(rect.center()), true, true));
|
||
assert!(!draw(click(rect.center()), true, false));
|
||
assert!(!draw(click(rect.right_bottom() + egui::vec2(2.0, 2.0)), false, true));
|
||
}
|