toolbar.rs

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