guise-ui 1.5.0

A component library for gpui, Zed's GPU-accelerated UI framework: a themed palette, sizing tokens, 130+ composable components, a reactive state layer, and an in-app Safari-style inspector.
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
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
//! The record store behind every devtools panel.
//!
//! Safari's Web Inspector reads a live page; there is no equivalent firehose in
//! a native app, so `guise` inverts it: the host *reports* what it does — a log
//! line, a request, a storage domain — and this global keeps the rolling
//! history the panels render. Nothing here opens a socket or reads a file, the
//! same way [`crate::ai`] never issues the request it displays.
//!
//! Every store is a ring: capped, oldest-first eviction, so a long-running app
//! cannot grow the inspector without bound. `generation` ticks on every
//! mutation, which is what lets a panel skip work when nothing changed.

use std::collections::VecDeque;
use std::time::{Duration, Instant};

use gpui::{App, Global, SharedString};

/// Where a record came from in the source. Built from a gpui element's
/// `#[track_caller]` location, or supplied by the host for its own records.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct SourceRef {
    pub file: SharedString,
    pub line: u32,
    pub column: u32,
}

impl SourceRef {
    pub fn new(file: impl Into<SharedString>, line: u32, column: u32) -> Self {
        SourceRef {
            file: file.into(),
            line,
            column,
        }
    }

    /// Just the file name, which is all the one-line displays have room for.
    pub fn basename(&self) -> &str {
        let file = self.file.as_ref();
        match file.rsplit_once('/') {
            Some((_, name)) => name,
            None => file,
        }
    }

    /// `foo.rs:12:5`, the form both Safari and rustc print.
    pub fn short(&self) -> String {
        format!("{}:{}:{}", self.basename(), self.line, self.column)
    }
}

impl From<&'static std::panic::Location<'static>> for SourceRef {
    fn from(loc: &'static std::panic::Location<'static>) -> Self {
        SourceRef::new(loc.file(), loc.line(), loc.column())
    }
}

/// Log severity. Mirrors the levels Safari's console filters by.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Default)]
pub enum LogLevel {
    /// `console.log` — no icon, no color.
    #[default]
    Log,
    /// `console.debug` — dimmed.
    Debug,
    /// `console.info` — blue dot.
    Info,
    /// `console.warn` — amber, counted in the toolbar.
    Warning,
    /// `console.error` — red, counted in the toolbar.
    Error,
}

impl LogLevel {
    pub fn label(self) -> &'static str {
        match self {
            LogLevel::Log => "Log",
            LogLevel::Debug => "Debug",
            LogLevel::Info => "Info",
            LogLevel::Warning => "Warning",
            LogLevel::Error => "Error",
        }
    }

    /// Whether the toolbar's warning/error badges count this level.
    pub fn is_issue(self) -> bool {
        matches!(self, LogLevel::Warning | LogLevel::Error)
    }
}

/// One log line. `count` is the repeat tally: Safari collapses identical
/// consecutive messages into a single row with a counter rather than scrolling
/// the useful history away, and so do we.
#[derive(Debug, Clone)]
pub struct LogRecord {
    pub id: u64,
    pub level: LogLevel,
    pub message: SharedString,
    /// Expandable key/value rows shown when the row is disclosed — the native
    /// stand-in for expanding a logged object.
    pub details: Vec<(SharedString, SharedString)>,
    pub source: Option<SourceRef>,
    pub at: Duration,
    pub count: usize,
}

impl LogRecord {
    pub fn new(level: LogLevel, message: impl Into<SharedString>) -> Self {
        LogRecord {
            id: 0,
            level,
            message: message.into(),
            details: Vec::new(),
            source: None,
            at: Duration::ZERO,
            count: 1,
        }
    }

    pub fn detail(mut self, key: impl Into<SharedString>, value: impl Into<SharedString>) -> Self {
        self.details.push((key.into(), value.into()));
        self
    }

