gpui-kit 0.7.3

GPUI Kit: one dependency for building desktop applications with GPUI, GPUI Base, GPUI Component and default assets.
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
mod common;
use gpui_kit::component::dock::{
    BasePanel, DockArea, DockLayout, DockSkin, Panel, PanelControl, PanelEvent, PanelStyle,
    panel_handle,
};
use gpui_kit::component::input::{Input, InputState};
use gpui_kit::test::{TestAppContextExt, TestSupportExt, TestWindowExt};
use gpui_kit::{
    App, AppContext, Context, Entity, EventEmitter, FocusHandle, Focusable, TestAppContext, Window,
    div, prelude::*, px, size,
};
use std::time::Duration;
struct Document {
    name: &'static str,
    focus: FocusHandle,
    input: Entity<InputState>,
    removed_focus: Option<FocusHandle>,
    visible: bool,
}
impl Document {
    fn new(name: &'static str, window: &mut Window, cx: &mut Context<Self>) -> Self {
        Self {
            name,
            focus: cx.focus_handle(),
            input: cx.new(|cx| InputState::new(window, cx)),
            removed_focus: None,
            visible: true,
        }
    }
}
impl BasePanel for Document {
    fn panel_name(&self) -> &'static str {
        self.name
    }
    fn visible(&self, _: &App) -> bool {
        self.visible
    }
    fn on_removed(&mut self, window: &mut Window, cx: &mut Context<Self>) {
        if let Some(focus) = &self.removed_focus {
            focus.focus(window, cx);
        }
    }
}
impl Panel for Document {
    fn title(&mut self, _: &mut Window, _: &mut Context<Self>) -> impl IntoElement {
        self.name
    }
    fn zoom_control(&self, _: &App) -> Option<PanelControl> {
        Some(PanelControl::Toolbar)
    }
}
impl EventEmitter<PanelEvent> for Document {}
impl Focusable for Document {
    fn focus_handle(&self, _: &App) -> FocusHandle {
        self.focus.clone()
    }
}
impl Render for Document {
    fn render(&mut self, _: &mut Window, _: &mut Context<Self>) -> impl IntoElement {
        div()
            .id(self.name)
            .test_support()
            .track_focus(&self.focus)
            .size_full()
            .child(self.name)
            .child(Input::new(&self.input).id("editor"))
    }
}
struct Editor {
    area: Entity<DockArea>,
}
impl Render for Editor {
    fn render(&mut self, _: &mut Window, _: &mut Context<Self>) -> impl IntoElement {
        div().size_full().child(self.area.clone())
    }
}
#[gpui_kit::test]
async fn dock_switches_and_reorders_real_tabs(cx: &mut TestAppContext) {
    cx.update(gpui_kit::init);
    let (handle, _) = common::open_window(cx, Some(size(px(900.), px(600.))), |window, cx| {
        let (area, skin) = DockSkin::dock_area("editor", None, window, cx);
        skin.set_panel_style(PanelStyle::TabBar, cx);
        let a = cx.new(|cx| Document::new("alpha", window, cx));
        let b = cx.new(|cx| Document::new("beta", window, cx));
        let layout = DockLayout::tabs()
            .panel_view(panel_handle(a), cx)
            .panel_view(panel_handle(b), cx);
        area.update(cx, |area, cx| area.set_center(layout, window, cx));
        let editor = cx.new(|_| Editor { area });
        editor
    });
    cx.update_window(handle.into(), |_, window, cx| {
        window.render_frame(cx);
        assert!(window.find("alpha").visible());
        window.within("tab-bar").click(1usize, cx);
        assert!(window.find("beta").visible());
        assert!(window.try_find("alpha").is_none());
        window.within("tab-bar").drag_to(1usize, 0usize, cx);
    })
    .unwrap();
    cx.wait_for(handle.into(), Duration::from_secs(1), |window, _| {
        window.within("tab-bar").find(0usize).selected() == Some(true)
            && window.try_find("beta").is_some()
    })
    .await;
    cx.update_window(handle.into(), |_, window, cx| {
        window.within("tab-bar").click(1usize, cx);
        assert!(
            window.find("alpha").visible(),
            "dragging beta to slot zero must move alpha to slot one"
        );
        assert!(window.try_find("beta").is_none());
    })
    .unwrap();
}

