The canvas is one real Tab stop and takes the keyboard when pressed

This commit is contained in:
aiongg committed 2026-11-25 13:58:02 +00:00
1 parent bfa1042c2d
commit 252d671c2b
23 files changed
+290 -36

No files matched your search

+1 -7
View File
@@ -312,11 +312,6 @@ fn refresh_specs(app: &mut MaterialCraftApp) {
// ---------------------------------------------------------------------------------------------
// Drawing
/// The graph editor's id as the window's document canvas.
fn graph_canvas_id() -> egui::Id {
egui::Id::unique("materialcraft-graph-canvas")
}
/// Draw the dock (graph and panels) into `ui`.
pub fn show(app: &mut MaterialCraftApp, ui: &mut Ui) {
let ctx = ui.ctx().clone();
@@ -371,13 +366,12 @@ impl DockPanels<Panel> for Panels<'_> {
}
}
/// The graph editor, edge to edge in its group, as the window's document canvas.
/// The graph editor, edge to edge in its group.
fn group_view(&mut self, panels: &[Panel], _: usize, ui: &mut Ui, at: GroupAt, _: &mut BodyCx<'_>) -> Option<f32> {
let GroupAt::Show(rect) = at else { return None };
if !panels.contains(&Panel::Graph) {
return None;
}
crate::framework::host_canvas(ui.ctx(), graph_canvas_id());
crate::graph::show(self.app, ui);
Some(rect.height())
}
+1 -1
View File
@@ -158,7 +158,7 @@ pub(super) fn rename_dialog(app: &mut MaterialCraftApp, ctx: &egui::Context) {
.show(ctx, |ui| {
let response = ui.text_edit_singleline(&mut name);
crate::widgets::name(&response, egui::Role::TextInput, "Name");
if !ctx.memory(|memory| memory.has_focus(response.id)) && !ctx.egui_wants_keyboard_input() {
if !ctx.memory(|memory| memory.has_focus(response.id)) && !crate::framework::widget_has_keyboard(ctx) {
response.request_focus();
}
accept |= response.lost_focus() && ui.input(|input| input.key_pressed(egui::Key::Enter));
+44 -10
View File
@@ -6,20 +6,22 @@
//!
//! The runtime takes Escape, Tab and the clipboard events for the whole window unless one of MaterialCraft's own egui
//! widgets has the keyboard. The window's Escape is read with [`escape_pressed`] and taken with
//! [`take_window_cancel`]; the graph editor is declared to the runtime with [`host_canvas`], which keeps Tab, the edit
//! chords and the clipboard events in egui's input for MaterialCraft's shortcuts.
//! [`take_window_cancel`]; the graph editor's canvas is declared to the runtime with [`host_canvas`], which keeps the edit
//! chords and the clipboard events in egui's input for MaterialCraft's shortcuts, and Tab while it opens the node search.
//! The focused canvas leaves the keyboard to the shortcuts ([`widget_has_keyboard`]).
use std::sync::{Arc, OnceLock};
use craft_host::target::ShellInfo;
use craft_host::{AppInfo, Host};
use craft_keymap::Overrides;
use craft_keymap::{Chord, Key, Overrides};
use craft_ui::fonts::{I18nScripts, SystemFonts, UiFonts};
use craft_ui::runtime::commands::{CommandInput, CommandSet as _, QuitCommand};
use craft_ui::runtime::env::{RootEnv, Scale};
use craft_ui::runtime::focus::{EditSet, KeepKeys, KeyUse};
use craft_ui::runtime::focus::{self, EditSet, FocusTarget, KeyUse};
use craft_ui::runtime::frame::{FrameInput, FrameOutput};
use craft_ui::runtime::keys::{Intent, with_window_intents};
use craft_ui::runtime::overlay::{self, OpenFilter};
use craft_ui::shell::embed::{self, CloseDecision};
use craft_window::policy::Decision;
use craft_window::system::WindowSystem;
@@ -134,12 +136,44 @@ impl MaterialCraftApp {
}
}
/// Declares the graph editor `id`, drawn in this pass, as the window's document canvas. With nothing focused it is the
/// window's key target: Tab (node search), the edit chords and the clipboard events stay in egui's input for
/// MaterialCraft's shortcuts.
pub(crate) fn host_canvas(ctx: &egui::Context, id: egui::Id) {
const KEYS: KeyUse = KeyUse::CANVAS.with_keep(KeepKeys::Tab).with_edits(EditSet::ALL);
craft_ui::runtime::focus::host_canvas(ctx, id, KEYS);
/// The graph editor's canvas widget, the window's document canvas. The material, function and FX-Map editors share it,
/// so it keeps the focus when one replaces another.
pub fn canvas_id() -> egui::Id {
egui::Id::unique("materialcraft-graph-canvas")
}
/// Declares the graph editor's canvas, drawn in this pass, as the window's document canvas. With nothing or the canvas
/// focused, the edit chords and the clipboard events stay in egui's input for MaterialCraft's shortcuts. With
/// `opens_search` the focused canvas claims a bare Tab, which opens its node search; Shift+Tab and F6 always move on from
/// it, and Tab does otherwise.
pub(crate) fn host_canvas(ctx: &egui::Context, opens_search: bool) {
const KEYS: KeyUse = KeyUse::CANVAS.with_edits(EditSet::ALL);
const TAB: [Chord; 1] = [Chord::plain(Key::Tab)];
const SEARCH_KEYS: KeyUse = KEYS.with_claims(&TAB);
// The claim would hold with nothing focused too, where a bare Tab moves the focus into the window
let claims_tab = opens_search && ctx.memory(|memory| memory.has_focus(canvas_id()));
focus::host_canvas(ctx, canvas_id(), if claims_tab { SEARCH_KEYS } else { KEYS });
}
/// Gives the canvas the keyboard focus, with the arrows and Escape its own.
pub(crate) fn focus_canvas(ctx: &egui::Context) {
focus::request(ctx, FocusTarget::Id(canvas_id()));
}
/// The widget that has the keyboard, the canvas left out: the focused canvas leaves the keyboard to MaterialCraft's
/// shortcuts, as with nothing focused.
pub(crate) fn focused_widget(ctx: &egui::Context) -> Option<egui::Id> {
ctx.memory(|memory| memory.focused()).filter(|focused| *focused != canvas_id())
}
/// Whether a widget other than the canvas has the keyboard.
pub(crate) fn widget_has_keyboard(ctx: &egui::Context) -> bool {
focused_widget(ctx).is_some()
}
/// Whether a dialog, MaterialCraft's, egui's or the framework's, the home screen, or an egui menu or popup is up.
pub(crate) fn overlay_open(app: &MaterialCraftApp, ctx: &egui::Context) -> bool {
app.ui.windows.home || Modality::capture(app, ctx).is_blocked() || egui::Popup::is_any_open(ctx) || overlay::any_open(ctx, OpenFilter::Modal)
}
/// Whether Escape was pressed in this pass: from egui while one of MaterialCraft's own widgets or popups has the
+2
View File
@@ -39,6 +39,8 @@ pub(super) fn canvas(app: &mut MaterialCraftApp, editor: &mut FunctionEditor, ui
super::input::handle(app, editor, ui, &frame.response, &frame.ports, &frame.geometry, on_annotation);
let camera = GraphCamera { origin: frame.response.rect.min, pan: editor.pan, zoom: editor.zoom };
super::search::show(app, editor, ui, &camera);
let over_canvas = crate::graph::canvas_focus::under_pointer(ui, &frame.response, frame.response.hovered(), super::search::area());
crate::graph::canvas_focus::settle(app, ui, over_canvas && editor.search.is_none());
frame.geometry.publish(&mut app.graph, frame.response.rect);
if editor.drag.is_none() {
let camera = GraphCamera { origin: frame.response.rect.min, pan: editor.pan, zoom: editor.zoom };
+3 -2
View File
@@ -137,7 +137,8 @@ pub(super) fn handle(
}
}
if response.clicked() && !on_wire && !ui.input(|input| input.modifiers.alt) {
// A click of the pointer: Enter and Space on the focused canvas make it `clicked` too
if response.clicked_by(egui::PointerButton::Primary) && !on_wire && !ui.input(|input| input.modifiers.alt) {
crate::graph::node_selection::select(&mut editor.selection, None, ui.input(|input| input.modifiers));
crate::graph::node_selection::select(&mut editor.selected_wires, None, ui.input(|input| input.modifiers));
}
@@ -189,7 +190,7 @@ pub(super) fn handle(
// Space or Tab over the canvas, or a double-click on empty canvas, searches for a node to add
// Tab moves focus between widgets, so it waits only for text fields and focus held before this frame
let space = !ui.ctx().egui_wants_keyboard_input() && ui.input(|input| input.key_pressed(egui::Key::Space));
let space = !crate::framework::widget_has_keyboard(ui.ctx()) && ui.input(|input| input.key_pressed(egui::Key::Space));
let tab = app.ui.prefs.tab_opens_node_search
&& !app.graph.keyboard_was_focused
&& !ui.ctx().text_edit_focused()
+2 -1
View File
@@ -7,7 +7,8 @@ use egui::{Align2, Color32, Rect, Sense, Stroke, Ui, Vec2, pos2, vec2};
use std::collections::{BTreeMap, BTreeSet};
pub(super) fn paint(app: &mut MaterialCraftApp, editor: &mut FunctionEditor, ui: &mut Ui) -> Option<FunctionCanvas> {
let (canvas_bounds, response) = ui.allocate_exact_size(ui.available_size().max(vec2(200.0, 200.0)), Sense::click_and_drag());
let size = ui.available_size().max(vec2(200.0, 200.0));
let (canvas_bounds, response) = crate::graph::canvas_focus::allocate(app, ui, size);
app.graph.view = canvas_bounds;
let graph = editor.current(app)?;
let snap = app.ui.prefs.snap_to_grid;
+10 -2
View File
@@ -85,12 +85,17 @@ pub(crate) fn pick(app: &mut MaterialCraftApp, editor: &mut FunctionEditor, sear
editor.inspected_node = Some(id);
}
/// The search popup's area
pub(super) fn area() -> egui::Id {
egui::Id::unique("function-node-search")
}
pub(super) fn show(app: &mut MaterialCraftApp, editor: &mut FunctionEditor, ui: &mut Ui, camera: &GraphCamera) {
let blocked = crate::shell::modality::Modality::capture(app, ui.ctx()).is_blocked();
let Some(mut search) = editor.search.take() else { return };
let tokens = Tokens::get(ui.ctx());
let area = egui::Id::unique("function-node-search");
let area = area();
let mut close = false;
let mut chosen = None;
egui::Area::new(area).fixed_pos(camera.to_screen(search.at)).order(egui::Order::Foreground).show(ui.ctx(), |ui| {
@@ -155,7 +160,10 @@ pub(super) fn show(app: &mut MaterialCraftApp, editor: &mut FunctionEditor, ui:
&& ui.ctx().pointer_interact_pos().is_some_and(|pointer| !ui.ctx().layer_id_at(pointer).is_some_and(|layer| layer.id == area));
if let Some(candidate) = chosen.filter(|_| !blocked) {
pick(app, editor, &search, &candidate);
} else if !close && !clicked_outside {
crate::framework::focus_canvas(ui.ctx());
} else if close {
crate::framework::focus_canvas(ui.ctx());
} else if !clicked_outside {
editor.search = Some(search);
}
}
+4 -1
View File
@@ -51,7 +51,8 @@ pub fn show(app: &mut MaterialCraftApp, ui: &mut Ui) {
fn canvas(app: &mut MaterialCraftApp, ui: &mut Ui) {
let theme = Tokens::get(ui.ctx());
let context = ui.ctx().clone();
let (rect, response) = ui.allocate_exact_size(ui.available_size(), Sense::click_and_drag());
let size = ui.available_size();
let (rect, response) = super::canvas_focus::allocate(app, ui, size);
crate::widgets::name(&response, egui::Role::Canvas, "Node graph");
app.graph.view = rect;
if app.ui.frame_all || app.graph.framed_graph != Some(app.session.graph) {
@@ -350,6 +351,8 @@ fn canvas(app: &mut MaterialCraftApp, ui: &mut Ui) {
}
super::search::show(app, ui, &camera);
let over_canvas = super::canvas_focus::under_pointer(ui, &response, ui.rect_contains_pointer(rect), super::search::area());
super::canvas_focus::settle(app, ui, over_canvas && app.graph.search.is_none());
let (failed, placeholders) = app.session.cook_failures();
let mut at = rect.right_bottom() - vec2(8.0, 8.0);
if !failed.is_empty() {
+64
View File
@@ -0,0 +1,64 @@
//! The graph editor's canvas widget and its keyboard focus
use crate::MaterialCraftApp;
use crate::framework::{canvas_id, focus_canvas, focused_widget, host_canvas, overlay_open};
use crate::shell::modality::Modality;
use egui::{Id, Rect, Response, Sense, Ui, Vec2};
/// Allocate and draw the canvas widget: every editor (material, function, FX-Map) draws the one that has the focus
pub(crate) fn allocate(app: &MaterialCraftApp, ui: &mut Ui, size: Vec2) -> (Rect, Response) {
let (_, rect) = ui.allocate_space(size);
let response = ui.interact(rect, canvas_id(), Sense::click_and_drag());
// Declared before the widgets on the canvas, for its place in Tab order
host_canvas(ui.ctx(), false);
focus_on_press(app, ui, &response);
(rect, response)
}
/// A press anywhere on the canvas gives it the keyboard focus. Real presses only: with the canvas focused, egui makes
/// Enter and Space clicks of it.
fn focus_on_press(app: &MaterialCraftApp, ui: &Ui, canvas: &Response) {
let ctx = ui.ctx();
if !ui.input(|input| input.pointer.any_pressed()) || !ui.rect_contains_pointer(canvas.rect) || Modality::capture(app, ctx).is_blocked() {
return;
}
// A press in the value being edited on a node stays there
let in_editor = ctx.text_edit_focused() && focused_widget(ctx).and_then(|id| ctx.read_response(id)).is_some_and(|field| field.is_pointer_button_down_on());
if !in_editor {
focus_canvas(ctx);
}
}
/// Whether the pointer is over the canvas: `hovered`, or over the node search `search`, which still covers the canvas in
/// the passes after it closes
pub(crate) fn under_pointer(ui: &Ui, canvas: &Response, hovered: bool, search: Id) -> bool {
let ctx = ui.ctx();
hovered
|| ctx.pointer_interact_pos().is_some_and(|pointer| canvas.rect.contains(pointer) && ctx.layer_id_at(pointer).is_some_and(|layer| layer.id == search))
}
/// Settle the focus and Tab once the canvas has handled its input. A graph that opens or becomes active asks for the
/// focus, and so does the canvas while nothing is focused and Tab opens its node search (`opens_search`): a click on a
/// panel leaves the focus nowhere. It waits for a dialog, menu or popup to close, and leaves the focus with a text field
/// and with a widget of another window.
pub(crate) fn settle(app: &mut MaterialCraftApp, ui: &Ui, opens_search: bool) {
let ctx = ui.ctx();
let opens_search = opens_search && app.ui.prefs.tab_opens_node_search && !Modality::capture(app, ctx).is_blocked();
let shown = Some((app.session.document_id, app.session.graph, app.graph.annotation_target.clone()));
if app.graph.shown != shown {
app.graph.shown = shown;
app.graph.wants_focus = true;
}
let focused = ctx.memory(|memory| memory.focused());
let unfocused = opens_search && focused.is_none() && !ui.input(|input| input.pointer.any_down());
if (app.graph.wants_focus || unfocused) && !overlay_open(app, ctx) {
app.graph.wants_focus = false;
let same_window = focused.is_none_or(|id| ctx.read_response(id).is_some_and(|widget| widget.layer_id == ui.layer_id()));
if same_window && !ctx.text_edit_focused() {
focus_canvas(ctx);
}
}
host_canvas(ctx, opens_search);
}
@@ -103,6 +103,21 @@ fn space_tab_and_double_click_open_the_search_and_enter_adds() {
assert!(nodes.iter().any(|(_, op, _)| op == "add"));
}
#[test]
fn space_on_the_focused_canvas_keeps_the_selection() {
let (mut frames, constant) = function_canvas();
let at = empty(&frames);
frames.click(at, Modifiers::NONE);
let selection = vec![materialcraft_engine::core::NodeId(u64::from(constant))];
frames.app.session.selection = selection.clone();
frames.run(Vec::new(), Modifiers::NONE);
assert_eq!(frames.ctx.memory(|memory| memory.focused()), Some(crate::framework::canvas_id()));
frames.run(vec![press(Key::Space)], Modifiers::NONE);
assert!(search(&frames).is_some());
assert_eq!(frames.app.session.selection, selection, "egui's click for Space selects nothing");
}
#[test]
fn wires_dropped_on_empty_canvas_search_and_connect_the_new_node() {
let (mut frames, constant) = function_canvas();
+1 -1
View File
@@ -58,7 +58,7 @@ impl GraphCamera {
pub(crate) fn frame_key(ui: &Ui, view: Rect) -> bool {
ui.rect_contains_pointer(view)
&& !ui.ctx().egui_wants_keyboard_input()
&& !crate::framework::widget_has_keyboard(ui.ctx())
&& ui.input(|input| input.key_pressed(egui::Key::F) && input.modifiers.is_none())
}
}
+6
View File
@@ -5,6 +5,10 @@ use materialcraft_engine::core::NodeId;
pub struct GraphView {
pub(crate) inspected_node: Option<NodeId>,
pub(crate) keyboard_was_focused: bool,
/// The document, graph and nested editor shown last frame
pub(super) shown: Option<(u64, usize, materialcraft_engine::AnnotationTarget)>,
/// What is shown changed, and the canvas has yet to ask for the keyboard focus
pub(super) wants_focus: bool,
pub(super) thumbnails_cleared: bool,
pub(super) drag: Option<GraphDrag>,
pub(super) search: Option<NodeSearch>,
@@ -56,6 +60,8 @@ impl Default for GraphView {
GraphView {
inspected_node: None,
keyboard_was_focused: false,
shown: None,
wants_focus: false,
thumbnails_cleared: false,
drag: None,
search: None,
+1 -1
View File
@@ -277,7 +277,7 @@ pub(super) fn handle(
&& app.graph.search.is_none()
&& !crate::shell::modality::Modality::capture(app, &context).is_blocked()
{
let typing = context.egui_wants_keyboard_input();
let typing = crate::framework::widget_has_keyboard(&context);
let platform = craft_keymap::egui::platform(&context);
if !typing && let Some(entry) = context.input(|input| crate::node_shortcuts::pressed(&app.ui.prefs.node_shortcuts, input, platform)) {
let under_pointer = camera.to_graph(position);
+1
View File
@@ -3,6 +3,7 @@ pub(crate) mod add_node_menu;
pub(crate) mod annotation_canvas;
pub(crate) mod annotation_input;
pub(crate) mod canvas;
pub(crate) mod canvas_focus;
mod context_menu;
pub(crate) mod frame_hit;
#[cfg(test)]
+11 -2
View File
@@ -5,6 +5,11 @@ use egui_phosphor::regular as icon;
use materialcraft_engine::core::NodeId;
use serde_json::json;
/// The search popup's area
pub(super) fn area() -> egui::Id {
egui::Id::unique("node-search")
}
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;
@@ -45,7 +50,7 @@ pub(super) fn show(app: &mut MaterialCraftApp, ui: &mut Ui, cam: &GraphCamera) {
let screen = cam.to_screen(s.at);
let mut close = false;
let mut pick: Option<&'static str> = None;
egui::Area::new(egui::Id::unique("node-search")).fixed_pos(screen).order(egui::Order::Foreground).show(ui.ctx(), |ui| {
egui::Area::new(area()).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 {
@@ -110,7 +115,7 @@ pub(super) fn show(app: &mut MaterialCraftApp, ui: &mut Ui, cam: &GraphCamera) {
});
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")));
&& ui.ctx().pointer_interact_pos().is_some_and(|p| !ui.ctx().layer_id_at(p).is_some_and(|l| l.id == area()));
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 {
@@ -125,7 +130,11 @@ pub(super) fn show(app: &mut MaterialCraftApp, ui: &mut Ui, cam: &GraphCamera) {
app.run("graph.connect", json!({"from": id.0, "fromPort": output, "to": to.0, "toPort": port}));
}
}
crate::framework::focus_canvas(ui.ctx());
} else if close || clicked_out {
app.graph.search = None;
if close {
crate::framework::focus_canvas(ui.ctx());
}
}
}
+2 -2
View File
@@ -570,7 +570,7 @@ impl MaterialCraftApp {
let t0 = now_ms();
// Tab can focus a menu or toolbar button while drawing; preserve keyboard ownership
self.graph.keyboard_was_focused = ctx.egui_wants_keyboard_input();
self.graph.keyboard_was_focused = crate::framework::widget_has_keyboard(&ctx);
menus::menu_bar(self, ui);
if self.ui.prefs.show_toolbar {
toolbar::main_toolbar(self, ui);
@@ -585,7 +585,7 @@ impl MaterialCraftApp {
crate::window_controls::resize_zones(ui);
}
let gesture_active = ctx.input(|input| input.pointer.any_down()) || ctx.egui_wants_keyboard_input();
let gesture_active = ctx.input(|input| input.pointer.any_down()) || crate::framework::widget_has_keyboard(&ctx);
if !gesture_active && (self.ui_gesture_active || ctx.input(|input| input.pointer.any_released())) {
self.session.finish_edit();
}
+2 -2
View File
@@ -454,9 +454,9 @@ fn menu_item(app: &mut MaterialCraftApp, context: &egui::Context, ui: &mut egui:
}
}
/// Dispatch shortcuts unless a widget, popup or dialog owns the keyboard
/// Dispatch shortcuts unless a widget other than the canvas, a popup or a 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) {
if crate::framework::widget_has_keyboard(ctx) || egui::Popup::is_any_open(ctx) {
return;
}
+3 -1
View File
@@ -51,12 +51,14 @@ pub(crate) fn show(app: &mut MaterialCraftApp, ui: &mut Ui) {
ui.painter_at(response.rect).add(egui::epaint::CubicBezierShape::from_points_stroke(points, false, Color32::TRANSPARENT, Stroke::new(2.0, PORT_COLOR)));
}
response.context_menu(|ui| super::context_menu::show(app, &mut editor, ui));
crate::graph::canvas_focus::settle(app, ui, false);
editor.inspected_node = crate::graph::node_selection::inspected(&editor.selection, editor.inspected_node);
app.pattern_editor = Some(editor);
}
fn paint(app: &mut MaterialCraftApp, editor: &mut PatternEditor, ui: &mut Ui) -> Option<PatternCanvas> {
let (bounds, response) = ui.allocate_exact_size(ui.available_size().max(vec2(100.0, 100.0)), Sense::click_and_drag());
let size = ui.available_size().max(vec2(100.0, 100.0));
let (bounds, response) = crate::graph::canvas_focus::allocate(app, ui, size);
app.graph.view = bounds;
let graph = editor.current(app)?;
let selected: std::collections::BTreeSet<_> = editor.selection.iter().copied().collect();
+1 -1
View File
@@ -118,7 +118,7 @@ pub(super) fn handle(app: &mut MaterialCraftApp, editor: &mut PatternEditor, ui:
}
}
if response.hovered()
&& !ui.ctx().egui_wants_keyboard_input()
&& !crate::framework::widget_has_keyboard(ui.ctx())
&& ui.input(|input| input.key_pressed(egui::Key::Space))
&& !crate::shell::modality::Modality::capture(app, ui.ctx()).is_blocked()
{
+1 -1
View File
@@ -199,7 +199,7 @@ fn stretch_for(physical: [f32; 3], pixels: [u32; 2]) -> f32 {
/// Space toggles tiling, F fits the image and C, R, G, B and A pick the channels, while the pointer is over the image
fn keys(app: &mut MaterialCraftApp, ui: &Ui, response: &Response) {
if !response.hovered() || ui.ctx().egui_wants_keyboard_input() || crate::shell::modality::Modality::capture(app, ui.ctx()).is_blocked() {
if !response.hovered() || crate::framework::widget_has_keyboard(ui.ctx()) || crate::shell::modality::Modality::capture(app, ui.ctx()).is_blocked() {
return;
}
+1 -1
View File
@@ -130,7 +130,7 @@ pub(super) fn reset_selected(app: &mut MaterialCraftApp) {
/// The keys of the view, while the pointer is over it and nothing else has the keyboard
fn keys(app: &mut MaterialCraftApp, ui: &Ui, response: &Response) {
if !response.hovered() || ui.ctx().egui_wants_keyboard_input() || crate::shell::modality::Modality::capture(app, ui.ctx()).is_blocked() {
if !response.hovered() || crate::framework::widget_has_keyboard(ui.ctx()) || crate::shell::modality::Modality::capture(app, ui.ctx()).is_blocked() {
return;
}
+113
View File
@@ -0,0 +1,113 @@
//! The graph canvas as a Tab stop: it keeps the focus Tab gives it, a press and an opening graph give it the focus, Tab
//! over it opens the node search, and its keys work as with nothing focused
use egui::{Event, Id, Key, Modifiers, PointerButton, vec2};
use egui_kittest::{Harness, kittest::Queryable};
use materialcraft_ui_egui::framework::{canvas_id, install_headless};
use materialcraft_ui_egui::{MaterialCraftApp, UiState};
type AppHarness = Harness<'static, MaterialCraftApp>;
fn harness(session: materialcraft_engine::Session) -> AppHarness {
let mut h = Harness::builder().with_context_setup(install_headless).with_size(vec2(1600.0, 960.0)).build_ui_state(
|ui, app: &mut MaterialCraftApp| {
app.step(&ui.ctx().clone());
app.draw(ui);
},
MaterialCraftApp::headless(session, UiState::default()),
);
h.run_steps(8);
h
}
fn focused(h: &AppHarness) -> Option<Id> {
h.ctx.memory(|memory| memory.focused())
}
fn press(h: &mut AppHarness, modifiers: Modifiers, key: Key) -> Option<Id> {
h.key_press_modifiers(modifiers, key);
h.run_steps(3);
focused(h)
}
fn search_open(h: &AppHarness) -> bool {
h.query_by_label("Search nodes").is_some()
}
fn hover_canvas(h: &mut AppHarness) {
h.hover_at(h.state().graph.view.center());
h.run_steps(2);
}
#[test]
fn tab_walks_over_the_canvas_to_the_stops_after_it_and_back() {
let mut h = harness(materialcraft_engine::new_session());
let canvas = Some(canvas_id());
assert_eq!(focused(&h), canvas, "an opened graph has the keyboard");
let after = press(&mut h, Modifiers::NONE, Key::Tab);
assert!(after.is_some() && after != canvas, "Tab moves on from the canvas");
assert_eq!(press(&mut h, Modifiers::SHIFT, Key::Tab), canvas);
let before = press(&mut h, Modifiers::SHIFT, Key::Tab);
assert!(before.is_some() && before != canvas && before != after, "Shift+Tab leaves it backward");
assert_eq!(press(&mut h, Modifiers::NONE, Key::Tab), canvas, "Tab from the stop before it stops at it");
assert_eq!(press(&mut h, Modifiers::NONE, Key::Tab), after, "and reaches the stop after it");
assert!(!search_open(&h));
}
#[test]
fn tab_with_the_pointer_over_the_canvas_opens_the_search_and_shift_tab_and_f6_leave_it() {
let mut h = harness(materialcraft_engine::new_session());
let canvas = Some(canvas_id());
hover_canvas(&mut h);
for (modifiers, key) in [(Modifiers::SHIFT, Key::Tab), (Modifiers::NONE, Key::F6)] {
let left = press(&mut h, modifiers, key);
assert!(left.is_some() && left != canvas, "{key:?} leaves the canvas");
assert!(!search_open(&h));
h.ctx.memory_mut(|memory| memory.surrender_focus(left.unwrap()));
h.run_steps(2);
assert_eq!(focused(&h), canvas, "with nothing focused, the canvas under the pointer takes the keyboard");
}
press(&mut h, Modifiers::NONE, Key::Tab);
assert!(search_open(&h), "Tab opens the search");
assert_eq!(press(&mut h, Modifiers::NONE, Key::Escape), canvas, "closing it gives the keyboard back");
assert!(!search_open(&h));
}
#[test]
fn a_press_in_the_canvas_takes_the_keyboard_from_a_toolbar_button() {
let mut h = harness(materialcraft_engine::new_session());
h.get_by_label("Open package... (Ctrl+O)").focus();
h.run_steps(2);
press(&mut h, Modifiers::SHIFT, Key::Tab);
assert!(h.get_by_label("New graph... (Ctrl+N)").is_focused());
hover_canvas(&mut h);
assert!(h.get_by_label("New graph... (Ctrl+N)").is_focused(), "the pointer alone takes no focus from a widget");
let pos = h.state().graph.view.center();
for pressed in [true, false] {
h.event(Event::PointerButton { pos, button: PointerButton::Primary, pressed, modifiers: Modifiers::NONE });
h.run_steps(2);
assert_eq!(focused(&h), Some(canvas_id()));
}
press(&mut h, Modifiers::NONE, Key::Space);
assert!(search_open(&h), "Space goes to the canvas");
assert!(!h.state().ui.windows.new_graph, "and not to the button");
}
#[test]
fn shortcuts_run_with_the_canvas_focused() {
let mut h = harness(materialcraft_engine::sample_session());
assert_eq!(focused(&h), Some(canvas_id()));
let nodes = |h: &AppHarness| h.state().session.graph().unwrap().nodes.len();
let before = nodes(&h);
press(&mut h, Modifiers::COMMAND, Key::A);
assert_eq!(h.state().session.selection.len(), before, "Ctrl+A selects every node");
press(&mut h, Modifiers::NONE, Key::Delete);
assert_eq!(nodes(&h), 0, "Delete deletes them");
press(&mut h, Modifiers::COMMAND, Key::Z);
assert_eq!(nodes(&h), before, "Ctrl+Z brings them back");
assert_eq!(focused(&h), Some(canvas_id()));
}
+1
View File
@@ -2,6 +2,7 @@
//! Keep snapshot.rs standalone; language_switching.rs needs process isolation for global locale state.
//! resize.rs isolates its bounded render wait from unrelated tests sharing the global Rayon pool.
mod canvas_focus;
mod clipboard_shortcuts;
mod dialogs;
mod export_window;