1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
use gpui::{App, Axis, FocusHandle, Window};
use gpui_material_theme::Direction;
/// Moves within an ordered focus group, skipping disabled items and wrapping.
/// Supports axis arrows (`left`/`right` or `up`/`down`) plus `home` and `end`.
/// The caller owns selection and sets the active item's tab stop.
pub fn move_roving_focus(
items: &[(&FocusHandle, bool)],
key: &str,
axis: Axis,
direction: Direction,
window: &mut Window,
cx: &mut App,
) -> Option<usize> {
match key {
"home" => {
return items.iter().enumerate().find_map(|(ix, (focus, enabled))| {
enabled.then(|| {
focus.focus(window, cx);
ix
})
});
}
"end" => {
return items
.iter()
.enumerate()
.rev()
.find_map(|(ix, (focus, enabled))| {
enabled.then(|| {
focus.focus(window, cx);
ix
})
});
}
_ => {}
}
let backward = match key {
"left" if axis == Axis::Horizontal => direction == Direction::Ltr,
"right" if axis == Axis::Horizontal => direction == Direction::Rtl,
"up" if axis == Axis::Vertical => true,
"down" if axis == Axis::Vertical => false,
_ => return None,
};
let len = items.len();
let current = items.iter().position(|(focus, _)| focus.is_focused(window));
let start = current.unwrap_or(if backward { 0 } else { len.saturating_sub(1) });
(1..=len).find_map(|offset| {
let ix = if backward {
(start + len - offset) % len
} else {
(start + offset) % len
};
let (focus, enabled) = items[ix];
enabled.then(|| {
focus.focus(window, cx);
ix
})
})
}
/// Watches an active GPUI drag for unexpected native capture loss.
///
/// On Windows, this polls Win32 capture ownership on the UI thread while
/// upstream GPUI lacks capture-loss notifications; on other platforms it is a
/// lightweight no-op guard. Keep the guard for the drag lifetime and drop it
/// on completion or cancellation.
pub struct DragCapture {
#[cfg(windows)]
_watch: Option<gpui::Task<()>>,
}
impl DragCapture {
/// Call from GPUI's drag start handler. The callback runs once on the window thread
/// after GPUI's drag is cancelled. Handle Esc, disable and window blur at the owner.
pub fn watch(
window: &Window,
cx: &App,
on_lost: impl FnOnce(&mut Window, &mut App) + 'static,
) -> Self {
Self::watch_capture(
window,
cx,
|_, cx| cx.has_active_drag(),
move |window, cx| {
window.release_pointer();
cx.stop_active_drag(window);
on_lost(window, cx);
},
)
}
/// Watches a direct pointer gesture after calling `Window::capture_pointer`.
/// The owner retains this guard until the gesture ends or is cancelled.
pub fn watch_pointer(
window: &Window,
cx: &App,
on_lost: impl FnOnce(&mut Window, &mut App) + 'static,
) -> Self {
Self::watch_capture(
window,
cx,
|window, _| window.captured_hitbox().is_some(),
move |window, cx| {
window.release_pointer();
on_lost(window, cx);
},
)
}
fn watch_capture(
window: &Window,
cx: &App,
is_active: fn(&Window, &App) -> bool,
on_lost: impl FnOnce(&mut Window, &mut App) + 'static,
) -> Self {
#[cfg(windows)]
{
use std::time::Duration;
use windows::Win32::UI::Input::KeyboardAndMouse::GetCapture;
// GPUI takes capture before starting a native mouse drag. GetCapture
// is thread-local, and this task resumes on the window's UI thread.
let capture = unsafe { GetCapture() };
let watch = (!capture.is_invalid()).then(|| {
let executor = cx.background_executor().clone();
let mut on_lost = Some(on_lost);
window.spawn(cx, async move |cx| {
loop {
executor.timer(Duration::from_millis(16)).await;
let keep_watching = cx.update(|window, cx| {
if !is_active(window, cx) {
return false;
}
if unsafe { GetCapture() } == capture {
return true;
}
if let Some(on_lost) = on_lost.take() {
on_lost(window, cx);
}
false
});
if !keep_watching.unwrap_or(false) {
break;
}
}
})
});
Self { _watch: watch }
}
#[cfg(not(windows))]
{
let _ = (window, cx, is_active, on_lost);
Self {}
}
}
/// Cancels the current GPUI drag and releases its captured pointer.
pub fn cancel(self, window: &mut Window, cx: &mut App) {
window.release_pointer();
cx.stop_active_drag(window);
}
}