Skip to content
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

docs(features): Add single instance page #1931

Merged
merged 18 commits into from
Apr 1, 2024
Merged
Changes from 3 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
78 changes: 72 additions & 6 deletions src/content/docs/features/single-instance.mdx
Original file line number Diff line number Diff line change
@@ -1,14 +1,80 @@
---
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:
naman-crabnebula marked this conversation as resolved.
Show resolved Hide resolved

```rust title="src-tauri/src/main.rs" ins={4}
fn main() {
tauri::Builder::default()
.plugin(tauri_plugin_shell::init())
.plugin(tauri_plugin_single_instance::init(|_, _, _| {}))
.invoke_handler(tauri::generate_handler![greet])
.run(tauri::generate_context!())
.expect("error while running tauri application");
}
```
naman-crabnebula marked this conversation as resolved.
Show resolved Hide resolved

### Focusing on New Instance

By default, nothing happens if you open a new instance and another instance is already running. To focus the Window of already running instance when user tries to open a new instance:
naman-crabnebula marked this conversation as resolved.
Show resolved Hide resolved


```rust title="src-tauri/src/main.rs" {1} {6-8} {14-23}
use tauri::{AppHandle, Manager};

fn main() {
tauri::Builder::default()
.plugin(tauri_plugin_shell::init())
.plugin(tauri_plugin_single_instance::init(|app, _, _| {
let _ = show_window(app);
}))
.invoke_handler(tauri::generate_handler![greet])
.run(tauri::generate_context!())
.expect("error while running tauri application");
}
naman-crabnebula marked this conversation as resolved.
Show resolved Hide resolved

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