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