toolbar.rs

  1use crate::ItemHandle;
  2use gpui::{
  3    AnyView, Div, Entity, EntityId, EventEmitter, Render, View, ViewContext, WindowContext,
  4};
  5
  6pub enum ToolbarItemEvent {
  7    ChangeLocation(ToolbarItemLocation),
  8}
  9
 10pub trait ToolbarItemView: Render + EventEmitter<ToolbarItemEvent> {
 11    fn set_active_pane_item(
 12        &mut self,
 13        active_pane_item: Option<&dyn crate::ItemHandle>,
 14        cx: &mut ViewContext<Self>,
 15    ) -> ToolbarItemLocation;
 16
 17    fn pane_focus_update(&mut self, _pane_focused: bool, _cx: &mut ViewContext<Self>) {}
 18
 19    /// Number of times toolbar's height will be repeated to get the effective height.
 20    /// Useful when multiple rows one under each other are needed.
 21    /// The rows have the same width and act as a whole when reacting to resizes and similar events.
 22    fn row_count(&self, _cx: &WindowContext) -> usize {
 23        1
 24    }
 25}
 26
 27trait ToolbarItemViewHandle: Send {
 28    fn id(&self) -> EntityId;
 29    fn to_any(&self) -> AnyView;
 30    fn set_active_pane_item(
 31        &self,
 32        active_pane_item: Option<&dyn ItemHandle>,
 33        cx: &mut WindowContext,
 34    ) -> ToolbarItemLocation;
 35    fn focus_changed(&mut self, pane_focused: bool, cx: &mut WindowContext);
 36    fn row_count(&self, cx: &WindowContext) -> usize;
 37}
 38
 39#[derive(Copy, Clone, Debug, PartialEq)]
 40pub enum ToolbarItemLocation {
 41    Hidden,
 42    PrimaryLeft { flex: Option<(f32, bool)> },
 43    PrimaryRight { flex: Option<(f32, bool)> },
 44    Secondary,
 45}
 46
 47pub struct Toolbar {
 48    active_item: Option<Box<dyn ItemHandle>>,
 49    hidden: bool,
 50    can_navigate: bool,
 51    items: Vec<(Box<dyn ToolbarItemViewHandle>, ToolbarItemLocation)>,
 52}
 53
 54impl Render for Toolbar {
 55    type Element = Div<Self>;
 56
 57    fn render(&mut self, cx: &mut ViewContext<Self>) -> Self::Element {
 58        todo!()
 59    }
 60}
 61
 62// todo!()
 63// impl View for Toolbar {
 64//     fn ui_name() -> &'static str {
 65//         "Toolbar"
 66//     }
 67
 68//     fn render(&mut self, cx: &mut ViewContext<Self>) -> AnyElement<Self> {
 69//         let theme = &theme::current(cx).workspace.toolbar;
 70
 71//         let mut primary_left_items = Vec::new();
 72//         let mut primary_right_items = Vec::new();
 73//         let mut secondary_item = None;
 74//         let spacing = theme.item_spacing;
 75//         let mut primary_items_row_count = 1;
 76
 77//         for (item, position) in &self.items {
 78//             match *position {
 79//                 ToolbarItemLocation::Hidden => {}
 80
 81//                 ToolbarItemLocation::PrimaryLeft { flex } => {
 82//                     primary_items_row_count = primary_items_row_count.max(item.row_count(cx));
 83//                     let left_item = ChildView::new(item.as_any(), cx).aligned();
 84//                     if let Some((flex, expanded)) = flex {
 85//                         primary_left_items.push(left_item.flex(flex, expanded).into_any());
 86//                     } else {
 87//                         primary_left_items.push(left_item.into_any());
 88//                     }
 89//                 }
 90
 91//                 ToolbarItemLocation::PrimaryRight { flex } => {
 92//                     primary_items_row_count = primary_items_row_count.max(item.row_count(cx));
 93//                     let right_item = ChildView::new(item.as_any(), cx).aligned().flex_float();
 94//                     if let Some((flex, expanded)) = flex {
 95//                         primary_right_items.push(right_item.flex(flex, expanded).into_any());
 96//                     } else {
 97//                         primary_right_items.push(right_item.into_any());
 98//                     }
 99//                 }
