cranpose 0.1.70

Cranpose runtime and UI facade
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
//! Android IME bridge: real `InputConnection` support for text fields.
//!
//! A `NativeActivity` has no Java view overriding `onCreateInputConnection`,
//! so IMEs run in fallback mode and can only deliver plain key events (the
//! [`crate::android_keyboard`] `KeyCharacterMap` path). This module drives the
//! `dev.cranpose.android.CranposeTextInput` Java helper, which attaches an
//! invisible focusable view to the activity and returns a
//! `BaseInputConnection` subclass when the soft keyboard opens. Editing
//! operations flow back through the JNI callbacks below into an event queue
//! drained by the Android main loop:
//!
//! - `commitText` → [`AndroidImeEvent::CommitText`] → `AppShell::on_paste`
//! - `setComposingText` → [`AndroidImeEvent::SetComposingText`] →
//!   `AppShell::on_ime_preedit`
//! - `setComposingRegion` → [`AndroidImeEvent::SetComposingRegion`] →
//!   `AppShell::on_ime_set_composing_region`
//! - `finishComposingText` → [`AndroidImeEvent::FinishComposing`] →
//!   `AppShell::on_ime_finish_composing`
//! - `deleteSurroundingText` → [`AndroidImeEvent::DeleteSurrounding`] →
//!   `AppShell::on_ime_delete_surrounding`
//! - `sendKeyEvent` → [`AndroidImeEvent::Key`] → `AppShell::on_key_event`
//! - `performEditorAction` → [`AndroidImeEvent::EditorAction`]
//!
//! The Java side keeps a UTF-16 `Editable` mirror so IME text queries are
//! answered synchronously; the Rust text field remains the source of truth
//! and pushes its state back through [`update_android_text_input_state`]
//! (selection updates, or an input-session restart when the contents
//! diverged). All boundary offsets are converted at this JNI layer: Rust
//! works in UTF-8 bytes, Java in UTF-16 code units.
#![allow(unsafe_code)]

use crate::android_jni::{
    clear_pending_android_jni_exception, load_cranpose_java_class, with_android_activity_env,
};
use android_activity::AndroidAppWaker;
use cranpose_app_shell::ImeEditorState;
use jni::{
    jni_sig, jni_str,
    objects::{JClass, JObject, JString, JValue},
    sys::{jint, jlong},
    EnvUnowned, Outcome,
};
use std::sync::{Arc, Mutex, MutexGuard, OnceLock};

const TEXT_INPUT_CLASS: &str = "dev/cranpose/android/CranposeTextInput";

/// One editing operation forwarded from the Java `InputConnection`.
#[derive(Clone, Debug, PartialEq, Eq)]
pub(crate) enum AndroidImeEvent {
    /// `commitText`: replace the composing text (if any) with `text` and move
    /// the cursor after it. `new_cursor_position` follows the Android
    /// contract; values other than 1 are currently treated as 1 (cursor
    /// after the committed text) and self-correct through the state sync.
    CommitText {
        text: String,
        new_cursor_position: i32,
    },
    /// `setComposingText`: replace the current preedit with `text`;
    /// `cursor_bytes` is the caret offset inside `text` in UTF-8 bytes.
    SetComposingText { text: String, cursor_bytes: usize },
    /// `setComposingRegion`: mark existing text as composing (UTF-8 bytes).
    SetComposingRegion {
        start_bytes: usize,
        end_bytes: usize,
    },
    /// `setSelection`: move the selection/caret without editing text (UTF-8
    /// bytes). Gboard's spacebar-swipe cursor control scrubs the caret this way.
    SetSelection {
        start_bytes: usize,
        end_bytes: usize,
    },
    /// `finishComposingText`: keep the composed text, clear the region.
    FinishComposing,
    /// `deleteSurroundingText`, already converted to UTF-8 byte counts.
    DeleteSurrounding {
        before_bytes: usize,
        after_bytes: usize,
    },
    /// `sendKeyEvent`: editing keys some IMEs deliver as key events
    /// (backspace, enter). Raw Android values; translated by
    /// [`crate::android_keyboard::ime_key_event`].
    Key {
        action: i32,
        key_code: i32,
        meta_state: i32,
        unicode_char: i32,
    },
    /// `performEditorAction` with an `EditorInfo.IME_ACTION_*` code.
    EditorAction { action: i32 },
    /// The on-screen keyboard's height (physical px) at the bottom of the
    /// window changed. `0` when the keyboard is hidden. Forwarded from a Java
    /// `View.OnApplyWindowInsetsListener` / visible-frame listener so the shell
    /// can expose it as IME insets to composition.
    ImeInsetsChanged { bottom_px: i32 },
}

