Skip to main content

fc_ui/capabilities/navigation/
dock.rs

1//! macOS Dock-style magnification row.
2
3use gpui::{prelude::FluentBuilder as _, *};
4
5use crate::capabilities::foundation::theme::use_theme;
6
7pub struct DockState {
8    cursor_x: Option<Pixels>,
9    bounds: Option<Bounds<Pixels>>,
10}
11
12impl DockState {
13    pub fn new(_cx: &mut Context<Self>) -> Self {
14        Self {
15            cursor_x: None,
16            bounds: None,
17        }
18    }
19}
20
21impl Render for DockState {
22    fn render(&mut self, _window: &mut Window, _cx: &mut Context<Self>) -> impl IntoElement {
23        Empty
24    }
25}
26
27#[allow(dead_code)]
28struct DockItemInfo {
29    center_x: f32,
30    scale: f32,
31}
32
33fn compute_scales(
34    item_count: usize,
35    item_size_f: f32,
36    gap: f32,
37    cursor_rel_x: Option<f32>,
38    max_scale: f32,
39    sigma: f32,
40) -> Vec<DockItemInfo> {
41    let mut items = Vec::with_capacity(item_count);
42    let total_width = item_count as f32 * item_size_f + (item_count.saturating_sub(1)) as f32 * gap;
43    let start_x = -total_width * 0.5;
44
45    for i in 0..item_count {
46        let center = start_x + i as f32 * (item_size_f + gap) + item_size_f * 0.5;
47        let scale = cursor_rel_x.map_or(1.0, |cx_pos| {
48            let dist = (center - cx_pos).abs();
49            1.0 + max_scale * (-dist * dist / (2.0 * sigma * sigma)).exp()
50        });
51        items.push(DockItemInfo {
52            center_x: center,
53            scale,
54        });
55    }
56    items
57}
58
59#[derive(IntoElement)]
60pub struct Dock {
61    id: ElementId,
62    state: Entity<DockState>,
63    max_scale: f32,
64    item_size: Pixels,
65    gap: Pixels,
66    style: StyleRefinement,
67    children: Vec<AnyElement>,
68}
69
70impl Dock {
71    pub fn new(id: impl Into<ElementId>, state: Entity<DockState>) -> Self {
72        Self {
73            id: id.into(),
74            state,
75            max_scale: 0.5,
76            item_size: px(48.0),
77            gap: px(4.0),
78            style: StyleRefinement::default(),
79            children: Vec::new(),
80        }
81    }
82
83    pub fn max_scale(mut self, scale: f32) -> Self {
84        self.max_scale = scale.max(0.0);
85        self
86    }
87
88    pub fn item_size(mut self, size: Pixels) -> Self {
89        self.item_size = size;
90        self
91    }
92
93    pub fn gap(mut self, gap: Pixels) -> Self {
94        self.gap = gap;
95        self
96    }
97}
98
99impl Styled for Dock {
100    fn style(&mut self) -> &mut StyleRefinement {
101        &mut self.style
102    }
103}
104
105impl ParentElement for Dock {
106    fn extend(&mut self, elements: impl IntoIterator<Item = AnyElement>) {
107        self.children.extend(elements);
108    }
109}
110
111impl RenderOnce for Dock {
112    fn render(self, _window: &mut Window, cx: &mut App) -> impl IntoElement {
113        let theme = use_theme(cx);
114        let user_style = self.style;
115        let state = self.state.read(cx);
116        let item_count = self.children.len();
117        let item_size_f = self.item_size / px(1.0);
118        let gap_f = self.gap / px(1.0);
119        let sigma = item_size_f * 1.5;
120
121        let cursor_rel = state.cursor_x.map(|cx_pos| cx_pos / px(1.0));
122
123        let scales = compute_scales(
124            item_count,
125            item_size_f,
126            gap_f,
127            cursor_rel,
128            self.max_scale,
129            sigma,
130        );
131
132        let state_bounds = self.state.clone();
133        let state_move = self.state.clone();
134        let state_hover = self.state.clone();
135
136        let mut row = div()
137            .id(self.id)
138            .relative()
139            .flex()
140            .flex_row()
141            .items_end()
142            .gap(self.gap)
143            .px(px(8.0))
144            .py(px(4.0))
145            .rounded(px(12.0))
146            .bg(theme.tokens.card)
147            .border_1()
148            .border_color(theme.tokens.border)
149            .child(
150                canvas(
151                    move |bounds, _window, cx| {
152                        state_bounds.update(cx, |s, _| s.bounds = Some(bounds));
153                    },
154                    |_bounds, _, _window, _cx| {},
155                )
156                .absolute()
157                .inset_0()
158                .size_full(),
159            )
160            .on_mouse_move(move |event: &MouseMoveEvent, _window, cx| {
161                state_move.update(cx, |s, cx| {
162                    if let Some(bounds) = s.bounds {
163                        let center_x: f32 = bounds.center().x.into();
164                        let event_x: f32 = event.position.x.into();
165                        s.cursor_x = Some(px(event_x - center_x));
166                        cx.notify();
167                    }
168                });
169            })
170            .on_hover(move |hovered: &bool, _window, cx| {
171                if !*hovered {
172                    state_hover.update(cx, |s, cx| {
173                        s.cursor_x = None;
174                        cx.notify();
175                    });
176                }
177            })
178            .map(|this: Stateful<Div>| {
179                let mut el = this;
180                el.style().refine(&user_style);
181                el
182            });
183
184        for (child, info) in self.children.into_iter().zip(scales.iter()) {
185            let scaled_size = px(item_size_f * info.scale);
186            row = row.child(
187                div()
188                    .flex_shrink_0()
189                    .w(scaled_size)
190                    .h(scaled_size)
191                    .flex()
192                    .items_center()
193                    .justify_center()
194                    .child(child),
195            );
196        }
197
198        row
199    }
200}