| 1 | use gpui_kit::prelude::FluentBuilder as _; |
| 2 | use gpui_kit::*; |
| 3 | |
| 4 | use app_base::theme; |
| 5 | |
| 6 | pub const EDGE_THICKNESS: f32 = 1.; |
| 7 | const SCROLLED_THRESHOLD: f32 = 0.5; |
| 8 | |
| 9 | #[derive(Debug, Clone, Copy, PartialEq, Eq)] |
| 10 | pub enum EdgeRule { |
| 11 | Always, |
| 12 | WhenScrolled, |
| 13 | } |
| 14 | |
| 15 | pub fn content_hidden_above(offset_y: Pixels) -> bool { |
| 16 | f32::from(offset_y) < -SCROLLED_THRESHOLD |
| 17 | } |
| 18 | |
| 19 | pub fn edge_visible(rule: EdgeRule, offset_y: Pixels) -> bool { |
| 20 | match rule { |
| 21 | EdgeRule::Always => true, |
| 22 | EdgeRule::WhenScrolled => content_hidden_above(offset_y), |
| 23 | } |
| 24 | } |
| 25 | |
| 26 | pub fn edge_divider(tag: &'static str) -> Div { |
| 27 | div() |
| 28 | .debug_selector(move || tag.into()) |
| 29 | .w_full() |
| 30 | .h(px(EDGE_THICKNESS)) |
| 31 | .flex_none() |
| 32 | .bg(theme::border()) |
| 33 | } |
| 34 | |
| 35 | pub fn with_top_edge( |
| 36 | scroll_area: impl IntoElement, |
| 37 | handle: &ScrollHandle, |
| 38 | tag: &'static str, |
| 39 | ) -> Div { |
| 40 | let visible = edge_visible(EdgeRule::WhenScrolled, handle.offset().y); |
| 41 | div() |
| 42 | .relative() |
| 43 | .flex() |
| 44 | .flex_col() |
| 45 | .flex_1() |
| 46 | .min_h_0() |
| 47 | .w_full() |
| 48 | .child(scroll_area) |
| 49 | .when(visible, |area| { |
| 50 | area.child(edge_divider(tag).absolute().top_0().left_0().right_0()) |
| 51 | }) |
| 52 | } |
| 53 | |
| 54 | #[cfg(test)] |
| 55 | mod tests { |
| 56 | use gpui_kit::px; |
| 57 | |
| 58 | use super::{EdgeRule, content_hidden_above, edge_visible}; |
| 59 | |
| 60 | #[test] |
| 61 | fn the_list_at_its_top_shows_no_line() { |
| 62 | assert!(!content_hidden_above(px(0.))); |
| 63 | assert!(!edge_visible(EdgeRule::WhenScrolled, px(0.))); |
| 64 | } |
| 65 | |
| 66 | #[test] |
| 67 | fn subpixel_jitter_at_the_top_shows_no_line() { |
| 68 | assert!(!edge_visible(EdgeRule::WhenScrolled, px(-0.3))); |
| 69 | } |
| 70 | |
| 71 | #[test] |
| 72 | fn a_scrolled_list_shows_the_line() { |
| 73 | assert!(edge_visible(EdgeRule::WhenScrolled, px(-1.))); |
| 74 | assert!(edge_visible(EdgeRule::WhenScrolled, px(-480.))); |
| 75 | } |
| 76 | |
| 77 | #[test] |
| 78 | fn a_fixed_header_always_shows_the_line() { |
| 79 | assert!(edge_visible(EdgeRule::Always, px(0.))); |
| 80 | assert!(edge_visible(EdgeRule::Always, px(-20.))); |
| 81 | } |
| 82 | } |