Skip to main content

arc_flow/
renderer.rs

1//! 节点渲染器:按 `Node::kind` 路由;未注册的类型使用 [`DefaultNodeRenderer`]。
2
3use arc_flow_core::{Node, NodeStatus};
4use arc_tokens::{ArcColors, Radii, ThemeMode, Typography};
5use gpui_kit::{
6    AnyElement, App, Hsla, IntoElement, ParentElement, Styled, Window, div,
7    prelude::FluentBuilder as _, px,
8};
9
10use crate::metrics::{NODE_PADDING, RUNNING_PULSE_MIN_ALPHA, STATUS_DOT};
11
12/// 渲染时传给节点的上下文。节点外框(位置、尺寸、ID、指针拦截)由画布提供,
13/// 渲染器只负责填满外框的内容。
14pub struct NodeRenderCtx<'a> {
15    pub zoom: f32,
16    pub selected: bool,
17    pub colors: &'a ArcColors,
18    pub mode: ThemeMode,
19    /// 执行高亮的呼吸进度 0..=1(F6):Running 节点用它调制边框;reduced motion 下恒为 1。
20    pub pulse: f32,
21    /// 正在拖动节点(F8 拖拽降级):渲染器可省掉阴影等昂贵效果。
22    pub interacting: bool,
23}
24
25impl NodeRenderCtx<'_> {
26    /// 世界尺寸 → 屏幕像素。
27    pub fn s(&self, v: f32) -> gpui_kit::Pixels {
28        px(v * self.zoom)
29    }
30
31    pub fn status_color(&self, status: NodeStatus) -> Hsla {
32        let c = self.colors;
33        match status {
34            NodeStatus::Idle => c.text_muted,
35            NodeStatus::Queued => c.warning,
36            NodeStatus::Running => c.accent,
37            NodeStatus::Done => c.success,
38            NodeStatus::Failed => c.danger,
39            NodeStatus::Skipped => c.border_strong,
40        }
41    }
42}
43
44pub trait NodeRenderer: 'static {
45    fn render(
46        &self,
47        node: &Node,
48        ctx: &NodeRenderCtx,
49        window: &mut Window,
50        cx: &mut App,
51    ) -> AnyElement;
52}
53
54/// 默认卡片:标题 + 状态点与状态文字。
55#[derive(Default)]
56pub struct DefaultNodeRenderer;
57
58pub fn status_text(status: NodeStatus) -> &'static str {
59    match status {
60        NodeStatus::Idle => "Idle",
61        NodeStatus::Queued => "Queued",
62        NodeStatus::Running => "Running",
63        NodeStatus::Done => "Done",
64        NodeStatus::Failed => "Failed",
65        NodeStatus::Skipped => "Skipped",
66    }
67}
68
69impl NodeRenderer for DefaultNodeRenderer {
70    fn render(&self, node: &Node, ctx: &NodeRenderCtx, _: &mut Window, _: &mut App) -> AnyElement {
71        let c = ctx.colors;
72        let radius = px(f32::from(Radii::CONTROL) * ctx.zoom);
73        let running = node.status == NodeStatus::Running;
74        let border = if ctx.selected {
75            c.accent
76        } else if running {
77            let alpha = RUNNING_PULSE_MIN_ALPHA + (1.0 - RUNNING_PULSE_MIN_ALPHA) * ctx.pulse;
78            c.accent.opacity(alpha)
79        } else {
80            c.border
81        };
82        div()
83            .size_full()
84            .flex()
85            .flex_col()
86            .justify_center()
87            .gap(ctx.s(4.0))
88            .px(ctx.s(NODE_PADDING))
89            .rounded(radius)
90            .bg(c.surface_raised)
91            .border(px(if ctx.selected || running { 2.0 } else { 1.0 }))
92            .border_color(border)
93            .when(!ctx.interacting, |d| {
94                d.shadow(arc_tokens::Shadows::resting(ctx.mode))
95            })
96            .overflow_hidden()
97            .child(
98                div()
99                    .text_size(px(f32::from(Typography::TEXT_SM) * ctx.zoom))
100                    .font_weight(gpui_kit::FontWeight::SEMIBOLD)
101                    .text_color(c.foreground)
102                    .whitespace_nowrap()
103                    .child(node.label.to_string()),
104            )
105            .child(
106                div()
107                    .flex()
108                    .items_center()
109                    .gap(ctx.s(6.0))
110                    .child(
111                        div()
112                            .size(ctx.s(STATUS_DOT))
113                            .rounded_full()
114                            .bg(ctx.status_color(node.status)),
115                    )
116                    .child(
117                        div()
118                            .text_size(px(f32::from(Typography::TEXT_XS) * ctx.zoom))
119                            .text_color(c.text_muted)
120                            .child(status_text(node.status)),
121                    ),
122            )
123            .into_any_element()
124    }
125}