| 1 | #![allow(unused)] |
| 2 | |
| 3 | use std::thread; |
| 4 | |
| 5 | pub fn setup(example_file: &str) -> bool { |
| 6 | cfg_if::cfg_if! { |
| 7 | if #[cfg(all(target_os = "macos", not(feature = "preview-macos-un")))] { |
| 8 | env_logger::init(); |
| 9 | log::trace!("setup env_logger"); |
| 10 | |
| 11 | // for NSUserNotifications we need to set the application bundle identifier to the default of safari |
| 12 | let bundle_id = notify_rust::get_bundle_identifier_or_default("safari"); |
| 13 | notify_rust::set_application(&bundle_id).unwrap(); |
| 14 | log::trace!("set_application: {bundle_id}"); |
| 15 | true |
| 16 | } else if #[cfg(all(target_os = "macos", feature = "preview-macos-un"))] { |
| 17 | // for UNUserNotifications we need to log to the system log |
| 18 | // and request authorization |
| 19 | oslog::OsLogger::new("notify-rust") |
| 20 | .level_filter(log::LevelFilter::Trace) |
| 21 | .init() |
| 22 | .unwrap(); |
| 23 | log::trace!("setup oslog"); |
| 24 | notify_rust::request_auth_blocking(); |
| 25 | |
| 26 | true |
| 27 | } else { |
| 28 | env_logger::init(); |
| 29 | log::trace!("setup env_logger"); |
| 30 | true |
| 31 | } |
| 32 | } |
| 33 | } |
| 34 | |
| 35 | pub fn run_main_loop_while<T>(thread: thread::JoinHandle<T>) -> thread::Result<T> { |
| 36 | #[cfg(target_os = "macos")] |
| 37 | { |
| 38 | use objc2_foundation::{NSDate, NSDefaultRunLoopMode, NSRunLoop}; |
| 39 | let run_loop = NSRunLoop::mainRunLoop(); |
| 40 | while !thread.is_finished() { |
| 41 | let until = NSDate::dateWithTimeIntervalSinceNow(0.05); |
| 42 | unsafe { run_loop.runMode_beforeDate(NSDefaultRunLoopMode, &until) }; |
| 43 | } |
| 44 | thread.join() |
| 45 | } |
| 46 | |
| 47 | #[cfg(not(target_os = "macos"))] |
| 48 | thread.join() |
| 49 | } |
| 50 | |
| 51 | /// Blocks until the user presses a key or timeout is reached (in bundle context). |
| 52 | pub fn wait_for_keypress(msg: &str) { |
| 53 | #[cfg(all(target_os = "macos", feature = "preview-macos-un"))] |
| 54 | { |
| 55 | if mac_usernotifications::check_bundle().is_ok() { |
| 56 | // Running in a bundle - use timeout instead |
| 57 | use std::{sync::mpsc, thread, time::Duration}; |
| 58 | log::info!("{msg}, will timeout in 4 seconds"); |
| 59 | |
| 60 | let (sender, receiver) = mpsc::channel(); |
| 61 | let timeout_sender = sender.clone(); |
| 62 | |
| 63 | thread::spawn(move || { |
| 64 | thread::sleep(Duration::from_secs(4)); |
| 65 | let _ = timeout_sender.send(()); |
| 66 | }); |
| 67 | |
| 68 | let _ = receiver.recv(); |
| 69 | return; |
| 70 | } |
| 71 | } |
| 72 | |
| 73 | // Not in bundle (or not macOS) - wait for keypress |
| 74 | log::info!("{}", msg); |
| 75 | std::io::stdin().read_line(&mut String::new()).unwrap(); |
| 76 | } |