@@ -12,19 +12,14 @@ import NativeAPI
1212 return
1313 }
1414
15- print ( " ✅ System tray is supported " )
16-
1715 // Create a basic tray icon
1816 guard let trayIcon = trayManager. create ( ) else {
1917 print ( " ❌ Failed to create tray icon " )
2018 return
2119 }
2220
23- print ( " ✅ Tray icon created successfully with ID: \( trayIcon. id) " )
24-
2521 trayIcon. setTitle ( " NativeAPI Demo " )
2622 trayIcon. setTooltip ( " NativeAPI Tray Icon Demo " )
27- print ( " ✅ Tray icon configured " )
2823
2924 // Create context menu for tray icon
3025 guard let contextMenu = Menu . create ( ) else {
@@ -39,10 +34,8 @@ import NativeAPI
3934 }
4035 contextMenu. addItem ( showItem)
4136 showItem. onClicked { menuItem in
42- print ( " 📱 显示窗口 - 菜单项点击成功! " )
43- print ( " 菜单项详情: ID= \( menuItem. id) , Text=' \( menuItem. getText ( ) ) ' " )
37+ print ( " 📱 显示窗口 " )
4438 }
45- print ( " ✅ 创建'显示窗口'菜单项,ID: \( showItem. id) " )
4639
4740 // Add separator
4841 contextMenu. addSeparator ( )
@@ -54,11 +47,8 @@ import NativeAPI
5447 }
5548 contextMenu. addItem ( aboutItem)
5649 aboutItem. onClicked { menuItem in
57- print ( " ℹ️ 关于菜单项点击成功! " )
58- print ( " NativeAPI Demo v1.0 " )
59- print ( " 菜单项详情: ID= \( menuItem. id) , Text=' \( menuItem. getText ( ) ) ' " )
50+ print ( " ℹ️ 关于 - NativeAPI Demo v1.0 " )
6051 }
61- print ( " ✅ 创建'关于'菜单项,ID: \( aboutItem. id) " )
6252
6353 // Add "Settings" menu item
6454 guard let settingsItem = MenuItem . create ( " 设置 " ) else {
@@ -67,65 +57,49 @@ import NativeAPI
6757 }
6858 contextMenu. addItem ( settingsItem)
6959 settingsItem. onClicked { menuItem in
70- print ( " ⚙️ 设置菜单项点击成功! " )
71- print ( " 打开设置面板 " )
72- print ( " 菜单项详情: ID= \( menuItem. id) , Text=' \( menuItem. getText ( ) ) ' " )
60+ print ( " ⚙️ 打开设置面板 " )
7361 }
74- print ( " ✅ 创建'设置'菜单项,ID: \( settingsItem. id) " )
7562
7663 // Add separator
7764 contextMenu. addSeparator ( )
7865
7966 // Add checkbox items for demonstration
80- print ( " 📝 添加 Checkbox 菜单项演示... " )
81-
82- // First create the checkbox items without event handlers
8367 guard let showToolbarItem = MenuItem . create ( " 显示工具栏 " , type: . checkbox) else {
8468 print ( " ❌ Failed to create toolbar checkbox " )
8569 return
8670 }
8771 showToolbarItem. setChecked ( true )
8872 contextMenu. addItem ( showToolbarItem)
89- print ( " ✅ 创建复选框'显示工具栏',ID: \( showToolbarItem. id) , 初始状态: \( showToolbarItem. isChecked ( ) ) " )
9073
9174 guard let autoSaveItem = MenuItem . create ( " 自动保存 " , type: . checkbox) else {
9275 print ( " ❌ Failed to create autosave checkbox " )
9376 return
9477 }
9578 autoSaveItem. setChecked ( false )
9679 contextMenu. addItem ( autoSaveItem)
97- print ( " ✅ 创建复选框'自动保存',ID: \( autoSaveItem. id) , 初始状态: \( autoSaveItem. isChecked ( ) ) " )
9880
99- // Now add event handlers after the variables are declared
81+ // Add event handlers for checkboxes
10082 showToolbarItem. onClicked { menuItem in
10183 let isChecked = menuItem. isChecked ( )
102- print ( " ☑️ 工具栏显示状态切换: \( isChecked) " )
103- print ( " 工具栏现在 \( isChecked ? " 显示 " : " 隐藏 " ) " )
104- print ( " 菜单项详情: ID= \( menuItem. id) , Text=' \( menuItem. getText ( ) ) ' " )
84+ print ( " ☑️ 工具栏 \( isChecked ? " 显示 " : " 隐藏 " ) " )
10585 }
10686
10787 autoSaveItem. onClicked { menuItem in
10888 let isChecked = menuItem. isChecked ( )
109- print ( " ☑️ 自动保存状态切换: \( isChecked) " )
110- print ( " 自动保存功能 \( isChecked ? " 已启用 " : " 已禁用 " ) " )
111- print ( " 菜单项详情: ID= \( menuItem. id) , Text=' \( menuItem. getText ( ) ) ' " )
89+ print ( " ☑️ 自动保存 \( isChecked ? " 已启用 " : " 已禁用 " ) " )
11290 }
11391
11492 // Add separator
11593 contextMenu. addSeparator ( )
11694
11795 // Add radio button group for view mode selection
118- print ( " 📝 添加 Radio 按钮组演示... " )
119-
120- // First create the radio items without event handlers
12196 guard let compactViewItem = MenuItem . create ( " 紧凑视图 " , type: . radio) else {
12297 print ( " ❌ Failed to create compact view radio " )
12398 return
12499 }
125100 compactViewItem. setRadioGroup ( 1 )
126101 compactViewItem. setChecked ( false )
127102 contextMenu. addItem ( compactViewItem)
128- print ( " ✅ 创建单选按钮'紧凑视图',ID: \( compactViewItem. id) , 组: 1, 选中: \( compactViewItem. isChecked ( ) ) " )
129103
130104 guard let normalViewItem = MenuItem . create ( " 普通视图 " , type: . radio) else {
131105 print ( " ❌ Failed to create normal view radio " )
@@ -134,7 +108,6 @@ import NativeAPI
134108 normalViewItem. setRadioGroup ( 1 )
135109 normalViewItem. setChecked ( true )
136110 contextMenu. addItem ( normalViewItem)
137- print ( " ✅ 创建单选按钮'普通视图',ID: \( normalViewItem. id) , 组: 1, 选中: \( normalViewItem. isChecked ( ) ) " )
138111
139112 guard let detailedViewItem = MenuItem . create ( " 详细视图 " , type: . radio) else {
140113 print ( " ❌ Failed to create detailed view radio " )
@@ -143,28 +116,21 @@ import NativeAPI
143116 detailedViewItem. setRadioGroup ( 1 )
144117 detailedViewItem. setChecked ( false )
145118 contextMenu. addItem ( detailedViewItem)
146- print ( " ✅ 创建单选按钮'详细视图',ID: \( detailedViewItem. id) , 组: 1, 选中: \( detailedViewItem. isChecked ( ) ) " )
147119
148- // Now add event handlers after the variables are declared
120+ // Add event handlers for radio buttons
149121 compactViewItem. onClicked { menuItem in
150122 menuItem. setState ( . checked)
151- print ( " 🔘 视图模式切换为: 紧凑视图 " )
152- print ( " Radio 组 ID: 1 " )
153- print ( " 菜单项详情: ID= \( menuItem. id) , Text=' \( menuItem. getText ( ) ) ' " )
123+ print ( " 🔘 视图模式: 紧凑视图 " )
154124 }
155125
156126 normalViewItem. onClicked { menuItem in
157127 menuItem. setState ( . checked)
158- print ( " 🔘 视图模式切换为: 普通视图 " )
159- print ( " Radio 组 ID: 1 " )
160- print ( " 菜单项详情: ID= \( menuItem. id) , Text=' \( menuItem. getText ( ) ) ' " )
128+ print ( " 🔘 视图模式: 普通视图 " )
161129 }
162130
163131 detailedViewItem. onClicked { menuItem in
164132 menuItem. setState ( . checked)
165- print ( " 🔘 视图模式切换为: 详细视图 " )
166- print ( " Radio 组 ID: 1 " )
167- print ( " 菜单项详情: ID= \( menuItem. id) , Text=' \( menuItem. getText ( ) ) ' " )
133+ print ( " 🔘 视图模式: 详细视图 " )
168134 }
169135
170136 // Add separator
@@ -177,75 +143,52 @@ import NativeAPI
177143 }
178144 contextMenu. addItem ( exitItem)
179145 exitItem. onClicked { menuItem in
180- print ( " 👋 退出菜单项点击成功! " )
181- print ( " 退出应用程序 " )
182- print ( " 菜单项详情: ID= \( menuItem. id) , Text=' \( menuItem. getText ( ) ) ' " )
146+ print ( " 👋 退出应用程序 " )
183147 exit ( 0 )
184148 }
185- print ( " ✅ 创建'退出'菜单项,ID: \( exitItem. id) " )
186149
187150 // Set the context menu for tray icon
188151 trayIcon. setContextMenu ( contextMenu)
189- print ( " ✅ 上下文菜单已设置 " )
190152
191153 // Configure click handlers
192- trayIcon. onLeftClick { event in
193- print ( " 👆 Tray icon left clicked, ID: \( event. trayIconId) " )
194- print ( " 💡 左键点击 - 可以显示主窗口或切换可见性 " )
154+ trayIcon. onLeftClick { trayIcon, event in
155+ print ( " 👆 托盘图标左键点击 " )
195156 }
196157
197- trayIcon. onRightClick { event in
198- print ( " 👆 Tray icon right clicked, ID: \( event. trayIconId) " )
199- print ( " 💡 右键点击 - 显示上下文菜单 " )
158+ trayIcon. onRightClick { trayIcon, event in
159+ print ( " 👆 托盘图标右键点击 " )
200160 }
201161
202- trayIcon. onDoubleClick { event in
203- print ( " 👆 Tray icon double clicked, ID: \( event. trayIconId) " )
204- print ( " 💡 双击事件触发 " )
162+ trayIcon. onDoubleClick { trayIcon, event in
163+ print ( " 👆 托盘图标双击 " )
205164 }
206165
207- print ( " ✅ Click handlers configured " )
208-
209166 // Show the tray icon
210167 if trayIcon. show ( ) {
211- print ( " ✅ Tray icon shown successfully " )
212- print ( " 💡 Tray icon is visible: \( trayIcon. isVisible) " )
213- print ( " 💡 右键点击托盘图标可查看菜单 " )
214-
215- let bounds = trayIcon. bounds
216- print ( " 💡 Tray icon bounds: ( \( bounds. x) , \( bounds. y) , \( bounds. width) x \( bounds. height) ) " )
168+ print ( " ✅ 托盘图标已显示,右键点击查看菜单 " )
217169 } else {
218170 print ( " ❌ Failed to show tray icon " )
219171 }
220172}
221- // MARK: - Main Application
222173
223- print ( " === NativeAPI Tray Icon Demo === " )
224- print ( " 🚀 Testing TrayIcon with ContextMenu " )
225- print ( )
174+ // MARK: - Main Application
226175
227- // Create and run the application with tray icon
228176@MainActor func runApplication( ) {
229177 // Create tray icon with context menu
230178 createTrayIconWithContextMenu ( )
231179
232- // Keep the application running
233- print ( " \n ✅ Tray icon demo started " )
234- print ( " 💡 应用程序正在运行,托盘图标已显示 " )
235- print ( " 💡 右键点击托盘图标查看上下文菜单 " )
236- print ( " 💡 测试功能: " )
180+ print ( " \n ✅ NativeAPI 托盘图标演示已启动 " )
181+ print ( " 💡 功能测试: " )
237182 print ( " • 普通菜单项: 显示窗口、关于、设置 " )
238183 print ( " • 复选框菜单项: 显示工具栏、自动保存 " )
239184 print ( " • 单选按钮组: 紧凑视图、普通视图、详细视图 " )
240185 print ( " • 退出: 关闭应用程序 " )
241- print ( " 💡 点击菜单项测试各种事件处理 " )
242186
243187 // Create a minimal window to keep the app running
244188 let windowOptions = WindowOptions ( )
245189 _ = windowOptions. setTitle ( " NativeAPI Demo " )
246190 windowOptions. setSize ( Size ( width: 400 , height: 300 ) )
247191
248- print ( " 💡 创建后台窗口以保持应用程序运行 " )
249192 guard let window = WindowManager . shared. create ( with: windowOptions) else {
250193 print ( " ❌ 无法创建窗口 " )
251194 return
0 commit comments