rust_macios/foundation/
ns_spell_server.rs

1use std::sync::Once;
2
3use objc::{
4    class,
5    declare::ClassDecl,
6    msg_send,
7    runtime::{Class, Object},
8    sel, sel_impl,
9};
10
11use objc_id::Id;
12
13use crate::{
14    objective_c_runtime::{
15        self, id,
16        macros::interface_impl,
17        traits::{FromId, PNSObject, ToId},
18    },
19    utils::to_bool,
20};
21
22use super::NSString;
23
24pub static NSSPELLSERVER_PTR: &str = "rstNSSpellServerPtr";
25
26/// A server that your app uses to provide a spell checker service to other apps running in the system.
27pub struct NSSpellServer {
28    ptr: Id<Object>,
29}
30
31impl PNSObject for NSSpellServer {
32    fn m_class<'a>() -> &'a Class {
33        static mut NSSPELL_SERVER_CLASS: *const Class = 0 as *const Class;
34        static INIT: Once = Once::new();
35
36        INIT.call_once(|| unsafe {
37            let superclass = class!(NSApplication);
38            let decl = ClassDecl::new("RSTNSSpellServer", superclass).unwrap();
39            NSSPELL_SERVER_CLASS = decl.register();
40        });
41
42        unsafe { &*NSSPELL_SERVER_CLASS }
43    }
44
45    fn m_self(&self) -> id {
46        unsafe { msg_send![self.ptr, self] }
47    }
48}
49
50impl core::fmt::Debug for NSSpellServer {
51    fn fmt(&self, f: &mut ::core::fmt::Formatter<'_>) -> ::core::fmt::Result {
52        std::write!(f, "{}", self.p_debug_description())
53    }
54}
55
56impl core::fmt::Display for NSSpellServer {
57    fn fmt(&self, f: &mut ::core::fmt::Formatter<'_>) -> ::core::fmt::Result {
58        std::write!(f, "{}", self.p_description())
59    }
60}
61
62impl ToId for NSSpellServer {
63    fn to_id(mut self) -> id {
64        &mut *self.ptr
65    }
66}
67
68impl FromId for NSSpellServer {
69    unsafe fn from_id(ptr: id) -> Self {
70        Self {
71            ptr: Id::from_ptr(ptr),
72        }
73    }
74}
75
76#[interface_impl(NSObject)]
77impl NSSpellServer {
78    /*  Configuring Spelling Servers */
79
80    /// Returns the receiver’s delegate.
81    #[property]
82    pub fn delegate(&self) -> id {
83        unsafe { msg_send![self.m_self(), delegate] }
84    }
85
86    /// Sets the receiver's delegate.
87
88    /* Providing Spelling Services
89     */
90
91    /// Notifies the receiver of a language your spelling checker can check.
92    #[method]
93    pub fn register_language_by_vendor(&mut self, language: &NSString, vendor: &NSString) {
94        unsafe {
95            msg_send![self.m_self(), registerLanguage: language.m_self() byVendor: vendor.m_self()]
96        }
97    }
98
99    /// Causes the receiver to start listening for spell-checking requests.
100    #[method]
101    pub fn run(&mut self) {
102        unsafe { msg_send![self.m_self(), run] }
103    }
104
105    /* Managing the Spell-Checking Process
106     */
107
108    /// Indicates whether a given word is in the user’s list of learned words or the document’s list of words to ignore.
109    #[method]
110    pub fn is_word_in_user_dictionaries_case_sensitive(&self, word: &NSString, flag: bool) -> bool {
111        unsafe {
112            to_bool(
113                msg_send![self.m_self(), isWordInUserDictionaries: word.m_self() caseSensitive: flag],
114            )
115        }
116    }
117}
118
119unsafe impl objective_c_runtime::Encode for NSSpellServer {
120    fn encode() -> objc::Encoding {
121        unsafe { objective_c_runtime::Encoding::from_str("@") }
122    }
123}
124
125extern "C" {
126    /* Grammatical-Analysis Details
127     */
128
129    /// The value for the [`NSGrammarRange`] dictionary key should be an NSValue containing an NSRange, a subrange of the sentence range used as the return value, whose location should be an offset from the beginning of the sentence--so, for example, an NSGrammarRange for the first four characters of the overall sentence range should be {0, 4}. If the NSGrammarRange key is not present in the dictionary it is assumed to be equal to the overall sentence range.
130    pub static NSGrammarRange: NSString;
131
132    /// The value for the [`NSGrammarUserDescription`] dictionary key should be an NSString containing descriptive text about that range, to be presented directly to the user; it is intended that the user description should provide enough information to allow the user to correct the problem. It is recommended that NSGrammarUserDescription be supplied in all cases, however, NSGrammarUserDescription or NSGrammarCorrections must be supplied in order for correction guidance to be presented to the user.
133    pub static NSGrammarUserDescription: NSString;
134
135    /// The value for the [`NSGrammarCorrections`] key should be an NSArray of NSStrings representing potential substitutions to correct the problem, but it is expected that this may not be available in all cases. NSGrammarUserDescription or NSGrammarCorrections must be supplied in order for correction guidance to be presented to the user.
136    pub static NSGrammarCorrections: NSString;
137}