    pub fn details(mut self, rows: impl IntoIterator<Item = (SharedString, SharedString)>) -> Self {
        self.details.extend(rows);
        self
    }

    pub fn source(mut self, source: SourceRef) -> Self {
        self.source = Some(source);
        self
    }

    /// Two records coalesce when they would render identically.
    fn same_as(&self, other: &LogRecord) -> bool {
        self.level == other.level && self.message == other.message && self.details == other.details
    }
}

/// What kind of resource a request fetched. Drives the Network panel's type
/// filter and the waterfall color, exactly as in Safari.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Default)]
pub enum ResourceKind {
    Document,
    Stylesheet,
    Image,
    Font,
    Script,
    /// `XHR` and `fetch` in Safari; any app-level API call here.
    Fetch,
    WebSocket,
    Media,
    #[default]
    Other,
}

impl ResourceKind {
    pub fn label(self) -> &'static str {
        match self {
            ResourceKind::Document => "Document",
            ResourceKind::Stylesheet => "Stylesheet",
            ResourceKind::Image => "Image",
            ResourceKind::Font => "Font",
            ResourceKind::Script => "Script",
            ResourceKind::Fetch => "Fetch",
            ResourceKind::WebSocket => "Socket",
            ResourceKind::Media => "Media",
            ResourceKind::Other => "Other",
        }
    }

    /// The set the Network panel's type filter offers, in Safari's order.
    pub const ALL: [ResourceKind; 9] = [
        ResourceKind::Document,
        ResourceKind::Stylesheet,
        ResourceKind::Image,
        ResourceKind::Font,
        ResourceKind::Script,
        ResourceKind::Fetch,
        ResourceKind::WebSocket,
        ResourceKind::Media,
        ResourceKind::Other,
    ];
}

/// The phase breakdown behind the waterfall bar. Each field is the time spent
/// in that phase, not a timestamp, so a partially-complete request is just one
/// with later phases still zero.
#[derive(Debug, Clone, Copy, Default, PartialEq)]
pub struct Timings {
    pub stalled: Duration,
    pub dns: Duration,
    pub connect: Duration,
    pub tls: Duration,
    pub request: Duration,
    pub response: Duration,
}

impl Timings {
    pub fn total(&self) -> Duration {
        self.stalled + self.dns + self.connect + self.tls + self.request + self.response
    }

    /// The phases in waterfall order, skipping the ones that took no time.
    pub fn phases(&self) -> Vec<(&'static str, Duration)> {
        [
            ("Stalled", self.stalled),
            ("DNS", self.dns),
            ("Connect", self.connect),
            ("Secure", self.tls),
            ("Request", self.request),
            ("Response", self.response),
        ]
        .into_iter()
        .filter(|(_, d)| !d.is_zero())
        .collect()
    }
}

/// How far along a request is. A record starts `Pending` and the host settles
/// it later by id — the inspector shows the row the whole time, as Safari does.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
pub enum RequestState {
    #[default]
    Pending,
    Finished,
    Failed,
    Canceled,
}

/// One network request.
#[derive(Debug, Clone)]
pub struct NetworkRecord {
    pub id: u64,
    pub method: SharedString,
    pub url: SharedString,
    pub kind: ResourceKind,
    pub state: RequestState,
    pub status: Option<u16>,
    pub status_text: SharedString,
    pub protocol: SharedString,
    pub remote_address: SharedString,
    pub priority: SharedString,
    /// Bytes on the wire, after compression.
    pub transfer_size: u64,
    /// Bytes after decoding — what the app actually received.
    pub resource_size: u64,
    pub cached: bool,
    pub request_headers: Vec<(SharedString, SharedString)>,
    pub response_headers: Vec<(SharedString, SharedString)>,
    pub request_body: Option<SharedString>,
    pub response_body: Option<SharedString>,
    pub initiator: Option<SourceRef>,
    pub error: Option<SharedString>,
    /// When the request started, relative to the store's epoch. The waterfall
    /// lays rows out against this.
    pub start: Duration,
    pub timings: Timings,
}

