bevy_gantz 0.2.0

A bevy plugin for gantz
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
//! Entity-based head management for gantz.
//!
//! This module provides Bevy components and resources for managing open graph
//! heads as entities rather than parallel `Vec`s.

use crate::reg::Registry;
use bevy_ecs::{prelude::*, query::QueryData};
use bevy_log as log;
use gantz_ca as ca;
use std::{
    collections::HashMap,
    ops::{Deref, DerefMut},
};
use steel::steel_vm::engine::Engine;

// ----------------------------------------------------------------------------
// QueryData
// ----------------------------------------------------------------------------

/// QueryData for accessing open head components.
///
/// Simplifies Query type signatures by bundling head-related components.
/// Use with `Query<OpenHeadData<N>, With<OpenHead>>`.
#[derive(QueryData)]
#[query_data(mutable)]
pub struct OpenHeadData<N: 'static + Send + Sync> {
    pub entity: Entity,
    pub head_ref: &'static mut HeadRef,
    pub working_graph: &'static mut WorkingGraph<N>,
    pub compiled: &'static mut CompiledModule,
}

// ----------------------------------------------------------------------------
// Components
// ----------------------------------------------------------------------------

/// Marker component for an open gantz head entity.
#[derive(Component)]
pub struct OpenHead;

/// The gantz_ca::Head (branch or commit reference).
#[derive(Component, Clone)]
pub struct HeadRef(pub ca::Head);

/// The working copy of the graph associated with this head.
#[derive(Component)]
pub struct WorkingGraph<N>(pub gantz_core::node::graph::Graph<N>);

/// The compiled Steel module for this head (as a string).
#[derive(Component, Default, Clone)]
pub struct CompiledModule(pub String);

// ----------------------------------------------------------------------------
// Events
// ----------------------------------------------------------------------------

/// Event to open a head as a new tab (or focus if already open).
#[derive(Event)]
pub struct OpenEvent(pub ca::Head);

/// Event to close a head tab.
#[derive(Event)]
pub struct CloseEvent(pub ca::Head);

/// Event to replace the focused head with a different head.
#[derive(Event)]
pub struct ReplaceEvent(pub ca::Head);

/// Event to create a new branch from an existing head.
#[derive(Event)]
pub struct BranchEvent {
    pub original: ca::Head,
    pub new_name: String,
}

/// Event to move a branch's commit pointer to a different commit.
#[derive(Event)]
pub struct MoveBranchEvent {
    pub entity: Entity,
    pub name: ca::Branch,
    pub target: ca::CommitAddr,
}

// ----------------------------------------------------------------------------
// Hook Events (emitted after core operations for app-specific handling)
// ----------------------------------------------------------------------------

/// Emitted after a head has been opened.
#[derive(Event)]
pub struct OpenedEvent {
    pub entity: Entity,
    pub head: ca::Head,
}

/// Emitted after a head has been closed.
#[derive(Event)]
pub struct ClosedEvent {
    pub entity: Entity,
    pub head: ca::Head,
}

/// Emitted when a head's backing data has changed (replacement, branch move, etc.).
#[derive(Event)]
pub struct ChangedEvent {
    pub entity: Entity,
    pub old_head: ca::Head,
    pub new_head: ca::Head,
}

/// Emitted after a branch has been created from a head.
#[derive(Event)]
pub struct BranchedEvent {
    pub entity: Entity,
    pub old_head: ca::Head,
    pub new_head: ca::Head,
}

/// Emitted when a head's working graph is committed (graph changed).
///
/// This event is emitted by `vm::update` when it detects a graph change
/// and commits to the registry. Apps can observe this to update UI state.
#[derive(Event)]
pub struct CommittedEvent {
    pub entity: Entity,
    pub old_head: ca::Head,
    pub new_head: ca::Head,
}

// ----------------------------------------------------------------------------
// Resources
// ----------------------------------------------------------------------------

/// Per-head VMs stored in a NonSend resource.
///
/// VMs are keyed by Entity ID since `Engine` is not `Send`.
#[derive(Default)]
pub struct HeadVms(pub HashMap<Entity, Engine>);

/// The currently focused head entity.
#[derive(Resource, Default)]
pub struct FocusedHead(pub Option<Entity>);

