subspace_chat/agent_chat/
canvas.rs1mod actions;
3mod content;
4mod controls;
5#[cfg(test)]
6mod runtime_tests;
7mod table;
8use super::{ChatPanel, Context, Tokens, ToolCall};
9use crate::action::{Intent, action_button};
10use gpui_kit::component::Selectable as _;
11use gpui_kit::component::{h_flex, v_flex};
12use gpui_kit::prelude::FluentBuilder as _;
13use gpui_kit::{
14 AnyElement, AppContext as _, Div, DragMoveEvent, EmptyView, InteractiveElement as _,
15 IntoElement, KeyDownEvent, ParentElement as _, SharedString, StatefulInteractiveElement as _,
16 Styled as _, TestSupportExt as _, Window, div, px, relative,
17};
18use std::sync::Arc;
19use subspace_agent::canvas::{Canvas, CanvasKind};
20use subspace_lucide::IconName;
21
22struct Resize;
23const MIN_BOARD: f32 = 240.;
24const MIN_CANVAS: f32 = 280.;
25const HANDLE_OFFSET: f32 = -3.;
26const HANDLE_WIDTH: f32 = 6.;
27const THUMBNAIL_WIDTH: f32 = 144.;
28const THUMBNAIL_HEIGHT: f32 = 80.;
29
30impl ChatPanel {
31 pub fn canvas_gallery(&self) -> bool {
32 self.tab().canvases.gallery && !self.tab().canvases.hidden && !self.sessions_open
33 }
34
35 pub fn render_canvas(&self, window: &mut Window, cx: &mut Context<Self>) -> Option<AnyElement> {
36 let list = &self.tab().canvases;
37 let canvas = shown_canvas(list, self.sessions_open)?.clone();
38 let tokens = Tokens::current(cx);
39 let width = dock_width(
40 self.canvas_width,
41 self.width,
42 f32::from(window.viewport_size().width),
43 );
44 Some(
45 v_flex()
46 .id("chat-canvas-area")
47 .test_support()
48 .track_focus(&self.canvas_focus)
49 .relative()
50 .h_full()
51 .min_h_0()
52 .min_w_0()
53 .flex_shrink_0()
54 .bg(tokens.pane)
55 .border_l_1()
56 .border_color(tokens.line)
57 .when(list.gallery, |frame| frame.flex_1().min_w_0())
58 .when(!list.gallery, |frame| {
59 frame
60 .w(px(width))
61 .max_w(relative(self.pane_width_fraction()))
62 })
63 .on_key_down(cx.listener(Self::canvas_key))
64 .on_drag_move::<Resize>(cx.listener(
65 |chat, event: &DragMoveEvent<Resize>, _, cx| {
66 chat.canvas_width =
67 f32::from(event.bounds.right() - event.event.position.x)
68 .max(MIN_CANVAS);
69 cx.notify();
70 },
71 ))
72 .child(self.canvas_header(&canvas, &tokens, cx))
73 .child(self.canvas_content(&canvas, width, cx))
74 .child(self.canvas_caption(&canvas, &tokens, cx))
75 .when(list.gallery, |frame| {
76 frame.child(self.canvas_filmstrip(&tokens, cx))
77 })
78 .when(!list.gallery, |frame| {
79 frame.child(
80 div()
81 .id("canvas-resize")
82 .absolute()
83 .left(px(HANDLE_OFFSET))
84 .top_0()
85 .bottom_0()
86 .w(px(HANDLE_WIDTH))
87 .cursor_col_resize()
88 .on_drag(Resize, |_, _, _, cx| cx.new(|_| EmptyView)),
89 )
90 })
91 .into_any_element(),
92 )
93 }
94
95 fn canvas_caption(&self, canvas: &Canvas, tokens: &Tokens, cx: &mut Context<Self>) -> Div {
96 h_flex()
97 .whitespace_nowrap()
98 .p_1()
99 .gap_1()
100 .border_t_1()
101 .border_color(tokens.line)
102 .text_color(tokens.muted)
103 .child(
104 div()
105 .flex_1()
106 .min_w_0()
107 .text_ellipsis()
108 .child(canvas.caption.clone()),
109 )
110 .child(format!("Copilot · {}", canvas.opened_at))
111 .child(
112 action_button(
113 "canvas-data",
114 Intent::Neutral,
115 Some(IconName::Table),
116 if canvas.kind == CanvasKind::Chart {
117 "Data"
118 } else {
119 "Source"
120 },
121 )
122 .selected(self.tab().canvases.data)
123 .on_click(cx.listener(|chat, _, _, cx| {
124 let list = &mut chat.tab_mut().canvases;
125 list.data = !list.data;
126 cx.notify();
127 })),
128 )
129 }
130
131 fn canvas_filmstrip(&self, tokens: &Tokens, cx: &mut Context<Self>) -> gpui_kit::Stateful<Div> {
132 h_flex()
133 .id("canvas-filmstrip")
134 .gap_1()
135 .p_1()
136 .overflow_x_scroll()
137 .border_t_1()
138 .border_color(tokens.line)
139 .children(
140 self.tab()
141 .canvases
142 .items
143 .iter()
144 .enumerate()
145 .map(|(index, canvas)| {
146 v_flex()
147 .id(("canvas-film", index))
148 .role(gpui_kit::accesskit::Role::Button)
149 .aria_label(format!("Show {}: {}", canvas.kind.label(), canvas.title))
150 .w(px(THUMBNAIL_WIDTH))
151 .flex_shrink_0()
152 .overflow_hidden()
153 .p_1()
154 .border_1()
155 .border_color(tokens.line)
156 .when(index == self.tab().canvases.selected, |preview| {
157 preview.border_color(tokens.accent)
158 })
159 .child(
160 div()
161 .w_full()
162 .h(px(THUMBNAIL_HEIGHT))
163 .overflow_hidden()
164 .child(Self::canvas_thumbnail(canvas, cx)),
165 )
166 .child(
167 h_flex()
168 .gap_1()
169 .child(kind_icon(canvas.kind))
170 .child(div().text_ellipsis().child(canvas.title.clone())),
171 )
172 .on_click(cx.listener(move |chat, _, _, cx| {
173 chat.tab_mut().canvases.selected = index;
174 chat.tab_mut().canvases.data = false;
175 cx.notify();
176 }))
177 }),
178 )
179 }
180
181 pub(super) fn toggle_canvas_gallery(&mut self, window: &mut Window, cx: &mut Context<Self>) {
182 if self.tab().canvases.current().is_none() {
183 return;
184 }
185 let list = &mut self.tab_mut().canvases;
186 list.gallery = !list.gallery;
187 list.hidden = false;
188 if list.gallery {
189 self.canvas_focus.focus(window, cx);
190 } else {
191 self.input.update(cx, |input, cx| input.focus(window, cx));
192 }
193 cx.notify();
194 }
195
196 pub(super) fn canvas_key(
197 &mut self,
198 event: &KeyDownEvent,
199 window: &mut Window,
200 cx: &mut Context<Self>,
201 ) {
202 let key = &event.keystroke;
203 if (key.modifiers.platform || key.modifiers.control) && key.modifiers.alt && key.key == "g"
204 {
205 self.toggle_canvas_gallery(window, cx);
206 cx.stop_propagation();
207 return;
208 }
209 if !self.tab().canvases.gallery {
210 return;
211 }
212 if key.key == "escape" {
213 self.toggle_canvas_gallery(window, cx);
214 cx.stop_propagation();
215 } else if self.canvas_focus.is_focused(window)
216 && matches!(key.key.as_str(), "left" | "right")
217 {
218 self.tab_mut()
219 .canvases
220 .step(if key.key == "left" { -1 } else { 1 });
221 cx.notify();
222 cx.stop_propagation();
223 }
224 }
225
226 pub(super) fn canvas_tool_card(
227 &self,
228 call: &ToolCall,
229 tokens: &Tokens,
230 cx: &mut Context<Self>,
231 ) -> Option<Div> {
232 let id = call.detail.strip_prefix("canvasInstanceId=")?;
233 let canvas = self.tab().canvas_history.get(id)?.clone();
234 Some(self.canvas_card(canvas, tokens, cx))
235 }
236
237 fn canvas_card(&self, canvas: Arc<Canvas>, tokens: &Tokens, cx: &mut Context<Self>) -> Div {
238 let selected = self
239 .tab()
240 .canvases
241 .current()
242 .is_some_and(|item| item.id == canvas.id);
243 h_flex()
244 .p_2()
245 .gap_2()
246 .border_1()
247 .border_color(if selected { tokens.accent } else { tokens.line })
248 .child(kind_icon(canvas.kind))
249 .child(div().flex_1().min_w_0().text_ellipsis().child(format!(
250 "{} · {}",
251 canvas.kind.label(),
252 canvas.title
253 )))
254 .child(
255 action_button(
256 SharedString::from(format!("show-canvas:{}", canvas.id)),
257 Intent::Neutral,
258 Some(IconName::ExternalLink),
259 "Show in canvas",
260 )
261 .on_click(cx.listener(move |chat, _, _, cx| {
262 chat.send(
263 subspace_agent::Command::Chat {
264 chat: chat.tab().id,
265 command: subspace_agent::ChatCommand::OpenCanvas(canvas.clone()),
266 },
267 cx,
268 );
269 cx.notify();
270 })),
271 )
272 }
273}
274
275fn kind_icon(kind: CanvasKind) -> subspace_lucide::SvgIcon {
276 match kind {
277 CanvasKind::Chart => IconName::ChartColumn,
278 CanvasKind::Diagram => IconName::Network,
279 CanvasKind::Drawing => IconName::Pencil,
280 }
281}
282
283fn dock_width(wanted: f32, chat: f32, viewport: f32) -> f32 {
284 wanted
285 .min((viewport - chat - MIN_BOARD).max(MIN_CANVAS))
286 .max(MIN_CANVAS)
287}
288
289pub(super) fn shown_canvas(
290 list: &subspace_agent::canvas::CanvasList,
291 sessions: bool,
292) -> Option<&Arc<Canvas>> {
293 if list.hidden || sessions {
294 return None;
295 }
296 list.items.get(list.selected)
297}