cranpose 0.9.43

Build desktop, mobile and web apps with composable Rust functions
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
#![expect(unsafe_code)]

use std::sync::{Arc, Mutex, MutexGuard, OnceLock, PoisonError};

use android_activity::AndroidAppWaker;
use cranpose_app_shell::ImeEditorState;
use cranpose_ui::{ContentType, ImeAction, KeyboardCapitalization, KeyboardOptions, KeyboardType};
use jni::{
    EnvUnowned, Outcome, jni_sig, jni_str,
    objects::{JClass, JObject, JString, JValue},
    sys::{jint, jlong},
};

use crate::android_jni::{
    clear_pending_android_jni_exception, load_cranpose_java_class, with_android_activity_env,
};

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

#[derive(Clone, Debug, PartialEq, Eq)]
pub(crate) enum AndroidImeEvent {
    CommitText {
        text: String,
        new_cursor_position: i32,
    },
    SetComposingText {
        text: String,
        cursor_bytes: usize,
    },
    SetComposingRegion {
        start_bytes: usize,
        end_bytes: usize,
    },
    SetSelection {
        start_bytes: usize,
        end_bytes: usize,
    },
    FinishComposing,
    DeleteSurrounding {
        before_bytes: usize,
        after_bytes: usize,
    },
    Key {
        action: i32,
        key_code: i32,
        meta_state: i32,
        unicode_char: i32,
    },
    EditorAction {
        action: i32,
    },
    ImeInsetsChanged {
        bottom_px: i32,
    },
}

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(),
        }
    }

    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(PoisonError::into_inner)
    }
}

#[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);
}

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
}

const IME_ACTION_UNSPECIFIED: i32 = 0;
const IME_ACTION_NONE: i32 = 1;
const IME_ACTION_GO: i32 = 2;
const IME_ACTION_SEARCH: i32 = 3;
const IME_ACTION_SEND: i32 = 4;
const IME_ACTION_NEXT: i32 = 5;
const IME_ACTION_DONE: i32 = 6;
const IME_ACTION_PREVIOUS: i32 = 7;

/// The action an `InputConnection.performEditorAction` code names;
/// `IME_ACTION_UNSPECIFIED` and unknown codes stand for the field's own.
pub(crate) fn ime_action_from_android(code: i32) -> ImeAction {
    match code {
        IME_ACTION_NONE => ImeAction::None,
        IME_ACTION_GO => ImeAction::Go,
        IME_ACTION_SEARCH => ImeAction::Search,
        IME_ACTION_SEND => ImeAction::Send,
        IME_ACTION_NEXT => ImeAction::Next,
        IME_ACTION_DONE => ImeAction::Done,
        IME_ACTION_PREVIOUS => ImeAction::Previous,
        _ => ImeAction::Default,
    }
}

/// `EditorInfo.inputType` and the action part of `imeOptions` for a field's
/// keyboard, as Compose's Android text input sets them.
fn android_editor_info(options: &KeyboardOptions, single_line: bool) -> (i32, i32) {
    const TYPE_MASK_CLASS: i32 = 0xf;
    const TYPE_TEXT_FLAG_AUTO_CORRECT: i32 = 0x8000;
    const TYPE_TEXT_FLAG_MULTI_LINE: i32 = 0x20000;
    const IME_FLAG_NO_ENTER_ACTION: i32 = 0x4000_0000;

    let (mut input_type, mut ime_options) = android_input_class(options.keyboard_type);
    ime_options |= android_ime_action(options.ime_action, single_line);
    if input_type & TYPE_MASK_CLASS == TYPE_CLASS_TEXT {
        if !single_line {
            input_type |= TYPE_TEXT_FLAG_MULTI_LINE;
            if options.ime_action == ImeAction::Default {
                ime_options |= IME_FLAG_NO_ENTER_ACTION;
            }
        }
        input_type |= android_capitalization(options.capitalization);
        if options.corrects() {
            input_type |= TYPE_TEXT_FLAG_AUTO_CORRECT;
        }
    }
    (input_type, ime_options)
}

const TYPE_CLASS_TEXT: i32 = 0x1;

/// The input class and variation of a keyboard type, and the `imeOptions`
/// flags it needs.
fn android_input_class(keyboard_type: KeyboardType) -> (i32, i32) {
    const TYPE_CLASS_NUMBER: i32 = 0x2;
    const TYPE_CLASS_PHONE: i32 = 0x3;
    const TYPE_TEXT_VARIATION_URI: i32 = 0x10;
    const TYPE_TEXT_VARIATION_EMAIL_ADDRESS: i32 = 0x20;
    const TYPE_TEXT_VARIATION_PASSWORD: i32 = 0x80;
    const TYPE_NUMBER_VARIATION_PASSWORD: i32 = 0x10;
    const TYPE_NUMBER_FLAG_DECIMAL: i32 = 0x2000;
    const IME_FLAG_FORCE_ASCII: i32 = i32::MIN;

    match keyboard_type {
        KeyboardType::Text => (TYPE_CLASS_TEXT, 0),
        KeyboardType::Ascii => (TYPE_CLASS_TEXT, IME_FLAG_FORCE_ASCII),
        KeyboardType::Number => (TYPE_CLASS_NUMBER, 0),
        KeyboardType::Phone => (TYPE_CLASS_PHONE, 0),
        KeyboardType::Uri => (TYPE_CLASS_TEXT | TYPE_TEXT_VARIATION_URI, 0),
        KeyboardType::Email => (TYPE_CLASS_TEXT | TYPE_TEXT_VARIATION_EMAIL_ADDRESS, 0),
        KeyboardType::Password => (TYPE_CLASS_TEXT | TYPE_TEXT_VARIATION_PASSWORD, 0),
        KeyboardType::NumberPassword => (TYPE_CLASS_NUMBER | TYPE_NUMBER_VARIATION_PASSWORD, 0),
        KeyboardType::Decimal => (TYPE_CLASS_NUMBER | TYPE_NUMBER_FLAG_DECIMAL, 0),
    }
}

