use super::*;
pub(crate) fn automation_protocol_label() -> &'static str {
#[cfg(debug_assertions)]
{
AUTOMATION_SCHEMA_V2
}
#[cfg(not(debug_assertions))]
{
AUTOMATION_SCHEMA_STRIPPED
}
}
pub(crate) fn automation_compat_protocol_label() -> &'static str {
#[cfg(debug_assertions)]
{
AI_SCHEMA_PROTOCOL_VERSION
}
#[cfg(not(debug_assertions))]
{
AUTOMATION_SCHEMA_STRIPPED
}
}
pub(crate) fn automation_bridge_label() -> &'static str {
if DevAutomationBridge::is_enabled() {
AUTOMATION_BRIDGE_ACTIVE
} else {
AUTOMATION_BRIDGE_OFF
}
}
pub(crate) fn build_automation_schema_json(ctx: &Context, state: &ShowcaseState) -> String {
let rs: ReactiveStats = reactive_stats();
let vp: Size = ctx.get_viewport_size().get_untracked();
let bp: Breakpoint = ctx.get_breakpoint().get_untracked();
let route: String = state.get_route().get_untracked();
let scheme: DesignScheme = state.get_theme().get_scheme().get_untracked();
let mode: ThemeMode = state.get_theme().get_mode().get_untracked();
let live_fps: f32 = ctx.get_live_fps().get_untracked();
let hz: f32 = ctx
.get_display_refresh_rate()
.get_untracked()
.clamp(30.0, 480.0);
let (alive_count, a11y_count, sample_nodes): (usize, usize, Vec<String>) =
ctx.get_scene().with_read(
|tree: &SceneTree| {
let mut alive: usize = 0;
let mut a11y_total: usize = 0;
let mut samples: Vec<String> = Vec::new();
for (idx, node) in tree.get_nodes().iter().enumerate() {
if !node.get_alive() {
continue;
}
let nid: NodeId = NodeId::new(idx as u32, node.get_generation());
if !tree.is_node_visible(nid) {
continue;
}
alive += 1;
if let Some(a11y) = node.a11y_props_ref() {
a11y_total += 1;
if samples.len() < AUTOMATION_PREVIEW_NODE_LIMIT {
let b: Rect = node.get_layout_box().get_bounds();
let escaped: String = a11y.get_label().replace('"', "'");
samples.push(format!(
" {{\"id\": {idx}, \"role\": \"{:?}\", \"label\": \"{escaped}\", \"bounds\": [{:.0}, {:.0}, {:.0}, {:.0}]}}",
a11y.get_role(),
b.get_x(),
b.get_y(),
b.get_width(),
b.get_height()
));
}
}
}
(alive, a11y_total, samples)
},
(0, 0, Vec::new()),
);
let nodes_block: String = sample_nodes.join(",\n");
format!(
"{{\n \"protocol\": \"{}\",\n \"bridge\": \"{}\",\n \"window\": {{\"route\": \"{route}\", \"viewport\": [{:.0}, {:.0}], \"breakpoint\": \"{bp:?}\", \"refresh_hz\": {:.0}, \"live_fps\": {:.1}}},\n \"theme\": {{\"scheme\": \"{}\", \"mode\": \"{mode:?}\"}},\n \"reactive\": {{\"signals\": {}, \"memos\": {}, \"effects\": {}, \"scopes\": {}, \"clock\": {}}},\n \"scene\": {{\"alive_nodes\": {alive_count}, \"a11y_nodes\": {a11y_count}}},\n \"a11y_preview\": [\n{nodes_block}\n ]\n}}",
automation_protocol_label(),
automation_bridge_label(),
vp.get_width(),
vp.get_height(),
hz,
if live_fps.is_finite() && live_fps > 0.05 {
live_fps
} else {
0.0
},
scheme.as_str(),
rs.get_live_signals(),
rs.get_live_memos(),
rs.get_live_effects(),
rs.get_live_scopes(),
rs.get_clock()
)
}
pub(crate) fn scan_automation_layout_anomalies(ctx: &Context) -> (usize, Vec<String>) {
let vp: Size = ctx.get_viewport_size().get_untracked();
let vw: f32 = vp.get_width().max(1.0);
ctx.get_scene().with_read(
|tree: &SceneTree| {
let mut checked: usize = 0;
let mut findings: Vec<String> = Vec::new();
for (idx, node) in tree.get_nodes().iter().enumerate() {
if !node.get_alive() {
continue;
}
let nid: NodeId = NodeId::new(idx as u32, node.get_generation());
if !tree.is_node_visible(nid) {
continue;
}
checked += 1;
let mut clipped_by_scroll: bool = false;
let mut anc: Option<NodeId> = node.parent_id();
while let Some(pid) = anc {
if let Some(pnode) = tree.get_node(pid) {
let pb: Rect = pnode.get_layout_box().get_bounds();
if pnode.style_ref().get_layout().get_overflow() == Overflow::Scroll
&& pb.get_width() > 0.0
&& pb.right() <= vw + 1.5
{
clipped_by_scroll = true;
break;
}
anc = pnode.parent_id();
} else {
break;
}
}
let b: Rect = node.get_layout_box().get_bounds();
if !clipped_by_scroll && b.get_width() > 0.0 && b.right() > vw + 1.5 {
findings.push(format!(
"HorizontalOverflow node#{idx}: right={:.1} > vw={:.1}",
b.right(),
vw
));
}
if let Some(a11y) = node.a11y_props_ref()
&& a11y.get_role() == A11yRole::Button
&& (b.get_width() <= 0.0 || b.get_height() <= 0.0)
{
findings.push(format!(
"ZeroSizedInteractive node#{idx} ({}): {:.0}x{:.0}",
a11y.get_label(),
b.get_width(),
b.get_height()
));
}
}
(checked, findings)
},
(0, Vec::new()),
)
}
pub(crate) fn query_automation_scene_nodes(ctx: &Context, needle: &str) -> String {
let trimmed: String = needle.trim().to_lowercase();
if trimmed.is_empty() {
return AUTOMATION_QUERY_IDLE.to_string();
}
ctx.get_scene().with_read(
|tree: &SceneTree| {
let mut count: usize = 0;
let mut first_desc: Option<String> = None;
for (idx, node) in tree.get_nodes().iter().enumerate() {
if !node.get_alive() {
continue;
}
let nid: NodeId = NodeId::new(idx as u32, node.get_generation());
if !tree.is_node_visible(nid) {
continue;
}
let text_hit: Option<String> = match node.get_kind() {
NodeKind::Text(t) if t.to_lowercase().contains(trimmed.as_str()) => {
Some(t.clone())
}
_ => None,
};
let a11y_hit: Option<String> = node
.a11y_props_ref()
.filter(|a: &&A11yProps| {
a.get_label().to_lowercase().contains(trimmed.as_str())
})
.map(|a: &A11yProps| format!("{:?}:{}", a.get_role(), a.get_label()));
if let Some(hit_label) = a11y_hit.or(text_hit) {
count += 1;
if first_desc.is_none() {
let b: Rect = node.get_layout_box().get_bounds();
first_desc = Some(format!(
"node#{idx} `{hit_label}` @ ({:.0},{:.0} {:.0}x{:.0})",
b.get_x(),
b.get_y(),
b.get_width(),
b.get_height()
));
}
}
}
match first_desc {
Some(first) => format!("Matched {count} node(s) | First: {first}"),
None => format!("Matched 0 nodes for query `{trimmed}`"),
}
},
AUTOMATION_QUERY_IDLE.to_string(),
)
}
pub(crate) fn mount_automation_schema_card(
state: &ShowcaseState,
ai_tick: Signal<u32>,
tokens: ThemeTokens,
) -> NodeId {
let p: ThemePalette = tokens.get_palette();
let g: ThemeGeometry = tokens.get_geometry();
let ctx: Context = current_context();
let ai_fps_sig: Signal<f32> = ctx.get_live_fps();
let ai_hz_sig: Signal<f32> = ctx.get_display_refresh_rate();
let ai_route_sig: Signal<String> = state.get_route();
let ai_scheme_sig: Signal<DesignScheme> = state.get_theme().get_scheme();
let telemetry_line = move || -> String {
let _: u32 = ai_tick.get();
let rs: ReactiveStats = reactive_stats();
let ai_fps: f32 = ai_fps_sig.get();
let ai_hz: f32 = ai_hz_sig.get().clamp(30.0, 480.0);
let fps_display: String = if !ai_fps.is_finite() || ai_fps <= 0.05 {
LABEL_FPS_IDLE.to_string()
} else {
format!("{ai_fps:.1}")
};
format!(
"Protocol: {} ({}) | Route: {} | Scheme: {} | Display: {:.0}Hz | Live FPS: {} | Signals: {} | Memos: {} | Effects: {}",
automation_protocol_label(),
automation_bridge_label(),
ai_route_sig.get(),
ai_scheme_sig.get().as_str(),
ai_hz,
fps_display,
rs.get_live_signals(),
rs.get_live_memos(),
rs.get_live_effects()
)
};
let json_ctx: Context = ctx.clone();
let json_state: ShowcaseState = state.clone();
let schema_json_text = move || -> String {
let _: u32 = ai_tick.get();
build_automation_schema_json(&json_ctx, &json_state)
};
let on_refresh = move |_: &UiEvent| {
let _: bool = ai_tick.update(|v: &mut u32| *v = v.wrapping_add(1));
};
view! {
EuvCard {
title: AUTOMATION_SCHEMA_CARD_TITLE.to_string()
tokens: tokens
subtitle: AUTOMATION_SCHEMA_CARD_SUB
selectable: true
column {
style: {
width: Length::Percent(100.0)
gap: SPACE_SM
}
text {
style: {
width: Length::Percent(100.0)
font_size: 12.5
font_weight: FontWeight::SemiBold
color: p.get_primary()
}
{ telemetry_line }
}
EuvActionRow {
EuvButton {
label: AUTOMATION_BTN_REFRESH_SCHEMA.to_string()
tokens: tokens
sm()
on_click: on_refresh
}
}
column {
style: {
width: Length::Percent(100.0)
padding: Edges::all(SPACE_MD)
background: p.get_muted()
border: (g.get_border_width(), p.get_border())
radius: g.get_radius_md()
overflow: Overflow::Hidden
}
text {
style: {
width: Length::Percent(100.0)
font_size: 11.5
font_family: FontFamily::Monospace
color: p.get_foreground()
}
{ schema_json_text }
}
}
}
}
}
}
pub(crate) fn mount_automation_anomaly_card(tokens: ThemeTokens) -> NodeId {
let p: ThemePalette = tokens.get_palette();
let g: ThemeGeometry = tokens.get_geometry();
let ctx: Context = current_context();
let anomaly_summary: Signal<String> = Signal::new(AUTOMATION_ANOMALY_READY.to_string());
let anomaly_findings: Signal<Vec<String>> = Signal::new(Vec::new());
let anomaly_runs: Signal<u32> = Signal::new(0);
let summary_text = move || -> String { anomaly_summary.get() };
let ctx_scan: Context = ctx.clone();
let on_run_check = move |_: &UiEvent| {
let (checked, findings): (usize, Vec<String>) = scan_automation_layout_anomalies(&ctx_scan);
let run_no: u32 = anomaly_runs.get_untracked().saturating_add(1);
let _: bool = anomaly_runs.set(run_no);
let vp: Size = ctx_scan.get_viewport_size().get_untracked();
let status: String = if findings.is_empty() {
format!(
"Run #{run_no}: {AUTOMATION_ANOMALY_CLEAN} ({checked} nodes @ {:.0}x{:.0})",
vp.get_width(),
vp.get_height()
)
} else {
format!(
"Run #{run_no}: {} anomaly(s) across {checked} nodes @ {:.0}x{:.0}",
findings.len(),
vp.get_width(),
vp.get_height()
)
};
let _: bool = anomaly_summary.set(status);
let _: bool = anomaly_findings.set(findings);
};
let build_findings_rows = move || -> Vec<NodeId> {
let live: ThemeTokens = live_tokens_untracked(tokens);
let live_p: ThemePalette = live.get_palette();
let items: Vec<String> = anomaly_findings.get();
if items.is_empty() {
let label: String = if anomaly_runs.get() == 0 {
AUTOMATION_ANOMALY_READY.to_string()
} else {
AUTOMATION_ANOMALY_CLEAN.to_string()
};
return vec![view! {
text {
style: {
width: Length::Percent(100.0)
font_size: 12.0
font_family: FontFamily::Monospace
color: live_p.get_foreground()
}
{ label }
}
}];
}
items
.into_iter()
.map(|item: String| {
view! {
text {
style: {
width: Length::Percent(100.0)
font_size: 12.0
font_family: FontFamily::Monospace
color: live_p.get_foreground()
}
{ item }
}
}
})
.collect()
};
view! {
EuvCard {
title: AUTOMATION_ANOMALY_CARD_TITLE.to_string()
tokens: tokens
subtitle: AUTOMATION_ANOMALY_CARD_SUB
column {
style: {
width: Length::Percent(100.0)
gap: SPACE_SM
}
text {
style: {
width: Length::Percent(100.0)
font_size: 13.0
font_weight: FontWeight::SemiBold
color: p.get_card_foreground()
}
{ summary_text }
}
EuvActionRow {
EuvButton {
label: AUTOMATION_BTN_RUN_ANOMALY.to_string()
tokens: tokens
sm()
on_click: on_run_check
}
}
column {
style: {
width: Length::Percent(100.0)
padding: Edges::all(SPACE_SM)
gap: SPACE_XS
background: p.get_muted()
border: (g.get_border_width(), p.get_border())
radius: g.get_radius_md()
overflow: Overflow::Hidden
}
bind_children: build_findings_rows
}
}
}
}
}
pub(crate) fn mount_automation_bridge_card(tokens: ThemeTokens) -> NodeId {
let p: ThemePalette = tokens.get_palette();
let bridge_enabled: bool = DevAutomationBridge::is_enabled();
let badge_tone: EuvTone = if bridge_enabled {
EuvTone::Success
} else {
EuvTone::Neutral
};
let meta_line: String = format!(
"Protocol: {} (compat: {}) | Bind: 127.0.0.1:4174 | Queue: 16 | Budget: 2000us",
automation_protocol_label(),
automation_compat_protocol_label()
);
view! {
EuvCard {
title: AUTOMATION_BRIDGE_CARD_TITLE.to_string()
tokens: tokens
subtitle: AUTOMATION_BRIDGE_CARD_SUB
column {
style: {
width: Length::Percent(100.0)
gap: SPACE_SM
}
row {
style: {
width: Length::Percent(100.0)
flex_wrap: FlexWrap::Wrap
align: AlignItems::Center
gap: SPACE_SM
}
EuvBadge {
label: automation_bridge_label().to_string()
tone: badge_tone
tokens: tokens
}
EuvBadge {
label: automation_protocol_label().to_string()
tone: EuvTone::Primary
tokens: tokens
}
}
text {
style: {
width: Length::Percent(100.0)
font_size: 12.0
font_family: FontFamily::Monospace
color: p.get_muted_foreground()
}
{ meta_line }
}
EuvCodeBlock {
code: AUTOMATION_BRIDGE_ENDPOINTS.to_string()
tokens: tokens
}
}
}
}
}
pub(crate) fn mount_automation_runner_card(
state: &ShowcaseState,
ai_tick: Signal<u32>,
tokens: ThemeTokens,
) -> NodeId {
let p: ThemePalette = tokens.get_palette();
let g: ThemeGeometry = tokens.get_geometry();
let ctx: Context = current_context();
let query_sig: Signal<String> = Signal::new(AUTOMATION_QUERY_DEFAULT.to_string());
let query_result: Signal<String> = Signal::new(AUTOMATION_QUERY_IDLE.to_string());
let sandbox_clicks: Signal<u32> = Signal::new(0);
let action_log: Signal<Vec<String>> = Signal::new(Vec::new());
let push_trace = move |entry: String| {
let _: bool = action_log.update(|items: &mut Vec<String>| {
items.insert(0, entry);
if items.len() > AUTOMATION_LOG_LIMIT {
items.truncate(AUTOMATION_LOG_LIMIT);
}
});
};
let query_result_text = move || -> String { query_result.get() };
let sandbox_status_text = move || -> String {
format!(
"Sandbox Target Clicks: {} | Actions Logged: {}",
sandbox_clicks.get(),
action_log.get().len()
)
};
let ctx_query: Context = ctx.clone();
let on_query = move |_: &UiEvent| {
let q: String = query_sig.get_untracked();
let res: String = query_automation_scene_nodes(&ctx_query, &q);
let _: bool = query_result.set(res.clone());
push_trace(format!("Query(`{q}`) -> {res}"));
};
let on_sandbox_click = move |_: &UiEvent| {
let next: u32 = sandbox_clicks.get_untracked().saturating_add(1);
let _: bool = sandbox_clicks.set(next);
let _: bool = ai_tick.update(|v: &mut u32| *v = v.wrapping_add(1));
push_trace(format!("Action::Click(sandbox) -> OK (hits={next})"));
};
let state_theme: ShowcaseState = state.clone();
let on_toggle_theme = move |_: &UiEvent| {
state_theme.toggle_theme_mode();
let mode: ThemeMode = state_theme.get_theme().get_mode().get_untracked();
let _: bool = ai_tick.update(|v: &mut u32| *v = v.wrapping_add(1));
push_trace(format!("Action::SetTheme(mode={mode:?}) -> OK"));
};
let on_clear_log = move |_: &UiEvent| {
let _: bool = action_log.set_if_changed(Vec::new());
};
let build_trace_rows = move || -> Vec<NodeId> {
let live: ThemeTokens = live_tokens_untracked(tokens);
let live_p: ThemePalette = live.get_palette();
let items: Vec<String> = action_log.get();
if items.is_empty() {
return vec![view! {
text {
style: {
width: Length::Percent(100.0)
font_size: 12.0
font_family: FontFamily::Monospace
color: live_p.get_muted_foreground()
}
{ AUTOMATION_LOG_EMPTY }
}
}];
}
items
.into_iter()
.map(|entry: String| {
view! {
text {
style: {
width: Length::Percent(100.0)
font_size: 11.5
font_family: FontFamily::Monospace
color: live_p.get_foreground()
}
{ entry }
}
}
})
.collect()
};
view! {
EuvCard {
title: AUTOMATION_RUNNER_CARD_TITLE.to_string()
tokens: tokens
subtitle: AUTOMATION_RUNNER_CARD_SUB
column {
style: {
width: Length::Percent(100.0)
gap: SPACE_SM
}
EuvInput {
value: query_sig
placeholder: AUTOMATION_QUERY_PLACEHOLDER.to_string()
tokens: tokens
}
EuvActionRow {
EuvButton {
label: AUTOMATION_BTN_QUERY_NODES.to_string()
tokens: tokens
sm()
on_click: on_query
}
EuvButton {
label: AUTOMATION_BTN_SANDBOX_CLICK.to_string()
tokens: tokens
secondary()
sm()
on_click: on_sandbox_click
}
EuvButton {
label: AUTOMATION_BTN_THEME_ACTION.to_string()
tokens: tokens
outline()
sm()
on_click: on_toggle_theme
}
EuvButton {
label: AUTOMATION_BTN_CLEAR_LOG.to_string()
tokens: tokens
ghost()
sm()
on_click: on_clear_log
}
}
text {
style: {
width: Length::Percent(100.0)
font_size: 12.0
font_weight: FontWeight::Medium
color: p.get_primary()
}
{ query_result_text }
}
text {
style: {
width: Length::Percent(100.0)
font_size: 12.0
font_family: FontFamily::Monospace
color: p.get_muted_foreground()
}
{ sandbox_status_text }
}
column {
style: {
width: Length::Percent(100.0)
padding: Edges::all(SPACE_SM)
gap: SPACE_XS
background: p.get_muted()
border: (g.get_border_width(), p.get_border())
radius: g.get_radius_md()
overflow: Overflow::Hidden
}
bind_children: build_trace_rows
}
}
}
}
}
pub fn mount_automation_page(state: &ShowcaseState, tokens: ThemeTokens) -> NodeId {
let ai_tick: Signal<u32> = Signal::new(0);
let schema_card: NodeId = mount_automation_schema_card(state, ai_tick, tokens);
let anomaly_card: NodeId = mount_automation_anomaly_card(tokens);
let bridge_card: NodeId = mount_automation_bridge_card(tokens);
let runner_card: NodeId = mount_automation_runner_card(state, ai_tick, tokens);
let main: NodeId = view! {
column {
style: {
width: Length::Percent(100.0)
gap: SPACE_MD
}
{ anomaly_card }
{ schema_card }
}
};
let rail: NodeId = view! {
column {
style: {
width: Length::Percent(100.0)
gap: SPACE_MD
}
{ bridge_card }
{ runner_card }
}
};
let studio: NodeId = mount_studio_layout(main, rail);
view! {
EuvPage {
title: AUTOMATION_TITLE.to_string()
subtitle: AUTOMATION_SUBTITLE.to_string()
tokens: tokens
{ studio }
}
}
}