[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:
Jacob Bolda
2024-10-01 08:15:22 -05:00
committed by GitHub
parent 30bcf5dcc2
commit 2f7e32b5e0
11 changed files with 103 additions and 6 deletions
+35
View File
@@ -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