Skip to main content

kernel/
actions.rs

1// Copyright 2026 Mark Alan Boykin
2// This Source Code Form is subject to the terms of the Mozilla Public
3// License, v. 2.0. If a copy of the MPL was not distributed with this
4// file, You can obtain one at https://mozilla.org/MPL/2.0/.
5// SPDX-License-Identifier: MPL-2.0
6
7//! The shared commands as a typed vocabulary.
8//!
9//! [`ActionId`] is the typed form of `hest`'s catalogue (Scenograph
10//! editor plan, SE49 and SE50): one variant per shared id, its key and label
11//! read from the catalogue, so a command is defined once. ux-events and
12//! `HostIntent` carry it where a typed id is wanted; hosts register the same
13//! ids as strings. The egui-era catalogue it replaces (68 actions, four
14//! categories) was audited in the plan's F12.
15
16use hest::catalogue::{self, ids};
17use serde::{Deserialize, Serialize};
18
19/// A shared command.
20#[derive(Clone, Copy, PartialEq, Eq, Debug, Hash, Serialize, Deserialize)]
21pub enum ActionId {
22    NodeNew,
23    NodeDelete,
24    NodeEdit,
25    NodePin,
26    NodeUnpin,
27    NodePinToggle,
28    NodeViewerAuto,
29    RelationAdd,
30    RelationAddBoth,
31    RelationRetract,
32    IdentityWebfinger,
33    IdentityActivitypub,
34    IdentityNip05,
35    WorkbenchSplitBeside,
36    WorkbenchSplitOut,
37    WorkbenchStackOnto,
38    ViewFit,
39    ViewZoomIn,
40    ViewZoomOut,
41    PhysicsToggle,
42    PhysicsSettings,
43    SessionUndo,
44    SessionRedo,
45    SessionSave,
46    PaneSettings,
47    PaneTrail,
48    PaneWorkbench,
49    PaletteOpen,
50    ImportBookmarks,
51    NavBack,
52    NavForward,
53    NavReload,
54    NavStop,
55    FoldSelection,
56    FoldCollapseDescendants,
57    FoldExpand,
58    ArrangementResetToSeed,
59    ArrangementSetSeed,
60    ArrangementRestoreRecipeSeed,
61    ArrangementDismissRetained,
62    NodeForegroundPin,
63    TileGrow,
64    TileShrink,
65    ViewTerritory,
66}
67
68impl ActionId {
69    /// Every variant, in the catalogue's order.
70    pub const ALL: [Self; 44] = [
71        Self::NodeNew,
72        Self::NodeDelete,
73        Self::NodeEdit,
74        Self::NodePin,
75        Self::NodeUnpin,
76        Self::NodePinToggle,
77        Self::NodeViewerAuto,
78        Self::RelationAdd,
79        Self::RelationAddBoth,
80        Self::RelationRetract,
81        Self::IdentityWebfinger,
82        Self::IdentityActivitypub,
83        Self::IdentityNip05,
84        Self::WorkbenchSplitBeside,
85        Self::WorkbenchSplitOut,
86        Self::WorkbenchStackOnto,
87        Self::ViewFit,
88        Self::ViewZoomIn,
89        Self::ViewZoomOut,
90        Self::PhysicsToggle,
91        Self::PhysicsSettings,
92        Self::SessionUndo,
93        Self::SessionRedo,
94        Self::SessionSave,
95        Self::PaneSettings,
96        Self::PaneTrail,
97        Self::PaneWorkbench,
98        Self::PaletteOpen,
99        Self::ImportBookmarks,
100        Self::NavBack,
101        Self::NavForward,
102        Self::NavReload,
103        Self::NavStop,
104        Self::FoldSelection,
105        Self::FoldCollapseDescendants,
106        Self::FoldExpand,
107        Self::ArrangementResetToSeed,
108        Self::ArrangementSetSeed,
109        Self::ArrangementRestoreRecipeSeed,
110        Self::ArrangementDismissRetained,
111        Self::NodeForegroundPin,
112        Self::TileGrow,
113        Self::TileShrink,
114        Self::ViewTerritory,
115    ];
116
117    /// The shared id, `namespace:name`.
118    pub fn key(self) -> &'static str {
119        match self {
120            Self::NodeNew => ids::NODE_NEW,
121            Self::NodeDelete => ids::NODE_DELETE,
122            Self::NodeEdit => ids::NODE_EDIT,
123            Self::NodePin => ids::NODE_PIN,
124            Self::NodeUnpin => ids::NODE_UNPIN,
125            Self::NodePinToggle => ids::NODE_PIN_TOGGLE,
126            Self::NodeViewerAuto => ids::NODE_VIEWER_AUTO,
127            Self::RelationAdd => ids::RELATION_ADD,
128            Self::RelationAddBoth => ids::RELATION_ADD_BOTH,
129            Self::RelationRetract => ids::RELATION_RETRACT,
130            Self::IdentityWebfinger => ids::IDENTITY_WEBFINGER,
131            Self::IdentityActivitypub => ids::IDENTITY_ACTIVITYPUB,
132            Self::IdentityNip05 => ids::IDENTITY_NIP05,
133            Self::WorkbenchSplitBeside => ids::WORKBENCH_SPLIT_BESIDE,
134            Self::WorkbenchSplitOut => ids::WORKBENCH_SPLIT_OUT,
135            Self::WorkbenchStackOnto => ids::WORKBENCH_STACK_ONTO,
136            Self::ViewFit => ids::VIEW_FIT,
137            Self::ViewZoomIn => ids::VIEW_ZOOM_IN,
138            Self::ViewZoomOut => ids::VIEW_ZOOM_OUT,
139            Self::PhysicsToggle => ids::PHYSICS_TOGGLE,
140            Self::PhysicsSettings => ids::PHYSICS_SETTINGS,
141            Self::SessionUndo => ids::SESSION_UNDO,
142            Self::SessionRedo => ids::SESSION_REDO,
143            Self::SessionSave => ids::SESSION_SAVE,
144            Self::PaneSettings => ids::PANE_SETTINGS,
145            Self::PaneTrail => ids::PANE_TRAIL,
146            Self::PaneWorkbench => ids::PANE_WORKBENCH,
147            Self::PaletteOpen => ids::PALETTE_OPEN,
148            Self::ImportBookmarks => ids::IMPORT_BOOKMARKS,
149            Self::NavBack => ids::NAV_BACK,
150            Self::NavForward => ids::NAV_FORWARD,
151            Self::NavReload => ids::NAV_RELOAD,
152            Self::NavStop => ids::NAV_STOP,
153            Self::FoldSelection => ids::FOLD_SELECTION,
154            Self::FoldCollapseDescendants => ids::FOLD_COLLAPSE_DESCENDANTS,
155            Self::FoldExpand => ids::FOLD_EXPAND,
156            Self::ArrangementResetToSeed => ids::ARRANGEMENT_RESET_TO_SEED,
157            Self::ArrangementSetSeed => ids::ARRANGEMENT_SET_SEED,
158            Self::ArrangementRestoreRecipeSeed => ids::ARRANGEMENT_RESTORE_RECIPE_SEED,
159            Self::ArrangementDismissRetained => ids::ARRANGEMENT_DISMISS_RETAINED,
160            Self::NodeForegroundPin => ids::NODE_FOREGROUND_PIN,
161            Self::TileGrow => ids::TILE_GROW,
162            Self::TileShrink => ids::TILE_SHRINK,
163            Self::ViewTerritory => ids::VIEW_TERRITORY,
164        }
165    }
166
167    /// The catalogue's label.
168    pub fn label(self) -> &'static str {
169        catalogue::label(self.key()).expect("every variant is in the catalogue")
170    }
171
172    /// What it acts on: the id's namespace.
173    pub fn namespace(self) -> &'static str {
174        catalogue::namespace(self.key())
175    }
176
177    /// The variant for a shared id.
178    pub fn from_key(key: &str) -> Option<Self> {
179        Self::ALL.into_iter().find(|action| action.key() == key)
180    }
181}
182
183#[cfg(test)]
184mod tests {
185    use super::*;
186
187    #[test]
188    fn every_shared_command_round_trips_through_the_typed_catalogue() {
189        assert_eq!(ActionId::ALL.len(), catalogue::CATALOGUE.len());
190        for action in ActionId::ALL {
191            assert_eq!(ActionId::from_key(action.key()), Some(action));
192            assert_eq!(catalogue::label(action.key()), Some(action.label()));
193        }
194        for (key, action) in [
195            ("fold:selection", ActionId::FoldSelection),
196            (
197                "fold:collapse_descendants",
198                ActionId::FoldCollapseDescendants,
199            ),
200            ("fold:expand", ActionId::FoldExpand),
201        ] {
202            assert_eq!(ActionId::from_key(key), Some(action));
203            assert_eq!(action.namespace(), "fold");
204        }
205        assert_eq!(ActionId::from_key("fold:unknown"), None);
206    }
207}