/// Cross-thread queue for IME events (pushed on the Android UI thread,
/// drained by the native main loop). Pushes wake the main-loop looper so
/// events are handled without waiting for the next frame.
pub(crate) struct AndroidImeEventQueue {
    events: Mutex<Vec<AndroidImeEvent>>,
    waker: OnceLock<AndroidAppWaker>,
}

impl AndroidImeEventQueue {
    pub(crate) fn new() -> Self {
        Self {
            events: Mutex::new(Vec::new()),
            waker: OnceLock::new(),
        }
    }

    /// Installs the main-loop waker. Events pushed before this are delivered
    /// on the next natural poll.
    pub(crate) fn set_waker(&self, waker: AndroidAppWaker) {
        let _ = self.waker.set(waker);
    }

    pub(crate) fn push(&self, event: AndroidImeEvent) {
        self.lock_events().push(event);
        if let Some(waker) = self.waker.get() {
            waker.wake();
        }
    }

    pub(crate) fn drain(&self) -> Vec<AndroidImeEvent> {
        std::mem::take(&mut *self.lock_events())
    }

    fn lock_events(&self) -> MutexGuard<'_, Vec<AndroidImeEvent>> {
        self.events
            .lock()
            .unwrap_or_else(|poisoned| poisoned.into_inner())
    }
}

/// Opaque `jlong` wrapper over a retained `Arc<AndroidImeEventQueue>`.
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
pub(crate) struct AndroidImeQueueHandle(jlong);

impl AndroidImeQueueHandle {
    fn raw(self) -> jlong {
        self.0
    }
}

fn retain_ime_queue_handle(queue: &Arc<AndroidImeEventQueue>) -> AndroidImeQueueHandle {
    let pointer = Arc::into_raw(Arc::clone(queue));
    AndroidImeQueueHandle(pointer as usize as jlong)
}

fn release_ime_queue_handle_raw(handle: jlong) {
    if handle == 0 {
        return;
    }
    // SAFETY: handles are created by `retain_ime_queue_handle` (Arc::into_raw)
    // and released exactly once: by the Java helper when the session is
    // disposed, or by Rust when `show` failed to transfer ownership.
    unsafe {
        drop(Arc::from_raw(
            handle as usize as *const AndroidImeEventQueue,
        ));
    }
}

fn push_ime_event_for_handle(handle: jlong, event: AndroidImeEvent) {
    if handle == 0 {
        log::warn!("dropped Android IME event because the Java callback carried no queue handle");
        return;
    }
    // SAFETY: the Java helper stores a handle retained from an
    // Arc<AndroidImeEventQueue> and releases it only when the session is
    // disposed on the Android UI thread; callbacks check `disposed` first.
    let Some(queue) = (unsafe { (handle as usize as *const AndroidImeEventQueue).as_ref() }) else {
        log::warn!("dropped Android IME event because the queue handle was invalid");
        return;
    };
    queue.push(event);
}