impl NetworkRecord {
    pub fn new(method: impl Into<SharedString>, url: impl Into<SharedString>) -> Self {
        NetworkRecord {
            id: 0,
            method: method.into(),
            url: url.into(),
            kind: ResourceKind::default(),
            state: RequestState::Pending,
            status: None,
            status_text: SharedString::default(),
            protocol: SharedString::default(),
            remote_address: SharedString::default(),
            priority: SharedString::default(),
            transfer_size: 0,
            resource_size: 0,
            cached: false,
            request_headers: Vec::new(),
            response_headers: Vec::new(),
            request_body: None,
            response_body: None,
            initiator: None,
            error: None,
            start: Duration::ZERO,
            timings: Timings::default(),
        }
    }

    pub fn kind(mut self, kind: ResourceKind) -> Self {
        self.kind = kind;
        self
    }

    pub fn status(mut self, status: u16, text: impl Into<SharedString>) -> Self {
        self.status = Some(status);
        self.status_text = text.into();
        self
    }

    pub fn sizes(mut self, transfer: u64, resource: u64) -> Self {
        self.transfer_size = transfer;
        self.resource_size = resource;
        self
    }

    pub fn timings(mut self, timings: Timings) -> Self {
        self.timings = timings;
        self
    }

    pub fn request_header(
        mut self,
        name: impl Into<SharedString>,
        value: impl Into<SharedString>,
    ) -> Self {
        self.request_headers.push((name.into(), value.into()));
        self
    }

    pub fn response_header(
        mut self,
        name: impl Into<SharedString>,
        value: impl Into<SharedString>,
    ) -> Self {
        self.response_headers.push((name.into(), value.into()));
        self
    }

    pub fn request_body(mut self, body: impl Into<SharedString>) -> Self {
        self.request_body = Some(body.into());
        self
    }

    pub fn response_body(mut self, body: impl Into<SharedString>) -> Self {
        self.response_body = Some(body.into());
        self
    }

    pub fn initiator(mut self, source: SourceRef) -> Self {
        self.initiator = Some(source);
        self
    }

    pub fn finished(mut self) -> Self {
        self.state = RequestState::Finished;
        self
    }

    pub fn failed(mut self, error: impl Into<SharedString>) -> Self {
        self.state = RequestState::Failed;
        self.error = Some(error.into());
        self
    }

    /// The Name column: the last path segment, or the host for a bare origin.
    pub fn name(&self) -> &str {
        let url = self.url.as_ref();
        let path = url
            .split_once("://")
            .map(|(_, rest)| rest)
            .unwrap_or(url)
            .split(['?', '#'])
            .next()
            .unwrap_or("");
        match path.rsplit_once('/') {
            Some((_, last)) if !last.is_empty() => last,
            _ => path.split('/').next().unwrap_or(url),
        }
    }

    /// The Domain column.
    pub fn domain(&self) -> &str {
        let url = self.url.as_ref();
        let rest = url.split_once("://").map(|(_, r)| r).unwrap_or(url);
        rest.split(['/', '?', '#']).next().unwrap_or(rest)
    }

    /// The Scheme column.
    pub fn scheme(&self) -> &str {
        self.url
            .as_ref()
            .split_once("://")
            .map(|(s, _)| s)
            .unwrap_or("")
    }

    /// Whether the row renders in the error color: a transport failure, or any
    /// 4xx/5xx response.
    pub fn is_error(&self) -> bool {
        self.state == RequestState::Failed || self.status.is_some_and(|s| s >= 400)
    }

    pub fn duration(&self) -> Duration {
        self.timings.total()
    }
}

/// What a storage domain holds. Only affects the icon and the sidebar grouping.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
pub enum StorageKind {
    /// Persisted settings — the native analogue of Local Storage.
    #[default]
    Local,
    /// In-memory state that dies with the process.
    Session,
    Cookies,
    /// A structured store: a database, an index.
    Database,
    Cache,
}

