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 box_size: Size = ctx.get_viewport_size().get_untracked();
let breakpoint_tier: 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 rect_bounds: 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(),
rect_bounds.get_x(),
rect_bounds.get_y(),
rect_bounds.get_width(),
rect_bounds.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\": \"{breakpoint_tier:?}\", \"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(),
box_size.get_width(),
box_size.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 box_size: Size = ctx.get_viewport_size().get_untracked();
let vw: f32 = box_size.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 rect_bounds: Rect = node.get_layout_box().get_bounds();
if !clipped_by_scroll
&& rect_bounds.get_width() > 0.0
&& rect_bounds.right() > vw + 1.5
{
findings.push(format!(
"HorizontalOverflow node#{idx}: right={:.1} > vw={:.1}",
rect_bounds.right(),
vw
));
}
if let Some(a11y) = node.a11y_props_ref()
&& a11y.get_role() == A11yRole::Button
&& (rect_bounds.get_width() <= 0.0 || rect_bounds.get_height() <= 0.0)
{
findings.push(format!(
"ZeroSizedInteractive node#{idx} ({}): {:.0}x{:.0}",
a11y.get_label(),
rect_bounds.get_width(),
rect_bounds.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(blend_factor)
if blend_factor.to_lowercase().contains(trimmed.as_str()) =>
{
Some(blend_factor.clone())
}
_ => None,
};
let a11y_hit: Option<String> = node
.a11y_props_ref()
.filter(|alpha_val: &&A11yProps| {
alpha_val
.get_label()
.to_lowercase()
.contains(trimmed.as_str())
})
.map(|first_val: &A11yProps| {
format!("{:?}:{}", first_val.get_role(), first_val.get_label())
});
if let Some(hit_label) = a11y_hit.or(text_hit) {
count += 1;
if first_desc.is_none() {
let rect_bounds: Rect = node.get_layout_box().get_bounds();
first_desc = Some(format!(
"node#{idx} `{hit_label}` @ ({:.0},{:.0} {:.0}x{:.0})",
rect_bounds.get_x(),
rect_bounds.get_y(),
rect_bounds.get_width(),
rect_bounds.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 palette: ThemePalette = tokens.get_palette();
let geometry: 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 = {
if let Some(mut updated_value) = ai_tick.try_get_untracked() {
{
let item_val: &mut u32 = &mut updated_value;
*item_val = item_val.wrapping_add(1);
}
ai_tick.set(updated_value)
} else {
false
}
};
};
let euv_card_style: Style = style! {
TextSelectMode::SelectAndCopy
};
let column_eighth_style: Style = style! {
Width(Length::Percent(100.0))
Gap(SPACE_SM)
};
let text_ninth_style: Style = style! {
Width(Length::Percent(100.0))
FontSize(12.5)
FontWeight::SemiBold
TextColor(palette.get_primary())
};
let column_ninth_style: Style = style! {
Width(Length::Percent(100.0))
Padding(Edges::all(SPACE_MD))
Background(palette.get_muted())
Border::new(geometry.get_border_width(), palette.get_border())
Radius(geometry.get_radius_md())
Overflow::Hidden
};
let text_tenth_style: Style = style! {
Width(Length::Percent(100.0))
FontSize(11.5)
FontFamily::Monospace
TextColor(palette.get_foreground())
};
view! {
EuvCard {
title: AUTOMATION_SCHEMA_CARD_TITLE.to_string()
tokens: tokens
subtitle: AUTOMATION_SCHEMA_CARD_SUB
style: euv_card_style
column {
style: column_eighth_style
text {
style: text_ninth_style
{ telemetry_line }
}
EuvActionRow {
EuvButton {
label: AUTOMATION_BTN_REFRESH_SCHEMA.to_string()
tokens: tokens
small()
on_click: on_refresh
}
}
column {
style: column_ninth_style
text {
style: text_tenth_style
{ schema_json_text }
}
}
}
}
}
}
pub(crate) fn mount_automation_anomaly_card(tokens: ThemeTokens) -> NodeId {
let palette: ThemePalette = tokens.get_palette();
let geometry: 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 box_size: 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})",
box_size.get_width(),
box_size.get_height()
)
} else {
format!(
"Run #{run_no}: {} anomaly(s) across {checked} nodes @ {:.0}x{:.0}",
findings.len(),
box_size.get_width(),
box_size.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![{
let text_eighth_style: Style = style! {
Width(Length::Percent(100.0))
FontSize(12.0)
FontFamily::Monospace
TextColor(live_p.get_foreground())
};
view! {
text {
style: text_eighth_style
{ label }
}
}
}];
}
items
.into_iter()
.map(|item: String| {
let text_seventh_style: Style = style! {
Width(Length::Percent(100.0))
FontSize(12.0)
FontFamily::Monospace
TextColor(live_p.get_foreground())
};
view! {
text {
style: text_seventh_style
{ item }
}
}
})
.collect()
};
let column_sixth_style: Style = style! {
Width(Length::Percent(100.0))
Gap(SPACE_SM)
};
let text_sixth_style: Style = style! {
Width(Length::Percent(100.0))
FontSize(13.0)
FontWeight::SemiBold
TextColor(palette.get_card_foreground())
};
let column_seventh_style: Style = style! {
Width(Length::Percent(100.0))
Padding(Edges::all(SPACE_SM))
Gap(SPACE_XS)
Background(palette.get_muted())
Border::new(geometry.get_border_width(), palette.get_border())
Radius(geometry.get_radius_md())
Overflow::Hidden
};
view! {
EuvCard {
title: AUTOMATION_ANOMALY_CARD_TITLE.to_string()
tokens: tokens
subtitle: AUTOMATION_ANOMALY_CARD_SUB
column {
style: column_sixth_style
text {
style: text_sixth_style
{ summary_text }
}
EuvActionRow {
EuvButton {
label: AUTOMATION_BTN_RUN_ANOMALY.to_string()
tokens: tokens
small()
on_click: on_run_check
}
}
column {
style: column_seventh_style
bind_children: build_findings_rows
}
}
}
}
}
pub(crate) fn mount_automation_bridge_card(tokens: ThemeTokens) -> NodeId {
let palette: 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()
);
let column_fifth_style: Style = style! {
Width(Length::Percent(100.0))
Gap(SPACE_SM)
};
let row_style: Style = style! {
Width(Length::Percent(100.0))
FlexWrap::Wrap
AlignItems::Center
Gap(SPACE_SM)
};
let text_fifth_style: Style = style! {
Width(Length::Percent(100.0))
FontSize(12.0)
FontFamily::Monospace
TextColor(palette.get_muted_foreground())
};
view! {
EuvCard {
title: AUTOMATION_BRIDGE_CARD_TITLE.to_string()
tokens: tokens
subtitle: AUTOMATION_BRIDGE_CARD_SUB
column {
style: column_fifth_style
row {
style: row_style
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: text_fifth_style
{ 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 palette: ThemePalette = tokens.get_palette();
let geometry: 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 = {
if let Some(mut updated_value) = action_log.try_get_untracked() {
{
let items: &mut Vec<String> = &mut updated_value;
items.insert(0, entry);
if items.len() > AUTOMATION_LOG_LIMIT {
items.truncate(AUTOMATION_LOG_LIMIT);
}
}
action_log.set(updated_value)
} else {
false
}
};
};
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 text_value: String = query_sig.get_untracked();
let res: String = query_automation_scene_nodes(&ctx_query, &text_value);
let _: bool = query_result.set(res.clone());
push_trace(format!("Query(`{text_value}`) -> {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 = {
if let Some(mut updated_value) = ai_tick.try_get_untracked() {
{
let item_val: &mut u32 = &mut updated_value;
*item_val = item_val.wrapping_add(1);
}
ai_tick.set(updated_value)
} else {
false
}
};
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 = {
if let Some(mut updated_value) = ai_tick.try_get_untracked() {
{
let tex_v: &mut u32 = &mut updated_value;
*tex_v = tex_v.wrapping_add(1);
}
ai_tick.set(updated_value)
} else {
false
}
};
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![{
let text_quaternary_style: Style = style! {
Width(Length::Percent(100.0))
FontSize(12.0)
FontFamily::Monospace
TextColor(live_p.get_muted_foreground())
};
view! {
text {
style: text_quaternary_style
{ AUTOMATION_LOG_EMPTY }
}
}
}];
}
items
.into_iter()
.map(|entry: String| {
let text_tertiary_style: Style = style! {
Width(Length::Percent(100.0))
FontSize(11.5)
FontFamily::Monospace
TextColor(live_p.get_foreground())
};
view! {
text {
style: text_tertiary_style
{ entry }
}
}
})
.collect()
};
let column_tertiary_style: Style = style! {
Width(Length::Percent(100.0))
Gap(SPACE_SM)
};
let text_style: Style = style! {
Width(Length::Percent(100.0))
FontSize(12.0)
FontWeight::Medium
TextColor(palette.get_primary())
};
let text_secondary_style: Style = style! {
Width(Length::Percent(100.0))
FontSize(12.0)
FontFamily::Monospace
TextColor(palette.get_muted_foreground())
};
let column_quaternary_style: Style = style! {
Width(Length::Percent(100.0))
Padding(Edges::all(SPACE_SM))
Gap(SPACE_XS)
Background(palette.get_muted())
Border::new(geometry.get_border_width(), palette.get_border())
Radius(geometry.get_radius_md())
Overflow::Hidden
};
view! {
EuvCard {
title: AUTOMATION_RUNNER_CARD_TITLE.to_string()
tokens: tokens
subtitle: AUTOMATION_RUNNER_CARD_SUB
column {
style: column_tertiary_style
EuvInput {
value: query_sig
placeholder: AUTOMATION_QUERY_PLACEHOLDER.to_string()
tokens: tokens
}
EuvActionRow {
EuvButton {
label: AUTOMATION_BTN_QUERY_NODES.to_string()
tokens: tokens
small()
on_click: on_query
}
EuvButton {
label: AUTOMATION_BTN_SANDBOX_CLICK.to_string()
tokens: tokens
outline()
small()
on_click: on_sandbox_click
}
EuvButton {
label: AUTOMATION_BTN_THEME_ACTION.to_string()
tokens: tokens
outline()
small()
on_click: on_toggle_theme
}
EuvButton {
label: AUTOMATION_BTN_CLEAR_LOG.to_string()
tokens: tokens
ghost()
small()
on_click: on_clear_log
}
}
text {
style: text_style
{ query_result_text }
}
text {
style: text_secondary_style
{ sandbox_status_text }
}
column {
style: column_quaternary_style
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 column_secondary_style: Style = style! {
Width(Length::Percent(100.0))
Gap(SPACE_MD)
};
let main: NodeId = view! {
column {
style: column_secondary_style
{ anomaly_card }
{ schema_card }
}
};
let column_style: Style = style! {
Width(Length::Percent(100.0))
Gap(SPACE_MD)
};
let rail: NodeId = view! {
column {
style: column_style
{ 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 }
}
}
}