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