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
121 changes: 121 additions & 0 deletions winit-uikit/src/ime.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,121 @@
//! Helpers for the iOS `UITextInput` protocol implementation.
//!
//! UIKit requires that anything which can be the target of a multi-stage
//! (CJK) input method adopt `UITextInput`, which talks in terms of opaque
//! `UITextPosition` / `UITextRange` objects. iOS will call back into our view
//! with these objects, so we need concrete subclasses we can downcast and read
//! offsets from.
//!
//! We treat the "document" that `UITextInput` sees as just the current marked
//! (preedit) text — outside of an active composition we report an empty
//! document. The committed text lives on the application side; we only forward
//! `Ime::Preedit` / `Ime::Commit` events.

use std::cell::Cell;

use objc2::rc::Retained;
use objc2::runtime::NSObjectProtocol;
use objc2::{DefinedClass, MainThreadMarker, MainThreadOnly, define_class, msg_send};
use objc2_foundation::NSObject;
use objc2_ui_kit::{UITextPosition, UITextRange};

/// State that the view tracks for an active IME composition.
///
/// `marked_text` is the current preedit string. `selected_range` is the
/// selection inside that preedit string (start/end in UTF-8 byte offsets,
/// matching what `Ime::Preedit` expects).
#[derive(Default)]
pub(crate) struct ImeState {
pub(crate) marked_text: String,
pub(crate) selected_range: (usize, usize),
}

impl ImeState {
pub(crate) fn is_marked(&self) -> bool {
!self.marked_text.is_empty()
}

pub(crate) fn marked_len_chars(&self) -> usize {
self.marked_text.chars().count()
}
}

pub(crate) struct WinitTextPositionState {
offset: Cell<i64>,
}

define_class!(
/// `UITextPosition` subclass carrying a character offset into the marked text.
#[unsafe(super(UITextPosition, NSObject))]
#[thread_kind = MainThreadOnly]
#[name = "WinitTextPosition"]
#[ivars = WinitTextPositionState]
pub(crate) struct WinitTextPosition;
);

impl WinitTextPosition {
pub(crate) fn new(mtm: MainThreadMarker, offset: i64) -> Retained<Self> {
let this = mtm.alloc().set_ivars(WinitTextPositionState { offset: Cell::new(offset) });
unsafe { msg_send![super(this), init] }
}

pub(crate) fn offset(&self) -> i64 {
self.ivars().offset.get()
}
}

pub(crate) struct WinitTextRangeState {
start: Cell<i64>,
end: Cell<i64>,
}

define_class!(
/// `UITextRange` subclass holding a `[start, end)` interval of character
/// offsets into the marked text.
#[unsafe(super(UITextRange, NSObject))]
#[thread_kind = MainThreadOnly]
#[name = "WinitTextRange"]
#[ivars = WinitTextRangeState]
pub(crate) struct WinitTextRange;

impl WinitTextRange {
#[unsafe(method(isEmpty))]
fn is_empty(&self) -> bool {
self.ivars().start.get() == self.ivars().end.get()
}

#[unsafe(method_id(start))]
fn start_position(&self) -> Retained<UITextPosition> {
let mtm = MainThreadMarker::new().expect("WinitTextRange used off the main thread");
let p = WinitTextPosition::new(mtm, self.ivars().start.get());
Retained::into_super(p)
}

#[unsafe(method_id(end))]
fn end_position(&self) -> Retained<UITextPosition> {
let mtm = MainThreadMarker::new().expect("WinitTextRange used off the main thread");
let p = WinitTextPosition::new(mtm, self.ivars().end.get());
Retained::into_super(p)
}
}
);

impl WinitTextRange {
pub(crate) fn new(mtm: MainThreadMarker, start: i64, end: i64) -> Retained<Self> {
let this = mtm
.alloc()
.set_ivars(WinitTextRangeState { start: Cell::new(start), end: Cell::new(end) });
unsafe { msg_send![super(this), init] }
}

pub(crate) fn start_offset(&self) -> i64 {
self.ivars().start.get()
}

pub(crate) fn end_offset(&self) -> i64 {
self.ivars().end.get()
}
}

unsafe impl NSObjectProtocol for WinitTextPosition {}
unsafe impl NSObjectProtocol for WinitTextRange {}
1 change: 1 addition & 0 deletions winit-uikit/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,7 @@

mod app_state;
mod event_loop;
mod ime;
mod monitor;
mod view;
mod view_controller;
Expand Down
Loading
Loading