#[gpui_kit::test]
async fn dock_moves_a_tab_between_groups_and_zooms_the_result(cx: &mut TestAppContext) {
    cx.update(gpui_kit::init);
    let (handle, _) = common::open_window(cx, Some(size(px(900.), px(600.))), |window, cx| {
        let (area, _) = DockSkin::dock_area("editor", None, window, cx);
        let a = cx.new(|cx| Document::new("alpha", window, cx));
        let b = cx.new(|cx| Document::new("beta", window, cx));
        let c = cx.new(|cx| Document::new("gamma", window, cx));
        let layout = DockLayout::h_split()
            .child(
                DockLayout::tabs()
                    .panel_view(panel_handle(a), cx)
                    .panel_view(panel_handle(b), cx),
                None,
            )
            .child(DockLayout::tabs().panel_view(panel_handle(c), cx), None);
        area.update(cx, |area, cx| area.set_center(layout, window, cx));
        let editor = cx.new(|_| Editor { area });
        editor
    });
    let mut right_bounds = None;
    cx.update_window(handle.into(), |_, window, cx| {
        window.render_frame(cx);
        let right = window.find("gamma").bounds();
        right_bounds = Some(right);
        assert!(window.find("alpha").bounds().right() <= right.left());
        let from = window.within("tab-bar").find(1usize).bounds().center();
        window.drag(from, right.center(), cx);
    })
    .unwrap();
    let right_bounds = right_bounds.unwrap();
    cx.wait_for(handle.into(), Duration::from_secs(1), |window, _| {
        window
            .try_find("beta")
            .is_some_and(|panel| panel.bounds().left() >= right_bounds.left())
    })
    .await;
    cx.update_window(handle.into(), |_, window, cx| {
        assert!(window.find("alpha").visible());
        assert!(window.try_find("gamma").is_none());
        // The panel path contains its owning group's native entity scope. No
        // new global IDs or layout wrappers are needed for repeated toolbars.
        let snapshot = window.find("beta");
        let group_ix = snapshot
            .path()
            .iter()
            .position(|id| *id == "tab-panel".into())
            .unwrap();
        let group = snapshot.path()[group_ix - 1].clone();
        window.within(group).click("zoom-in", cx);
    })
    .unwrap();
    cx.wait_for(handle.into(), Duration::from_secs(1), |window, _| {
        window.find("beta").bounds().size.width > right_bounds.size.width
    })
    .await;
    cx.update_window(handle.into(), |_, window, cx| {
        assert!(window.try_find("alpha").is_none());
        window.click("zoom-out", cx);
    })
    .unwrap();
    cx.wait_for(handle.into(), Duration::from_secs(1), |window, _| {
        window.try_find("alpha").is_some()
            && window.find("beta").bounds().size.width <= right_bounds.size.width
    })
    .await;
}

struct TwoTabs {
    area: Entity<DockArea>,
    alpha: Entity<Document>,
    beta: Entity<Document>,
    elsewhere: FocusHandle,
}
impl Render for TwoTabs {
    fn render(&mut self, _: &mut Window, _: &mut Context<Self>) -> impl IntoElement {
        div()
            .size_full()
            .child(div().track_focus(&self.elsewhere).h(px(20.)))
            .child(self.area.clone())
    }
}

