tauri-plugin-gpui 0.1.1

Render GPUI views inside ordinary Tauri windows
Documentation
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
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
//! Android platform layer.
//!
//! Tauri's Android activity has no native surface of its own, so the plugin
//! installs `GpuiView` (a `SurfaceView`, see `android/`) as the activity's
//! content and renders into its surface. The view's callbacks run on the
//! Android UI thread, while TAO, and with it GPUI, runs on a thread of its own.
//! Every callback therefore becomes a [`ViewEvent`] queued for the event-loop
//! thread, which drains the queue in [`crate::runtime::Runtime::drain`].

pub(crate) mod keys;
pub(crate) mod selection;

use std::{
    collections::VecDeque,
    ffi::c_void,
    sync::{Arc, Condvar, Mutex, OnceLock, PoisonError},
    time::Duration,
};

use gpui::{Pixels, Point, point, px};
use jni::{
    JNIEnv, JavaVM, NativeMethod,
    objects::{GlobalRef, JClass, JObject, JString, JValue},
    sys::{jboolean, jfloat, jint},
};
use ndk::native_window::NativeWindow;

use crate::ffi::{self, lock};
use crate::platform::dispatcher::LoopWaker;

/// How long `surfaceDestroyed` waits for the renderer to release the surface.
const SURFACE_RELEASE_TIMEOUT: Duration = Duration::from_secs(2);

pub(crate) enum ViewEvent {
    SurfaceChanged {
        window: NativeWindow,
        width: u32,
        height: u32,
        density: f32,
    },
    SurfaceDestroyed(Arc<Released>),
    Touch {
        phase: i32,
        id: i32,
        x: f32,
        y: f32,
    },
    Key {
        down: bool,
        key_code: i32,
        unicode: i32,
        meta: i32,
        repeat: bool,
    },
    CommitText(String),
    ComposingText(String),
    FinishComposing,
    DeleteSurrounding {
        before: usize,
        after: usize,
    },
    /// System bar and keyboard regions, in physical pixels.
    Insets {
        left: i32,
        top: i32,
        right: i32,
        bottom: i32,
        ime_bottom: i32,
    },
    Back,
    Lifecycle {
        active: bool,
    },
    Appearance {
        dark: bool,
    },
    /// A long press, in view pixels (Android's own detection, so it also
    /// covers inputs GPUI draws itself).
    LongPress {
        x: f32,
        y: f32,
    },
    /// An item chosen in the native selection toolbar.
    EditAction(keys::EditAction),
    /// Text from Android Autofill for the focused input.
    Autofill(String),
    /// A native selection handle dragged. `position` (logical pixels) is where
    /// the handle wants the caret, already aimed at the text line.
    HandleDrag {
        handle: selection::Handle,
        phase: selection::DragPhase,
        position: Point<Pixels>,
    },
}

/// Signalled once the renderer no longer references a destroyed surface.
#[derive(Default)]
pub(crate) struct Released {
    done: Mutex<bool>,
    condvar: Condvar,
}

impl Released {
    pub(crate) fn signal(&self) {
        *lock(&self.done) = true;
        self.condvar.notify_all();
    }

    fn wait(&self) {
        let done = lock(&self.done);
        let (_done, timeout) = self
            .condvar
            .wait_timeout_while(done, SURFACE_RELEASE_TIMEOUT, |done| !*done)
            .unwrap_or_else(PoisonError::into_inner);
        if timeout.timed_out() {
            log::warn!(
                "tauri-plugin-gpui: timed out waiting for the renderer to release the surface"
            );
        }
    }
}

#[derive(Default)]
struct Queue {
    events: VecDeque<ViewEvent>,
    waker: Option<Arc<LoopWaker>>,
}

static QUEUE: Mutex<Queue> = Mutex::new(Queue {
    events: VecDeque::new(),
    waker: None,
});
static VM: OnceLock<JavaVM> = OnceLock::new();
static VIEW: Mutex<Option<GlobalRef>> = Mutex::new(None);

/// Display density (physical pixels per logical pixel) last reported.
static DENSITY: Mutex<f32> = Mutex::new(1.0);
static DARK: Mutex<bool> = Mutex::new(false);

fn push(event: ViewEvent) -> bool {
    let mut queue = lock(&QUEUE);
    queue.events.push_back(event);
    match &queue.waker {
        Some(waker) => {
            waker.wake();
            true
        }
        None => false,
    }
}

/// Connects the queue to the event loop once the GPUI runtime exists.
pub(crate) fn set_waker(waker: Arc<LoopWaker>) {
    let mut queue = lock(&QUEUE);
    if !queue.events.is_empty() {
        waker.wake();
    }
    queue.waker = Some(waker);
}

