1
+ using System ;
2
+ using System . Diagnostics ;
3
+ using System . IO ;
4
+ using System . Linq ;
1
5
using Avalonia ;
6
+ using Avalonia . Controls ;
2
7
using Avalonia . Controls . ApplicationLifetimes ;
3
8
using Avalonia . Markup . Xaml ;
9
+ using Avalonia . Platform ;
4
10
using SemanticCode . ViewModels ;
5
11
using SemanticCode . Views ;
6
12
7
13
namespace SemanticCode ;
8
14
9
15
public partial class App : Application
10
16
{
17
+ private TrayIcon ? _trayIcon ;
18
+
11
19
public override void Initialize ( )
12
20
{
13
21
AvaloniaXamlLoader . Load ( this ) ;
@@ -17,10 +25,13 @@ public override void OnFrameworkInitializationCompleted()
17
25
{
18
26
if ( ApplicationLifetime is IClassicDesktopStyleApplicationLifetime desktop )
19
27
{
20
- desktop . MainWindow = new MainWindow
28
+ var mainWindow = new MainWindow
21
29
{
22
30
DataContext = new MainViewModel ( )
23
31
} ;
32
+ desktop . MainWindow = mainWindow ;
33
+
34
+ CreateTrayIcon ( ) ;
24
35
}
25
36
else if ( ApplicationLifetime is ISingleViewApplicationLifetime singleViewPlatform )
26
37
{
@@ -32,4 +43,107 @@ public override void OnFrameworkInitializationCompleted()
32
43
33
44
base . OnFrameworkInitializationCompleted ( ) ;
34
45
}
46
+
47
+ private void CreateTrayIcon ( )
48
+ {
49
+ try
50
+ {
51
+ var menu = new NativeMenu ( ) ;
52
+
53
+ var showMenuItem = new NativeMenuItem ( "显示主窗口" ) ;
54
+ showMenuItem . Click += ShowMainWindow ;
55
+ menu . Add ( showMenuItem ) ;
56
+
57
+ var openClaudeMenuItem = new NativeMenuItem ( "打开 .claude 文件夹" ) ;
58
+ openClaudeMenuItem . Click += OpenClaudeFolder ;
59
+ menu . Add ( openClaudeMenuItem ) ;
60
+
61
+ menu . Add ( new NativeMenuItemSeparator ( ) ) ;
62
+
63
+ var exitMenuItem = new NativeMenuItem ( "退出" ) ;
64
+ exitMenuItem . Click += ExitApplication ;
65
+ menu . Add ( exitMenuItem ) ;
66
+
67
+ _trayIcon = new TrayIcon
68
+ {
69
+ Icon = new WindowIcon ( AssetLoader . Open ( new Uri ( "avares://SemanticCode/Assets/favicon.ico" ) ) ) ,
70
+ ToolTipText = "SemanticCode" ,
71
+ Menu = menu
72
+ } ;
73
+
74
+ _trayIcon . Clicked += ShowMainWindow ;
75
+ TrayIcon . SetIcons ( this , [ _trayIcon ] ) ;
76
+ }
77
+ catch ( Exception ex )
78
+ {
79
+ Debug . WriteLine ( $ "Failed to create tray icon: { ex . Message } ") ;
80
+ }
81
+ }
82
+
83
+ private void OpenClaudeFolder ( object ? sender , EventArgs eventArgs )
84
+ {
85
+ try
86
+ {
87
+ var userProfile = Environment . GetFolderPath ( Environment . SpecialFolder . UserProfile ) ;
88
+ var claudeFolder = Path . Combine ( userProfile , ".claude" ) ;
89
+
90
+ if ( ! Directory . Exists ( claudeFolder ) )
91
+ {
92
+ Directory . CreateDirectory ( claudeFolder ) ;
93
+ }
94
+
95
+ if ( OperatingSystem . IsWindows ( ) )
96
+ {
97
+ Process . Start ( new ProcessStartInfo
98
+ {
99
+ FileName = "explorer.exe" ,
100
+ Arguments = claudeFolder ,
101
+ UseShellExecute = true
102
+ } ) ;
103
+ }
104
+ else if ( OperatingSystem . IsMacOS ( ) )
105
+ {
106
+ Process . Start ( new ProcessStartInfo
107
+ {
108
+ FileName = "open" ,
109
+ Arguments = claudeFolder ,
110
+ UseShellExecute = true
111
+ } ) ;
112
+ }
113
+ else if ( OperatingSystem . IsLinux ( ) )
114
+ {
115
+ Process . Start ( new ProcessStartInfo
116
+ {
117
+ FileName = "xdg-open" ,
118
+ Arguments = claudeFolder ,
119
+ UseShellExecute = true
120
+ } ) ;
121
+ }
122
+ }
123
+ catch ( Exception ex )
124
+ {
125
+ // Log error or show message
126
+ Debug . WriteLine ( $ "Failed to open .claude folder: { ex . Message } ") ;
127
+ }
128
+ }
129
+
130
+ private void ExitApplication ( object ? sender , EventArgs eventArgs )
131
+ {
132
+ if ( ApplicationLifetime is IClassicDesktopStyleApplicationLifetime desktop )
133
+ {
134
+ desktop . Shutdown ( ) ;
135
+ }
136
+ }
137
+ private void ShowMainWindow ( object ? sender , EventArgs e )
138
+ {
139
+ if ( Application . Current ? . ApplicationLifetime is IClassicDesktopStyleApplicationLifetime desktop )
140
+ {
141
+ if ( desktop . MainWindow != null )
142
+ {
143
+ desktop . MainWindow . Show ( ) ;
144
+ desktop . MainWindow . WindowState = WindowState . Normal ;
145
+ desktop . MainWindow . Activate ( ) ;
146
+ }
147
+ }
148
+ }
35
149
}
0 commit comments