use crate::{
components::CopyButton,
icons::CarbonIcon,
t3::craft::{mono, rem},
theme::{LayerLevel, color},
tokens::{self, ColorRole},
u0::panels::code_viewer::scroll_target,
u0::panels::head::{code_head, grow, head_icon},
u0::primitives::parts::{focus_ring, focus_visible, keyed_focus},
};
use gpui_kit::{
App, ElementId, InteractiveElement as _, IntoElement, KeyDownEvent, ParentElement, RenderOnce,
ScrollHandle, SharedString, StatefulInteractiveElement as _, Styled, Window, div, point,
prelude::FluentBuilder as _, px,
};
use std::rc::Rc;
const MAX_HEIGHT: f32 = 160.;
const LINE: f32 = 12. * 1.4;
const COPY: f32 = 32.;
#[derive(Clone, Debug, PartialEq)]
pub enum Payload {
Null,
Bool(bool),
Number(f64),
String(SharedString),
Array(Vec<Payload>),
Object(Vec<(SharedString, Payload)>),
}
impl Payload {
pub fn object<K: Into<SharedString>>(pairs: impl IntoIterator<Item = (K, Payload)>) -> Self {
Self::Object(pairs.into_iter().map(|(k, v)| (k.into(), v)).collect())
}
pub fn str(s: impl Into<SharedString>) -> Self {
Self::String(s.into())
}
pub fn to_pretty(&self) -> String {
let mut out = String::new();
self.write(&mut out, 0);
out
}
fn write(&self, out: &mut String, depth: usize) {
let pad = |out: &mut String, d: usize| out.push_str(&" ".repeat(d));
match self {
Self::Null => out.push_str("null"),
Self::Bool(b) => out.push_str(if *b { "true" } else { "false" }),
Self::Number(n) => out.push_str(&number(*n)),
Self::String(s) => quote(out, s),
Self::Array(items) if items.is_empty() => out.push_str("[]"),
Self::Object(pairs) if pairs.is_empty() => out.push_str("{}"),
Self::Array(items) => {
out.push_str("[\n");
for (i, v) in items.iter().enumerate() {
pad(out, depth + 1);
v.write(out, depth + 1);
out.push_str(if i + 1 < items.len() { ",\n" } else { "\n" });
}
pad(out, depth);
out.push(']');
}
Self::Object(pairs) => {
out.push_str("{\n");
for (i, (k, v)) in pairs.iter().enumerate() {
pad(out, depth + 1);
quote(out, k);
out.push_str(": ");
v.write(out, depth + 1);
out.push_str(if i + 1 < pairs.len() { ",\n" } else { "\n" });
}
pad(out, depth);
out.push('}');
}
}
}
}
fn number(n: f64) -> String {
if !n.is_finite() {
"null".into()
} else if n.fract() == 0. && n.abs() < 1e15 {
format!("{}", n as i64)
} else {
format!("{n}")
}
}
fn quote(out: &mut String, s: &str) {
out.push('"');
for c in s.chars() {
match c {
'"' => out.push_str("\\\""),
'\\' => out.push_str("\\\\"),
'\n' => out.push_str("\\n"),
'\r' => out.push_str("\\r"),
'\t' => out.push_str("\\t"),
c if (c as u32) < 0x20 => out.push_str(&format!("\\u{:04x}", c as u32)),
c => out.push(c),
}
}
out.push('"');
}
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub enum Syn {
Property,
String,
Plain,
}
impl Syn {
pub fn role(self) -> ColorRole {
match self {
Self::Property => ColorRole::SyntaxProperty,
Self::String => ColorRole::SyntaxString,
Self::Plain => ColorRole::SyntaxVariable,
}
}
}
pub fn line_runs(line: &str) -> Vec<(&str, Syn)> {
let b = line.as_bytes();
let mut out = Vec::new();
let (mut start, mut i) = (0, 0);
let plain = |s: &'_ str| {
if s.trim_start().starts_with('"') || s.contains(": \"") {
Syn::String
} else {
Syn::Plain
}
};
while i < b.len() {
if b[i] == b'"'
&& let Some(len) = line[i + 1..].find('"')
&& len > 0
&& b.get(i + 1 + len + 1) == Some(&b':')
{
let end = i + len + 2;
if start < i {
out.push((&line[start..i], plain(&line[start..i])));
}
out.push((&line[i..end], Syn::Property));
start = end;
i = end + 1;
continue;
}
i += 1;
}
if start < line.len() {
out.push((&line[start..], plain(&line[start..])));
}
out
}
#[derive(Clone, Debug, PartialEq)]
pub enum TelemetryLevel {
Number(u32),
Named(SharedString),
}
impl From<u32> for TelemetryLevel {
fn from(n: u32) -> Self {
Self::Number(n)
}
}
impl From<&'static str> for TelemetryLevel {
fn from(s: &'static str) -> Self {
Self::Named(s.into())
}
}
#[derive(Clone)]
pub struct TelemetryPreviewTexts {
pub at_level: Rc<dyn Fn(u32) -> SharedString>,
pub at_named: Rc<dyn Fn(&str) -> SharedString>,
pub copy: SharedString,
pub copied: SharedString,
}
impl Default for TelemetryPreviewTexts {
fn default() -> Self {
Self {
at_level: Rc::new(|n| format!("What is sent at level {n}").into()),
at_named: Rc::new(|s| format!("What is sent at {s}").into()),
copy: "Copy JSON".into(),
copied: "Copied!".into(),
}
}
}
impl TelemetryPreviewTexts {
pub fn head(&self, level: &TelemetryLevel) -> SharedString {
match level {
TelemetryLevel::Number(n) => (self.at_level)(*n),
TelemetryLevel::Named(s) => (self.at_named)(s),
}
}
}
#[derive(IntoElement)]
pub struct TelemetryPreview {
id: ElementId,
level: TelemetryLevel,
payload: Payload,
texts: TelemetryPreviewTexts,
}
impl TelemetryPreview {
pub fn new(
id: impl Into<ElementId>,
level: impl Into<TelemetryLevel>,
payload: Payload,
) -> Self {
Self {
id: id.into(),
level: level.into(),
payload,
texts: TelemetryPreviewTexts::default(),
}
}
pub fn texts(mut self, texts: TelemetryPreviewTexts) -> Self {
self.texts = texts;
self
}
}
impl RenderOnce for TelemetryPreview {
fn render(self, window: &mut Window, cx: &mut App) -> impl IntoElement {
let id = self.id;
let json = self.payload.to_pretty();
let scroll = window
.use_keyed_state((id.clone(), "scroll"), cx, |_, _| ScrollHandle::new())
.read(cx)
.clone();
let head = code_head()
.child(head_icon(CarbonIcon::Json))
.child(
div()
.min_w_0()
.overflow_hidden()
.whitespace_nowrap()
.text_ellipsis()
.child(self.texts.head(&self.level)),
)
.child(grow())
.child(
CopyButton::new((id.clone(), "copy"), json.clone())
.label(self.texts.copy.clone())
.feedback(self.texts.copied.clone())
.size(COPY)
.quiet(true)
.layer(LayerLevel::One),
);
let lines = json.lines().map(|line| {
div().flex().whitespace_nowrap().min_h(rem(LINE)).children(
line_runs(line).into_iter().map(|(text, syn)| {
div()
.flex_none()
.text_color(color(syn.role()))
.child(text.to_string())
}),
)
});
let focus = keyed_focus((id.clone(), "pre-focus"), window, cx);
let ring = focus_visible(&focus, window);
let scrolls = f32::from(scroll.max_offset().y) > 0.;
let key_scroll = scroll.clone();
let pre = mono(div(), 12., 400)
.id((id.clone(), "pre"))
.track_focus(&focus.clone().tab_index(0).tab_stop(scrolls))
.on_key_down(move |e: &KeyDownEvent, window, cx| {
let s = &key_scroll;
if let Some(y) = scroll_target(
e.keystroke.key.as_str(),
f32::from(s.offset().y),
f32::from(s.bounds().size.height),
f32::from(s.max_offset().y),
) {
s.set_offset(point(s.offset().x, px(y)));
window.refresh();
cx.stop_propagation();
}
})
.max_h(rem(MAX_HEIGHT))
.overflow_scroll()
.track_scroll(&scroll)
.py(tokens::SPACING_03)
.px(tokens::SPACING_04)
.bg(color(ColorRole::Background))
.text_color(color(ColorRole::SyntaxVariable))
.line_height(rem(LINE))
.child(div().flex().flex_col().children(lines));
div()
.id(id)
.flex()
.flex_col()
.w_full()
.min_w_0()
.bg(color(ColorRole::Layer01))
.child(head)
.child(
div()
.relative()
.child(pre)
.when(ring, |d| d.child(focus_ring())),
)
}
}
#[cfg(test)]
mod tests {
use super::*;
fn reference() -> Payload {
Payload::object([
("app", Payload::str("1.4.2")),
("os", Payload::str("macOS 15.2")),
(
"crash",
Payload::object([
("signal", Payload::str("SIGABRT")),
("frames", Payload::Number(12.)),
]),
),
(
"features",
Payload::object([
("palette", Payload::Number(14.)),
("sideChat", Payload::Number(3.)),
]),
),
])
}
#[test]
fn pretty_prints_like_json_stringify() {
assert_eq!(
reference().to_pretty(),
"{\n \"app\": \"1.4.2\",\n \"os\": \"macOS 15.2\",\n \"crash\": {\n \"signal\": \"SIGABRT\",\n \"frames\": 12\n },\n \"features\": {\n \"palette\": 14,\n \"sideChat\": 3\n }\n}"
);
assert_eq!(
Payload::Array(vec![
Payload::Null,
Payload::Bool(true),
Payload::Number(1.5)
])
.to_pretty(),
"[\n null,\n true,\n 1.5\n]"
);
assert_eq!(Payload::object::<&str>([]).to_pretty(), "{}");
assert_eq!(Payload::str("a\"b").to_pretty(), "\"a\\\"b\"");
}
#[test]
fn lines_split_keys_and_strings() {
assert_eq!(
line_runs(" \"os\": \"macOS 15.2\","),
[
(" ", Syn::Plain),
("\"os\"", Syn::Property),
(": \"macOS 15.2\",", Syn::String)
]
);
assert_eq!(
line_runs(" \"frames\": 12"),
[
(" ", Syn::Plain),
("\"frames\"", Syn::Property),
(": 12", Syn::Plain)
]
);
assert_eq!(line_runs(" \"a\","), [(" \"a\",", Syn::String)]);
assert_eq!(line_runs("}"), [("}", Syn::Plain)]);
}
#[test]
fn head_names_the_level() {
let t = TelemetryPreviewTexts::default();
assert_eq!(t.head(&2.into()).as_ref(), "What is sent at level 2");
assert_eq!(
t.head(&"Crash reports".into()).as_ref(),
"What is sent at Crash reports"
);
assert_eq!(Syn::Property.role(), ColorRole::SyntaxProperty);
}
}