guise-ui 1.0.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
//! The Timelines panel: the frame graph, the bands, and the event list.
//!
//! Safari records instruments — Network, Layout & Rendering, JavaScript &
//! Events — and lays them out against one ruler so you can see what overlapped
//! what. The Frames band here is measured by the inspector itself, from the
//! interval between its own renders; the others are whatever the host reported
//! through [`super::timeline_event`] and [`super::measure`].

use std::time::Duration;

use gpui::prelude::*;
use gpui::{div, px, AnyElement, Context, Hsla, SharedString, Window};

use super::shell::{
    cell, empty_state, filter_pill, header_cell, tool_button, Ink, LABEL_SIZE, MONO_SIZE,
    NAV_WIDTH, ROW_HEIGHT,
};
use super::state::{format_duration, DevToolsState, TimelineEvent, TimelineKind};
use super::DevTools;
use crate::icon::IconName;
use crate::style::MONO_FAMILY;

/// The frame budget for 60 Hz. Anything past this dropped a frame, and the
/// graph colors it accordingly.
const FRAME_BUDGET: Duration = Duration::from_micros(16_667);

#[derive(Default)]
pub struct TimelinesPanel {
    /// `None` shows every band, as Safari's "All" instrument does.
    band: Option<TimelineKind>,
    /// Whether the frame recorder is armed.
    ///
    /// Off by default, and that is not laziness. gpui paints on demand, so the
    /// interval between two frames of an idle window is however long it sat
    /// idle — reported as a frame rate it reads as a stall that never
    /// happened. Safari makes you press Record for the same reason: the
    /// measurement only means something while something is being measured.
    pub(crate) recording: bool,
}

impl TimelinesPanel {
    /// The window every band is laid out against: the earliest start to the
    /// latest end across the events being shown.
    pub fn span(events: &[TimelineEvent]) -> (Duration, Duration) {
        let start = events
            .iter()
            .map(|event| event.start)
            .min()
            .unwrap_or(Duration::ZERO);
        let end = events
            .iter()
            .map(|event| event.end())
            .max()
            .unwrap_or(Duration::ZERO);
        (start, end.max(start))
    }

    fn band_color(kind: TimelineKind, ink: &Ink) -> Hsla {
        match kind {
            TimelineKind::Frame => ink.accent,
            TimelineKind::Layout => ink.info,
            TimelineKind::Paint => ink.tag,
            TimelineKind::Script => ink.warning,
            TimelineKind::Network => ink.success,
        }
    }

    pub fn render(&self, window: &mut Window, cx: &mut Context<DevTools>) -> AnyElement {
        let ink = Ink::read(cx);
        let (events, frames, fps) = cx
            .try_global::<DevToolsState>()
            .map(|state| {
                let events: Vec<TimelineEvent> = state
                    .timeline()
                    .iter()
                    .filter(|event| self.band.is_none_or(|band| event.kind == band))
                    .cloned()
                    .collect();
                let frames: Vec<Duration> = state.frames().iter().copied().collect();
                (events, frames, state.fps())
            })
            .unwrap_or_default();

        let mut bar = div()
            .flex()
            .flex_none()
            .items_center()
            .gap(px(4.0))
            .h(px(26.0))
            .px(px(8.0))
            .w_full()
            .bg(ink.chrome)
            .border_b_1()
            .border_color(ink.border)
            .child(
                tool_button(
                    "devtools-timeline-record",
                    if self.recording {
                        IconName::Square
                    } else {
                        IconName::Circle
                    },
                    if self.recording {
                        "Stop recording"
                    } else {
                        "Start recording"
                    },
                    self.recording,
                    &ink,
                    cx,
                )
                .on_click(cx.listener(
                    |this: &mut DevTools, _event, _window, cx| {
                        this.timelines.recording = !this.timelines.recording;
                        if !this.timelines.recording && cx.has_global::<DevToolsState>() {
                            cx.update_global::<DevToolsState, _>(|state, _cx| state.stop_frames());
                        }
                        cx.notify();
                    },
                )),
            )
            .child(
                filter_pill("devtools-timeline-all", "All", self.band.is_none(), &ink).on_click(
                    cx.listener(|this: &mut DevTools, _event, _window, cx| {
                        this.timelines.band = None;
                        cx.notify();
                    }),
                ),
            );
        for kind in TimelineKind::ALL {
            bar = bar.child(
                filter_pill(
                    ("devtools-timeline-band", kind as usize),
                    kind.label(),
                    self.band == Some(kind),
                    &ink,
                )
                .on_click(cx.listener(
                    move |this: &mut DevTools, _event, _window, cx| {
                        this.timelines.band = Some(kind);
                        cx.notify();
                    },
                )),
            );
        }
        bar = bar.child(div().flex_1()).when_some(fps, |el, fps| {
            el.child(
                div()
                    .text_size(px(LABEL_SIZE))
                    .text_color(if fps >= 55.0 {
                        ink.success
                    } else {
                        ink.warning
                    })
                    .child(SharedString::from(format!("{fps:.0} fps"))),
            )
        });

        let _ = window;

        div()
            .flex()
            .flex_col()
            .flex_1()
            .min_h(px(0.0))
            .w_full()
            .child(bar)
            .child(self.frame_graph(&frames, &ink))
            .child(self.bands(&events, &ink))
            .child(self.list(&events, &ink, cx))
            .into_any_element()
    }