/// Converts a UTF-8 byte offset into `text` to UTF-16 code units, clamping
/// into the valid range (and down to a character boundary).
fn utf16_offset(text: &str, byte_offset: usize) -> i32 {
    let mut offset = byte_offset.min(text.len());
    while offset > 0 && !text.is_char_boundary(offset) {
        offset -= 1;
    }
    text[..offset].encode_utf16().count() as i32
}

/// Opens (or reseeds) the Java text-input session for the focused field.
///
/// Transfers one retained queue handle to Java on success; on failure the
/// handle is released here. Errors are returned so the caller can fall back
/// to the plain `show_soft_input` path (for apps that do not ship the
/// `cranpose/android/java` sources).
pub(crate) fn show_android_text_input(
    app: &android_activity::AndroidApp,
    queue: &Arc<AndroidImeEventQueue>,
    state: &ImeEditorState,
) -> Result<(), String> {
    let handle = retain_ime_queue_handle(queue);
    let sel_start = utf16_offset(&state.text, state.selection_start);
    let sel_end = utf16_offset(&state.text, state.selection_end);

    let result = with_android_activity_env(app, |env, activity| {
        let class = load_cranpose_java_class(env, &activity, TEXT_INPUT_CLASS)?;
        let text = env.new_string(&state.text).map_err(|error| {
            clear_pending_android_jni_exception(env);
            format!("failed to create Android IME text string: {error}")
        })?;
        let text = JObject::from(text);
        env.call_static_method(
            class,
            jni_str!("show"),
            jni_sig!("(Landroid/app/Activity;JLjava/lang/String;IIZ)I"),
            &[
                JValue::Object(&activity),
                JValue::Long(handle.raw()),
                JValue::Object(&text),
                JValue::Int(sel_start),
                JValue::Int(sel_end),
                JValue::Bool(state.single_line),
            ],
        )
        .and_then(|value| value.i())
        .map_err(|error| {
            clear_pending_android_jni_exception(env);
            format!("failed to show Android text input: {error}")
        })
    });

    match result {
        Ok(0) => Ok(()),
        Ok(code) => {
            release_ime_queue_handle_raw(handle.raw());
            Err(format!("Android text input helper returned {code}"))
        }
        Err(error) => {
            release_ime_queue_handle_raw(handle.raw());
            Err(error)
        }
    }
}

/// Closes the Java text-input session (hides the IME, detaches the editor
/// view, releases the queue handle owned by the session).
pub(crate) fn hide_android_text_input(app: &android_activity::AndroidApp) -> Result<(), String> {
    with_android_activity_env(app, |env, activity| {
        let class = load_cranpose_java_class(env, &activity, TEXT_INPUT_CLASS)?;
        env.call_static_method(
            class,
            jni_str!("hide"),
            jni_sig!("(Landroid/app/Activity;)V"),
            &[JValue::Object(&activity)],
        )
        .map_err(|error| {
            clear_pending_android_jni_exception(env);
            format!("failed to hide Android text input: {error}")
        })?;
        Ok(())
    })
}

/// Pushes the Rust-side editor state into the Java mirror. Java refreshes the
/// IME's selection view, or restarts the input session when the text content
/// diverged from the mirror.
pub(crate) fn update_android_text_input_state(
    app: &android_activity::AndroidApp,
    state: &ImeEditorState,
) -> Result<(), String> {
    let sel_start = utf16_offset(&state.text, state.selection_start);
    let sel_end = utf16_offset(&state.text, state.selection_end);
    let (comp_start, comp_end) = match state.composition {
        Some((start, end)) => (
            utf16_offset(&state.text, start),
            utf16_offset(&state.text, end),
        ),
        None => (-1, -1),
    };

    with_android_activity_env(app, |env, activity| {
        let class = load_cranpose_java_class(env, &activity, TEXT_INPUT_CLASS)?;
        let text = env.new_string(&state.text).map_err(|error| {
            clear_pending_android_jni_exception(env);
            format!("failed to create Android IME text string: {error}")
        })?;
        let text = JObject::from(text);
        env.call_static_method(
            class,
            jni_str!("updateState"),
            jni_sig!("(Landroid/app/Activity;Ljava/lang/String;IIII)V"),
            &[
                JValue::Object(&activity),
                JValue::Object(&text),
                JValue::Int(sel_start),
                JValue::Int(sel_end),
                JValue::Int(comp_start),
                JValue::Int(comp_end),
            ],
        )
        .map_err(|error| {
            clear_pending_android_jni_exception(env);
            format!("failed to update Android text input state: {error}")
        })?;
        Ok(())
    })
}