impl StorageKind {
    pub fn label(self) -> &'static str {
        match self {
            StorageKind::Local => "Local Storage",
            StorageKind::Session => "Session Storage",
            StorageKind::Cookies => "Cookies",
            StorageKind::Database => "Databases",
            StorageKind::Cache => "Caches",
        }
    }
}

/// One row in a storage domain.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct StorageEntry {
    pub key: SharedString,
    pub value: SharedString,
    /// Extra columns — cookie domain/path/expiry, a record's type, and so on.
    pub extra: Vec<(SharedString, SharedString)>,
}

impl StorageEntry {
    pub fn new(key: impl Into<SharedString>, value: impl Into<SharedString>) -> Self {
        StorageEntry {
            key: key.into(),
            value: value.into(),
            extra: Vec::new(),
        }
    }

    pub fn extra(mut self, name: impl Into<SharedString>, value: impl Into<SharedString>) -> Self {
        self.extra.push((name.into(), value.into()));
        self
    }
}

/// A named collection of storage rows, listed in the Storage panel's sidebar.
#[derive(Debug, Clone)]
pub struct StorageDomain {
    pub id: SharedString,
    pub name: SharedString,
    pub kind: StorageKind,
    pub entries: Vec<StorageEntry>,
    /// Extra column headers beyond Key and Value.
    pub columns: Vec<SharedString>,
}

impl StorageDomain {
    pub fn new(id: impl Into<SharedString>, name: impl Into<SharedString>) -> Self {
        StorageDomain {
            id: id.into(),
            name: name.into(),
            kind: StorageKind::default(),
            entries: Vec::new(),
            columns: Vec::new(),
        }
    }

    pub fn kind(mut self, kind: StorageKind) -> Self {
        self.kind = kind;
        self
    }

    pub fn columns(mut self, columns: impl IntoIterator<Item = SharedString>) -> Self {
        self.columns = columns.into_iter().collect();
        self
    }

    pub fn entry(mut self, entry: StorageEntry) -> Self {
        self.entries.push(entry);
        self
    }

    pub fn entries(mut self, entries: impl IntoIterator<Item = StorageEntry>) -> Self {
        self.entries.extend(entries);
        self
    }

    /// Total bytes, as the Storage panel's footer reports.
    pub fn size(&self) -> u64 {
        self.entries
            .iter()
            .map(|e| (e.key.len() + e.value.len()) as u64)
            .sum()
    }
}

/// A band on the Timelines panel.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Default)]
pub enum TimelineKind {
    /// A rendered frame — the band that reveals dropped frames.
    #[default]
    Frame,
    Layout,
    Paint,
    /// App work: a handler, a task, a computation.
    Script,
    Network,
}

impl TimelineKind {
    pub fn label(self) -> &'static str {
        match self {
            TimelineKind::Frame => "Frames",
            TimelineKind::Layout => "Layout",
            TimelineKind::Paint => "Rendering",
            TimelineKind::Script => "JavaScript & Events",
            TimelineKind::Network => "Network Requests",
        }
    }

    pub const ALL: [TimelineKind; 5] = [
        TimelineKind::Frame,
        TimelineKind::Layout,
        TimelineKind::Paint,
        TimelineKind::Script,
        TimelineKind::Network,
    ];
}

/// One span on a timeline band.
#[derive(Debug, Clone)]
pub struct TimelineEvent {
    pub id: u64,
    pub kind: TimelineKind,
    pub label: SharedString,
    pub start: Duration,
    pub duration: Duration,
    pub source: Option<SourceRef>,
}

impl TimelineEvent {
    pub fn new(kind: TimelineKind, label: impl Into<SharedString>, duration: Duration) -> Self {
        TimelineEvent {
            id: 0,
            kind,
            label: label.into(),
            start: Duration::ZERO,
            duration,
            source: None,
        }
    }

    pub fn end(&self) -> Duration {
        self.start + self.duration
    }
}