    /// One bar per recorded frame, red past the 60 Hz budget. This is the band
    /// that makes a stutter visible without any host instrumentation at all.
    fn frame_graph(&self, frames: &[Duration], ink: &Ink) -> AnyElement {
        let mut graph = div()
            .flex()
            .flex_none()
            .items_end()
            .gap(px(1.0))
            .h(px(46.0))
            .w_full()
            .px(px(8.0))
            .py(px(4.0))
            .bg(ink.content)
            .border_b_1()
            .border_color(ink.border);

        if frames.is_empty() {
            return graph
                .items_center()
                .child(div().text_size(px(LABEL_SIZE)).text_color(ink.dim).child(
                    SharedString::new_static(if self.recording {
                        "Measuring frames…"
                    } else {
                        "Press Record to measure frames"
                    }),
                ))
                .into_any_element();
        }

        // Scale against twice the budget so a normal frame sits at half height
        // and a dropped one visibly spikes.
        let ceiling = (FRAME_BUDGET.as_secs_f32() * 2.0).max(0.001);
        for delta in frames {
            let fraction = (delta.as_secs_f32() / ceiling).clamp(0.02, 1.0);
            let over = *delta > FRAME_BUDGET;
            graph = graph.child(
                div()
                    .flex_1()
                    .min_w(px(1.0))
                    .h(gpui::relative(fraction))
                    .bg(if over { ink.danger } else { ink.success }),
            );
        }

        graph.into_any_element()
    }

    /// The instrument bands: one row per kind, spans placed on a shared ruler.
    fn bands(&self, events: &[TimelineEvent], ink: &Ink) -> AnyElement {
        if events.is_empty() {
            return div().into_any_element();
        }

        let (start, end) = Self::span(events);
        let window = (end.saturating_sub(start)).as_secs_f32().max(0.001);

        let mut rows = div()
            .flex()
            .flex_col()
            .flex_none()
            .w_full()
            .bg(ink.content)
            .border_b_1()
            .border_color(ink.border);

        for kind in TimelineKind::ALL {
            let band: Vec<&TimelineEvent> =
                events.iter().filter(|event| event.kind == kind).collect();
            if band.is_empty() {
                continue;
            }

            let mut track = div().relative().flex_1().h(px(14.0)).bg(ink.stripe);
            for event in band {
                let offset = event.start.saturating_sub(start).as_secs_f32() / window;
                let width = (event.duration.as_secs_f32() / window).clamp(0.002, 1.0);
                track = track.child(
                    div()
                        .absolute()
                        .top(px(2.0))
                        .left(gpui::relative(offset.clamp(0.0, 0.998)))
                        .w(gpui::relative(width))
                        .h(px(10.0))
                        .rounded(px(2.0))
                        .bg(Self::band_color(kind, ink)),
                );
            }

            rows = rows.child(
                div()
                    .flex()
                    .items_center()
                    .gap(px(6.0))
                    .w_full()
                    .px(px(8.0))
                    .py(px(2.0))
                    .child(
                        div()
                            .flex_none()
                            .w(px(NAV_WIDTH - 40.0))
                            .text_size(px(LABEL_SIZE))
                            .text_color(ink.dim)
                            .child(SharedString::new_static(kind.label())),
                    )
                    .child(track),
            );
        }

        rows.into_any_element()
    }

