|
| 1 | +use notch::*; |
| 2 | + |
| 3 | +use std::time::Duration; |
| 4 | + |
| 5 | +use objc2::rc::Retained; |
| 6 | +use objc2::runtime::ProtocolObject; |
| 7 | +use objc2::{define_class, msg_send, MainThreadOnly}; |
| 8 | +use objc2_app_kit::{ |
| 9 | + NSAppearance, NSApplication, NSApplicationActivationPolicy, NSApplicationDelegate, |
| 10 | +}; |
| 11 | +use objc2_foundation::{ns_string, MainThreadMarker, NSObject, NSObjectProtocol}; |
| 12 | + |
| 13 | +#[derive(Debug, Default)] |
| 14 | +struct AppDelegateIvars {} |
| 15 | + |
| 16 | +define_class! { |
| 17 | + #[unsafe(super = NSObject)] |
| 18 | + #[thread_kind = MainThreadOnly] |
| 19 | + #[name = "AppDelegate"] |
| 20 | + #[ivars = AppDelegateIvars] |
| 21 | + struct AppDelegate; |
| 22 | + |
| 23 | + unsafe impl NSObjectProtocol for AppDelegate {} |
| 24 | + unsafe impl NSApplicationDelegate for AppDelegate {} |
| 25 | +} |
| 26 | + |
| 27 | +impl AppDelegate { |
| 28 | + fn new(mtm: MainThreadMarker) -> Retained<Self> { |
| 29 | + let this = Self::alloc(mtm).set_ivars(AppDelegateIvars::default()); |
| 30 | + unsafe { msg_send![super(this), init] } |
| 31 | + } |
| 32 | +} |
| 33 | + |
| 34 | +fn main() { |
| 35 | + let mtm = MainThreadMarker::new().unwrap(); |
| 36 | + |
| 37 | + let app = NSApplication::sharedApplication(mtm); |
| 38 | + app.setActivationPolicy(NSApplicationActivationPolicy::Regular); |
| 39 | + |
| 40 | + if let Some(appearance) = NSAppearance::appearanceNamed(ns_string!("NSAppearanceNameAqua")) { |
| 41 | + app.setAppearance(Some(&appearance)); |
| 42 | + } |
| 43 | + |
| 44 | + let delegate = AppDelegate::new(mtm); |
| 45 | + app.setDelegate(Some(&ProtocolObject::from_ref(&*delegate))); |
| 46 | + |
| 47 | + std::thread::spawn(|| { |
| 48 | + std::thread::sleep(Duration::from_millis(200)); |
| 49 | + |
| 50 | + show_notch( |
| 51 | + "Hello from Notch!", |
| 52 | + "This is a test notification in the dynamic notch", |
| 53 | + "bell.fill", |
| 54 | + ); |
| 55 | + |
| 56 | + std::thread::sleep(Duration::from_secs(10)); |
| 57 | + hide_notch(); |
| 58 | + |
| 59 | + std::thread::sleep(Duration::from_secs(1)); |
| 60 | + std::process::exit(0); |
| 61 | + }); |
| 62 | + |
| 63 | + app.run(); |
| 64 | +} |
0 commit comments