From a1af5a5cc8b73c75628286b2edc1f312d5b134f0 Mon Sep 17 00:00:00 2001
From: Belligerently
Date: Thu, 5 Nov 2026 19:05:00 -0500
Subject: [PATCH 01/20] Handle platform clipboard events for node shortcuts
---
crates/ui-egui/src/graph_view.rs | 6 +-
crates/ui-egui/src/menus.rs | 28 +++-
crates/ui-egui/tests/clipboard_shortcuts.rs | 150 ++++++++++++++++++++
3 files changed, 180 insertions(+), 4 deletions(-)
create mode 100644 crates/ui-egui/tests/clipboard_shortcuts.rs
diff --git a/crates/ui-egui/src/graph_view.rs b/crates/ui-egui/src/graph_view.rs
index 285171c..06a7a2d 100644
--- a/crates/ui-egui/src/graph_view.rs
+++ b/crates/ui-egui/src/graph_view.rs
@@ -1258,7 +1258,11 @@ fn node_menu(app: &mut MaterialCraftApp, ui: &mut Ui, id: NodeId) {
}
let mut item = |ui: &mut Ui, label: &str, cmd: &str, params: serde_json::Value| {
if ui.button(tr(label)).clicked() {
- app.run(cmd, params);
+ if cmd == "edit.copy" {
+ crate::menus::invoke(app, ui.ctx(), cmd);
+ } else {
+ app.run(cmd, params);
+ }
ui.close();
}
};
diff --git a/crates/ui-egui/src/menus.rs b/crates/ui-egui/src/menus.rs
index 3878d45..616a054 100644
--- a/crates/ui-egui/src/menus.rs
+++ b/crates/ui-egui/src/menus.rs
@@ -119,6 +119,14 @@ pub fn invoke(app: &mut MaterialCraftApp, ctx: &egui::Context, id: &str) {
return;
}
match id {
+ "edit.copy" | "edit.cut" => {
+ if let Some(result) = app.run(id, Value::Null)
+ && let Some(clip) = result.get("clip")
+ {
+ // Native paste emits no event when the system clipboard is empty.
+ ctx.copy_text(clip.to_string());
+ }
+ }
"ui.newGraph" => app.ui.windows.new_graph = true,
"ui.export" => app.ui.windows.export = true,
"ui.quit" => ctx.send_viewport_cmd(egui::ViewportCommand::Close),
@@ -248,7 +256,22 @@ pub fn shortcuts(app: &mut MaterialCraftApp, ctx: &egui::Context) {
if ctx.egui_wants_keyboard_input() {
return;
}
- if !ctx.input(|i| i.events.iter().any(|e| matches!(e, egui::Event::Key { pressed: true, .. }))) {
+ let mut fire: Vec<&str> = Vec::new();
+ // Native and web backends translate clipboard chords into these events rather than keys.
+ // Leave them for TextEdit when it owns the keyboard (the guard above).
+ ctx.input_mut(|i| {
+ i.events.retain(|event| {
+ let id = match event {
+ egui::Event::Copy => "edit.copy",
+ egui::Event::Cut => "edit.cut",
+ egui::Event::Paste(_) => "edit.paste",
+ _ => return true,
+ };
+ fire.push(id);
+ false
+ });
+ });
+ if fire.is_empty() && !ctx.input(|i| i.events.iter().any(|e| matches!(e, egui::Event::Key { pressed: true, .. }))) {
return;
}
// Selected wires take Delete and Backspace before the node commands do, and X swaps them.
@@ -270,10 +293,9 @@ pub fn shortcuts(app: &mut MaterialCraftApp, ctx: &egui::Context) {
}
// Longer chords first, so Ctrl+Shift+S is not taken by Ctrl+S.
table.sort_by_key(|(_, sc)| std::cmp::Reverse(sc.modifiers.ctrl as u8 + sc.modifiers.shift as u8 + sc.modifiers.alt as u8 + sc.modifiers.command as u8));
- let mut fire: Vec<&str> = Vec::new();
ctx.input_mut(|i| {
for (id, sc) in &table {
- if i.consume_shortcut(sc) {
+ if i.consume_shortcut(sc) && !fire.contains(id) {
fire.push(*id);
}
}
diff --git a/crates/ui-egui/tests/clipboard_shortcuts.rs b/crates/ui-egui/tests/clipboard_shortcuts.rs
new file mode 100644
index 0000000..af10a33
--- /dev/null
+++ b/crates/ui-egui/tests/clipboard_shortcuts.rs
@@ -0,0 +1,150 @@
+use egui::{Event, Key, Modifiers, OutputCommand, vec2};
+use egui_kittest::{Harness, kittest::Queryable};
+use materialcraft_ui_egui::{MaterialCraftApp, UiState};
+use serde_json::json;
+
+fn harness(render: bool) -> Harness<'static, MaterialCraftApp> {
+ let mut app = MaterialCraftApp::headless(materialcraft_engine::new_session(), UiState::default());
+ app.session.preview_size = Some(32);
+ let source = app.run("graph.node.add", json!({"def": "uniform", "at": [0, 0]})).unwrap()["id"].as_u64().unwrap();
+ let levels = app.run("graph.node.add", json!({"def": "levels", "connectFrom": {"node": source}})).unwrap()["id"].as_u64().unwrap();
+ app.run("graph.select", json!({"ids": [source, levels]})).unwrap();
+ let builder = Harness::builder().with_size(vec2(1600.0, 960.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);
+ h
+}
+
+fn copied_text(h: &Harness<'static, MaterialCraftApp>) -> String {
+ h.output()
+ .platform_output
+ .commands
+ .iter()
+ .find_map(|c| if let OutputCommand::CopyText(text) = c { Some(text.clone()) } else { None })
+ .expect("node copy must populate the system clipboard so native paste generates an event")
+}
+
+fn copy_paste_cut(h: &mut Harness<'static, MaterialCraftApp>) {
+ h.event(Event::Copy);
+ h.step();
+ assert!(h.state().session.clipboard.is_some(), "native Copy event did not copy nodes");
+ let payload = copied_text(h);
+ assert_eq!(serde_json::from_str::(&payload).unwrap()["nodes"].as_array().unwrap().len(), 2);
+ h.event(Event::Paste(payload));
+ h.step();
+ assert_eq!(h.state().session.graph().unwrap().nodes.len(), 4);
+ assert_eq!(h.state().session.graph().unwrap().connections.len(), 2);
+ h.event(Event::Cut);
+ h.step();
+ assert_eq!(h.state().session.graph().unwrap().nodes.len(), 2);
+ let payload = copied_text(h);
+ h.event(Event::Paste(payload));
+ h.step();
+ assert_eq!(h.state().session.graph().unwrap().nodes.len(), 4);
+ assert_eq!(h.state().session.graph().unwrap().connections.len(), 2);
+ h.state_mut().run("edit.undo", json!({})).unwrap();
+ assert_eq!(h.state().session.graph().unwrap().nodes.len(), 2);
+ h.state_mut().run("edit.redo", json!({})).unwrap();
+ assert_eq!(h.state().session.graph().unwrap().nodes.len(), 4);
+}
+
+#[test]
+fn native_clipboard_events_edit_nodes_and_preserve_links() {
+ copy_paste_cut(&mut harness(false));
+}
+
+#[test]
+fn menu_copy_also_populates_system_clipboard() {
+ let mut h = harness(false);
+ h.get_by_label("Edit").click();
+ h.run_steps(4);
+ h.get_by_label("Copy Ctrl+C").click();
+ h.step();
+ assert!(!copied_text(&h).is_empty());
+}
+
+fn chord(key: Key) -> Event {
+ Event::Key { key, physical_key: None, pressed: true, repeat: false, modifiers: Modifiers::COMMAND }
+}
+
+#[test]
+fn mixed_clipboard_and_key_events_do_not_paste_twice() {
+ let mut h = harness(false);
+ h.input_mut().events.extend([Event::ModifiersChanged(Modifiers::COMMAND), Event::Copy, chord(Key::C)]);
+ h.step();
+ let payload = copied_text(&h);
+ h.input_mut().events.extend([Event::Paste(payload), chord(Key::V)]);
+ h.step();
+ assert_eq!(h.state().session.graph().unwrap().nodes.len(), 4);
+}
+
+#[test]
+fn raw_command_chords_continue_to_edit_nodes() {
+ let mut h = harness(false);
+ h.input_mut().events.extend([Event::ModifiersChanged(Modifiers::COMMAND), chord(Key::C)]);
+ h.step();
+ assert!(!copied_text(&h).is_empty());
+ h.input_mut().events.push(chord(Key::V));
+ h.step();
+ assert_eq!(h.state().session.graph().unwrap().nodes.len(), 4);
+ h.input_mut().events.push(chord(Key::X));
+ h.step();
+ assert_eq!(h.state().session.graph().unwrap().nodes.len(), 2);
+}
+
+#[test]
+fn node_context_copy_populates_system_clipboard() {
+ let mut h = harness(false);
+ let pos = h.state().graph.rects[0].1.center();
+ h.event(Event::PointerMoved(pos));
+ h.step();
+ for pressed in [true, false] {
+ h.event(Event::PointerButton { pos, button: egui::PointerButton::Secondary, pressed, modifiers: Modifiers::NONE });
+ h.step();
+ }
+ h.run_steps(4);
+ h.get_by_label("Copy selection").click();
+ h.step();
+ assert!(!copied_text(&h).is_empty());
+}
+
+#[test]
+fn clipboard_events_keep_text_editing_and_do_not_touch_nodes() {
+ let mut h = harness(false);
+ h.get_by_label("Node label").click();
+ h.step();
+ h.key_press_modifiers(Modifiers::COMMAND, Key::A);
+ h.step();
+ h.event(Event::Paste("Edited label".into()));
+ h.step();
+ let selected = *h.state().session.selection.last().unwrap();
+ assert_eq!(h.state().session.graph().unwrap().node(selected).unwrap().label, "Edited label");
+ h.key_press_modifiers(Modifiers::COMMAND, Key::A);
+ h.step();
+ h.event(Event::Copy);
+ h.step();
+ assert_eq!(copied_text(&h), "Edited label");
+ assert!(h.state().session.clipboard.is_none());
+ h.event(Event::Cut);
+ h.step();
+ assert_eq!(h.state().session.graph().unwrap().nodes.len(), 2);
+ assert_eq!(h.state().session.graph().unwrap().node(selected).unwrap().label, "");
+}
+
+#[test]
+#[ignore = "requires an offscreen GPU"]
+fn clipboard_graph_snapshot() {
+ let mut h = harness(true);
+ copy_paste_cut(&mut h);
+ h.run_steps(10);
+ let dir = std::path::PathBuf::from(std::env::var_os("MC_SNAPSHOT_DIR").expect("MC_SNAPSHOT_DIR"));
+ std::fs::create_dir_all(&dir).unwrap();
+ h.render().unwrap().save(dir.join("clipboard-shortcuts.png")).unwrap();
+}
From 49d8d2e16b367cdb5c7984f3876d00808e5278c7 Mon Sep 17 00:00:00 2001
From: dotCooCoo
Date: Mon, 9 Nov 2026 05:07:30 -0800
Subject: [PATCH 02/20] fix(ui): shortcuts and floating OS windows stop while a
dialog or prompt is open
Delete edited the package behind the Save, Discard, Cancel prompt, and
floating OS windows stayed live behind every dialog, because no single
function said whether input was blocked.
Modality::capture answers it from the app's dialogs, the unsaved prompt,
native file dialogs, the update prompt and result window, and egui's
modal layer. Shortcuts, Graph, Function and FX-Map keys, the node search
and add menus, file drops, floating OS windows, Home's actions and menu
items all read it. Home and the Shortcuts list do not block.
Behind a file dialog, the commands that show one still run, so File >
Open brings a hidden macOS Open panel forward.
ui.menu.invoke now returns an error behind a dialog instead of ok,
ui.menu.list reports those items as disabled, and package_save without a
path in bridge mode returns the same error. command.run is unchanged.
Floating panels drawn inside the main window, as on the web build, still
take clicks behind a dialog until the dialogs use egui's modal layer.
---
.../src/server/material_craft_mcp.rs | 6 +-
crates/ui-egui/src/control.rs | 60 ++++++-
crates/ui-egui/src/docking.rs | 20 ++-
crates/ui-egui/src/function/context_menu.rs | 2 +-
crates/ui-egui/src/function/input.rs | 1 +
crates/ui-egui/src/function/render.rs | 2 +-
crates/ui-egui/src/graph/add_node_menu.rs | 31 +++-
crates/ui-egui/src/graph/context_menu.rs | 2 +-
crates/ui-egui/src/graph/input.rs | 6 +-
crates/ui-egui/src/graph/mod.rs | 2 +
crates/ui-egui/src/graph/modality_tests.rs | 104 ++++++++++++
crates/ui-egui/src/graph/search.rs | 2 +-
crates/ui-egui/src/lib.rs | 1 +
crates/ui-egui/src/material_craft_app.rs | 6 +-
crates/ui-egui/src/menus.rs | 157 ++++++++++++++++--
crates/ui-egui/src/pattern/canvas.rs | 2 +-
crates/ui-egui/src/pattern/context_menu.rs | 2 +-
crates/ui-egui/src/pattern/input.rs | 6 +-
crates/ui-egui/src/shell/mod.rs | 1 +
crates/ui-egui/src/shell/modality.rs | 106 ++++++++++++
crates/ui-egui/src/shell/modality.test.rs | 128 ++++++++++++++
crates/ui-egui/src/windows.rs | 13 +-
crates/ui-egui/src/windows.test.rs | 74 +++++++++
docs/control-protocol.md | 6 +-
24 files changed, 697 insertions(+), 43 deletions(-)
create mode 100644 crates/ui-egui/src/graph/modality_tests.rs
create mode 100644 crates/ui-egui/src/shell/mod.rs
create mode 100644 crates/ui-egui/src/shell/modality.rs
create mode 100644 crates/ui-egui/src/shell/modality.test.rs
diff --git a/crates/automation/src/server/material_craft_mcp.rs b/crates/automation/src/server/material_craft_mcp.rs
index 0d42054..fb3614f 100644
--- a/crates/automation/src/server/material_craft_mcp.rs
+++ b/crates/automation/src/server/material_craft_mcp.rs
@@ -207,7 +207,7 @@ impl MaterialCraftMcp {
}
#[tool(
- description = "Save the package (path relative to the write root; omit to save to its own file). Writes .sbs XML with adjacent resources/ or a self-contained .mcraft ZIP."
+ description = "Save the package (path relative to the write root; omit to save to its own file). Writes .sbs XML with adjacent resources/ or a self-contained .mcraft ZIP. In bridge mode, saving without a path runs File > Save and returns an error while a dialog or prompt blocks it."
)]
async fn package_save(&self, Parameters(p): Parameters) -> Result {
to_result(self.call("package.save", params(&p)).await)
@@ -254,7 +254,9 @@ impl MaterialCraftMcp {
to_result(self.bridge_only("ui.inspect", json!({})).await)
}
- #[tool(description = "Bridge mode: activate a menu item by id like a click (may open a window). ui.menu.list lists ids.")]
+ #[tool(
+ description = "Bridge mode: activate a menu item by id like a click (may open a window); returns an error while a dialog or prompt blocks it. ui.menu.list lists ids and which are enabled."
+ )]
async fn ui_menu_invoke(&self, Parameters(p): Parameters) -> Result {
to_result(self.bridge_only("ui.menu.invoke", params(&p)).await)
}
diff --git a/crates/ui-egui/src/control.rs b/crates/ui-egui/src/control.rs
index f0146ca..127fffc 100644
--- a/crates/ui-egui/src/control.rs
+++ b/crates/ui-egui/src/control.rs
@@ -7,7 +7,9 @@
//! - `command.list {filter?}`: every command with params docs and enabled state.
//! - `session.inspect`: package, graph, nodes, wires, parameters, cook results, views, history.
//! - `ui.inspect`: the UI state (panels, windows, graph camera, node rectangles and ports on screen).
-//! - `ui.menu.list` / `ui.menu.invoke {id}`: the menu catalog; behave like a menu click.
+//! - `ui.menu.list` / `ui.menu.invoke {id}`: the menu catalog; behave like a menu click, so behind
+//! a dialog or prompt an item lists as disabled and invoking it returns an error, except the
+//! commands [`crate::shell::modality::Modality::allows`] lets through.
//! - `ui.set {...}`: merge fields into the UI state (e.g. `{"prefs": {"thumbnails": false}}`).
//! - `ui.input {events}`: synthetic pointer, wheel, key and text input (see [`parse_events`]).
//! - `view.screenshot {view?: "3d"|"2d"}`: PNG of a view, base64.
@@ -326,12 +328,18 @@ fn handle_inner(app: &mut MaterialCraftApp, ctx: &egui::Context, method: &str, p
"toasts": app.toasts.iter().map(|t| json!({"text": t.text, "error": t.error})).collect::>(),
}))
}
- "ui.menu.list" => ok(crate::menus::list(app)),
+ "ui.menu.list" => ok(crate::menus::list(app, ctx)),
"ui.menu.invoke" => {
let Some(id) = p.get("id").and_then(Value::as_str) else { return err("`id` is required") };
if !crate::menus::implemented(id) {
return err(format!("menu item `{id}` is not implemented"));
}
+ let modality = crate::shell::modality::Modality::capture(app, ctx);
+ if let crate::shell::modality::Modality::Blocked(blocker) = modality
+ && !modality.allows(id)
+ {
+ return err(format!("menu item `{id}` cannot run while {} is open", blocker.name()));
+ }
crate::menus::invoke(app, ctx, id);
ok(Value::Null)
@@ -400,6 +408,54 @@ mod tests {
MaterialCraftApp::headless(materialcraft_engine::sample_session(), crate::state::UiState::default())
}
+ #[test]
+ fn a_menu_item_returns_an_error_behind_a_dialog() {
+ let mut app = app();
+ let ctx = egui::Context::default();
+ app.ui.windows.unsaved = Some(crate::unsaved::Guarded::Quit);
+
+ let reply = handle(&mut app, &ctx, "ui.menu.invoke", &json!({"id": "ui.export"}));
+ assert_eq!(reply["ok"], json!(false), "{reply}");
+ assert_eq!(reply["error"], json!("menu item `ui.export` cannot run while the unsaved-changes prompt is open"), "{reply}");
+ assert!(!app.ui.windows.export);
+
+ let list = handle(&mut app, &ctx, "ui.menu.list", &json!({}));
+ assert!(list["result"].as_array().unwrap().iter().all(|item| item["enabled"] == json!(false)), "{list}");
+
+ app.ui.windows.unsaved = None;
+ let reply = handle(&mut app, &ctx, "ui.menu.invoke", &json!({"id": "ui.export"}));
+ assert_eq!(reply["ok"], json!(true), "{reply}");
+ assert!(app.ui.windows.export);
+ }
+
+ #[test]
+ fn a_menu_item_reports_the_blocker_behind_a_file_dialog_or_an_update_window() {
+ let ctx = egui::Context::default();
+ let mut behind_dialog = app();
+ behind_dialog.dialogs.ask(craft_dialogs::FileDialog::open(), crate::native_dialogs::Then::OpenPackage);
+ assert_eq!(handle(&mut behind_dialog, &ctx, "ui.menu.invoke", &json!({"id": "file.open"}))["ok"], json!(true));
+ let reply = handle(&mut behind_dialog, &ctx, "ui.menu.invoke", &json!({"id": "file.save"}));
+ assert_eq!(reply["error"], json!("menu item `file.save` cannot run while a file dialog is open"), "{reply}");
+ let list = handle(&mut behind_dialog, &ctx, "ui.menu.list", &json!({}));
+ let open = list["result"].as_array().unwrap().iter().find(|item| item["id"] == json!("file.open")).unwrap();
+ assert_eq!(open["enabled"], json!(true), "{list}");
+
+ let mut behind_update = app();
+ crate::updates_ui::open_prompt_for_test(&mut behind_update);
+ let reply = handle(&mut behind_update, &ctx, "ui.menu.invoke", &json!({"id": "ui.export"}));
+ assert_eq!(reply["error"], json!("menu item `ui.export` cannot run while an update window is open"), "{reply}");
+ }
+
+ #[test]
+ fn command_run_stays_available_behind_a_dialog() {
+ let mut app = app();
+ let ctx = egui::Context::default();
+ app.ui.windows.unsaved = Some(crate::unsaved::Guarded::Quit);
+ let reply = handle(&mut app, &ctx, "command.run", &json!({"id": "edit.selectAll"}));
+ assert_eq!(reply["ok"], json!(true), "{reply}");
+ assert_eq!(app.ui.windows.unsaved, Some(crate::unsaved::Guarded::Quit));
+ }
+
#[test]
fn ui_set_merges_nested_objects_instead_of_resetting_siblings() {
let mut app = app();
diff --git a/crates/ui-egui/src/docking.rs b/crates/ui-egui/src/docking.rs
index 3e0128d..f2ca03a 100644
--- a/crates/ui-egui/src/docking.rs
+++ b/crates/ui-egui/src/docking.rs
@@ -290,7 +290,7 @@ pub fn show(app: &mut MaterialCraftApp, ui: &mut Ui) {
let tk = Tokens::get(&ctx);
let style = style(ui, &tk);
let layout = app.ui.dock.workspace.layout.clone();
- let mut area = DockArea::new(area_id()).input_blocked(ctx.memory(|m| m.top_modal_layer()).is_some());
+ let mut area = DockArea::new(area_id()).input_blocked(crate::shell::modality::Modality::capture(app, &ctx).is_blocked());
if let Some(panel) = app.ui.dock.raise.take() {
area = area.raise_panel(panel);
}
@@ -375,6 +375,24 @@ mod tests {
}
}
+ #[test]
+ fn floating_panels_stop_while_a_dialog_is_open() {
+ let mut app = crate::MaterialCraftApp::headless(materialcraft_engine::sample_session(), crate::UiState::default());
+ let ctx = egui::Context::default();
+ let screen_rect = Some(egui::Rect::from_min_size(egui::Pos2::ZERO, vec2(1200.0, 800.0)));
+ let frame = |app: &mut crate::MaterialCraftApp| {
+ ctx.run_ui(egui::RawInput { screen_rect, ..Default::default() }, |ui| show(app, ui)).textures_delta.clear();
+ ctx.data(|data| data.get_temp::(area_id().with("input-blocked")))
+ };
+
+ assert_eq!(frame(&mut app), Some(false));
+ app.ui.windows.unsaved = Some(crate::unsaved::Guarded::Quit);
+ assert_eq!(frame(&mut app), Some(true), "the panels stop on the frame the prompt opens");
+ app.ui.windows.unsaved = None;
+ app.ui.windows.shortcuts = true;
+ assert_eq!(frame(&mut app), Some(false), "the Shortcuts list does not block");
+ }
+
#[test]
fn default_dock_round_trips() {
let dock = Dock::default();
diff --git a/crates/ui-egui/src/function/context_menu.rs b/crates/ui-egui/src/function/context_menu.rs
index 541d97e..98b0b12 100644
--- a/crates/ui-egui/src/function/context_menu.rs
+++ b/crates/ui-egui/src/function/context_menu.rs
@@ -58,7 +58,7 @@ fn background_actions(app: &mut MaterialCraftApp, editor: &mut FunctionEditor, u
ui.menu_button(tr("Add node"), |ui| {
let entries =
target.available_operations(&app.session).filter(|operation| operation.id != "passthrough").map(|operation| (operation.id, operation.label, ""));
- add = crate::graph::add_node_menu::show(ui, entries);
+ add = crate::graph::add_node_menu::show(app, ui, entries);
});
annotations(app, editor, ui);
if edit_menu::button(ui, "Add Navigation pin", "graph.pin.add", standalone) {
diff --git a/crates/ui-egui/src/function/input.rs b/crates/ui-egui/src/function/input.rs
index b5a376d..67571c5 100644
--- a/crates/ui-egui/src/function/input.rs
+++ b/crates/ui-egui/src/function/input.rs
@@ -179,6 +179,7 @@ pub(super) fn handle(
if response.hovered()
&& !ui.ctx().egui_wants_keyboard_input()
&& ui.input(|input| input.key_pressed(egui::Key::Space))
+ && !crate::shell::modality::Modality::capture(app, ui.ctx()).is_blocked()
&& let Some(pointer_position) = pointer
{
editor.menu_at = camera.to_graph(pointer_position);
diff --git a/crates/ui-egui/src/function/render.rs b/crates/ui-egui/src/function/render.rs
index a873cb2..9658e38 100644
--- a/crates/ui-egui/src/function/render.rs
+++ b/crates/ui-egui/src/function/render.rs
@@ -32,7 +32,7 @@ pub(super) fn paint(app: &MaterialCraftApp, editor: &mut FunctionEditor, ui: &mu
let mut nodes = Vec::new();
let mut clear_annotation = false;
- let frame_key = crate::graph::GraphCamera::frame_key(ui, canvas_bounds);
+ let frame_key = crate::graph::GraphCamera::frame_key(ui, canvas_bounds) && !crate::shell::modality::Modality::capture(app, ui.ctx()).is_blocked();
if !editor.framed || app.ui.frame_all || frame_key {
editor.framed = true;
let selected_only = frame_key && !editor.selection.is_empty();
diff --git a/crates/ui-egui/src/graph/add_node_menu.rs b/crates/ui-egui/src/graph/add_node_menu.rs
index 3090be3..3f7e424 100644
--- a/crates/ui-egui/src/graph/add_node_menu.rs
+++ b/crates/ui-egui/src/graph/add_node_menu.rs
@@ -1,8 +1,11 @@
use egui::{Button, Event, Key, Ui};
+use crate::MaterialCraftApp;
use crate::i18n::tr;
+use crate::shell::modality::Modality;
-pub(crate) fn show(ui: &mut Ui, entries: impl IntoIterator- ) -> Option<&'static str> {
+pub(crate) fn show(app: &MaterialCraftApp, ui: &mut Ui, entries: impl IntoIterator
- ) -> Option<&'static str> {
+ let blocked = Modality::capture(app, ui.ctx()).is_blocked();
let filter_id = ui.make_persistent_id("add-node-filter");
let frame = ui.ctx().cumulative_frame_nr();
let previous = ui.ctx().data_mut(|data| data.get_temp::<(u64, Option)>(filter_id));
@@ -39,7 +42,7 @@ pub(crate) fn show(ui: &mut Ui, entries: impl IntoIterator
- crate::MaterialCraftApp {
+ crate::MaterialCraftApp::headless(materialcraft_engine::sample_session(), crate::UiState::default())
+ }
+
+ #[test]
+ fn entries_are_disabled_behind_a_prompt() {
+ let mut app = app();
+ app.ui.windows.unsaved = Some(crate::unsaved::Guarded::Quit);
+ let mut harness = Harness::builder().build_ui(move |ui| {
+ super::show(&app, ui, [("svg", "SVG", "Atomic nodes")]);
+ });
+ harness.run_steps(3);
+ assert!(harness.get(By::new().role(egui::accesskit::Role::Button).label("SVG")).accesskit_node().is_disabled());
+ }
+
#[test]
fn ordered_entries_keep_the_catalog_sequence() {
- let mut harness = Harness::builder().build_ui(|ui| {
- super::show(ui, [("second", "Second", "Native"), ("first", "First", "Native"), ("third", "Third", "Native")]);
+ let app = app();
+ let mut harness = Harness::builder().build_ui(move |ui| {
+ super::show(&app, ui, [("second", "Second", "Native"), ("first", "First", "Native"), ("third", "Third", "Native")]);
});
harness.run_steps(3);
let rows: Vec<_> = ["Second", "First", "Third"]
@@ -78,9 +97,11 @@ mod tests {
#[test]
fn letters_disable_other_initials_without_removing_entries() {
+ let app = app();
let mut harness = Harness::builder().build_ui_state(
- |ui, selected: &mut Option<&'static str>| {
+ move |ui, selected: &mut Option<&'static str>| {
if let Some(id) = super::show(
+ &app,
ui,
[("svg", "SVG", "Atomic nodes"), ("sharpen", "Sharpen", "Atomic nodes"), ("grayscale", "Grayscale conversion", "Atomic nodes")],
) {
diff --git a/crates/ui-egui/src/graph/context_menu.rs b/crates/ui-egui/src/graph/context_menu.rs
index c809a0a..640e1e0 100644
--- a/crates/ui-egui/src/graph/context_menu.rs
+++ b/crates/ui-egui/src/graph/context_menu.rs
@@ -64,7 +64,7 @@ pub(super) fn show(app: &mut MaterialCraftApp, ui: &mut Ui, at: Pos2, node: Opti
pub(super) fn add_nodes(app: &mut MaterialCraftApp, ui: &mut Ui, at: Pos2) {
let definitions = materialcraft_engine::nodes::catalog().iter().filter(|definition| definition.id != "dot");
let entries = definitions.map(|definition| (definition.id, definition.label, if definition.id == "output" { "Output" } else { definition.category }));
- if let Some(definition) = super::add_node_menu::show(ui, entries) {
+ if let Some(definition) = super::add_node_menu::show(app, ui, entries) {
app.run("graph.node.add", json!({"def": definition, "at": [at.x, at.y], "snapToGrid": app.ui.prefs.snap_to_grid}));
}
}
diff --git a/crates/ui-egui/src/graph/input.rs b/crates/ui-egui/src/graph/input.rs
index 8e293bc..686424e 100644
--- a/crates/ui-egui/src/graph/input.rs
+++ b/crates/ui-egui/src/graph/input.rs
@@ -252,11 +252,7 @@ pub(super) fn handle(
// Keys over the canvas
let hovered = pointer.is_some_and(|position| rect.contains(position));
- #[cfg(not(target_arch = "wasm32"))]
- let update_dialog_open = crate::updates_ui::blocks_input(app);
- #[cfg(target_arch = "wasm32")]
- let update_dialog_open = false;
- if hovered && app.graph.search.is_none() && !update_dialog_open {
+ if hovered && app.graph.search.is_none() && !crate::shell::modality::Modality::capture(app, &context).is_blocked() {
let space = !context.egui_wants_keyboard_input() && context.input(|input| input.key_pressed(egui::Key::Space));
let tab = !app.graph.keyboard_was_focused && !context.text_edit_focused() && context.input(|input| input.key_pressed(egui::Key::Tab));
if (space || tab)
diff --git a/crates/ui-egui/src/graph/mod.rs b/crates/ui-egui/src/graph/mod.rs
index 186ca7a..cde9113 100644
--- a/crates/ui-egui/src/graph/mod.rs
+++ b/crates/ui-egui/src/graph/mod.rs
@@ -15,6 +15,8 @@ mod instance_context_menu;
#[cfg(test)]
mod interaction_tests;
mod library_drag;
+#[cfg(test)]
+mod modality_tests;
pub(crate) mod node_card;
mod node_layout;
mod node_search;
diff --git a/crates/ui-egui/src/graph/modality_tests.rs b/crates/ui-egui/src/graph/modality_tests.rs
new file mode 100644
index 0000000..730881f
--- /dev/null
+++ b/crates/ui-egui/src/graph/modality_tests.rs
@@ -0,0 +1,104 @@
+use egui::{Event, Key, Modifiers, Pos2, vec2};
+use materialcraft_engine::core::{FunctionGraph, NodeId, Value};
+use materialcraft_engine::new_session;
+use serde_json::json;
+
+use super::frames::Frames;
+use crate::unsaved::Guarded;
+
+const KINDS: [&str; 3] = ["graph", "function", "pattern"];
+
+fn press(key: Key) -> Event {
+ Event::Key { key, physical_key: None, pressed: true, repeat: false, modifiers: Modifiers::NONE }
+}
+
+/// A canvas of `kind` holding a node, with the pointer on an empty corner away from the centered prompt
+fn canvas(kind: &str) -> Frames {
+ let mut frames = Frames::new(new_session());
+ match kind {
+ "function" => {
+ frames.app.run("file.new", json!({"kind": "function"})).unwrap();
+ let mut graph = FunctionGraph::default();
+ let source = graph.add_constant(Value::Float(2.0));
+ graph.output = Some(source);
+ frames.app.run("graph.function.set", json!({"function": graph})).unwrap();
+ }
+ "pattern" => {
+ let owner = NodeId(frames.app.run("graph.node.add", json!({"def": "fx_map"})).unwrap()["id"].as_u64().unwrap());
+ frames.app.pattern_editor = Some(crate::pattern::PatternEditor::new(frames.app.session.graph, owner));
+ }
+ _ => {
+ frames.app.run("graph.node.add", json!({"def": "uniform"})).unwrap();
+ }
+ }
+ frames.app.ui.frame_all = true;
+ for _ in 0..3 {
+ frames.run(Vec::new(), Modifiers::NONE);
+ }
+ assert!(!frames.app.graph.rects.is_empty(), "{kind}");
+ let corner = corner(&frames);
+ frames.run(vec![Event::PointerMoved(corner)], Modifiers::NONE);
+ frames
+}
+
+fn corner(frames: &Frames) -> Pos2 {
+ frames.app.graph.view.min + vec2(24.0, 24.0)
+}
+
+fn add_menu_open(frames: &Frames) -> bool {
+ frames.app.graph.search.is_some() || egui::Popup::is_any_open(&frames.ctx)
+}
+
+#[test]
+fn space_does_not_open_the_node_menu_behind_a_prompt() {
+ for kind in KINDS {
+ let mut frames = canvas(kind);
+ frames.app.ui.windows.unsaved = Some(Guarded::Quit);
+ frames.run(vec![press(Key::Space)], Modifiers::NONE);
+ assert!(!add_menu_open(&frames), "{kind}");
+
+ frames.app.ui.windows.unsaved = None;
+ frames.run(Vec::new(), Modifiers::NONE);
+ frames.run(vec![press(Key::Space)], Modifiers::NONE);
+ assert!(add_menu_open(&frames), "{kind}: Space works once the prompt is answered");
+ }
+}
+
+#[test]
+fn f_does_not_move_the_camera_behind_a_prompt() {
+ for kind in KINDS {
+ let mut frames = canvas(kind);
+ // Zoom away from the framed view, so that F has a camera to change
+ frames.run(vec![Event::Zoom(1.4)], Modifiers::NONE);
+ frames.run(Vec::new(), Modifiers::NONE);
+ let before = frames.app.graph.rects.clone();
+
+ frames.app.ui.windows.unsaved = Some(Guarded::Quit);
+ frames.run(vec![press(Key::F)], Modifiers::NONE);
+ frames.run(Vec::new(), Modifiers::NONE);
+ assert_eq!(frames.app.graph.rects, before, "{kind}");
+
+ frames.app.ui.windows.unsaved = None;
+ frames.run(Vec::new(), Modifiers::NONE);
+ frames.run(vec![press(Key::F)], Modifiers::NONE);
+ frames.run(Vec::new(), Modifiers::NONE);
+ assert_ne!(frames.app.graph.rects, before, "{kind}: F works once the prompt is answered");
+ }
+}
+
+#[test]
+fn enter_in_the_node_search_waits_behind_a_prompt() {
+ let mut frames = canvas("graph");
+ frames.run(vec![press(Key::Space)], Modifiers::NONE);
+ assert!(frames.app.graph.search.is_some());
+ let nodes = frames.app.session.graph().unwrap().nodes.len();
+
+ frames.app.ui.windows.unsaved = Some(Guarded::Quit);
+ frames.run(vec![press(Key::Enter)], Modifiers::NONE);
+ assert_eq!(frames.app.session.graph().unwrap().nodes.len(), nodes);
+
+ frames.app.ui.windows.unsaved = None;
+ frames.run(Vec::new(), Modifiers::NONE);
+ 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");
+}
diff --git a/crates/ui-egui/src/graph/search.rs b/crates/ui-egui/src/graph/search.rs
index adc1a46..ef79b6f 100644
--- a/crates/ui-egui/src/graph/search.rs
+++ b/crates/ui-egui/src/graph/search.rs
@@ -88,7 +88,7 @@ pub(super) fn show(app: &mut MaterialCraftApp, ui: &mut Ui, cam: &GraphCamera) {
});
let clicked_out = 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 {
+ if let Some(def) = pick.filter(|_| !crate::shell::modality::Modality::capture(app, ui.ctx()).is_blocked()) {
let s = app.graph.search.take();
if let Some(s) = s {
let mut p = json!({"def": def, "at": [s.at.x, s.at.y - 20.0]});
diff --git a/crates/ui-egui/src/lib.rs b/crates/ui-egui/src/lib.rs
index aa933f6..8ef53ac 100644
--- a/crates/ui-egui/src/lib.rs
+++ b/crates/ui-egui/src/lib.rs
@@ -20,6 +20,7 @@ pub mod menus;
mod preview_resolution;
#[path = "property/mod.rs"]
pub mod properties;
+mod shell;
pub mod state;
pub mod theme;
pub mod thumbs;
diff --git a/crates/ui-egui/src/material_craft_app.rs b/crates/ui-egui/src/material_craft_app.rs
index 8abcf63..e89d73c 100644
--- a/crates/ui-egui/src/material_craft_app.rs
+++ b/crates/ui-egui/src/material_craft_app.rs
@@ -363,8 +363,10 @@ impl MaterialCraftApp {
web_files::poll(self);
#[cfg(not(target_arch = "wasm32"))]
crate::native_dialogs::poll(self);
- for f in ctx.input(|i| i.raw.dropped_files.clone()) {
- web_files::dropped(self, ctx, &f);
+ if !crate::shell::modality::Modality::capture(self, ctx).is_blocked() {
+ for f in ctx.input(|i| i.raw.dropped_files.clone()) {
+ web_files::dropped(self, ctx, &f);
+ }
}
self.apply_prefs();
cook::drive(self, ctx);
diff --git a/crates/ui-egui/src/menus.rs b/crates/ui-egui/src/menus.rs
index 0cfdddc..cdec828 100644
--- a/crates/ui-egui/src/menus.rs
+++ b/crates/ui-egui/src/menus.rs
@@ -5,6 +5,7 @@ use serde_json::Value;
use crate::MaterialCraftApp;
use crate::i18n::tr;
+use crate::shell::modality::Modality;
use crate::theme::Tokens;
pub use crate::menu_item::MenuItem;
@@ -144,8 +145,12 @@ pub fn implemented(id: &str) -> bool {
is_ui(id) || materialcraft_engine::find(id).is_some()
}
-/// Invoke a menu item (menus, shortcuts, the control channel's `ui.menu.invoke`)
+/// Invoke a menu item (menus, shortcuts, the control channel's `ui.menu.invoke`), unless a dialog or prompt blocks it
pub fn invoke(app: &mut MaterialCraftApp, ctx: &egui::Context, id: &str) {
+ if !Modality::capture(app, ctx).allows(id) {
+ return;
+ }
+
if crate::graph::editor_actions::invoke(app, id) {
return;
}
@@ -295,7 +300,8 @@ pub fn menu_bar(app: &mut MaterialCraftApp, ui: &mut egui::Ui) {
}
fn menu_item(app: &mut MaterialCraftApp, ctx: &egui::Context, ui: &mut egui::Ui, item: &MenuItem) {
- let on = enabled(app, item.id);
+ let modality = Modality::capture(app, ctx);
+ let on = modality.allows(item.id) && enabled(app, item.id);
let mut b = egui::Button::new(tr(item.label));
if let Some(sc) = item.shortcut {
b = b.shortcut_text(shortcut_label(ctx, sc));
@@ -316,7 +322,7 @@ fn menu_item(app: &mut MaterialCraftApp, ctx: &egui::Context, ui: &mut egui::Ui,
}
#[cfg(not(target_arch = "wasm32"))]
if item.id == "file.open" {
- ui.add_enabled_ui(!app.ui.recent.is_empty(), |ui| {
+ ui.add_enabled_ui(!modality.is_blocked() && !app.ui.recent.is_empty(), |ui| {
ui.menu_button(tr("Recent packages"), |ui| {
for path in app.ui.recent.clone() {
if ui.button(&path).clicked() {
@@ -334,20 +340,19 @@ fn menu_item(app: &mut MaterialCraftApp, ctx: &egui::Context, ui: &mut egui::Ui,
}
}
-/// Dispatch shortcuts unless a widget or popup owns the keyboard
+/// Dispatch shortcuts unless a widget, popup or dialog owns the keyboard
pub fn shortcuts(app: &mut MaterialCraftApp, ctx: &egui::Context) {
if ctx.egui_wants_keyboard_input() || egui::Popup::is_any_open(ctx) {
return;
}
- #[cfg(not(target_arch = "wasm32"))]
- if crate::updates_ui::blocks_input(app) {
- return;
- }
if !ctx.input(|i| i.events.iter().any(|e| matches!(e, egui::Event::Key { pressed: true, .. }))) {
return;
}
- crate::graph::wire_actions::shortcuts(app, ctx);
+ let modality = Modality::capture(app, ctx);
+ if !modality.is_blocked() {
+ crate::graph::wire_actions::shortcuts(app, ctx);
+ }
let platform = craft_keymap::egui::platform(ctx);
if app.keymap.platform() != platform {
@@ -356,7 +361,9 @@ pub fn shortcuts(app: &mut MaterialCraftApp, ctx: &egui::Context) {
// Selected label text copies through egui's Copy and Cut events, which a hit would remove
let label_selected = label_text_selected(ctx);
let hits = ctx.input_mut(|i| {
- app.dispatcher.dispatch_filtered(i, &app.keymap, &Active::NONE, |hit| !(label_selected && matches!(hit.command.id.as_str(), "edit.copy" | "edit.cut")))
+ app.dispatcher.dispatch_filtered(i, &app.keymap, &Active::NONE, |hit| {
+ modality.allows(&hit.command.id) && !(label_selected && matches!(hit.command.id.as_str(), "edit.copy" | "edit.cut"))
+ })
});
for hit in hits {
if crate::graph::editor_actions::shortcut_enabled(app, &hit.command.id) {
@@ -370,14 +377,15 @@ fn label_text_selected(ctx: &egui::Context) -> bool {
}
/// Menu items for `ui.menu.list`
-pub fn list(app: &MaterialCraftApp) -> Value {
+pub fn list(app: &MaterialCraftApp, ctx: &egui::Context) -> Value {
+ let modality = Modality::capture(app, ctx);
Value::Array(
CATALOG
.iter()
.filter(|i| i.label != "-")
.map(|i| {
serde_json::json!({"menu": i.menu, "submenu": i.sub, "label": i.label, "id": i.id, "shortcut": i.shortcut,
- "implemented": implemented(i.id), "enabled": enabled(app, i.id)})
+ "implemented": implemented(i.id), "enabled": modality.allows(i.id) && enabled(app, i.id)})
})
.collect(),
)
@@ -500,6 +508,131 @@ mod tests {
assert!(!app.ui.windows.new_graph);
}
+ #[test]
+ fn delete_waits_behind_the_unsaved_prompt() {
+ let mut app = app();
+ app.run("edit.selectAll", serde_json::json!({}));
+ let nodes = app.session.graph().unwrap().nodes.len();
+ assert!(nodes > 0);
+
+ app.ui.windows.unsaved = Some(crate::unsaved::Guarded::Quit);
+ dispatched(egui::Key::Delete, egui::Modifiers::NONE, &mut app);
+ assert_eq!(app.session.graph().unwrap().nodes.len(), nodes, "Delete does not edit the package behind the prompt");
+
+ app.ui.windows.unsaved = None;
+ dispatched(egui::Key::Delete, egui::Modifiers::NONE, &mut app);
+ assert_eq!(app.session.graph().unwrap().nodes.len(), 0, "Delete works once the prompt is answered");
+ }
+
+ #[test]
+ fn wire_keys_wait_behind_the_unsaved_prompt() {
+ let mut app = app();
+ let wire = app.session.graph().unwrap().connections.first().map(|wire| (wire.to, wire.to_port.clone())).unwrap();
+ let wires = app.session.graph().unwrap().connections.len();
+ app.graph.selected_wires = vec![wire];
+
+ app.ui.windows.unsaved = Some(crate::unsaved::Guarded::Quit);
+ dispatched(egui::Key::Delete, egui::Modifiers::NONE, &mut app);
+ assert_eq!(app.session.graph().unwrap().connections.len(), wires);
+
+ app.ui.windows.unsaved = None;
+ dispatched(egui::Key::Delete, egui::Modifiers::NONE, &mut app);
+ assert_eq!(app.session.graph().unwrap().connections.len(), wires - 1, "Delete works once the prompt is answered");
+ }
+
+ /// Whether a press is still in egui's input after `shortcuts`, where a shortcut that ran removes it
+ fn press_left_in_input(app: &mut MaterialCraftApp, key: egui::Key, modifiers: egui::Modifiers) -> bool {
+ let ctx = egui::Context::default();
+ let input =
+ egui::RawInput { events: vec![egui::Event::Key { key, physical_key: None, pressed: true, repeat: false, modifiers }], ..Default::default() };
+ let mut left = false;
+ ctx.run_ui(input, |ui| {
+ shortcuts(app, ui.ctx());
+ left = ui.input(|i| i.events.iter().any(|e| matches!(e, egui::Event::Key { key: pressed, .. } if *pressed == key)));
+ })
+ .textures_delta
+ .clear();
+ left
+ }
+
+ #[test]
+ fn behind_a_file_dialog_only_the_commands_that_show_one_run() {
+ let mut app = app();
+ app.dialogs.ask(craft_dialogs::FileDialog::open(), crate::native_dialogs::Then::OpenPackage);
+
+ assert!(!press_left_in_input(&mut app, egui::Key::O, egui::Modifiers::COMMAND), "Ctrl+O reaches the open dialog");
+ assert!(press_left_in_input(&mut app, egui::Key::N, egui::Modifiers::COMMAND), "Ctrl+N waits");
+ assert!(!app.ui.windows.new_graph);
+ assert_eq!(app.dialogs.queued(), 1, "the dialog comes forward and no second one is asked for");
+ }
+
+ #[test]
+ fn invoke_waits_behind_a_file_dialog_except_for_the_commands_that_show_one() {
+ let ctx = egui::Context::default();
+ let mut app = app();
+ app.dialogs.ask(craft_dialogs::FileDialog::open(), crate::native_dialogs::Then::OpenPackage);
+ invoke(&mut app, &ctx, "ui.export");
+ assert!(!app.ui.windows.export);
+ invoke(&mut app, &ctx, "file.open");
+ assert_eq!(app.dialogs.queued(), 1, "the dialog comes forward and no second one is asked for");
+ }
+
+ #[test]
+ fn invoke_runs_nothing_behind_a_prompt() {
+ let mut app = app();
+ let ctx = egui::Context::default();
+ app.ui.windows.unsaved = Some(crate::unsaved::Guarded::Quit);
+ invoke(&mut app, &ctx, "ui.export");
+ assert!(!app.ui.windows.export);
+
+ app.ui.windows.unsaved = None;
+ invoke(&mut app, &ctx, "ui.export");
+ assert!(app.ui.windows.export);
+ }
+
+ #[test]
+ fn the_shortcuts_list_leaves_shortcuts_running() {
+ let mut app = app();
+ app.ui.windows.shortcuts = true;
+ dispatched(egui::Key::N, egui::Modifiers::COMMAND, &mut app);
+ assert!(app.ui.windows.new_graph);
+ }
+
+ #[test]
+ fn menu_bar_items_gray_out_behind_a_prompt() {
+ use egui_kittest::kittest::{By, NodeT, Queryable};
+
+ let mut app = app();
+ app.ui.recent.push("recent.sbs".into());
+ let mut harness = egui_kittest::Harness::builder().build_ui_state(|ui, app: &mut MaterialCraftApp| menu_bar(app, ui), app);
+ harness.run_steps(2);
+ harness.get_by_label("File").click();
+ harness.run_steps(3);
+ let item = |label| By::new().role(egui::accesskit::Role::Button).label(label);
+ assert!(!harness.get(item("Close all")).accesskit_node().is_disabled());
+ assert!(!harness.get(item("Recent packages")).accesskit_node().is_disabled());
+
+ harness.state_mut().ui.windows.unsaved = Some(crate::unsaved::Guarded::Quit);
+ harness.run_steps(3);
+ assert!(harness.get(item("Close all")).accesskit_node().is_disabled());
+ assert!(harness.get(item("Recent packages")).accesskit_node().is_disabled());
+ }
+
+ #[test]
+ fn open_stays_enabled_behind_a_file_dialog() {
+ use egui_kittest::kittest::{By, NodeT, Queryable};
+
+ let mut app = app();
+ app.dialogs.ask(craft_dialogs::FileDialog::open(), crate::native_dialogs::Then::OpenPackage);
+ let mut harness = egui_kittest::Harness::builder().build_ui_state(|ui, app: &mut MaterialCraftApp| menu_bar(app, ui), app);
+ harness.run_steps(2);
+ harness.get_by_label("File").click();
+ harness.run_steps(3);
+ let item = |label| By::new().role(egui::accesskit::Role::Button).label_contains(label);
+ assert!(!harness.get(item("Open...")).accesskit_node().is_disabled());
+ assert!(harness.get(item("Close all")).accesskit_node().is_disabled());
+ }
+
fn press_c(events_after: Vec) -> egui::RawInput {
let mut events = vec![
egui::Event::ModifiersChanged(egui::Modifiers::COMMAND),
diff --git a/crates/ui-egui/src/pattern/canvas.rs b/crates/ui-egui/src/pattern/canvas.rs
index 62e580d..ba8688e 100644
--- a/crates/ui-egui/src/pattern/canvas.rs
+++ b/crates/ui-egui/src/pattern/canvas.rs
@@ -53,7 +53,7 @@ fn paint(app: &MaterialCraftApp, editor: &mut PatternEditor, ui: &mut Ui) -> Opt
let painter = ui.painter_at(bounds);
painter.rect_filled(bounds, 0.0, Color32::from_rgb(0x26, 0x28, 0x2c));
let snap = app.ui.prefs.snap_to_grid;
- let frame_key = GraphCamera::frame_key(ui, bounds);
+ let frame_key = GraphCamera::frame_key(ui, bounds) && !crate::shell::modality::Modality::capture(app, ui.ctx()).is_blocked();
if !editor.framed || app.ui.frame_all || frame_key {
let selected_only = frame_key && !editor.selection.is_empty();
let destination = graph
diff --git a/crates/ui-egui/src/pattern/context_menu.rs b/crates/ui-egui/src/pattern/context_menu.rs
index c5ec18b..e514097 100644
--- a/crates/ui-egui/src/pattern/context_menu.rs
+++ b/crates/ui-egui/src/pattern/context_menu.rs
@@ -7,7 +7,7 @@ pub(super) fn show(app: &mut MaterialCraftApp, editor: &mut PatternEditor, ui: &
let mut add = None;
ui.menu_button(tr("Add node"), |ui| {
let entries = PatternNodeKind::ALL.into_iter().filter(|kind| *kind != PatternNodeKind::Dot).map(|kind| (kind.native_id(), kind.label(), "FX-Map"));
- if let Some(id) = crate::graph::add_node_menu::show(ui, entries) {
+ if let Some(id) = crate::graph::add_node_menu::show(app, ui, entries) {
add = PatternNodeKind::ALL.into_iter().find(|kind| kind.native_id() == id);
}
});
diff --git a/crates/ui-egui/src/pattern/input.rs b/crates/ui-egui/src/pattern/input.rs
index 2a570c1..3d262b6 100644
--- a/crates/ui-egui/src/pattern/input.rs
+++ b/crates/ui-egui/src/pattern/input.rs
@@ -96,7 +96,11 @@ pub(super) fn handle(app: &mut MaterialCraftApp, editor: &mut PatternEditor, ui:
editor.selected_wires.clear();
}
}
- if response.hovered() && !ui.ctx().egui_wants_keyboard_input() && ui.input(|input| input.key_pressed(egui::Key::Space)) {
+ if response.hovered()
+ && !ui.ctx().egui_wants_keyboard_input()
+ && ui.input(|input| input.key_pressed(egui::Key::Space))
+ && !crate::shell::modality::Modality::capture(app, ui.ctx()).is_blocked()
+ {
editor.menu_at = position;
egui::Popup::open_id(ui.ctx(), response.id.with("menu"));
}
diff --git a/crates/ui-egui/src/shell/mod.rs b/crates/ui-egui/src/shell/mod.rs
new file mode 100644
index 0000000..5de3ba7
--- /dev/null
+++ b/crates/ui-egui/src/shell/mod.rs
@@ -0,0 +1 @@
+pub(crate) mod modality;
diff --git a/crates/ui-egui/src/shell/modality.rs b/crates/ui-egui/src/shell/modality.rs
new file mode 100644
index 0000000..f3716be
--- /dev/null
+++ b/crates/ui-egui/src/shell/modality.rs
@@ -0,0 +1,106 @@
+//! Whether input is blocked: the shortcuts, the canvas keys, the floating panels, file drops and
+//! `ui.menu.invoke` all ask [`Modality::capture`]
+//!
+//! Home and the Shortcuts list leave the app usable behind them, so they do not block
+
+use crate::MaterialCraftApp;
+
+/// The commands that show a file dialog: behind an open one they bring it forward
+#[cfg(not(target_arch = "wasm32"))]
+const SHOW_A_FILE_DIALOG: &[&str] = &["file.open", "file.saveAs", "file.saveCopyAs", "file.exportMaterial", "file.importBitmap", "file.importVector"];
+
+/// What is holding input back
+#[derive(Clone, Copy, Debug, PartialEq, Eq)]
+pub(crate) enum Blocker {
+ /// The unsaved-changes prompt, or the Save dialog it opened
+ UnsavedPrompt,
+ /// A native file dialog on screen or queued
+ #[cfg(not(target_arch = "wasm32"))]
+ FileDialog,
+ /// The update prompt or the result window of a check
+ #[cfg(not(target_arch = "wasm32"))]
+ Update,
+ /// One of the app's own dialogs
+ Dialog,
+ /// A window shown through egui's modal layer
+ Modal,
+}
+
+impl Blocker {
+ /// The English name the control channel reports
+ pub(crate) fn name(self) -> &'static str {
+ match self {
+ Blocker::UnsavedPrompt => "the unsaved-changes prompt",
+ #[cfg(not(target_arch = "wasm32"))]
+ Blocker::FileDialog => "a file dialog",
+ #[cfg(not(target_arch = "wasm32"))]
+ Blocker::Update => "an update window",
+ Blocker::Dialog => "a dialog",
+ Blocker::Modal => "a modal window",
+ }
+ }
+
+ /// The menu commands that still run behind this blocker
+ fn allowed_commands(self) -> &'static [&'static str] {
+ match self {
+ #[cfg(not(target_arch = "wasm32"))]
+ Blocker::FileDialog => SHOW_A_FILE_DIALOG,
+ _ => &[],
+ }
+ }
+}
+
+/// The answer for one moment
+#[derive(Clone, Copy, Debug, PartialEq, Eq)]
+pub(crate) enum Modality {
+ Free,
+ Blocked(Blocker),
+}
+
+impl Modality {
+ /// Reads the app's own state before egui's modal layer, which reports the previous frame
+ 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),
+ None => Modality::Free,
+ }
+ }
+
+ pub(crate) fn is_blocked(self) -> bool {
+ self != Modality::Free
+ }
+
+ /// Whether the menu command `id` can run now
+ pub(crate) fn allows(self, id: &str) -> bool {
+ match self {
+ Modality::Free => true,
+ Modality::Blocked(blocker) => blocker.allowed_commands().contains(&id),
+ }
+ }
+}
+
+fn open_blocker(app: &MaterialCraftApp) -> Option {
+ let windows = &app.ui.windows;
+ if windows.unsaved.is_some() || app.awaiting_save.is_some() {
+ 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);
+ }
+ }
+ #[cfg(any(test, target_arch = "wasm32"))]
+ if windows.package_save.is_some() {
+ return Some(Blocker::Dialog);
+ }
+ (windows.new_graph || windows.export || windows.preferences || windows.about || app.ui.explorer.rename.is_some()).then_some(Blocker::Dialog)
+}
+
+#[cfg(test)]
+#[path = "modality.test.rs"]
+mod tests;
diff --git a/crates/ui-egui/src/shell/modality.test.rs b/crates/ui-egui/src/shell/modality.test.rs
new file mode 100644
index 0000000..215ca6d
--- /dev/null
+++ b/crates/ui-egui/src/shell/modality.test.rs
@@ -0,0 +1,128 @@
+use std::path::{Path, PathBuf};
+use std::sync::Arc;
+
+use super::{Blocker, Modality};
+use crate::explorer::ExplorerTarget;
+use crate::{MaterialCraftApp, UiState};
+
+fn app() -> MaterialCraftApp {
+ MaterialCraftApp::headless(materialcraft_engine::sample_session(), UiState::default())
+}
+
+fn capture(app: &MaterialCraftApp) -> Modality {
+ Modality::capture(app, &egui::Context::default())
+}
+
+#[test]
+fn home_and_the_shortcuts_list_leave_input_free() {
+ let mut app = app();
+ assert_eq!(capture(&app), Modality::Free);
+ app.ui.windows.home = true;
+ app.ui.windows.shortcuts = true;
+ assert_eq!(capture(&app), Modality::Free);
+}
+
+#[test]
+fn every_blocking_source_blocks() {
+ type Open = fn(&mut MaterialCraftApp);
+ let cases: [(&str, Open, Blocker); 10] = [
+ ("new graph", |app| app.ui.windows.new_graph = true, Blocker::Dialog),
+ ("export", |app| app.ui.windows.export = true, Blocker::Dialog),
+ ("preferences", |app| app.ui.windows.preferences = true, Blocker::Dialog),
+ ("about", |app| app.ui.windows.about = true, Blocker::Dialog),
+ ("rename", |app| app.ui.explorer.rename = Some((ExplorerTarget::Folder { document: 0, path: "a".into() }, "a".into())), Blocker::Dialog),
+ ("web save", |app| crate::web_files::PackageSave::request(app, false), Blocker::Dialog),
+ ("unsaved prompt", |app| app.ui.windows.unsaved = Some(crate::unsaved::Guarded::Quit), Blocker::UnsavedPrompt),
+ ("awaited save", |app| app.awaiting_save = Some(crate::unsaved::Guarded::Quit), Blocker::UnsavedPrompt),
+ ("queued file dialog", |app| app.dialogs.ask(craft_dialogs::FileDialog::save(), crate::native_dialogs::Then::ExportFolder), Blocker::FileDialog),
+ ("update prompt", |app| crate::updates_ui::open_prompt_for_test(app), Blocker::Update),
+ ];
+
+ let wrong: Vec<_> = cases
+ .into_iter()
+ .filter_map(|(name, open, blocker)| {
+ let mut app = app();
+ open(&mut app);
+ (capture(&app) != Modality::Blocked(blocker)).then_some(name)
+ })
+ .collect();
+ assert!(wrong.is_empty(), "not reported as blocking: {wrong:?}");
+}
+
+#[test]
+fn a_blocker_lets_only_its_own_commands_through() {
+ assert!(Modality::Free.allows("edit.delete"));
+ for id in ["edit.delete", "file.open", "file.save"] {
+ assert!(!Modality::Blocked(Blocker::UnsavedPrompt).allows(id), "{id}");
+ assert!(!Modality::Blocked(Blocker::Dialog).allows(id), "{id}");
+ assert!(!Modality::Blocked(Blocker::Modal).allows(id), "{id}");
+ assert!(!Modality::Blocked(Blocker::Update).allows(id), "{id}");
+ }
+
+ let file_dialog = Modality::Blocked(Blocker::FileDialog);
+ for id in ["file.open", "file.saveAs", "file.saveCopyAs", "file.exportMaterial", "file.importBitmap", "file.importVector"] {
+ assert!(file_dialog.allows(id), "{id}");
+ }
+ assert!(!file_dialog.allows("file.save"), "File > Save writes without showing a dialog when the package has a path");
+ assert!(!file_dialog.allows("edit.delete"));
+}
+
+#[test]
+fn a_modal_layer_from_the_previous_frame_blocks() {
+ let app = app();
+ 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(&app, ui.ctx())).textures_delta.clear();
+ assert_eq!(answer, Modality::Blocked(Blocker::Modal));
+}
+
+#[derive(Debug)]
+struct Dropped(PathBuf, Vec);
+
+impl egui::DroppedFile for Dropped {
+ fn path(&self) -> &Path {
+ &self.0
+ }
+
+ fn bytes(&self) -> Result, String> {
+ Ok(self.1.clone())
+ }
+}
+
+/// One whole frame with `files` dropped on the window
+fn frame(app: &mut MaterialCraftApp, ctx: &egui::Context, files: Vec) {
+ let input = egui::RawInput { dropped_files: files, ..Default::default() };
+ ctx.run_ui(input, |ui| {
+ app.step(ui.ctx());
+ app.draw(ui);
+ })
+ .textures_delta
+ .clear();
+}
+
+#[test]
+fn a_file_dropped_behind_a_prompt_does_not_reach_the_package() {
+ use materialcraft_engine::formats::{Depth, encode};
+ use materialcraft_engine::image::{Image, Kind};
+
+ let mut app = app();
+ let ctx = egui::Context::default();
+ frame(&mut app, &ctx, Vec::new());
+ frame(&mut app, &ctx, Vec::new());
+ let png = encode(&Image::filled(2, 2, Kind::Gray, [0.5; 4]), "png", Depth::U8, false).unwrap();
+ let dropped = || -> Vec { vec![Arc::new(Dropped("dropped.png".into(), png.clone()))] };
+ let nodes = app.session.graph().unwrap().nodes.len();
+
+ app.ui.windows.unsaved = Some(crate::unsaved::Guarded::Quit);
+ frame(&mut app, &ctx, dropped());
+ assert_eq!(app.session.graph().unwrap().nodes.len(), nodes);
+
+ app.ui.windows.unsaved = None;
+ frame(&mut app, &ctx, dropped());
+ assert_eq!(app.session.graph().unwrap().nodes.len(), nodes + 1, "the drop works once the prompt is answered");
+}
diff --git a/crates/ui-egui/src/windows.rs b/crates/ui-egui/src/windows.rs
index a6fe488..73dd7b2 100644
--- a/crates/ui-egui/src/windows.rs
+++ b/crates/ui-egui/src/windows.rs
@@ -11,6 +11,7 @@ use serde_json::{Value, json};
use crate::MaterialCraftApp;
use crate::i18n::tr;
+use crate::shell::modality::Modality;
use crate::theme::{Accent, Brightness, Tokens};
/// Template previews for the New graph dialog (rendered in the background on first open)
@@ -358,18 +359,20 @@ fn shortcuts(app: &mut MaterialCraftApp, ctx: &egui::Context) {
fn home(app: &mut MaterialCraftApp, ctx: &egui::Context) {
let mut open = true;
let mut create: Option<(&str, &str)> = None;
+ let modality = Modality::capture(app, ctx);
+ let free = !modality.is_blocked();
modal(ctx, "home", tr("Home"), &mut open, vec2(760.0, 470.0), |ui| {
let tk = Tokens::get(ui.ctx());
ui.horizontal_top(|ui| {
// Left rail: actions and links
ui.vertical(|ui| {
ui.set_width(150.0);
- if ui.add(crate::widgets::primary_button(ui, tr("New graph"))).clicked() {
+ if ui.add_enabled(free, crate::widgets::primary_button(ui, tr("New graph"))).clicked() {
app.ui.windows.home = false;
app.ui.windows.new_graph = true;
}
ui.add_space(4.0);
- if ui.button(format!("{} {}", icon::FOLDER_OPEN, tr("Open package..."))).clicked() {
+ if ui.add_enabled(modality.allows("file.open"), egui::Button::new(format!("{} {}", icon::FOLDER_OPEN, tr("Open package...")))).clicked() {
app.ui.windows.home = false;
crate::menus::invoke(app, ui.ctx(), "file.open");
}
@@ -395,7 +398,7 @@ fn home(app: &mut MaterialCraftApp, ctx: &egui::Context) {
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);
- if resp.hovered() {
+ if free && resp.hovered() {
ui.painter().rect_filled(r, 6.0, tk.row_hover);
}
let img = egui::Rect::from_min_size(r.min + vec2(6.0, 4.0), vec2(80.0, 80.0));
@@ -409,7 +412,7 @@ fn home(app: &mut MaterialCraftApp, ctx: &egui::Context) {
}
let g = ui.painter().layout(tr(label).to_string(), egui::FontId::proportional(11.0), tk.text, 90.0);
ui.painter().galley(egui::pos2(r.center().x - g.size().x * 0.5, img.bottom() + 6.0), g, tk.text);
- if resp.clicked() {
+ if free && resp.clicked() {
create = Some((id, label));
}
}
@@ -421,7 +424,7 @@ fn home(app: &mut MaterialCraftApp, ctx: &egui::Context) {
}
#[cfg(not(target_arch = "wasm32"))]
for p in app.ui.recent.clone().into_iter().take(6) {
- if ui.link(&p).clicked() {
+ if ui.add_enabled(free, egui::Link::new(&p)).clicked() {
open_path(app, &p);
app.ui.windows.home = false;
}
diff --git a/crates/ui-egui/src/windows.test.rs b/crates/ui-egui/src/windows.test.rs
index 80ad883..41a3bff 100644
--- a/crates/ui-egui/src/windows.test.rs
+++ b/crates/ui-egui/src/windows.test.rs
@@ -1,3 +1,77 @@
+#[cfg(test)]
+mod home_modality_tests {
+ use egui_kittest::kittest::{By, NodeT, Queryable};
+
+ use crate::{MaterialCraftApp, state::UiState};
+
+ fn harness(block: impl FnOnce(&mut MaterialCraftApp)) -> egui_kittest::Harness<'static, MaterialCraftApp> {
+ let app = MaterialCraftApp::headless(materialcraft_engine::sample_session(), UiState::default());
+ // The icon font has to be in place for the frame that first draws Home
+ let mut fonts_installed = false;
+ let mut harness = egui_kittest::Harness::builder().with_size(egui::vec2(1200.0, 800.0)).build_ui_state(
+ move |ui, app: &mut MaterialCraftApp| {
+ if std::mem::replace(&mut fonts_installed, true) {
+ crate::windows::show(app, ui.ctx());
+ } else {
+ crate::theme::install(ui.ctx());
+ }
+ },
+ app,
+ );
+ let app = harness.state_mut();
+ app.ui.windows.home = true;
+ app.ui.recent.push("recent.sbs".into());
+ block(app);
+ harness.run_steps(3);
+ harness
+ }
+
+ fn file_dialog(app: &mut MaterialCraftApp) {
+ app.dialogs.ask(craft_dialogs::FileDialog::open(), crate::native_dialogs::Then::OpenPackage);
+ }
+
+ fn open_package(harness: &egui_kittest::Harness<'static, MaterialCraftApp>) -> bool {
+ !harness.get(By::new().role(egui::accesskit::Role::Button).label_contains("Open package")).accesskit_node().is_disabled()
+ }
+
+ #[test]
+ fn home_actions_are_live_when_nothing_blocks_them() {
+ let harness = harness(|_| {});
+ assert!(open_package(&harness));
+ assert!(!harness.get(By::new().role(egui::accesskit::Role::Button).label("New graph")).accesskit_node().is_disabled());
+ }
+
+ #[test]
+ fn open_package_is_off_behind_a_dialog_and_home_stays_open() {
+ let harness = harness(|app| app.ui.windows.about = true);
+ assert!(!open_package(&harness));
+ assert!(harness.get(By::new().role(egui::accesskit::Role::Button).label("New graph")).accesskit_node().is_disabled());
+ assert!(harness.state().ui.windows.home);
+ }
+
+ #[test]
+ fn open_package_brings_a_file_dialog_forward() {
+ let mut harness = harness(file_dialog);
+ assert!(open_package(&harness));
+ harness.get(By::new().role(egui::accesskit::Role::Button).label_contains("Open package")).click();
+ harness.run_steps(3);
+ assert!(!harness.state().ui.windows.home);
+ assert_eq!(harness.state().dialogs.queued(), 1, "no second dialog is asked for");
+ }
+
+ #[test]
+ fn a_sample_tile_and_a_recent_link_wait_behind_a_file_dialog() {
+ let mut harness = harness(file_dialog);
+ assert!(harness.get(By::new().role(egui::accesskit::Role::Link).label("recent.sbs")).accesskit_node().is_disabled());
+ let graphs = harness.state().session.pkg.graphs.len();
+ let (_, sample) = materialcraft_engine::samples::TEMPLATES.iter().find(|(id, _)| !matches!(*id, "empty" | "pbr")).unwrap();
+ harness.get(By::new().role(egui::accesskit::Role::ListItem).label(sample)).click();
+ harness.run_steps(3);
+ assert_eq!(harness.state().session.pkg.graphs.len(), graphs);
+ assert!(harness.state().ui.windows.home);
+ }
+}
+
#[cfg(test)]
mod open_guard_tests {
use crate::{MaterialCraftApp, state::UiState};
diff --git a/docs/control-protocol.md b/docs/control-protocol.md
index 728bfa6..ce91444 100644
--- a/docs/control-protocol.md
+++ b/docs/control-protocol.md
@@ -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 |
+| `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.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 |
@@ -311,7 +311,9 @@ each has its own reader and writer (`crates/formats/src/material.rs` here), so k
`session_inspect`, `command_list`, `command_run`, `command_batch`, `library_list`, `graph_new`,
`node_add`, `node_connect`, `node_param`, `package_open`, `package_save`, `bitmap_import`,
`outputs_export`, `material_export`, `view_render` (headless) and, bridged to the app, `view_screenshot`,
-`ui_screenshot`, `ui_inspect`, `ui_menu_invoke`, `ui_input`. `package_open` opens `.mcmat` files
+`ui_screenshot`, `ui_inspect`, `ui_menu_invoke`, `ui_input`. Bridged to the app, `package_save`
+without a path runs File > Save through `ui.menu.invoke`, so it returns that call's error when a
+dialog or prompt blocks it. `package_open` opens `.mcmat` files
too, and `material_export` `{path, graph?, size?, values?}` writes one below `--write-root` (`path` is
relative, the parent folder must exist, `values` are exposed input values). The workspace sandbox
still refuses `file.*` commands, so a sandboxed agent exports through the tool, not `file.exportMaterial`;
From 2eea5eef2ca9365cd711b76b14936cfb5ce7eabe Mon Sep 17 00:00:00 2001
From: dotCooCoo
Date: Mon, 9 Nov 2026 06:26:59 -0800
Subject: [PATCH 03/20] 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.
---
crates/ui-egui/src/graph/add_node_menu.rs | 44 ++++++++++++++------
crates/ui-egui/src/graph/input.rs | 6 +--
crates/ui-egui/src/graph/modality_tests.rs | 48 ++++++++++++++++++++++
crates/ui-egui/src/graph/search.rs | 18 ++++++--
crates/ui-egui/src/shell/modality.rs | 14 +++----
crates/ui-egui/src/shell/modality.test.rs | 28 +++++++++++++
crates/ui-egui/src/windows.rs | 3 ++
crates/ui-egui/src/windows.test.rs | 9 ++++
docs/control-protocol.md | 2 +-
9 files changed, 144 insertions(+), 28 deletions(-)
diff --git a/crates/ui-egui/src/graph/add_node_menu.rs b/crates/ui-egui/src/graph/add_node_menu.rs
index 3f7e424..c37119c 100644
--- a/crates/ui-egui/src/graph/add_node_menu.rs
+++ b/crates/ui-egui/src/graph/add_node_menu.rs
@@ -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)>(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();
diff --git a/crates/ui-egui/src/graph/input.rs b/crates/ui-egui/src/graph/input.rs
index 686424e..b964a73 100644
--- a/crates/ui-egui/src/graph/input.rs
+++ b/crates/ui-egui/src/graph/input.rs
@@ -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();
diff --git a/crates/ui-egui/src/graph/modality_tests.rs b/crates/ui-egui/src/graph/modality_tests.rs
index 730881f..ea244f0 100644
--- a/crates/ui-egui/src/graph/modality_tests.rs
+++ b/crates/ui-egui/src/graph/modality_tests.rs
@@ -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");
+}
diff --git a/crates/ui-egui/src/graph/search.rs b/crates/ui-egui/src/graph/search.rs
index ef79b6f..8a3eff0 100644
--- a/crates/ui-egui/src/graph/search.rs
+++ b/crates/ui-egui/src/graph/search.rs
@@ -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();
diff --git a/crates/ui-egui/src/shell/modality.rs b/crates/ui-egui/src/shell/modality.rs
index f3716be..cd15d62 100644
--- a/crates/ui-egui/src/shell/modality.rs
+++ b/crates/ui-egui/src/shell/modality.rs
@@ -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 {
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() {
diff --git a/crates/ui-egui/src/shell/modality.test.rs b/crates/ui-egui/src/shell/modality.test.rs
index 215ca6d..b7708cb 100644
--- a/crates/ui-egui/src/shell/modality.test.rs
+++ b/crates/ui-egui/src/shell/modality.test.rs
@@ -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();
diff --git a/crates/ui-egui/src/windows.rs b/crates/ui-egui/src/windows.rs
index 73dd7b2..fb143c6 100644
--- a/crates/ui-egui/src/windows.rs
+++ b/crates/ui-egui/src/windows.rs
@@ -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);
diff --git a/crates/ui-egui/src/windows.test.rs b/crates/ui-egui/src/windows.test.rs
index 41a3bff..28379f8 100644
--- a/crates/ui-egui/src/windows.test.rs
+++ b/crates/ui-egui/src/windows.test.rs
@@ -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)]
diff --git a/docs/control-protocol.md b/docs/control-protocol.md
index ce91444..2276d21 100644
--- a/docs/control-protocol.md
+++ b/docs/control-protocol.md
@@ -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 |
From c54e289a9d71709d3636a671146ad3553a0a8c99 Mon Sep 17 00:00:00 2001
From: dotCooCoo
Date: Mon, 9 Nov 2026 06:30:56 -0800
Subject: [PATCH 04/20] fix(ui): toolbar buttons are disabled when a blocker
refuses their command
The toolbar's New graph, Open package, Save and Export buttons stayed
enabled behind a dialog and did nothing when clicked. They now follow
the same rule as the menu items.
---
crates/ui-egui/src/toolbar.rs | 45 ++++++++++++++++++++++++++++++++++-
1 file changed, 44 insertions(+), 1 deletion(-)
diff --git a/crates/ui-egui/src/toolbar.rs b/crates/ui-egui/src/toolbar.rs
index 577855a..b8407cd 100644
--- a/crates/ui-egui/src/toolbar.rs
+++ b/crates/ui-egui/src/toolbar.rs
@@ -14,8 +14,9 @@ pub fn main_toolbar(app: &mut MaterialCraftApp, ui: &mut Ui) {
ui.horizontal_centered(|ui| {
ui.spacing_mut().item_spacing.x = 2.0;
let (u, r) = (crate::menus::enabled(app, "edit.undo"), crate::menus::enabled(app, "edit.redo"));
+ let modality = crate::shell::modality::Modality::capture(app, &ctx);
let mut b = |ui: &mut Ui, ic: &str, tip: &str, id: &str, on: bool| {
- let r = ui.add_enabled_ui(on, |ui| crate::widgets::icon_button(ui, ic, tip, false, 24.0)).inner;
+ let r = ui.add_enabled_ui(on && modality.allows(id), |ui| crate::widgets::icon_button(ui, ic, tip, false, 24.0)).inner;
if r.clicked() {
crate::menus::invoke(app, &ctx, id);
}
@@ -67,3 +68,45 @@ pub fn status_bar(app: &mut MaterialCraftApp, ui: &mut Ui) {
});
});
}
+
+#[cfg(test)]
+mod tests {
+ use egui_kittest::kittest::{By, NodeT, Queryable};
+
+ use crate::{MaterialCraftApp, state::UiState};
+
+ fn harness(block: impl FnOnce(&mut MaterialCraftApp)) -> egui_kittest::Harness<'static, MaterialCraftApp> {
+ let mut app = MaterialCraftApp::headless(materialcraft_engine::sample_session(), UiState::default());
+ block(&mut app);
+ let mut fonts_installed = false;
+ let mut harness = egui_kittest::Harness::builder().with_size(egui::vec2(800.0, 100.0)).build_ui_state(
+ move |ui, app: &mut MaterialCraftApp| {
+ if std::mem::replace(&mut fonts_installed, true) {
+ super::main_toolbar(app, ui);
+ } else {
+ crate::theme::install(ui.ctx());
+ }
+ },
+ app,
+ );
+ harness.run_steps(3);
+ harness
+ }
+
+ fn off(harness: &egui_kittest::Harness<'static, MaterialCraftApp>, label: &str) -> bool {
+ harness.get(By::new().role(egui::accesskit::Role::Button).label_contains(label)).accesskit_node().is_disabled()
+ }
+
+ #[test]
+ fn toolbar_buttons_match_the_commands_a_blocker_allows() {
+ let free = harness(|_| {});
+ assert!(!off(&free, "New graph") && !off(&free, "Open package") && !off(&free, "Save") && !off(&free, "Export"));
+
+ let dialog = harness(|app| app.ui.windows.about = true);
+ assert!(off(&dialog, "New graph") && off(&dialog, "Open package") && off(&dialog, "Save") && off(&dialog, "Export"));
+
+ let file_dialog = harness(|app| app.dialogs.ask(craft_dialogs::FileDialog::open(), crate::native_dialogs::Then::OpenPackage));
+ assert!(!off(&file_dialog, "Open package"), "a file dialog leaves Open enabled, which brings it forward");
+ assert!(off(&file_dialog, "Save"));
+ }
+}
From 590894b3462aa622121c71eb6b292466464588eb Mon Sep 17 00:00:00 2001
From: dotCooCoo
Date: Mon, 9 Nov 2026 07:07:53 -0800
Subject: [PATCH 05/20] test(ui): toolbar test finds buttons by position, not
translated label
---
crates/ui-egui/src/toolbar.rs | 18 ++++++++++++------
1 file changed, 12 insertions(+), 6 deletions(-)
diff --git a/crates/ui-egui/src/toolbar.rs b/crates/ui-egui/src/toolbar.rs
index b8407cd..569a87b 100644
--- a/crates/ui-egui/src/toolbar.rs
+++ b/crates/ui-egui/src/toolbar.rs
@@ -93,20 +93,26 @@ mod tests {
harness
}
- fn off(harness: &egui_kittest::Harness<'static, MaterialCraftApp>, label: &str) -> bool {
- harness.get(By::new().role(egui::accesskit::Role::Button).label_contains(label)).accesskit_node().is_disabled()
+ // Each button's position in the toolbar. The labels change with the UI language.
+ const NEW_GRAPH: usize = 0;
+ const OPEN: usize = 1;
+ const SAVE: usize = 2;
+ const EXPORT: usize = 5;
+
+ fn off(harness: &egui_kittest::Harness<'static, MaterialCraftApp>, button: usize) -> bool {
+ harness.get_all(By::new().role(egui::accesskit::Role::Button)).nth(button).expect("the toolbar has this button").accesskit_node().is_disabled()
}
#[test]
fn toolbar_buttons_match_the_commands_a_blocker_allows() {
let free = harness(|_| {});
- assert!(!off(&free, "New graph") && !off(&free, "Open package") && !off(&free, "Save") && !off(&free, "Export"));
+ assert!(!off(&free, NEW_GRAPH) && !off(&free, OPEN) && !off(&free, SAVE) && !off(&free, EXPORT));
let dialog = harness(|app| app.ui.windows.about = true);
- assert!(off(&dialog, "New graph") && off(&dialog, "Open package") && off(&dialog, "Save") && off(&dialog, "Export"));
+ assert!(off(&dialog, NEW_GRAPH) && off(&dialog, OPEN) && off(&dialog, SAVE) && off(&dialog, EXPORT));
let file_dialog = harness(|app| app.dialogs.ask(craft_dialogs::FileDialog::open(), crate::native_dialogs::Then::OpenPackage));
- assert!(!off(&file_dialog, "Open package"), "a file dialog leaves Open enabled, which brings it forward");
- assert!(off(&file_dialog, "Save"));
+ assert!(!off(&file_dialog, OPEN), "a file dialog leaves Open enabled, which brings it forward");
+ assert!(off(&file_dialog, SAVE));
}
}
From 7116602329b48b8a260007e1c39fc21ba6dd539a Mon Sep 17 00:00:00 2001
From: "Dylan M. Taylor"
Date: Mon, 9 Nov 2026 10:10:26 -0500
Subject: [PATCH 06/20] docs: add SonarCloud and CodeRabbit badges to README
(#179)
---
README.md | 3 +++
1 file changed, 3 insertions(+)
diff --git a/README.md b/README.md
index c983f39..bd02b26 100644
--- a/README.md
+++ b/README.md
@@ -29,6 +29,9 @@
+
+
+
From 9290c2d9060d2d80a98c20ffe01ee2c747dc17ac Mon Sep 17 00:00:00 2001
From: PiJellyPin
Date: Tue, 10 Nov 2026 00:52:05 +0100
Subject: [PATCH 07/20] Listing all nodes from custom functions.sbs
also fixed library ui with categories
---
crates/engine/src/commands.rs | 1 +
crates/engine/src/function_library.rs | 21 +-
crates/engine/src/graph_cmds.rs | 25 +-
crates/engine/src/lib.rs | 3 +
crates/engine/src/node_library.rs | 432 ++++++++++++++
crates/engine/src/node_library.test.rs | 118 ++++
crates/engine/src/node_library_commands.rs | 131 +++++
crates/engine/src/session.rs | 24 +-
crates/engine/tests/it/main.rs | 1 +
crates/engine/tests/it/node_library.rs | 82 +++
crates/formats/src/sbs/library.rs | 64 ++
crates/formats/src/sbs/library_layout.rs | 298 ++++++++++
crates/formats/src/sbs/mod.rs | 2 +
crates/ui-egui/src/explorer.rs | 29 +-
crates/ui-egui/src/explorer/actions.rs | 74 ++-
crates/ui-egui/src/explorer/context_menu.rs | 148 +++--
crates/ui-egui/src/explorer/explorer_state.rs | 3 +
crates/ui-egui/src/explorer/explorer_tree.rs | 8 +-
.../src/function/function_library_drag.rs | 3 +-
crates/ui-egui/src/function/library.rs | 84 +--
crates/ui-egui/src/graph/input.rs | 2 +-
crates/ui-egui/src/graph/library_drag.rs | 2 +-
crates/ui-egui/src/graph/mod.rs | 2 +-
crates/ui-egui/src/graph/navigation.rs | 6 +
crates/ui-egui/src/library.rs | 555 +++++++++++++-----
crates/ui-egui/src/library.test.rs | 103 ++++
crates/ui-egui/src/menus.rs | 119 +++-
crates/ui-egui/src/pattern/library.rs | 19 +-
crates/ui-egui/src/state/ui_state.rs | 15 +
crates/ui-egui/tests/snapshot.rs | 7 +-
crates/ui-egui/tests/snapshot/explorer.rs | 56 +-
crates/ui-egui/tests/snapshot/library.rs | 119 ++++
docs/architecture.md | 8 +
docs/control-protocol.md | 16 +
docs/gaps.md | 17 +-
35 files changed, 2257 insertions(+), 340 deletions(-)
create mode 100644 crates/engine/src/node_library.rs
create mode 100644 crates/engine/src/node_library.test.rs
create mode 100644 crates/engine/src/node_library_commands.rs
create mode 100644 crates/engine/tests/it/node_library.rs
create mode 100644 crates/formats/src/sbs/library_layout.rs
create mode 100644 crates/ui-egui/src/library.test.rs
create mode 100644 crates/ui-egui/tests/snapshot/library.rs
diff --git a/crates/engine/src/commands.rs b/crates/engine/src/commands.rs
index acc9729..84321f3 100644
--- a/crates/engine/src/commands.rs
+++ b/crates/engine/src/commands.rs
@@ -60,6 +60,7 @@ pub fn command_specs() -> &'static [CommandSpec] {
v.extend(crate::explorer_clipboard_commands::specs());
v.extend(crate::graph_instance_commands::specs());
v.extend(crate::function_library_commands::specs());
+ v.extend(crate::node_library_commands::specs());
v.extend(crate::node_info_commands::specs());
v.extend(crate::atomic_sample_commands::specs());
v.extend(crate::material_cmds::specs());
diff --git a/crates/engine/src/function_library.rs b/crates/engine/src/function_library.rs
index ad55a8b..072707f 100644
--- a/crates/engine/src/function_library.rs
+++ b/crates/engine/src/function_library.rs
@@ -1,21 +1,2 @@
-use materialcraft_core::Package;
-use std::{collections::BTreeMap, sync::Arc};
-
+/// The bundled function library every session loads (see `node_library`)
pub(crate) const STANDARD_PACKAGE: &str = "sbs://functions.sbs";
-
-pub(crate) struct FunctionLibrary {
- pub packages: BTreeMap>,
- pub warnings: Vec,
-}
-
-pub(crate) fn load() -> Result {
- let bytes = materialcraft_formats::sbs::library::read(STANDARD_PACKAGE)?;
- let mut read =
- materialcraft_formats::sbs::read_sbs(&bytes, STANDARD_PACKAGE, &crate::file_cmds::linked_files(STANDARD_PACKAGE)).map_err(|error| error.to_string())?;
- crate::linked_files::normalize_references(&mut read.pkg, STANDARD_PACKAGE);
- read.warnings.extend(crate::linked_files::load(&mut read.pkg));
-
- let mut packages = std::mem::take(&mut read.pkg.dependencies);
- packages.insert(STANDARD_PACKAGE.into(), Arc::new(read.pkg));
- Ok(FunctionLibrary { packages, warnings: read.warnings })
-}
diff --git a/crates/engine/src/graph_cmds.rs b/crates/engine/src/graph_cmds.rs
index 9f0954e..3c4356e 100644
--- a/crates/engine/src/graph_cmds.rs
+++ b/crates/engine/src/graph_cmds.rs
@@ -22,7 +22,15 @@ pub(crate) fn specs() -> Vec {
})
}),
cmd!("graph.format", "Output format", [], None, "{format: u8|u16|f16|f32, graph?, document?}", always, format),
- cmd!("graph.cook", "Compute graph", [], None, "{full?: bool} cooks now; returns per-node timings and errors", has_graph, cook),
+ cmd!(
+ "graph.cook",
+ "Compute graph",
+ [],
+ None,
+ "{full?: bool, graph?: index (another graph of the package cooks at full size into the cache)} cooks now; returns per-node timings and errors",
+ has_graph,
+ cook
+ ),
cmd!("graph.pin.add", "Add Navigation pin", [], None, "{name?, at? [x, y]}", has_graph, pin_add),
cmd!("graph.pin.delete", "Delete pin", [], None, "{id}", has_graph, pin_delete),
cmd!(
@@ -153,8 +161,19 @@ fn format(s: &mut Session, p: &J) -> Result {
}
fn cook(s: &mut Session, p: &J) -> Result {
- let cooked = if boolp(p, "full") == Some(true) { s.cook_full(s.graph) } else { s.cook().clone() };
- let g = s.graph()?;
+ let index = match p.get("graph").filter(|graph| !graph.is_null()) {
+ None => s.graph,
+ Some(graph) => {
+ let index = graph.as_u64().and_then(|index| usize::try_from(index).ok()).ok_or_else(|| bad("graph.cook", "`graph` is a graph index"))?;
+ let graph = s.pkg.graph(index)?;
+ if graph.function.is_some() {
+ return Err(bad("graph.cook", "a Function Graph has no outputs to compute"));
+ }
+ index
+ }
+ };
+ let cooked = if index != s.graph || boolp(p, "full") == Some(true) { s.cook_full(index) } else { s.cook().clone() };
+ let g = s.pkg.graph(index)?;
Ok(json!({
"ms": cooked.ms,
"nodes": cooked.nodes.iter().map(|(id, r)| {
diff --git a/crates/engine/src/lib.rs b/crates/engine/src/lib.rs
index bacdf62..0862a4c 100644
--- a/crates/engine/src/lib.rs
+++ b/crates/engine/src/lib.rs
@@ -11,6 +11,9 @@ mod folder_property_commands;
pub use explorer_clipboard::ExplorerClipboard;
mod function_library;
mod function_library_commands;
+pub mod node_library;
+mod node_library_commands;
+pub use node_library::{LibraryCanvas, LibraryEntry, LibraryItem, NodeLibrary};
mod function_variable;
mod function_variable_commands;
mod function_variables;
diff --git a/crates/engine/src/node_library.rs b/crates/engine/src/node_library.rs
new file mode 100644
index 0000000..80c4dfb
--- /dev/null
+++ b/crates/engine/src/node_library.rs
@@ -0,0 +1,432 @@
+//! The node library. Every SBS package in the library folder (`assets/sbs`, or the bundled copies)
+//! is loaded once per process. A package whose titled frames are library categories ("Filters -
+//! Blurs") lays the library out: each node inside a frame becomes an entry in that category.
+//! Every other package lends its graphs, which graph instances can use. Native nodes, function
+//! operations, FX-Map nodes and graph items fill in what no layout places.
+
+use materialcraft_core::{Package, PatternNodeKind};
+use materialcraft_formats::sbs::{LayoutItem, library};
+use serde::Serialize;
+use std::collections::{BTreeMap, BTreeSet};
+use std::sync::{Arc, Mutex};
+
+/// The library tree in display order: top-level categories and their sub-categories. The UI adds
+/// Favorites above it
+pub const CATEGORIES: &[(&str, &[&str])] = &[
+ ("Graph items", &[]),
+ ("Atomic nodes", &[]),
+ ("FxMap nodes", &[]),
+ ("Function nodes", &["Constant", "Vector", "Variables", "Samplers", "Cast", "Operator", "Logical", "Comparison", "Function", "Control"]),
+ ("Texture generators", &["Noises", "Patterns"]),
+ ("Filters", &["Adjustments", "Blending", "Blurs", "Channels", "Effects", "Normal map", "Tiling", "Transforms"]),
+ ("Material filters", &["1-click", "Effects", "Transforms", "Blending", "PBR utilities", "Scan processing"]),
+ ("Mesh-based generators", &["Mask generators", "Weathering", "Utilities"]),
+ ("Spline & Path tools", &["Spline tools", "Path tools"]),
+ ("MCF functions", &["Primitives", "Operators", "Transforms"]),
+ ("Functions", &["Color", "Comparison", "Constant", "Conversion", "Maths", "Parity", "Random", "Transformation", "Various", "Easings"]),
+ ("3D view", &["HDRI environments", "HDRI tools"]),
+ ("PBR materials", &["Bricks", "Concrete", "Fabric", "Ground", "Metal", "Paper", "Pavement", "Tiles", "Wood", "Stone"]),
+ ("Values", &["Constant"]),
+];
+
+/// Older layouts name categories MaterialCraft has renamed: (old, current)
+const RENAMED: &[(&str, &str)] = &[("SDF functions", "MCF functions")];
+
+/// Words that place a material in a sub-category when its layout frame names only the top level
+const SUB_KEYWORDS: &[(&str, &str, &[&str])] = &[
+ ("PBR materials", "Bricks", &["brick"]),
+ ("PBR materials", "Concrete", &["concrete", "cement"]),
+ ("PBR materials", "Fabric", &["fabric", "cloth", "textile", "leather"]),
+ ("PBR materials", "Ground", &["ground", "gravel", "soil", "sand", "dirt", "grass", "mud"]),
+ ("PBR materials", "Metal", &["metal", "steel", "iron", "copper", "rust"]),
+ ("PBR materials", "Paper", &["paper", "cardboard"]),
+ ("PBR materials", "Pavement", &["pavement", "paving", "cobble", "asphalt"]),
+ ("PBR materials", "Tiles", &["tile"]),
+ ("PBR materials", "Wood", &["wood", "plank", "bark"]),
+ ("PBR materials", "Stone", &["stone", "rock", "marble", "granite"]),
+];
+
+/// The graph kind that can take an entry
+#[derive(Clone, Copy, Debug, PartialEq, Eq, Serialize)]
+#[serde(rename_all = "camelCase")]
+pub enum LibraryCanvas {
+ Material,
+ Function,
+ Pattern,
+}
+
+/// What adding an entry puts in the graph
+#[derive(Clone, Debug, PartialEq, Serialize)]
+#[serde(tag = "type", rename_all = "camelCase")]
+pub enum LibraryItem {
+ /// A native node, by catalog id
+ Node {
+ def: String,
+ },
+ Comment,
+ Frame,
+ Pin,
+ /// An instance of a graph from a library package
+ Instance {
+ package: String,
+ graph: String,
+ },
+ /// A native function operation
+ Operation {
+ operation: String,
+ },
+ /// An instance of a function graph from a library package
+ FunctionInstance {
+ package: String,
+ graph: String,
+ },
+ /// An FX-Map node
+ Pattern {
+ kind: PatternNodeKind,
+ },
+ /// An environment image
+ Bitmap {
+ resource: String,
+ },
+}
+
+impl LibraryItem {
+ pub fn canvas(&self) -> LibraryCanvas {
+ match self {
+ Self::Operation { .. } | Self::FunctionInstance { .. } => LibraryCanvas::Function,
+ Self::Pattern { .. } => LibraryCanvas::Pattern,
+ _ => LibraryCanvas::Material,
+ }
+ }
+}
+
+#[derive(Clone, Debug, PartialEq, Serialize)]
+pub struct LibraryEntry {
+ /// Stable id, e.g. `node:blur`, `function:add`, `sbs://functions.sbs#Functions/Math/Pi`
+ pub id: String,
+ pub label: String,
+ /// `Top` or `Top/Sub`, from [`CATEGORIES`]
+ pub category: String,
+ pub description: String,
+ #[serde(flatten)]
+ pub item: LibraryItem,
+ pub canvas: LibraryCanvas,
+ /// Why the entry can't be added yet (the package it needs is not in MaterialCraft)
+ #[serde(skip_serializing_if = "Option::is_none")]
+ pub missing: Option,
+ /// It can be added, but is incomplete (it uses operations MaterialCraft lacks)
+ #[serde(skip_serializing_if = "Option::is_none")]
+ pub warning: Option,
+}
+
+/// A layout placement, before it is resolved against the loaded packages
+#[derive(Clone, Debug)]
+struct Placement {
+ category: String,
+ item: LayoutItem,
+}
+
+/// Everything loaded from the library folder
+#[derive(Debug, Default)]
+pub struct NodeLibrary {
+ /// Library packages (layouts excluded) and their own dependencies, by `sbs://` path
+ pub packages: BTreeMap>,
+ /// The `sbs://` files that laid the library out
+ pub layouts: Vec,
+ pub warnings: Vec,
+ placements: Vec,
+}
+
+static SHARED: Mutex