From 38aa7b244da756ec05a0dec20dd8e5932e1cc013 Mon Sep 17 00:00:00 2001 From: Sander van Rossen Date: Sun, 22 Nov 2026 15:10:47 +0100 Subject: [PATCH] Set a shortcut mode from where the focus is, so two panels of one mode don't undo each other A NodeView or VirtualTable set the app's one command mode while it was built: its own when it had the focus, else Default when the mode was its own. Two of them with one mode (two NodeView or two Hierarchy panels) undid each other when both were built in one frame, as after an edit in either: the focused one's shortcuts (F, Delete, Ctrl+G) stopped working, or the mode flipped twice a frame and cancelled a chord. - ClaimCommandMode(owner, mode): a control records its mode as it's built. The mode is set once a frame, before input is handled, from the claim the focus is within; Default once it's in none, unless the app set a mode of its own since. Claims no longer built drop out once the focus isn't in them. - NodeView and VirtualTable claim their modes instead of setting them. - Docs: two tables of one mode, and ClaimCommandMode, in the tables guide. - Tests: two node views of one mode keep the focused one's mode, whichever is built first. --- CHANGELOG.md | 1 + docs/controls/tables.md | 3 ++ src/gui_controls/controls_docking.cpp | 42 +++++++++++++++++++++ src/gui_controls/controls_node_view.cpp | 11 +----- src/gui_controls/controls_table_virtual.cpp | 13 +------ src/gui_controls/gui_application.h | 17 +++++++++ tests/node_view_two_views_test.cpp | 15 ++++++++ 7 files changed, 82 insertions(+), 20 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 2e902f91..c054bf24 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -296,3 +296,4 @@ First public preview. The API is not stable yet. - The library gives back the static IDs it makes. A panel `CreatePanel` made an ID for releases it when it closes: its tab, its window, its document, or a layout applied without it. `ClosePanel` closes a panel wherever it is, `ReleaseDockingLayout` closes a whole layout, and `CloseAllDocuments` closes a document set. `Cleanup` releases the application's own window IDs (menus, tooltips, pickers, toasts, popovers) and closes its torn-off panel windows. The showcase closes its documents at exit and holds no static ID when it's done. - Fixed: closing a docked panel with Ctrl+W, Ctrl+F4 or its tab menu's Close no longer frees what it holds while that frame still draws it. A viewport's scene drawn that frame read freed memory. A closed panel leaves its layout at once, and its callbacks are destroyed when the next frame starts. - Fixed: `FloatToHalf` lost the carry when rounding up past the largest mantissa, so 0.99995 became 0.5 and 65530 became 32768; it now carries into the exponent, and to infinity past the largest half. A gradient at an angle near an axis, whose rotation is packed this way, pointed the wrong way. `HalfToFloat` read subnormal halves as half their value. +- Fixed: two node views or tables with one shortcut mode (two panels of a kind) undid each other's mode, so the focused one's shortcuts could stop working while both were built. `ClaimCommandMode(owner, mode)` records a control's mode as it's built, and the mode is set once a frame, before input is handled, from where the focus is; Default once the focus is in none, unless the app set a mode of its own. diff --git a/docs/controls/tables.md b/docs/controls/tables.md index 3d62a84c..d7d3747d 100644 --- a/docs/controls/tables.md +++ b/docs/controls/tables.md @@ -93,6 +93,9 @@ tableState.contextMenu = [this](int row) { Keyboard shortcuts that only work in the table: give it a `commandMode`. - The mode is on while the focus is in the table, and Default again once the focus leaves. +- Two tables with one mode (two panels of a kind) don't undo each other: the mode is the + one the focus is in. +- Your own controls can do the same with `ClaimCommandMode(id, mode)`, called as they're built. - Register the mode's commands once, with the `CommandManager`. - For example, Delete deletes the selected rows. - A key the table uses itself, such as the arrow keys, doesn't reach the commands. diff --git a/src/gui_controls/controls_docking.cpp b/src/gui_controls/controls_docking.cpp index 4ef2bb6f..af81d707 100644 --- a/src/gui_controls/controls_docking.cpp +++ b/src/gui_controls/controls_docking.cpp @@ -1239,8 +1239,50 @@ namespace tachyon // m_dockingLayoutsThisFrame stays: onDragOut, run during input dispatch, reads it. // BeginDockingFrame clears it. } + void GUIApplication::ClaimCommandMode(ID owner, CommandMode mode) + { + if (owner == ID::Invalid() || mode == CommandMode::Default) + return; + m_commandModeClaims[owner] = { mode, m_commandModeFrame }; + } + + void GUIApplication::ApplyClaimedCommandMode() + { + // The claim the focus is in: the focused element's own, else one around it. + const InteractionState& interaction = guiStateManager.Interaction(); + const ID focused = interaction.GetFocusedID(); + std::optional mode; + for (auto it = m_commandModeClaims.begin(); it != m_commandModeClaims.end(); ) + { + const bool within = interaction.IsFocusWithin(it->first); + if (within && (!mode || it->first == focused)) + mode = it->second.mode; + // Not built this frame, and not where the focus is: gone (a closed panel's). + if (!within && it->second.frame != m_commandModeFrame) + it = m_commandModeClaims.erase(it); + else + ++it; + } + ++m_commandModeFrame; + + if (mode) + { + if (commandManager.GetMode() != *mode) + commandManager.SetMode(*mode); + m_claimedCommandMode = *mode; + } + else if (m_claimedCommandMode != CommandMode::Default) + { + // The focus left: Default again, unless the app set a mode of its own since. + if (commandManager.GetMode() == m_claimedCommandMode) + commandManager.SetMode(CommandMode::Default); + m_claimedCommandMode = CommandMode::Default; + } + } + void GUIApplication::DispatchInput(ID overID) { + ApplyClaimedCommandMode(); // What the user acts on, taken before the handlers move the focus or the capture. std::vector actedOn = ActedOnIDs(overID); if (inputManager) diff --git a/src/gui_controls/controls_node_view.cpp b/src/gui_controls/controls_node_view.cpp index 2effc3ae..5380c244 100644 --- a/src/gui_controls/controls_node_view.cpp +++ b/src/gui_controls/controls_node_view.cpp @@ -474,15 +474,8 @@ namespace tachyon view.state.transitionMenuTransition.reset(); } - // Edit-mode wiring: while this canvas has focus, set the command - // manager to the view's mode; restore Default when focus leaves. - if (guiStateManager.Interaction().GetFocusedID() == canvasID) - { - if (commandManager.GetMode() != view.state.commandMode) - commandManager.SetMode(view.state.commandMode); - } else - if (commandManager.GetMode() == view.state.commandMode) - commandManager.SetMode(CommandMode::Default); + // Its shortcuts while the focus is on the canvas. + ClaimCommandMode(canvasID, view.state.commandMode); // ---- Canvas: pan/zoom + focus capture. Everything below renders as // children of this element under the view's content transform. diff --git a/src/gui_controls/controls_table_virtual.cpp b/src/gui_controls/controls_table_virtual.cpp index a7d35ea8..0edf0f26 100644 --- a/src/gui_controls/controls_table_virtual.cpp +++ b/src/gui_controls/controls_table_virtual.cpp @@ -1554,17 +1554,8 @@ namespace tachyon m_tableEmit = std::move(m_outerTableEmits.back()); m_outerTableEmits.pop_back(); - // Its shortcuts while the focus is in it; Default again once it leaves. - if (state.commandMode != CommandMode::Default) - { - if (guiStateManager.Interaction().IsFocusWithin(id)) - { - if (commandManager.GetMode() != state.commandMode) - commandManager.SetMode(state.commandMode); - } - else if (commandManager.GetMode() == state.commandMode) - commandManager.SetMode(CommandMode::Default); - } + // Its shortcuts while the focus is in it. + ClaimCommandMode(id, state.commandMode); // The context menu: a shown row's, for that row; the sections' bodies', below the rows. if (state.contextMenu) diff --git a/src/gui_controls/gui_application.h b/src/gui_controls/gui_application.h index 7b7a23b0..61bd936a 100644 --- a/src/gui_controls/gui_application.h +++ b/src/gui_controls/gui_application.h @@ -370,6 +370,13 @@ namespace tachyon /// Closes the panel at `tabIndex` of a docked leaf, as the tab's Close action does. void CloseDockedPanel(DockingLayout& layout, DockNode& leaf, int tabIndex); + /// For a control with shortcuts of its own (CommandManager::RegisterMode): while the + /// focus is within `owner`, the command mode is `mode`; once it's within none, Default + /// again. Call it as the control is built, every frame it is. The mode is set once a + /// frame, before input is handled, from where the focus is, so two controls of one mode + /// (two panels of a kind) don't undo each other. Default claims nothing. + void ClaimCommandMode(ID owner, CommandMode mode); + /// Closes a panel of `layout` for good, wherever it is: out of its tree (its leaf /// closing up when it empties), hidden first (onDisable) if it's shown, its ID back when /// CreatePanel made it. Its callbacks, and what they hold, are destroyed when the next @@ -3073,6 +3080,13 @@ namespace tachyon std::vector m_dockingLayoutsThisFrame{}; /// Panels closed this frame, kept until the next one starts (ClosePanel). std::vector m_closedPanels; + /// The controls with a command mode of their own (ClaimCommandMode), by their ID, and + /// the frame each was last built in. + struct CommandModeClaim { CommandMode mode; uint64_t frame; }; + std::unordered_map m_commandModeClaims; + uint64_t m_commandModeFrame = 0; + /// The mode a claim set, while it holds; Default when none does. + CommandMode m_claimedCommandMode = CommandMode::Default; /// Toolbar docks emitted this frame, walked by SyncToolbarDockRects (type in controls_toolbar_dock.h). std::vector m_toolbarDocksThisFrame{}; @@ -3386,6 +3400,9 @@ namespace tachyon void ReleaseOwnIDs(); /// Destroys every panel of `layout`, as ClosePanel does each, leaving its tree. void ErasePanels(DockingLayout& layout); + /// Sets the command mode from the claims (ClaimCommandMode) the focus is within, before + /// the frame's input is handled; drops claims no longer built that the focus isn't in. + void ApplyClaimedCommandMode(); /// Hands the frame's input to the element handlers, then calls onChanged of the docked /// panels the user acted in. void DispatchInput(ID overID); diff --git a/tests/node_view_two_views_test.cpp b/tests/node_view_two_views_test.cpp index 64559a0f..bb5b3aa1 100644 --- a/tests/node_view_two_views_test.cpp +++ b/tests/node_view_two_views_test.cpp @@ -122,6 +122,21 @@ namespace tachyon EXPECT_NE(NodeRect(right, nodeA).position, rightBefore.position) << "the other view shows where it went"; } + // Two views with one shortcut mode: the mode is the focused view's, whichever of the two + // is built first, and it holds while the focus moves between them. + TEST_F(NodeViewTwoViewsTest, TheFocusedViewsShortcutModeHolds) + { + const CommandMode mode = app.commandManager.RegisterMode("Graph"); + left.commandMode = right.commandMode = mode; + Settle(); + app.ClickAt({ 600, 700 }); // the left view's empty canvas + Settle(); + EXPECT_EQ(app.commandManager.GetMode(), mode) << "the left one focused, built before the right"; + app.ClickAt({ 1400, 700 }); // the right one's + Settle(); + EXPECT_EQ(app.commandManager.GetMode(), mode) << "the right one focused, built after the left"; + } + // A node removed through one view goes from the other's selection on its next build, // which reports the change. TEST_F(NodeViewTwoViewsTest, ANodeRemovedInOneViewLeavesTheOthersSelection)