pub(crate) fn take_events() -> VecDeque<ViewEvent> {
    std::mem::take(&mut lock(&QUEUE).events)
}

pub(crate) fn density() -> f32 {
    *lock(&DENSITY)
}

pub(crate) fn is_dark() -> bool {
    *lock(&DARK)
}

// Calls into the view, from the event-loop thread.

fn with_view(name: &str, f: impl FnOnce(&mut JNIEnv, &JObject) -> jni::errors::Result<()>) {
    let Some(vm) = VM.get() else { return };
    let Some(view) = lock(&VIEW).clone() else {
        return;
    };
    let mut env = match vm.attach_current_thread_as_daemon() {
        Ok(env) => env,
        Err(error) => {
            log::warn!("tauri-plugin-gpui: cannot attach to the JVM for {name}: {error}");
            return;
        }
    };
    if let Err(error) = f(&mut env, view.as_obj()) {
        log::warn!("tauri-plugin-gpui: GpuiView.{name} failed: {error}");
        let _ = env.exception_describe();
        let _ = env.exception_clear();
    }
}

pub(crate) fn show_keyboard() {
    log::debug!("tauri-plugin-gpui: showing the soft keyboard");
    with_view("showKeyboard", |env, view| {
        env.call_method(view, "showKeyboard", "()V", &[]).map(drop)
    });
}

pub(crate) fn hide_keyboard() {
    log::debug!("tauri-plugin-gpui: hiding the soft keyboard");
    with_view("hideKeyboard", |env, view| {
        env.call_method(view, "hideKeyboard", "()V", &[]).map(drop)
    });
}

pub(crate) fn set_back_enabled(enabled: bool) {
    with_view("setBackEnabled", |env, view| {
        env.call_method(
            view,
            "setBackEnabled",
            "(Z)V",
            &[JValue::Bool(enabled.into())],
        )
        .map(drop)
    });
}

pub(crate) fn clipboard_text() -> Option<String> {
    let mut text = None;
    with_view("clipboardText", |env, view| {
        let value = env
            .call_method(view, "clipboardText", "()Ljava/lang/String;", &[])?
            .l()?;
        if !value.is_null() {
            text = Some(env.get_string(&JString::from(value))?.into());
        }
        Ok(())
    });
    text
}

pub(crate) fn set_clipboard_text(text: &str) {
    with_view("setClipboardText", |env, view| {
        let text = env.new_string(text)?;
        env.call_method(
            view,
            "setClipboardText",
            "(Ljava/lang/String;)V",
            &[JValue::Object(&text)],
        )
        .map(drop)
    });
}

/// Loads the app's Tauri plugins, which Tauri skips when there is no WebView.
/// They get a stand-in WebView, not a real one.
pub(crate) fn load_plugins() {
    with_view("loadPlugins", |env, view| {
        env.call_method(view, "loadPlugins", "()V", &[]).map(drop)
    });
}

/// Shows (or moves) the native selection toolbar over `rect`.
pub(crate) fn show_selection_toolbar(rect: selection::ViewRect, has_selection: bool) {
    with_view("showSelectionToolbar", |env, view| {
        env.call_method(
            view,
            "showSelectionToolbar",
            "(IIIIZ)V",
            &[
                JValue::Int(rect.left),
                JValue::Int(rect.top),
                JValue::Int(rect.right),
                JValue::Int(rect.bottom),
                JValue::Bool(has_selection.into()),
            ],
        )
        .map(drop)
    });
}

pub(crate) fn hide_selection_toolbar() {
    with_view("hideSelectionToolbar", |env, view| {
        env.call_method(view, "hideSelectionToolbar", "()V", &[])
            .map(drop)
    });
}

/// Shows (or moves) the native selection handles. `collapsed` shows only the
/// insertion handle, at the start anchor.
pub(crate) fn show_selection_handles(anchors: selection::HandleAnchors, collapsed: bool) {
    with_view("showSelectionHandles", |env, view| {
        env.call_method(
            view,
            "showSelectionHandles",
            "(FFFFFZ)V",
            &[
                JValue::Float(anchors.start_x),
                JValue::Float(anchors.start_y),
                JValue::Float(anchors.end_x),
                JValue::Float(anchors.end_y),
                JValue::Float(anchors.line_height),
                JValue::Bool(collapsed.into()),
            ],
        )
        .map(drop)
    });
}

pub(crate) fn hide_selection_handles() {
    with_view("hideSelectionHandles", |env, view| {
        env.call_method(view, "hideSelectionHandles", "()V", &[])
            .map(drop)
    });
}