// ---------------------------------------------------------------------------
// JNI callbacks from dev.cranpose.android.CranposeTextInput (UI thread)
// ---------------------------------------------------------------------------

fn jstring_to_string(env: &mut EnvUnowned<'_>, text: JString<'_>) -> Option<String> {
    match env
        .with_env(|env| -> jni::errors::Result<String> { text.try_to_string(env) })
        .into_outcome()
    {
        Outcome::Ok(text) => Some(text),
        Outcome::Err(_) | Outcome::Panic(_) => {
            log::warn!("failed to read Android IME text from JNI");
            None
        }
    }
}

fn non_negative(value: jint) -> usize {
    value.max(0) as usize
}

#[doc(hidden)]
#[no_mangle]
pub extern "system" fn Java_dev_cranpose_android_CranposeTextInput_nativeImeCommitText<'local>(
    mut env: EnvUnowned<'local>,
    _class: JClass<'local>,
    queue_handle: jlong,
    text: JString<'local>,
    new_cursor_position: jint,
) {
    let Some(text) = jstring_to_string(&mut env, text) else {
        return;
    };
    push_ime_event_for_handle(
        queue_handle,
        AndroidImeEvent::CommitText {
            text,
            new_cursor_position,
        },
    );
}

#[doc(hidden)]
#[no_mangle]
pub extern "system" fn Java_dev_cranpose_android_CranposeTextInput_nativeImeSetComposingText<
    'local,
>(
    mut env: EnvUnowned<'local>,
    _class: JClass<'local>,
    queue_handle: jlong,
    text: JString<'local>,
    cursor_bytes: jint,
) {
    let Some(text) = jstring_to_string(&mut env, text) else {
        return;
    };
    push_ime_event_for_handle(
        queue_handle,
        AndroidImeEvent::SetComposingText {
            text,
            cursor_bytes: non_negative(cursor_bytes),
        },
    );
}

#[doc(hidden)]
#[no_mangle]
pub extern "system" fn Java_dev_cranpose_android_CranposeTextInput_nativeImeSetComposingRegion(
    _env: EnvUnowned<'_>,
    _class: JClass<'_>,
    queue_handle: jlong,
    start_bytes: jint,
    end_bytes: jint,
) {
    push_ime_event_for_handle(
        queue_handle,
        AndroidImeEvent::SetComposingRegion {
            start_bytes: non_negative(start_bytes),
            end_bytes: non_negative(end_bytes),
        },
    );
}

#[doc(hidden)]
#[no_mangle]
pub extern "system" fn Java_dev_cranpose_android_CranposeTextInput_nativeImeSetSelection(
    _env: EnvUnowned<'_>,
    _class: JClass<'_>,
    queue_handle: jlong,
    start_bytes: jint,
    end_bytes: jint,
) {
    push_ime_event_for_handle(
        queue_handle,
        AndroidImeEvent::SetSelection {
            start_bytes: non_negative(start_bytes),
            end_bytes: non_negative(end_bytes),
        },
    );
}

#[doc(hidden)]
#[no_mangle]
pub extern "system" fn Java_dev_cranpose_android_CranposeTextInput_nativeImeFinishComposing(
    _env: EnvUnowned<'_>,
    _class: JClass<'_>,
    queue_handle: jlong,
) {
    push_ime_event_for_handle(queue_handle, AndroidImeEvent::FinishComposing);
}

