日本語

Starting at login

Os::autostart has the system start the application when the person logs in, and reads whether it does.

Turning it on and off

impl Autostart<'_> {
    pub async fn enable(&self) -> Result<(), Error>;
    pub async fn disable(&self) -> Result<(), Error>;
    pub async fn is_enabled(&self) -> Result<bool, Error>;
}

Os::autostart returns the system's entry that starts the application when the person logs in. enable writes it, disable removes it, and is_enabled reads whether it is there, so a settings page shows a switch in the state the system holds. A chat application with a tray starts this way and waits in the tray:

use hypersig_desktop::Os;

/// Turns the start at login on or off from a switch on the settings page.
async fn set_start_at_login(os: &Os, on: bool) -> Result<(), hypersig_desktop::Error> {
    if on {
        os.autostart().enable().await
    } else {
        os.autostart().disable().await
    }
}

Each fails with ErrorKind::Autostart when the entry cannot be read or written. In a test, the FakeOs keeps whether it is on.

On each system

  • The entry is named by the identifier App::new takes, and starts the program running now: on Linux the AppImage the person installed, which APPIMAGE names, and elsewhere the running binary. A run from cargo run writes an entry for the binary under target.
  • macOS keeps a launch agent in ~/Library/LaunchAgents, Windows a value under the Run key of the registry, and Linux a desktop entry in ~/.config/autostart, which the auto-launch crate writes. On macOS, a run saw enable write the launch agent, is_enabled read it back, and disable remove it.