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
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
//! Window roots and presentation-layer plugins.
use crate::input::Copy;
use crate::{StyledExt, TextSelectionLayer};
use gpui::{
AnyElement, AnyView, App, AppContext, ClipboardItem, Context, Div, Entity, Global,
InteractiveElement, IntoElement, KeyBinding, ParentElement, Render, Stateful, StyleRefinement,
Styled, Window, actions, div,
};
use std::{any::TypeId, rc::Rc};
actions!(root, [Tab, TabPrev]);
const CONTEXT: &str = "Root";
pub(crate) fn init(cx: &mut App) {
cx.bind_keys([
KeyBinding::new("tab", Tab, Some(CONTEXT)),
KeyBinding::new("shift-tab", TabPrev, Some(CONTEXT)),
#[cfg(target_os = "macos")]
KeyBinding::new("cmd-c", Copy, Some(CONTEXT)),
#[cfg(not(target_os = "macos"))]
KeyBinding::new("ctrl-c", Copy, Some(CONTEXT)),
]);
}
/// A presentation layer's retained, per-window facilities.
///
/// Register during explicit application initialization, before creating windows.
/// The view renders above application content. Base owns the root regardless of
/// which plugins are registered; Cargo features never select its type.
///
/// On every root render, each plugin participates in three stages:
///
/// 1. [`RootPlugin::prepare`] synchronizes window state before elements are built.
/// 2. [`RootPlugin::style`] supplies defaults for the root surface.
/// 3. [`RootPlugin::decorate`] wraps the completed surface in presentation owned
/// by the plugin.
///
/// The plugin's [`Render`] output is mounted as an overlay above application
/// content. Plugins and their overlays are processed in registration order, so
/// later plugins appear above earlier ones. Notify the plugin entity after its
/// state changes to render the root again; `prepare` and `style` must not notify,
/// because they run during that render.
///
/// Factories are captured when a root is created, so registration affects only
/// future windows.
pub trait RootPlugin: Render + Sized {
/// Synchronize settings derived from this plugin with the window.
///
/// This runs before the root surface and plugin overlays are built. It is
/// intended for window-scoped state such as rem size or the active text
/// selection scope, not for producing elements.
fn prepare(&mut self, _window: &mut Window, _cx: &mut Context<Self>) {}
/// Apply this plugin's default styles directly to the root surface.
///
/// Styles set on the [`Root`] instance are refined onto the surface after
/// this hook and therefore take precedence over plugin defaults.
fn style(&self, _surface: &mut Stateful<Div>, _window: &mut Window, _cx: &mut App) {}
/// Add presentation around the completed root surface.
///
/// This runs after plugin defaults and instance styles have been applied.
/// Return `surface` unchanged when no outer presentation is needed. Typical
/// uses include client-side window borders or another structural wrapper.
/// `root` provides read-only access to the root and its application view.
fn decorate(
&self,
surface: AnyElement,
_root: &Root,
_window: &mut Window,
_cx: &mut App,
) -> impl IntoElement {
surface
}
}
type PluginFactory = Rc<dyn Fn(&mut Window, &mut Context<Root>) -> Plugin>;
type Prepare = Rc<dyn Fn(&mut Window, &mut App)>;
type SurfaceStyle = Rc<dyn Fn(&mut Stateful<Div>, &mut Window, &mut App)>;
type Decorate = Rc<dyn Fn(AnyElement, &Root, &mut Window, &mut App) -> AnyElement>;
#[derive(Default)]
struct PluginRegistry(Vec<(TypeId, PluginFactory)>);
impl Global for PluginRegistry {}
struct Plugin {
view: AnyView,
prepare: Prepare,
style: SurfaceStyle,
decorate: Decorate,
}
/// The window's content and overlay host, independent of any styled component library.
pub struct Root {
view: AnyView,
style: StyleRefinement,
plugins: Vec<Plugin>,
}
impl Root {
/// Register a presentation plugin once per application. Re-registering its
/// type replaces the factory for future windows rather than mounting it twice.
pub fn register_plugin<V: RootPlugin>(
cx: &mut App,
build: fn(&mut Window, &mut Context<V>) -> V,
) {
if !cx.has_global::<PluginRegistry>() {
cx.set_global(PluginRegistry::default());
}
let factory: PluginFactory = Rc::new(move |window, cx| {
let entity = cx.new(|cx| build(window, cx));
let root = cx.weak_entity();
let observed = entity.clone();
// Plugin construction can enqueue notifications. Observe only after
// that effect cycle so mounting a Root does not immediately render
// application content a second time.
cx.defer(move |cx| {
cx.observe(&observed, move |_, cx| {
let _ = root.update(cx, |_, cx| cx.notify());
})
.detach();
});
let prepare = entity.clone();
let style = entity.clone();
let decorate = entity.clone();
Plugin {
view: entity.into(),
prepare: Rc::new(move |window, cx| {
prepare.update(cx, |state, cx| state.prepare(window, cx))
}),
style: Rc::new(move |surface, window, cx| {
style.update(cx, |state, cx| state.style(surface, window, cx))
}),
decorate: Rc::new(move |surface, root, window, cx| {
decorate.update(cx, |state, cx| {
state.decorate(surface, root, window, cx).into_any_element()
})
}),
}
});
let plugins = &mut cx.global_mut::<PluginRegistry>().0;
if let Some(entry) = plugins.iter_mut().find(|(id, _)| *id == TypeId::of::<V>()) {
entry.1 = factory;
} else {
plugins.push((TypeId::of::<V>(), factory));
}
}
pub fn new(view: impl Into<AnyView>, window: &mut Window, cx: &mut Context<Self>) -> Self {
#[cfg(all(target_os = "macos", not(test)))]
crate::install_window_hit_test_forwarder(window);
let factories = cx
.try_global::<PluginRegistry>()
.map(|e| e.0.clone())
.unwrap_or_default();
Self {
view: view.into(),
style: StyleRefinement::default(),
plugins: factories
.into_iter()
.map(|(_, build)| build(window, cx))
.collect(),
}
}
/// The original application content entity.
pub fn view(&self) -> &AnyView {
&self.view
}
/// Find a presentation plugin owned by this window.
pub fn plugin<V: RootPlugin>(&self) -> Option<Entity<V>> {
self.plugins
.iter()
.find_map(|entry| entry.view.clone().downcast::<V>().ok())
}
pub fn read<'a>(window: &'a Window, cx: &'a App) -> &'a Self {
window
.root::<Self>()
.flatten()
.expect("window must have a Base Root")
.read(cx)
}
pub fn update<R>(
window: &mut Window,
cx: &mut App,
f: impl FnOnce(&mut Self, &mut Window, &mut Context<Self>) -> R,
) -> R {
let root = window
.root::<Self>()
.flatten()
.expect("window must have a Base Root");
root.update(cx, |root, cx| f(root, window, cx))
}
fn on_action_tab(&mut self, _: &Tab, window: &mut Window, cx: &mut Context<Self>) {
// Check if we're inside a focus trap
if let Some(container_focus_handle) = crate::active_focus_trap(window, cx) {
// We're in a focus trap - try to focus next, then check if we're still inside
let before_focus = window.focused(cx);
// Try normal focus navigation
window.focus_next(cx);
// Check if we're still in the trap
if !container_focus_handle.contains_focused(window, cx) {
// We jumped out of the trap - need to cycle back to the beginning
// Find the first focusable element in the trap by continuing to focus_next
let mut attempts = 0;
const MAX_ATTEMPTS: usize = 100; // Prevent infinite loop
while !container_focus_handle.contains_focused(window, cx)
&& attempts < MAX_ATTEMPTS
{
window.focus_next(cx);
attempts += 1;
// If we cycled back to where we started, restore original focus
if window.focused(cx) == before_focus {
break;
}
}
}
return;
}
// Normal tab navigation
window.focus_next(cx);
}
fn on_action_tab_prev(&mut self, _: &TabPrev, window: &mut Window, cx: &mut Context<Self>) {
// Check if we're inside a focus trap
if let Some(container_focus_handle) = crate::active_focus_trap(window, cx) {
// We're in a focus trap - try to focus previous, then check if we're still inside
let before_focus = window.focused(cx);
// Try normal focus navigation
window.focus_prev(cx);
// Check if we're still in the trap
if !container_focus_handle.contains_focused(window, cx) {
// We jumped out of the trap - need to cycle back to the end
// Find the last focusable element in the trap by continuing to focus_prev
let mut attempts = 0;
const MAX_ATTEMPTS: usize = 100; // Prevent infinite loop
while !container_focus_handle.contains_focused(window, cx)
&& attempts < MAX_ATTEMPTS
{
window.focus_prev(cx);
attempts += 1;
// If we cycled back to where we started, restore original focus
if window.focused(cx) == before_focus {
break;
}
}
}
return;
}
// Normal tab navigation
window.focus_prev(cx);
}
fn on_action_copy(&mut self, _: &Copy, window: &mut Window, cx: &mut Context<Self>) {
let text = crate::TextSelection::selected_text(window, cx)
.trim()
.to_string();
if text.is_empty() {
cx.propagate();
return;
}
cx.write_to_clipboard(ClipboardItem::new_string(text));
}
}
impl Styled for Root {
fn style(&mut self) -> &mut StyleRefinement {
&mut self.style
}
}
impl Render for Root {
fn render(&mut self, window: &mut Window, cx: &mut Context<Self>) -> impl IntoElement {
for plugin in &self.plugins {
(plugin.prepare)(window, cx);
}
let mut content = div()
.id("root")
.key_context(CONTEXT)
.on_action(cx.listener(Self::on_action_tab))
.on_action(cx.listener(Self::on_action_tab_prev))
.on_action(cx.listener(Self::on_action_copy))
.relative()
.size_full()
.child(TextSelectionLayer)
.child(self.view.clone())
.child(
div()
.absolute()
.inset_0()
.children(self.plugins.iter().map(|plugin| plugin.view.clone())),
);
for plugin in &self.plugins {
(plugin.style)(&mut content, window, cx);
}
let mut content = content.refine_style(&self.style).into_any_element();
for plugin in &self.plugins {
content = (plugin.decorate)(content, self, window, cx);
}
content
}
}
#[cfg(test)]
mod tests {
use super::*;
use gpui::TestAppContext;
struct Content;
impl Render for Content {
fn render(&mut self, _: &mut Window, _: &mut Context<Self>) -> impl IntoElement {
div()
}
}
struct Layer;
impl Render for Layer {
fn render(&mut self, _: &mut Window, _: &mut Context<Self>) -> impl IntoElement {
div()
}
}
impl RootPlugin for Layer {}
fn layer(_: &mut Window, _: &mut Context<Layer>) -> Layer {
Layer
}
#[gpui::test]
fn plugin_registration_is_idempotent_and_state_is_per_window(cx: &mut TestAppContext) {
cx.update(|cx| {
crate::init(cx);
Root::register_plugin(cx, layer);
Root::register_plugin(cx, layer);
});
let mut ids = Vec::new();
for _ in 0..2 {
let (root, _) = cx.add_window_view(|window, cx| {
let content = cx.new(|_| Content);
Root::new(content, window, cx)
});
let id = root.read_with(cx, |root, _| {
assert_eq!(root.plugins.len(), 1);
root.plugin::<Layer>().unwrap().entity_id()
});
ids.push(id);
}
assert_ne!(ids[0], ids[1]);
}
}