-
Notifications
You must be signed in to change notification settings - Fork 882
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Soft keyboard input for iOS #3571
Closed
simlay
wants to merge
12
commits into
rust-windowing:master
from
simlay:add-soft-keyboard-input-to-ios
Closed
Changes from all commits
Commits
Show all changes
12 commits
Select commit
Hold shift + click to select a range
f9b058e
Initial work for adding soft keyboard input
simlay 78bac6b
Updated window example to set keyboard
simlay 77d049c
Updates from comments
simlay 9ce9105
cargo fmt
simlay 66c9658
More changes
simlay d3a54a2
Fixed WinitTextViewDelegate.
jpedrick e276c06
Minor changes
simlay ab4a9e9
Enabled the text to propigate to the main event loop
simlay 7200d84
Update src/platform_impl/ios/uikit/text_field.rs
simlay 11728ba
Update src/platform_impl/ios/uikit/text_field.rs
simlay 7bd5742
Update src/platform_impl/ios/uikit/text_field.rs
simlay d8094f1
minor updates from comments
simlay File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,141 @@ | ||
#![allow(clippy::unnecessary_cast)] | ||
|
||
use std::cell::RefCell; | ||
|
||
use icrate::Foundation::{CGPoint, CGRect, CGSize, MainThreadMarker, NSObject, NSObjectProtocol}; | ||
use objc2::rc::Id; | ||
use objc2::runtime::ProtocolObject; | ||
use objc2::{declare_class, msg_send_id, mutability, ClassType, DeclaredClass, extern_methods}; | ||
use super::app_state::{self, EventWrapper}; | ||
|
||
use super::uikit::{UIResponder, UITextView, UITextViewDelegate, UIView}; | ||
use super::window::WinitUIWindow; | ||
use crate::{ | ||
keyboard::{ | ||
KeyCode, | ||
PhysicalKey, | ||
Key, | ||
KeyLocation, | ||
}, | ||
dpi::PhysicalPosition, | ||
event::{Event, KeyEvent, Force, Touch, TouchPhase, WindowEvent}, | ||
platform_impl::platform::DEVICE_ID, | ||
window::{WindowAttributes, WindowId as RootWindowId}, | ||
}; | ||
|
||
pub struct WinitTextFieldState { | ||
delegate: RefCell<Id<WinitTextFieldDelegate>>, | ||
} | ||
|
||
declare_class!( | ||
pub(crate) struct WinitTextField; | ||
|
||
unsafe impl ClassType for WinitTextField { | ||
#[inherits(UIView, UIResponder, NSObject)] | ||
type Super = UITextView; | ||
type Mutability = mutability::InteriorMutable; | ||
const NAME: &'static str = "WinitUITextView"; | ||
} | ||
|
||
impl DeclaredClass for WinitTextField { | ||
type Ivars = WinitTextFieldState; | ||
} | ||
|
||
unsafe impl WinitTextField { } | ||
); | ||
extern_methods!( | ||
unsafe impl WinitTextField { | ||
fn window(&self) -> Option<Id<WinitUIWindow>> { | ||
unsafe { msg_send_id![self, window] } | ||
} | ||
Comment on lines
+48
to
+50
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'd rather have this on |
||
} | ||
); | ||
declare_class!( | ||
pub(crate) struct WinitTextFieldDelegate; | ||
|
||
unsafe impl ClassType for WinitTextFieldDelegate { | ||
type Super = NSObject; | ||
type Mutability = mutability::MainThreadOnly; | ||
const NAME: &'static str = "WinitTextViewDelegate"; | ||
} | ||
|
||
impl DeclaredClass for WinitTextFieldDelegate { | ||
type Ivars = (); | ||
} | ||
|
||
unsafe impl NSObjectProtocol for WinitTextFieldDelegate {} | ||
unsafe impl UITextViewDelegate for WinitTextFieldDelegate { | ||
#[method(textViewDidBeginEditing:)] | ||
unsafe fn text_field_did_begin_editing(&self, sender: &WinitTextField) { | ||
let text = sender.text(); | ||
//println!("DidBeginEditing: {text}"); | ||
} | ||
|
||
#[method(textViewDidEndEditing:)] | ||
unsafe fn text_field_did_end_editing(&self, sender: &WinitTextField) { | ||
let text = sender.text(); | ||
//println!("DidEndEditing: {text}"); | ||
} | ||
|
||
#[method(textViewDidChange:)] | ||
unsafe fn text_field_did_change(&self, sender: &WinitTextField) { | ||
let text = sender.text(); | ||
//println!("textViewDidChange: {text}"); | ||
sender.text_changed(); | ||
} | ||
} | ||
); | ||
|
||
|
||
impl WinitTextField { | ||
|
||
pub(crate) fn new(mtm: MainThreadMarker) -> Id<Self> { | ||
// TODO: This should be hidden someplace. | ||
let frame = CGRect { | ||
origin: CGPoint { x: -20.0, y: -50.0 }, | ||
size: CGSize { | ||
width: 200.0, | ||
height: 40.0, | ||
}, | ||
}; | ||
let delegate: Id<WinitTextFieldDelegate> = unsafe { objc2::msg_send_id![mtm.alloc(), init]}; | ||
let this = Self::alloc().set_ivars( WinitTextFieldState{ | ||
delegate: RefCell::new(delegate), | ||
}); | ||
let this: Id<WinitTextField> = unsafe { msg_send_id![super(this), init] }; | ||
|
||
{ | ||
let delegate = this.ivars().delegate.borrow(); | ||
this.setDelegate(Some(ProtocolObject::from_ref(&*delegate.clone()))); | ||
} | ||
|
||
this.setFrame(frame); | ||
|
||
this | ||
} | ||
fn text_changed(&self) { | ||
let window = self.window().unwrap(); | ||
let mtm = MainThreadMarker::new().unwrap(); | ||
let text = self.text(); | ||
let text = text.to_string(); | ||
app_state::handle_nonuser_event( | ||
mtm, | ||
EventWrapper::StaticEvent(Event::WindowEvent { | ||
window_id: RootWindowId(window.id()), | ||
event: WindowEvent::KeyboardInput { | ||
device_id: DEVICE_ID, | ||
event: KeyEvent { | ||
physical_key: PhysicalKey::Code(KeyCode::F35), | ||
logical_key: Key::Character(text.clone().into()), | ||
text: Some(text.into()), | ||
location: KeyLocation::Standard, | ||
state: crate::event::ElementState::Pressed, | ||
repeat: false, | ||
platform_specific: super::KeyEventExtra{}, | ||
}, | ||
is_synthetic: false, | ||
}, | ||
}), | ||
); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
use super::{UIView, UIResponder}; | ||
use icrate::Foundation::{NSObject, NSString}; | ||
use objc2::mutability::IsMainThreadOnly; | ||
use objc2::rc::Id; | ||
use objc2::runtime::{NSObjectProtocol, ProtocolObject}; | ||
use objc2::{extern_class, extern_methods, extern_protocol, mutability, ClassType, ProtocolType}; | ||
|
||
extern_class!( | ||
#[derive(Debug, PartialEq, Eq, Hash)] | ||
pub(crate) struct UITextView; | ||
|
||
unsafe impl ClassType for UITextView { | ||
#[inherits(UIResponder, NSObject)] | ||
type Super = UIView; | ||
type Mutability = mutability::InteriorMutable; | ||
} | ||
); | ||
extern_methods!( | ||
unsafe impl UITextView { | ||
#[method_id(text)] | ||
pub fn text(&self) -> Id<NSString>; | ||
|
||
#[method(setText:)] | ||
pub fn setText(&self, text: &NSString); | ||
|
||
#[method_id(delegate)] | ||
pub unsafe fn delegate(&self) -> Option<Id<ProtocolObject<dyn UITextViewDelegate>>>; | ||
|
||
#[method(setDelegate:)] | ||
pub fn setDelegate(&self, delegate: Option<&ProtocolObject<dyn UITextViewDelegate>>); | ||
} | ||
); | ||
extern_protocol!( | ||
pub unsafe trait UITextViewDelegate: NSObjectProtocol + IsMainThreadOnly { | ||
#[optional] | ||
#[method(textViewShouldBeginEditing:)] | ||
unsafe fn textViewShouldBeginEditing(&self, sender: &UITextView) -> bool; | ||
|
||
#[optional] | ||
#[method(textViewDidBeginEditing:)] | ||
unsafe fn textViewDidBeginEditing(&self, sender: &UITextView); | ||
|
||
#[optional] | ||
#[method(textViewShouldEndEditing:)] | ||
unsafe fn textViewShouldEndEditing(&self, sender: &UITextView) -> bool; | ||
|
||
#[optional] | ||
#[method(textViewDidEndEditing:)] | ||
unsafe fn textViewDidEndEditing(&self, sender: &UITextView); | ||
|
||
#[optional] | ||
#[method(textViewDidChange:)] | ||
unsafe fn textViewDidChange(&self, sender: &UITextView); | ||
} | ||
unsafe impl ProtocolType for dyn UITextViewDelegate {} | ||
); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I suspect you don't need to subclass
UITextField
, i.e. this class is unnecessary as long as you make sure to store theWinitTextFieldDelegate
somewhere else (e.g. onWinitView
, as done currently).