/// How many records each store keeps before evicting the oldest.
#[derive(Debug, Clone, Copy)]
pub struct Limits {
    pub logs: usize,
    pub network: usize,
    pub timeline: usize,
}

impl Default for Limits {
    fn default() -> Self {
        Limits {
            logs: 1000,
            network: 1000,
            timeline: 4000,
        }
    }
}

/// Everything the inspector displays, and the only mutable state it owns.
///
/// Installed once with [`DevToolsState::init`]; feed it through the free
/// functions in [`crate::devtools`] (`console_log`, `network_begin`, …), which
/// are no-ops when it was never installed. That is deliberate: instrumentation
/// left in a release build costs a global lookup and nothing more.
pub struct DevToolsState {
    epoch: Instant,
    next_id: u64,
    generation: u64,
    limits: Limits,
    logs: VecDeque<LogRecord>,
    network: VecDeque<NetworkRecord>,
    timeline: VecDeque<TimelineEvent>,
    storage: Vec<StorageDomain>,
    /// Wall-clock frame durations, newest last — the Timelines FPS graph.
    frames: VecDeque<Duration>,
    last_frame: Option<Instant>,
}

impl Global for DevToolsState {}

impl Default for DevToolsState {
    fn default() -> Self {
        DevToolsState::new()
    }
}

impl DevToolsState {
    pub fn new() -> Self {
        DevToolsState {
            epoch: Instant::now(),
            next_id: 1,
            generation: 0,
            limits: Limits::default(),
            logs: VecDeque::new(),
            network: VecDeque::new(),
            timeline: VecDeque::new(),
            storage: Vec::new(),
            frames: VecDeque::new(),
            last_frame: None,
        }
    }

    pub fn limits(mut self, limits: Limits) -> Self {
        self.limits = limits;
        self
    }

    /// Install as the app global. Call once at startup, before opening
    /// [`crate::devtools::DevTools`].
    pub fn init(self, cx: &mut App) {
        cx.set_global(self);
    }

    /// Bumped on every mutation, so a panel can tell "nothing changed" cheaply.
    pub fn generation(&self) -> u64 {
        self.generation
    }

    /// Time since the store was created — the clock every record is stamped on.
    pub fn now(&self) -> Duration {
        self.epoch.elapsed()
    }

    fn tick(&mut self) -> u64 {
        self.generation += 1;
        let id = self.next_id;
        self.next_id += 1;
        id
    }

    // --- logs ---------------------------------------------------------------

    /// Append a line, coalescing it into the previous one when identical.
    pub fn push_log(&mut self, mut record: LogRecord) {
        if let Some(last) = self.logs.back_mut() {
            if last.same_as(&record) {
                last.count += 1;
                self.generation += 1;
                return;
            }
        }
        record.id = self.tick();
        record.at = self.now();
        self.logs.push_back(record);
        while self.logs.len() > self.limits.logs {
            self.logs.pop_front();
        }
    }

    pub fn logs(&self) -> &VecDeque<LogRecord> {
        &self.logs
    }

    pub fn clear_logs(&mut self) {
        self.logs.clear();
        self.generation += 1;
    }

    /// Warning and error tallies for the toolbar badges.
    pub fn log_issues(&self) -> (usize, usize) {
        let mut warnings = 0;
        let mut errors = 0;
        for record in &self.logs {
            match record.level {
                LogLevel::Warning => warnings += record.count,
                LogLevel::Error => errors += record.count,
                _ => {}
            }
        }
        (warnings, errors)
    }

    // --- network -----------------------------------------------------------

    /// Record a request that has started. Returns its id, which the host keeps
    /// to settle the request later.
    pub fn push_network(&mut self, mut record: NetworkRecord) -> u64 {
        let id = self.tick();
        record.id = id;
        if record.start.is_zero() {
            record.start = self.now();
        }
        self.network.push_back(record);
        while self.network.len() > self.limits.network {
            self.network.pop_front();
        }
        id
    }

