-
Notifications
You must be signed in to change notification settings - Fork 1.3k
Expand file tree
/
Copy pathime.rs
More file actions
136 lines (114 loc) · 4.28 KB
/
Copy pathime.rs
File metadata and controls
136 lines (114 loc) · 4.28 KB
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
//! 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::{declare_class, msg_send_id, mutability, ClassType, DeclaredClass};
use objc2_foundation::{MainThreadMarker, 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>,
}
declare_class!(
/// `UITextPosition` subclass carrying a character offset into the marked
/// text.
pub(crate) struct WinitTextPosition;
unsafe impl ClassType for WinitTextPosition {
#[inherits(NSObject)]
type Super = UITextPosition;
type Mutability = mutability::MainThreadOnly;
const NAME: &'static str = "WinitTextPosition";
}
impl DeclaredClass for WinitTextPosition {
type Ivars = WinitTextPositionState;
}
);
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_id![super(this), init] }
}
pub(crate) fn offset(&self) -> i64 {
self.ivars().offset.get()
}
}
pub(crate) struct WinitTextRangeState {
start: Cell<i64>,
end: Cell<i64>,
}
declare_class!(
/// `UITextRange` subclass holding a `[start, end)` interval of character
/// offsets into the marked text.
pub(crate) struct WinitTextRange;
unsafe impl ClassType for WinitTextRange {
#[inherits(NSObject)]
type Super = UITextRange;
type Mutability = mutability::MainThreadOnly;
const NAME: &'static str = "WinitTextRange";
}
impl DeclaredClass for WinitTextRange {
type Ivars = WinitTextRangeState;
}
unsafe impl WinitTextRange {
#[method(isEmpty)]
fn is_empty(&self) -> bool {
self.ivars().start.get() == self.ivars().end.get()
}
#[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)
}
#[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_id![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 {}