/// Tab ordering for open heads (entities in display order).
#[derive(Resource, Default)]
pub struct HeadTabOrder(pub Vec<Entity>);

// ----------------------------------------------------------------------------
// Deref impls
// ----------------------------------------------------------------------------

impl Deref for HeadRef {
    type Target = ca::Head;
    fn deref(&self) -> &Self::Target {
        &self.0
    }
}

impl DerefMut for HeadRef {
    fn deref_mut(&mut self) -> &mut Self::Target {
        &mut self.0
    }
}

impl<N> Deref for WorkingGraph<N> {
    type Target = gantz_core::node::graph::Graph<N>;
    fn deref(&self) -> &Self::Target {
        &self.0
    }
}

impl<N> DerefMut for WorkingGraph<N> {
    fn deref_mut(&mut self) -> &mut Self::Target {
        &mut self.0
    }
}

impl Deref for CompiledModule {
    type Target = str;
    fn deref(&self) -> &Self::Target {
        &self.0
    }
}

impl Deref for HeadVms {
    type Target = HashMap<Entity, Engine>;
    fn deref(&self) -> &Self::Target {
        &self.0
    }
}

impl DerefMut for HeadVms {
    fn deref_mut(&mut self) -> &mut Self::Target {
        &mut self.0
    }
}

impl Deref for FocusedHead {
    type Target = Option<Entity>;
    fn deref(&self) -> &Self::Target {
        &self.0
    }
}

impl DerefMut for FocusedHead {
    fn deref_mut(&mut self) -> &mut Self::Target {
        &mut self.0
    }
}

impl Deref for HeadTabOrder {
    type Target = Vec<Entity>;
    fn deref(&self) -> &Self::Target {
        &self.0
    }
}

impl DerefMut for HeadTabOrder {
    fn deref_mut(&mut self) -> &mut Self::Target {
        &mut self.0
    }
}

// ----------------------------------------------------------------------------
// Utility fns
// ----------------------------------------------------------------------------

/// Find the entity for the given head, if it exists.
pub fn find_entity(
    head: &ca::Head,
    heads: &Query<(Entity, &HeadRef), With<OpenHead>>,
) -> Option<Entity> {
    heads
        .iter()
        .find(|(_, head_ref)| &***head_ref == head)
        .map(|(entity, _)| entity)
}

/// Check if the given head is the currently focused head.
pub fn is_focused(
    head: &ca::Head,
    heads: &Query<(Entity, &HeadRef), With<OpenHead>>,
    focused: &FocusedHead,
) -> bool {
    find_entity(head, heads)
        .map(|entity| **focused == Some(entity))
        .unwrap_or(false)
}

// ----------------------------------------------------------------------------
// Event Handlers (Observers)
// ----------------------------------------------------------------------------

/// Handle request to open a head as a new tab (or focus if already open).
pub fn on_open<N>(
    trigger: On<OpenEvent>,
    mut cmds: Commands,
    registry: Res<Registry<N>>,
    mut tab_order: ResMut<HeadTabOrder>,
    mut focused: ResMut<FocusedHead>,
    heads: Query<(Entity, &HeadRef), With<OpenHead>>,
) where
    N: 'static + Clone + Send + Sync,
{
    let OpenEvent(new_head) = trigger.event();

    // If already open, just focus it.
    if let Some(entity) = find_entity(new_head, &heads) {
        **focused = Some(entity);
        return;
    }

    // Get graph from registry.
    let Some(graph) = registry.head_graph(new_head).cloned() else {
        log::error!("cannot open head: graph missing from registry");
        return;
    };

    // Spawn entity (NO CompiledModule - app observer adds it after VM init).
    // Note: HeadGuiState and GraphViews are added by GantzEguiPlugin observer.
    let entity = cmds
        .spawn((OpenHead, HeadRef(new_head.clone()), WorkingGraph(graph)))
        .id();

    tab_order.push(entity);
    **focused = Some(entity);

    // Emit hook for app to do VM init + GUI state + views.
    cmds.trigger(OpenedEvent {
        entity,
        head: new_head.clone(),
    });
}