    fn list(&self, events: &[TimelineEvent], ink: &Ink, cx: &mut Context<DevTools>) -> AnyElement {
        if events.is_empty() {
            return empty_state(
                "No timeline events recorded. Wrap work in devtools::measure to populate this.",
                ink,
            )
            .into_any_element();
        }

        let mut list = div()
            .id("devtools-timeline-list")
            .flex()
            .flex_col()
            .flex_1()
            .min_h(px(0.0))
            .w_full()
            .overflow_scroll()
            .bg(ink.content)
            .font_family(MONO_FAMILY)
            .text_size(px(MONO_SIZE));

        list = list.child(
            div()
                .flex()
                .flex_none()
                .items_center()
                .h(px(20.0))
                .w_full()
                .bg(ink.chrome)
                .border_b_1()
                .border_color(ink.border)
                .child(header_cell("Event", None, ink))
                .child(header_cell("Type", Some(140.0), ink))
                .child(header_cell("Start", Some(80.0), ink))
                .child(header_cell("Duration", Some(80.0), ink)),
        );

        // Newest first: the thing that just happened is the thing being chased.
        for (position, event) in events.iter().rev().enumerate() {
            let source = event.source.clone();
            let hover_bg = ink.hover;
            let mut row = div()
                .id(("devtools-timeline-row", position))
                .flex()
                .items_center()
                .flex_none()
                .h(px(ROW_HEIGHT))
                .w_full()
                .when(position % 2 == 1, |el| el.bg(ink.stripe))
                .hover(move |st| st.bg(hover_bg))
                .child(cell(event.label.clone(), None, ink.text))
                .child(cell(
                    event.kind.label(),
                    Some(140.0),
                    Self::band_color(event.kind, ink),
                ))
                .child(cell(format_duration(event.start), Some(80.0), ink.dim))
                .child(cell(
                    format_duration(event.duration),
                    Some(80.0),
                    if event.duration > FRAME_BUDGET {
                        ink.warning
                    } else {
                        ink.dim
                    },
                ));

            if let Some(source) = source {
                row = row.on_click(
                    cx.listener(move |this: &mut DevTools, _event, _window, cx| {
                        this.reveal_source(source.clone(), cx);
                    }),
                );
            }

            list = list.child(row);
        }

        list.into_any_element()
    }
}

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

    fn event(kind: TimelineKind, start_ms: u64, duration_ms: u64) -> TimelineEvent {
        let mut event = TimelineEvent::new(kind, "work", Duration::from_millis(duration_ms));
        event.start = Duration::from_millis(start_ms);
        event
    }

    #[test]
    fn the_span_covers_every_event() {
        let events = vec![
            event(TimelineKind::Script, 100, 40),
            event(TimelineKind::Layout, 20, 10),
            event(TimelineKind::Paint, 60, 5),
        ];

        let (start, end) = TimelinesPanel::span(&events);
        assert_eq!(start, Duration::from_millis(20));
        assert_eq!(end, Duration::from_millis(140));
    }

    #[test]
    fn an_empty_timeline_has_a_zero_span() {
        assert_eq!(TimelinesPanel::span(&[]), (Duration::ZERO, Duration::ZERO));
    }

    #[test]
    fn a_single_instant_event_does_not_invert_the_span() {
        let events = vec![event(TimelineKind::Script, 50, 0)];
        let (start, end) = TimelinesPanel::span(&events);
        assert_eq!(start, Duration::from_millis(50));
        assert_eq!(end, Duration::from_millis(50));
    }

    #[test]
    fn the_frame_budget_is_sixty_hertz() {
        assert!(FRAME_BUDGET < Duration::from_millis(17));
        assert!(FRAME_BUDGET > Duration::from_millis(16));
    }
}