concinnity-dev 0.18.69

The Concinnity dev tooling library: world authoring, the in-engine editor, the debug server, docs and packaging
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
// src/editor/notify.rs
//
// Editor notifications: the queue of transient toast messages the rest of the
// editor (and its worker threads) pushes into, plus the pure lifetime and
// stack policy the overlay draws from. The card geometry lives in
// `toast_overlay.rs`; the per-frame drive and click routing in
// `hook/notify_drive.rs`. The Console panel keeps the full history; a toast is
// the additional at-a-glance surface for a result the user should not need the
// console open to see.

use std::sync::atomic::{AtomicBool, AtomicU32, Ordering};
use std::sync::{Arc, Mutex};
use std::time::{Duration, Instant};

// Visible card cap; older live toasts collapse into the "+N more" row.
pub(crate) const MAX_VISIBLE: usize = 4;

// Auto-dismissing toasts fade out over this tail of their lifetime.
const FADE: Duration = Duration::from_millis(300);

#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub(crate) enum Level {
    Info,
    Success,
    Warning,
    Error,
}

impl Level {
    // How long a toast of this level lives; `None` is sticky until clicked.
    fn ttl(self) -> Option<Duration> {
        match self {
            Level::Info | Level::Success => Some(Duration::from_secs(4)),
            Level::Warning => Some(Duration::from_secs(8)),
            Level::Error => None,
        }
    }
}

// What clicking a toast does, beyond dismissing it.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub(crate) enum Action {
    OpenConsole,
    GoToBehaviorFault,
}

#[derive(Debug)]
struct Toast {
    level: Level,
    message: String,
    action: Option<Action>,
    born: Instant,
}

impl Toast {
    // 1.0 for most of the toast's life, easing to 0.0 over the fade tail.
    // Sticky levels never fade.
    fn alpha(&self, now: Instant) -> f32 {
        let Some(ttl) = self.level.ttl() else {
            return 1.0;
        };
        let age = now.saturating_duration_since(self.born);
        if age >= ttl {
            return 0.0;
        }
        let left = ttl - age;
        (left.as_secs_f32() / FADE.as_secs_f32()).min(1.0)
    }

    fn expired(&self, now: Instant) -> bool {
        self.level
            .ttl()
            .is_some_and(|ttl| now.saturating_duration_since(self.born) >= ttl)
    }
}

// A long operation's shared progress state: the worker that runs it updates
// the counters, the drive reads them each frame. `total == 0` marks progress
// as indeterminate (the operation cannot count its work yet).
struct OpShared {
    label: String,
    done: AtomicU32,
    total: AtomicU32,
    finished: AtomicBool,
    born: Instant,
}

// The worker's handle to its operation card. Explicitly finished on
// completion; dropping the last handle (a worker unwinding included) also
// retires the card, so no path can leave one up forever.
#[derive(Clone)]
pub(crate) struct OpHandle(Arc<OpShared>);

impl OpHandle {
    pub(crate) fn set(&self, done: u32, total: u32) {
        self.0.done.store(done, Ordering::Relaxed);
        self.0.total.store(total, Ordering::Relaxed);
    }

    pub(crate) fn finish(&self) {
        self.0.finished.store(true, Ordering::Relaxed);
    }
}

// One operation card, computed for this frame. `fraction` is real measured
// progress; `None` draws the indeterminate sweep (`phase` is the operation's
// age in seconds, which the overlay animates from).
#[derive(Debug)]
pub(crate) struct OpCard {
    pub label: String,
    pub fraction: Option<f32>,
    pub phase: f32,
}

// One visible card, newest first (slot 0 sits nearest the anchor corner). A
// card's action stays in the queue; `click_card` hands it back on dismissal.
#[derive(Debug)]
pub(crate) struct Card {
    pub level: Level,
    pub message: String,
    pub alpha: f32,
}

// The stack the overlay draws this frame.
#[derive(Debug, Default)]
pub(crate) struct Stack {
    // Running operations, nearest the anchor corner (below the toasts).
    pub ops: Vec<OpCard>,
    pub cards: Vec<Card>,
    // Live toasts beyond the visible cap (the "+N more" row; 0 hides it).
    pub overflow: usize,
}

#[derive(Default)]
struct Queue {
    // Push order: oldest first.
    toasts: Vec<Toast>,
    ops: Vec<Arc<OpShared>>,
}

impl Queue {
    fn push_at(&mut self, level: Level, message: String, action: Option<Action>, now: Instant) {
        // A repeat of a live toast refreshes it instead of stacking a twin.
        if let Some(t) = self
            .toasts
            .iter_mut()
            .find(|t| t.level == level && t.message == message)
        {
            t.born = now;
            t.action = action;
            return;
        }
        self.toasts.push(Toast {
            level,
            message,
            action,
            born: now,
        });
    }