/// Tells the view about the focused input (for Autofill): its text and the
/// rectangle it occupies.
pub(crate) fn set_autofill_input(text: &str, rect: selection::ViewRect) {
    with_view("setAutofillInput", |env, view| {
        let text = env.new_string(text)?;
        env.call_method(
            view,
            "setAutofillInput",
            "(Ljava/lang/String;IIII)V",
            &[
                JValue::Object(&text),
                JValue::Int(rect.left),
                JValue::Int(rect.top),
                JValue::Int(rect.right),
                JValue::Int(rect.bottom),
            ],
        )
        .map(drop)
    });
}

pub(crate) fn clear_autofill_input() {
    with_view("clearAutofillInput", |env, view| {
        env.call_method(view, "clearAutofillInput", "()V", &[])
            .map(drop)
    });
}

// Registration.

/// Registers the `GpuiView` natives. Runs on the UI thread before Tauri
/// instantiates `GpuiPlugin`, which creates the view.
pub(crate) fn register_natives(env: &mut JNIEnv, activity: &JObject) -> jni::errors::Result<()> {
    let name = env.new_string("app.tauri.gpui.GpuiView")?;
    // WryActivity.getAppClass resolves through the app's class loader; a
    // plain FindClass would use the system one.
    let class = env
        .call_method(
            activity,
            "getAppClass",
            "(Ljava/lang/String;)Ljava/lang/Class;",
            &[JValue::Object(&name)],
        )?
        .l()?;
    let class = JClass::from(class);
    let method = |name: &str, sig: &str, fn_ptr: *mut c_void| NativeMethod {
        name: name.into(),
        sig: sig.into(),
        fn_ptr,
    };
    env.register_native_methods(
        &class,
        &[
            method(
                "nativeAttach",
                "(Lapp/tauri/gpui/GpuiView;F)V",
                native_attach as *mut c_void,
            ),
            method(
                "nativeSurfaceChanged",
                "(Landroid/view/Surface;IIF)V",
                native_surface_changed as *mut c_void,
            ),
            method(
                "nativeSurfaceDestroyed",
                "()V",
                native_surface_destroyed as *mut c_void,
            ),
            method("nativeTouch", "(IIFF)V", native_touch as *mut c_void),
            method("nativeKey", "(ZIIII)V", native_key as *mut c_void),
            method(
                "nativeCommitText",
                "(Ljava/lang/String;)V",
                native_commit_text as *mut c_void,
            ),
            method(
                "nativeSetComposingText",
                "(Ljava/lang/String;)V",
                native_set_composing_text as *mut c_void,
            ),
            method(
                "nativeFinishComposingText",
                "()V",
                native_finish_composing_text as *mut c_void,
            ),
            method(
                "nativeDeleteSurroundingText",
                "(II)V",
                native_delete_surrounding_text as *mut c_void,
            ),
            method("nativeInsets", "(IIIII)V", native_insets as *mut c_void),
            method("nativeBack", "()V", native_back as *mut c_void),
            method("nativeLifecycle", "(I)V", native_lifecycle as *mut c_void),
            method("nativeAppearance", "(Z)V", native_appearance as *mut c_void),
            method("nativeLongPress", "(FF)V", native_long_press as *mut c_void),
            method(
                "nativeEditAction",
                "(I)V",
                native_edit_action as *mut c_void,
            ),
            method(
                "nativeAutofill",
                "(Ljava/lang/String;)V",
                native_autofill as *mut c_void,
            ),
            method(
                "nativeHandleDrag",
                "(IIFF)V",
                native_handle_drag as *mut c_void,
            ),
        ],
    )
}

// Natives. All run on the Android UI thread.

extern "system" fn native_attach(env: JNIEnv, _: JClass, view: JObject, density: jfloat) {
    ffi::callback("native_attach", (), || {
        if let Ok(vm) = env.get_java_vm() {
            let _ = VM.set(vm);
        }
        match env.new_global_ref(view) {
            Ok(view) => *lock(&VIEW) = Some(view),
            Err(error) => {
                log::error!("tauri-plugin-gpui: cannot keep a reference to GpuiView: {error}")
            }
        }
        *lock(&DENSITY) = density;
    })
}

extern "system" fn native_surface_changed(
    env: JNIEnv,
    _: JClass,
    surface: JObject,
    width: jint,
    height: jint,
    density: jfloat,
) {
    ffi::callback("native_surface_changed", (), || {
        // SAFETY: `surface` is a live android.view.Surface for this call.
        let window = unsafe { NativeWindow::from_surface(env.get_raw(), surface.as_raw()) };
        let Some(window) = window else {
            log::error!("tauri-plugin-gpui: ANativeWindow_fromSurface returned null");
            return;
        };
        *lock(&DENSITY) = density;
        push(ViewEvent::SurfaceChanged {
            window,
            width: width.max(1) as u32,
            height: height.max(1) as u32,
            density,
        });
    })
}