100
101//                 ToolbarItemLocation::Secondary => {
102//                     secondary_item = Some(
103//                         ChildView::new(item.as_any(), cx)
104//                             .constrained()
105//                             .with_height(theme.height * item.row_count(cx) as f32)
106//                             .into_any(),
107//                     );
108//                 }
109//             }
110//         }
111
112//         let container_style = theme.container;
113//         let height = theme.height * primary_items_row_count as f32;
114
115//         let mut primary_items = Flex::row().with_spacing(spacing);
116//         primary_items.extend(primary_left_items);
117//         primary_items.extend(primary_right_items);
118
119//         let mut toolbar = Flex::column();
120//         if !primary_items.is_empty() {
121//             toolbar.add_child(primary_items.constrained().with_height(height));
122//         }
123//         if let Some(secondary_item) = secondary_item {
124//             toolbar.add_child(secondary_item);
125//         }
126
127//         if toolbar.is_empty() {
128//             toolbar.into_any_named("toolbar")
129//         } else {
130//             toolbar
131//                 .contained()
132//                 .with_style(container_style)
133//                 .into_any_named("toolbar")
134//         }
135//     }
136// }
137
138impl Toolbar {
139    pub fn new() -> Self {
140        Self {
141            active_item: None,
142            items: Default::default(),
143            hidden: false,
144            can_navigate: true,
145        }
146    }
147
148    pub fn set_can_navigate(&mut self, can_navigate: bool, cx: &mut ViewContext<Self>) {
149        self.can_navigate = can_navigate;
150        cx.notify();
151    }
152
153    pub fn add_item<T>(&mut self, item: View<T>, cx: &mut ViewContext<Self>)
154    where
155        T: 'static + ToolbarItemView,
156    {
157        let location = item.set_active_pane_item(self.active_item.as_deref(), cx);
158        cx.subscribe(&item, |this, item, event, cx| {
159            if let Some((_, current_location)) =
160                this.items.iter_mut().find(|(i, _)| i.id() == item.id())
161            {
162                match event {
163                    ToolbarItemEvent::ChangeLocation(new_location) => {
164                        if new_location != current_location {
165                            *current_location = *new_location;
166                            cx.notify();
167                        }
168                    }
169                }
170            }
171        })
172        .detach();
173        self.items.push((Box::new(item), location));
174        cx.notify();
175    }
176
177    pub fn set_active_item(&mut self, item: Option<&dyn ItemHandle>, cx: &mut ViewContext<Self>) {
178        self.active_item = item.map(|item| item.boxed_clone());
179        self.hidden = self
180            .active_item
181            .as_ref()
182            .map(|item| !item.show_toolbar(cx))
183            .unwrap_or(false);
184
185        for (toolbar_item, current_location) in self.items.iter_mut() {
186            let new_location = toolbar_item.set_active_pane_item(item, cx);
187            if new_location != *current_location {
188                *current_location = new_location;
189                cx.notify();
190            }
191        }
192    }
193
194    pub fn focus_changed(&mut self, focused: bool, cx: &mut ViewContext<Self>) {
195        for (toolbar_item, _) in self.items.iter_mut() {
196            toolbar_item.focus_changed(focused, cx);
197        }
198    }
199
200    pub fn item_of_type<T: ToolbarItemView>(&self) -> Option<View<T>> {
201        self.items
202            .iter()
203            .find_map(|(item, _)| item.to_any().downcast().ok())
204    }
205
206    pub fn hidden(&self) -> bool {
207        self.hidden
208    }
209}
210
211impl<T: ToolbarItemView> ToolbarItemViewHandle for View<T> {
212    fn id(&self) -> EntityId {
213        self.entity_id()
214    }
215
216    fn to_any(&self) -> AnyView {
217        self.clone().into()
218    }
219
220    fn set_active_pane_item(
221        &self,
222        active_pane_item: Option<&dyn ItemHandle>,
223        cx: &mut WindowContext,
224    ) -> ToolbarItemLocation {
225        self.update(cx, |this, cx| {
226            this.set_active_pane_item(active_pane_item, cx)
227        })
228    }
229
230    fn focus_changed(&mut self, pane_focused: bool, cx: &mut WindowContext) {
231        self.update(cx, |this, cx| {
232            this.pane_focus_update(pane_focused, cx);
233            cx.notify();
234        });
235    }
236
237    fn row_count(&self, cx: &WindowContext) -> usize {
238        self.read(cx).row_count(cx)
239    }
240}
241
242// todo!()
243// impl From<&dyn ToolbarItemViewHandle> for AnyViewHandle {
244//     fn from(val: &dyn ToolbarItemViewHandle) -> Self {
245//         val.as_any().clone()
246//     }
247// }