arc-flow 0.4.0

GPUI node & workflow editor canvas built on arc-flow-core, arc-tokens, arc-motion, and arc-ui.
Documentation
//! 节点渲染器:按 `Node::kind` 路由;未注册的类型使用 [`DefaultNodeRenderer`]。

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};

/// 渲染时传给节点的上下文。节点外框(位置、尺寸、ID、指针拦截)由画布提供,
/// 渲染器只负责填满外框的内容。
pub struct NodeRenderCtx<'a> {
    pub zoom: f32,
    pub selected: bool,
    pub colors: &'a ArcColors,
    pub mode: ThemeMode,
    /// 执行高亮的呼吸进度 0..=1(F6):Running 节点用它调制边框;reduced motion 下恒为 1。
    pub pulse: f32,
    /// 正在拖动节点(F8 拖拽降级):渲染器可省掉阴影等昂贵效果。
    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()
    }
}