| 1 | use std::cell::Cell; |
| 2 | use std::rc::Rc; |
| 3 | |
| 4 | use gpui_kit::*; |
| 5 | |
| 6 | pub type HoverChange = Rc<dyn Fn(bool, &mut App)>; |
| 7 | pub type TimelineBounds = Rc<Cell<Option<Bounds<Pixels>>>>; |
| 8 | |
| 9 | pub const TOOLBAR_HEIGHT: f32 = 36.; |
| 10 | const TOOLBAR_GAP: f32 = 4.; |
| 11 | const SLOT_HEIGHT: f32 = TOOLBAR_HEIGHT + 2. * TOOLBAR_GAP; |
| 12 | |
| 13 | pub fn toolbar_slot(id: ElementId, hover: HoverChange, bar: impl IntoElement) -> Stateful<Div> { |
| 14 | div() |
| 15 | .id(id) |
| 16 | .py(px(TOOLBAR_GAP)) |
| 17 | .block_mouse_except_scroll() |
| 18 | .on_hover(move |hovered, _, cx| hover(*hovered, cx)) |
| 19 | .child(bar) |
| 20 | } |
| 21 | |
| 22 | pub fn record_bounds(timeline: &TimelineBounds) -> impl IntoElement { |
| 23 | let timeline = timeline.clone(); |
| 24 | canvas( |
| 25 | move |bounds, _, _| timeline.set(Some(bounds)), |
| 26 | |_, _, _, _| {}, |
| 27 | ) |
| 28 | .absolute() |
| 29 | .top_0() |
| 30 | .left_0() |
| 31 | .size_full() |
| 32 | } |
| 33 | |
| 34 | pub fn placed_toolbar( |
| 35 | toolbar: impl IntoElement, |
| 36 | timeline: TimelineBounds, |
| 37 | right_inset: f32, |
| 38 | ) -> impl IntoElement { |
| 39 | PlacedToolbar { |
| 40 | timeline, |
| 41 | overlay: div() |
| 42 | .absolute() |
| 43 | .top_0() |
| 44 | .left_0() |
| 45 | .size_full() |
| 46 | .child( |
| 47 | div() |
| 48 | .absolute() |
| 49 | .top(px(-SLOT_HEIGHT)) |
| 50 | .right(px(right_inset)) |
| 51 | .child(deferred(toolbar).with_priority(1)), |
| 52 | ) |
| 53 | .into_any_element(), |
| 54 | } |
| 55 | } |
| 56 | |
| 57 | struct PlacedToolbar { |
| 58 | timeline: TimelineBounds, |
| 59 | overlay: AnyElement, |
| 60 | } |
| 61 | |
| 62 | impl PlacedToolbar { |
| 63 | fn flip_offset(&self, anchor: Bounds<Pixels>) -> Pixels { |
| 64 | let bar_top = anchor.top() - px(SLOT_HEIGHT - TOOLBAR_GAP); |
| 65 | match self.timeline.get() { |
| 66 | Some(timeline) if bar_top < timeline.top() => anchor.size.height + px(SLOT_HEIGHT), |
| 67 | _ => px(0.), |
| 68 | } |
| 69 | } |
| 70 | } |
| 71 | |
| 72 | impl Element for PlacedToolbar { |
| 73 | type RequestLayoutState = (); |
| 74 | type PrepaintState = (); |
| 75 | |
| 76 | fn id(&self) -> Option<ElementId> { |
| 77 | None |
| 78 | } |
| 79 | |
| 80 | fn source_location(&self) -> Option<&'static core::panic::Location<'static>> { |
| 81 | None |
| 82 | } |
| 83 | |
| 84 | fn request_layout( |
| 85 | &mut self, |
| 86 | _id: Option<&GlobalElementId>, |
| 87 | _inspector_id: Option<&InspectorElementId>, |
| 88 | window: &mut Window, |
| 89 | cx: &mut App, |
| 90 | ) -> (LayoutId, ()) { |
| 91 | (self.overlay.request_layout(window, cx), ()) |
| 92 | } |
| 93 | |
| 94 | fn prepaint( |
| 95 | &mut self, |
| 96 | _id: Option<&GlobalElementId>, |
| 97 | _inspector_id: Option<&InspectorElementId>, |
| 98 | bounds: Bounds<Pixels>, |
| 99 | _request_layout: &mut (), |
| 100 | window: &mut Window, |
| 101 | cx: &mut App, |
| 102 | ) { |
| 103 | let offset = point(px(0.), self.flip_offset(bounds)); |
| 104 | window.with_element_offset(offset, |window| self.overlay.prepaint(window, cx)); |
| 105 | } |
| 106 | |
| 107 | fn paint( |
| 108 | &mut self, |
| 109 | _id: Option<&GlobalElementId>, |
| 110 | _inspector_id: Option<&InspectorElementId>, |
| 111 | _bounds: Bounds<Pixels>, |
| 112 | _request_layout: &mut (), |
| 113 | _prepaint: &mut (), |
| 114 | window: &mut Window, |
| 115 | cx: &mut App, |
| 116 | ) { |
| 117 | self.overlay.paint(window, cx); |
| 118 | } |
| 119 | } |
| 120 | |
| 121 | impl IntoElement for PlacedToolbar { |
| 122 | type Element = Self; |
| 123 | |
| 124 | fn into_element(self) -> Self::Element { |
| 125 | self |
| 126 | } |
| 127 | } |