Skip to main content

gpui_base/
global_state.rs

1#[cfg(not(target_family = "wasm"))]
2use std::time::Instant;
3use std::{
4    cell::RefCell,
5    rc::{Rc, Weak},
6};
7#[cfg(target_family = "wasm")]
8use web_time::Instant;
9
10use gpui::{App, Bounds, Entity, Global, OwnedMenu, Pixels, Point};
11
12use crate::text::TextViewState;
13
14/// Holds the deferred interaction context open for as long as it is alive.
15///
16/// Handed out by [`GlobalState::register_deferred_popover`]; drop it to close
17/// the context again. The registry only ever weighs the token's liveness and
18/// its place among the others, so there is nothing inside to read.
19pub struct DeferredPopover(Rc<()>);
20
21/// Application-wide state shared by Base behaviors.
22pub struct GlobalState {
23    app_menus: Vec<OwnedMenu>,
24    deferred_popovers: Vec<Weak<()>>,
25    suppress_text_selection: bool,
26    /// The text views being prepainted or painted, innermost last. Kept
27    /// apart from what views read of this global: pushing and popping as
28    /// they draw is not a change of it.
29    text_view_state_stack: RefCell<Vec<Entity<TextViewState>>>,
30    /// The table cells being prepainted or painted, innermost last.
31    table_cell_stack: RefCell<Vec<Bounds<Pixels>>>,
32    /// When a finger last went down. A tap reaches controls as a mouse press;
33    /// this is how they tell it from one.
34    last_touch: Option<Instant>,
35}
36
37impl Global for GlobalState {}
38
39impl GlobalState {
40    fn new() -> Self {
41        Self {
42            app_menus: Vec::new(),
43            deferred_popovers: Vec::new(),
44            suppress_text_selection: false,
45            text_view_state_stack: RefCell::new(Vec::new()),
46            table_cell_stack: RefCell::new(Vec::new()),
47            last_touch: None,
48        }
49    }
50
51    /// Records that a finger went down. Called from the touch drag GPUI
52    /// offers on every touch, before it becomes a tap, a long press or a pan.
53    pub fn note_touch(cx: &mut App) {
54        Self::init(cx);
55        Self::global_mut(cx).last_touch = Some(Instant::now());
56    }
57
58    /// Whether the press being handled came from a finger: a touch went
59    /// down recently enough that the mouse events of a tap are still
60    /// arriving. A double tap takes up to twice the tap interval.
61    pub fn is_touch_press(cx: &App) -> bool {
62        cx.try_global::<Self>()
63            .and_then(|state| state.last_touch)
64            .is_some_and(|at| at.elapsed() < std::time::Duration::from_secs(1))
65    }
66
67    /// Ensures that the Base global exists.
68    #[doc(hidden)]
69    pub fn init(cx: &mut App) {
70        if !cx.has_global::<Self>() {
71            cx.set_global(Self::new());
72        }
73    }
74
75    /// Suppresses window-level text selection for the current mouse down.
76    ///
77    /// Controls that own a press or drag interaction use this so the same
78    /// pointer event does not also start application text selection.
79    pub fn suppress_text_selection(cx: &mut App) {
80        Self::global_mut(cx).suppress_text_selection = true;
81    }
82
83    /// Clears the current mouse-down text-selection suppression.
84    #[doc(hidden)]
85    pub fn reset_text_selection_suppression(cx: &mut App) {
86        Self::global_mut(cx).suppress_text_selection = false;
87    }
88
89    /// Returns whether the current mouse down suppresses text selection.
90    #[doc(hidden)]
91    pub fn is_text_selection_suppressed(cx: &App) -> bool {
92        Self::global(cx).suppress_text_selection
93    }
94
95    pub fn global(cx: &App) -> &Self {
96        cx.global::<Self>()
97    }
98
99    pub fn global_mut(cx: &mut App) -> &mut Self {
100        cx.global_mut::<Self>()
101    }
102
103    pub(crate) fn text_view_state(&self) -> Option<Entity<TextViewState>> {
104        self.text_view_state_stack.borrow().last().cloned()
105    }
106
107    pub(crate) fn push_text_view_state(&self, state: Entity<TextViewState>) {
108        self.text_view_state_stack.borrow_mut().push(state);
109    }
110
111    pub(crate) fn pop_text_view_state(&self) {
112        self.text_view_state_stack.borrow_mut().pop();
113    }
114
115    /// Enters the table cell being prepainted or painted.
116    pub(crate) fn push_table_cell(&self, bounds: Bounds<Pixels>) {
117        self.table_cell_stack.borrow_mut().push(bounds);
118    }
119
120    pub(crate) fn pop_table_cell(&self) {
121        self.table_cell_stack.borrow_mut().pop();
122    }
123
124    /// The table cell being prepainted or painted, if any.
125    pub(crate) fn table_cell(&self) -> Option<Bounds<Pixels>> {
126        self.table_cell_stack.borrow().last().copied()
127    }
128
129    #[doc(hidden)]
130    // Kept for compatibility; automatic order now belongs to the window's selection state.
131    pub fn begin_selection_frame(&self) {}
132
133    /// Returns the application menus.
134    pub fn app_menus(&self) -> &[OwnedMenu] {
135        &self.app_menus
136    }
137
138    /// Replaces the application menus.
139    pub fn set_app_menus(&mut self, menus: Vec<OwnedMenu>) {
140        self.app_menus = menus;
141    }
142
143    /// Returns whether any deferred popup currently owns an open interaction
144    /// context.
145    pub fn is_in_deferred_context(cx: &App) -> bool {
146        Self::global(cx)
147            .deferred_popovers
148            .iter()
149            .any(|popover| popover.strong_count() > 0)
150    }
151
152    /// Whether a deferred popup that opened after `popover` is still open,
153    /// which is how a popup opened from inside another one appears here.
154    pub(crate) fn has_deferred_popover_after(popover: &DeferredPopover, cx: &App) -> bool {
155        let token = Rc::downgrade(&popover.0);
156        Self::global(cx)
157            .deferred_popovers
158            .iter()
159            .skip_while(|other| !other.ptr_eq(&token))
160            .skip(1)
161            .any(|other| other.strong_count() > 0)
162    }
163
164    /// Registers an open deferred popup, which stays registered for as long as
165    /// the returned token is held.
166    ///
167    /// A token rather than an identifier, because popup state is routinely
168    /// dropped without ever being closed: state that stops being rendered — a
169    /// popover scrolled out of a virtual list, a panel closed while its menu is
170    /// open — is collected at the end of the frame, with no chance to
171    /// deregister. A registration that outlived its popup would leave the
172    /// application believing a popup is open forever, and everything that
173    /// steps aside for open popups (the native context menu of a text input,
174    /// say) would stay disabled for the rest of the session.
175    pub fn register_deferred_popover(cx: &mut App) -> DeferredPopover {
176        let token = Rc::new(());
177        let state = Self::global_mut(cx);
178        state
179            .deferred_popovers
180            .retain(|popover| popover.strong_count() > 0);
181        state.deferred_popovers.push(Rc::downgrade(&token));
182        DeferredPopover(token)
183    }
184}
185
186/// Confines a selection to its anchor cell and excludes text in other cells.
187pub(crate) fn confine_to_cell(
188    anchor_cell: Option<(Point<Pixels>, Bounds<Pixels>)>,
189    painting: Option<Bounds<Pixels>>,
190    anchor: Point<Pixels>,
191    cursor: Point<Pixels>,
192) -> Option<(Point<Pixels>, Point<Pixels>)> {
193    let Some((_, cell)) = anchor_cell.filter(|(cell_anchor, _)| *cell_anchor == anchor) else {
194        return Some((anchor, cursor));
195    };
196    if painting != Some(cell) {
197        return None;
198    }
199    let cursor = gpui::point(
200        cursor.x.clamp(cell.left(), cell.right()),
201        cursor.y.clamp(cell.top(), cell.bottom()),
202    );
203    Some((anchor, cursor))
204}
205
206#[cfg(test)]
207mod tests {
208    use super::*;
209
210    mod table_cell {
211        use gpui::{Bounds, point, px, size};
212
213        use super::super::confine_to_cell;
214
215        fn cell(x: f32) -> Bounds<gpui::Pixels> {
216            Bounds::new(point(px(x), px(0.)), size(px(100.), px(30.)))
217        }
218
219        #[test]
220        fn a_drag_outside_a_cell_is_unchanged() {
221            let (anchor, cursor) = (point(px(5.), px(5.)), point(px(250.), px(80.)));
222            assert_eq!(
223                confine_to_cell(None, Some(cell(0.)), anchor, cursor),
224                Some((anchor, cursor))
225            );
226            // A cell from an older drag does not hold this anchor.
227            let old = Some((point(px(50.), px(10.)), cell(0.)));
228            assert_eq!(
229                confine_to_cell(old, None, anchor, cursor),
230                Some((anchor, cursor))
231            );
232        }
233
234        #[test]
235        fn a_drag_from_a_cell_selects_nothing_outside_it() {
236            let anchor = point(px(10.), px(10.));
237            let anchor_cell = Some((anchor, cell(0.)));
238            let cursor = point(px(150.), px(20.));
239            assert_eq!(
240                confine_to_cell(anchor_cell, Some(cell(100.)), anchor, cursor),
241                None
242            );
243            assert_eq!(confine_to_cell(anchor_cell, None, anchor, cursor), None);
244        }
245
246        #[test]
247        fn a_drag_from_a_cell_is_clamped_to_it() {
248            let anchor = point(px(10.), px(10.));
249            let anchor_cell = Some((anchor, cell(0.)));
250            assert_eq!(
251                confine_to_cell(
252                    anchor_cell,
253                    Some(cell(0.)),
254                    anchor,
255                    point(px(250.), px(80.))
256                ),
257                Some((anchor, point(px(100.), px(30.))))
258            );
259            assert_eq!(
260                confine_to_cell(
261                    anchor_cell,
262                    Some(cell(0.)),
263                    anchor,
264                    point(px(-20.), px(-5.))
265                ),
266                Some((anchor, point(px(0.), px(0.))))
267            );
268            let inside = point(px(60.), px(12.));
269            assert_eq!(
270                confine_to_cell(anchor_cell, Some(cell(0.)), anchor, inside),
271                Some((anchor, inside))
272            );
273        }
274    }
275
276    #[gpui::test]
277    fn initialization_is_idempotent_and_suppression_can_be_reset(cx: &mut gpui::TestAppContext) {
278        cx.update(|cx| {
279            GlobalState::init(cx);
280            GlobalState::suppress_text_selection(cx);
281            GlobalState::init(cx);
282            assert!(GlobalState::is_text_selection_suppressed(cx));
283
284            GlobalState::reset_text_selection_suppression(cx);
285            assert!(!GlobalState::is_text_selection_suppressed(cx));
286
287            assert!(!GlobalState::is_in_deferred_context(cx));
288            let popover = GlobalState::register_deferred_popover(cx);
289            assert!(GlobalState::is_in_deferred_context(cx));
290            drop(popover);
291            assert!(!GlobalState::is_in_deferred_context(cx));
292        });
293    }
294
295    /// Popup state is routinely dropped without being closed first, and a
296    /// registration that survived it would disable everything that steps aside
297    /// for an open popup, permanently.
298    #[gpui::test]
299    fn a_dropped_registration_closes_the_deferred_context(cx: &mut gpui::TestAppContext) {
300        cx.update(|cx| {
301            GlobalState::init(cx);
302
303            let outer = GlobalState::register_deferred_popover(cx);
304            {
305                let _inner = GlobalState::register_deferred_popover(cx);
306                assert!(GlobalState::is_in_deferred_context(cx));
307            }
308            assert!(GlobalState::is_in_deferred_context(cx));
309
310            drop(outer);
311            assert!(!GlobalState::is_in_deferred_context(cx));
312
313            // Registering again must not resurrect the collected ones.
314            let popover = GlobalState::register_deferred_popover(cx);
315            assert_eq!(GlobalState::global(cx).deferred_popovers.len(), 1);
316            drop(popover);
317            assert!(!GlobalState::is_in_deferred_context(cx));
318        });
319    }
320}