fn android_ime_action(action: ImeAction, single_line: bool) -> i32 {
    match action {
        ImeAction::Default if single_line => IME_ACTION_DONE,
        ImeAction::Default => IME_ACTION_UNSPECIFIED,
        ImeAction::None => IME_ACTION_NONE,
        ImeAction::Go => IME_ACTION_GO,
        ImeAction::Search => IME_ACTION_SEARCH,
        ImeAction::Send => IME_ACTION_SEND,
        ImeAction::Previous => IME_ACTION_PREVIOUS,
        ImeAction::Next => IME_ACTION_NEXT,
        ImeAction::Done => IME_ACTION_DONE,
    }
}

fn android_capitalization(capitalization: KeyboardCapitalization) -> i32 {
    const TYPE_TEXT_FLAG_CAP_CHARACTERS: i32 = 0x1000;
    const TYPE_TEXT_FLAG_CAP_WORDS: i32 = 0x2000;
    const TYPE_TEXT_FLAG_CAP_SENTENCES: i32 = 0x4000;

    match capitalization {
        KeyboardCapitalization::None => 0,
        KeyboardCapitalization::Characters => TYPE_TEXT_FLAG_CAP_CHARACTERS,
        KeyboardCapitalization::Words => TYPE_TEXT_FLAG_CAP_WORDS,
        KeyboardCapitalization::Sentences => TYPE_TEXT_FLAG_CAP_SENTENCES,
    }
}

/// The autofill hint of a content type: a `View.AUTOFILL_HINT_*` value, or
/// one of androidx `HintConstants` where the platform names none.
fn android_autofill_hint(content_type: ContentType) -> &'static str {
    match content_type {
        ContentType::Username => "username",
        ContentType::Password => "password",
        ContentType::NewUsername => "newUsername",
        ContentType::NewPassword => "newPassword",
        ContentType::EmailAddress => "emailAddress",
        ContentType::PhoneNumber => "phone",
        ContentType::PersonFullName => "name",
        ContentType::PostalAddress => "postalAddress",
        ContentType::PostalCode => "postalCode",
        ContentType::CreditCardNumber => "creditCardNumber",
        ContentType::SmsOtpCode => "smsOTPCode",
    }
}

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 (input_type, ime_options) = android_editor_info(&state.keyboard_options, state.single_line);

    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);
        let autofill_hint = match state.keyboard_options.content_type {
            Some(content_type) => JObject::from(
                env.new_string(android_autofill_hint(content_type))
                    .map_err(|error| {
                        clear_pending_android_jni_exception(env);
                        format!("failed to create Android autofill hint string: {error}")
                    })?,
            ),
            None => JObject::null(),
        };
        env.call_static_method(
            class,
            jni_str!("show"),
            jni_sig!("(Landroid/app/Activity;JLjava/lang/String;IIIILjava/lang/String;)I"),
            &[
                JValue::Object(&activity),
                JValue::Long(handle.raw()),
                JValue::Object(&text),
                JValue::Int(sel_start),
                JValue::Int(sel_end),
                JValue::Int(input_type),
                JValue::Int(ime_options),
                JValue::Object(&autofill_hint),
            ],
        )
        .and_then(jni::JValueOwned::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)
        }
    }
}

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(())
    })
}

/// Moves the editor view over the focused field, at `[left, top, width,
/// height]` in content-relative physical pixels, so autofill anchors its
/// suggestions to the field.
pub(crate) fn place_android_text_input(
    app: &android_activity::AndroidApp,
    [left, top, width, height]: [i32; 4],
) -> 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!("place"),
            jni_sig!("(Landroid/app/Activity;IIII)V"),
            &[
                JValue::Object(&activity),
                JValue::Int(left),
                JValue::Int(top),
                JValue::Int(width),
                JValue::Int(height),
            ],
        )
        .map_err(|error| {
            clear_pending_android_jni_exception(env);
            format!("failed to place Android text input: {error}")
        })?;
        Ok(())
    })
}

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(())
    })
}

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)]
#[unsafe(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)]
#[unsafe(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)]
#[unsafe(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)]
#[unsafe(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)]
#[unsafe(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)]
#[unsafe(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)]
#[unsafe(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)]
#[unsafe(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)]
#[unsafe(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)]
#[unsafe(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)]
#[path = "tests/android_text_input_tests.rs"]
mod tests;