mirror of
https://github.com/tauri-apps/plugins-workspace.git
synced 2026-07-24 17:20:51 +02:00
[positioner] handleIconState in JS (#1822)
* [positioner] handleIconState in JS * update readme * fix change file version * fixes --------- Co-authored-by: Lucas Nogueira <lucas@tauri.app>
This commit is contained in:
@@ -58,6 +58,7 @@ fn main() {
|
||||
.plugin(tauri_plugin_positioner::init())
|
||||
// This is required to get tray-relative positions to work
|
||||
.setup(|app| {
|
||||
// note that this will create a new TrayIcon
|
||||
TrayIconBuilder::new()
|
||||
.on_tray_icon_event(|app, event| {
|
||||
tauri_plugin_positioner::on_tray_event(app.app_handle(), &event);
|
||||
@@ -70,6 +71,40 @@ fn main() {
|
||||
}
|
||||
```
|
||||
|
||||
Alternatively, you may handle the tray events through JavaScript. Register the plugin as previously noted.
|
||||
|
||||
```rust
|
||||
fn main() {
|
||||
tauri::Builder::default()
|
||||
.plugin(tauri_plugin_positioner::init())
|
||||
.run(tauri::generate_context!())
|
||||
.expect("error while running tauri application");
|
||||
}
|
||||
```
|
||||
|
||||
And in JavaScript, the `action` passed to the TrayIcon should include the handler.
|
||||
|
||||
```javascript
|
||||
import {
|
||||
moveWindow,
|
||||
Position,
|
||||
handleIconState,
|
||||
} from "@tauri-apps/plugin-positioner";
|
||||
|
||||
const action = async (event: TrayIconEvent) => {
|
||||
// add the handle in the action to update the state
|
||||
await handleIconState(event);
|
||||
if ("click" in event) {
|
||||
const { click } = event;
|
||||
// note this option requires enabling the `tray-icon`
|
||||
// feature in the Cargo.toml
|
||||
await moveWindow(Position.TrayLeft);
|
||||
}
|
||||
};
|
||||
|
||||
const tray = await TrayIcon.new({ id: "main", action });
|
||||
```
|
||||
|
||||
Afterwards all the plugin's APIs are available through the JavaScript guest bindings:
|
||||
|
||||
```javascript
|
||||
|
||||
Reference in New Issue
Block a user