fn two_tabs(cx: &mut TestAppContext) -> (gpui_kit::AnyWindowHandle, Entity<TwoTabs>) {
    cx.update(gpui_kit::init);
    let (handle, view) = common::open_window(cx, Some(size(px(900.), px(600.))), |window, cx| {
        let (area, skin) = DockSkin::dock_area("editor", None, window, cx);
        skin.set_panel_style(PanelStyle::TabBar, cx);
        skin.set_close_button_visible(true, cx);
        let alpha = cx.new(|cx| Document::new("alpha", window, cx));
        let beta = cx.new(|cx| Document::new("beta", window, cx));
        let layout = DockLayout::tabs()
            .panel_view(panel_handle(alpha.clone()), cx)
            .panel_view(panel_handle(beta.clone()), cx);
        area.update(cx, |area, cx| area.set_center(layout, window, cx));
        cx.new(|cx| TwoTabs {
            area,
            alpha,
            beta,
            elsewhere: cx.focus_handle(),
        })
    });
    (handle.into(), view)
}

#[gpui_kit::test]
fn dock_hands_the_focus_to_the_tab_that_replaces_a_closed_one(cx: &mut TestAppContext) {
    let (handle, view) = two_tabs(cx);
    cx.update_window(handle, |_, window, cx| {
        let view = view.read(cx);
        let (area, alpha, beta) = (view.area.clone(), view.alpha.clone(), view.beta.clone());
        window.render_frame(cx);
        let alpha_focus = alpha.read(cx).focus.clone();
        alpha_focus.focus(window, cx);
        window.render_frame(cx);
        assert!(alpha.read(cx).focus.is_focused(window));

        area.update(cx, |area, cx| area.remove_panel(alpha, window, cx));
        window.render_frame(cx);
        assert!(window.find("beta").visible());
        assert!(
            beta.read(cx).focus.is_focused(window),
            "closing the focused tab must leave the keyboard on the one shown in its place"
        );
    })
    .unwrap();
}

#[gpui_kit::test]
fn dock_leaves_the_focus_alone_when_a_tab_without_it_closes(cx: &mut TestAppContext) {
    let (handle, view) = two_tabs(cx);
    cx.update_window(handle, |_, window, cx| {
        let view = view.read(cx);
        let (area, alpha, elsewhere) = (
            view.area.clone(),
            view.alpha.clone(),
            view.elsewhere.clone(),
        );
        window.render_frame(cx);
        elsewhere.focus(window, cx);
        window.render_frame(cx);

        area.update(cx, |area, cx| area.remove_panel(alpha, window, cx));
        window.render_frame(cx);
        assert!(elsewhere.is_focused(window));
    })
    .unwrap();
}

fn close_visible_tab(window: &mut Window, name: &str, ix: usize, cx: &mut App) {
    let panel = window.find(name.to_owned());
    let group_ix = panel
        .path()
        .iter()
        .position(|id| *id == "tab-panel".into())
        .unwrap();
    let group = panel.path()[group_ix - 1].clone();
    window.within(group).click(("close-tab", ix), cx);
}

#[gpui_kit::test]
async fn dock_close_button_transfers_focus_from_a_child_input(cx: &mut TestAppContext) {
    let (handle, view) = two_tabs(cx);
    let beta = cx
        .update_window(handle, |_, window, cx| {
            let beta = view.read(cx).beta.clone();
            window.render_frame(cx);
            window.within("alpha").click("editor", cx);
            assert_eq!(window.within("alpha").find("editor").focused(), Some(true));
            close_visible_tab(window, "alpha", 0, cx);
            beta
        })
        .unwrap();
    cx.wait_for(handle, Duration::from_secs(1), |window, cx| {
        window.try_find("alpha").is_none()
            && window.try_find("beta").is_some_and(|panel| panel.visible())
            && beta.read(cx).focus.is_focused(window)
    })
    .await;
}

#[gpui_kit::test]
async fn dock_close_button_preserves_focus_chosen_by_on_removed(cx: &mut TestAppContext) {
    let (handle, view) = two_tabs(cx);
    let elsewhere = cx
        .update_window(handle, |_, window, cx| {
            let view = view.read(cx);
            let (alpha, elsewhere) = (view.alpha.clone(), view.elsewhere.clone());
            alpha.update(cx, |panel, _| panel.removed_focus = Some(elsewhere.clone()));
            window.render_frame(cx);
            window.within("alpha").click("editor", cx);
            close_visible_tab(window, "alpha", 0, cx);
            elsewhere
        })
        .unwrap();
    cx.wait_for(handle, Duration::from_secs(1), |window, _| {
        window.try_find("alpha").is_none() && elsewhere.is_focused(window)
    })
    .await;
}