    /// Amend a request in flight — the response landed, the transfer grew, it
    /// failed. Silently does nothing if the record was already evicted.
    pub fn update_network(&mut self, id: u64, f: impl FnOnce(&mut NetworkRecord)) {
        if let Some(record) = self.network.iter_mut().find(|r| r.id == id) {
            f(record);
            self.generation += 1;
        }
    }

    pub fn network(&self) -> &VecDeque<NetworkRecord> {
        &self.network
    }

    pub fn clear_network(&mut self) {
        self.network.clear();
        self.generation += 1;
    }

    /// The window the waterfall is drawn against: earliest start to latest end.
    pub fn network_span(&self) -> (Duration, Duration) {
        let start = self
            .network
            .iter()
            .map(|r| r.start)
            .min()
            .unwrap_or(Duration::ZERO);
        let end = self
            .network
            .iter()
            .map(|r| r.start + r.duration())
            .max()
            .unwrap_or(Duration::ZERO);
        (start, end.max(start))
    }

    /// Row count, total transfer, and total resource bytes — the status bar.
    pub fn network_totals(&self) -> (usize, u64, u64) {
        let transfer = self.network.iter().map(|r| r.transfer_size).sum();
        let resource = self.network.iter().map(|r| r.resource_size).sum();
        (self.network.len(), transfer, resource)
    }

    // --- storage -----------------------------------------------------------

    /// Register a domain, replacing any existing one with the same id. Hosts
    /// call this whenever their store changes; the panel always shows the
    /// latest snapshot.
    pub fn set_storage(&mut self, domain: StorageDomain) {
        self.generation += 1;
        match self.storage.iter_mut().find(|d| d.id == domain.id) {
            Some(existing) => *existing = domain,
            None => self.storage.push(domain),
        }
    }

    pub fn remove_storage(&mut self, id: &str) {
        self.storage.retain(|d| d.id.as_ref() != id);
        self.generation += 1;
    }

    pub fn storage(&self) -> &[StorageDomain] {
        &self.storage
    }

    // --- timelines ---------------------------------------------------------

    pub fn push_timeline(&mut self, mut event: TimelineEvent) {
        event.id = self.tick();
        if event.start.is_zero() {
            event.start = self.now().saturating_sub(event.duration);
        }
        self.timeline.push_back(event);
        while self.timeline.len() > self.limits.timeline {
            self.timeline.pop_front();
        }
    }

    pub fn timeline(&self) -> &VecDeque<TimelineEvent> {
        &self.timeline
    }

    pub fn clear_timeline(&mut self) {
        self.timeline.clear();
        self.frames.clear();
        self.last_frame = None;
        self.generation += 1;
    }

    /// Called once per rendered frame by the inspector itself. Deriving the
    /// interval here — rather than asking the host to report it — is what makes
    /// the Frames band work with no wiring at all.
    pub fn record_frame(&mut self) {
        let now = Instant::now();
        if let Some(previous) = self.last_frame.replace(now) {
            let delta = now.duration_since(previous);
            // A frame gap longer than a second means the window was idle, not
            // slow; counting it would flatten the graph for minutes.
            if delta < Duration::from_secs(1) {
                self.frames.push_back(delta);
                while self.frames.len() > 240 {
                    self.frames.pop_front();
                }
            }
        }
    }

    /// Forget where the last frame landed. Called when recording stops so the
    /// idle gap before it resumes is not measured as one enormous frame.
    pub fn stop_frames(&mut self) {
        self.last_frame = None;
    }

    pub fn frames(&self) -> &VecDeque<Duration> {
        &self.frames
    }

    /// Frames per second over the recorded window, or `None` before the second
    /// frame has been seen.
    pub fn fps(&self) -> Option<f32> {
        if self.frames.is_empty() {
            return None;
        }
        let total: Duration = self.frames.iter().sum();
        if total.is_zero() {
            return None;
        }
        Some(self.frames.len() as f32 / total.as_secs_f32())
    }