    fn prune(&mut self, now: Instant) {
        self.toasts.retain(|t| !t.expired(now));
        // An op retires when finished, or when every worker-side handle is
        // gone (the queue's own Arc is the last one standing).
        self.ops
            .retain(|op| !op.finished.load(Ordering::Relaxed) && Arc::strong_count(op) > 1);
    }

    fn begin_op(&mut self, label: String, now: Instant) -> OpHandle {
        let shared = Arc::new(OpShared {
            label,
            done: AtomicU32::new(0),
            total: AtomicU32::new(0),
            finished: AtomicBool::new(false),
            born: now,
        });
        self.ops.push(shared.clone());
        OpHandle(shared)
    }

    fn stack_at(&mut self, now: Instant) -> Stack {
        self.prune(now);
        let ops = self
            .ops
            .iter()
            .map(|op| {
                let total = op.total.load(Ordering::Relaxed);
                OpCard {
                    label: op.label.clone(),
                    fraction: (total > 0)
                        .then(|| (op.done.load(Ordering::Relaxed) as f32 / total as f32).min(1.0)),
                    phase: now.saturating_duration_since(op.born).as_secs_f32(),
                }
            })
            .collect();
        let visible = self.toasts.len().min(MAX_VISIBLE);
        let cards = self
            .toasts
            .iter()
            .rev()
            .take(visible)
            .map(|t| Card {
                level: t.level,
                message: t.message.clone(),
                alpha: t.alpha(now),
            })
            .collect();
        Stack {
            ops,
            cards,
            overflow: self.toasts.len() - visible,
        }
    }

    // Remove the toast shown in visible slot `slot` (newest-first order) and
    // hand back its action.
    fn click_at(&mut self, slot: usize, now: Instant) -> Option<Action> {
        self.prune(now);
        let visible = self.toasts.len().min(MAX_VISIBLE);
        if slot >= visible {
            return None;
        }
        let index = self.toasts.len() - 1 - slot;
        self.toasts.remove(index).action
    }
}

// The shared handle to the queue: cloned into worker threads (a cook worker
// reports its finish through it) and drained by the hook each frame. A
// poisoned lock drops the push rather than panicking the frame loop.
#[derive(Clone, Default)]
pub(crate) struct Notifier(Arc<Mutex<Queue>>);

impl Notifier {
    pub(crate) fn push(&self, level: Level, message: &str) {
        self.push_with(level, message, None);
    }

    pub(crate) fn push_with(&self, level: Level, message: &str, action: Option<Action>) {
        if let Ok(mut q) = self.0.lock() {
            q.push_at(level, message.to_string(), action, Instant::now());
        }
    }

    pub(crate) fn info(&self, message: &str) {
        self.push(Level::Info, message);
    }
    pub(crate) fn success(&self, message: &str) {
        self.push(Level::Success, message);
    }
    pub(crate) fn warning(&self, message: &str) {
        self.push(Level::Warning, message);
    }
    pub(crate) fn error_with(&self, message: &str, action: Action) {
        self.push_with(Level::Error, message, Some(action));
    }

    // Start an operation card; the returned handle is the worker's reporter.
    pub(crate) fn begin_op(&self, label: &str) -> OpHandle {
        match self.0.lock() {
            Ok(mut q) => q.begin_op(label.to_string(), Instant::now()),
            // A poisoned queue still hands out a working (orphan) handle.
            Err(_) => OpHandle(Arc::new(OpShared {
                label: label.to_string(),
                done: AtomicU32::new(0),
                total: AtomicU32::new(0),
                finished: AtomicBool::new(false),
                born: Instant::now(),
            })),
        }
    }

    // Whether any toast or operation is retained (expired-but-unpruned
    // included, so the drive still runs the frame that prunes them). The idle
    // fast path.
    pub(crate) fn is_empty(&self) -> bool {
        self.0
            .lock()
            .map(|q| q.toasts.is_empty() && q.ops.is_empty())
            .unwrap_or(true)
    }

    // The stack to draw this frame; prunes expired toasts on the way.
    pub(crate) fn stack(&self) -> Stack {
        self.0
            .lock()
            .map(|mut q| q.stack_at(Instant::now()))
            .unwrap_or_default()
    }

