mirror of
https://github.com/storytold/materialcraft.git
synced 2026-11-19 14:01:02 +01:00
fix(ui): node search, the add-node filter and sample tiles stop behind a dialog
Node search takes no keys, focus or outside clicks while input is blocked and does not open behind a blocker. The add-node menu's letter filter does not change while blocked. Sample tiles are disabled behind a dialog. A file dialog keeps its commands enabled only when nothing else blocks input. The control protocol notes that the installed-update notice does not block.
This commit is contained in:
1 parent
49d8d2e16b
commit
2eea5eef2c
9 files changed
+144
-28
No files matched your search
@@ -11,20 +11,22 @@ pub(crate) fn show(app: &MaterialCraftApp, ui: &mut Ui, entries: impl IntoIterat
|
||||
let previous = ui.ctx().data_mut(|data| data.get_temp::<(u64, Option<char>)>(filter_id));
|
||||
let mut filter = previous.filter(|(last_frame, _)| frame <= last_frame.saturating_add(1)).and_then(|(_, letter)| letter);
|
||||
|
||||
ui.input(|input| {
|
||||
for event in &input.events {
|
||||
let Event::Key { key, pressed: true, modifiers, .. } = event else { continue };
|
||||
if modifiers.ctrl || modifiers.alt || modifiers.command {
|
||||
continue;
|
||||
}
|
||||
if !blocked {
|
||||
ui.input(|input| {
|
||||
for event in &input.events {
|
||||
let Event::Key { key, pressed: true, modifiers, .. } = event else { continue };
|
||||
if modifiers.ctrl || modifiers.alt || modifiers.command {
|
||||
continue;
|
||||
}
|
||||
|
||||
if *key == Key::Backspace {
|
||||
filter = None;
|
||||
} else if key.name().len() == 1 {
|
||||
filter = key.name().chars().next().map(|letter| letter.to_ascii_lowercase());
|
||||
if *key == Key::Backspace {
|
||||
filter = None;
|
||||
} else if key.name().len() == 1 {
|
||||
filter = key.name().chars().next().map(|letter| letter.to_ascii_lowercase());
|
||||
}
|
||||
}
|
||||
}
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
let mut selected = None;
|
||||
let enter = filter.is_some() && ui.input(|input| input.key_pressed(Key::Enter));
|
||||
@@ -95,6 +97,24 @@ mod tests {
|
||||
assert!(rows.windows(2).all(|pair| pair[0] < pair[1]), "catalog order must survive rendering: {rows:?}");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn letters_typed_behind_a_prompt_do_not_become_the_filter() {
|
||||
let mut harness = Harness::builder().build_ui_state(
|
||||
|ui, app: &mut crate::MaterialCraftApp| {
|
||||
super::show(app, ui, [("svg", "SVG", "Atomic nodes"), ("grayscale", "Grayscale conversion", "Atomic nodes")]);
|
||||
},
|
||||
app(),
|
||||
);
|
||||
harness.run_steps(3);
|
||||
harness.state_mut().ui.windows.unsaved = Some(crate::unsaved::Guarded::Quit);
|
||||
harness.run_steps(3);
|
||||
harness.key_press(egui::Key::S);
|
||||
harness.run_steps(3);
|
||||
harness.state_mut().ui.windows.unsaved = None;
|
||||
harness.run_steps(3);
|
||||
assert!(!harness.get(By::new().role(egui::accesskit::Role::Button).label("Grayscale conversion")).accesskit_node().is_disabled());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn letters_disable_other_initials_without_removing_entries() {
|
||||
let app = app();
|
||||
|
||||
@@ -134,7 +134,7 @@ pub(super) fn handle(
|
||||
} else if let Some(position) = pointer
|
||||
&& rect.contains(position)
|
||||
{
|
||||
open_search(app, camera.to_graph(position), Some(from));
|
||||
open_search(app, &context, camera.to_graph(position), Some(from));
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -196,7 +196,7 @@ pub(super) fn handle(
|
||||
app.run("graph.wire.insertDot", json!({"to": wire.0.0, "toPort": wire.1, "at": [at.x, at.y]}));
|
||||
app.graph.selected_wires.clear();
|
||||
}
|
||||
None => open_search(app, camera.to_graph(position), None),
|
||||
None => open_search(app, &context, camera.to_graph(position), None),
|
||||
}
|
||||
}
|
||||
|
||||
@@ -258,7 +258,7 @@ pub(super) fn handle(
|
||||
if (space || tab)
|
||||
&& let Some(position) = pointer
|
||||
{
|
||||
open_search(app, camera.to_graph(position), None);
|
||||
open_search(app, &context, camera.to_graph(position), None);
|
||||
}
|
||||
if GraphCamera::frame_key(ui, rect) {
|
||||
let selected = !app.session.selection.is_empty();
|
||||
|
||||
@@ -102,3 +102,51 @@ fn enter_in_the_node_search_waits_behind_a_prompt() {
|
||||
frames.run(vec![press(Key::Enter)], Modifiers::NONE);
|
||||
assert_eq!(frames.app.session.graph().unwrap().nodes.len(), nodes + 1, "Enter works once the prompt is answered");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn the_node_search_takes_no_keys_behind_a_prompt() {
|
||||
let mut frames = canvas("graph");
|
||||
frames.run(vec![press(Key::Space)], Modifiers::NONE);
|
||||
frames.run(Vec::new(), Modifiers::NONE);
|
||||
assert!(frames.app.graph.search.is_some());
|
||||
|
||||
frames.app.ui.windows.unsaved = Some(Guarded::Quit);
|
||||
frames.run(Vec::new(), Modifiers::NONE);
|
||||
frames.run(vec![press(Key::ArrowDown)], Modifiers::NONE);
|
||||
assert_eq!(frames.app.graph.search.as_ref().unwrap().sel, 0, "arrows wait behind the prompt");
|
||||
frames.run(vec![Event::Text("x".into())], Modifiers::NONE);
|
||||
assert_eq!(frames.app.graph.search.as_ref().unwrap().text, "", "typing waits behind the prompt");
|
||||
|
||||
frames.run(vec![press(Key::Escape)], Modifiers::NONE);
|
||||
assert!(frames.app.graph.search.is_some(), "Escape answers the prompt and leaves the search open");
|
||||
|
||||
frames.app.ui.windows.unsaved = None;
|
||||
frames.run(Vec::new(), Modifiers::NONE);
|
||||
frames.run(Vec::new(), Modifiers::NONE);
|
||||
frames.run(vec![Event::Text("n".into())], Modifiers::NONE);
|
||||
assert_eq!(frames.app.graph.search.as_ref().unwrap().text, "n", "the search has the keyboard once the prompt is answered");
|
||||
frames.run(vec![press(Key::Escape)], Modifiers::NONE);
|
||||
assert!(frames.app.graph.search.is_none(), "Escape closes the search once the prompt is answered");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn a_double_click_does_not_open_the_node_search_behind_a_prompt() {
|
||||
let double_click = |frames: &mut Frames| {
|
||||
let pos = corner(frames);
|
||||
frames.time += 1.0;
|
||||
frames.run(vec![Event::PointerMoved(pos)], Modifiers::NONE);
|
||||
for pressed in [true, false, true, false] {
|
||||
frames.run(vec![Event::PointerButton { pos, button: egui::PointerButton::Primary, pressed, modifiers: Modifiers::NONE }], Modifiers::NONE);
|
||||
}
|
||||
};
|
||||
let mut frames = canvas("graph");
|
||||
frames.app.ui.windows.unsaved = Some(Guarded::Quit);
|
||||
frames.run(Vec::new(), Modifiers::NONE);
|
||||
double_click(&mut frames);
|
||||
assert!(frames.app.graph.search.is_none());
|
||||
|
||||
frames.app.ui.windows.unsaved = None;
|
||||
frames.run(Vec::new(), Modifiers::NONE);
|
||||
double_click(&mut frames);
|
||||
assert!(frames.app.graph.search.is_some(), "a double-click opens the search once the prompt is answered");
|
||||
}
|
||||
@@ -5,7 +5,10 @@ use egui_phosphor::regular as icon;
|
||||
use materialcraft_engine::core::NodeId;
|
||||
use serde_json::json;
|
||||
|
||||
pub(super) fn open_search(app: &mut MaterialCraftApp, at: Pos2, connect: Option<(NodeId, String)>) {
|
||||
pub(super) fn open_search(app: &mut MaterialCraftApp, ctx: &egui::Context, at: Pos2, connect: Option<(NodeId, String)>) {
|
||||
if crate::shell::modality::Modality::capture(app, ctx).is_blocked() {
|
||||
return;
|
||||
}
|
||||
app.graph.search = Some(NodeSearch { at, text: String::new(), sel: 0, connect, focus: true });
|
||||
}
|
||||
|
||||
@@ -27,6 +30,7 @@ pub(super) fn matches(text: &str) -> Vec<&'static materialcraft_engine::nodes::N
|
||||
}
|
||||
|
||||
pub(super) fn show(app: &mut MaterialCraftApp, ui: &mut Ui, cam: &GraphCamera) {
|
||||
let blocked = crate::shell::modality::Modality::capture(app, ui.ctx()).is_blocked();
|
||||
let Some(s) = app.graph.search.as_mut() else { return };
|
||||
|
||||
let tk = Tokens::get(ui.ctx());
|
||||
@@ -36,12 +40,16 @@ pub(super) fn show(app: &mut MaterialCraftApp, ui: &mut Ui, cam: &GraphCamera) {
|
||||
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);
|
||||
if blocked {
|
||||
ui.disable();
|
||||
s.focus = true;
|
||||
}
|
||||
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));
|
||||
crate::widgets::name(&r, egui::Role::SearchInput, "Search nodes");
|
||||
if s.focus {
|
||||
if s.focus && !blocked {
|
||||
r.request_focus();
|
||||
s.focus = false;
|
||||
}
|
||||
@@ -52,7 +60,8 @@ pub(super) fn show(app: &mut MaterialCraftApp, ui: &mut Ui, cam: &GraphCamera) {
|
||||
});
|
||||
let list = matches(&s.text);
|
||||
let (up, down, enter, esc) = ui.input(|i| {
|
||||
(i.key_pressed(egui::Key::ArrowUp), i.key_pressed(egui::Key::ArrowDown), i.key_pressed(egui::Key::Enter), i.key_pressed(egui::Key::Escape))
|
||||
let pressed = |key| !blocked && i.key_pressed(key);
|
||||
(pressed(egui::Key::ArrowUp), pressed(egui::Key::ArrowDown), pressed(egui::Key::Enter), pressed(egui::Key::Escape))
|
||||
});
|
||||
if down {
|
||||
s.sel = (s.sel + 1).min(list.len().saturating_sub(1));
|
||||
@@ -86,7 +95,8 @@ pub(super) fn show(app: &mut MaterialCraftApp, ui: &mut Ui, cam: &GraphCamera) {
|
||||
});
|
||||
});
|
||||
});
|
||||
let clicked_out = ui.input(|i| i.pointer.any_pressed())
|
||||
let clicked_out = !blocked
|
||||
&& ui.input(|i| i.pointer.any_pressed())
|
||||
&& ui.ctx().pointer_interact_pos().is_some_and(|p| !ui.ctx().layer_id_at(p).is_some_and(|l| l.id == egui::Id::unique("node-search")));
|
||||
if let Some(def) = pick.filter(|_| !crate::shell::modality::Modality::capture(app, ui.ctx()).is_blocked()) {
|
||||
let s = app.graph.search.take();
|
||||
|
||||
@@ -58,11 +58,14 @@ pub(crate) enum Modality {
|
||||
}
|
||||
|
||||
impl Modality {
|
||||
/// Reads the app's own state before egui's modal layer, which reports the previous frame
|
||||
/// Reads the app's own state before egui's modal layer, which reports the previous frame. A
|
||||
/// file dialog is reported only when nothing else blocks input
|
||||
pub(crate) fn capture(app: &MaterialCraftApp, ctx: &egui::Context) -> Modality {
|
||||
match open_blocker(app) {
|
||||
Some(blocker) => Modality::Blocked(blocker),
|
||||
None if ctx.memory(|memory| memory.top_modal_layer()).is_some() => Modality::Blocked(Blocker::Modal),
|
||||
#[cfg(not(target_arch = "wasm32"))]
|
||||
None if app.dialogs.is_open() || app.dialogs.queued() > 0 => Modality::Blocked(Blocker::FileDialog),
|
||||
None => Modality::Free,
|
||||
}
|
||||
}
|
||||
@@ -86,13 +89,8 @@ fn open_blocker(app: &MaterialCraftApp) -> Option<Blocker> {
|
||||
return Some(Blocker::UnsavedPrompt);
|
||||
}
|
||||
#[cfg(not(target_arch = "wasm32"))]
|
||||
{
|
||||
if app.dialogs.is_open() || app.dialogs.queued() > 0 {
|
||||
return Some(Blocker::FileDialog);
|
||||
}
|
||||
if crate::updates_ui::blocks_input(app) {
|
||||
return Some(Blocker::Update);
|
||||
}
|
||||
if crate::updates_ui::blocks_input(app) {
|
||||
return Some(Blocker::Update);
|
||||
}
|
||||
#[cfg(any(test, target_arch = "wasm32"))]
|
||||
if windows.package_save.is_some() {
|
||||
|
||||
@@ -67,6 +67,34 @@ fn a_blocker_lets_only_its_own_commands_through() {
|
||||
assert!(!file_dialog.allows("edit.delete"));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn a_file_dialog_frees_its_commands_only_when_nothing_else_blocks() {
|
||||
let queued = |app: &mut MaterialCraftApp| app.dialogs.ask(craft_dialogs::FileDialog::open(), crate::native_dialogs::Then::OpenPackage);
|
||||
|
||||
let mut preferences = app();
|
||||
queued(&mut preferences);
|
||||
preferences.ui.windows.preferences = true;
|
||||
assert_eq!(capture(&preferences), Modality::Blocked(Blocker::Dialog));
|
||||
assert!(!capture(&preferences).allows("file.open"));
|
||||
|
||||
let mut update = app();
|
||||
queued(&mut update);
|
||||
crate::updates_ui::open_prompt_for_test(&mut update);
|
||||
assert_eq!(capture(&update), Modality::Blocked(Blocker::Update));
|
||||
|
||||
let mut modal = app();
|
||||
queued(&mut modal);
|
||||
let ctx = egui::Context::default();
|
||||
ctx.run_ui(egui::RawInput::default(), |ui| {
|
||||
egui::Modal::new(egui::Id::unique("modal")).show(ui.ctx(), |_| {});
|
||||
})
|
||||
.textures_delta
|
||||
.clear();
|
||||
let mut answer = Modality::Free;
|
||||
ctx.run_ui(egui::RawInput::default(), |ui| answer = Modality::capture(&modal, ui.ctx())).textures_delta.clear();
|
||||
assert_eq!(answer, Modality::Blocked(Blocker::Modal));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn a_modal_layer_from_the_previous_frame_blocks() {
|
||||
let app = app();
|
||||
|
||||
@@ -395,6 +395,9 @@ fn home(app: &mut MaterialCraftApp, ctx: &egui::Context) {
|
||||
ui.add_space(8.0);
|
||||
ui.label(RichText::new(tr("Samples")).strong());
|
||||
ui.horizontal_wrapped(|ui| {
|
||||
if !free {
|
||||
ui.disable();
|
||||
}
|
||||
for (id, label) in materialcraft_engine::samples::TEMPLATES.iter().filter(|(id, _)| !matches!(*id, "empty" | "pbr")) {
|
||||
let (r, resp) = ui.allocate_exact_size(vec2(92.0, 116.0), egui::Sense::click());
|
||||
crate::widgets::name(&resp, egui::Role::ListItem, label);
|
||||
|
||||
@@ -70,6 +70,15 @@ mod home_modality_tests {
|
||||
assert_eq!(harness.state().session.pkg.graphs.len(), graphs);
|
||||
assert!(harness.state().ui.windows.home);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn sample_tiles_are_disabled_only_behind_a_dialog() {
|
||||
let (_, sample) = materialcraft_engine::samples::TEMPLATES.iter().find(|(id, _)| !matches!(*id, "empty" | "pbr")).unwrap();
|
||||
let tile = By::new().role(egui::accesskit::Role::ListItem).label(sample);
|
||||
assert!(!harness(|_| {}).get(tile).accesskit_node().is_disabled());
|
||||
let tile = By::new().role(egui::accesskit::Role::ListItem).label(sample);
|
||||
assert!(harness(|app| app.ui.windows.about = true).get(tile).accesskit_node().is_disabled());
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
|
||||
@@ -41,7 +41,7 @@ when resaving a file from an older release. `formatVersion` identifies the SBS s
|
||||
| `material.export {path, graph?, size?, values?}` | headless | write a portable `.mcmat` below the write root |
|
||||
| `render {view?, width?, height?, yaw?, pitch?, scene?, node?, port?, full?, tiling?, hq?, displacement?}` | headless | PNG (base64) of the 3D view or of one node; `hq` is the beauty render (displaced mesh, ray-traced AO, shadowed key light, 3×3 supersampling), `displacement` its depth in scene radii |
|
||||
| `ui.inspect` | app | UI state, the graph view rectangle, every node's screen rectangle, every port's identifier, display label and screen position, fps, toasts |
|
||||
| `ui.menu.list` / `ui.menu.invoke {id}` | app | the menu catalog; behave like a click. While a dialog or prompt is open (an app dialog, the unsaved-changes prompt, a file dialog, or the update prompt or result window; not Home or the Shortcuts list), an item lists as disabled and `ui.menu.invoke` returns an error. Behind a file dialog, the items that show a file dialog stay enabled and bring it forward |
|
||||
| `ui.menu.list` / `ui.menu.invoke {id}` | app | the menu catalog; behave like a click. While a dialog or prompt is open (an app dialog, the unsaved-changes prompt, a file dialog, or the update prompt or result window; not Home, the Shortcuts list or the notice that an update installed), an item lists as disabled and `ui.menu.invoke` returns an error. When a file dialog is the only one open, the items that show a file dialog stay enabled and bring it forward |
|
||||
| `ui.set {...}` | app | recursively merge fields into the UI state |
|
||||
| `ui.input {events}` | app | synthetic input, below |
|
||||
| `view.screenshot {view?: "3d"\|"2d"}` | app | PNG of the 3D or 2D view |
|
||||
|
||||
Reference in new issue
Block a user