    /// Drop every record. The toolbar's clear button, and what a host calls
    /// when it wants a clean slate around a reproduction.
    pub fn clear_all(&mut self) {
        self.logs.clear();
        self.network.clear();
        self.timeline.clear();
        self.frames.clear();
        self.last_frame = None;
        self.generation += 1;
    }
}

/// Format a byte count the way Safari's size columns do.
pub fn format_bytes(bytes: u64) -> String {
    const KB: f64 = 1024.0;
    let bytes = bytes as f64;
    if bytes < KB {
        format!("{} B", bytes as u64)
    } else if bytes < KB * KB {
        format!("{:.1} KB", bytes / KB)
    } else if bytes < KB * KB * KB {
        format!("{:.2} MB", bytes / (KB * KB))
    } else {
        format!("{:.2} GB", bytes / (KB * KB * KB))
    }
}

/// Format a duration the way Safari's timing columns do: sub-millisecond work
/// still reads as a number, and anything past a second switches unit.
pub fn format_duration(duration: Duration) -> String {
    let ms = duration.as_secs_f64() * 1000.0;
    if ms < 1.0 {
        format!("{:.2} ms", ms)
    } else if ms < 1000.0 {
        format!("{:.0} ms", ms)
    } else {
        format!("{:.2} s", ms / 1000.0)
    }
}

