use std::fmt::Display;
use block::{ConcreteBlock, IntoConcreteBlock};
use libc::c_ulonglong;
use objc::{msg_send, sel, sel_impl};
use crate::{
object,
objective_c_runtime::{
id,
macros::interface_impl,
traits::{FromId, PNSObject},
},
utils::to_bool,
};
use super::{Int, NSArray, NSDictionary, NSNotificationName, NSString, NSTimeInterval, UInt};
#[derive(Debug, PartialEq, Eq, PartialOrd, Ord, Clone, Copy)]
pub struct NSOperatingSystemVersion {
pub major: isize,
pub minor: isize,
pub patch_version: isize,
}
impl Display for NSOperatingSystemVersion {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(f, "{}.{}.{}", self.major, self.minor, self.patch_version)
}
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord)]
#[repr(u64)]
pub enum NSActivityOptions {
IdleDisplaySleepDisabled = 1 << 40,
IdleSystemSleepDisabled = 1 << 20,
SuddenTerminationDisabled = 1 << 14,
AutomaticTerminationDisabled = 1 << 15,
UserInitiated = 0x00FFFFFF | Self::IdleSystemSleepDisabled as u64,
UserInteractive = (Self::UserInitiated as u64 | Self::LatencyCritical as u64),
Background = 0x000000ff,
LatencyCritical = 0xFF00000000,
InitiatedAllowingIdleSystemSleep =
Self::UserInitiated as u64 & !(Self::IdleSystemSleepDisabled as u64),
AnimationTrackingEnabled = (1 << 45),
TrackingEnabled = (1 << 46),
}
#[derive(Debug)]
#[repr(i64)]
pub enum NSProcessInfoThermalState {
Nominal,
Fair,
Serious,
Critical,
}
object! {
unsafe pub struct NSProcessInfo;
}
#[interface_impl(NSObject)]
impl NSProcessInfo {
#[property]
pub fn process_info() -> NSProcessInfo {
unsafe { NSProcessInfo::from_id(msg_send![Self::m_class(), processInfo]) }
}
#[property]
pub fn arguments(&self) -> NSArray<NSString> {
unsafe { NSArray::from_id(msg_send![self.m_self(), arguments]) }
}
#[property]
pub fn environment(&self) -> NSDictionary<NSString, NSString> {
unsafe { NSDictionary::from_id(msg_send![self.m_self(), environment]) }
}
#[property]
pub fn globally_unique_string(&self) -> NSString {
unsafe { NSString::from_id(msg_send![self.m_self(), globallyUniqueString]) }
}
#[property]
pub fn mac_catalyst_app(&self) -> bool {
unsafe { to_bool(msg_send![self.m_self(), isMacCatalystApp]) }
}
#[property]
pub fn ios_app_on_mac(&self) -> bool {
unsafe { to_bool(msg_send![self.m_self(), isiOSAppOnMac]) }
}
#[property]
pub fn process_identifier(&self) -> Int {
unsafe { msg_send![self.m_self(), processIdentifier] }
}
#[property]
pub fn process_name(&self) -> NSString {
unsafe { NSString::from_id(msg_send![self.m_self(), processName]) }
}
#[property]
pub fn set_process_name(&mut self, name: NSString) {
unsafe { msg_send![self.m_self(), setProcessName: name] }
}
#[property]
pub fn user_name(&self) -> NSString {
unsafe { NSString::from_id(msg_send![self.m_self(), userName]) }
}
#[property]
pub fn full_user_name(&self) -> NSString {
unsafe { NSString::from_id(msg_send![self.m_self(), fullUserName]) }
}
#[method]
pub fn disable_sudden_termination(&mut self) {
unsafe { msg_send![self.m_self(), disableSuddenTermination] }
}
#[method]
pub fn enable_sudden_termination(&mut self) {
unsafe { msg_send![self.m_self(), enableSuddenTermination] }
}
#[method]
pub fn disable_automatic_termination(&mut self, reason: &NSString) {
unsafe { msg_send![self.m_self(), disableAutomaticTermination: reason.m_self()] }
}
#[method]
pub fn enable_automatic_termination(&mut self, reason: &NSString) {
unsafe { msg_send![self.m_self(), enableAutomaticTermination: reason.m_self()] }
}
#[property]
pub fn automatic_termination_support_enabled(&self) -> bool {
unsafe { to_bool(msg_send![self.m_self(), automaticTerminationSupportEnabled]) }
}
#[property]
pub fn host_name(&self) -> NSString {
unsafe { NSString::from_id(msg_send![self.m_self(), hostName]) }
}
#[property]
pub fn operating_system_version_string(&self) -> NSString {
unsafe { NSString::from_id(msg_send![self.m_self(), operatingSystemVersionString]) }
}
#[property]
pub fn operating_system_version(&self) -> NSOperatingSystemVersion {
unsafe { msg_send![self.m_self(), operatingSystemVersion] }
}
#[method]
pub fn is_operating_system_at_least_version(&self, version: NSOperatingSystemVersion) -> bool {
unsafe {
to_bool(msg_send![
self.m_self(),
isOperatingSystemAtLeastVersion: version
])
}
}
#[deprecated = "Use `operating_system_version` or `is_operating_system_at_least_version` instead"]
#[method]
pub fn operating_system(&self) -> UInt {
unsafe { msg_send![self.m_self(), operatingSystem] }
}
#[method]
#[deprecated = "Use `operating_system_version` or `is_operating_system_at_least_version` instead"]
pub fn operating_system_name(&self) -> NSString {
unsafe { NSString::from_id(msg_send![self.m_self(), operatingSystemName]) }
}
#[property]
pub fn processor_count(&self) -> UInt {
unsafe { msg_send![self.m_self(), processorCount] }
}
#[property]
pub fn active_processor_count(&self) -> UInt {
unsafe { msg_send![self.m_self(), activeProcessorCount] }
}
#[property]
pub fn physical_memory(&self) -> c_ulonglong {
unsafe { msg_send![self.m_self(), physicalMemory] }
}
#[property]
pub fn system_uptime(&self) -> NSTimeInterval {
unsafe { msg_send![self.m_self(), systemUptime] }
}
#[method]
pub fn begin_activity_with_options_reason(
&self,
options: NSActivityOptions,
reason: &NSString,
) -> id {
unsafe {
msg_send![self.m_self(), beginActivityWithOptions: options reason: reason.m_self()]
}
}
#[method]
pub fn end_activity(&self, activity: id) {
unsafe { msg_send![self.m_self(), endActivity: activity] }
}
#[method]
pub fn perform_activity_with_options_reason_using_block<F>(
&self,
activity: id,
reason: &NSString,
block: F,
) where
F: IntoConcreteBlock<(), Ret = ()> + 'static,
{
let block = ConcreteBlock::new(block);
let block = block.copy();
unsafe {
msg_send![self.m_self(), performActivityWithOptions: activity reason: reason.m_self() usingBlock: block]
}
}
#[method]
pub fn perform_expiring_activity_with_reason_using_block<F>(&self, reason: &NSString, block: F)
where
F: IntoConcreteBlock<(bool,), Ret = ()> + 'static,
{
let block = ConcreteBlock::new(block);
let block = block.copy();
unsafe {
msg_send![self.m_self(), performExpiringActivityWithReason: reason.m_self() usingBlock: block]
}
}
#[property]
pub fn thermal_state(&self) -> NSProcessInfoThermalState {
unsafe { msg_send![self.m_self(), thermalState] }
}
#[property]
pub fn low_power_mode_enabled(&self) -> bool {
unsafe { msg_send![self.m_self(), isLowPowerModeEnabled] }
}
}
extern "C" {
pub static NSProcessInfoThermalStateDidChangeNotification: NSNotificationName;
pub static NSProcessInfoPowerStateDidChangeNotification: NSNotificationName;
}