rust_macios/appkit/
ns_running_application.rs

1use objc::{msg_send, sel, sel_impl};
2
3use crate::{
4    object,
5    objective_c_runtime::traits::{FromId, PNSObject},
6};
7
8use super::{interface_impl, NSApplicationActivationOptions};
9
10object! {
11    /// An object that can manipulate and provide information for a single instance of an app.
12    unsafe pub struct NSRunningApplication;
13}
14
15#[interface_impl(NSObject)]
16impl NSRunningApplication {
17    /// Returns the application instance, creating it if it doesn’t exist yet.
18    #[property]
19    pub fn current_application() -> Self
20    where
21        Self: Sized + FromId,
22    {
23        unsafe { Self::from_id(msg_send![Self::m_class(), currentApplication]) }
24    }
25
26    /// Indicates whether the application is currently frontmost.
27    #[property]
28    pub fn is_active(&self) -> bool {
29        unsafe { msg_send![self.m_self(), isActive] }
30    }
31
32    /// Attempts to activate the application using the specified options.
33    #[method]
34    pub fn activate_with_options(&mut self, options: NSApplicationActivationOptions) {
35        unsafe { msg_send![self.m_self(), activateWithOptions: options] }
36    }
37}