/// Elapsed time as the log's timestamp column shows it.
pub fn format_timestamp(at: Duration) -> String {
    let total = at.as_secs();
    let minutes = total / 60;
    let seconds = total % 60;
    format!("{:02}:{:02}.{:03}", minutes, seconds, at.subsec_millis())
}

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

    #[test]
    fn identical_log_lines_coalesce() {
        let mut state = DevToolsState::new();
        state.push_log(LogRecord::new(LogLevel::Warning, "slow frame"));
        state.push_log(LogRecord::new(LogLevel::Warning, "slow frame"));
        state.push_log(LogRecord::new(LogLevel::Warning, "slow frame"));

        assert_eq!(state.logs().len(), 1);
        assert_eq!(state.logs()[0].count, 3);
        assert_eq!(state.log_issues(), (3, 0));
    }

    #[test]
    fn a_different_line_breaks_the_run() {
        let mut state = DevToolsState::new();
        state.push_log(LogRecord::new(LogLevel::Log, "a"));
        state.push_log(LogRecord::new(LogLevel::Log, "b"));
        state.push_log(LogRecord::new(LogLevel::Log, "a"));

        assert_eq!(state.logs().len(), 3);
        assert!(state.logs().iter().all(|r| r.count == 1));
    }

    #[test]
    fn logs_evict_oldest_past_the_limit() {
        let mut state = DevToolsState::new().limits(Limits {
            logs: 3,
            ..Limits::default()
        });
        for i in 0..6 {
            state.push_log(LogRecord::new(LogLevel::Log, format!("line {i}")));
        }

        assert_eq!(state.logs().len(), 3);
        assert_eq!(state.logs()[0].message.as_ref(), "line 3");
        assert_eq!(state.logs()[2].message.as_ref(), "line 5");
    }

    #[test]
    fn a_request_settles_by_id() {
        let mut state = DevToolsState::new();
        let id = state.push_network(NetworkRecord::new(
            "GET",
            "https://api.example.com/v1/users",
        ));
        assert_eq!(state.network()[0].state, RequestState::Pending);

        state.update_network(id, |record| {
            record.state = RequestState::Finished;
            record.status = Some(200);
        });

        assert_eq!(state.network()[0].state, RequestState::Finished);
        assert_eq!(state.network()[0].status, Some(200));
    }

    #[test]
    fn settling_an_evicted_request_is_a_no_op() {
        let mut state = DevToolsState::new().limits(Limits {
            network: 1,
            ..Limits::default()
        });
        let first = state.push_network(NetworkRecord::new("GET", "https://example.com/a"));
        state.push_network(NetworkRecord::new("GET", "https://example.com/b"));

        state.update_network(first, |record| record.status = Some(500));

        assert_eq!(state.network().len(), 1);
        assert_eq!(state.network()[0].status, None);
    }

    #[test]
    fn url_splits_into_name_domain_and_scheme() {
        let record = NetworkRecord::new("GET", "https://api.example.com/v1/users?page=2");
        assert_eq!(record.name(), "users");
        assert_eq!(record.domain(), "api.example.com");
        assert_eq!(record.scheme(), "https");

        let root = NetworkRecord::new("GET", "https://example.com/");
        assert_eq!(root.domain(), "example.com");

        let bare = NetworkRecord::new("GET", "https://example.com");
        assert_eq!(bare.name(), "example.com");
    }

    #[test]
    fn errors_are_status_or_transport() {
        assert!(NetworkRecord::new("GET", "/a")
            .status(404, "Not Found")
            .is_error());
        assert!(NetworkRecord::new("GET", "/a")
            .status(500, "Server Error")
            .is_error());
        assert!(!NetworkRecord::new("GET", "/a")
            .status(304, "Not Modified")
            .is_error());
        assert!(NetworkRecord::new("GET", "/a").failed("offline").is_error());
    }

    #[test]
    fn timings_sum_and_drop_empty_phases() {
        let timings = Timings {
            dns: Duration::from_millis(4),
            connect: Duration::from_millis(11),
            response: Duration::from_millis(35),
            ..Timings::default()
        };

        assert_eq!(timings.total(), Duration::from_millis(50));
        assert_eq!(
            timings.phases().iter().map(|(n, _)| *n).collect::<Vec<_>>(),
            vec!["DNS", "Connect", "Response"]
        );
    }

    #[test]
    fn registering_a_storage_domain_twice_replaces_it() {
        let mut state = DevToolsState::new();
        state.set_storage(
            StorageDomain::new("prefs", "Preferences").entry(StorageEntry::new("a", "1")),
        );
        state.set_storage(
            StorageDomain::new("prefs", "Preferences")
                .entry(StorageEntry::new("a", "2"))
                .entry(StorageEntry::new("b", "3")),
        );

        assert_eq!(state.storage().len(), 1);
        assert_eq!(state.storage()[0].entries.len(), 2);
        assert_eq!(state.storage()[0].entries[0].value.as_ref(), "2");
    }

    #[test]
    fn network_span_covers_every_row() {
        let mut state = DevToolsState::new();
        let mut first = NetworkRecord::new("GET", "/a");
        first.start = Duration::from_millis(100);
        first.timings.response = Duration::from_millis(50);
        let mut second = NetworkRecord::new("GET", "/b");
        second.start = Duration::from_millis(20);
        second.timings.response = Duration::from_millis(10);
        state.push_network(first);
        state.push_network(second);

        let (start, end) = state.network_span();
        assert_eq!(start, Duration::from_millis(20));
        assert_eq!(end, Duration::from_millis(150));
    }

    #[test]
    fn byte_and_duration_formats_match_the_columns() {
        assert_eq!(format_bytes(512), "512 B");
        assert_eq!(format_bytes(2048), "2.0 KB");
        assert_eq!(format_bytes(5 * 1024 * 1024), "5.00 MB");

        assert_eq!(format_duration(Duration::from_micros(250)), "0.25 ms");
        assert_eq!(format_duration(Duration::from_millis(42)), "42 ms");
        assert_eq!(format_duration(Duration::from_millis(1500)), "1.50 s");

        assert_eq!(format_timestamp(Duration::from_millis(63_042)), "01:03.042");
    }

    #[test]
    fn source_refs_shorten_to_basename() {
        let source = SourceRef::new("crates/guise/src/button.rs", 42, 9);
        assert_eq!(source.basename(), "button.rs");
        assert_eq!(source.short(), "button.rs:42:9");
    }
}