Skip to main content

DesktopApp

Struct DesktopApp 

Source
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

Source

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.

Source

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?
examples/desktop_app.rs (line 2)
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}
Source

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,
);
Source

pub fn company_name(&self) -> Option<&str>

Returns the optional company name.

Source

pub fn app_name(&self) -> &str

Returns the app name.

Examples found in repository?
examples/desktop_app.rs (line 7)
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}
Source

pub fn app_dir_name(&self) -> &str

Returns the app-data directory name used by the paths helpers.

Source

pub fn app_id(&self) -> &str

Returns the default app ID used by the single-instance helpers.

Examples found in repository?
examples/desktop_app.rs (line 19)
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}
Source

pub fn configured_instance_scope(&self) -> InstanceScope

Returns the configured single-instance scope.

Source

pub fn local_data_dir(&self) -> Result<PathBuf>

Returns the per-user local app-data directory for this app without creating it.

Source

pub fn roaming_data_dir(&self) -> Result<PathBuf>

Returns the per-user roaming app-data directory for this app without creating it.

Source

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?
examples/desktop_app.rs (line 12)
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}
Source

pub fn ensure_roaming_data_dir(&self) -> Result<PathBuf>

Creates and returns the per-user roaming app-data directory for this app.

Source

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,
);
Source

pub fn single_instance(&self) -> Result<Option<InstanceGuard>>

Attempts to acquire the configured single-instance guard for this app.

Examples found in repository?
examples/desktop_app.rs (line 4)
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

Source§

fn clone(&self) -> DesktopApp

Returns a duplicate of the value. Read more
1.0.0 · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl Debug for DesktopApp

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl PartialEq for DesktopApp

Source§

fn eq(&self, other: &DesktopApp) -> bool

Tests for self and other values to be equal, and is used by ==.
1.0.0 · Source§

fn ne(&self, other: &Rhs) -> bool

Tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
Source§

impl Eq for DesktopApp

Source§

impl StructuralPartialEq for DesktopApp

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.