#[doc(hidden)]
#[no_mangle]
pub extern "system" fn Java_dev_cranpose_android_CranposeTextInput_nativeImeDeleteSurrounding(
    _env: EnvUnowned<'_>,
    _class: JClass<'_>,
    queue_handle: jlong,
    before_bytes: jint,
    after_bytes: jint,
) {
    push_ime_event_for_handle(
        queue_handle,
        AndroidImeEvent::DeleteSurrounding {
            before_bytes: non_negative(before_bytes),
            after_bytes: non_negative(after_bytes),
        },
    );
}

#[doc(hidden)]
#[no_mangle]
pub extern "system" fn Java_dev_cranpose_android_CranposeTextInput_nativeImeSendKeyEvent(
    _env: EnvUnowned<'_>,
    _class: JClass<'_>,
    queue_handle: jlong,
    action: jint,
    key_code: jint,
    meta_state: jint,
    unicode_char: jint,
) {
    push_ime_event_for_handle(
        queue_handle,
        AndroidImeEvent::Key {
            action,
            key_code,
            meta_state,
            unicode_char,
        },
    );
}

#[doc(hidden)]
#[no_mangle]
pub extern "system" fn Java_dev_cranpose_android_CranposeTextInput_nativeImePerformEditorAction(
    _env: EnvUnowned<'_>,
    _class: JClass<'_>,
    queue_handle: jlong,
    action: jint,
) {
    push_ime_event_for_handle(queue_handle, AndroidImeEvent::EditorAction { action });
}

#[doc(hidden)]
#[no_mangle]
pub extern "system" fn Java_dev_cranpose_android_CranposeTextInput_nativeImeInsetsChanged(
    _env: EnvUnowned<'_>,
    _class: JClass<'_>,
    queue_handle: jlong,
    bottom_px: jint,
) {
    push_ime_event_for_handle(
        queue_handle,
        AndroidImeEvent::ImeInsetsChanged { bottom_px },
    );
}

#[doc(hidden)]
#[no_mangle]
pub extern "system" fn Java_dev_cranpose_android_CranposeTextInput_nativeImeReleaseQueue(
    _env: EnvUnowned<'_>,
    _class: JClass<'_>,
    queue_handle: jlong,
) {
    release_ime_queue_handle_raw(queue_handle);
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn utf16_offset_converts_and_clamps() {
        // "aé漢x" - 'a'=1 byte/1 unit, 'é'=2 bytes/1 unit, '漢'=3 bytes/1 unit.
        let text = "a\u{00e9}\u{6f22}x";
        assert_eq!(utf16_offset(text, 0), 0);
        assert_eq!(utf16_offset(text, 1), 1);
        assert_eq!(utf16_offset(text, 3), 2);
        assert_eq!(utf16_offset(text, 6), 3);
        assert_eq!(utf16_offset(text, 7), 4);
        // Past the end clamps to the full length.
        assert_eq!(utf16_offset(text, 100), 4);
        // Mid-character offsets clamp down to the previous boundary.
        assert_eq!(utf16_offset(text, 2), 1);
        assert_eq!(utf16_offset(text, 4), 2);
        // Supplementary-plane characters count as two UTF-16 units.
        let emoji = "\u{1f600}b";
        assert_eq!(utf16_offset(emoji, 4), 2);
        assert_eq!(utf16_offset(emoji, 5), 3);
    }

    #[test]
    fn queue_push_and_drain() {
        let queue = AndroidImeEventQueue::new();
        queue.push(AndroidImeEvent::FinishComposing);
        queue.push(AndroidImeEvent::CommitText {
            text: "hi".to_string(),
            new_cursor_position: 1,
        });
        let events = queue.drain();
        assert_eq!(events.len(), 2);
        assert_eq!(events[0], AndroidImeEvent::FinishComposing);
        assert!(queue.drain().is_empty());
    }
}