-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathClaudeCodeControl.Settings.cs
More file actions
298 lines (259 loc) · 10.3 KB
/
ClaudeCodeControl.Settings.cs
File metadata and controls
298 lines (259 loc) · 10.3 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
/* *******************************************************************************************************************
* 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: Settings management for Claude Code extension
*
* *******************************************************************************************************************/
using System;
using System.IO;
using System.Diagnostics;
using System.Windows;
using Newtonsoft.Json;
namespace ClaudeCodeVS
{
public partial class ClaudeCodeControl
{
#region Settings Fields
/// <summary>
/// Configuration file name
/// </summary>
private const string ConfigurationFileName = "claudecode-settings.json";
/// <summary>
/// Full path to the configuration file
/// </summary>
private static readonly string ConfigurationPath = Path.Combine(
Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData),
"ClaudeCodeExtension",
ConfigurationFileName);
/// <summary>
/// Current settings instance
/// </summary>
private ClaudeCodeSettings _settings;
/// <summary>
/// Flag to prevent saving settings during initialization
/// </summary>
private bool _isInitializing = true;
#endregion
#region Settings Management
/// <summary>
/// Loads settings from the configuration file
/// </summary>
private void LoadSettings()
{
try
{
if (File.Exists(ConfigurationPath))
{
var json = File.ReadAllText(ConfigurationPath);
_settings = JsonConvert.DeserializeObject<ClaudeCodeSettings>(json) ?? new ClaudeCodeSettings();
}
else
{
_settings = new ClaudeCodeSettings();
// Save the default settings to create the file
SaveDefaultSettings();
}
// Apply loaded settings to UI
SendWithEnterCheckBox.IsChecked = _settings.SendWithEnter;
if (_settings.SplitterPosition > 0)
{
SetSplitterPosition(_settings.SplitterPosition);
// Re-apply after first layout pass completes — during Loaded the
// control may not have its final size yet, so the pixel value can
// be overridden by WPF layout recalculation
double savedPos = _settings.SplitterPosition;
#pragma warning disable VSTHRD001, VSTHRD110
_ = Dispatcher.BeginInvoke(
System.Windows.Threading.DispatcherPriority.Loaded,
new Action(() => SetSplitterPosition(savedPos)));
#pragma warning restore VSTHRD001, VSTHRD110
}
// Only apply if user has explicitly set a font size (0 = use VS default)
if (_settings.PromptFontSize >= 8)
{
PromptTextBox.FontSize = _settings.PromptFontSize;
}
}
catch (Exception ex)
{
Debug.WriteLine($"Error loading settings: {ex.Message}");
_settings = new ClaudeCodeSettings();
}
}
/// <summary>
/// Saves default settings to create the initial configuration file
/// </summary>
private void SaveDefaultSettings()
{
try
{
var directory = Path.GetDirectoryName(ConfigurationPath);
if (!Directory.Exists(directory))
{
Directory.CreateDirectory(directory);
}
var json = JsonConvert.SerializeObject(_settings, Formatting.Indented);
File.WriteAllText(ConfigurationPath, json);
}
catch (Exception ex)
{
Debug.WriteLine($"Error saving default settings: {ex.Message}");
}
}
/// <summary>
/// Saves current settings to the configuration file
/// </summary>
private void SaveSettings()
{
try
{
// Don't save settings during initialization to prevent overwriting with default values
if (_isInitializing)
{
return;
}
if (_settings == null)
_settings = new ClaudeCodeSettings();
// Update settings from UI
_settings.SendWithEnter = SendWithEnterCheckBox.IsChecked == true;
// Only update splitter position if we can get a valid value (not 0.0)
// Skip when terminal is detached because the grid layout is collapsed
// and FindSplitterPosition would return the full control height
if (!_isTerminalDetached)
{
var splitterPosition = FindSplitterPosition();
if (splitterPosition.HasValue && splitterPosition.Value > 0)
{
_settings.SplitterPosition = splitterPosition.Value;
}
}
// Save to file
var directory = Path.GetDirectoryName(ConfigurationPath);
if (!Directory.Exists(directory))
{
Directory.CreateDirectory(directory);
}
var json = JsonConvert.SerializeObject(_settings, Formatting.Indented);
File.WriteAllText(ConfigurationPath, json);
}
catch (Exception ex)
{
Debug.WriteLine($"Error saving settings: {ex.Message}");
}
}
#endregion
#region Splitter Position Management
/// <summary>
/// Finds the current splitter position in pixels
/// </summary>
/// <returns>The pixel height of the top row, or null if unable to determine</returns>
private double? FindSplitterPosition()
{
try
{
var grid = MainGrid;
if (grid?.RowDefinitions?.Count >= 3 && this.ActualHeight > 0)
{
// ActualHeight is always the real rendered pixel height,
// regardless of whether the row uses Star or Pixel GridLength
double topHeight = grid.RowDefinitions[0].ActualHeight;
if (topHeight > 0)
{
return topHeight;
}
}
return null;
}
catch (Exception ex)
{
Debug.WriteLine($"Error finding splitter position: {ex.Message}");
return null;
}
}
/// <summary>
/// Sets the splitter position to the specified pixel height
/// </summary>
/// <param name="position">The desired pixel height for the top row</param>
private void SetSplitterPosition(double position)
{
try
{
var grid = MainGrid;
if (grid?.RowDefinitions?.Count >= 3 && position > 0)
{
// Set absolute height for the top row
grid.RowDefinitions[0].Height = new GridLength(position, GridUnitType.Pixel);
// Keep the bottom row as star to fill remaining space
grid.RowDefinitions[2].Height = new GridLength(1, GridUnitType.Star);
}
}
catch (Exception ex)
{
Debug.WriteLine($"Error setting splitter position: {ex.Message}");
}
}
/// <summary>
/// Saves the splitter position after WPF has applied the final layout.
/// This is required because GridSplitter uses a preview adorner while dragging,
/// so the row ActualHeight can still be stale inside DragCompleted.
/// </summary>
private void SaveSplitterPositionAfterLayout()
{
#pragma warning disable VSTHRD001, VSTHRD110
_ = Dispatcher.BeginInvoke(
System.Windows.Threading.DispatcherPriority.Loaded,
new Action(() =>
{
MainGrid?.UpdateLayout();
var splitterPosition = FindSplitterPosition();
if (splitterPosition.HasValue && splitterPosition.Value > 0)
{
if (_settings == null)
{
_settings = new ClaudeCodeSettings();
}
_settings.SplitterPosition = splitterPosition.Value;
}
SaveSettings();
}));
#pragma warning restore VSTHRD001, VSTHRD110
}
/// <summary>
/// Handles splitter drag completed event to save the new position
/// </summary>
private void MainGridSplitter_DragCompleted(object sender, System.Windows.Controls.Primitives.DragCompletedEventArgs e)
{
SaveSplitterPositionAfterLayout();
}
#endregion
#region Settings Application
/// <summary>
/// Applies loaded settings to the UI elements
/// </summary>
private void ApplyLoadedSettings()
{
Microsoft.VisualStudio.Shell.ThreadHelper.ThrowIfNotOnUIThread();
// Ensure the send button visibility matches the checkbox state
if (SendWithEnterCheckBox.IsChecked == true)
{
SendPromptButton.Visibility = Visibility.Collapsed;
}
else
{
SendPromptButton.Visibility = Visibility.Visible;
}
// Update provider selection and title
UpdateProviderSelection();
// Update model selection
UpdateModelSelection();
// Update effort selection
UpdateEffortSelection();
}
#endregion
}
}