Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
44 changes: 44 additions & 0 deletions winit-android/src/event_loop.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,8 @@ use winit_core::window::{
WindowLevel,
};

#[cfg(feature = "game-activity")]
use crate::ime::ImeState;
use crate::keycodes;

static HAS_FOCUS: AtomicBool = AtomicBool::new(true);
Expand Down Expand Up @@ -155,6 +157,8 @@ impl EventLoop {
exit: Cell::new(false),
redraw_requester: RedrawRequester::new(&redraw_flag, android_app.create_waker()),
event_loop_proxy,
#[cfg(feature = "game-activity")]
ime: Arc::default(),
},
redraw_flag,
loop_running: false,
Expand Down Expand Up @@ -269,6 +273,18 @@ impl EventLoop {
let android_app = self.android_app.clone();

// Process input events
#[cfg(feature = "game-activity")]
{
let notifications =
std::mem::take(&mut self.window_target.ime.lock().unwrap().notifications);
for event in notifications {
app.window_event(
&self.window_target,
GLOBAL_WINDOW,
event::WindowEvent::Ime(event),
);
}
}
match android_app.input_events_iter() {
Ok(mut input_iter) => loop {
let read_event =
Expand Down Expand Up @@ -503,6 +519,17 @@ impl EventLoop {
},
}
},
#[cfg(feature = "game-activity")]
InputEvent::TextEvent(state) => {
let events = self.window_target.ime.lock().unwrap().update(state);
for event in events {
app.window_event(
&self.window_target,
GLOBAL_WINDOW,
event::WindowEvent::Ime(event),
);
}
},
_ => {
warn!("Unknown android_activity input event {event:?}")
},
Expand Down Expand Up @@ -712,6 +739,8 @@ pub struct ActiveEventLoop {
exit: Cell<bool>,
redraw_requester: RedrawRequester,
event_loop_proxy: Arc<EventLoopProxy>,
#[cfg(feature = "game-activity")]
ime: Arc<Mutex<ImeState>>,
}

impl ActiveEventLoop {
Expand Down Expand Up @@ -803,6 +832,8 @@ pub struct Window {
app: AndroidApp,
ime_capabilities: Mutex<Option<ImeCapabilities>>,
redraw_requester: RedrawRequester,
#[cfg(feature = "game-activity")]
ime: Arc<Mutex<ImeState>>,
}

impl Window {
Expand All @@ -822,6 +853,8 @@ impl Window {
app: el.app.clone(),
ime_capabilities: Default::default(),
redraw_requester: el.redraw_requester.clone(),
#[cfg(feature = "game-activity")]
ime: el.ime.clone(),
})
}

Expand Down Expand Up @@ -1003,6 +1036,12 @@ impl CoreWindow for Window {
return Err(ImeRequestError::AlreadyEnabled);
}
*current_caps = Some(capabilities);
#[cfg(feature = "game-activity")]
{
self.ime.lock().unwrap().enable();
self.app.set_text_input_state(Default::default());
self.redraw_requester.waker.wake();
}
self.app.show_soft_input(true);
},
ImeRequest::Update(_) => {
Expand All @@ -1012,6 +1051,11 @@ impl CoreWindow for Window {
},
ImeRequest::Disable => {
*current_caps = None;
#[cfg(feature = "game-activity")]
{
self.ime.lock().unwrap().disable();
self.redraw_requester.waker.wake();
}
self.app.hide_soft_input(true);
},
_ => return Err(ImeRequestError::NotSupported),
Expand Down
92 changes: 92 additions & 0 deletions winit-android/src/ime.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
use android_activity::input::TextInputState;
use winit_core::event::Ime;

#[derive(Debug, Default)]
pub(crate) struct ImeState {
committed: String,
preedit: Option<(String, (usize, usize))>,
pub(crate) enabled: bool,
pub(crate) notifications: Vec<Ime>,
}

impl ImeState {
pub(crate) fn enable(&mut self) {
self.committed.clear();
self.preedit = None;
self.enabled = true;
self.notifications.push(Ime::Enabled);
}

pub(crate) fn disable(&mut self) {
if self.enabled {
self.enabled = false;
self.notifications.push(Ime::Disabled);
}
}

pub(crate) fn update(&mut self, state: &TextInputState) -> Vec<Ime> {
if !self.enabled {
return Vec::new();
}

let mut committed = state.text.clone();
let preedit = state.compose_region.as_ref().and_then(|span| {
let start = utf8_offset(&state.text, span.start);
let end = utf8_offset(&state.text, span.end);
(start < end).then(|| {
let cursor = |offset| utf8_offset(&state.text, offset).clamp(start, end) - start;
let selection = (cursor(state.selection.start), cursor(state.selection.end));
let text = committed.drain(start..end).collect();
(text, selection)
})
});

// GameActivity sends a snapshot of its editable buffer, including text
// already committed to the application. Only send the changed suffix.
let common = self
.committed
.chars()
.zip(committed.chars())
.take_while(|(old, new)| old == new)
.map(|(ch, _)| ch.len_utf8())
.sum::<usize>();
let mut events = Vec::new();
if self.committed.len() > common {
events.push(Ime::DeleteSurrounding {
before_bytes: self.committed.len() - common,
after_bytes: 0,
});
}
let did_commit = committed.len() > common;
if did_commit {
events.push(Ime::Commit(committed[common..].to_owned()));
}
if preedit != self.preedit || did_commit {
match &preedit {
Some((text, selection)) => {
events.push(Ime::Preedit(text.clone(), Some(*selection)));
},
None if self.preedit.is_some() && !did_commit => {
events.push(Ime::Preedit(String::new(), None));
},
None => (),
}
}
self.committed = committed;
self.preedit = preedit;
events
}
}

// Java reports offsets in UTF-16 code units. Round a split surrogate down to
// its scalar boundary and clamp positions past the end to the string length.
fn utf8_offset(text: &str, offset: usize) -> usize {
let mut utf16 = 0;
for (byte, ch) in text.char_indices() {
utf16 += ch.len_utf16();
if utf16 > offset {
return byte;
}
}
text.len()
}
2 changes: 2 additions & 0 deletions winit-android/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,8 @@
#![warn(clippy::exhaustive_enums)]

mod event_loop;
#[cfg(feature = "game-activity")]
mod ime;
mod keycodes;

use winit_core::event_loop::ActiveEventLoop as CoreActiveEventLoop;
Expand Down
2 changes: 2 additions & 0 deletions winit/src/changelog/unreleased.md
Original file line number Diff line number Diff line change
Expand Up @@ -58,5 +58,7 @@ changelog entry.

- Fix `RgbaIcon::new` and `CustomCursorSource::from_rgba` rejecting every valid
RGBA buffer.
- On Android GameActivity, forward composing and committed software keyboard
text as IME events.
- On macOS, fix text selected from the Character Viewer not producing an IME commit.
- On Wayland, fix text not being copied to some application due to missing some mime types.
Loading