forge
Explore
Log in

Don't you dare go Hollow

edith

Clone
★ 0

0 watching0 forksRust18.3 MiBff99ad6Tahinli6 days ago1055 commits

anchor.rs78 lines, 3.1 KiBHistoryRawDownload
1//! Where a dropdown control sits, so the list it opens hangs from that control
2//! (below it, or above it when the window has no room below) instead of from
3//! wherever the pointer happened to land. Every dropdown box records its
4//! bounds while it is painted ([`probe`]); the click that opens a list asks
5//! which recorded box holds the click ([`control_at`]).
6
7use gpui::{Bounds, IntoElement, Pixels, Point, Styled, canvas};
8use std::cell::{Cell, RefCell};
9use std::collections::HashMap;
10
11thread_local! {
12 static ROWS: RefCell<HashMap<(std::path::PathBuf, usize), Bounds<Pixels>>> = RefCell::new(HashMap::new());
13 static BOXES: RefCell<HashMap<&'static str, Bounds<Pixels>>> = RefCell::new(HashMap::new());
14 /// The window's height as the last painted control saw it.
15 static VIEWPORT_H: Cell<f32> = const { Cell::new(0.) };
16}
17
18/// An invisible child that records its parent's bounds under `id` each frame.
19pub(crate) fn probe(id: &'static str) -> impl IntoElement {
20 canvas(
21 move |bounds, window, _| {
22 VIEWPORT_H.with(|v| v.set(f32::from(window.viewport_size().height)));
23 BOXES.with(|b| {
24 b.borrow_mut().insert(id, bounds);
25 })
26 },
27 |_, _, _, _| (),
28 )
29 .absolute()
30 .size_full()
31}
32
33/// Whether a list `list_h` tall hung from the control `id` would run off the bottom of the
34/// window, so it should open above the control instead. An unknown control does not flip.
35pub(crate) fn opens_above(id: &'static str, list_h: f32) -> bool {
36 let Some(control) = BOXES.with(|b| b.borrow().get(id).copied()) else {
37 return false;
38 };
39 let bottom = f32::from(control.origin.y + control.size.height) + 2. + list_h;
40 bottom > VIEWPORT_H.with(Cell::get) - crate::layout::MENU_EDGE
41}
42
43/// The bounds a control recorded under `id` when it was last painted, if any.
44pub(crate) fn box_of(id: &'static str) -> Option<Bounds<Pixels>> {
45 BOXES.with(|b| b.borrow().get(id).copied())
46}
47
48/// The recorded control that holds `click`, if any.
49pub(crate) fn control_at(click: Point<Pixels>) -> Option<Bounds<Pixels>> {
50 BOXES.with(|b| b.borrow().values().find(|r| r.contains(&click)).copied())
51}
52
53/// The list's top-left for a control: its left edge and bottom, and the y it
54/// would take instead when it opens above ([`Bounds::origin`]'s y).
55pub(crate) fn hang(control: Bounds<Pixels>) -> (Point<Pixels>, f32) {
56 (
57 Point { x: control.origin.x, y: control.origin.y + control.size.height + gpui::px(2.) },
58 f32::from(control.origin.y) - 2.,
59 )
60}
61
62/// An invisible child that records its parent's bounds as the media row (or tile) of a source.
63pub(crate) fn probe_row(path: &std::path::Path, stream: usize) -> impl IntoElement {
64 let key = (path.to_path_buf(), stream);
65 canvas(
66 move |bounds, _, _| ROWS.with(|r| {
67 r.borrow_mut().insert(key.clone(), bounds);
68 }),
69 |_, _, _, _| (),
70 )
71 .absolute()
72 .size_full()
73}
74
75/// Where a source's row was last painted.
76pub(crate) fn row_of(path: &std::path::Path, stream: usize) -> Option<Bounds<Pixels>> {
77 ROWS.with(|r| r.borrow().get(&(path.to_path_buf(), stream)).copied())
78}