Skip to content

docs(features): Add single instance page #1931

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 18 commits into from
Apr 1, 2024
Merged
Changes from 13 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
113 changes: 107 additions & 6 deletions src/content/docs/features/single-instance.mdx
Original file line number Diff line number Diff line change
@@ -1,14 +1,115 @@
---
title: Single Instance
description: Ensure a single instance of your tauri app is running.
description: Plugin to ensure that a single instance of your tauri app is running at a time.
---

import Stub from '@components/Stub.astro';
import PluginLinks from '@components/PluginLinks.astro';
import { Tabs, TabItem, Steps } from '@astrojs/starlight/components';
import CommandTabs from '@components/CommandTabs.astro';

<PluginLinks plugin="single-instance" showJsLinks={false} />

<Stub>
Based on
https://github.com/tauri-apps/plugins-workspace/tree/v2/plugins/single-instance
</Stub>
Plugin to ensure that a single instance of your tauri app is running at a time.

## Setup

We can install and initialize the Single Instance Plugin in one of the two ways:

<Tabs>
<TabItem label="Automatic">

We can simply use [`tauri add`](/references/v2/cli/#add) to automatically install as well as initialize the plugin with your Tauri Application.

<CommandTabs npm="npm run tauri add single-instance"
yarn="yarn run tauri add single-instance"
pnpm="pnpm tauri add single-instance"
cargo="cargo tauri add single-instance" />

</TabItem>
<TabItem label="Manual">
<Steps>
<ol>
We also have the option to manually install and initialize the plugin. To do so, we need to follow two simple steps:

<li>
**Install :** Add the plugin to the project's dependencies in `Cargo.toml`.
<Tabs>
<TabItem label="crates.io">
```toml title="src-tauri/Cargo.toml" ins={2}
[dependencies]
tauri-plugin-single-instance = "2.0.0-beta"
```
</TabItem>
<TabItem label="Git">

```toml title="src-tauri/Cargo.toml" ins={2}
[dependencies]
tauri-plugin-single-instance = { git = "https://github.com/tauri-apps/plugins-workspace", branch = "v2" }
```
</TabItem>
</Tabs>
</li>
<li>
**Initialize :** Update `lib.rs` (or `main.rs` for desktop-only apps) to initialize the plugin:

```rust title="src-tauri/src/lib.rs" ins={3}
pub fn run() {
tauri::Builder::default()
.plugin(tauri_plugin_single_instance::init(|app, args, cwd| {}))
.run(tauri::generate_context!())
.expect("error while running tauri application");
}
```
</li>
</ol>
</Steps>
</TabItem>

</Tabs>

## Usage

The plugin is already installed and initialized, and it should be functioning correctly right away. Nevertheless, we can also enhance its functionality with the `init()` method.

The plugin `init()` method takes a closure that is invoked when a new app instance was started, but closed by the plugin.
The closure has three arguments:
1. **`app` :** The [AppHandle](https://docs.rs/tauri/latest/tauri/struct.AppHandle.html) of the application.
2. **`args` :** The list of arguments, that was passed by the user to initiate this new instance.
3. **`cwd` :** The Current Working Directory denotes the directory from which the new application instance was launched.

So, the closure should look like below

```rust
.plugin(tauri_plugin_single_instance::init(|app, args, cwd| {
// Write your code here...
}))
```

### Focusing on New Instance

By default, when you initiate a new instance while the application is already running, no action is taken. To focus the window of the running instance when user tries to open a new instance, alter the callback closure as follows:


```rust title="src-tauri/src/lib.rs" {1} {5-7} {12-21}
use tauri::{AppHandle, Manager};

pub fn run() {
tauri::Builder::default()
.plugin(tauri_plugin_single_instance::init(|app, args, cwd| {
let _ = show_window(app);
}))
.run(tauri::generate_context!())
.expect("error while running tauri application");
}

fn show_window(app: &AppHandle) {
let windows = app.webview_windows();

windows
.values()
.next()
.expect("Sorry, no window found")
.set_focus()
.expect("Can't Bring Window to Focus");
}
```
Loading