pub struct DesktopApp { /* private fields */ }Expand description
Validated Windows desktop app identity used by the app-data and single-instance helpers.
DesktopApp keeps a validated app identity in one place and uses it for app-data
paths and single-instance locking. It does not own any global state.
§Examples
let app = win_desktop_utils::DesktopApp::new(format!(
"demo-app-{}",
std::process::id()
))?;
let local = app.ensure_local_data_dir()?;
assert!(local.exists());Implementations§
Source§impl DesktopApp
impl DesktopApp
Sourcepub fn new(app_name: impl Into<String>) -> Result<Self>
pub fn new(app_name: impl Into<String>) -> Result<Self>
Creates a desktop app identity without a company namespace.
§Errors
Returns Error::InvalidInput if app_name is empty, contains NUL bytes,
or contains characters that are invalid in Windows file names.
Sourcepub fn with_company(
company_name: impl Into<String>,
app_name: impl Into<String>,
) -> Result<Self>
pub fn with_company( company_name: impl Into<String>, app_name: impl Into<String>, ) -> Result<Self>
Creates a desktop app identity grouped under a company namespace.
App-data paths are nested as Company\App, while the default single-instance
mutex ID uses Company.App.
§Examples
let app = win_desktop_utils::DesktopApp::with_company("Acme", "Editor")?;
assert_eq!(app.company_name(), Some("Acme"));
assert_eq!(app.app_name(), "Editor");
assert_eq!(app.app_dir_name(), "Acme\\Editor");
assert_eq!(app.app_id(), "Acme.Editor");§Errors
Returns Error::InvalidInput if either identity part is empty, contains NUL
bytes, or contains characters that are invalid in Windows file names.
Examples found in repository?
1fn main() -> Result<(), Box<dyn std::error::Error>> {
2 let app = win_desktop_utils::DesktopApp::with_company("Acme", "Demo Utility")?;
3
4 let _guard = match app.single_instance()? {
5 Some(guard) => guard,
6 None => {
7 println!("{} is already running", app.app_name());
8 return Ok(());
9 }
10 };
11
12 let local_dir = app.ensure_local_data_dir()?;
13 let state_file = local_dir.join("state.txt");
14 std::fs::write(&state_file, "started\n")?;
15
16 let docs_shortcut = local_dir.join("win-desktop-utils docs.url");
17 win_desktop_utils::create_url_shortcut(&docs_shortcut, "https://docs.rs/win-desktop-utils")?;
18
19 println!("app id: {}", app.app_id());
20 println!("local data: {}", local_dir.display());
21 println!("state file: {}", state_file.display());
22 println!("docs shortcut: {}", docs_shortcut.display());
23
24 Ok(())
25}Sourcepub fn instance_scope(self, scope: InstanceScope) -> Self
pub fn instance_scope(self, scope: InstanceScope) -> Self
Sets the default single-instance mutex namespace scope used by Self::single_instance.
§Examples
let app = win_desktop_utils::DesktopApp::new("Admin Tool")?
.instance_scope(win_desktop_utils::InstanceScope::Global);
assert_eq!(
app.configured_instance_scope(),
win_desktop_utils::InstanceScope::Global,
);Sourcepub fn company_name(&self) -> Option<&str>
pub fn company_name(&self) -> Option<&str>
Returns the optional company name.
Sourcepub fn app_name(&self) -> &str
pub fn app_name(&self) -> &str
Returns the app name.
Examples found in repository?
1fn main() -> Result<(), Box<dyn std::error::Error>> {
2 let app = win_desktop_utils::DesktopApp::with_company("Acme", "Demo Utility")?;
3
4 let _guard = match app.single_instance()? {
5 Some(guard) => guard,
6 None => {
7 println!("{} is already running", app.app_name());
8 return Ok(());
9 }
10 };
11
12 let local_dir = app.ensure_local_data_dir()?;
13 let state_file = local_dir.join("state.txt");
14 std::fs::write(&state_file, "started\n")?;
15
16 let docs_shortcut = local_dir.join("win-desktop-utils docs.url");
17 win_desktop_utils::create_url_shortcut(&docs_shortcut, "https://docs.rs/win-desktop-utils")?;
18
19 println!("app id: {}", app.app_id());
20 println!("local data: {}", local_dir.display());
21 println!("state file: {}", state_file.display());
22 println!("docs shortcut: {}", docs_shortcut.display());
23
24 Ok(())
25}Sourcepub fn app_dir_name(&self) -> &str
pub fn app_dir_name(&self) -> &str
Returns the app-data directory name used by the paths helpers.
Sourcepub fn app_id(&self) -> &str
pub fn app_id(&self) -> &str
Returns the default app ID used by the single-instance helpers.
Examples found in repository?
1fn main() -> Result<(), Box<dyn std::error::Error>> {
2 let app = win_desktop_utils::DesktopApp::with_company("Acme", "Demo Utility")?;
3
4 let _guard = match app.single_instance()? {
5 Some(guard) => guard,
6 None => {
7 println!("{} is already running", app.app_name());
8 return Ok(());
9 }
10 };
11
12 let local_dir = app.ensure_local_data_dir()?;
13 let state_file = local_dir.join("state.txt");
14 std::fs::write(&state_file, "started\n")?;
15
16 let docs_shortcut = local_dir.join("win-desktop-utils docs.url");
17 win_desktop_utils::create_url_shortcut(&docs_shortcut, "https://docs.rs/win-desktop-utils")?;
18
19 println!("app id: {}", app.app_id());
20 println!("local data: {}", local_dir.display());
21 println!("state file: {}", state_file.display());
22 println!("docs shortcut: {}", docs_shortcut.display());
23
24 Ok(())
25}Sourcepub fn configured_instance_scope(&self) -> InstanceScope
pub fn configured_instance_scope(&self) -> InstanceScope
Returns the configured single-instance scope.
Sourcepub fn local_data_dir(&self) -> Result<PathBuf>
pub fn local_data_dir(&self) -> Result<PathBuf>
Returns the per-user local app-data directory for this app without creating it.
Sourcepub fn roaming_data_dir(&self) -> Result<PathBuf>
pub fn roaming_data_dir(&self) -> Result<PathBuf>
Returns the per-user roaming app-data directory for this app without creating it.
Sourcepub fn ensure_local_data_dir(&self) -> Result<PathBuf>
pub fn ensure_local_data_dir(&self) -> Result<PathBuf>
Creates and returns the per-user local app-data directory for this app.
Examples found in repository?
1fn main() -> Result<(), Box<dyn std::error::Error>> {
2 let app = win_desktop_utils::DesktopApp::with_company("Acme", "Demo Utility")?;
3
4 let _guard = match app.single_instance()? {
5 Some(guard) => guard,
6 None => {
7 println!("{} is already running", app.app_name());
8 return Ok(());
9 }
10 };
11
12 let local_dir = app.ensure_local_data_dir()?;
13 let state_file = local_dir.join("state.txt");
14 std::fs::write(&state_file, "started\n")?;
15
16 let docs_shortcut = local_dir.join("win-desktop-utils docs.url");
17 win_desktop_utils::create_url_shortcut(&docs_shortcut, "https://docs.rs/win-desktop-utils")?;
18
19 println!("app id: {}", app.app_id());
20 println!("local data: {}", local_dir.display());
21 println!("state file: {}", state_file.display());
22 println!("docs shortcut: {}", docs_shortcut.display());
23
24 Ok(())
25}Sourcepub fn ensure_roaming_data_dir(&self) -> Result<PathBuf>
pub fn ensure_roaming_data_dir(&self) -> Result<PathBuf>
Creates and returns the per-user roaming app-data directory for this app.
Sourcepub fn single_instance_options(&self) -> SingleInstanceOptions
pub fn single_instance_options(&self) -> SingleInstanceOptions
Returns single-instance options for this app.
§Examples
let app = win_desktop_utils::DesktopApp::with_company("Acme", "Editor")?;
let options = app.single_instance_options();
assert_eq!(options.app_id(), "Acme.Editor");
assert_eq!(
options.configured_scope(),
win_desktop_utils::InstanceScope::CurrentSession,
);Sourcepub fn single_instance(&self) -> Result<Option<InstanceGuard>>
pub fn single_instance(&self) -> Result<Option<InstanceGuard>>
Attempts to acquire the configured single-instance guard for this app.
Examples found in repository?
1fn main() -> Result<(), Box<dyn std::error::Error>> {
2 let app = win_desktop_utils::DesktopApp::with_company("Acme", "Demo Utility")?;
3
4 let _guard = match app.single_instance()? {
5 Some(guard) => guard,
6 None => {
7 println!("{} is already running", app.app_name());
8 return Ok(());
9 }
10 };
11
12 let local_dir = app.ensure_local_data_dir()?;
13 let state_file = local_dir.join("state.txt");
14 std::fs::write(&state_file, "started\n")?;
15
16 let docs_shortcut = local_dir.join("win-desktop-utils docs.url");
17 win_desktop_utils::create_url_shortcut(&docs_shortcut, "https://docs.rs/win-desktop-utils")?;
18
19 println!("app id: {}", app.app_id());
20 println!("local data: {}", local_dir.display());
21 println!("state file: {}", state_file.display());
22 println!("docs shortcut: {}", docs_shortcut.display());
23
24 Ok(())
25}Trait Implementations§
Source§impl Clone for DesktopApp
impl Clone for DesktopApp
Source§fn clone(&self) -> DesktopApp
fn clone(&self) -> DesktopApp
1.0.0 · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
source. Read more