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 7 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
74 changes: 68 additions & 6 deletions src/content/docs/features/single-instance.mdx
Original file line number Diff line number Diff line change
@@ -1,14 +1,76 @@
---
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 } from '@astrojs/starlight/components';

<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.


## Install

_This plugin requires a Rust version of at least **1.75**_

You can install the Core plugin by adding the following to your `Cargo.toml` file:

<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>

## Usage

To use this plugin, you need to register the core plugin with Tauri. The plugin init() method takes a closure that is called when a new app instance was started, but closed by the plugin. The closure has three arguments: the AppHandle, the list of arguments (important for instance on deep link usage) and the current working directory:
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

(important for instance on deep link usage)

as this is being mentioned, it could be explained why in a aside, without diving too much. Since this was Lucas' suggestions, I'll tag him to give us a hand @lucasfernog could you help on these?

the plugin takes a closure that is called when a new app instance was started, but closed by the plugin

this is a byte ambiguous

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

General idea (you can use Starlight steps component) if you choose to do like this

Usage

  1. register the core plugin with Tauri.

// show how

  1. the plugin takes a closure that is called when a new app instance was started, but closed by the plugin. It has three arguments:
  • app, being the AppHandle,
  • args: the list of arguments
  • cwd the current working directory (what should it default to?)

:::note
why args is important for instance on deep link usage
:::

// maybe show only the line highlighting app, agrs, cwd
// .plugin(tauri_plugin_single_instance::init(|app, args, cwd| {}))


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

### Focusing on New Instance

By default nothing happens when you start a new instance and the application is already running. To focus the window of the running instance when user tries to open a new instance, change the callback closure:


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

fn main() {
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");
}
```