#[gpui_kit::test]
async fn dock_close_button_focuses_the_remaining_split_when_a_group_disappears(
    cx: &mut TestAppContext,
) {
    for close_alpha in [true, false] {
        let (handle, view) = two_tabs(cx);
        let next = cx
            .update_window(handle, |_, window, cx| {
                let view = view.read(cx);
                let (area, alpha, beta) =
                    (view.area.clone(), view.alpha.clone(), view.beta.clone());
                let layout = DockLayout::h_split()
                    .child(
                        DockLayout::tabs().panel_view(panel_handle(alpha.clone()), cx),
                        None,
                    )
                    .child(
                        DockLayout::tabs().panel_view(panel_handle(beta.clone()), cx),
                        None,
                    );
                area.update(cx, |area, cx| area.set_center(layout, window, cx));
                window.render_frame(cx);
                let (name, next) = if close_alpha {
                    ("alpha", beta)
                } else {
                    ("beta", alpha)
                };
                window.within(name).click("editor", cx);
                close_visible_tab(window, name, 0, cx);
                next
            })
            .unwrap();
        cx.wait_for(handle, Duration::from_secs(1), |window, cx| {
            let closed = if close_alpha { "alpha" } else { "beta" };
            window.try_find(closed).is_none()
                && next.read(cx).focus.is_focused(window)
                && window.find(next.read(cx).name).visible()
        })
        .await;
    }
}

#[gpui_kit::test]
async fn dock_close_button_preserves_focus_when_closing_an_inactive_tab(cx: &mut TestAppContext) {
    let (handle, view) = two_tabs(cx);
    let input = cx
        .update_window(handle, |_, window, cx| {
            let input = view.read(cx).alpha.read(cx).input.clone();
            window.render_frame(cx);
            window.within("alpha").click("editor", cx);
            close_visible_tab(window, "alpha", 1, cx);
            input
        })
        .unwrap();
    cx.wait_for(handle, Duration::from_secs(1), |window, cx| {
        let beta = view.read(cx).beta.clone();
        let area = view.read(cx).area.clone();
        area.read(cx).panel(beta.entity_id().into()).is_none()
            && input.focus_handle(cx).is_focused(window)
    })
    .await;
}

#[gpui_kit::test]
async fn dock_close_button_skips_a_hidden_neighbor_when_handing_off_focus(cx: &mut TestAppContext) {
    let (handle, view) = two_tabs(cx);
    let beta = cx
        .update_window(handle, |_, window, cx| {
            let view = view.read(cx);
            let (area, alpha, beta) = (view.area.clone(), view.alpha.clone(), view.beta.clone());
            let hidden = cx.new(|cx| Document {
                visible: false,
                ..Document::new("hidden", window, cx)
            });
            let layout = DockLayout::h_split()
                .child(DockLayout::tabs().panel_view(panel_handle(alpha), cx), None)
                .child(
                    DockLayout::tabs().panel_view(panel_handle(hidden), cx),
                    None,
                )
                .child(
                    DockLayout::tabs().panel_view(panel_handle(beta.clone()), cx),
                    None,
                );
            area.update(cx, |area, cx| area.set_center(layout, window, cx));
            window.render_frame(cx);
            assert!(window.try_find("hidden").is_none());
            window.within("alpha").click("editor", cx);
            close_visible_tab(window, "alpha", 0, cx);
            beta
        })
        .unwrap();
    cx.wait_for(handle, Duration::from_secs(1), |window, cx| {
        window.try_find("alpha").is_none()
            && window.find("beta").visible()
            && beta.read(cx).focus.is_focused(window)
    })
    .await;
}