| 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, top_inset: Pixels) -> impl IntoElement { |
| 23 | let timeline = timeline.clone(); |
| 24 | canvas( |
| 25 | move |bounds, _, _| timeline.set(Some(bounds)), |
| 26 | |_, _, _, _| {}, |
| 27 | ) |
| 28 | .absolute() |
| 29 | .top(top_inset) |
| 30 | .left_0() |
| 31 | .right_0() |
| 32 | .bottom_0() |
| 33 | } |
| 34 | |
| 35 | #[derive(Clone, Copy, Debug, PartialEq)] |
| 36 | pub enum ToolbarSide { |
| 37 | Above, |
| 38 | Below, |
| 39 | ViewportTop, |
| 40 | } |
| 41 | |
| 42 | pub fn toolbar_side(anchor: Bounds<Pixels>, viewport: Bounds<Pixels>) -> ToolbarSide { |
| 43 | let above_top = anchor.top() - px(SLOT_HEIGHT - TOOLBAR_GAP); |
| 44 | let below_bottom = anchor.bottom() + px(SLOT_HEIGHT - TOOLBAR_GAP); |
| 45 | if above_top >= viewport.top() { |
| 46 | ToolbarSide::Above |
| 47 | } else if below_bottom <= viewport.bottom() { |
| 48 | ToolbarSide::Below |
| 49 | } else { |
| 50 | ToolbarSide::ViewportTop |
| 51 | } |
| 52 | } |
| 53 | |
| 54 | fn toolbar_offset(anchor: Bounds<Pixels>, viewport: Option<Bounds<Pixels>>) -> Pixels { |
| 55 | let Some(viewport) = viewport else { |
| 56 | return px(0.); |
| 57 | }; |
| 58 | match toolbar_side(anchor, viewport) { |
| 59 | ToolbarSide::Above => px(0.), |
| 60 | ToolbarSide::Below => anchor.size.height + px(SLOT_HEIGHT), |
| 61 | ToolbarSide::ViewportTop => viewport.top() - anchor.top() + px(SLOT_HEIGHT), |
| 62 | } |
| 63 | } |
| 64 | |
| 65 | pub fn placed_toolbar( |
| 66 | toolbar: impl IntoElement, |
| 67 | timeline: TimelineBounds, |
| 68 | right_inset: f32, |
| 69 | ) -> impl IntoElement { |
| 70 | PlacedToolbar { |
| 71 | timeline, |
| 72 | overlay: div() |
| 73 | .absolute() |
| 74 | .top_0() |
| 75 | .left_0() |
| 76 | .size_full() |
| 77 | .child( |
| 78 | div() |
| 79 | .absolute() |
| 80 | .top(px(-SLOT_HEIGHT)) |
| 81 | .right(px(right_inset)) |
| 82 | .child(deferred(toolbar).with_priority(1)), |
| 83 | ) |
| 84 | .into_any_element(), |
| 85 | } |
| 86 | } |
| 87 | |
| 88 | struct PlacedToolbar { |
| 89 | timeline: TimelineBounds, |
| 90 | overlay: AnyElement, |
| 91 | } |
| 92 | |
| 93 | impl Element for PlacedToolbar { |
| 94 | type RequestLayoutState = (); |
| 95 | type PrepaintState = (); |
| 96 | |
| 97 | fn id(&self) -> Option<ElementId> { |
| 98 | None |
| 99 | } |
| 100 | |
| 101 | fn source_location(&self) -> Option<&'static core::panic::Location<'static>> { |
| 102 | None |
| 103 | } |
| 104 | |
| 105 | fn request_layout( |
| 106 | &mut self, |
| 107 | _id: Option<&GlobalElementId>, |
| 108 | _inspector_id: Option<&InspectorElementId>, |
| 109 | window: &mut Window, |
| 110 | cx: &mut App, |
| 111 | ) -> (LayoutId, ()) { |
| 112 | (self.overlay.request_layout(window, cx), ()) |
| 113 | } |
| 114 | |
| 115 | fn prepaint( |
| 116 | &mut self, |
| 117 | _id: Option<&GlobalElementId>, |
| 118 | _inspector_id: Option<&InspectorElementId>, |
| 119 | bounds: Bounds<Pixels>, |
| 120 | _request_layout: &mut (), |
| 121 | window: &mut Window, |
| 122 | cx: &mut App, |
| 123 | ) { |
| 124 | let offset = point(px(0.), toolbar_offset(bounds, self.timeline.get())); |
| 125 | window.with_element_offset(offset, |window| self.overlay.prepaint(window, cx)); |
| 126 | } |
| 127 | |
| 128 | fn paint( |
| 129 | &mut self, |
| 130 | _id: Option<&GlobalElementId>, |
| 131 | _inspector_id: Option<&InspectorElementId>, |
| 132 | _bounds: Bounds<Pixels>, |
| 133 | _request_layout: &mut (), |
| 134 | _prepaint: &mut (), |
| 135 | window: &mut Window, |
| 136 | cx: &mut App, |
| 137 | ) { |
| 138 | self.overlay.paint(window, cx); |
| 139 | } |
| 140 | } |
| 141 | |
| 142 | impl IntoElement for PlacedToolbar { |
| 143 | type Element = Self; |
| 144 | |
| 145 | fn into_element(self) -> Self::Element { |
| 146 | self |
| 147 | } |
| 148 | } |
| 149 | |
| 150 | #[cfg(test)] |
| 151 | mod tests { |
| 152 | use super::{SLOT_HEIGHT, TOOLBAR_GAP, ToolbarSide, toolbar_offset, toolbar_side}; |
| 153 | use gpui_kit::{Bounds, point, px, size}; |
| 154 | |
| 155 | fn span(top: f32, bottom: f32) -> Bounds<gpui_kit::Pixels> { |
| 156 | Bounds::new(point(px(0.), px(top)), size(px(400.), px(bottom - top))) |
| 157 | } |
| 158 | |
| 159 | const VIEWPORT: (f32, f32) = (100., 600.); |
| 160 | const BAR_ROOM: f32 = SLOT_HEIGHT - TOOLBAR_GAP; |
| 161 | |
| 162 | #[test] |
| 163 | fn stays_above_with_room_below_the_viewport_top() { |
| 164 | let viewport = span(VIEWPORT.0, VIEWPORT.1); |
| 165 | let message = span(VIEWPORT.0 + BAR_ROOM, 300.); |
| 166 | assert_eq!(toolbar_side(message, viewport), ToolbarSide::Above); |
| 167 | assert_eq!(toolbar_offset(message, Some(viewport)), px(0.)); |
| 168 | } |
| 169 | |
| 170 | #[test] |
| 171 | fn flips_below_when_the_bar_would_cross_the_viewport_top() { |
| 172 | let viewport = span(VIEWPORT.0, VIEWPORT.1); |
| 173 | let message = span(VIEWPORT.0 + BAR_ROOM - 1., 300.); |
| 174 | assert_eq!(toolbar_side(message, viewport), ToolbarSide::Below); |
| 175 | assert_eq!( |
| 176 | toolbar_offset(message, Some(viewport)), |
| 177 | message.size.height + px(SLOT_HEIGHT) |
| 178 | ); |
| 179 | } |
| 180 | |
| 181 | #[test] |
| 182 | fn sticks_to_the_viewport_top_when_neither_side_fits() { |
| 183 | let viewport = span(VIEWPORT.0, VIEWPORT.1); |
| 184 | let message = span(VIEWPORT.0 - 50., VIEWPORT.1 - BAR_ROOM + 1.); |
| 185 | assert_eq!(toolbar_side(message, viewport), ToolbarSide::ViewportTop); |
| 186 | let bar_top = message.top() - px(SLOT_HEIGHT) + toolbar_offset(message, Some(viewport)); |
| 187 | assert_eq!(bar_top, viewport.top()); |
| 188 | } |
| 189 | |
| 190 | #[test] |
| 191 | fn stays_above_without_measured_viewport() { |
| 192 | assert_eq!(toolbar_offset(span(0., 100.), None), px(0.)); |
| 193 | } |
| 194 | } |