    // Dismiss the card in visible slot `slot`, returning its action.
    pub(crate) fn click_card(&self, slot: usize) -> Option<Action> {
        self.0
            .lock()
            .ok()
            .and_then(|mut q| q.click_at(slot, Instant::now()))
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    fn queue_with(entries: &[(Level, &str)], now: Instant) -> Queue {
        let mut q = Queue::default();
        for (level, msg) in entries {
            q.push_at(*level, msg.to_string(), None, now);
        }
        q
    }

    #[test]
    fn info_expires_and_errors_stick() {
        let now = Instant::now();
        let mut q = queue_with(&[(Level::Info, "done"), (Level::Error, "broke")], now);
        let later = now + Duration::from_secs(10);
        let stack = q.stack_at(later);
        assert_eq!(stack.cards.len(), 1);
        assert_eq!(stack.cards[0].level, Level::Error);
        assert_eq!(stack.cards[0].alpha, 1.0, "sticky toasts never fade");
    }

    #[test]
    fn warnings_outlive_info() {
        let now = Instant::now();
        let mut q = queue_with(&[(Level::Info, "a"), (Level::Warning, "b")], now);
        let mid = now + Duration::from_secs(6);
        let stack = q.stack_at(mid);
        assert_eq!(stack.cards.len(), 1);
        assert_eq!(stack.cards[0].level, Level::Warning);
    }

    #[test]
    fn alpha_fades_over_the_tail_of_the_lifetime() {
        let now = Instant::now();
        let t = Toast {
            level: Level::Info,
            message: String::new(),
            action: None,
            born: now,
        };
        assert_eq!(t.alpha(now), 1.0);
        assert_eq!(t.alpha(now + Duration::from_secs(3)), 1.0, "pre-fade");
        let mid_fade = t.alpha(now + Duration::from_millis(3850));
        assert!(
            (0.0..1.0).contains(&mid_fade),
            "inside the fade tail: {mid_fade}"
        );
        assert_eq!(t.alpha(now + Duration::from_secs(4)), 0.0, "expired");
    }

    #[test]
    fn stack_caps_visible_cards_and_counts_overflow_newest_first() {
        let now = Instant::now();
        let mut q = Queue::default();
        for i in 0..6 {
            q.push_at(Level::Error, format!("e{i}"), None, now);
        }
        let stack = q.stack_at(now);
        assert_eq!(stack.cards.len(), MAX_VISIBLE);
        assert_eq!(stack.overflow, 2);
        assert_eq!(stack.cards[0].message, "e5", "slot 0 is the newest");
        assert_eq!(stack.cards[MAX_VISIBLE - 1].message, "e2");
    }

    #[test]
    fn a_repeat_refreshes_instead_of_stacking() {
        let now = Instant::now();
        let mut q = queue_with(&[(Level::Info, "saved")], now);
        let later = now + Duration::from_secs(3);
        q.push_at(Level::Info, "saved".to_string(), None, later);
        assert_eq!(q.toasts.len(), 1, "coalesced");
        // The refreshed toast lives a full lifetime from the repeat.
        let stack = q.stack_at(later + Duration::from_secs(3));
        assert_eq!(stack.cards.len(), 1);
        // The same text at a different level is a different toast.
        q.push_at(Level::Error, "saved".to_string(), None, later);
        assert_eq!(q.toasts.len(), 2);
    }

    #[test]
    fn click_dismisses_the_slot_and_returns_its_action() {
        let now = Instant::now();
        let mut q = Queue::default();
        q.push_at(Level::Error, "older".to_string(), None, now);
        q.push_at(
            Level::Error,
            "newest".to_string(),
            Some(Action::OpenConsole),
            now,
        );
        assert_eq!(q.click_at(0, now), Some(Action::OpenConsole));
        assert_eq!(q.toasts.len(), 1);
        assert_eq!(q.toasts[0].message, "older");
        assert_eq!(q.click_at(5, now), None, "a slot past the stack is a miss");
        assert_eq!(q.toasts.len(), 1);
    }

    #[test]
    fn ops_report_progress_and_retire_on_finish_or_drop() {
        let now = Instant::now();
        let mut q = Queue::default();
        let op = q.begin_op("Cooking".to_string(), now);
        let stack = q.stack_at(now);
        assert_eq!(stack.ops.len(), 1);
        assert_eq!(stack.ops[0].fraction, None, "no counts yet: indeterminate");
        op.set(3, 12);
        let stack = q.stack_at(now);
        assert_eq!(stack.ops[0].fraction, Some(0.25));
        op.finish();
        assert!(q.stack_at(now).ops.is_empty(), "finished ops retire");
        // Dropping the last worker handle retires the card too, so a worker
        // that unwinds can never leave one up forever.
        let op2 = q.begin_op("Saving".to_string(), now);
        assert_eq!(q.stack_at(now).ops.len(), 1);
        drop(op2);
        assert!(q.stack_at(now).ops.is_empty());
    }

    #[test]
    fn notifier_is_shared_across_clones() {
        let n = Notifier::default();
        let clone = n.clone();
        assert!(n.is_empty());
        clone.success("from a worker");
        assert!(!n.is_empty());
        let stack = n.stack();
        assert_eq!(stack.cards.len(), 1);
        assert_eq!(stack.cards[0].message, "from a worker");
        assert_eq!(
            n.click_card(0),
            None,
            "an actionless card dismisses plainly"
        );
        assert!(n.is_empty());
    }
}