BlueGit

ict rusty-teams

Files

rusty-teams / vendor / gpui-component / src / dock / test_support.rs

test_support.rs

test_support.rs 4.4 KiB Download Raw
1//! Panel doubles shared by the skin's tests.
2//!
3//! This lives beside the production modules rather than inside one module's
4//! `mod tests` because the same double is needed by more than one of them: a
5//! tab group and a dock each hand a panel to a different frame, and the
6//! question — did the panel get a height? — is the same. Mirrors
7//! `gpui_base::dock::test_support`.
8
9use std::{cell::Cell, rc::Rc};
10
11use gpui::{
12 App, AppContext as _, Context, Entity, EventEmitter, FocusHandle, Focusable, IntoElement,
13 Pixels, Render, Styled as _, Window, div,
14};
15use gpui_base::dock::PanelEvent;
16
17use crate::{ElementExt as _, dock::Panel};
18
19/// A panel that records the height its container actually gave it.
20///
21/// The defect it exists for is invisible to every behavioral test: a panel
22/// whose content region resolves to zero height still activates, still
23/// persists, and still opens a window. Only a measurement sees it.
24pub(crate) struct MeasuredProbe {
25 focus_handle: FocusHandle,
26 height: Rc<Cell<Pixels>>,
27}
28
29impl MeasuredProbe {
30 pub(crate) fn new(height: Rc<Cell<Pixels>>, cx: &mut App) -> Entity<Self> {
31 cx.new(|cx| Self {
32 focus_handle: cx.focus_handle(),
33 height,
34 })
35 }
36}
37
38impl gpui_base::dock::Panel for MeasuredProbe {
39 fn panel_name(&self) -> &'static str {
40 "MeasuredProbe"
41 }
42}
43
44impl Panel for MeasuredProbe {}
45impl EventEmitter<PanelEvent> for MeasuredProbe {}
46
47impl Focusable for MeasuredProbe {
48 fn focus_handle(&self, _: &App) -> FocusHandle {
49 self.focus_handle.clone()
50 }
51}
52
53impl Render for MeasuredProbe {
54 fn render(&mut self, _: &mut Window, _: &mut Context<Self>) -> impl IntoElement {
55 let height = self.height.clone();
56 div()
57 .size_full()
58 .on_prepaint(move |bounds, _, _| height.set(bounds.size.height))
59 }
60}
61
62/// A probe that records the whole box it was given, not only its height.
63///
64/// A dock's fault is a width fault -- a dock that never states its extent stops
65/// being a column and takes whatever the row hands it -- so asking about it
66/// needs the measurement `MeasuredProbe` does not keep.
67pub(crate) struct SizedProbe {
68 focus_handle: FocusHandle,
69 size: Rc<Cell<gpui::Size<Pixels>>>,
70}
71
72impl SizedProbe {
73 pub(crate) fn new(size: Rc<Cell<gpui::Size<Pixels>>>, cx: &mut App) -> Entity<Self> {
74 cx.new(|cx| Self {
75 focus_handle: cx.focus_handle(),
76 size,
77 })
78 }
79}
80
81impl gpui_base::dock::Panel for SizedProbe {
82 fn panel_name(&self) -> &'static str {
83 "SizedProbe"
84 }
85}
86
87impl Panel for SizedProbe {}
88impl EventEmitter<PanelEvent> for SizedProbe {}
89
90impl Focusable for SizedProbe {
91 fn focus_handle(&self, _: &App) -> FocusHandle {
92 self.focus_handle.clone()
93 }
94}
95
96impl Render for SizedProbe {
97 fn render(&mut self, _: &mut Window, _: &mut Context<Self>) -> impl IntoElement {
98 let size = self.size.clone();
99 div()
100 .size_full()
101 .on_prepaint(move |bounds, _, _| size.set(bounds.size))
102 }
103}
104
105/// A [`MeasuredProbe`] whose `visible` can be switched off.
106///
107/// Hiding the only panel of a slot is the one edit that takes a whole slot out
108/// of a split without changing the tree, so it is the only way to ask whether
109/// the *drawn* slots still fill the split.
110pub(crate) struct HideableProbe {
111 focus_handle: FocusHandle,
112 visible: bool,
113 height: Rc<Cell<Pixels>>,
114}
115
116impl HideableProbe {
117 pub(crate) fn new(height: Rc<Cell<Pixels>>, cx: &mut App) -> Entity<Self> {
118 cx.new(|cx| Self {
119 focus_handle: cx.focus_handle(),
120 visible: true,
121 height,
122 })
123 }
124
125 pub(crate) fn set_visible(&mut self, visible: bool, cx: &mut Context<Self>) {
126 self.visible = visible;
127 cx.notify();
128 }
129}
130
131impl gpui_base::dock::Panel for HideableProbe {
132 fn panel_name(&self) -> &'static str {
133 "HideableProbe"
134 }
135
136 fn visible(&self, _: &App) -> bool {
137 self.visible
138 }
139}
140
141impl Panel for HideableProbe {}
142impl EventEmitter<PanelEvent> for HideableProbe {}
143
144impl Focusable for HideableProbe {
145 fn focus_handle(&self, _: &App) -> FocusHandle {
146 self.focus_handle.clone()
147 }
148}
149
150impl Render for HideableProbe {
151 fn render(&mut self, _: &mut Window, _: &mut Context<Self>) -> impl IntoElement {
152 let height = self.height.clone();
153 div()
154 .size_full()
155 .on_prepaint(move |bounds, _, _| height.set(bounds.size.height))
156 }
157}