Skip to content

Commit f601be8

Browse files
committed
enable IME when text-input focused
1 parent 2d46270 commit f601be8

File tree

2 files changed

+18
-7
lines changed

2 files changed

+18
-7
lines changed

src/app_handle.rs

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -437,9 +437,6 @@ impl ApplicationHandle {
437437
let Ok(window) = event_loop.create_window(window_attributes) else {
438438
return;
439439
};
440-
441-
window.set_ime_allowed(true);
442-
443440
let window_id = window.id();
444441
let window_handle = WindowHandle::new(
445442
window,

src/views/text_input.rs

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
use crate::action::exec_after;
1+
use crate::action::{exec_after, set_ime_allowed, set_ime_cursor_area};
22
use crate::event::{EventListener, EventPropagation};
33
use crate::id::ViewId;
44
use crate::keyboard::{self, KeyEvent, Modifiers};
@@ -134,7 +134,15 @@ pub fn text_input(buffer: RwSignal<String>) -> TextInput {
134134
{
135135
create_effect(move |_| {
136136
let text = buffer.get();
137-
id.update_state((text, is_focused.get()));
137+
let is_focused = is_focused.get();
138+
id.update_state((text, is_focused));
139+
set_ime_allowed(is_focused);
140+
141+
let rect = id.layout_rect();
142+
set_ime_cursor_area(
143+
Point::new(rect.x0, rect.y0),
144+
id.get_size().unwrap_or(Size::ZERO),
145+
);
138146
});
139147
}
140148

@@ -881,7 +889,7 @@ impl TextInput {
881889

882890
// FIXME: any way to improve performance?
883891
// but string commited by ime should be not too long, is it necessary to optimize?
884-
(0.. ch.chars().count()).all(|_| self.move_cursor(Movement::Glyph, TextDirection::Right))
892+
(0..ch.chars().count()).all(|_| self.move_cursor(Movement::Glyph, TextDirection::Right))
885893
}
886894

887895
fn move_selection(
@@ -1109,7 +1117,13 @@ impl View for TextInput {
11091117
false
11101118
}
11111119
Event::KeyDown(event) => self.handle_key_down(cx, event),
1112-
Event::ImeCommit(str) => self.insert_text(&SmolStr::from(str.as_str())),
1120+
Event::ImeCommit(str) => {
1121+
if self.is_focused {
1122+
self.insert_text(&SmolStr::from(str.as_str()))
1123+
} else {
1124+
false
1125+
}
1126+
}
11131127
_ => false,
11141128
};
11151129

0 commit comments

Comments
 (0)