From b0e8b14017a04ab39fcf517cbd7cbbf01bd15a64 Mon Sep 17 00:00:00 2001 From: Belligerently Date: Wed, 4 Nov 2026 20:57:57 -0500 Subject: [PATCH] Reveal node search selection after query changes and reopening --- crates/ui-egui/src/graph_view.rs | 4 +- crates/ui-egui/tests/common/search.rs | 40 +++++++++++++++ crates/ui-egui/tests/search_query.rs | 71 +++++++++++++++++++++++++++ 3 files changed, 114 insertions(+), 1 deletion(-) create mode 100644 crates/ui-egui/tests/common/search.rs create mode 100644 crates/ui-egui/tests/search_query.rs diff --git a/crates/ui-egui/src/graph_view.rs b/crates/ui-egui/src/graph_view.rs index ee877da9..8b2b3652 100644 --- a/crates/ui-egui/src/graph_view.rs +++ b/crates/ui-egui/src/graph_view.rs @@ -1085,6 +1085,7 @@ fn search_popup(app: &mut MaterialCraftApp, ui: &mut Ui, cam: &Cam) { egui::Area::new(egui::Id::unique("node-search")).fixed_pos(screen).order(egui::Order::Foreground).show(ui.ctx(), |ui| { egui::Frame::popup(ui.style()).show(ui, |ui| { ui.set_width(260.0); + let mut reveal_selection = s.focus; ui.horizontal(|ui| { ui.label(egui::RichText::new(icon::MAGNIFYING_GLASS).color(tk.text_dim)); let r = ui.add(egui::TextEdit::singleline(&mut s.text).hint_text(tr("Search nodes...")).desired_width(220.0)); @@ -1095,6 +1096,7 @@ fn search_popup(app: &mut MaterialCraftApp, ui: &mut Ui, cam: &Cam) { } if r.changed() { s.sel = 0; + reveal_selection = true; } }); let list = matches(&s.text); @@ -1119,7 +1121,7 @@ fn search_popup(app: &mut MaterialCraftApp, ui: &mut Ui, cam: &Cam) { egui::Button::selectable(i == s.sel, format!("{} ", d.label)) .right_text(egui::RichText::new(d.group()).size(10.0).color(tk.text_dim)), ); - if i == s.sel && (up || down) { + if i == s.sel && (up || down || reveal_selection) { r.scroll_to_me(None); } if r.clicked() { diff --git a/crates/ui-egui/tests/common/search.rs b/crates/ui-egui/tests/common/search.rs new file mode 100644 index 00000000..63735d78 --- /dev/null +++ b/crates/ui-egui/tests/common/search.rs @@ -0,0 +1,40 @@ +use egui::{Event, Pos2, Rect, epaint::Shape, vec2}; +use egui_kittest::Harness; +use materialcraft_ui_egui::{MaterialCraftApp, UiState}; + +pub fn harness(render: bool) -> Harness<'static, MaterialCraftApp> { + let app = MaterialCraftApp::headless(materialcraft_engine::new_session(), UiState::default()); + let builder = Harness::builder().with_size(vec2(1600.0, 960.0)).with_step_dt(1.0 / 60.0); + let builder = if render { builder.wgpu() } else { builder }; + let mut h = builder.build_ui_state( + |ui, app: &mut MaterialCraftApp| { + app.step(&ui.ctx().clone()); + app.draw(ui); + }, + app, + ); + h.run_steps(8); + let pointer = h.state().graph.view.center(); + h.event(Event::PointerMoved(pointer)); + h.step(); + h.key_press(egui::Key::Tab); + h.step(); + h.run_steps(3); + assert!(h.ctx.memory(|m| m.area_rect(egui::Id::unique("node-search"))).is_some()); + assert!(label_position(&h, "Bitmap").is_some(), "fresh search must show its first result"); + h +} + +pub fn label_position(h: &Harness<'static, MaterialCraftApp>, label: &str) -> Option { + let popup = h.ctx.memory(|m| m.area_rect(egui::Id::unique("node-search"))).unwrap(); + fn find(shape: &Shape, clip: Rect, popup: Rect, label: &str) -> Option { + match shape { + Shape::Text(t) if clip.contains(t.pos) && popup.contains(t.pos) && t.galley.text().trim() == label => { + Some(t.pos + vec2(0.0, t.galley.size().y * 0.5)) + } + Shape::Vec(v) => v.iter().find_map(|s| find(s, clip, popup, label)), + _ => None, + } + } + h.output().shapes.iter().find_map(|s| find(&s.shape, s.clip_rect, popup, label)) +} diff --git a/crates/ui-egui/tests/search_query.rs b/crates/ui-egui/tests/search_query.rs new file mode 100644 index 00000000..3e9ea102 --- /dev/null +++ b/crates/ui-egui/tests/search_query.rs @@ -0,0 +1,71 @@ +//! Resetting the search selection must also reveal it in a previously scrolled list. +#[path = "common/search.rs"] +mod search; +use egui::{Event, Key, Modifiers}; +use egui_kittest::{Harness, kittest::Queryable}; +use materialcraft_ui_egui::MaterialCraftApp; + +fn scroll(h: &mut Harness<'static, MaterialCraftApp>, rows: usize) { + for _ in 0..rows { + h.key_press(Key::ArrowDown); + h.step(); + } + h.run_steps(25); +} +fn filter_after_scrolling(h: &mut Harness<'static, MaterialCraftApp>) { + scroll(h, 45); + h.event(Event::Text("noises".into())); + h.step(); + h.run_steps(25); + assert!(search::label_position(h, "Anisotropic Noise").is_some(), "selected first filtered result is offscreen"); +} +fn assert_inserted(h: &mut Harness<'static, MaterialCraftApp>, def: &str) { + h.key_press(Key::Enter); + h.step(); + let g = h.state().session.graph().unwrap(); + assert_eq!(g.nodes.len(), 1); + assert_eq!(g.nodes[0].def, def); +} +#[test] +fn filtering_after_scrolling_reveals_and_inserts_first_match() { + let mut h = search::harness(false); + filter_after_scrolling(&mut h); + assert_inserted(&mut h, "anisotropic_noise"); +} +#[test] +fn clearing_a_scrolled_query_reveals_and_inserts_first_match() { + let mut h = search::harness(false); + h.event(Event::Text("noises".into())); + h.step(); + scroll(&mut h, 15); + h.key_press_modifiers(Modifiers::COMMAND, Key::A); + h.step(); + h.key_press(Key::Backspace); + h.step(); + h.run_steps(25); + assert!(search::label_position(&h, "Bitmap").is_some(), "selected first result is offscreen after clearing"); + assert_inserted(&mut h, "bitmap"); +} +#[test] +fn reopening_a_scrolled_search_reveals_first_match() { + let mut h = search::harness(false); + scroll(&mut h, 45); + h.key_press(Key::Escape); + h.step(); + h.run_steps(3); + h.key_press(Key::Tab); + h.step(); + h.run_steps(25); + assert!(h.get_by_label("Search nodes").rect().width() > 0.0); + assert!(search::label_position(&h, "Bitmap").is_some(), "selected first result is offscreen after reopening"); + assert_inserted(&mut h, "bitmap"); +} +#[test] +#[ignore = "requires an offscreen GPU"] +fn filtered_first_result_screenshot() { + let mut h = search::harness(true); + filter_after_scrolling(&mut h); + let dir = std::path::PathBuf::from(std::env::var_os("MC_SNAPSHOT_DIR").expect("set MC_SNAPSHOT_DIR")); + std::fs::create_dir_all(&dir).unwrap(); + h.render().unwrap().save(dir.join("filtered-first-result.png")).unwrap(); +}