use arc_flow_core::{Node, NodeStatus};
use arc_tokens::{ArcColors, Radii, ThemeMode, Typography};
use gpui_kit::{
AnyElement, App, Hsla, IntoElement, ParentElement, Styled, Window, div,
prelude::FluentBuilder as _, px,
};
use crate::metrics::{NODE_PADDING, RUNNING_PULSE_MIN_ALPHA, STATUS_DOT};
pub struct NodeRenderCtx<'a> {
pub zoom: f32,
pub selected: bool,
pub colors: &'a ArcColors,
pub mode: ThemeMode,
pub pulse: f32,
pub interacting: bool,
}
impl NodeRenderCtx<'_> {
pub fn s(&self, v: f32) -> gpui_kit::Pixels {
px(v * self.zoom)
}
pub fn status_color(&self, status: NodeStatus) -> Hsla {
let c = self.colors;
match status {
NodeStatus::Idle => c.text_muted,
NodeStatus::Queued => c.warning,
NodeStatus::Running => c.accent,
NodeStatus::Done => c.success,
NodeStatus::Failed => c.danger,
NodeStatus::Skipped => c.border_strong,
}
}
}
pub trait NodeRenderer: 'static {
fn render(
&self,
node: &Node,
ctx: &NodeRenderCtx,
window: &mut Window,
cx: &mut App,
) -> AnyElement;
}
#[derive(Default)]
pub struct DefaultNodeRenderer;
pub fn status_text(status: NodeStatus) -> &'static str {
match status {
NodeStatus::Idle => "Idle",
NodeStatus::Queued => "Queued",
NodeStatus::Running => "Running",
NodeStatus::Done => "Done",
NodeStatus::Failed => "Failed",
NodeStatus::Skipped => "Skipped",
}
}
impl NodeRenderer for DefaultNodeRenderer {
fn render(&self, node: &Node, ctx: &NodeRenderCtx, _: &mut Window, _: &mut App) -> AnyElement {
let c = ctx.colors;
let radius = px(f32::from(Radii::CONTROL) * ctx.zoom);
let running = node.status == NodeStatus::Running;
let border = if ctx.selected {
c.accent
} else if running {
let alpha = RUNNING_PULSE_MIN_ALPHA + (1.0 - RUNNING_PULSE_MIN_ALPHA) * ctx.pulse;
c.accent.opacity(alpha)
} else {
c.border
};
div()
.size_full()
.flex()
.flex_col()
.justify_center()
.gap(ctx.s(4.0))
.px(ctx.s(NODE_PADDING))
.rounded(radius)
.bg(c.surface_raised)
.border(px(if ctx.selected || running { 2.0 } else { 1.0 }))
.border_color(border)
.when(!ctx.interacting, |d| {
d.shadow(arc_tokens::Shadows::resting(ctx.mode))
})
.overflow_hidden()
.child(
div()
.text_size(px(f32::from(Typography::TEXT_SM) * ctx.zoom))
.font_weight(gpui_kit::FontWeight::SEMIBOLD)
.text_color(c.foreground)
.whitespace_nowrap()
.child(node.label.to_string()),
)
.child(
div()
.flex()
.items_center()
.gap(ctx.s(6.0))
.child(
div()
.size(ctx.s(STATUS_DOT))
.rounded_full()
.bg(ctx.status_color(node.status)),
)
.child(
div()
.text_size(px(f32::from(Typography::TEXT_XS) * ctx.zoom))
.text_color(c.text_muted)
.child(status_text(node.status)),
),
)
.into_any_element()
}
}