use std::{
ffi::c_void,
ptr::NonNull,
sync::{
Mutex,
atomic::{AtomicBool, AtomicPtr, Ordering},
},
time::{Duration, Instant},
};
use objc2::{
class,
encode::{Encode, Encoding},
msg_send,
rc::Retained,
runtime::{AnyClass, AnyObject, AnyProtocol, Bool, ClassBuilder, Sel},
sel,
};
use super::ios::{CGPoint, CGRect, CGSize};
use crate::{
app::{
Tree,
events::ImeEvent,
runtime::{AutofillPeer, KeyboardTraits, TextInputSnapshot},
},
node::{
node_kind::{ContentKind, KeyboardKind, ReturnKey},
text_selection::previous_delete_boundary,
},
};
#[repr(C)]
#[derive(Clone, Copy)]
struct NSRange {
location: usize,
length: usize,
}
unsafe impl Encode for NSRange {
const ENCODING: Encoding = Encoding::Struct("_NSRange", &[usize::ENCODING, usize::ENCODING]);
}
enum Edit {
Insert(String),
DeleteBackward,
Replace {
start: usize,
end: usize,
text: String,
},
Select {
start: usize,
end: usize,
},
Mark {
text: String,
selection: Option<(usize, usize)>,
},
Unmark(String),
Command(crate::app::events::EditCommand),
}
#[derive(Default)]
struct Mirror {
text: String,
selection: (usize, usize),
marked: Option<(usize, usize)>,
geometry: Option<Geometry>,
traits: Option<KeyboardTraits>,
edits: Vec<Edit>,
filled_at: Option<Instant>,
caret_astray: bool,
can_undo: bool,
can_redo: bool,
}
const FILL_SETTLES: Duration = Duration::from_millis(700);
static TYPES: AtomicBool = AtomicBool::new(false);
#[derive(Clone, PartialEq)]
struct Geometry {
bounds: (f64, f64, f64, f64),
carets: Vec<(usize, f64, f64)>,
line_height: f64,
}
const CARET_WIDTH: f64 = 2.0;
impl Geometry {
fn place(&self, byte: usize) -> (f64, f64) {
self
.carets
.iter()
.rev()
.find(|(index, ..)| *index <= byte)
.or(self.carets.first())
.map_or((self.bounds.0, self.bounds.1), |(_, x, y)| (*x, *y))
}
fn in_view(&self, x: f64, y: f64, width: f64) -> CGRect {
CGRect {
origin: CGPoint {
x: x - self.bounds.0,
y: y - self.bounds.1,
},
size: CGSize {
width,
height: self.line_height,
},
}
}
fn rows(&self, start: usize, end: usize) -> Vec<(f64, f64, f64)> {
let mut rows: Vec<(f64, f64, f64)> = Vec::new();
for (_, x, y) in self.carets.iter().filter(|(index, ..)| (start..=end).contains(index)) {
match rows.iter_mut().find(|(_, row, _)| (*row - *y).abs() < 0.5) {
Some((left, _, right)) => {
*left = left.min(*x);
*right = right.max(*x);
}
None => rows.push((*x, *y, *x)),
}
}
rows
}
fn nearest(&self, point: CGPoint) -> usize {
let (x, y) = (point.x + self.bounds.0, point.y + self.bounds.1);
let level = |row: f64| (row + self.line_height / 2.0 - y).abs();
let Some(row) = self
.carets
.iter()
.map(|(_, _, row)| *row)
.min_by(|a, b| level(*a).total_cmp(&level(*b)))
else {
return 0;
};
self
.carets
.iter()
.filter(|(_, _, caret)| (*caret - row).abs() < 0.5)
.min_by(|a, b| (a.1 - x).abs().total_cmp(&(b.1 - x).abs()))
.map_or(0, |(index, ..)| *index)
}
}
static MIRROR: Mutex<Option<Mirror>> = Mutex::new(None);
static WINDOW_VIEW: AtomicPtr<AnyObject> = AtomicPtr::new(std::ptr::null_mut());
static HIGHLIGHT: Mutex<Vec<(f64, f64, f64, f64)>> = Mutex::new(Vec::new());
static TYPING_VIEW: AtomicPtr<AnyObject> = AtomicPtr::new(std::ptr::null_mut());
static INPUT_DELEGATE: AtomicPtr<AnyObject> = AtomicPtr::new(std::ptr::null_mut());
static TOKENIZER: AtomicPtr<AnyObject> = AtomicPtr::new(std::ptr::null_mut());
fn types_now(view: &AnyObject) -> bool {
std::ptr::eq(TYPING_VIEW.load(Ordering::Acquire), view)
}
fn with_mirror<T>(default: T, read: impl FnOnce(&mut Mirror) -> T) -> T {
MIRROR.lock().unwrap().as_mut().map_or(default, read)
}
impl Mirror {
fn utf16_len(&self) -> isize {
self.text.encode_utf16().count() as isize
}
fn byte_of(&self, utf16: isize) -> usize {
let mut units = 0isize;
for (byte, character) in self.text.char_indices() {
if units >= utf16 {
return byte;
}
units += character.len_utf16() as isize;
}
self.text.len()
}
fn utf16_of(&self, byte: usize) -> isize {
let byte = byte.min(self.text.len());
self.text[..floor_char_boundary(&self.text, byte)]
.encode_utf16()
.count() as isize
}
fn replace(&mut self, start: usize, end: usize, text: &str) {
let start = floor_char_boundary(&self.text, start.min(self.text.len()));
let end = floor_char_boundary(&self.text, end.clamp(start, self.text.len()));
self.text.replace_range(start..end, text);
self.selection = (start + text.len(), start + text.len());
self.marked = None;
}
}
fn floor_char_boundary(text: &str, mut index: usize) -> usize {
index = index.min(text.len());
while index > 0 && !text.is_char_boundary(index) {
index -= 1;
}
index
}
fn string_of(text: *mut AnyObject) -> Option<String> {
let text = unsafe { text.as_ref() }?;
let utf8: *const std::ffi::c_char = unsafe { msg_send![text, UTF8String] };
if utf8.is_null() {
return None;
}
Some(unsafe { std::ffi::CStr::from_ptr(utf8) }.to_string_lossy().into_owned())
}
fn ns_string(text: &str) -> *mut AnyObject {
let units: Vec<u16> = text.encode_utf16().collect();
let string: Retained<AnyObject> =
unsafe { msg_send![class!(NSString), stringWithCharacters: units.as_ptr(), length: units.len()] };
Retained::autorelease_ptr(string)
}
fn position_class() -> &'static AnyClass {
if let Some(class) = AnyClass::get(c"LurqTextPosition") {
return class;
}
let mut builder = ClassBuilder::new(c"LurqTextPosition", class!(UITextPosition)).expect("a class name of its own");
builder.add_ivar::<isize>(c"lurqOffset");
builder.register()
}
fn range_class() -> &'static AnyClass {
if let Some(class) = AnyClass::get(c"LurqTextRange") {
return class;
}
extern "C-unwind" fn start(this: &AnyObject, _cmd: Sel) -> *mut AnyObject {
position(range_ends(this).0)
}
extern "C-unwind" fn end(this: &AnyObject, _cmd: Sel) -> *mut AnyObject {
position(range_ends(this).1)
}
extern "C-unwind" fn is_empty(this: &AnyObject, _cmd: Sel) -> Bool {
let (start, end) = range_ends(this);
Bool::new(start == end)
}
let mut builder = ClassBuilder::new(c"LurqTextRange", class!(UITextRange)).expect("a class name of its own");
builder.add_ivar::<isize>(c"lurqStart");
builder.add_ivar::<isize>(c"lurqEnd");
unsafe {
builder.add_method(sel!(start), start as extern "C-unwind" fn(_, _) -> _);
builder.add_method(sel!(end), end as extern "C-unwind" fn(_, _) -> _);
builder.add_method(sel!(isEmpty), is_empty as extern "C-unwind" fn(_, _) -> _);
}
builder.register()
}
fn set_ivar(object: &AnyObject, class: &AnyClass, name: &std::ffi::CStr, value: isize) {
let ivar = class.instance_variable(name).expect("an ivar the class was made with");
unsafe { *ivar.load_ptr::<isize>(object) = value };
}
fn ivar(object: &AnyObject, class: &AnyClass, name: &std::ffi::CStr) -> isize {
let ivar = class.instance_variable(name).expect("an ivar the class was made with");
unsafe { *ivar.load::<isize>(object) }
}
fn position(offset: isize) -> *mut AnyObject {
let class = position_class();
let position: Retained<AnyObject> = unsafe { msg_send![class, new] };
set_ivar(&position, class, c"lurqOffset", offset);
Retained::autorelease_ptr(position)
}
fn range(start: isize, end: isize) -> *mut AnyObject {
let class = range_class();
let range: Retained<AnyObject> = unsafe { msg_send![class, new] };
set_ivar(&range, class, c"lurqStart", start.min(end));
set_ivar(&range, class, c"lurqEnd", start.max(end));
Retained::autorelease_ptr(range)
}
fn offset_of(position: *mut AnyObject) -> Option<isize> {
let position = unsafe { position.as_ref() }?;
let class = position_class();
let ours: bool = unsafe { msg_send![position, isKindOfClass: class] };
ours.then(|| ivar(position, class, c"lurqOffset"))
}
fn range_ends(range: &AnyObject) -> (isize, isize) {
let class = range_class();
(ivar(range, class, c"lurqStart"), ivar(range, class, c"lurqEnd"))
}
fn ends_of(range: *mut AnyObject) -> Option<(isize, isize)> {
let range = unsafe { range.as_ref() }?;
let ours: bool = unsafe { msg_send![range, isKindOfClass: range_class()] };
ours.then(|| range_ends(range))
}
extern "C-unwind" fn can_become_first_responder(_this: &AnyObject, _cmd: Sel) -> Bool {
Bool::YES
}
extern "C-unwind" fn has_text(_this: &AnyObject, _cmd: Sel) -> Bool {
Bool::new(with_mirror(false, |mirror| !mirror.text.is_empty()))
}
extern "C-unwind" fn insert_text(_this: &AnyObject, _cmd: Sel, text: *mut AnyObject) {
let Some(text) = string_of(text) else {
return;
};
with_mirror((), |mirror| {
let (start, end) = mirror.marked.unwrap_or(mirror.selection);
if !text.contains('\n') {
mirror.replace(start, end, &text);
}
if mirror.traits.is_some_and(|traits| traits.secure) && text.chars().nth(1).is_some() {
mirror.filled_at = Some(Instant::now());
mirror.caret_astray = true;
}
mirror.edits.push(Edit::Insert(text));
});
}
extern "C-unwind" fn delete_backward(_this: &AnyObject, _cmd: Sel) {
with_mirror((), |mirror| {
let (start, end) = mirror.selection;
if start != end {
mirror.replace(start, end, "");
} else if start > 0 {
let from = previous_delete_boundary(&mirror.text, start);
mirror.replace(from, start, "");
}
mirror.edits.push(Edit::DeleteBackward);
});
}
extern "C-unwind" fn text_in_range(_this: &AnyObject, _cmd: Sel, range: *mut AnyObject) -> *mut AnyObject {
let Some((start, end)) = ends_of(range) else {
return std::ptr::null_mut();
};
let text = with_mirror(String::new(), |mirror| {
mirror.text[mirror.byte_of(start)..mirror.byte_of(end)].to_owned()
});
ns_string(&text)
}
extern "C-unwind" fn replace_range(_this: &AnyObject, _cmd: Sel, range: *mut AnyObject, text: *mut AnyObject) {
let (Some((start, end)), Some(text)) = (ends_of(range), string_of(text)) else {
return;
};
with_mirror((), |mirror| {
let (start, end) = (mirror.byte_of(start), mirror.byte_of(end));
mirror.replace(start, end, &text);
mirror.edits.push(Edit::Replace { start, end, text });
});
}
extern "C-unwind" fn selected_text_range(_this: &AnyObject, _cmd: Sel) -> *mut AnyObject {
match with_mirror(None, |mirror| {
Some((mirror.utf16_of(mirror.selection.0), mirror.utf16_of(mirror.selection.1)))
}) {
Some((start, end)) => range(start, end),
None => std::ptr::null_mut(),
}
}
extern "C-unwind" fn set_selected_text_range(_this: &AnyObject, _cmd: Sel, selected: *mut AnyObject) {
let Some((start, end)) = ends_of(selected) else {
return;
};
let refused = with_mirror(false, |mirror| {
let selection = (mirror.byte_of(start), mirror.byte_of(end));
let filling = mirror.filled_at.is_some_and(|at| at.elapsed() < FILL_SETTLES);
if filling && selection == (0, 0) && !mirror.text.is_empty() {
mirror.caret_astray = true;
return true;
}
if selection != mirror.selection {
mirror.selection = selection;
mirror.edits.push(Edit::Select {
start: selection.0,
end: selection.1,
});
}
false
});
if refused {
wake_shell();
}
}
fn wake_shell() {
let window_view = WINDOW_VIEW.load(Ordering::Acquire);
if let Some(window_view) = unsafe { window_view.as_ref() } {
let _: () = unsafe { msg_send![window_view, setNeedsDisplay] };
}
}
extern "C-unwind" fn marked_text_range(_this: &AnyObject, _cmd: Sel) -> *mut AnyObject {
match with_mirror(None, |mirror| {
mirror
.marked
.map(|(start, end)| (mirror.utf16_of(start), mirror.utf16_of(end)))
}) {
Some((start, end)) => range(start, end),
None => std::ptr::null_mut(),
}
}
extern "C-unwind" fn marked_text_style(_this: &AnyObject, _cmd: Sel) -> *mut AnyObject {
std::ptr::null_mut()
}
extern "C-unwind" fn set_marked_text_style(_this: &AnyObject, _cmd: Sel, _style: *mut AnyObject) {}
extern "C-unwind" fn set_marked_text(_this: &AnyObject, _cmd: Sel, text: *mut AnyObject, selected: NSRange) {
let text = string_of(text).unwrap_or_default();
with_mirror((), |mirror| {
let (start, end) = mirror.marked.unwrap_or(mirror.selection);
mirror.replace(start, end, &text);
let inside = |utf16: usize| {
let mut units = 0;
for (byte, character) in text.char_indices() {
if units >= utf16 {
return byte;
}
units += character.len_utf16();
}
text.len()
};
let selection = (inside(selected.location), inside(selected.location + selected.length));
if text.is_empty() {
mirror.marked = None;
mirror.selection = (start, start);
} else {
mirror.marked = Some((start, start + text.len()));
mirror.selection = (start + selection.0, start + selection.1);
}
mirror.edits.push(Edit::Mark {
selection: (!text.is_empty()).then_some(selection),
text,
});
});
}
extern "C-unwind" fn unmark_text(_this: &AnyObject, _cmd: Sel) {
with_mirror((), |mirror| {
if let Some((start, end)) = mirror.marked.take() {
let text = mirror.text[start..end].to_owned();
mirror.edits.push(Edit::Unmark(text));
}
});
}
extern "C-unwind" fn beginning_of_document(_this: &AnyObject, _cmd: Sel) -> *mut AnyObject {
position(0)
}
extern "C-unwind" fn end_of_document(_this: &AnyObject, _cmd: Sel) -> *mut AnyObject {
position(with_mirror(0, |mirror| mirror.utf16_len()))
}
extern "C-unwind" fn text_range_from_position(
_this: &AnyObject,
_cmd: Sel,
from: *mut AnyObject,
to: *mut AnyObject,
) -> *mut AnyObject {
match (offset_of(from), offset_of(to)) {
(Some(from), Some(to)) => range(from, to),
_ => std::ptr::null_mut(),
}
}
extern "C-unwind" fn position_from_position(
_this: &AnyObject,
_cmd: Sel,
from: *mut AnyObject,
offset: isize,
) -> *mut AnyObject {
let Some(from) = offset_of(from) else {
return std::ptr::null_mut();
};
let length = with_mirror(0, |mirror| mirror.utf16_len());
let to = from + offset;
if to < 0 || to > length {
return std::ptr::null_mut();
}
position(to)
}
extern "C-unwind" fn position_in_direction(
this: &AnyObject,
cmd: Sel,
from: *mut AnyObject,
direction: isize,
offset: isize,
) -> *mut AnyObject {
if matches!(direction, 4 | 5) {
let Some(from) = offset_of(from) else {
return std::ptr::null_mut();
};
let lines = if direction == 4 { -offset } else { offset };
let to = with_mirror(None, |mirror| {
let geometry = mirror.geometry.as_ref()?;
let (x, y) = geometry.place(mirror.byte_of(from));
let point = CGPoint {
x: x - geometry.bounds.0,
y: y - geometry.bounds.1 + geometry.line_height * (lines as f64 + 0.5),
};
Some(mirror.utf16_of(geometry.nearest(point)))
});
return match to {
Some(to) => position(to),
None => std::ptr::null_mut(),
};
}
let offset = if direction == 2 { offset } else { -offset };
position_from_position(this, cmd, from, offset)
}
extern "C-unwind" fn compare_position(
_this: &AnyObject,
_cmd: Sel,
one: *mut AnyObject,
other: *mut AnyObject,
) -> isize {
match (offset_of(one), offset_of(other)) {
(Some(one), Some(other)) => (one - other).signum(),
_ => 0,
}
}
extern "C-unwind" fn offset_from_position(
_this: &AnyObject,
_cmd: Sel,
from: *mut AnyObject,
to: *mut AnyObject,
) -> isize {
match (offset_of(from), offset_of(to)) {
(Some(from), Some(to)) => to - from,
_ => 0,
}
}
extern "C-unwind" fn input_delegate(this: &AnyObject, _cmd: Sel) -> *mut AnyObject {
if types_now(this) {
INPUT_DELEGATE.load(Ordering::Acquire)
} else {
std::ptr::null_mut()
}
}
extern "C-unwind" fn set_input_delegate(this: &AnyObject, _cmd: Sel, delegate: *mut AnyObject) {
if types_now(this) {
INPUT_DELEGATE.store(delegate, Ordering::Release);
}
}
extern "C-unwind" fn tokenizer(this: &AnyObject, _cmd: Sel) -> *mut AnyObject {
let make = || -> Retained<AnyObject> {
unsafe {
let tokenizer: objc2::rc::Allocated<AnyObject> = msg_send![class!(UITextInputStringTokenizer), alloc];
msg_send![tokenizer, initWithTextInput: this]
}
};
if !types_now(this) {
return Retained::autorelease_ptr(make());
}
let tokenizer = TOKENIZER.load(Ordering::Acquire);
if !tokenizer.is_null() {
return tokenizer;
}
let tokenizer = Retained::into_raw(make());
TOKENIZER.store(tokenizer, Ordering::Release);
tokenizer
}
extern "C-unwind" fn position_within_range(
_this: &AnyObject,
_cmd: Sel,
within: *mut AnyObject,
direction: isize,
) -> *mut AnyObject {
match ends_of(within) {
Some((_, end)) if matches!(direction, 2 | 5) => position(end),
Some((start, _)) => position(start),
None => std::ptr::null_mut(),
}
}
extern "C-unwind" fn character_range_by_extending(
_this: &AnyObject,
_cmd: Sel,
from: *mut AnyObject,
direction: isize,
) -> *mut AnyObject {
let Some(from) = offset_of(from) else {
return std::ptr::null_mut();
};
if matches!(direction, 2 | 5) {
range(from, with_mirror(from, |mirror| mirror.utf16_len()))
} else {
range(0, from)
}
}
extern "C-unwind" fn base_writing_direction(
_this: &AnyObject,
_cmd: Sel,
_position: *mut AnyObject,
_direction: isize,
) -> isize {
0
}
extern "C-unwind" fn set_base_writing_direction(
_this: &AnyObject,
_cmd: Sel,
_direction: isize,
_range: *mut AnyObject,
) {
}
fn no_rect() -> CGRect {
CGRect {
origin: CGPoint { x: 0.0, y: 0.0 },
size: CGSize {
width: 0.0,
height: 0.0,
},
}
}
extern "C-unwind" fn first_rect_for_range(_this: &AnyObject, _cmd: Sel, range: *mut AnyObject) -> CGRect {
let Some((start, end)) = ends_of(range) else {
return no_rect();
};
with_mirror(None, |mirror| {
let geometry = mirror.geometry.as_ref()?;
let (left, row, right) = *geometry.rows(mirror.byte_of(start), mirror.byte_of(end)).first()?;
Some(geometry.in_view(left, row, (right - left).max(CARET_WIDTH)))
})
.unwrap_or_else(no_rect)
}
extern "C-unwind" fn text_input_view(this: &AnyObject, _cmd: Sel) -> *const AnyObject {
this
}
extern "C-unwind" fn caret_rect_for_position(_this: &AnyObject, _cmd: Sel, position: *mut AnyObject) -> CGRect {
let Some(offset) = offset_of(position) else {
return no_rect();
};
with_mirror(None, |mirror| {
let geometry = mirror.geometry.as_ref()?;
let (x, y) = geometry.place(mirror.byte_of(offset));
Some(geometry.in_view(x, y, CARET_WIDTH))
})
.unwrap_or_else(no_rect)
}
fn selection_rect_class() -> &'static AnyClass {
if let Some(class) = AnyClass::get(c"LurqTextSelectionRect") {
return class;
}
fn part(this: &AnyObject, name: &std::ffi::CStr) -> f64 {
let ivar = selection_rect_class()
.instance_variable(name)
.expect("an ivar the class was made with");
unsafe { *ivar.load::<f64>(this) }
}
extern "C-unwind" fn rect(this: &AnyObject, _cmd: Sel) -> CGRect {
CGRect {
origin: CGPoint {
x: part(this, c"lurqX"),
y: part(this, c"lurqY"),
},
size: CGSize {
width: part(this, c"lurqWidth"),
height: part(this, c"lurqHeight"),
},
}
}
extern "C-unwind" fn writing_direction(_this: &AnyObject, _cmd: Sel) -> isize {
0
}
extern "C-unwind" fn contains_start(this: &AnyObject, _cmd: Sel) -> Bool {
Bool::new(part(this, c"lurqStart") != 0.0)
}
extern "C-unwind" fn contains_end(this: &AnyObject, _cmd: Sel) -> Bool {
Bool::new(part(this, c"lurqEnd") != 0.0)
}
extern "C-unwind" fn is_vertical(_this: &AnyObject, _cmd: Sel) -> Bool {
Bool::NO
}
let mut builder =
ClassBuilder::new(c"LurqTextSelectionRect", class!(UITextSelectionRect)).expect("a class name of its own");
for name in [
c"lurqX",
c"lurqY",
c"lurqWidth",
c"lurqHeight",
c"lurqStart",
c"lurqEnd",
] {
builder.add_ivar::<f64>(name);
}
unsafe {
builder.add_method(sel!(rect), rect as extern "C-unwind" fn(_, _) -> _);
builder.add_method(
sel!(writingDirection),
writing_direction as extern "C-unwind" fn(_, _) -> _,
);
builder.add_method(sel!(containsStart), contains_start as extern "C-unwind" fn(_, _) -> _);
builder.add_method(sel!(containsEnd), contains_end as extern "C-unwind" fn(_, _) -> _);
builder.add_method(sel!(isVertical), is_vertical as extern "C-unwind" fn(_, _) -> _);
}
builder.register()
}
extern "C-unwind" fn selection_rects_for_range(_this: &AnyObject, _cmd: Sel, range: *mut AnyObject) -> *mut AnyObject {
wake_shell();
let rects: Retained<AnyObject> = unsafe { msg_send![class!(NSMutableArray), array] };
let rows = ends_of(range).and_then(|(start, end)| {
with_mirror(None, |mirror| {
let geometry = mirror.geometry.as_ref()?;
let rows = geometry.rows(mirror.byte_of(start), mirror.byte_of(end));
Some(
rows
.into_iter()
.map(|(left, row, right)| geometry.in_view(left, row, right - left))
.collect::<Vec<_>>(),
)
})
});
let rows = rows.unwrap_or_default();
let class = selection_rect_class();
for (index, row) in rows.iter().enumerate() {
let rect: Retained<AnyObject> = unsafe { msg_send![class, new] };
let parts = [
(c"lurqX", row.origin.x),
(c"lurqY", row.origin.y),
(c"lurqWidth", row.size.width),
(c"lurqHeight", row.size.height),
(c"lurqStart", f64::from(u8::from(index == 0))),
(c"lurqEnd", f64::from(u8::from(index + 1 == rows.len()))),
];
for (name, value) in parts {
let ivar = class.instance_variable(name).expect("an ivar the class was made with");
unsafe { *ivar.load_ptr::<f64>(&rect) = value };
}
let _: () = unsafe { msg_send![&*rects, addObject: &*rect] };
}
Retained::autorelease_ptr(rects)
}
fn nearest_to(point: CGPoint) -> isize {
with_mirror(0, |mirror| match &mirror.geometry {
Some(geometry) => mirror.utf16_of(geometry.nearest(point)),
None => mirror.utf16_of(mirror.selection.1),
})
}
extern "C-unwind" fn closest_position_to_point(_this: &AnyObject, _cmd: Sel, point: CGPoint) -> *mut AnyObject {
position(nearest_to(point))
}
extern "C-unwind" fn closest_position_within_range(
_this: &AnyObject,
_cmd: Sel,
point: CGPoint,
within: *mut AnyObject,
) -> *mut AnyObject {
let nearest = nearest_to(point);
match ends_of(within) {
Some((start, end)) => position(nearest.clamp(start, end)),
None => position(nearest),
}
}
extern "C-unwind" fn character_range_at_point(_this: &AnyObject, _cmd: Sel, point: CGPoint) -> *mut AnyObject {
let nearest = nearest_to(point);
let length = with_mirror(0, |mirror| mirror.utf16_len());
if nearest >= length {
return std::ptr::null_mut();
}
range(nearest, nearest + 1)
}
fn selection_changed_by(this: &AnyObject, change: impl FnOnce()) {
let delegate = INPUT_DELEGATE.load(Ordering::Acquire);
if let Some(delegate) = unsafe { delegate.as_ref() } {
let _: () = unsafe { msg_send![delegate, selectionWillChange: this] };
}
change();
if let Some(delegate) = unsafe { delegate.as_ref() } {
let _: () = unsafe { msg_send![delegate, selectionDidChange: this] };
}
}
fn selected_text() -> Option<String> {
with_mirror(None, |mirror| {
let (start, end) = mirror.selection;
let secure = mirror.traits.is_some_and(|traits| traits.secure);
(start != end && !secure).then(|| mirror.text[start..end].to_owned())
})
}
fn pasteboard() -> Retained<AnyObject> {
unsafe { msg_send![class!(UIPasteboard), generalPasteboard] }
}
extern "C-unwind" fn copy(_this: &AnyObject, _cmd: Sel, _sender: *mut AnyObject) {
if let Some(text) = selected_text() {
let _: () = unsafe { msg_send![&*pasteboard(), setString: ns_string(&text)] };
}
}
extern "C-unwind" fn cut(this: &AnyObject, cmd: Sel, sender: *mut AnyObject) {
if selected_text().is_none() {
return;
}
copy(this, cmd, sender);
delete_backward(this, cmd);
}
extern "C-unwind" fn paste(this: &AnyObject, cmd: Sel, _sender: *mut AnyObject) {
let text: Option<Retained<AnyObject>> = unsafe { msg_send![&*pasteboard(), string] };
if let Some(text) = text {
insert_text(this, cmd, Retained::as_ptr(&text).cast_mut());
}
}
extern "C-unwind" fn select_all(this: &AnyObject, _cmd: Sel, _sender: *mut AnyObject) {
selection_changed_by(this, || {
with_mirror((), |mirror| {
mirror.selection = (0, mirror.text.len());
mirror.edits.push(Edit::Select {
start: 0,
end: mirror.text.len(),
});
});
});
}
extern "C-unwind" fn can_perform_action(_this: &AnyObject, _cmd: Sel, action: Sel, _sender: *mut AnyObject) -> Bool {
let selected = selected_text().is_some();
let allowed = if action == sel!(copy:) || action == sel!(cut:) {
selected
} else if action == sel!(paste:) {
unsafe { msg_send![&*pasteboard(), hasStrings] }
} else if action == sel!(selectAll:) {
with_mirror(false, |mirror| {
!mirror.text.is_empty() && mirror.selection != (0, mirror.text.len())
})
} else {
false
};
Bool::new(allowed)
}
static UNDO_MANAGER: AtomicPtr<AnyObject> = AtomicPtr::new(std::ptr::null_mut());
extern "C-unwind" fn undo_manager(_this: &AnyObject, _cmd: Sel) -> *mut AnyObject {
let manager = UNDO_MANAGER.load(Ordering::Acquire);
if !manager.is_null() {
return manager;
}
let manager: Retained<AnyObject> = unsafe { msg_send![undo_manager_class(), new] };
let manager = Retained::into_raw(manager);
UNDO_MANAGER.store(manager, Ordering::Release);
manager
}
extern "C-unwind" fn can_undo(_this: &AnyObject, _cmd: Sel) -> Bool {
Bool::new(with_mirror(false, |mirror| mirror.can_undo))
}
extern "C-unwind" fn can_redo(_this: &AnyObject, _cmd: Sel) -> Bool {
Bool::new(with_mirror(false, |mirror| mirror.can_redo))
}
fn queue_history(command: crate::app::events::EditCommand) {
use crate::app::events::EditCommand;
let queued = with_mirror(false, |mirror| {
let able = match command {
EditCommand::Undo => &mut mirror.can_undo,
_ => &mut mirror.can_redo,
};
if !*able {
return false;
}
match command {
EditCommand::Undo => mirror.can_redo = true,
_ => mirror.can_undo = true,
}
mirror.edits.push(Edit::Command(command));
true
});
if queued {
wake_shell();
}
}
extern "C-unwind" fn undo(_this: &AnyObject, _cmd: Sel) {
queue_history(crate::app::events::EditCommand::Undo);
}
extern "C-unwind" fn redo(_this: &AnyObject, _cmd: Sel) {
queue_history(crate::app::events::EditCommand::Redo);
}
fn undo_manager_class() -> &'static AnyClass {
if let Some(class) = AnyClass::get(c"LurqTextUndoManager") {
return class;
}
let mut builder = ClassBuilder::new(c"LurqTextUndoManager", class!(NSUndoManager)).expect("a class name of its own");
unsafe {
builder.add_method(sel!(canUndo), can_undo as extern "C-unwind" fn(_, _) -> _);
builder.add_method(sel!(canRedo), can_redo as extern "C-unwind" fn(_, _) -> _);
builder.add_method(sel!(undo), undo as extern "C-unwind" fn(_, _));
builder.add_method(sel!(redo), redo as extern "C-unwind" fn(_, _));
}
builder.register()
}
const TREE_KEYS: &[&str] = &["Tab", "Escape"];
extern "C-unwind" fn presses_began(this: &AnyObject, _cmd: Sel, presses: *mut AnyObject, event: *mut AnyObject) {
if !super::ios::take_presses(presses, true, Some(TREE_KEYS)) {
let _: () = unsafe { msg_send![super(this, class!(UIView)), pressesBegan: presses, withEvent: event] };
}
}
extern "C-unwind" fn presses_ended(this: &AnyObject, _cmd: Sel, presses: *mut AnyObject, event: *mut AnyObject) {
if !super::ios::take_presses(presses, false, Some(TREE_KEYS)) {
let _: () = unsafe { msg_send![super(this, class!(UIView)), pressesEnded: presses, withEvent: event] };
}
}
fn traits() -> Option<KeyboardTraits> {
with_mirror(None, |mirror| mirror.traits)
}
extern "C-unwind" fn is_secure_text_entry(_this: &AnyObject, _cmd: Sel) -> Bool {
Bool::new(traits().is_some_and(|traits| traits.secure))
}
extern "C-unwind" fn keyboard_type(_this: &AnyObject, _cmd: Sel) -> isize {
match traits().map(|traits| traits.kind) {
Some(KeyboardKind::Url) => 3,
Some(KeyboardKind::Number) => 4,
Some(KeyboardKind::Phone) => 5,
Some(KeyboardKind::Email) => 7,
Some(KeyboardKind::Decimal) => 8,
Some(KeyboardKind::Search) => 10,
Some(KeyboardKind::Text | KeyboardKind::None) | None => 0,
}
}
extern "C-unwind" fn return_key_type(_this: &AnyObject, _cmd: Sel) -> isize {
match traits().map(|traits| traits.return_key) {
Some(ReturnKey::Go) => 1,
Some(ReturnKey::Next) => 4,
Some(ReturnKey::Search) => 6,
Some(ReturnKey::Send) => 7,
Some(ReturnKey::Done) => 9,
Some(ReturnKey::Default) | None => 0,
}
}
#[link(name = "UIKit", kind = "framework")]
unsafe extern "C" {
static UITextContentTypeName: *mut AnyObject;
static UITextContentTypeEmailAddress: *mut AnyObject;
static UITextContentTypeUsername: *mut AnyObject;
static UITextContentTypePassword: *mut AnyObject;
static UITextContentTypeNewPassword: *mut AnyObject;
static UITextContentTypeOneTimeCode: *mut AnyObject;
static UITextContentTypeTelephoneNumber: *mut AnyObject;
static UITextContentTypeURL: *mut AnyObject;
}
extern "C-unwind" fn text_content_type(_this: &AnyObject, _cmd: Sel) -> *mut AnyObject {
unsafe {
match traits().map(|traits| traits.content) {
Some(ContentKind::Name) => UITextContentTypeName,
Some(ContentKind::Email) => UITextContentTypeEmailAddress,
Some(ContentKind::Username) => UITextContentTypeUsername,
Some(ContentKind::Password) => UITextContentTypePassword,
Some(ContentKind::NewPassword) => UITextContentTypeNewPassword,
Some(ContentKind::OneTimeCode) => UITextContentTypeOneTimeCode,
Some(ContentKind::Phone) => UITextContentTypeTelephoneNumber,
Some(ContentKind::Url) => UITextContentTypeURL,
Some(ContentKind::Auto | ContentKind::None) | None => std::ptr::null_mut(),
}
}
}
fn is_prose() -> bool {
traits().is_none_or(|traits| !traits.secure && matches!(traits.kind, KeyboardKind::Text | KeyboardKind::Search))
}
extern "C-unwind" fn autocapitalization_type(_this: &AnyObject, _cmd: Sel) -> isize {
if is_prose() { 2 } else { 0 }
}
extern "C-unwind" fn correction_type(_this: &AnyObject, _cmd: Sel) -> isize {
if is_prose() { 0 } else { 1 }
}
fn text_input_class() -> &'static AnyClass {
if let Some(class) = AnyClass::get(c"LurqTextInputView") {
return class;
}
let mut builder = ClassBuilder::new(c"LurqTextInputView", class!(UIView)).expect("a class name of its own");
for protocol in [c"UIKeyInput", c"UITextInputTraits", c"UITextInput"] {
if let Some(protocol) = AnyProtocol::get(protocol) {
builder.add_protocol(protocol);
}
}
macro_rules! methods {
($($selector:expr => $function:expr),* $(,)?) => {
unsafe {
$(builder.add_method($selector, $function);)*
}
};
}
methods! {
sel!(canBecomeFirstResponder) => can_become_first_responder as extern "C-unwind" fn(_, _) -> _,
sel!(hasText) => has_text as extern "C-unwind" fn(_, _) -> _,
sel!(insertText:) => insert_text as extern "C-unwind" fn(_, _, _),
sel!(deleteBackward) => delete_backward as extern "C-unwind" fn(_, _),
sel!(textInRange:) => text_in_range as extern "C-unwind" fn(_, _, _) -> _,
sel!(replaceRange:withText:) => replace_range as extern "C-unwind" fn(_, _, _, _),
sel!(selectedTextRange) => selected_text_range as extern "C-unwind" fn(_, _) -> _,
sel!(setSelectedTextRange:) => set_selected_text_range as extern "C-unwind" fn(_, _, _),
sel!(markedTextRange) => marked_text_range as extern "C-unwind" fn(_, _) -> _,
sel!(markedTextStyle) => marked_text_style as extern "C-unwind" fn(_, _) -> _,
sel!(setMarkedTextStyle:) => set_marked_text_style as extern "C-unwind" fn(_, _, _),
sel!(setMarkedText:selectedRange:) => set_marked_text as extern "C-unwind" fn(_, _, _, _),
sel!(unmarkText) => unmark_text as extern "C-unwind" fn(_, _),
sel!(beginningOfDocument) => beginning_of_document as extern "C-unwind" fn(_, _) -> _,
sel!(endOfDocument) => end_of_document as extern "C-unwind" fn(_, _) -> _,
sel!(textRangeFromPosition:toPosition:) => text_range_from_position as extern "C-unwind" fn(_, _, _, _) -> _,
sel!(positionFromPosition:offset:) => position_from_position as extern "C-unwind" fn(_, _, _, _) -> _,
sel!(positionFromPosition:inDirection:offset:) =>
position_in_direction as extern "C-unwind" fn(_, _, _, _, _) -> _,
sel!(comparePosition:toPosition:) => compare_position as extern "C-unwind" fn(_, _, _, _) -> _,
sel!(offsetFromPosition:toPosition:) => offset_from_position as extern "C-unwind" fn(_, _, _, _) -> _,
sel!(inputDelegate) => input_delegate as extern "C-unwind" fn(_, _) -> _,
sel!(setInputDelegate:) => set_input_delegate as extern "C-unwind" fn(_, _, _),
sel!(tokenizer) => tokenizer as extern "C-unwind" fn(_, _) -> _,
sel!(positionWithinRange:farthestInDirection:) =>
position_within_range as extern "C-unwind" fn(_, _, _, _) -> _,
sel!(characterRangeByExtendingPosition:inDirection:) =>
character_range_by_extending as extern "C-unwind" fn(_, _, _, _) -> _,
sel!(baseWritingDirectionForPosition:inDirection:) =>
base_writing_direction as extern "C-unwind" fn(_, _, _, _) -> _,
sel!(setBaseWritingDirection:forRange:) => set_base_writing_direction as extern "C-unwind" fn(_, _, _, _),
sel!(textInputView) => text_input_view as extern "C-unwind" fn(_, _) -> _,
sel!(firstRectForRange:) => first_rect_for_range as extern "C-unwind" fn(_, _, _) -> _,
sel!(caretRectForPosition:) => caret_rect_for_position as extern "C-unwind" fn(_, _, _) -> _,
sel!(selectionRectsForRange:) => selection_rects_for_range as extern "C-unwind" fn(_, _, _) -> _,
sel!(closestPositionToPoint:) => closest_position_to_point as extern "C-unwind" fn(_, _, _) -> _,
sel!(closestPositionToPoint:withinRange:) =>
closest_position_within_range as extern "C-unwind" fn(_, _, _, _) -> _,
sel!(characterRangeAtPoint:) => character_range_at_point as extern "C-unwind" fn(_, _, _) -> _,
sel!(copy:) => copy as extern "C-unwind" fn(_, _, _),
sel!(cut:) => cut as extern "C-unwind" fn(_, _, _),
sel!(paste:) => paste as extern "C-unwind" fn(_, _, _),
sel!(selectAll:) => select_all as extern "C-unwind" fn(_, _, _),
sel!(canPerformAction:withSender:) => can_perform_action as extern "C-unwind" fn(_, _, _, _) -> _,
sel!(undoManager) => undo_manager as extern "C-unwind" fn(_, _) -> _,
sel!(pressesBegan:withEvent:) => presses_began as extern "C-unwind" fn(_, _, _, _),
sel!(pressesEnded:withEvent:) => presses_ended as extern "C-unwind" fn(_, _, _, _),
sel!(isSecureTextEntry) => is_secure_text_entry as extern "C-unwind" fn(_, _) -> _,
sel!(keyboardType) => keyboard_type as extern "C-unwind" fn(_, _) -> _,
sel!(returnKeyType) => return_key_type as extern "C-unwind" fn(_, _) -> _,
sel!(autocapitalizationType) => autocapitalization_type as extern "C-unwind" fn(_, _) -> _,
sel!(autocorrectionType) => correction_type as extern "C-unwind" fn(_, _) -> _,
sel!(spellCheckingType) => correction_type as extern "C-unwind" fn(_, _) -> _,
sel!(textContentType) => text_content_type as extern "C-unwind" fn(_, _) -> _,
sel!(isKindOfClass:) => is_kind_of_class as extern "C-unwind" fn(_, _, _) -> _,
sel!(forwardingTargetForSelector:) => forwarding_target as extern "C-unwind" fn(_, _, _) -> _,
}
builder.register()
}
static FIELD_STAND_IN: AtomicPtr<AnyObject> = AtomicPtr::new(std::ptr::null_mut());
fn field_stand_in() -> *mut AnyObject {
let field = FIELD_STAND_IN.load(Ordering::Acquire);
if !field.is_null() {
return field;
}
let field: Retained<AnyObject> = unsafe { msg_send![class!(UITextField), new] };
let field = Retained::into_raw(field);
FIELD_STAND_IN.store(field, Ordering::Release);
field
}
fn is_secure() -> bool {
traits().is_some_and(|traits| traits.secure)
}
extern "C-unwind" fn is_kind_of_class(this: &AnyObject, _cmd: Sel, class: *const AnyClass) -> Bool {
let is: Bool = unsafe { msg_send![super(this, class!(UIView)), isKindOfClass: class] };
Bool::new(is.as_bool() || (is_secure() && std::ptr::eq(class, class!(UITextField))))
}
extern "C-unwind" fn forwarding_target(_this: &AnyObject, _cmd: Sel, _selector: Sel) -> *mut AnyObject {
if is_secure() {
field_stand_in()
} else {
std::ptr::null_mut()
}
}
enum Changed {
Text,
Selection,
Place,
}
fn quiet_window_view(view: &AnyObject) {
extern "C-unwind" fn is_editable(_this: &AnyObject, _cmd: Sel) -> Bool {
Bool::NO
}
extern "C-unwind" fn can_become_first_responder(_this: &AnyObject, _cmd: Sel) -> Bool {
Bool::new(!TYPES.load(Ordering::Acquire))
}
let class = view.class();
let types = c"B@:";
unsafe {
let class = std::ptr::from_ref(class).cast_mut();
objc2::ffi::class_addMethod(
class.cast(),
sel!(isEditable),
std::mem::transmute::<extern "C-unwind" fn(&AnyObject, Sel) -> Bool, unsafe extern "C-unwind" fn()>(is_editable),
types.as_ptr(),
);
objc2::ffi::class_replaceMethod(
class.cast(),
sel!(canBecomeFirstResponder),
std::mem::transmute::<extern "C-unwind" fn(&AnyObject, Sel) -> Bool, unsafe extern "C-unwind" fn()>(
can_become_first_responder,
),
types.as_ptr(),
);
}
}
pub(super) struct TextInput {
view: Retained<AnyObject>,
peers: Vec<Peer>,
}
struct Peer {
id: crate::core::NodeId,
field: Retained<AnyObject>,
value: String,
place: Option<(f64, f64)>,
}
impl TextInput {
pub(super) fn new(parent: NonNull<c_void>) -> Self {
let parent: &AnyObject = unsafe { parent.cast::<AnyObject>().as_ref() };
WINDOW_VIEW.store(std::ptr::from_ref(parent).cast_mut(), Ordering::Release);
quiet_window_view(parent);
Self {
view: Self::view_in(parent),
peers: Vec::new(),
}
}
fn view_in(parent: &AnyObject) -> Retained<AnyObject> {
unsafe {
let view: Retained<AnyObject> = msg_send![text_input_class(), new];
let _: () = msg_send![&*view, setUserInteractionEnabled: false];
let _: () = msg_send![parent, addSubview: &*view];
Self::interact(&view);
TYPING_VIEW.store(Retained::as_ptr(&view).cast_mut(), Ordering::Release);
INPUT_DELEGATE.store(std::ptr::null_mut(), Ordering::Release);
let tokenizer = TOKENIZER.swap(std::ptr::null_mut(), Ordering::AcqRel);
drop(Retained::from_raw(tokenizer));
view
}
}
fn interact(view: &AnyObject) {
unsafe {
let interaction: Retained<AnyObject> = msg_send![class!(UITextInteraction), textInteractionForMode: 0isize];
let _: () = msg_send![&*interaction, setTextInput: view];
let _: () = msg_send![view, addInteraction: &*interaction];
}
}
fn reread_selection(&self) {
unsafe {
let interactions: Retained<AnyObject> = msg_send![&*self.view, interactions];
let count: usize = msg_send![&*interactions, count];
for index in 0..count {
let interaction: Retained<AnyObject> = msg_send![&*interactions, objectAtIndex: index];
let _: () = msg_send![&*self.view, removeInteraction: &*interaction];
}
}
Self::interact(&self.view);
self.show_selection(true);
self.update_selection();
}
fn renew(&mut self) -> Retained<AnyObject> {
let parent: Option<Retained<AnyObject>> = unsafe { msg_send![&*self.view, superview] };
match parent {
Some(parent) => std::mem::replace(&mut self.view, Self::view_in(&parent)),
None => self.view.clone(),
}
}
pub(super) fn sync(&mut self, tree: &mut Tree) {
let edits = with_mirror(Vec::new(), |mirror| std::mem::take(&mut mirror.edits));
for edit in edits {
match edit {
Edit::Insert(text) => tree.keyboard_insert(&text),
Edit::DeleteBackward => tree.keyboard_delete_backward(),
Edit::Replace { start, end, text } => tree.text_input_replace(start..end, &text),
Edit::Select { start, end } => tree.text_input_select(start..end),
Edit::Mark { text, selection } => tree.ime(ImeEvent::Preedit {
text,
cursor: selection,
}),
Edit::Unmark(text) => tree.ime(ImeEvent::Commit(text)),
Edit::Command(command) => {
tree.keyboard_edit(command);
}
}
}
let shown = self.shown_selection();
let changed = {
let mut highlight = HIGHLIGHT.lock().unwrap();
let changed = *highlight != shown;
if changed {
highlight.clone_from(&shown);
}
changed
};
if changed {
tree.set_native_selection_rects(
shown
.into_iter()
.map(|(x, y, width, height)| (x as f32, y as f32, width as f32, height as f32))
.collect(),
);
}
let typing = MIRROR
.lock()
.unwrap()
.as_ref()
.is_some_and(|mirror| mirror.traits.is_some());
let other_peers = self.sync_peers(tree);
let active = unsafe {
let app: Retained<AnyObject> = msg_send![class!(UIApplication), sharedApplication];
let state: isize = msg_send![&*app, applicationState];
state == 0
};
let first: bool = unsafe { msg_send![&*self.view, isFirstResponder] };
if typing && active && !first {
tree.keyboard_was_put_away();
}
let traits = tree.keyboard_traits();
let snapshot = tree.text_input_snapshot();
let (Some(traits), Some(snapshot)) = (traits, snapshot) else {
self.dismiss();
return;
};
let previous = with_mirror(None, |mirror| mirror.traits);
let another = match previous {
None => true,
Some(previous) => previous.secure != traits.secure || previous.content != traits.content || other_peers,
};
let before = another.then(|| self.renew());
let geometry = snapshot.geometry.as_ref().map(|geometry| Geometry {
bounds: (
f64::from(geometry.bounds.0),
f64::from(geometry.bounds.1),
f64::from(geometry.bounds.2),
f64::from(geometry.bounds.3),
),
carets: geometry
.carets
.iter()
.map(|(index, x, y)| (*index, f64::from(*x), f64::from(*y)))
.collect(),
line_height: f64::from(geometry.line_height),
});
let moved = with_mirror(true, |mirror| mirror.geometry != geometry);
let changed = Self::changed(&snapshot).or(moved.then_some(Changed::Place));
if let Some(changed) = &changed {
self.tell(changed, true);
}
{
let mut mirror = MIRROR.lock().unwrap();
let mirror = mirror.get_or_insert_with(Mirror::default);
mirror.traits = Some(traits);
mirror.geometry = geometry.clone();
mirror.can_undo = snapshot.can_undo;
mirror.can_redo = snapshot.can_redo;
if matches!(changed, Some(Changed::Text | Changed::Selection)) {
mirror.text = snapshot.text;
mirror.selection = (snapshot.selection.start, snapshot.selection.end);
mirror.marked = snapshot.composition.map(|marked| (marked.start, marked.end));
}
}
if (moved || another)
&& let Some(geometry) = &geometry
{
let frame = CGRect {
origin: CGPoint {
x: geometry.bounds.0,
y: geometry.bounds.1,
},
size: CGSize {
width: geometry.bounds.2,
height: geometry.bounds.3,
},
};
unsafe {
let _: () = msg_send![&*self.view, setFrame: frame];
let _: () = msg_send![&*self.view, setUserInteractionEnabled: true];
}
} else if another {
let frame = CGRect {
origin: CGPoint { x: 0.0, y: 1.0 },
size: CGSize {
width: 1.0,
height: 1.0,
},
};
let _: () = unsafe { msg_send![&*self.view, setFrame: frame] };
}
if let Some(changed) = &changed {
self.tell(changed, false);
self.update_selection();
}
let astray = with_mirror(false, |mirror| std::mem::take(&mut mirror.caret_astray));
if before.is_none() && (astray || matches!(changed, Some(Changed::Text | Changed::Selection))) {
self.reread_selection();
}
TYPES.store(true, Ordering::Release);
unsafe {
match (before, previous) {
(Some(before), _) => {
let _: () = msg_send![&*before, setFrame: no_rect()];
let _: bool = msg_send![&*self.view, becomeFirstResponder];
self.show_selection(true);
let _: () = msg_send![&*before, removeFromSuperview];
}
(None, Some(previous)) if previous != traits => {
let _: () = msg_send![&*self.view, reloadInputViews];
}
(None, _) => {}
}
}
}
fn show_selection(&self, shown: bool) {
if shown {
self.lower_surface();
}
if let Some(display) = self.selection_display() {
unsafe {
let _: () = msg_send![&*display, setActivated: shown];
let highlight: Option<Retained<AnyObject>> = msg_send![&*display, highlightView];
if let Some(highlight) = highlight {
let _: () = msg_send![&*highlight, setAlpha: 0.0f64];
}
let _: () = msg_send![&*display, setNeedsSelectionUpdate];
}
}
}
fn shown_selection(&self) -> Vec<(f64, f64, f64, f64)> {
let Some(display) = self.selection_display() else {
return Vec::new();
};
unsafe {
let highlight: Option<Retained<AnyObject>> = msg_send![&*display, highlightView];
let Some(highlight) = highlight else {
return Vec::new();
};
let rects: Option<Retained<AnyObject>> = msg_send![&*highlight, selectionRects];
let Some(rects) = rects else {
return Vec::new();
};
let count: usize = msg_send![&*rects, count];
(0..count)
.map(|index| {
let rect: Retained<AnyObject> = msg_send![&*rects, objectAtIndex: index];
let rect: CGRect = msg_send![&*rect, rect];
let rect: CGRect = msg_send![&*highlight, convertRect: rect, toView: std::ptr::null::<AnyObject>()];
(rect.origin.x, rect.origin.y, rect.size.width, rect.size.height)
})
.collect()
}
}
fn lower_surface(&self) {
unsafe {
let parent: Option<Retained<AnyObject>> = msg_send![&*self.view, superview];
let Some(parent) = parent else {
return;
};
let layer: Retained<AnyObject> = msg_send![&*parent, layer];
let sublayers: Option<Retained<AnyObject>> = msg_send![&*layer, sublayers];
let Some(sublayers) = sublayers else {
return;
};
let count: usize = msg_send![&*sublayers, count];
for index in 0..count {
let sublayer: Retained<AnyObject> = msg_send![&*sublayers, objectAtIndex: index];
let renders: bool = msg_send![&*sublayer, isKindOfClass: class!(CAMetalLayer)];
if renders {
let _: () = msg_send![&*sublayer, setZPosition: -1.0f64];
}
}
}
}
fn update_selection(&self) {
if let Some(display) = self.selection_display() {
let _: () = unsafe { msg_send![&*display, setNeedsSelectionUpdate] };
let _: () = unsafe { msg_send![&*display, layoutManagedSubviews] };
}
}
fn selection_display(&self) -> Option<Retained<AnyObject>> {
unsafe {
let interactions: Retained<AnyObject> = msg_send![&*self.view, interactions];
let count: usize = msg_send![&*interactions, count];
(0..count)
.map(|index| -> Retained<AnyObject> { msg_send![&*interactions, objectAtIndex: index] })
.find(|interaction| msg_send![&**interaction, isKindOfClass: class!(UITextSelectionDisplayInteraction)])
}
}
fn changed(snapshot: &TextInputSnapshot) -> Option<Changed> {
let mirror = MIRROR.lock().unwrap();
let Some(mirror) = mirror.as_ref() else {
return Some(Changed::Text);
};
if mirror.text != snapshot.text {
Some(Changed::Text)
} else if mirror.selection != (snapshot.selection.start, snapshot.selection.end)
|| mirror.marked != snapshot.composition.as_ref().map(|marked| (marked.start, marked.end))
{
Some(Changed::Selection)
} else {
None
}
}
fn tell(&self, changed: &Changed, before: bool) {
let delegate = INPUT_DELEGATE.load(Ordering::Acquire);
let Some(delegate) = (unsafe { delegate.as_ref() }) else {
return;
};
unsafe {
let _: () = match (changed, before) {
(Changed::Text, true) => msg_send![delegate, textWillChange: &*self.view],
(Changed::Text, false) => msg_send![delegate, textDidChange: &*self.view],
(Changed::Selection, true) => msg_send![delegate, selectionWillChange: &*self.view],
(Changed::Selection, false) => msg_send![delegate, selectionDidChange: &*self.view],
(Changed::Place, _) => return,
};
}
}
fn sync_peers(&mut self, tree: &mut Tree) -> bool {
let wanted = tree.autofill_peers();
let mut other = false;
self.peers.retain(|peer| {
let kept = wanted.iter().any(|wanted| wanted.id == peer.id);
if !kept {
let _: () = unsafe { msg_send![&*peer.field, removeFromSuperview] };
other = true;
}
kept
});
for wanted in &wanted {
if self.peers.iter().any(|peer| peer.id == wanted.id) {
continue;
}
other = true;
unsafe {
let field: Retained<AnyObject> = msg_send![class!(UITextField), new];
let _: () = msg_send![&*field, setAlpha: 0.01f64];
let _: () = msg_send![&*field, setUserInteractionEnabled: false];
let content = match wanted.content {
ContentKind::Email => UITextContentTypeEmailAddress,
ContentKind::Username => UITextContentTypeUsername,
ContentKind::Password => UITextContentTypePassword,
ContentKind::NewPassword => UITextContentTypeNewPassword,
_ if wanted.secure => UITextContentTypePassword,
_ => std::ptr::null_mut(),
};
let _: () = msg_send![&*field, setTextContentType: content];
let _: () = msg_send![&*field, setSecureTextEntry: wanted.secure];
let superview: Option<Retained<AnyObject>> = msg_send![&*self.view, superview];
if let Some(superview) = superview {
let _: () = msg_send![&*superview, insertSubview: &*field, belowSubview: &*self.view];
}
self.peers.push(Peer {
id: wanted.id,
field,
value: String::from("\u{0}"),
place: None,
});
}
}
let password =
|peer: &AutofillPeer| peer.secure || matches!(peer.content, ContentKind::Password | ContentKind::NewPassword);
let bottom = unsafe {
let superview: Option<Retained<AnyObject>> = msg_send![&*self.view, superview];
match superview {
Some(superview) => {
let bounds: CGRect = msg_send![&*superview, bounds];
(bounds.size.height - 1.0).max(1.0)
}
None => 1.0,
}
};
let (mut before, mut after) = (0.0, 0.0);
let rows: [&dyn Fn(&AutofillPeer) -> bool; 3] = [
&|peer| !password(peer) && peer.content != ContentKind::Username,
&|peer| !password(peer) && peer.content == ContentKind::Username,
&|peer| password(peer),
];
for (row, belongs) in rows.iter().enumerate() {
for wanted in wanted.iter().filter(|wanted| belongs(wanted)) {
let Some(peer) = self.peers.iter_mut().find(|peer| peer.id == wanted.id) else {
continue;
};
let place = if row == 2 {
after += 1.0;
(after - 1.0, bottom)
} else {
before += 1.0;
(before - 1.0, 0.0)
};
if peer.place != Some(place) {
let frame = CGRect {
origin: CGPoint { x: place.0, y: place.1 },
size: CGSize {
width: 1.0,
height: 1.0,
},
};
let _: () = unsafe { msg_send![&*peer.field, setFrame: frame] };
peer.place = Some(place);
}
}
}
for peer in &mut self.peers {
let Some(wanted) = wanted.iter().find(|wanted| wanted.id == peer.id) else {
continue;
};
let said = unsafe {
let text: *mut AnyObject = msg_send![&*peer.field, text];
string_of(text).unwrap_or_default()
};
if peer.value != "\u{0}" && said != peer.value {
tree.autofill(peer.id, &said);
peer.value = said;
} else if wanted.value != peer.value {
let _: () = unsafe { msg_send![&*peer.field, setText: ns_string(&wanted.value)] };
peer.value.clone_from(&wanted.value);
}
}
other
}
fn dismiss(&mut self) {
if MIRROR.lock().unwrap().take().is_none() {
return;
}
unsafe {
self.show_selection(false);
let _: () = msg_send![&*self.view, setUserInteractionEnabled: false];
let _: () = msg_send![&*self.view, setHidden: true];
let _: bool = msg_send![&*self.view, resignFirstResponder];
}
TYPES.store(false, Ordering::Release);
}
}