Skip to content

Commit

Permalink
Undo a wrong modification
Browse files Browse the repository at this point in the history
  • Loading branch information
wuwbobo2021 committed Dec 27, 2024
1 parent 1cdfd3c commit 6ce33b0
Showing 1 changed file with 17 additions and 20 deletions.
37 changes: 17 additions & 20 deletions internal/backends/android-activity/javahelper.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright © SixtyFPS GmbH <info@slint.dev>
// Copyright © SixtyFPS GmbH <info@slint.dev>
// SPDX-License-Identifier: GPL-3.0-only OR LicenseRef-Slint-Royalty-free-2.0 OR LicenseRef-Slint-Software-3.0

use super::*;
Expand Down Expand Up @@ -209,7 +209,7 @@ impl JavaHelper {
}
}

let to_chars_index = |x| convert_utf8_index_to_char(&text, x as usize);
let to_utf16 = |x| convert_utf8_index_to_utf16(&text, x as usize);
let text = &env.auto_local(env.new_string(text.as_str())?);

let class_it = env.find_class("android/text/InputType")?;
Expand Down Expand Up @@ -247,13 +247,10 @@ impl JavaHelper {
"(Ljava/lang/String;IIIIIIIIIIZ)V",
&[
JValue::Object(&text),
JValue::from(to_chars_index(cursor_position) as jint),
JValue::from(to_chars_index(anchor_position) as jint),
JValue::from(to_chars_index(data.preedit_offset) as jint),
JValue::from(
(to_chars_index(data.preedit_offset) + data.preedit_text.chars().count())
as jint,
),
JValue::from(to_utf16(cursor_position) as jint),
JValue::from(to_utf16(anchor_position) as jint),
JValue::from(to_utf16(data.preedit_offset) as jint),
JValue::from(to_utf16(data.preedit_offset + data.preedit_text.len()) as jint),
JValue::from(cur_x as jint),
JValue::from(cur_y as jint),
JValue::from(anchor_x as jint),
Expand Down Expand Up @@ -361,10 +358,10 @@ extern "system" fn Java_SlintAndroidJavaHelper_updateText(
let decoded: std::borrow::Cow<str> = (&java_str).into();
let text = SharedString::from(decoded.as_ref());

let cursor_position = convert_char_index_to_utf8(&text, cursor_position as usize);
let anchor_position = convert_char_index_to_utf8(&text, anchor_position as usize);
let preedit_start = convert_char_index_to_utf8(&text, preedit_start as usize);
let preedit_end = convert_char_index_to_utf8(&text, preedit_end as usize);
let cursor_position = convert_utf16_index_to_utf8(&text, cursor_position as usize);
let anchor_position = convert_utf16_index_to_utf8(&text, anchor_position as usize);
let preedit_start = convert_utf16_index_to_utf8(&text, preedit_start as usize);
let preedit_end = convert_utf16_index_to_utf8(&text, preedit_end as usize);

i_slint_core::api::invoke_from_event_loop(move || {
if let Some(adaptor) = CURRENT_WINDOW.with_borrow(|x| x.upgrade()) {
Expand Down Expand Up @@ -410,20 +407,20 @@ extern "system" fn Java_SlintAndroidJavaHelper_updateText(
.unwrap()
}

fn convert_char_index_to_utf8(in_str: &str, char_index: usize) -> usize {
let mut char_counter = 0;
fn convert_utf16_index_to_utf8(in_str: &str, utf16_index: usize) -> usize {
let mut utf16_counter = 0;

for (utf8_index, _) in in_str.char_indices() {
if char_counter >= char_index {
for (utf8_index, c) in in_str.char_indices() {
if utf16_counter >= utf16_index {
return utf8_index;
}
char_counter += 1;
utf16_counter += c.len_utf16();
}
in_str.len()
}

fn convert_utf8_index_to_char(in_str: &str, utf8_index: usize) -> usize {
in_str[..utf8_index].chars().count()
fn convert_utf8_index_to_utf16(in_str: &str, utf8_index: usize) -> usize {
in_str[..utf8_index].encode_utf16().count()
}

#[no_mangle]
Expand Down

0 comments on commit 6ce33b0

Please sign in to comment.