ReviewOS

also looking at this

stacks/vite-plugin-tauri

feat: call custom window on tray icon event

#13
Open iamzjohn wants to merge feat/custom_window into main
43 files +5245 -1

Review threads live on the whole diff, not on one commit, so none are shown here - a thread's line means something in the branch's final form, and painting it into an intermediate step would put it on code it is not about.

eslint.config.tsmodified+1-1
Changes to eslint.config.ts
@@ -12,7 +12,7 @@ const config: ESLintConfig = stacks({
1212 yaml: true,
1313 ignores: [
1414 'fixtures/**',
15 'example/src-tauri/**',
15 'examples/**/src-tauri/**',
1616 ],
1717})
1818
examples/tray-with-custom-tray-menu-window/src-tauri/Cargo.tomlmodified+1-3
Changes to examples/tray-with-custom-tray-menu-window/src-tauri/Cargo.toml
@@ -22,7 +22,7 @@ tauri = { version = "2.4.1", features = [
2222 "image-png",
2323 "unstable"
2424] }
25# tauri-plugin-positioner = { version = "2.0.0", features = [ "tray-icon" ] }
25tauri-plugin-positioner = { version = "2.0.0", features = [ "tray-icon" ] }
2626
2727[features]
2828# this feature is used for production builds or when `devPath` points to the filesystem
@@ -30,5 +30,3 @@ tauri = { version = "2.4.1", features = [
3030custom-protocol = [ "tauri/custom-protocol" ]
3131tray-icon = [ ]
3232
33[target.'cfg(not(any(target_os = "android", target_os = "ios")))'.dependencies]
34tauri-plugin-positioner = "2"
examples/tray-with-custom-tray-menu-window/src-tauri/src/main.rsmodified+14-0
Changes to examples/tray-with-custom-tray-menu-window/src-tauri/src/main.rs
@@ -3,8 +3,22 @@
33 windows_subsystem = "windows"
44)]
55
6use tauri::Manager;
7use tauri_plugin_positioner::{WindowExt, Position};
8
9#[tauri::command]
10fn move_window(app: tauri::AppHandle, window_label: String) {
11 if let Some(win) = app.get_window(&window_label) {
12 let _ = win.move_window(Position::TrayBottomCenter);
13 } else {
14 eprintln!("Window with label '{}' not found", window_label);
15 }
16}
17
618fn main() {
719 tauri::Builder::default()
20 .plugin(tauri_plugin_positioner::init())
21 .invoke_handler(tauri::generate_handler![move_window])
822 .run(tauri::generate_context!())
923 .expect("error while running tauri application");
1024}
examples/tray-with-custom-tray-menu-window/src/tray.jsmodified+16-9
Changes to examples/tray-with-custom-tray-menu-window/src/tray.js
@@ -1,27 +1,33 @@
11import { window } from '@tauri-apps/api'
22import { defaultWindowIcon } from '@tauri-apps/api/app'
3import { invoke } from '@tauri-apps/api/core'
34import { TrayIcon } from '@tauri-apps/api/tray'
45import { Webview } from '@tauri-apps/api/webview'
56import { Window } from '@tauri-apps/api/window'
67import { handleIconState } from '@tauri-apps/plugin-positioner'
78
89/**
9 * Initialize the system tray icon and menu
10 * Initialize the system tray icon and custom window
1011 */
1112export async function initTray() {
1213 try {
1314 // eslint-disable-next-line no-console
1415 console.log('Initializing system tray...')
1516 const appWindow = window.getCurrentWindow()
16 const trayWindow = createTrayCustomWindow()
17 const trayWindow = await createTrayCustomWindow()
1718
1819 const action = async (event) => {
1920 await handleIconState(event)
2021
2122 if (event.type === 'Click') {
22 // eslint-disable-next-line no-console
23 console.log('Tray icon clicked')
24 trayWindow.show()
23 if (await trayWindow.isVisible()) {
24 // If the tray window is visible, hide it
25 await trayWindow.hide()
26 }
27 else {
28 await trayWindow.show()
29 await invoke('move_window', { windowLabel: 'tray-window' })
30 }
2531 }
2632 }
2733
@@ -53,9 +59,12 @@ export async function initTray() {
5359 }
5460}
5561
56function createTrayCustomWindow(url = '../index.html') {
62async function createTrayCustomWindow(url = '../index.html') {
5763 const trayKey = 'tray-window'
58 const customTrayWindow = new Window(trayKey, {
64
65 const trayWindow = await Window.getByLabel(trayKey)
66
67 const customTrayWindow = trayWindow || new Window(trayKey, {
5968 focus: true,
6069 decorations: false,
6170 resizable: false,
@@ -83,8 +92,6 @@ function createTrayCustomWindow(url = '../index.html') {
8392 // The webview was created successfully
8493 // eslint-disable-next-line no-console
8594 console.log('Webview created successfully')
86 // customTrayWindow.show()
87 // webview.show()
8895 })
8996 webview.once('tauri://error', (e) => {
9097 // The webview encountered an error
packages/vite-plugin-tauri/src/tauri.tsmodified+8-0
Changes to packages/vite-plugin-tauri/src/tauri.ts
@@ -188,6 +188,10 @@ export function tauri(config?: TauriConfig): PluginOption {
188188 'positioner:default',
189189 'core:window:allow-show',
190190 'core:webview:allow-webview-show',
191 'core:window:allow-is-visible',
192 'core:window:allow-get-all-windows',
193 'core:window:allow-is-focused',
194 'core:window:allow-set-position',
191195 ],
192196 },
193197 ],
@@ -276,6 +280,10 @@ export function tauri(config?: TauriConfig): PluginOption {
276280 'positioner:default',
277281 'core:window:allow-show',
278282 'core:webview:allow-webview-show',
283 'core:window:allow-is-visible',
284 'core:window:allow-get-all-windows',
285 'core:window:allow-is-focused',
286 'core:window:allow-set-position',
279287 ],
280288 },
281289 ],