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
//! The inspector, docked beside an app to inspect:
//! `cargo run -p guise-ui --example devtools`
//!
//! The left half is an ordinary `guise` UI. The right half is [`DevTools`],
//! reading that UI's live component tree and the records this example reports
//! as you click around.
//!
//! Pass a tab name to open on it: `--example devtools -- network`.
use std::time::Duration;
use gpui::prelude::*;
use gpui::{
div, px, size, App, Bounds, Context, Entity, IntoElement, SharedString, Window, WindowBounds,
WindowOptions,
};
use guise::prelude::*;
use guise::theme::{ColorName, Size, Theme};
struct Demo {
devtools: Entity<DevTools>,
checked: bool,
plan: Signal<usize>,
requests: u32,
name: Entity<TextInput>,
}
impl Demo {
fn new(cx: &mut Context<Self>) -> Self {
let devtools = cx.new(|cx| DevTools::new(cx).tab(starting_tab()));
let name = cx.new(|cx| {
TextInput::new(cx)
.label("Project")
.placeholder("guise")
.size(Size::Sm)
});
// The console prompt has nothing to evaluate against in a compiled
// binary, so the host answers it — which is the point of the event.
cx.subscribe(
&devtools,
|_this: &mut Demo, _devtools, event: &DevToolsEvent, cx| {
// The one thing the inspector cannot do alone: open a file in
// whatever the host calls an editor.
if let DevToolsEvent::RevealSource(source) = event {
guise::devtools::log(
cx,
LogLevel::Info,
format!("Host asked to open {}", source.short()),
);
}
},
)
.detach();
// Publish a storage domain up front so the Storage panel opens on
// something rather than on an empty state.
guise::devtools::storage_set(
cx,
StorageDomain::new("prefs", "example.preferences")
.kind(StorageKind::Local)
.entry(StorageEntry::new("theme", "dark"))
.entry(StorageEntry::new("plan", "0"))
.entry(StorageEntry::new("window", "1280×820")),
);
guise::devtools::log(
cx,
LogLevel::Info,
"Inspector attached. Click around on the left; the tree updates every frame.",
);
guise::devtools::log_record(
cx,
LogRecord::new(LogLevel::Warning, "Theme has no explicit `info` override")
.detail("scheme", "Dark")
.detail("falling back to", "Cyan/8"),
);
// A little seeded history so every panel opens on something. A real app
// would have reported these as the work actually happened.
let seeded: [(&str, &str, ResourceKind, u16, u64, u64, u64); 5] = [
(
"GET",
"https://api.example.com/v1/session",
ResourceKind::Document,
200,
1_204,
3_180,
46,
),
(
"GET",
"https://cdn.example.com/assets/app.css",
ResourceKind::Stylesheet,
200,
8_940,
41_220,
88,
),
(
"GET",
"https://cdn.example.com/assets/Inter.woff2",
ResourceKind::Font,
200,
31_002,
31_002,
132,
),
(
"POST",
"https://api.example.com/v1/events",
ResourceKind::Fetch,
202,
612,
612,
24,
),
(
"GET",
"https://api.example.com/v1/items?page=1",
ResourceKind::Fetch,
503,
344,
344,
214,
),
];
for (index, (method, url, kind, status, transfer, resource, ms)) in
seeded.into_iter().enumerate()
{
let mut record = NetworkRecord::new(method, url)
.kind(kind)
.status(
status,
if status < 400 {
"OK"
} else {
"Service Unavailable"
},
)
.sizes(transfer, resource)
.timings(Timings {
stalled: Duration::from_millis(1 + index as u64),
dns: Duration::from_millis(4),
connect: Duration::from_millis(9),
tls: Duration::from_millis(16),
request: Duration::from_millis(2),
response: Duration::from_millis(ms),
})
.request_header("Accept", "*/*")
.request_header("Cookie", "session=8f2c1a; theme=dark")
.response_header("Content-Type", "application/json")
.response_header("Set-Cookie", "session=8f2c1a; Path=/; HttpOnly")
.response_body("{\n \"items\": [],\n \"page\": 1\n}");
record.protocol = SharedString::new_static("h2");
record.priority = SharedString::new_static("High");
record.remote_address = SharedString::new_static("93.184.216.34:443");
record.start = Duration::from_millis(index as u64 * 60);
record.state = if status >= 400 {
RequestState::Failed
} else {
RequestState::Finished
};
guise::devtools::network_begin(cx, record);
}
for (label, kind, ms) in [
("Theme::init", TimelineKind::Script, 3u64),
("layout pass", TimelineKind::Layout, 12),
("paint", TimelineKind::Paint, 7),
("GET /v1/session", TimelineKind::Network, 46),
("reindex()", TimelineKind::Script, 28),
] {
guise::devtools::timeline_event(
cx,
TimelineEvent::new(kind, label, Duration::from_millis(ms)),
);
}
let plan = use_state(cx, 0usize);
Demo {
devtools,
checked: true,
plan,
requests: 0,
name,
}
}
/// Report a request the way a host with a real HTTP client would: open the
/// record when it starts, settle it by id when it finishes.
fn fetch(&mut self, cx: &mut Context<Self>) {
self.requests += 1;
let n = self.requests;
let id = guise::devtools::network_begin(
cx,
NetworkRecord::new("GET", format!("https://api.example.com/v1/items?page={n}"))
.kind(ResourceKind::Fetch)
.request_header("Accept", "application/json")
.request_header("Cookie", "session=8f2c1a; theme=dark"),
);
let Some(id) = id else { return };
let failed = n.is_multiple_of(4);
guise::devtools::network_update(cx, id, move |record| {
record.timings = Timings {
stalled: Duration::from_millis(2),
dns: Duration::from_millis(4),
connect: Duration::from_millis(11),
tls: Duration::from_millis(18),
request: Duration::from_millis(3),
response: Duration::from_millis(21 + u64::from(n % 6) * 14),
};
record.transfer_size = 4_200 + u64::from(n) * 137;
record.resource_size = 11_800 + u64::from(n) * 402;
record.protocol = SharedString::new_static("h2");
record.remote_address = SharedString::new_static("93.184.216.34:443");
record.priority = SharedString::new_static("High");
record
.response_headers
.push(("Content-Type".into(), "application/json".into()));
record.response_headers.push((
"Set-Cookie".into(),
"session=8f2c1a; Path=/; HttpOnly".into(),
));
record.response_body = Some("{\n \"items\": [],\n \"page\": 1\n}".into());
if failed {
record.state = RequestState::Failed;
record.status = Some(503);
record.status_text = SharedString::new_static("Service Unavailable");
record.error = Some(SharedString::new_static("upstream timed out"));
} else {
record.state = RequestState::Finished;
record.status = Some(200);
record.status_text = SharedString::new_static("OK");
}
});
if failed {
guise::devtools::log(
cx,
LogLevel::Error,
format!("Request {n} failed: upstream timed out"),
);
}
}
}
impl Render for Demo {
fn render(&mut self, _window: &mut Window, cx: &mut Context<Self>) -> impl IntoElement {
let t = cx.global::<Theme>();
let body = t.body().hsla();
let text = t.text().hsla();
let border = t.border().hsla();
let font = t.font_family.clone();
let app = Stack::new()
.gap(Size::Lg)
.child(Title::new("A small app").order(2))
.child(
Text::new(
"Everything here reports itself to the inspector on the right. \
Open Elements and select a node to read its real style.",
)
.size(Size::Sm)
.dimmed(),
)
.child(self.name.clone())
.child(
Group::new()
.gap(Size::Sm)
.child(
Button::new("fetch", "Fetch items")
.variant(Variant::Filled)
.on_click(cx.listener(|this: &mut Demo, _, _, cx| {
this.fetch(cx);
cx.notify();
})),
)
.child(
Button::new("work", "Slow work")
.variant(Variant::Default)
.on_click(cx.listener(|_this, _, _, cx| {
guise::devtools::measure(cx, "reindex()", || {
std::thread::sleep(Duration::from_millis(28));
});
cx.notify();
})),
)
.child(
Button::new("warn", "Warn")
.variant(Variant::Light)
.color(ColorName::Yellow)
.on_click(cx.listener(|_this, _, _, cx| {
guise::devtools::log_record(
cx,
LogRecord::new(
LogLevel::Warning,
"Layout pass exceeded the frame budget",
)
.detail("budget", "16.7ms")
.detail("actual", "22.4ms"),
);
cx.notify();
})),
),
)
.child(
Card::new().child(
Stack::new()
.gap(Size::Sm)
.child(Title::new("Settings").order(4))
.child(
Checkbox::new("notify")
.label("Email notifications")
.checked(self.checked)
.on_change(cx.listener(|this: &mut Demo, _, _, cx| {
this.checked = !this.checked;
guise::devtools::log(
cx,
LogLevel::Log,
format!("notifications = {}", this.checked),
);
cx.notify();
})),
)
.child(
RadioGroup::new()
.label("Plan")
.options(["Free", "Pro", "Team"])
.value(self.plan.get(cx))
.bind(self.plan.binding()),
)
.child(
Group::new()
.gap(Size::Xs)
.child(Badge::new("stable").color(ColorName::Green))
.child(Badge::new("v0.13").variant(Variant::Light)),
),
),
)
.child(
Alert::new("Nothing here opens a socket — the host reports, guise displays.")
.variant(Variant::Light),
);
div()
.size_full()
.flex()
.bg(body)
.text_color(text)
.font_family(font)
.child(
div()
.flex_1()
.min_w(px(0.0))
.h_full()
.p(px(28.0))
.overflow_hidden()
.child(app),
)
.child(div().w(px(1.0)).h_full().bg(border))
.child(
div()
.flex_none()
.w(px(620.0))
.h_full()
.child(self.devtools.clone()),
)
}
}
/// The tab named on the command line, so a screenshot or a demo can open
/// straight onto the panel it is about.
fn starting_tab() -> DevToolsTab {
let arg = std::env::args().nth(1).unwrap_or_default().to_lowercase();
DevToolsTab::ALL
.into_iter()
.find(|tab| tab.label().to_lowercase() == arg)
.unwrap_or(DevToolsTab::Elements)
}
fn main() {
gpui::Application::new().run(|cx: &mut App| {
Theme::dark().init(cx);
DevToolsState::new().init(cx);
let bounds = Bounds::centered(None, size(px(1360.0), px(840.0)), cx);
cx.open_window(
WindowOptions {
window_bounds: Some(WindowBounds::Windowed(bounds)),
..Default::default()
},
|_window, cx| cx.new(Demo::new),
)
.expect("open window");
cx.activate(true);
});
}