An open API service indexing awesome lists of open source software.

https://github.com/freethinkel/tauri-nspopover-plugin

NSPopover plugin for tauri
https://github.com/freethinkel/tauri-nspopover-plugin

cargo macos menu menuapp nspopover popover rust statusbar tauri

Last synced: 3 months ago
JSON representation

NSPopover plugin for tauri

Awesome Lists containing this project

README

          

# Tauri Plugin NSpopover

Only for MacOS



## Install

```toml
# Cargo.toml
[dependencies]
tauri-plugin-nspopover = { git = "https://github.com/freethinkel/tauri-nspopover-plugin.git", version = "4.0.0" }
```

```json
// package.json
"dependencies": {
"tauri-plugin-nspopover": "git+https://github.com/freethinkel/tauri-nspopover-plugin"
}
```

## Usage

```rust
// main.rs
use tauri::{ActivationPolicy, Manager};
use tauri_plugin_nspopover::{AppExt, ToPopoverOptions, WindowExt};

fn main() {
tauri::Builder::default()
.setup(|app| {
app.set_activation_policy(ActivationPolicy::Accessory);
let window = app.handle().get_webview_window("main").unwrap();

window.to_popover(ToPopoverOptions {
is_fullsize_content: true,
});

Ok(())
})
.plugin(tauri_plugin_nspopover::init())
.run(tauri::generate_context!())
.expect("error while running tauri application");
}
```

```ts
// main.ts
import { TrayIcon } from "@tauri-apps/api/tray";
import { isOpen, show, hide } from "tauri-plugin-nspopover";

TrayIcon.new({
id: "main",
async action(event) {
console.log(event);
if (
event.type === "Click" &&
event.buttonState === "Up" &&
event.button === "Left"
) {
const isShown = await isOpen();

if (isShown) {
hide();
} else {
show();
}
}
},
});
```

OR you can use rust api

```rust
fn main() {
tauri::Builder::default()
.plugin(tauri_plugin_shell::init())
.plugin(tauri_plugin_nspopover::init())
.setup(|app| {
app.set_activation_policy(ActivationPolicy::Accessory);
let window = app.handle().get_webview_window("main").unwrap();
window.to_popover(ToPopoverOptions {
is_fullsize_content: true,
});

let tray = app.tray_by_id("main").unwrap();
let handle = app.handle().clone();

tray.on_tray_icon_event(move |_, event| match event {
TrayIconEvent::Click {
button,
button_state,
..
} => {
if button == MouseButton::Left && button_state == MouseButtonState::Up {
if !handle.is_popover_shown() {
handle.show_popover();
} else {
handle.hide_popover();
}
}
}
_ => {}
});

Ok(())
})
.run(tauri::generate_context!())
.expect("error while running tauri application");
}
```

```json
// tauri.config.json
"systemTray": {
"iconPath": "icons/statusbar-icon.png",
"iconAsTemplate": true,
"id": "main"
},
...
"windows": [
{
"fullscreen": false,
"visible": false,
"title": "example",
"width": 300,
"height": 450,
"transparent": true
}
]
```

## Permissions

Don't forget to add the necessary permissions to your `src-tauri/capabilities/default.json` file.

```json
...
"nspopover:allow-show-popover",
"nspopover:allow-hide-popover",
"nspopover:allow-is-popover-shown",
"core:tray:allow-new",
"core:tray:default"
...
```

## Example

```sh
git clone https://github.com/freethinkel/tauri-plugin-nspopover
cd tauri-plugin-nspopover/example
pnpm install
pnpm tauri dev
```