extern "system" fn native_surface_destroyed(_: JNIEnv, _: JClass) {
    ffi::callback("native_surface_destroyed", (), || {
        let released = Arc::new(Released::default());
        if push(ViewEvent::SurfaceDestroyed(released.clone())) {
            released.wait();
        }
    })
}

extern "system" fn native_touch(_: JNIEnv, _: JClass, phase: jint, id: jint, x: jfloat, y: jfloat) {
    ffi::callback("native_touch", (), || {
        push(ViewEvent::Touch { phase, id, x, y });
    })
}

extern "system" fn native_key(
    _: JNIEnv,
    _: JClass,
    down: jboolean,
    key_code: jint,
    unicode: jint,
    meta: jint,
    repeat: jint,
) {
    ffi::callback("native_key", (), || {
        push(ViewEvent::Key {
            down: down != 0,
            key_code,
            unicode,
            meta,
            repeat: repeat > 0,
        });
    })
}

fn string(env: &mut JNIEnv, text: &JString) -> Option<String> {
    env.get_string(text).ok().map(Into::into)
}

extern "system" fn native_commit_text(mut env: JNIEnv, _: JClass, text: JString) {
    ffi::callback("native_commit_text", (), || {
        if let Some(text) = string(&mut env, &text) {
            push(ViewEvent::CommitText(text));
        }
    })
}

extern "system" fn native_set_composing_text(mut env: JNIEnv, _: JClass, text: JString) {
    ffi::callback("native_set_composing_text", (), || {
        if let Some(text) = string(&mut env, &text) {
            push(ViewEvent::ComposingText(text));
        }
    })
}

extern "system" fn native_finish_composing_text(_: JNIEnv, _: JClass) {
    ffi::callback("native_finish_composing_text", (), || {
        push(ViewEvent::FinishComposing);
    })
}

extern "system" fn native_delete_surrounding_text(_: JNIEnv, _: JClass, before: jint, after: jint) {
    ffi::callback("native_delete_surrounding_text", (), || {
        push(ViewEvent::DeleteSurrounding {
            before: before.max(0) as usize,
            after: after.max(0) as usize,
        });
    })
}

extern "system" fn native_insets(
    _: JNIEnv,
    _: JClass,
    left: jint,
    top: jint,
    right: jint,
    bottom: jint,
    ime_bottom: jint,
) {
    ffi::callback("native_insets", (), || {
        push(ViewEvent::Insets {
            left,
            top,
            right,
            bottom,
            ime_bottom,
        });
    })
}

extern "system" fn native_back(_: JNIEnv, _: JClass) {
    ffi::callback("native_back", (), || {
        push(ViewEvent::Back);
    })
}

extern "system" fn native_lifecycle(_: JNIEnv, _: JClass, phase: jint) {
    ffi::callback("native_lifecycle", (), || {
        push(ViewEvent::Lifecycle { active: phase == 0 });
    })
}

extern "system" fn native_appearance(_: JNIEnv, _: JClass, dark: jboolean) {
    ffi::callback("native_appearance", (), || {
        *lock(&DARK) = dark != 0;
        push(ViewEvent::Appearance { dark: dark != 0 });
    })
}

extern "system" fn native_long_press(_: JNIEnv, _: JClass, x: jfloat, y: jfloat) {
    ffi::callback("native_long_press", (), || {
        push(ViewEvent::LongPress { x, y });
    })
}

extern "system" fn native_edit_action(_: JNIEnv, _: JClass, action: jint) {
    ffi::callback("native_edit_action", (), || {
        if let Some(action) = keys::EditAction::from_code(action) {
            push(ViewEvent::EditAction(action));
        }
    })
}

extern "system" fn native_autofill(mut env: JNIEnv, _: JClass, text: JString) {
    ffi::callback("native_autofill", (), || {
        if let Some(text) = string(&mut env, &text) {
            push(ViewEvent::Autofill(text));
        }
    })
}

extern "system" fn native_handle_drag(
    _: JNIEnv,
    _: JClass,
    handle: jint,
    phase: jint,
    x: jfloat,
    y: jfloat,
) {
    ffi::callback("native_handle_drag", (), || {
        let (Some(handle), Some(phase)) = (
            selection::Handle::from_code(handle),
            selection::DragPhase::from_code(phase),
        ) else {
            return;
        };
        let scale = density().max(0.1);
        push(ViewEvent::HandleDrag {
            handle,
            phase,
            position: point(px(x / scale), px(y / scale)),
        });
    })
}