Get delegates working so speech interruption/queuing should now be possible.

* Fix broken delegate method signature.
* Add `NSRunLoop` into `hello_world` example so delegates are called. Presumably, MacOS apps already run one of these, but the example didn't.
This commit is contained in:
Nolan Darilek 2020-08-12 09:49:51 -05:00
parent faadc0e3b7
commit 47bfe768e6
2 changed files with 24 additions and 4 deletions

View File

@ -1,5 +1,12 @@
use std::io;
#[cfg(target_os = "macos")]
use cocoa_foundation::base::id;
#[cfg(target_os = "macos")]
use cocoa_foundation::foundation::NSRunLoop;
#[cfg(target_os = "macos")]
use objc::{msg_send, sel, sel_impl};
use tts::*;
fn main() -> Result<(), Error> {
@ -42,6 +49,13 @@ fn main() -> Result<(), Error> {
}
tts.speak("Goodbye.", false)?;
let mut _input = String::new();
#[cfg(target_os = "macos")]
{
let run_loop: id = unsafe { NSRunLoop::currentRunLoop() };
unsafe {
let _: () = msg_send![run_loop, run];
}
}
io::stdin().read_line(&mut _input)?;
Ok(())
}

View File

@ -17,18 +17,24 @@ impl NSSpeechSynthesizerBackend {
let mut obj: *mut Object = unsafe { msg_send![class!(NSSpeechSynthesizer), alloc] };
obj = unsafe { msg_send![obj, init] };
let mut decl = ClassDecl::new("MyNSSpeechSynthesizerDelegate", class!(NSObject)).unwrap();
extern "C" fn speech_synthesizer_did_finish_speaking(_: &Object, _: Sel, _: BOOL) {
extern "C" fn speech_synthesizer_did_finish_speaking(
_: &Object,
_: Sel,
_: *const Object,
_: BOOL,
) {
println!("Got it");
}
unsafe {
decl.add_method(
sel!(didFinishSpeaking:),
speech_synthesizer_did_finish_speaking as extern "C" fn(&Object, Sel, BOOL) -> (),
sel!(speechSynthesizer:didFinishSpeaking:),
speech_synthesizer_did_finish_speaking
as extern "C" fn(&Object, Sel, *const Object, BOOL) -> (),
)
};
let delegate_class = decl.register();
let delegate_obj: *mut Object = unsafe { msg_send![delegate_class, new] };
let _: () = unsafe { msg_send![obj, setDelegate: delegate_obj] };
let _: Object = unsafe { msg_send![obj, setDelegate: delegate_obj] };
NSSpeechSynthesizerBackend(obj, delegate_obj)
}
}