1
1
using System ;
2
2
using System . Collections . ObjectModel ;
3
+ using System . Diagnostics ;
3
4
using System . IO ;
4
5
using System . Linq ;
5
6
using System . Reactive ;
6
7
using System . Threading . Tasks ;
8
+ using Avalonia ;
9
+ using Avalonia . Controls ;
10
+ using Avalonia . Controls . ApplicationLifetimes ;
11
+ using Avalonia . Layout ;
12
+ using Avalonia . Media ;
7
13
using ReactiveUI ;
8
14
using SemanticCode . Models ;
9
15
using SemanticCode . Services ;
@@ -28,6 +34,7 @@ public AgentModel? SelectedAgent
28
34
29
35
public ReactiveCommand < Unit , Unit > AddAgentCommand { get ; }
30
36
public ReactiveCommand < AgentModel , Unit > EditAgentCommand { get ; }
37
+ public ReactiveCommand < AgentModel , Unit > EditAgentInEditorCommand { get ; }
31
38
public ReactiveCommand < AgentModel , Unit > DeleteAgentCommand { get ; }
32
39
public ReactiveCommand < Unit , Unit > RefreshCommand { get ; }
33
40
@@ -37,6 +44,7 @@ public AgentsManagementViewModel()
37
44
38
45
AddAgentCommand = ReactiveCommand . Create ( AddAgent ) ;
39
46
EditAgentCommand = ReactiveCommand . Create < AgentModel > ( EditAgent ) ;
47
+ EditAgentInEditorCommand = ReactiveCommand . Create < AgentModel > ( EditAgentInEditor ) ;
40
48
DeleteAgentCommand = ReactiveCommand . Create < AgentModel > ( DeleteAgent ) ;
41
49
RefreshCommand = ReactiveCommand . Create ( LoadAgents ) ;
42
50
@@ -115,10 +123,37 @@ private void EditAgent(AgentModel agent)
115
123
}
116
124
}
117
125
118
- private void DeleteAgent ( AgentModel agent )
126
+ private void EditAgentInEditor ( AgentModel agent )
127
+ {
128
+ if ( agent != null && ! string . IsNullOrEmpty ( agent . FilePath ) && File . Exists ( agent . FilePath ) )
129
+ {
130
+ try
131
+ {
132
+ Process . Start ( new ProcessStartInfo
133
+ {
134
+ FileName = agent . FilePath ,
135
+ UseShellExecute = true
136
+ } ) ;
137
+ System . Diagnostics . Debug . WriteLine ( $ "Opened agent file in editor: { agent . FilePath } ") ;
138
+ }
139
+ catch ( Exception ex )
140
+ {
141
+ System . Diagnostics . Debug . WriteLine ( $ "Failed to open agent file in editor: { ex . Message } ") ;
142
+ }
143
+ }
144
+ }
145
+
146
+ private async void DeleteAgent ( AgentModel agent )
119
147
{
120
148
if ( agent != null && ! string . IsNullOrEmpty ( agent . FileName ) )
121
149
{
150
+ // 显示确认对话框
151
+ var result = await ShowDeleteConfirmationDialog ( agent ) ;
152
+ if ( result != true )
153
+ {
154
+ return ;
155
+ }
156
+
122
157
if ( _directoryService . DeleteAgent ( agent . FileName ) )
123
158
{
124
159
Agents . Remove ( agent ) ;
@@ -136,6 +171,99 @@ private void DeleteAgent(AgentModel agent)
136
171
}
137
172
}
138
173
174
+ private async Task < bool > ShowDeleteConfirmationDialog ( AgentModel agent )
175
+ {
176
+ try
177
+ {
178
+ var mainWindow = Application . Current ? . ApplicationLifetime is IClassicDesktopStyleApplicationLifetime desktop
179
+ ? desktop . MainWindow
180
+ : null ;
181
+
182
+ if ( mainWindow == null )
183
+ return false ;
184
+
185
+ var dialog = new Window
186
+ {
187
+ Title = "确认删除" ,
188
+ Width = 400 ,
189
+ Height = 200 ,
190
+ WindowStartupLocation = WindowStartupLocation . CenterOwner ,
191
+ CanResize = false
192
+ } ;
193
+
194
+ var panel = new StackPanel
195
+ {
196
+ Margin = new Thickness ( 20 ) ,
197
+ Spacing = 20
198
+ } ;
199
+
200
+ panel . Children . Add ( new TextBlock
201
+ {
202
+ Text = $ "确定要删除Agent \" { agent . Name } \" 吗?",
203
+ FontSize = 16 ,
204
+ TextWrapping = TextWrapping . Wrap
205
+ } ) ;
206
+
207
+ panel . Children . Add ( new TextBlock
208
+ {
209
+ Text = "此操作无法撤销。" ,
210
+ FontSize = 12 ,
211
+ Foreground = Brushes . Gray
212
+ } ) ;
213
+
214
+ var buttonPanel = new StackPanel
215
+ {
216
+ Orientation = Orientation . Horizontal ,
217
+ HorizontalAlignment = HorizontalAlignment . Right ,
218
+ Spacing = 10
219
+ } ;
220
+
221
+ var cancelButton = new Button
222
+ {
223
+ Content = "取消" ,
224
+ Width = 80 ,
225
+ Height = 32
226
+ } ;
227
+
228
+ var deleteButton = new Button
229
+ {
230
+ Content = "删除" ,
231
+ Width = 80 ,
232
+ Height = 32 ,
233
+ Background = Brushes . Red ,
234
+ Foreground = Brushes . White
235
+ } ;
236
+
237
+ bool ? result = null ;
238
+
239
+ cancelButton . Click += ( s , e ) =>
240
+ {
241
+ result = false ;
242
+ dialog . Close ( ) ;
243
+ } ;
244
+
245
+ deleteButton . Click += ( s , e ) =>
246
+ {
247
+ result = true ;
248
+ dialog . Close ( ) ;
249
+ } ;
250
+
251
+ buttonPanel . Children . Add ( cancelButton ) ;
252
+ buttonPanel . Children . Add ( deleteButton ) ;
253
+ panel . Children . Add ( buttonPanel ) ;
254
+
255
+ dialog . Content = panel ;
256
+
257
+ await dialog . ShowDialog ( mainWindow ) ;
258
+ return result == true ;
259
+ }
260
+ catch ( Exception ex )
261
+ {
262
+ System . Diagnostics . Debug . WriteLine ( $ "Error showing delete confirmation dialog: { ex . Message } ") ;
263
+ return false ;
264
+ }
265
+ }
266
+
139
267
private void SetupFileWatcher ( )
140
268
{
141
269
try
@@ -153,8 +281,6 @@ private void SetupFileWatcher()
153
281
} ;
154
282
155
283
_fileWatcher . Changed += OnFileChanged ;
156
- _fileWatcher . Created += OnFileCreated ;
157
- _fileWatcher . Deleted += OnFileDeleted ;
158
284
_fileWatcher . Renamed += OnFileRenamed ;
159
285
160
286
System . Diagnostics . Debug . WriteLine ( $ "File watcher setup for: { agentsDirectory } ") ;
@@ -175,24 +301,6 @@ private void OnFileChanged(object sender, FileSystemEventArgs e)
175
301
} , TaskScheduler . FromCurrentSynchronizationContext ( ) ) ;
176
302
}
177
303
178
- private void OnFileCreated ( object sender , FileSystemEventArgs e )
179
- {
180
- System . Diagnostics . Debug . WriteLine ( $ "File created: { e . Name } ") ;
181
- Task . Delay ( 500 ) . ContinueWith ( _ =>
182
- {
183
- LoadAgents ( ) ;
184
- } , TaskScheduler . FromCurrentSynchronizationContext ( ) ) ;
185
- }
186
-
187
- private void OnFileDeleted ( object sender , FileSystemEventArgs e )
188
- {
189
- System . Diagnostics . Debug . WriteLine ( $ "File deleted: { e . Name } ") ;
190
- Task . Delay ( 500 ) . ContinueWith ( _ =>
191
- {
192
- LoadAgents ( ) ;
193
- } , TaskScheduler . FromCurrentSynchronizationContext ( ) ) ;
194
- }
195
-
196
304
private void OnFileRenamed ( object sender , RenamedEventArgs e )
197
305
{
198
306
System . Diagnostics . Debug . WriteLine ( $ "File renamed: { e . OldName } -> { e . Name } ") ;
@@ -207,8 +315,6 @@ public void Dispose()
207
315
if ( _fileWatcher != null )
208
316
{
209
317
_fileWatcher . Changed -= OnFileChanged ;
210
- _fileWatcher . Created -= OnFileCreated ;
211
- _fileWatcher . Deleted -= OnFileDeleted ;
212
318
_fileWatcher . Renamed -= OnFileRenamed ;
213
319
_fileWatcher . Dispose ( ) ;
214
320
_fileWatcher = null ;
0 commit comments