#[repr(C)]pub struct NSWritingToolsCoordinator { /* private fields */ }
NSWritingToolsCoordinator
only.Expand description
An object that manages interactions between Writing Tools and your custom text view.
Add a NSWritingToolsCoordinator
object to a custom view when you
want to add Writing Tools support to that view. The coordinator manages
interactions between your view and the Writing Tools UI and back-end
capabilities. When creating a coordinator, you supply a delegate object
to respond to requests from the system and provide needed information.
Your delegate delivers your view’s text to Writing Tools, incorporates
suggested changes back into your text storage, and supports the animations
that Writing Tools creates to show the state of an operation.
Create the NSWritingToolsCoordinator
object when setting up your UI, and
initialize it with a custom object that adopts the NSWritingToolsCoordinator/Delegate
protocol. Add the coordinator to the NSView/writingToolsCoordinator
property
of your view. When a coordinator is present on a view, the system adds UI elements
to initiate Writing Tools operations.
When defining the delegate, choose an object from your app that has access
to your view and its text storage. You can adopt the NSWritingToolsCoordinator/Delegate
protocol in the view itself, or in another type that your view uses to
manage content. During the interactions with Writing Tools, the delegate
gets and sets the contents of the view’s text storage and supports Writing Tools behaviors.
Note: You don’t need to create an
NSWritingToolsCoordinator
object if you display text using a <doc ://com.apple.documentation/documentation/uikit/uitextview>,NSTextField
,NSTextView
, <doc ://com.apple.documentation/documentation/swiftui/textfield>, or <doc ://com.apple.documentation/documentation/swiftui/texteditor> view. Those views already include the required support to handle Writing Tools interactions.
See also Apple’s documentation
Implementations§
Source§impl NSWritingToolsCoordinator
impl NSWritingToolsCoordinator
Sourcepub unsafe fn isWritingToolsAvailable(mtm: MainThreadMarker) -> bool
pub unsafe fn isWritingToolsAvailable(mtm: MainThreadMarker) -> bool
A Boolean value that indicates whether Writing Tools features are currently available.
The value of this property is true
when Writing Tools features are
available, and false
when they aren’t. Writing Tools support might
be unavailable because of device constraints or because the system isn’t
ready to process Writing Tools requests.
Sourcepub unsafe fn initWithDelegate(
this: Allocated<Self>,
delegate: Option<&ProtocolObject<dyn NSWritingToolsCoordinatorDelegate>>,
) -> Retained<Self>
pub unsafe fn initWithDelegate( this: Allocated<Self>, delegate: Option<&ProtocolObject<dyn NSWritingToolsCoordinatorDelegate>>, ) -> Retained<Self>
Creates a writing tools coordinator and assigns the specified delegate object to it.
- Parameters:
- delegate: An object capable of handling Writing Tools interactions for your view. The delegate must be able to modify your view’s text storage and refresh the view’s layout and appearance.
Create the coordinator object during your view’s initialization, and assign
the object to your view. Assign the coordinator to the NSView/writingToolsCoordinator
property of your view.
Sourcepub unsafe fn delegate(
&self,
) -> Option<Retained<ProtocolObject<dyn NSWritingToolsCoordinatorDelegate>>>
pub unsafe fn delegate( &self, ) -> Option<Retained<ProtocolObject<dyn NSWritingToolsCoordinatorDelegate>>>
The object that handles Writing Tools interactions for your view.
Specify this object at initialization time when creating your NSWritingToolsCoordinator
object. The object must adopt the NSWritingToolsCoordinator/Delegate
protocol, and be capable of modifying your view’s text storage and
refreshing the view’s layout and appearance.
Sourcepub unsafe fn view(&self) -> Option<Retained<NSView>>
Available on crate features NSResponder
and NSView
only.
pub unsafe fn view(&self) -> Option<Retained<NSView>>
NSResponder
and NSView
only.The view that currently uses the writing tools coordinator.
Use this property to refer to the view that currently owns the coordinator
object. The system updates this property automatically when you assign the
coordinator to the NSView/writingToolsCoordinator
property of your view.
The value of this property is nil
if there is no associated view.
Sourcepub unsafe fn effectContainerView(&self) -> Option<Retained<NSView>>
Available on crate features NSResponder
and NSView
only.
pub unsafe fn effectContainerView(&self) -> Option<Retained<NSView>>
NSResponder
and NSView
only.The view that Writing Tools uses to display visual effects during the text-rewriting process.
Writing Tools uses the view in this property to host the visual effects
it creates when making interactive changes to your view’s content.
These visual effects let people know the state of the text and provide
feedback about what’s happening to it. Set this property to a subview
that sits visually above, and covers, all of the text in your custom
text view. If you don’t assign a value to this property, the coordinator
places its own effect view in front of the subviews in your custom view.
The default value of this property is nil
.
If you display your view’s text using multiple text containers, implement the
NSWritingToolsCoordinator/Delegate/writingToolsCoordinator(_:singleContainerSubrangesOf:in:)
method to request multiple previews.
Sourcepub unsafe fn setEffectContainerView(
&self,
effect_container_view: Option<&NSView>,
)
Available on crate features NSResponder
and NSView
only.
pub unsafe fn setEffectContainerView( &self, effect_container_view: Option<&NSView>, )
NSResponder
and NSView
only.This is a weak property.
Setter for effectContainerView
.
Sourcepub unsafe fn decorationContainerView(&self) -> Option<Retained<NSView>>
Available on crate features NSResponder
and NSView
only.
pub unsafe fn decorationContainerView(&self) -> Option<Retained<NSView>>
NSResponder
and NSView
only.The view that Writing Tools uses to display background decorations such as proofreading marks.
Writing Tools uses the view in this property to host proofreading marks
and other visual elements that show any suggested changes. Set this
property to a subview situated visibly below the text in your custom
text view. It’s also satisfactory to place this view visually in front
of the text. Make sure the size of the view is big enough to
cover all of the affected text. If you don’t assign a value to this
property, the coordinator places its own decoration view behind the subviews
in your custom view. The default value of this property is nil
.
If you display your view’s text using multiple text containers, implement the
NSWritingToolsCoordinator/Delegate/writingToolsCoordinator(_:singleContainerSubrangesOf:in:)
and NSWritingToolsCoordinator/Delegate/writingToolsCoordinator(_:decorationContainerViewFor:in:)
methods to provide separate decoration views for each container.
Sourcepub unsafe fn setDecorationContainerView(
&self,
decoration_container_view: Option<&NSView>,
)
Available on crate features NSResponder
and NSView
only.
pub unsafe fn setDecorationContainerView( &self, decoration_container_view: Option<&NSView>, )
NSResponder
and NSView
only.This is a weak property.
Setter for decorationContainerView
.
Sourcepub unsafe fn state(&self) -> NSWritingToolsCoordinatorState
pub unsafe fn state(&self) -> NSWritingToolsCoordinatorState
The current level of Writing Tools activity in your view.
Use this property to determine when Writing Tools is actively making
changes to your view. During the course of Writing Tools interactions,
the system reports state changes to the delegate’s
NSWritingToolsCoordinator/Delegate/writingToolsCoordinator(_:willChangeTo:completion:)
method and updates this property accordingly.
Sourcepub unsafe fn stopWritingTools(&self)
pub unsafe fn stopWritingTools(&self)
Stops the current Writing Tools operation and dismisses the system UI.
Call this method to abort the current Writing Tools operation. This method dismisses the system’s Writing Tools UI and stops any in-flight interactions with your view. This method does not undo any changes that Writing Tools already made to your view’s content.
Sourcepub unsafe fn preferredBehavior(&self) -> NSWritingToolsBehavior
Available on crate feature NSTextCheckingClient
only.
pub unsafe fn preferredBehavior(&self) -> NSWritingToolsBehavior
NSTextCheckingClient
only.The level of Writing Tools support you want the system to provide for your view.
Use this property to request an inline or panel-based experience,
or to disable Writing Tools for your view altogether. The default
value of this property is NSWritingToolsBehavior/default
.
Sourcepub unsafe fn setPreferredBehavior(
&self,
preferred_behavior: NSWritingToolsBehavior,
)
Available on crate feature NSTextCheckingClient
only.
pub unsafe fn setPreferredBehavior( &self, preferred_behavior: NSWritingToolsBehavior, )
NSTextCheckingClient
only.Setter for preferredBehavior
.
Sourcepub unsafe fn behavior(&self) -> NSWritingToolsBehavior
Available on crate feature NSTextCheckingClient
only.
pub unsafe fn behavior(&self) -> NSWritingToolsBehavior
NSTextCheckingClient
only.The actual level of Writing Tools support the system provides for your view.
The system chooses this value based on the device capabilities, and takes
the value in the preferredBehavior
property into consideration when
making the choice. The value in this property is never the default option,
and is instead one of the specific options such as NSWritingToolsBehavior/none
,
NSWritingToolsBehavior/limited
, or NSWritingToolsBehavior/complete
.
Sourcepub unsafe fn preferredResultOptions(&self) -> NSWritingToolsResultOptions
Available on crate feature NSTextCheckingClient
only.
pub unsafe fn preferredResultOptions(&self) -> NSWritingToolsResultOptions
NSTextCheckingClient
only.The type of content you allow Writing Tools to generate for your custom text view.
Writing Tools can create plain text or rich text, and it can format text
using lists or tables as needed. If your view doesn’t support specific
types of content, specify the types you do support in this property.
The default value of this property is NSWritingToolsResult/default
,
which lets the system determine the type of content to generate.
Sourcepub unsafe fn setPreferredResultOptions(
&self,
preferred_result_options: NSWritingToolsResultOptions,
)
Available on crate feature NSTextCheckingClient
only.
pub unsafe fn setPreferredResultOptions( &self, preferred_result_options: NSWritingToolsResultOptions, )
NSTextCheckingClient
only.Setter for preferredResultOptions
.
Sourcepub unsafe fn resultOptions(&self) -> NSWritingToolsResultOptions
Available on crate feature NSTextCheckingClient
only.
pub unsafe fn resultOptions(&self) -> NSWritingToolsResultOptions
NSTextCheckingClient
only.The type of content the system generates for your custom text view.
This property contains the set of options that Writing Tools outputs
for your view. Writing Tools takes the value in the
NSWritingToolsCoordinator/preferredResultOptions
property into
consideration when determining this value.
Sourcepub unsafe fn updateRange_withText_reason_forContextWithIdentifier(
&self,
range: NSRange,
replacement_text: &NSAttributedString,
reason: NSWritingToolsCoordinatorTextUpdateReason,
context_id: &NSUUID,
)
pub unsafe fn updateRange_withText_reason_forContextWithIdentifier( &self, range: NSRange, replacement_text: &NSAttributedString, reason: NSWritingToolsCoordinatorTextUpdateReason, context_id: &NSUUID, )
Informs the coordinator about changes your app made to the text in the specified context object.
- Parameters:
- range: The range of text to replace. This range is relative to the starting location of the specified context object’s text in your view’s text storage. If you initialized the context object with the entire contents of your view’s text storage, specify the range of text you’re replacing in your text storage. However, if you initialized the context object with only a portion of your view’s text, specify a range that is relative to the starting location of the context object’s text.
- replacementText: The text that replaces the previous content in
range
. Specify an empty string to delete the text in the specified range. - reason: The reason you updated the text.
- contextID: The unique identifier of the context object that contains the text you modified.
If you make any changes to the text Writing Tools is evaluating, call this method to report those changes to your view’s coordinator object. You might make changes in response to an undo command or when someone types into the same part of your view’s text. Calling this method keeps the coordinator object informed of any changes, and ensures it delivers accurate information to its delegate. In response, the coordinator refreshes previews and other information related to your view. If the scope of the update is significantly large, the coordinator can optionally cancel the Writing Tools session altogether.
Use this method to report changes that precisely intersect your context
object’s text. The first time you call this method for a context object,
report changes only to the original attributed string in that object.
If you call this method more than once, report changes to the newly
modified version of that string. Don’t use this method to report changes
to text that comes before or after the context object. If you make
changes before your context object, report those changes separately using the
updateForReflowedTextInContextWithIdentifier(_:)
method.
Warning: Failure to call this method for a change can cause Writing Tools to deliver inaccurate information to your delegate and lead to data loss.
Sourcepub unsafe fn updateForReflowedTextInContextWithIdentifier(
&self,
context_id: &NSUUID,
)
pub unsafe fn updateForReflowedTextInContextWithIdentifier( &self, context_id: &NSUUID, )
Informs the coordinator that a change occurred to the view or its text that requires a layout update.
- Parameters:
- contextID: The unique identifier of the context object affected by the change. Pass the identifier for the context object that comes after the changes.
Use this method to inform Writing Tools when the geometry of your view changes, or when the text that precedes one of your context objects changes. Changes to the view’s geometry or text can affect the flow of any remaining text, and require a layout update. Writing Tools uses this method to refresh any layout-dependent information it’s currently tracking. For example, it uses it to refresh the location of proofreading marks it’s displaying in your view.
If a text change affects the text inside a context object, call the
updateRange(_:with:reason:forContextWithIdentifier:)
method to report
that change instead.
Methods from Deref<Target = NSObject>§
Sourcepub fn doesNotRecognizeSelector(&self, sel: Sel) -> !
pub fn doesNotRecognizeSelector(&self, sel: Sel) -> !
Handle messages the object doesn’t recognize.
See Apple’s documentation for details.
Methods from Deref<Target = AnyObject>§
Sourcepub fn class(&self) -> &'static AnyClass
Available on crate feature NSAccessibilityConstants
only.
pub fn class(&self) -> &'static AnyClass
NSAccessibilityConstants
only.Dynamically find the class of this object.
§Panics
May panic if the object is invalid (which may be the case for objects
returned from unavailable init
/new
methods).
§Example
Check that an instance of NSObject
has the precise class NSObject
.
use objc2::ClassType;
use objc2::runtime::NSObject;
let obj = NSObject::new();
assert_eq!(obj.class(), NSObject::class());
Sourcepub unsafe fn get_ivar<T>(&self, name: &str) -> &Twhere
T: Encode,
👎Deprecated: this is difficult to use correctly, use Ivar::load
instead.Available on crate feature NSAccessibilityConstants
only.
pub unsafe fn get_ivar<T>(&self, name: &str) -> &Twhere
T: Encode,
Ivar::load
instead.NSAccessibilityConstants
only.Use Ivar::load
instead.
§Safety
The object must have an instance variable with the given name, and it
must be of type T
.
See Ivar::load_ptr
for details surrounding this.
Sourcepub fn downcast_ref<T>(&self) -> Option<&T>where
T: DowncastTarget,
Available on crate feature NSAccessibilityConstants
only.
pub fn downcast_ref<T>(&self) -> Option<&T>where
T: DowncastTarget,
NSAccessibilityConstants
only.Attempt to downcast the object to a class of type T
.
This is the reference-variant. Use Retained::downcast
if you want
to convert a retained object to another type.
§Mutable classes
Some classes have immutable and mutable variants, such as NSString
and NSMutableString
.
When some Objective-C API signature says it gives you an immutable class, it generally expects you to not mutate that, even though it may technically be mutable “under the hood”.
So using this method to convert a NSString
to a NSMutableString
,
while not unsound, is generally frowned upon unless you created the
string yourself, or the API explicitly documents the string to be
mutable.
See Apple’s documentation on mutability and on
isKindOfClass:
for more details.
§Generic classes
Objective-C generics are called “lightweight generics”, and that’s because they aren’t exposed in the runtime. This makes it impossible to safely downcast to generic collections, so this is disallowed by this method.
You can, however, safely downcast to generic collections where all the
type-parameters are AnyObject
.
§Panics
This works internally by calling isKindOfClass:
. That means that the
object must have the instance method of that name, and an exception
will be thrown (if CoreFoundation is linked) or the process will abort
if that is not the case. In the vast majority of cases, you don’t need
to worry about this, since both root objects NSObject
and
NSProxy
implement this method.
§Examples
Cast an NSString
back and forth from NSObject
.
use objc2::rc::Retained;
use objc2_foundation::{NSObject, NSString};
let obj: Retained<NSObject> = NSString::new().into_super();
let string = obj.downcast_ref::<NSString>().unwrap();
// Or with `downcast`, if we do not need the object afterwards
let string = obj.downcast::<NSString>().unwrap();
Try (and fail) to cast an NSObject
to an NSString
.
use objc2_foundation::{NSObject, NSString};
let obj = NSObject::new();
assert!(obj.downcast_ref::<NSString>().is_none());
Try to cast to an array of strings.
use objc2_foundation::{NSArray, NSObject, NSString};
let arr = NSArray::from_retained_slice(&[NSObject::new()]);
// This is invalid and doesn't type check.
let arr = arr.downcast_ref::<NSArray<NSString>>();
This fails to compile, since it would require enumerating over the array to ensure that each element is of the desired type, which is a performance pitfall.
Downcast when processing each element instead.
use objc2_foundation::{NSArray, NSObject, NSString};
let arr = NSArray::from_retained_slice(&[NSObject::new()]);
for elem in arr {
if let Some(data) = elem.downcast_ref::<NSString>() {
// handle `data`
}
}
Trait Implementations§
Source§impl AsRef<AnyObject> for NSWritingToolsCoordinator
impl AsRef<AnyObject> for NSWritingToolsCoordinator
Source§impl AsRef<NSObject> for NSWritingToolsCoordinator
impl AsRef<NSObject> for NSWritingToolsCoordinator
Source§impl Borrow<AnyObject> for NSWritingToolsCoordinator
impl Borrow<AnyObject> for NSWritingToolsCoordinator
Source§impl Borrow<NSObject> for NSWritingToolsCoordinator
impl Borrow<NSObject> for NSWritingToolsCoordinator
Source§impl ClassType for NSWritingToolsCoordinator
impl ClassType for NSWritingToolsCoordinator
Source§const NAME: &'static str = "NSWritingToolsCoordinator"
const NAME: &'static str = "NSWritingToolsCoordinator"
Source§type ThreadKind = dyn MainThreadOnly
type ThreadKind = dyn MainThreadOnly
Source§impl Debug for NSWritingToolsCoordinator
impl Debug for NSWritingToolsCoordinator
Source§impl Deref for NSWritingToolsCoordinator
impl Deref for NSWritingToolsCoordinator
Source§impl Hash for NSWritingToolsCoordinator
impl Hash for NSWritingToolsCoordinator
Source§impl Message for NSWritingToolsCoordinator
impl Message for NSWritingToolsCoordinator
Source§impl NSObjectProtocol for NSWritingToolsCoordinator
impl NSObjectProtocol for NSWritingToolsCoordinator
Source§fn isEqual(&self, other: Option<&AnyObject>) -> bool
fn isEqual(&self, other: Option<&AnyObject>) -> bool
Source§fn hash(&self) -> usize
fn hash(&self) -> usize
Source§fn isKindOfClass(&self, cls: &AnyClass) -> bool
fn isKindOfClass(&self, cls: &AnyClass) -> bool
Source§fn is_kind_of<T>(&self) -> bool
fn is_kind_of<T>(&self) -> bool
isKindOfClass
directly, or cast your objects with AnyObject::downcast_ref
Source§fn isMemberOfClass(&self, cls: &AnyClass) -> bool
fn isMemberOfClass(&self, cls: &AnyClass) -> bool
Source§fn respondsToSelector(&self, aSelector: Sel) -> bool
fn respondsToSelector(&self, aSelector: Sel) -> bool
Source§fn conformsToProtocol(&self, aProtocol: &AnyProtocol) -> bool
fn conformsToProtocol(&self, aProtocol: &AnyProtocol) -> bool
Source§fn debugDescription(&self) -> Retained<NSObject>
fn debugDescription(&self) -> Retained<NSObject>
Source§impl RefEncode for NSWritingToolsCoordinator
impl RefEncode for NSWritingToolsCoordinator
Source§const ENCODING_REF: Encoding = <NSObject as ::objc2::RefEncode>::ENCODING_REF
const ENCODING_REF: Encoding = <NSObject as ::objc2::RefEncode>::ENCODING_REF
impl DowncastTarget for NSWritingToolsCoordinator
impl Eq for NSWritingToolsCoordinator
Auto Trait Implementations§
impl !Freeze for NSWritingToolsCoordinator
impl !RefUnwindSafe for NSWritingToolsCoordinator
impl !Send for NSWritingToolsCoordinator
impl !Sync for NSWritingToolsCoordinator
impl !Unpin for NSWritingToolsCoordinator
impl !UnwindSafe for NSWritingToolsCoordinator
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Source§impl<'a, T> MainThreadOnly for T
impl<'a, T> MainThreadOnly for T
Source§fn mtm(&self) -> MainThreadMarker
fn mtm(&self) -> MainThreadMarker
MainThreadMarker
from the main-thread-only object. Read more