1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
//! Auto launch any application or executable at startup. Supports Windows, Mac (via AppleScript or Launch Agent), and Linux.
//!
//! ## Usage
//!
//! The parameters of `AutoLaunch::new` are different on each os.
//! See the function definition of the demo below for details.
//!
//!
//! ### Linux
//!
//! On Linux, it supports `hidden` parameter which means that hidden the app on launch.
//!
//! ```rust
//! use auto_launch::AutoLaunch;
//!
//! fn main() {
//!     let app_name = "the-app";
//!     let app_path = "/path/to/the-app";
//!     let auto = AutoLaunch::new(app_name, app_path, false);
//!
//!     // enable the auto launch
//!     assets!(auto.enable().is_ok());
//!     assets!(auto.is_enabled().unwrap());
//!
//!     // disable the auto launch
//!     assets!(auto.disable().is_ok());
//!     assets!(!auto.is_enabled().unwrap());
//! }
//! ```
//!
//! ### Macos
//!
//! Macos supports two way to achieve auto launch (via AppleScript or Launch Agent).
//! When the `use_launch_agent` is true, it will achieve by Launch Agent, otherwise by AppleScript.
//! On Macos, it supports `hidden` parameter which means that hidden the app on launch.
//!
//! **Note**:
//! - The `app_path` should be a absolute path and exists. Otherwise, it will cause an error when `enable`.
//! - When in the AppleScript way, the `app_name` should be same as the basename of `app_path`, or it will be corrected automately.
//!
//! ```rust
//! use auto_launch::AutoLaunch;
//!
//! #[cfg(target_os = "macos")]
//! fn main() {
//!     let app_name = "the-app";
//!     let app_path = "/path/to/the-app.app";
//!     let auto = AutoLaunch::new(app_name, app_path, false, false);
//!     
//!     // enable the auto launch
//!     assets!(auto.enable().is_ok());
//!     assets!(auto.is_enabled().unwrap());
//!
//!     // disable the auto launch
//!     assets!(auto.disable().is_ok());
//!     assets!(!auto.is_enabled().unwrap());
//! }
//! ```
//!
//! ### Windows
//!
//! On Windows, it will add a registry entry under `\HKEY_CURRENT_USER\SOFTWARE\Microsoft\Windows\CurrentVersion\Run`.
//!
//! ```rust
//! use auto_launch::AutoLaunch;
//!
//! #[cfg(target_os = "macos")]
//! fn main() {
//!     let app_name = "the-app";
//!     let app_path = "C:\\path\\to\\the-app.exe";
//!     let auto = AutoLaunch::new(app_name, app_path);
//!
//!     // enable the auto launch
//!     assets!(auto.enable().is_ok());
//!     assets!(auto.is_enabled().unwrap());
//!
//!     // disable the auto launch
//!     assets!(auto.disable().is_ok());
//!     assets!(!auto.is_enabled().unwrap());
//! }
//! ```
//!

#[cfg(target_os = "linux")]
mod linux;
#[cfg(target_os = "macos")]
mod macos;
#[cfg(target_os = "windows")]
mod windows;

#[derive(Debug, Clone, PartialEq, Eq)]
pub struct AutoLaunch<'a> {
    /// The application name
    pub(crate) app_name: &'a str,

    /// The application executable path (absolute path will be better)
    pub(crate) app_path: &'a str,

    #[cfg(target_os = "macos")]
    /// Whether use Launch Agent for implement or use AppleScript
    pub(crate) use_launch_agent: bool,

    #[cfg(not(target_os = "windows"))]
    /// Supports hidden the application on launch
    pub(crate) hidden: bool,
}

impl AutoLaunch<'_> {
    /// get the application name
    pub fn get_app_name(&self) -> &str {
        self.app_name
    }

    /// get the application path
    pub fn get_app_path(&self) -> &str {
        self.app_path
    }

    #[cfg(not(target_os = "windows"))]
    /// get whether it is hidden
    pub fn is_hidden(&self) -> bool {
        self.hidden
    }
}