Skip to content
Merged
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
17 changes: 16 additions & 1 deletion adapters/ios/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -25,11 +25,14 @@ objc2-foundation = { version = "0.2.0", features = [
"NSAttributedString",
"NSDictionary",
"NSNotification",
"NSRange",
"NSString",
"NSValue",
"NSThread",
"NSZone",
] }
objc2-ui-kit = { version = "0.2.0", features = [
"NSText",
"UIAccessibility",
"UIAccessibilityConstants",
"UIAccessibilityContainer",
Expand All @@ -38,6 +41,8 @@ objc2-ui-kit = { version = "0.2.0", features = [
"UIGeometry",
"UIResponder",
"UIScreen",
"UITextInput",
"UITextInputTraits",
"UIView",
"UIViewController",
"UIWindow",
Expand All @@ -46,13 +51,23 @@ objc2-ui-kit = { version = "0.2.0", features = [
[dev-dependencies]
example_common = { path = "../../example_common" }
libtest-mimic = "0.8.2"
objc2-foundation = { version = "0.2.0", features = ["NSObjCRuntime", "NSRunLoop"] }
objc2-foundation = { version = "0.2.0", features = [
"NSObjCRuntime",
"NSRange",
"NSRunLoop",
"NSZone",
] }
objc2-ui-kit = { version = "0.2.0", features = [
"NSText",
"UIApplication",
"UIColor",
"UIControl",
"UIScene",
"UISceneOptions",
"UISceneSession",
"UITextField",
"UITextInput",
"UITextInputTraits",
"UIWindowScene",
] }

Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
mod text_input;

use accesskit::{
ActionHandler, ActionRequest, ActivationHandler, DeactivationHandler, Size, TreeUpdate, Vec2,
Action, ActionHandler, ActionRequest, ActivationHandler, DeactivationHandler, NodeId, Size,
TreeUpdate, Vec2,
};
use accesskit_ios::Adapter;
use example_common::UiState;
use example_common::{KeyEvent, UiState, widgets::ImeEdit};
use objc2::{
ClassType, DeclaredClass, declare_class, msg_send, msg_send_id,
mutability::MainThreadOnly,
Expand All @@ -20,10 +23,11 @@ use objc2_ui_kit::{
UIView, UIViewAutoresizing, UIViewController, UIWindow, UIWindowScene, UIWindowSceneDelegate,
};
use std::{
cell::RefCell,
cell::{Cell, RefCell},
ffi::{CString, c_char, c_int, c_void},
ptr::{NonNull, null_mut},
};
use text_input::TextInputView;

// The handlers only hold weak references to the view, which owns the adapter,
// which owns the handlers.
Expand Down Expand Up @@ -77,6 +81,8 @@ struct ContentViewIvars {
adapter: RefCell<Option<Adapter>>,
ui: RefCell<UiState>,
pending_actions: RefCell<Vec<ActionRequest>>,
text_input_view: RefCell<Option<Retained<TextInputView>>>,
show_keyboard: Cell<bool>,
}

declare_class!(
Expand Down Expand Up @@ -151,6 +157,13 @@ declare_class!(
let mut ui = self.ivars().ui.borrow_mut();
for request in &requests {
ui.do_action(request);
if request.action == Action::Click
&& ui
.focused_text_input()
.is_some_and(|input| input.id == request.target_node)
{
self.ivars().show_keyboard.set(true);
}
}
}
self.after_input();
Expand All @@ -161,9 +174,15 @@ declare_class!(
let processed = self.ivars().ui.borrow_mut().process_due_messages();
if processed {
self.update();
self.sync_keyboard();
}
self.schedule_next_message();
}

#[method(syncKeyboard)]
fn sync_keyboard_later(&self) {
self.sync_keyboard();
}
}
);

Expand All @@ -173,6 +192,8 @@ impl ContentView {
adapter: RefCell::new(None),
ui: RefCell::new(UiState::new()),
pending_actions: RefCell::new(Vec::new()),
text_input_view: RefCell::new(None),
show_keyboard: Cell::new(false),
});
let this: Retained<Self> = unsafe { msg_send_id![super(this), initWithFrame: frame] };

Expand All @@ -194,8 +215,19 @@ impl ContentView {
ShowcaseDeactivationHandler { view: weak },
)
};
let text_input_view = TextInputView::new(mtm, &this, &adapter);
*this.ivars().adapter.borrow_mut() = Some(adapter);

// Like Flutter's `FlutterTextInputViewAccessibilityHider`, so that
// VoiceOver doesn't see the text input twice.
let text_input_hider = unsafe { UIView::new(mtm) };
unsafe {
let _: () = msg_send![&text_input_hider, setAccessibilityElementsHidden: true];
this.addSubview(&text_input_hider);
}
unsafe { text_input_hider.addSubview(&text_input_view) };
*this.ivars().text_input_view.borrow_mut() = Some(text_input_view);

this
}

Expand Down Expand Up @@ -234,6 +266,37 @@ impl ContentView {
fn after_input(&self) {
self.update();
self.schedule_next_message();
self.sync_keyboard();
}

fn sync_keyboard(&self) {
let text_input_view = self.ivars().text_input_view.borrow().clone();
if let Some(text_input_view) = text_input_view {
text_input_view.sync(self.ivars().show_keyboard.take());
}
}

fn edit_text(&self, id: NodeId, edits: &[ImeEdit]) {
{
let mut ui = self.ivars().ui.borrow_mut();
for edit in edits {
ui.edit_text(id, edit);
}
}
self.after_keyboard_input();
}

fn handle_key(&self, event: KeyEvent) {
self.ivars().ui.borrow_mut().handle_key(event);
self.after_keyboard_input();
}

/// The keyboard is in the middle of a call into the text input view, so
/// focus changes have to wait for the run loop.
fn after_keyboard_input(&self) {
self.update();
self.schedule_next_message();
self.perform_after_delay(sel!(syncKeyboard), 0.0);
}

fn schedule_next_message(&self) {
Expand Down
Loading
Loading