gpui_material_components/navigation/
adaptive.rs1use gpui::{
2 AnyElement, App, ElementId, Entity, InteractiveElement, IntoElement, ParentElement, Pixels,
3 RenderOnce, Size, Styled, Window, div, prelude::FluentBuilder, px,
4};
5use gpui_base::TestSupportExt;
6use gpui_material_foundation::window_material_theme;
7use gpui_material_theme::{Direction, WidthClass};
8
9use super::{AdaptivePane, AdaptivePaneLayout, AdaptivePaneState, PaneAdaptStrategy, PaneRole};
10
11#[derive(Clone, Copy, Debug, Default, PartialEq, Eq)]
12pub enum ListDetailPane {
13 #[default]
14 List,
15 Detail,
16}
17
18#[derive(IntoElement)]
22pub struct ListDetail {
23 id: ElementId,
24 list: Option<AnyElement>,
25 detail: AnyElement,
26 width: Pixels,
27 active: ListDetailPane,
28 list_width: Pixels,
29}
30
31impl ListDetail {
32 pub fn new(
33 id: impl Into<ElementId>,
34 width: Pixels,
35 list: impl IntoElement,
36 detail: impl IntoElement,
37 ) -> Self {
38 Self {
39 id: id.into(),
40 list: Some(list.into_any_element()),
41 detail: detail.into_any_element(),
42 width,
43 active: ListDetailPane::List,
44 list_width: px(360.),
45 }
46 }
47
48 pub fn adaptive(
50 id: impl Into<ElementId>,
51 viewport: Size<Pixels>,
52 state: &Entity<AdaptivePaneState>,
53 list: AdaptivePane,
54 detail: AdaptivePane,
55 ) -> AdaptivePaneLayout {
56 AdaptivePaneLayout::new(id, viewport, state)
57 .order([PaneRole::Secondary, PaneRole::Primary, PaneRole::Tertiary])
58 .pane(PaneRole::Secondary, list)
59 .pane(PaneRole::Primary, detail)
60 }
61
62 pub fn detail_only(mut self, detail_only: bool) -> Self {
64 if detail_only {
65 self.list = None;
66 self.active = ListDetailPane::Detail;
67 }
68 self
69 }
70
71 pub fn active_pane(mut self, pane: ListDetailPane) -> Self {
72 self.active = pane;
73 self
74 }
75
76 pub fn list_width(mut self, width: Pixels) -> Self {
77 self.list_width = width.max(px(0.));
78 self
79 }
80
81 pub fn is_single_pane(width: Pixels) -> bool {
82 matches!(
83 WidthClass::from_width(width.into()),
84 WidthClass::Compact | WidthClass::Medium
85 )
86 }
87}
88
89impl RenderOnce for ListDetail {
90 fn render(self, window: &mut Window, cx: &mut App) -> impl IntoElement {
91 let single = Self::is_single_pane(self.width);
92 let rtl = window_material_theme(window, cx).layout.direction == Direction::Rtl;
93 let show_detail = !single || self.active == ListDetailPane::Detail || self.list.is_none();
94 let list = self.list.filter(|_| !single || !show_detail);
95 div()
96 .id(self.id)
97 .test_support()
98 .size_full()
99 .flex()
100 .gap(px(if single { 0. } else { 24. }))
101 .when(rtl, Styled::flex_row_reverse)
102 .children(list.map(|list| {
103 div()
104 .id("list-pane")
105 .test_support()
106 .min_w_0()
107 .min_h_0()
108 .flex()
109 .flex_col()
110 .when_else(single, Styled::flex_1, |pane| {
111 pane.w(self.list_width.min(self.width / 2.)).flex_none()
112 })
113 .child(list)
114 }))
115 .children(show_detail.then(|| {
116 div()
117 .id("detail-pane")
118 .test_support()
119 .min_w_0()
120 .min_h_0()
121 .flex()
122 .flex_col()
123 .flex_1()
124 .child(self.detail)
125 }))
126 }
127}
128
129#[derive(IntoElement)]
132pub struct SupportingPane {
133 id: ElementId,
134 primary: AnyElement,
135 supporting: AnyElement,
136 width: Pixels,
137 show_support: bool,
138}
139
140impl SupportingPane {
141 pub fn new(
142 id: impl Into<ElementId>,
143 width: Pixels,
144 primary: impl IntoElement,
145 supporting: impl IntoElement,
146 ) -> Self {
147 Self {
148 id: id.into(),
149 primary: primary.into_any_element(),
150 supporting: supporting.into_any_element(),
151 width,
152 show_support: false,
153 }
154 }
155
156 pub fn adaptive(
159 id: impl Into<ElementId>,
160 viewport: Size<Pixels>,
161 state: &Entity<AdaptivePaneState>,
162 primary: AdaptivePane,
163 supporting: AdaptivePane,
164 ) -> AdaptivePaneLayout {
165 let supporting = if supporting.has_adaptation() {
166 supporting
167 } else {
168 supporting.adapt(PaneAdaptStrategy::Reflow(PaneRole::Primary))
169 };
170 AdaptivePaneLayout::new(id, viewport, state)
171 .pane(PaneRole::Primary, primary)
172 .pane(PaneRole::Secondary, supporting)
173 }
174
175 pub fn show_support(mut self, show_support: bool) -> Self {
176 self.show_support = show_support;
177 self
178 }
179}
180
181impl RenderOnce for SupportingPane {
182 fn render(self, window: &mut Window, cx: &mut App) -> impl IntoElement {
183 let rtl = window_material_theme(window, cx).layout.direction == Direction::Rtl;
184 let single = ListDetail::is_single_pane(self.width);
185 div()
186 .id(self.id)
187 .test_support()
188 .size_full()
189 .flex()
190 .gap(px(if single { 0. } else { 24. }))
191 .when(rtl, Styled::flex_row_reverse)
192 .children((!single || !self.show_support).then(|| {
193 div()
194 .id("primary-pane")
195 .test_support()
196 .min_w_0()
197 .min_h_0()
198 .flex()
199 .flex_col()
200 .flex_1()
201 .child(self.primary)
202 }))
203 .children((!single || self.show_support).then(|| {
204 div()
205 .id("supporting-pane")
206 .test_support()
207 .min_w_0()
208 .min_h_0()
209 .flex()
210 .flex_col()
211 .when_else(single, Styled::flex_1, |pane| {
212 pane.w((self.width / 3.).min(px(360.))).flex_none()
213 })
214 .child(self.supporting)
215 }))
216 }
217}
218
219#[derive(IntoElement)]
222pub struct Feed {
223 id: ElementId,
224 width: Pixels,
225 scroll: gpui::ScrollHandle,
226 cards: Vec<AnyElement>,
227}
228
229impl Feed {
230 pub fn new(id: impl Into<ElementId>, width: Pixels, scroll: &gpui::ScrollHandle) -> Self {
231 Self {
232 id: id.into(),
233 width,
234 scroll: scroll.clone(),
235 cards: Vec::new(),
236 }
237 }
238
239 pub fn columns(width: Pixels) -> usize {
240 match WidthClass::from_width(width.into()) {
241 WidthClass::Compact => 1,
242 WidthClass::Medium => 2,
243 WidthClass::Expanded => 3,
244 WidthClass::Large | WidthClass::ExtraLarge => 4,
245 }
246 }
247}
248
249impl ParentElement for Feed {
250 fn extend(&mut self, elements: impl IntoIterator<Item = AnyElement>) {
251 self.cards.extend(elements);
252 }
253}
254
255impl RenderOnce for Feed {
256 fn render(self, window: &mut Window, cx: &mut App) -> impl IntoElement {
257 let columns = Self::columns(self.width);
258 let rtl = window_material_theme(window, cx).layout.direction == Direction::Rtl;
259 let state = window.use_keyed_state((self.id.clone(), "feed-columns"), cx, |_, _| columns);
260 state.update(cx, |previous, _| {
261 if *previous != columns && !self.cards.is_empty() {
262 let first_card = (self.scroll.top_item() * *previous).min(self.cards.len() - 1);
263 self.scroll.scroll_to_top_of_item(first_card / columns);
264 }
265 *previous = columns;
266 });
267 let mut cards = self.cards.into_iter();
268 let rows = std::iter::from_fn(move || {
269 let card = cards.next()?;
270 let row = std::iter::once(card).chain(cards.by_ref().take(columns - 1));
271 let mut len = 0;
272 let mut line = div()
273 .flex()
274 .flex_none()
275 .gap(px(16.))
276 .when(rtl, Styled::flex_row_reverse)
277 .children(row.map(|card| {
278 len += 1;
279 div().flex_1().min_w_0().child(card)
280 }));
281 line.extend((len..columns).map(|_| div().flex_1().min_w_0().into_any_element()));
282 Some(line)
283 });
284 gpui_material_foundation::isolated_scroll(self.id, &self.scroll)
285 .test_support()
286 .flex_1()
287 .w_full()
288 .min_w_0()
289 .flex()
290 .flex_col()
291 .gap(px(16.))
292 .p(px(16.))
293 .children(rows)
294 }
295}