-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathClaudeCodeControl.Detach.cs
More file actions
482 lines (412 loc) · 17.9 KB
/
ClaudeCodeControl.Detach.cs
File metadata and controls
482 lines (412 loc) · 17.9 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
/* *******************************************************************************************************************
* Application: ClaudeCodeExtension
*
* Autor: Daniel Carvalho Liedke
*
* Copyright © Daniel Carvalho Liedke 2026
* Usage and reproduction in any manner whatsoever without the written permission of Daniel Carvalho Liedke is strictly forbidden.
*
* Purpose: Terminal detach/attach logic - allows detaching the terminal to a separate VS tool window tab
*
* *******************************************************************************************************************/
using System;
using System.Diagnostics;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Shapes;
using Microsoft.VisualStudio.Shell;
using Microsoft.VisualStudio.Shell.Interop;
namespace ClaudeCodeVS
{
public partial class ClaudeCodeControl
{
#region Detach Fields
/// <summary>
/// Reference to the detached terminal tool window
/// </summary>
private DetachedTerminalToolWindow _detachedTerminalWindow;
/// <summary>
/// The WinForms panel inside the detached tool window
/// </summary>
private System.Windows.Forms.Panel _detachedTerminalPanel;
/// <summary>
/// Whether the terminal is currently detached
/// </summary>
private bool _isTerminalDetached;
/// <summary>
/// Guard flag to prevent double-subscribing to Closed event
/// </summary>
private bool _detachedClosedSubscribed;
/// <summary>
/// Guard flag to prevent double-subscribing to VisibilityChanged event
/// </summary>
private bool _detachedVisibilitySubscribed;
#endregion
#region Active Panel Property
/// <summary>
/// Returns the currently active terminal panel (detached or main)
/// </summary>
private System.Windows.Forms.Panel ActiveTerminalPanel
{
get
{
if (_isTerminalDetached && _detachedTerminalPanel != null)
{
return _detachedTerminalPanel;
}
return terminalPanel;
}
}
#endregion
#region Detach/Attach Methods
/// <summary>
/// Detaches the terminal from the main control into a separate VS tool window tab
/// </summary>
private async System.Threading.Tasks.Task DetachTerminalAsync()
{
await ThreadHelper.JoinableTaskFactory.SwitchToMainThreadAsync();
if (terminalHandle == IntPtr.Zero || !IsWindow(terminalHandle))
{
return;
}
try
{
// Get the VS package to find/create the tool window
var package = await GetPackageAsync();
if (package == null)
{
Debug.WriteLine("DetachTerminalAsync: Could not get package");
return;
}
// Find or create the detached terminal tool window
_detachedTerminalWindow = package.FindToolWindow(typeof(DetachedTerminalToolWindow), 0, true) as DetachedTerminalToolWindow;
if (_detachedTerminalWindow == null)
{
Debug.WriteLine("DetachTerminalAsync: Could not create detached terminal tool window");
return;
}
_detachedTerminalPanel = _detachedTerminalWindow.TerminalPanel;
// Set background color to match current theme
var bgColor = GetTerminalBackgroundColor();
_detachedTerminalPanel.BackColor = bgColor;
// Subscribe to Closed event (with guard)
if (!_detachedClosedSubscribed)
{
_detachedTerminalWindow.Closed += OnDetachedWindowClosed;
_detachedClosedSubscribed = true;
}
// Subscribe to VisibilityChanged event (with guard)
if (!_detachedVisibilitySubscribed)
{
_detachedTerminalWindow.VisibilityChanged += OnDetachedVisibilityChanged;
_detachedVisibilitySubscribed = true;
}
// Wire resize event to keep terminal fitting the panel
_detachedTerminalPanel.Resize += DetachedPanel_Resize;
// Show the tool window FIRST so the panel gets its dimensions
var frame = _detachedTerminalWindow.Frame as IVsWindowFrame;
frame?.Show();
// Update caption with current provider name
string providerName = GetCurrentProviderName();
_detachedTerminalWindow.UpdateCaption(providerName);
// Wait for the tool window to be fully laid out before re-parenting
await System.Threading.Tasks.Task.Delay(300);
// Set detached flag BEFORE re-parent so ActiveTerminalPanel returns the correct panel
_isTerminalDetached = true;
// Now re-parent the terminal handle to the detached panel (panel has dimensions now)
SetParent(terminalHandle, _detachedTerminalPanel.Handle);
ShowWindow(terminalHandle, SW_SHOW);
ResizeEmbeddedTerminal();
// Retry resize after short delays to ensure terminal fills the panel
await System.Threading.Tasks.Task.Delay(200);
ResizeEmbeddedTerminal();
await System.Threading.Tasks.Task.Delay(300);
ResizeEmbeddedTerminal();
// Save current splitter position and expand prompt area slightly
if (_settings != null)
{
var currentPos = FindSplitterPosition();
if (currentPos.HasValue && currentPos.Value > 0)
{
_settings.SplitterPosition = currentPos.Value;
// Expand prompt area by 80px for more comfortable editing while detached
SetSplitterPosition(currentPos.Value + 80);
}
}
// Hide terminal area (terminal moved to separate tab)
// Keep splitter visible so user can resize the prompt area freely
TerminalGroupBox.Visibility = Visibility.Collapsed;
MainGrid.RowDefinitions[2].MinHeight = 0;
MainGrid.UpdateLayout();
// Update button icon and tooltip to show "attach" (arrow pointing right into box)
UpdateDetachButtonIcon(true);
DetachTerminalButton.ToolTip = "Attach Terminal Back to Main Panel";
// Save state
if (_settings != null)
{
_settings.IsTerminalDetached = true;
SaveSettings();
}
}
catch (Exception ex)
{
Debug.WriteLine($"Error detaching terminal: {ex.Message}");
}
}
/// <summary>
/// Attaches the terminal back from the detached tool window to the main control
/// </summary>
/// <param name="skipCloseFrame">If true, does not close the detached window frame (used when window is already closing)</param>
private async System.Threading.Tasks.Task AttachTerminalAsync(bool skipCloseFrame = false)
{
await ThreadHelper.JoinableTaskFactory.SwitchToMainThreadAsync();
// Guard against double-fire (Closed event can fire from multiple paths)
if (!_isTerminalDetached)
{
return;
}
try
{
// Mark as not detached immediately to prevent re-entry
_isTerminalDetached = false;
// Restore main terminal area
TerminalGroupBox.Visibility = Visibility.Visible;
MainGrid.RowDefinitions[2].MinHeight = 150;
// Restore splitter to pre-detach position
if (_settings != null && _settings.SplitterPosition > 0)
{
SetSplitterPosition(_settings.SplitterPosition);
}
// Force layout recalculation
MainGrid.UpdateLayout();
// Re-parent the terminal handle back to the main panel
if (terminalHandle != IntPtr.Zero && IsWindow(terminalHandle) && terminalPanel != null)
{
SetParent(terminalHandle, terminalPanel.Handle);
ShowWindow(terminalHandle, SW_SHOW);
ResizeEmbeddedTerminal();
}
// Retry resize after short delays to ensure terminal fills the panel
// (WPF-to-WinForms layout propagation may not be immediate)
await System.Threading.Tasks.Task.Delay(200);
ResizeEmbeddedTerminal();
await System.Threading.Tasks.Task.Delay(300);
ResizeEmbeddedTerminal();
// Unwire events from detached window
if (_detachedTerminalWindow != null)
{
if (_detachedClosedSubscribed)
{
_detachedTerminalWindow.Closed -= OnDetachedWindowClosed;
_detachedClosedSubscribed = false;
}
if (_detachedVisibilitySubscribed)
{
_detachedTerminalWindow.VisibilityChanged -= OnDetachedVisibilityChanged;
_detachedVisibilitySubscribed = false;
}
}
// Unwire resize from detached panel
if (_detachedTerminalPanel != null)
{
_detachedTerminalPanel.Resize -= DetachedPanel_Resize;
}
// Close detached window frame if needed
if (!skipCloseFrame && _detachedTerminalWindow?.Frame is IVsWindowFrame windowFrame)
{
windowFrame.CloseFrame((uint)__FRAMECLOSE.FRAMECLOSE_NoSave);
}
// Clear references
_detachedTerminalPanel = null;
_detachedTerminalWindow = null;
// Update button icon and tooltip to show "detach" (arrow pointing left out of box)
UpdateDetachButtonIcon(false);
DetachTerminalButton.ToolTip = "Detach Terminal to Separate Tab";
// Save state
if (_settings != null)
{
_settings.IsTerminalDetached = false;
SaveSettings();
}
}
catch (Exception ex)
{
Debug.WriteLine($"Error attaching terminal: {ex.Message}");
}
}
/// <summary>
/// Toggles between detached and attached terminal states
/// </summary>
private async System.Threading.Tasks.Task ToggleDetachAsync()
{
if (_isTerminalDetached)
{
await AttachTerminalAsync();
}
else
{
await DetachTerminalAsync();
}
}
#endregion
#region Event Handlers
/// <summary>
/// Handles the main tool window frame show notifications (tab activated, shown, etc.).
/// This is more reliable than WPF IsVisibleChanged because it fires on every VS
/// tab activation, even when WPF considers the control already visible.
/// </summary>
private void OnToolWindowFrameShow(object sender, int fShow)
{
ThreadHelper.ThrowIfNotOnUIThread();
var frameShow = (__FRAMESHOW)fShow;
bool activated = frameShow == __FRAMESHOW.FRAMESHOW_WinShown ||
frameShow == __FRAMESHOW.FRAMESHOW_WinRestored ||
frameShow == __FRAMESHOW.FRAMESHOW_WinMaximized ||
frameShow == __FRAMESHOW.FRAMESHOW_TabActivated;
if (activated && _isTerminalDetached)
{
if (_detachedTerminalWindow?.Frame is IVsWindowFrame detachedFrame)
{
detachedFrame.Show();
}
if (terminalHandle != IntPtr.Zero && IsWindow(terminalHandle))
{
ShowWindow(terminalHandle, SW_SHOW);
ResizeEmbeddedTerminal();
}
}
}
/// <summary>
/// Handles the detached window being closed by the user - re-attaches the terminal
/// </summary>
private void OnDetachedWindowClosed(object sender, EventArgs e)
{
#pragma warning disable VSSDK007, VSTHRD110
_ = ThreadHelper.JoinableTaskFactory.RunAsync(async () =>
{
await ThreadHelper.JoinableTaskFactory.SwitchToMainThreadAsync();
await AttachTerminalAsync(skipCloseFrame: true);
});
#pragma warning restore VSSDK007, VSTHRD110
}
/// <summary>
/// Handles visibility changes of the detached terminal window
/// </summary>
private void OnDetachedVisibilityChanged(object sender, bool isVisible)
{
if (isVisible && terminalHandle != IntPtr.Zero && IsWindow(terminalHandle))
{
ShowWindow(terminalHandle, SW_SHOW);
ResizeEmbeddedTerminal();
}
}
/// <summary>
/// Handles resize of the detached panel to keep terminal fitting
/// </summary>
private void DetachedPanel_Resize(object sender, EventArgs e)
{
ResizeEmbeddedTerminal();
}
/// <summary>
/// Handles the detach button click
/// </summary>
private void DetachTerminalButton_Click(object sender, RoutedEventArgs e)
{
#pragma warning disable VSSDK007, VSTHRD110
_ = ThreadHelper.JoinableTaskFactory.RunAsync(async () =>
{
await ThreadHelper.JoinableTaskFactory.SwitchToMainThreadAsync();
await ToggleDetachAsync();
});
#pragma warning restore VSSDK007, VSTHRD110
}
#endregion
#region Helper Methods
/// <summary>
/// Updates the detach button icon: arrow left (detach) or arrow right (attach)
/// </summary>
/// <param name="isDetached">True = show attach icon (arrow right into box), False = show detach icon (arrow left out of box)</param>
private void UpdateDetachButtonIcon(bool isDetached)
{
try
{
var canvas = new Canvas { Width = 16, Height = 14 };
var rect = new Rectangle
{
Width = 10,
Height = 12,
Stroke = Brushes.Gray,
StrokeThickness = 1.5,
Fill = Brushes.Transparent
};
if (isDetached)
{
// Attach icon: box on left, arrow pointing right into box
Canvas.SetLeft(rect, 1);
Canvas.SetTop(rect, 1);
canvas.Children.Add(rect);
var line = new Line { X1 = 8, Y1 = 7, X2 = 15, Y2 = 7, Stroke = Brushes.Gray, StrokeThickness = 1.5 };
canvas.Children.Add(line);
var arrow = new Polyline
{
Points = new PointCollection { new Point(12, 4), new Point(15, 7), new Point(12, 10) },
Stroke = Brushes.Gray,
StrokeThickness = 1.5,
Fill = Brushes.Transparent
};
canvas.Children.Add(arrow);
}
else
{
// Detach icon: box on right, arrow pointing left out of box
Canvas.SetLeft(rect, 5);
Canvas.SetTop(rect, 1);
canvas.Children.Add(rect);
var line = new Line { X1 = 8, Y1 = 7, X2 = 1, Y2 = 7, Stroke = Brushes.Gray, StrokeThickness = 1.5 };
canvas.Children.Add(line);
var arrow = new Polyline
{
Points = new PointCollection { new Point(4, 4), new Point(1, 7), new Point(4, 10) },
Stroke = Brushes.Gray,
StrokeThickness = 1.5,
Fill = Brushes.Transparent
};
canvas.Children.Add(arrow);
}
DetachButtonIcon.Child = canvas;
}
catch (Exception ex)
{
Debug.WriteLine($"Error updating detach button icon: {ex.Message}");
}
}
/// <summary>
/// Gets the display name of the currently selected provider
/// </summary>
private string GetCurrentProviderName()
{
if (_settings == null) return "Claude Code";
switch (_settings.SelectedProvider)
{
case AiProvider.ClaudeCode:
case AiProvider.ClaudeCodeWSL:
return "Claude Code";
case AiProvider.CodexNative:
case AiProvider.Codex:
return "Codex";
case AiProvider.CursorAgentNative:
case AiProvider.CursorAgent:
return "Cursor Agent";
case AiProvider.QwenCode:
return "Qwen Code";
case AiProvider.OpenCode:
return "Open Code";
default:
return "Claude Code";
}
}
#endregion
}
}