From 47bfe768e684b305745998529aaad4b7cf044f94 Mon Sep 17 00:00:00 2001 From: Nolan Darilek Date: Wed, 12 Aug 2020 09:49:51 -0500 Subject: [PATCH] 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. --- examples/hello_world.rs | 14 ++++++++++++++ src/backends/ns_speech_synthesizer.rs | 14 ++++++++++---- 2 files changed, 24 insertions(+), 4 deletions(-) diff --git a/examples/hello_world.rs b/examples/hello_world.rs index 75a4eb1..e95c53c 100644 --- a/examples/hello_world.rs +++ b/examples/hello_world.rs @@ -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(()) } diff --git a/src/backends/ns_speech_synthesizer.rs b/src/backends/ns_speech_synthesizer.rs index fbaf3a4..946d3d6 100644 --- a/src/backends/ns_speech_synthesizer.rs +++ b/src/backends/ns_speech_synthesizer.rs @@ -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) } }