#[cfg(any(feature = "router", feature = "query", feature = "form"))]
use gpui::SharedString;
use gpui::{actions, div, prelude::*, App, Global, KeyBinding, Window};
use crate::{styles, sx::SxStyled};
actions!(
rok_devtools,
[
ToggleDevtools,
]
);
#[derive(Default)]
struct DevtoolsState {
open: bool,
}
impl Global for DevtoolsState {}
pub(crate) fn init(cx: &mut App) {
cx.bind_keys([KeyBinding::new("ctrl-shift-d", ToggleDevtools, None)]);
cx.on_action(|_: &ToggleDevtools, cx| toggle(cx));
}
pub fn toggle(cx: &mut App) {
let state = cx.default_global::<DevtoolsState>();
state.open = !state.open;
cx.refresh_windows();
}
#[must_use]
pub fn is_open(cx: &App) -> bool {
cx.try_global::<DevtoolsState>()
.is_some_and(|state| state.open)
}
styles! {
DEVTOOLS = {
panel: {
position: absolute,
bottom: 3,
right: 3,
width: 96,
max_height: 120,
display: flex,
direction: column,
gap: 3,
padding: 3,
radius: lg,
border: 1,
border_color: border,
background: popover,
color: popover_foreground,
shadow: lg,
text: xs,
overflow: hidden,
},
title: { font: semibold, text: sm },
section: { display: flex, direction: column, gap: 1 },
heading: { font: semibold, color: muted_foreground },
row: { display: flex, gap: 2, justify: between },
mono: { font_family: mono, truncate: true },
current: { color: primary, font: semibold },
bad: { color: destructive_text },
muted: { color: muted_foreground },
}
}
#[derive(IntoElement, Default)]
pub struct Devtools;
impl Devtools {
#[must_use]
pub fn new() -> Self {
Self
}
}
#[cfg(any(feature = "router", feature = "query", feature = "form"))]
fn line(text: impl Into<SharedString>, style: &crate::sx::Sx) -> gpui::Div {
div()
.sx((&DEVTOOLS.mono, style))
.child(crate::components::BidiText::new(text.into()))
}
#[cfg(feature = "router")]
fn router_section(cx: &mut App) -> gpui::AnyElement {
let (entries, index) = crate::router::history_entries(cx);
div()
.sx(&DEVTOOLS.section)
.child(div().sx(&DEVTOOLS.heading).child("Router"))
.children(
entries
.into_iter()
.enumerate()
.rev()
.take(8)
.map(|(position, entry)| {
let style = if position == index {
&DEVTOOLS.current
} else {
&DEVTOOLS.muted
};
line(entry, style)
}),
)
.into_any_element()
}
#[cfg(not(feature = "router"))]
fn router_section(_: &mut App) -> gpui::AnyElement {
div().into_any_element()
}
#[cfg(feature = "query")]
fn query_section(cx: &mut App) -> gpui::AnyElement {
let queries = crate::query::queries(cx);
let count = queries.len();
div()
.sx(&DEVTOOLS.section)
.child(
div()
.sx(&DEVTOOLS.heading)
.child(format!("Queries ({count})")),
)
.children(queries.into_iter().take(20).map(|query| {
let status = if query.is_fetching {
"fetching"
} else if query.error.is_some() {
"error"
} else if query.is_invalidated {
"stale"
} else if query.has_data {
"fresh"
} else {
"pending"
};
let age = query
.age
.map(|age| format!("{:.1}s", age.as_secs_f32()))
.unwrap_or_default();
div()
.sx(&DEVTOOLS.row)
.child(line(query.key.to_string(), &DEVTOOLS.mono))
.child(line(
format!("{status} {age}"),
if query.error.is_some() {
&DEVTOOLS.bad
} else {
&DEVTOOLS.muted
},
))
}))
.into_any_element()
}
#[cfg(not(feature = "query"))]
fn query_section(_: &mut App) -> gpui::AnyElement {
div().into_any_element()
}
#[cfg(feature = "query")]
fn mutation_section(cx: &mut App) -> gpui::AnyElement {
use crate::query::MutationStatus;
let mutations = crate::query::mutations(cx);
if mutations.is_empty() {
return div().into_any_element();
}
let running = mutations
.iter()
.filter(|mutation| mutation.status == MutationStatus::Pending)
.count();
div()
.sx(&DEVTOOLS.section)
.child(
div()
.sx(&DEVTOOLS.heading)
.child(format!("Mutations ({running} running)")),
)
.children(mutations.into_iter().take(10).map(|mutation| {
let status = match mutation.status {
MutationStatus::Pending => "running",
MutationStatus::Success => "done",
MutationStatus::Error => "failed",
MutationStatus::Idle => "dropped",
};
let time = mutation
.duration
.map(|duration| format!(" in {:.1}s", duration.as_secs_f32()))
.unwrap_or_default();
let name = mutation
.key
.map_or_else(|| "mutation".to_string(), |key| key.to_string());
div()
.sx(&DEVTOOLS.row)
.child(line(name, &DEVTOOLS.mono))
.child(line(
format!("{status}{time}"),
if mutation.status == MutationStatus::Error {
&DEVTOOLS.bad
} else {
&DEVTOOLS.muted
},
))
}))
.into_any_element()
}
#[cfg(not(feature = "query"))]
fn mutation_section(_: &mut App) -> gpui::AnyElement {
div().into_any_element()
}
#[cfg(feature = "form")]
fn form_section(cx: &mut App) -> gpui::AnyElement {
let forms = crate::form::live_forms(cx);
if forms.is_empty() {
return div().into_any_element();
}
let count = forms.len();
div()
.sx(&DEVTOOLS.section)
.child(
div()
.sx(&DEVTOOLS.heading)
.child(format!("Forms ({count})")),
)
.children(forms.into_iter().take(10).map(|form| {
let status = if form.is_submitting {
"submitting".to_string()
} else if form.is_submitted {
"submitted".to_string()
} else if !form.is_valid {
"invalid".to_string()
} else if form.is_dirty {
"edited".to_string()
} else {
"pristine".to_string()
};
let attempts = if form.attempts > 0 {
format!(", {} submit(s)", form.attempts)
} else {
String::new()
};
let errors = form
.form_errors
.iter()
.map(ToString::to_string)
.chain(
form.field_errors
.iter()
.map(|(key, message)| format!("{key}: {message}")),
)
.take(6)
.map(|error| line(error, &DEVTOOLS.bad));
div()
.child(
div()
.sx(&DEVTOOLS.row)
.child(line(form.name.clone(), &DEVTOOLS.mono))
.child(line(
format!("{status}{attempts}"),
if form.is_valid {
&DEVTOOLS.muted
} else {
&DEVTOOLS.bad
},
)),
)
.children(errors)
}))
.into_any_element()
}
#[cfg(not(feature = "form"))]
fn form_section(_: &mut App) -> gpui::AnyElement {
div().into_any_element()
}
impl RenderOnce for Devtools {
fn render(self, _: &mut Window, cx: &mut App) -> impl IntoElement {
if !is_open(cx) {
return div().into_any_element();
}
div()
.id("rok-devtools")
.sx(&DEVTOOLS.panel)
.child(div().sx(&DEVTOOLS.title).child("rok-ui devtools"))
.child(router_section(cx))
.child(query_section(cx))
.child(mutation_section(cx))
.child(form_section(cx))
.into_any_element()
}
}
#[cfg(test)]
mod tests {
use super::*;
#[gpui::test]
fn the_shortcut_action_toggles_the_overlay(cx: &mut gpui::TestAppContext) {
cx.update(|cx| {
crate::init(cx);
assert!(!is_open(cx));
toggle(cx);
assert!(is_open(cx));
cx.dispatch_action(&ToggleDevtools);
assert!(!is_open(cx));
});
}
struct Root;
impl Render for Root {
fn render(&mut self, _: &mut Window, _: &mut gpui::Context<Self>) -> impl IntoElement {
crate::components::AppRoot::new().child(Devtools::new())
}
}
#[gpui::test]
fn the_open_overlay_renders_queries_and_history(cx: &mut gpui::TestAppContext) {
cx.update(|cx| {
crate::init(cx);
#[cfg(feature = "query")]
crate::query::set_query_data(cx, &crate::query_key!["notes", 1], "Groceries");
#[cfg(feature = "router")]
crate::router::navigate("/notes/1", cx);
toggle(cx);
});
let (_, window) = cx.add_window_view(|_, _| Root);
window.run_until_parked();
#[cfg(feature = "router")]
window.update(|_, cx| {
let (entries, index) = crate::router::history_entries(cx);
assert_eq!((entries.len(), index), (2, 1));
});
}
}