1mod erased;
5mod find;
6
7use gpui_kit::{
8 AnyElement, AnyView, App, AppContext, Entity, FocusHandle, SharedString, Subscription, Window,
9};
10
11use crate::workspace::{SpaceDock, SpaceDocks};
12use crate::{
13 AgentContext, Dashboard, DashboardEvent, KeyHelp, Modal, PaletteCommand, ReturnLocation,
14 ReturnPoint, Row, Search,
15};
16
17use erased::AnyDashboard;
18
19type FindView = Entity<crate::find_in_page::FindInPage>;
20
21pub type EventSink = Box<dyn Fn(&DashboardEvent, &mut Window, &mut App)>;
23
24pub struct DashboardHandle {
26 pub id: &'static str,
27 pub title: SharedString,
28 pub description: SharedString,
29 pub icon: Option<&'static str>,
30 pub help: &'static str,
32 pub help_images: &'static [crate::HelpImage],
33 pub searchable: bool,
34 inner: Box<dyn AnyDashboard>,
35 find: std::cell::OnceCell<FindView>,
36}
37
38#[derive(Default)]
40struct BoundKeys(std::collections::HashSet<std::any::TypeId>);
41
42impl gpui_kit::Global for BoundKeys {}
43
44fn first_creation<D: Dashboard>(cx: &mut App) -> bool {
46 cx.default_global::<BoundKeys>()
47 .0
48 .insert(std::any::TypeId::of::<D>())
49}
50
51impl DashboardHandle {
52 pub fn open_notification(&self, target: u64, window: &mut Window, cx: &mut App) {
54 self.inner.open_notification(target, window, cx);
55 }
56 pub fn new<D: Dashboard>(window: &mut Window, cx: &mut App) -> Self {
59 first_creation::<D>(cx).then(|| D::bind_keys(cx));
60 let entity = cx.new(|cx| D::new(window, cx));
61 Self::from_entity(entity)
62 }
63
64 pub fn from_entity<D: Dashboard>(entity: Entity<D>) -> Self {
68 Self {
69 id: D::ID,
70 title: D::TITLE.into(),
71 description: D::DESCRIPTION.into(),
72 icon: D::ICON,
73 help: D::HELP_MARKDOWN,
74 help_images: D::HELP_IMAGES,
75 searchable: D::SEARCHABLE,
76 inner: Box::new(entity),
77 find: std::cell::OnceCell::new(),
78 }
79 }
80
81 pub fn view(&self) -> AnyView {
82 self.inner.view()
83 }
84
85 pub fn focus_handle(&self, cx: &App) -> FocusHandle {
86 self.inner.focus_handle(cx)
87 }
88
89 pub fn activate(&self, window: &mut Window, cx: &mut App) {
90 self.inner.activate(window, cx);
91 }
92
93 pub fn deactivate(&self, window: &mut Window, cx: &mut App) {
94 self.inner.deactivate(window, cx);
95 }
96
97 pub fn covered(&self, covered: bool, window: &mut Window, cx: &mut App) {
99 self.inner.covered(covered, window, cx);
100 }
101
102 pub fn window_title(&self, cx: &App) -> Option<SharedString> {
104 self.inner.window_title(cx)
105 }
106
107 pub fn diagnostic_config_path(&self, cx: &App) -> Option<std::path::PathBuf> {
109 self.inner.diagnostic_config_path(cx)
110 }
111
112 pub fn background_refresh(&self, cx: &mut App) {
113 self.inner.background_refresh(cx);
114 }
115
116 pub fn render_toolbar(&self, window: &mut Window, cx: &mut App) -> Option<AnyElement> {
118 self.inner.render_toolbar(window, cx)
119 }
120
121 pub fn status(&self, cx: &App) -> Option<SharedString> {
122 self.inner.status(cx)
123 }
124
125 pub fn docks(&self, cx: &App) -> SpaceDocks {
127 self.inner.docks(cx)
128 }
129
130 pub fn toggle_dock(&self, dock: SpaceDock, window: &mut Window, cx: &mut App) {
132 self.inner.toggle_dock(dock, window, cx);
133 }
134
135 pub fn badge(&self, cx: &App) -> Option<SharedString> {
138 self.inner.badge(cx)
139 }
140
141 pub fn busy(&self, cx: &App) -> bool {
142 self.inner.busy(cx)
143 }
144
145 pub fn subject(&self, cx: &App) -> Option<Row> {
146 self.inner.subject(cx)
147 }
148
149 pub fn agent_context(&self, cx: &App) -> Option<AgentContext> {
151 self.inner.agent_context(cx)
152 }
153
154 pub fn commands(&self, cx: &mut App) -> Vec<PaletteCommand> {
155 self.inner.commands(cx)
156 }
157
158 pub fn search(&self, cx: &App) -> Option<Search> {
159 self.inner.search(cx)
160 }
161
162 pub fn open_search_result(&self, id: &str, window: &mut Window, cx: &mut App) {
164 self.inner.open_search_result(id, window, cx);
165 }
166
167 pub fn open_link(&self, url: &str, cx: &mut App) -> bool {
168 self.inner.open_link(url, cx)
169 }
170
171 pub fn return_location(&self, cx: &App) -> Option<ReturnLocation> {
173 self.inner.return_location(cx)
174 }
175
176 pub fn arrived_from(&self, from: Option<ReturnPoint>, cx: &mut App) {
178 self.inner.arrived_from(from, cx);
179 }
180
181 pub fn return_to(&self, key: &str, window: &mut Window, cx: &mut App) -> bool {
183 self.inner.return_to(key, window, cx)
184 }
185
186 pub fn open_indexed_result_fenced(
187 &self,
188 hit: &subspace_search::Hit,
189 permit: subspace_search::ReadPermit,
190 window: &mut Window,
191 cx: &mut App,
192 ) {
193 self.inner
194 .open_indexed_result_fenced(hit, permit, window, cx);
195 }
196
197 pub fn modal(&self, cx: &App) -> Option<Modal> {
198 self.inner.modal(cx)
199 }
200
201 pub fn dismiss(&self, window: &mut Window, cx: &mut App) {
202 self.inner.dismiss(window, cx);
203 }
204
205 pub fn keys(&self, cx: &App) -> &'static [KeyHelp] {
206 self.inner.keys(cx)
207 }
208
209 pub fn help_foot(&self, cx: &App) -> Option<SharedString> {
211 self.inner.help_foot(cx)
212 }
213
214 pub fn quit_warnings(&self, cx: &App) -> Vec<SharedString> {
215 self.inner.quit_warnings(cx)
216 }
217
218 pub fn reload(&self, window: &mut Window, cx: &mut App) {
220 self.inner.reload(window, cx);
221 }
222
223 pub fn subscribe(&self, window: &mut Window, cx: &mut App, sink: EventSink) -> Subscription {
226 self.inner.subscribe(window, cx, sink)
227 }
228}
229
230#[cfg(test)]
231mod tests {
232 use gpui_kit::{
233 App, Context, EventEmitter, FocusHandle, Focusable, Global, IntoElement, Render,
234 TestAppContext, Window, div,
235 };
236
237 use super::DashboardHandle;
238 use crate::workspace::{SpaceDock, SpaceDocks};
239 use crate::{Dashboard, DashboardEvent};
240
241 #[derive(Default)]
242 struct Binds(usize);
243 impl Global for Binds {}
244
245 struct Board {
246 focus: FocusHandle,
247 source: Option<std::path::PathBuf>,
248 navigator: bool,
249 }
250 impl EventEmitter<DashboardEvent> for Board {}
251 impl Focusable for Board {
252 fn focus_handle(&self, _: &App) -> FocusHandle {
253 self.focus.clone()
254 }
255 }
256 impl Render for Board {
257 fn render(&mut self, _: &mut Window, _: &mut Context<Self>) -> impl IntoElement {
258 div()
259 }
260 }
261 impl Dashboard for Board {
262 const ID: &'static str = "board";
263 const TITLE: &'static str = "Board";
264 fn new(_: &mut Window, cx: &mut Context<Self>) -> Self {
265 Self {
266 focus: cx.focus_handle(),
267 source: None,
268 navigator: true,
269 }
270 }
271 fn bind_keys(cx: &mut App) {
272 cx.default_global::<Binds>().0 += 1;
273 }
274 fn diagnostic_config_path(&self, _: &App) -> Option<std::path::PathBuf> {
275 self.source.clone()
276 }
277 fn docks(&self, _: &App) -> SpaceDocks {
278 SpaceDocks {
279 navigator: Some(self.navigator),
280 inspector: None,
281 }
282 }
283 fn toggle_dock(&mut self, dock: SpaceDock, _: &mut Window, _: &mut Context<Self>) {
284 if dock == SpaceDock::Navigator {
285 self.navigator = !self.navigator;
286 }
287 }
288 fn reload(&mut self, _: &mut Window, _: &mut Context<Self>) {}
289 }
290
291 #[gpui_kit::test]
294 fn the_handle_reports_a_spaces_docks_and_toggles_them(cx: &mut TestAppContext) {
295 let (board, cx) = cx.add_window_view(Board::new);
296 let handle = DashboardHandle::from_entity(board);
297 cx.update(|window, cx| {
298 assert_eq!(
299 handle.docks(cx),
300 SpaceDocks {
301 navigator: Some(true),
302 inspector: None,
303 }
304 );
305 handle.toggle_dock(SpaceDock::Navigator, window, cx);
306 assert_eq!(handle.docks(cx).navigator, Some(false));
307 handle.toggle_dock(SpaceDock::Inspector, window, cx);
308 assert_eq!(handle.docks(cx).inspector, None);
309 });
310 }
311
312 #[gpui_kit::test]
313 fn creating_a_board_again_binds_its_keys_once(cx: &mut TestAppContext) {
314 let window = cx.add_window(Board::new);
315 window
316 .update(cx, |_, window, cx| {
317 let first = DashboardHandle::new::<Board>(window, cx);
318 let second = DashboardHandle::new::<Board>(window, cx);
319 assert_ne!(first.focus_handle(cx), second.focus_handle(cx));
320 assert_eq!(cx.global::<Binds>().0, 1);
321 })
322 .unwrap();
323 }
324
325 #[gpui_kit::test]
326 fn diagnostic_path_capability_reads_the_current_board_without_loading_a_file(
327 cx: &mut TestAppContext,
328 ) {
329 let (board, cx) = cx.add_window_view(Board::new);
330 let handle = DashboardHandle::from_entity(board.clone());
331 cx.update(|_, cx| {
332 assert_eq!(handle.diagnostic_config_path(cx), None);
333 board.update(cx, |board, _| {
334 board.source = Some("/fixture/config/active.json".into());
335 });
336 assert_eq!(
337 handle.diagnostic_config_path(cx),
338 Some(std::path::PathBuf::from("/fixture/config/active.json")),
339 );
340 });
341 }
342}