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
examples/tray-with-custom-tray-menu-window/src/tray.jsadded+110-0
Changes to examples/tray-with-custom-tray-menu-window/src/tray.js
@@ -0,0 +1,110 @@
1import { window } from '@tauri-apps/api'
2import { defaultWindowIcon } from '@tauri-apps/api/app'
3import { invoke } from '@tauri-apps/api/core'
4import { TrayIcon } from '@tauri-apps/api/tray'
5import { Webview } from '@tauri-apps/api/webview'
6import { Window } from '@tauri-apps/api/window'
7import { handleIconState } from '@tauri-apps/plugin-positioner'
8
9/**
10 * Initialize the system tray icon and custom window
11 */
12export async function initTray() {
13 try {
14 // eslint-disable-next-line no-console
15 console.log('Initializing system tray...')
16 const appWindow = window.getCurrentWindow()
17 const trayWindow = await createTrayCustomWindow()
18
19 const action = async (event) => {
20 await handleIconState(event)
21
22 if (event.type === 'Click') {
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 }
31 }
32 }
33
34 // Create the tray icon with minimal options
35 const trayIcon = await TrayIcon.new({
36 id: 'main-tray',
37 icon: await defaultWindowIcon(),
38 tooltip: 'Tauri Example App',
39 iconAsTemplate: true,
40 action,
41 })
42
43 // eslint-disable-next-line no-console
44 console.log('System tray initialized with ID:', trayIcon.id)
45
46 // Setup window close handler to minimize to tray instead of quitting
47 appWindow.onCloseRequested(async (event) => {
48 // eslint-disable-next-line no-console
49 console.log('Window close requested, minimizing to tray instead')
50 event.preventDefault()
51 await appWindow.hide()
52 })
53
54 return trayIcon
55 }
56 catch (error) {
57 console.error('Failed to initialize system tray:', error)
58 throw error
59 }
60}
61
62async function createTrayCustomWindow(url = '../index.html') {
63 const trayKey = 'tray-window'
64
65 const trayWindow = await Window.getByLabel(trayKey)
66
67 const customTrayWindow = trayWindow || new Window(trayKey, {
68 focus: true,
69 decorations: false,
70 resizable: false,
71 height: 300,
72 width: 400,
73 skipTaskbar: true,
74 visible: false,
75 })
76
77 customTrayWindow.once('tauri://created', () => {
78 // The window was created successfully
79 // eslint-disable-next-line no-console
80 console.log('Custom tray window created successfully')
81
82 const webview = new Webview(customTrayWindow, trayKey, {
83 // url: 'https://github.com/tauri-apps/tauri',
84 url,
85 height: 300,
86 width: 400,
87 x: 0,
88 y: 0,
89 })
90
91 webview.once('tauri://created', () => {
92 // The webview was created successfully
93 // eslint-disable-next-line no-console
94 console.log('Webview created successfully')
95 })
96 webview.once('tauri://error', (e) => {
97 // The webview encountered an error
98
99 console.error('Webview error:', e)
100 })
101 })
102
103 customTrayWindow.once('tauri://error', (e) => {
104 // The window encountered an error
105
106 console.error('Custom tray window error:', e)
107 })
108
109 return customTrayWindow
110}