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
/// Defines `Terminal` using the caller's GPUI crate or module path.
///
/// Invoke once per module: `gpui_libghostty::bind_gpui!(gpui);`.
/// The selected GPUI must implement the API described in the README.
/// Re-export the generated type to share it across your application.
#[macro_export]
macro_rules! bind_gpui {
($gpui:path) => {
pub use self::__gpui_ghostty_adapter::Terminal;
use $gpui as __gpui_ghostty;
mod __gpui_ghostty_adapter {
use super::__gpui_ghostty as gpui;
use gpui::{
AppContext as _, Bounds, ClipboardItem, Context, Entity, FocusHandle,
InteractiveElement as _, IntoElement, KeyDownEvent, KeyUpEvent, MouseDownEvent,
MouseMoveEvent, MouseUpEvent, ParentElement as _, Pixels, Render, RenderImage,
ScrollDelta, ScrollWheelEvent, Styled as _, Subscription, Task, Window, canvas,
div,
};
use std::sync::Arc;
use $crate::__private::{KeyAction, Modifiers, MouseButton, MouseState, NativeSurface};
use $crate::TerminalOptions;
/// A GPUI entity backed by Ghostty's native Metal or Wayland/OpenGL surface.
pub struct Terminal {
surface: NativeSurface,
focus: FocusHandle,
bounds: Bounds<Pixels>,
tick_task: Option<Task<()>>,
visible: bool,
window_focused: bool,
_subscriptions: Vec<Subscription>,
}
impl Terminal {
/// Spawns the configured command and attaches its native surface to `window`.
pub fn spawn<T: 'static>(
options: TerminalOptions,
window: &mut Window,
cx: &mut Context<T>,
) -> Result<Entity<Self>, String> {
let focus_on_spawn = options.focus_on_spawn;
// SAFETY: GPUI owns the parent window; this entity services and drops
// its native child on the window's UI thread, as in the native adapter.
let surface = unsafe {
$crate::__private::spawn_surface(
options,
window,
f64::from(window.scale_factor()),
)?
};
let focus = cx.focus_handle();
if focus_on_spawn {
focus.focus(window, cx);
}
Ok(cx.new(|cx| {
let subscriptions = vec![
cx.on_focus(&focus, window, |terminal: &mut Self, window, _| {
terminal.sync_focus(window)
}),
cx.on_blur(&focus, window, |terminal: &mut Self, window, _| {
terminal.sync_focus(window)
}),
cx.observe_window_activation(
window,
|terminal: &mut Self, window, _| terminal.sync_focus(window),
),
];
let mut terminal = Self {
surface,
focus,
bounds: Bounds::default(),
tick_task: None,
visible: true,
window_focused: false,
_subscriptions: subscriptions,
};
terminal.sync_focus(window);
terminal
}))
}
pub fn is_alive(&self) -> bool {
self.surface.is_alive()
}
pub fn focus<T>(&mut self, window: &mut Window, cx: &mut Context<T>) {
self.set_visible(true);
self.focus.focus(window, cx);
self.sync_focus(window);
}
/// Shows or hides the native child without changing GPUI keyboard focus.
/// Hidden surfaces remain hidden across layout, resize, and scale changes.
pub fn set_visible(&mut self, visible: bool) {
self.visible = visible;
self.surface.set_visible(visible);
self.surface.set_focus(self.visible && self.window_focused);
}
fn sync_focus(&mut self, window: &Window) {
self.window_focused =
self.focus.is_focused(window) && window.is_window_active();
self.surface.set_focus(self.visible && self.window_focused);
}
/// Captures the last completed native frame for temporary GPUI compositing.
///
/// This performs a synchronous GPU readback and should only be used for
/// infrequent transitions such as presenting a modal over the terminal.
/// Render the image at the terminal's logical bounds because its pixels use
/// the native surface's display scale.
pub fn snapshot(&mut self) -> Result<Arc<RenderImage>, String> {
Ok(Arc::new(RenderImage::new([self
.surface
.snapshot_frame()?])))
}
fn start_ticking(&mut self, cx: &mut Context<Self>) {
if self.tick_task.is_some() {
return;
}
self.surface.tick();
self.service_clipboard(cx);
let wakeup = self.surface.wakeup();
let terminal = cx.entity().downgrade();
self.tick_task = Some(cx.spawn(async move |_, cx| {
loop {
wakeup.wait().await;
let updated = terminal.update(cx, |terminal, cx| {
// Ghostty draws its native child during the tick; GPUI has no
// terminal pixels to repaint for this wakeup.
terminal.surface.tick();
terminal.service_clipboard(cx);
});
if updated.is_err() {
break;
}
}
}));
}
fn service_clipboard(&mut self, cx: &mut Context<Self>) {
self.surface.service_clipboard_read(|selection| {
let item = if selection {
#[cfg(any(target_os = "linux", target_os = "freebsd"))]
{
cx.read_from_primary()
}
#[cfg(not(any(target_os = "linux", target_os = "freebsd")))]
{
cx.read_from_clipboard()
}
} else {
cx.read_from_clipboard()
};
item.and_then(|item| item.text()).unwrap_or_default()
});
while let Some(write) = self.surface.take_clipboard_write() {
let item = ClipboardItem::new_string(write.text);
if write.selection {
#[cfg(any(target_os = "linux", target_os = "freebsd"))]
cx.write_to_primary(item);
#[cfg(not(any(target_os = "linux", target_os = "freebsd")))]
cx.write_to_clipboard(item);
} else {
cx.write_to_clipboard(item);
}
}
}
fn update_frame(&mut self, bounds: Bounds<Pixels>, scale_factor: f64) {
self.bounds = bounds;
self.surface.set_frame(
f64::from(f32::from(bounds.origin.x)),
f64::from(f32::from(bounds.origin.y)),
f64::from(f32::from(bounds.size.width)),
f64::from(f32::from(bounds.size.height)),
scale_factor,
);
}
fn key_down(&mut self, event: &KeyDownEvent) {
self.send_key(
if event.is_held {
KeyAction::Repeat
} else {
KeyAction::Press
},
&event.keystroke,
);
}
fn key_up(&mut self, event: &KeyUpEvent) {
self.send_key(KeyAction::Release, &event.keystroke);
}
fn send_key(&mut self, action: KeyAction, keystroke: &gpui::Keystroke) {
self.surface.send_key(
action,
&keystroke.key,
keystroke.key_char.as_deref(),
modifiers(keystroke.modifiers),
);
}
fn mouse_position(
&mut self,
position: gpui::Point<Pixels>,
input_modifiers: gpui::Modifiers,
) {
let x = f64::from(f32::from(position.x - self.bounds.origin.x));
let y = f64::from(f32::from(position.y - self.bounds.origin.y));
self.surface
.mouse_position(x, y, modifiers(input_modifiers));
}
fn mouse_down(
&mut self,
event: &MouseDownEvent,
window: &mut Window,
cx: &mut Context<Self>,
) {
self.focus.focus(window, cx);
self.sync_focus(window);
self.mouse_position(event.position, event.modifiers);
self.surface.mouse_button(
MouseState::Press,
mouse_button(event.button),
modifiers(event.modifiers),
);
}
fn mouse_up(&mut self, event: &MouseUpEvent) {
self.mouse_position(event.position, event.modifiers);
self.surface.mouse_button(
MouseState::Release,
mouse_button(event.button),
modifiers(event.modifiers),
);
}
fn scroll(&mut self, event: &ScrollWheelEvent) {
self.mouse_position(event.position, event.modifiers);
let (x, y, precision) = match event.delta {
ScrollDelta::Pixels(delta) => (
f64::from(f32::from(delta.x)),
f64::from(f32::from(delta.y)),
true,
),
ScrollDelta::Lines(delta) => {
(f64::from(delta.x), f64::from(delta.y), false)
}
};
self.surface.mouse_scroll(x, y, precision);
}
}
impl Render for Terminal {
fn render(
&mut self,
window: &mut Window,
cx: &mut Context<Self>,
) -> impl IntoElement {
self.sync_focus(window);
self.start_ticking(cx);
let terminal = cx.entity().downgrade();
let mut element = div()
.key_context("Terminal")
.track_focus(&self.focus)
.size_full()
.min_h_0()
.child(
canvas(
move |bounds, window, cx| {
let scale_factor = f64::from(window.scale_factor());
let _ = terminal.update(cx, |terminal, _| {
terminal.update_frame(bounds, scale_factor);
});
},
|_, _, _, _| {},
)
.absolute()
.size_full(),
)
.on_key_down(cx.listener(|terminal, event, _, _| terminal.key_down(event)))
.on_key_up(cx.listener(|terminal, event, _, _| terminal.key_up(event)))
.on_mouse_move(cx.listener(|terminal, event: &MouseMoveEvent, _, _| {
terminal.mouse_position(event.position, event.modifiers);
}));
for button in [
gpui::MouseButton::Left,
gpui::MouseButton::Middle,
gpui::MouseButton::Right,
] {
element = element
.on_mouse_down(
button,
cx.listener(|terminal, event, window, cx| {
terminal.mouse_down(event, window, cx)
}),
)
.on_mouse_up(
button,
cx.listener(|terminal, event, _, _| terminal.mouse_up(event)),
);
}
element.on_scroll_wheel(
cx.listener(|terminal, event, _, _| terminal.scroll(event)),
)
}
}
fn modifiers(value: gpui::Modifiers) -> Modifiers {
let mut result = Modifiers::empty();
if value.shift {
result.insert(Modifiers::SHIFT);
}
if value.control {
result.insert(Modifiers::CONTROL);
}
if value.alt {
result.insert(Modifiers::ALT);
}
if value.platform {
result.insert(Modifiers::SUPER);
}
result
}
fn mouse_button(value: gpui::MouseButton) -> MouseButton {
match value {
gpui::MouseButton::Left => MouseButton::Left,
gpui::MouseButton::Right => MouseButton::Right,
gpui::MouseButton::Middle => MouseButton::Middle,
_ => MouseButton::Unknown,
}
}
}
};
}