-
Notifications
You must be signed in to change notification settings - Fork 867
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Initial work for adding soft keyboard input
- Loading branch information
Showing
10 changed files
with
190 additions
and
2 deletions.
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
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 |
---|---|---|
|
@@ -65,6 +65,7 @@ mod monitor; | |
mod uikit; | ||
mod view; | ||
mod window; | ||
mod text_field; | ||
|
||
use std::fmt; | ||
|
||
|
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,92 @@ | ||
#![allow(clippy::unnecessary_cast)] | ||
|
||
use icrate::Foundation::{CGRect, CGPoint, CGSize, MainThreadMarker, NSObject, NSObjectProtocol}; | ||
use objc2::rc::Id; | ||
use objc2::runtime::ProtocolObject; | ||
use objc2::{ | ||
declare_class, msg_send_id, mutability, sel, ClassType, DeclaredClass, | ||
}; | ||
|
||
use super::uikit::{ | ||
UITextView, UITextViewDelegate, | ||
UIResponder, | ||
}; | ||
|
||
declare_class!( | ||
pub(crate) struct WinitTextField; | ||
|
||
unsafe impl ClassType for WinitTextField { | ||
#[inherits(UIResponder, NSObject)] | ||
type Super = UITextView; | ||
type Mutability = mutability::InteriorMutable; | ||
const NAME: &'static str = "WinitUITextView"; | ||
} | ||
|
||
impl DeclaredClass for WinitTextField { } | ||
|
||
unsafe impl WinitTextField { } | ||
); | ||
|
||
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 textViewDidBeginEditing(&self, sender: &UITextView) { | ||
let text = sender.text(); | ||
println!("DidBeginEditing: {text}"); | ||
} | ||
|
||
#[method(textViewDidEndEditing:)] | ||
unsafe fn textViewDidEndEditing(&self, sender: &UITextView) { | ||
let text = sender.text(); | ||
println!("DidEndEditing: {text}"); | ||
} | ||
|
||
#[method(textViewDidChange:)] | ||
unsafe fn textViewDidChange(&self, sender: &UITextView) { | ||
let text = sender.text(); | ||
println!("ShouldEndEditing: {text}"); | ||
} | ||
|
||
} | ||
); | ||
|
||
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 this: Id<WinitTextField> = unsafe { msg_send_id![Self::alloc(), init] }; | ||
this.setFrame(frame); | ||
let delegate = mtm.alloc(); | ||
let delegate: Id<WinitTextFieldDelegate> = unsafe { msg_send_id![delegate, init] }; | ||
|
||
this.setDelegate(Some(ProtocolObject::from_ref(delegate.as_ref()))); | ||
|
||
this | ||
} | ||
} |
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,62 @@ | ||
|
||
use icrate::Foundation::{NSObject, NSString}; | ||
use objc2::{extern_class, extern_methods, mutability, ClassType, extern_protocol, ProtocolType}; | ||
use objc2::rc::Id; | ||
use objc2::runtime::{NSObjectProtocol, ProtocolObject}; | ||
use objc2::mutability::IsMainThreadOnly; | ||
use super::UIView; | ||
|
||
extern_class!( | ||
#[derive(Debug, PartialEq, Eq, Hash)] | ||
pub(crate) struct UITextView; | ||
|
||
unsafe impl ClassType for UITextView { | ||
#[inherits(NSObject)] | ||
type Super = UIView; | ||
type Mutability = mutability::InteriorMutable; | ||
} | ||
); | ||
extern_methods!( | ||
unsafe impl UITextView { | ||
#[method(frame)] | ||
pub fn text(&self) -> &NSString; | ||
|
||
// These are methods from UIResponder | ||
#[method(becomeFirstResponder)] | ||
pub fn focus(&self) -> bool; | ||
|
||
#[method(resignFirstResponder)] | ||
pub fn unfocus(&self) -> bool; | ||
|
||
#[method_id(@__retain_semantics Other 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
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