Skip to content

Commit b9aa960

Browse files
jboldalucasfernog
authored andcommitted
[positioner] handleIconState in JS (tauri-apps#1822)
* [positioner] handleIconState in JS * update readme * fix change file version * fixes --------- Co-authored-by: Lucas Nogueira <[email protected]>
1 parent 3e54c48 commit b9aa960

File tree

11 files changed

+103
-6
lines changed

11 files changed

+103
-6
lines changed
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
---
2+
"positioner": patch
3+
"positioner-js": patch
4+
---
5+
6+
`handleIconState` function for use in JavaScript event handlers. This allows one to update the TrayIcon state through JavaScript and fully create and handle the TrayIcon without requiring Rust (and the side-effect of creating a TrayIcon).

plugins/barcode-scanner/api-iife.js

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

plugins/positioner/README.md

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,7 @@ fn main() {
5858
.plugin(tauri_plugin_positioner::init())
5959
// This is required to get tray-relative positions to work
6060
.setup(|app| {
61+
// note that this will create a new TrayIcon
6162
TrayIconBuilder::new()
6263
.on_tray_icon_event(|app, event| {
6364
tauri_plugin_positioner::on_tray_event(app.app_handle(), &event);
@@ -70,6 +71,40 @@ fn main() {
7071
}
7172
```
7273

74+
Alternatively, you may handle the tray events through JavaScript. Register the plugin as previously noted.
75+
76+
```rust
77+
fn main() {
78+
tauri::Builder::default()
79+
.plugin(tauri_plugin_positioner::init())
80+
.run(tauri::generate_context!())
81+
.expect("error while running tauri application");
82+
}
83+
```
84+
85+
And in JavaScript, the `action` passed to the TrayIcon should include the handler.
86+
87+
```javascript
88+
import {
89+
moveWindow,
90+
Position,
91+
handleIconState,
92+
} from "@tauri-apps/plugin-positioner";
93+
94+
const action = async (event: TrayIconEvent) => {
95+
// add the handle in the action to update the state
96+
await handleIconState(event);
97+
if ("click" in event) {
98+
const { click } = event;
99+
// note this option requires enabling the `tray-icon`
100+
// feature in the Cargo.toml
101+
await moveWindow(Position.TrayLeft);
102+
}
103+
};
104+
105+
const tray = await TrayIcon.new({ id: "main", action });
106+
```
107+
73108
Afterwards all the plugin's APIs are available through the JavaScript guest bindings:
74109

75110
```javascript

plugins/positioner/api-iife.js

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

plugins/positioner/guest-js/index.ts

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
// SPDX-License-Identifier: MIT
55

66
import { invoke } from '@tauri-apps/api/core'
7+
import type { TrayIconEvent } from '@tauri-apps/api/tray'
78

89
/**
910
* Well known window positions.
@@ -37,3 +38,14 @@ export async function moveWindow(to: Position): Promise<void> {
3738
position: to
3839
})
3940
}
41+
42+
export async function handleIconState(event: TrayIconEvent): Promise<void> {
43+
await invokeSetTrayIconState(event.rect)
44+
}
45+
46+
async function invokeSetTrayIconState(rect: TrayIconEvent['rect']) {
47+
await invoke('plugin:positioner|set_tray_icon_state', {
48+
position: rect.position,
49+
size: rect.size
50+
})
51+
}
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
# Automatically generated - DO NOT EDIT!
2+
3+
"$schema" = "../../schemas/schema.json"
4+
5+
[[permission]]
6+
identifier = "allow-set-tray-icon-state"
7+
description = "Enables the set_tray_icon_state to handle events and set the TrayIcon state."
8+
commands.allow = ["set_tray_icon_state"]

plugins/positioner/permissions/autogenerated/reference.md

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
Allows the move_window command
44

55
- `allow-move-window`
6+
- `set-tray-icon-state`
67

78
## Permission Table
89

@@ -36,6 +37,19 @@ Enables the move_window command without any pre-configured scope.
3637

3738
Denies the move_window command without any pre-configured scope.
3839

40+
</td>
41+
</tr>
42+
43+
<tr>
44+
<td>
45+
46+
`positioner:allow-set-tray-icon-state`
47+
48+
</td>
49+
<td>
50+
51+
Enables the set_tray_icon_state to handle events and set the TrayIcon state.
52+
3953
</td>
4054
</tr>
4155
</table>
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
11
"$schema" = "schemas/schema.json"
22
[default]
33
description = "Allows the move_window command"
4-
permissions = ["allow-move-window"]
4+
permissions = ["allow-move-window", "set-tray-icon-state"]

plugins/positioner/permissions/schemas/schema.json

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -304,6 +304,11 @@
304304
"type": "string",
305305
"const": "deny-move-window"
306306
},
307+
{
308+
"description": "Enables the set_tray_icon_state to handle events and set the TrayIcon state.",
309+
"type": "string",
310+
"const": "allow-set-tray-icon-state"
311+
},
307312
{
308313
"description": "Allows the move_window command",
309314
"type": "string",

plugins/positioner/src/lib.rs

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -63,10 +63,27 @@ async fn move_window<R: Runtime>(window: tauri::Window<R>, position: Position) -
6363
window.move_window(position)
6464
}
6565

66+
#[cfg(feature = "tray-icon")]
67+
#[tauri::command]
68+
fn set_tray_icon_state<R: Runtime>(
69+
app: AppHandle<R>,
70+
position: PhysicalPosition<f64>,
71+
size: PhysicalSize<f64>,
72+
) {
73+
app.state::<Tray>()
74+
.0
75+
.lock()
76+
.unwrap()
77+
.replace((position, size));
78+
}
79+
6680
/// The Tauri plugin that exposes [`WindowExt::move_window`] to the webview.
6781
pub fn init<R: Runtime>() -> TauriPlugin<R> {
68-
let plugin =
69-
plugin::Builder::new("positioner").invoke_handler(tauri::generate_handler![move_window]);
82+
let plugin = plugin::Builder::new("positioner").invoke_handler(tauri::generate_handler![
83+
move_window,
84+
#[cfg(feature = "tray-icon")]
85+
set_tray_icon_state
86+
]);
7087

7188
#[cfg(feature = "tray-icon")]
7289
let plugin = plugin.setup(|app_handle, _api| {

0 commit comments

Comments
 (0)