/// Handle request to replace the focused head with a different head.
pub fn on_replace<N>(
    trigger: On<ReplaceEvent>,
    mut cmds: Commands,
    registry: Res<Registry<N>>,
    mut focused: ResMut<FocusedHead>,
    heads: Query<(Entity, &HeadRef), With<OpenHead>>,
) where
    N: 'static + Clone + Send + Sync,
{
    let ReplaceEvent(new_head) = trigger.event();

    // If new head already open, just focus it.
    if let Some(entity) = find_entity(new_head, &heads) {
        **focused = Some(entity);
        return;
    }

    let Some(focused_entity) = **focused else {
        return;
    };
    let old_head = heads.get(focused_entity).ok().map(|(_, h)| (**h).clone());

    // Get new graph.
    let Some(graph) = registry.head_graph(new_head).cloned() else {
        log::error!("cannot replace head: graph missing from registry");
        return;
    };

    // Update entity components (NO CompiledModule - app observer adds it).
    // Note: HeadGuiState and GraphViews are updated by GantzEguiPlugin observer.
    cmds.entity(focused_entity)
        .insert(HeadRef(new_head.clone()))
        .insert(WorkingGraph(graph));

    // Emit hook.
    if let Some(old) = old_head {
        cmds.trigger(ChangedEvent {
            entity: focused_entity,
            old_head: old,
            new_head: new_head.clone(),
        });
    }
}

/// Handle request to close a head tab.
pub fn on_close<N>(
    trigger: On<CloseEvent>,
    mut cmds: Commands,
    mut tab_order: ResMut<HeadTabOrder>,
    mut focused: ResMut<FocusedHead>,
    mut vms: NonSendMut<HeadVms>,
    heads: Query<(Entity, &HeadRef), With<OpenHead>>,
) where
    N: 'static + Send + Sync,
{
    let CloseEvent(head) = trigger.event();

    // Don't close if last head.
    if tab_order.len() <= 1 {
        return;
    }

    let Some(entity) = find_entity(head, &heads) else {
        return;
    };
    let Some(ix) = tab_order.iter().position(|&x| x == entity) else {
        return;
    };

    // Clean up VM for this head.
    vms.remove(&entity);

    cmds.entity(entity).despawn();
    tab_order.retain(|&x| x != entity);

    // Update focus to remain valid.
    if **focused == Some(entity) {
        let new_ix = ix.saturating_sub(1).min(tab_order.len().saturating_sub(1));
        **focused = tab_order.get(new_ix).copied();
    }

    cmds.trigger(ClosedEvent {
        entity,
        head: head.clone(),
    });
}

/// Handle request to create a new branch from an existing head.
pub fn on_branch<N>(
    trigger: On<BranchEvent>,
    mut cmds: Commands,
    mut registry: ResMut<Registry<N>>,
    mut heads: Query<(Entity, &mut HeadRef), With<OpenHead>>,
) where
    N: 'static + Send + Sync,
{
    let BranchEvent { original, new_name } = trigger.event();

    // Get commit CA from original head.
    let Some(commit_ca) = registry.head_commit_ca(original).copied() else {
        log::error!("Failed to get commit address for head: {:?}", original);
        return;
    };

    // Insert new branch name.
    registry.insert_name(new_name.clone(), commit_ca);

    // Find and update the entity.
    let new_head = ca::Head::Branch(new_name.clone());
    for (entity, mut head_ref) in heads.iter_mut() {
        if &**head_ref == original {
            let old_head = (**head_ref).clone();
            **head_ref = new_head.clone();

            cmds.trigger(BranchedEvent {
                entity,
                old_head,
                new_head,
            });
            break;
        }
    }
}

/// Handle request to move a branch's commit pointer to a different commit.
///
/// Atomically updates the registry, WorkingGraph, and emits ChangedEvent
/// within the command flush to prevent inconsistent state between systems.
pub fn on_move_branch<N>(
    trigger: On<MoveBranchEvent>,
    mut cmds: Commands,
    mut registry: ResMut<Registry<N>>,
) where
    N: 'static + Clone + Send + Sync,
{
    let event = trigger.event();
    registry.insert_name(event.name.clone(), event.target);
    let head = ca::Head::Branch(event.name.clone());
    let Some(graph) = registry.head_graph(&head).cloned() else {
        log::error!("MoveBranch: graph missing for target commit");
        return;
    };
    cmds.entity(event.entity).insert(WorkingGraph(graph));
    cmds.trigger(ChangedEvent {
        entity: event.entity,
        old_head: head.clone(),
        new_head: head,
    });
}