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
14pub struct DeferredPopover(Rc<()>);
20
21pub struct GlobalState {
23 app_menus: Vec<OwnedMenu>,
24 deferred_popovers: Vec<Weak<()>>,
25 suppress_text_selection: bool,
26 text_view_state_stack: RefCell<Vec<Entity<TextViewState>>>,
30 table_cell_stack: RefCell<Vec<Bounds<Pixels>>>,
32 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 pub fn note_touch(cx: &mut App) {
54 Self::init(cx);
55 Self::global_mut(cx).last_touch = Some(Instant::now());
56 }
57
58 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 #[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 pub fn suppress_text_selection(cx: &mut App) {
80 Self::global_mut(cx).suppress_text_selection = true;
81 }
82
83 #[doc(hidden)]
85 pub fn reset_text_selection_suppression(cx: &mut App) {
86 Self::global_mut(cx).suppress_text_selection = false;
87 }
88
89 #[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 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 pub(crate) fn table_cell(&self) -> Option<Bounds<Pixels>> {
126 self.table_cell_stack.borrow().last().copied()
127 }
128
129 #[doc(hidden)]
130 pub fn begin_selection_frame(&self) {}
132
133 pub fn app_menus(&self) -> &[OwnedMenu] {
135 &self.app_menus
136 }
137
138 pub fn set_app_menus(&mut self, menus: Vec<OwnedMenu>) {
140 self.app_menus = menus;
141 }
142
143 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 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 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
186pub(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 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 #[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 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}