-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSettingsWindow.xaml.cs
More file actions
337 lines (297 loc) · 15.7 KB
/
Copy pathSettingsWindow.xaml.cs
File metadata and controls
337 lines (297 loc) · 15.7 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
using System;
using System.Linq;
using System.Windows;
using System.Windows.Automation;
using System.Windows.Controls;
using System.Windows.Media;
using Caelum.Models;
using Caelum.Services;
namespace Caelum
{
public partial class SettingsWindow : Window
{
private readonly AppSettings _originalSettings;
private bool _isApplyingLocalization;
private static readonly string[] WorkspaceBackdropValues =
{
"Neutral", "Paper", "Mist", "Warm", "Slate", "Midnight"
};
public SettingsWindow(AppSettings currentSettings)
{
_originalSettings = CloneSettings(currentSettings);
InitializeComponent();
MouseLeftButtonDown += (sender, args) => DragMove();
LocalizationService.LanguageChanged += LocalizationService_LanguageChanged;
Closed += (_, __) => LocalizationService.LanguageChanged -= LocalizationService_LanguageChanged;
// Dropdown popup must not float above other applications after Alt-Tab (Task 10)
PopupZOrderHelper.FixComboBoxPopupTopmost(LanguageComboBox);
PopupZOrderHelper.FixComboBoxPopupTopmost(AutoSaveIntervalComboBox);
PopupZOrderHelper.FixComboBoxPopupTopmost(SmoothingComboBox);
PopupZOrderHelper.FixComboBoxPopupTopmost(PerformanceModeComboBox);
PopupZOrderHelper.FixComboBoxPopupTopmost(ThemeComboBox);
PopupZOrderHelper.FixComboBoxPopupTopmost(WorkspaceBackdropComboBox);
LanguageComboBox.SelectionChanged += LanguageComboBox_SelectionChanged;
LanguageComboBox.ItemsSource = LocalizationService.GetLanguageOptions();
LanguageComboBox.SelectedValue = currentSettings.Language;
AutoSaveIntervalComboBox.ItemsSource = new[] { 15, 30, 60, 120 };
AutoSaveIntervalComboBox.SelectedItem = currentSettings.AutoSaveIntervalSeconds;
PressureCheckBox.IsChecked = currentSettings.EnablePressure;
PenOnlyCheckBox.IsChecked = currentSettings.PenOnlyMode;
var smoothingIndex = Math.Max(0, Math.Min(3, currentSettings.StrokeSmoothing));
var themeIndex = GetThemeIndex(currentSettings.Theme);
var performanceModeIndex = GetPerformanceModeIndex(currentSettings.PerformanceMode);
var workspaceBackdropIndex = GetWorkspaceBackdropIndex(currentSettings.WorkspaceBackdrop);
AutoSaveIntervalComboBox.SelectionChanged += SettingsControl_SelectionChanged;
PressureCheckBox.Checked += SettingsControl_Changed;
PressureCheckBox.Unchecked += SettingsControl_Changed;
PenOnlyCheckBox.Checked += SettingsControl_Changed;
PenOnlyCheckBox.Unchecked += SettingsControl_Changed;
SmoothingComboBox.SelectionChanged += SettingsControl_SelectionChanged;
PerformanceModeComboBox.SelectionChanged += SettingsControl_SelectionChanged;
ThemeComboBox.SelectionChanged += SettingsControl_SelectionChanged;
WorkspaceBackdropComboBox.SelectionChanged += SettingsControl_SelectionChanged;
ApplyLocalization();
SmoothingComboBox.SelectedIndex = smoothingIndex;
PerformanceModeComboBox.SelectedIndex = performanceModeIndex;
ThemeComboBox.SelectedIndex = themeIndex;
WorkspaceBackdropComboBox.SelectedIndex = workspaceBackdropIndex;
}
public AppSettings SelectedSettings { get; private set; }
public void ApplyLocalization()
{
var smoothingIndex = SmoothingComboBox.SelectedIndex < 0 ? 2 : SmoothingComboBox.SelectedIndex;
var performanceModeIndex = PerformanceModeComboBox.SelectedIndex < 0 ? 1 : PerformanceModeComboBox.SelectedIndex;
var themeIndex = ThemeComboBox.SelectedIndex < 0 ? 0 : ThemeComboBox.SelectedIndex;
var workspaceBackdropIndex = WorkspaceBackdropComboBox.SelectedIndex < 0 ? 0 : WorkspaceBackdropComboBox.SelectedIndex;
_isApplyingLocalization = true;
try
{
TitleTextBlock.Text = LocalizationService.Get("Settings.Title");
SubtitleTextBlock.Text = LocalizationService.Get("Settings.Subtitle");
LanguageLabelTextBlock.Text = LocalizationService.Get("Settings.LanguageLabel");
LanguageHintTextBlock.Text = LocalizationService.Get("Settings.LanguageHint");
UtilityLabelTextBlock.Text = LocalizationService.Get("Settings.UtilityLabel");
UtilityHintTextBlock.Text = LocalizationService.Get("Settings.UtilityHint");
AutoSaveIntervalLabelTextBlock.Text = LocalizationService.Get("Settings.AutoSaveInterval");
PressureLabelTextBlock.Text = LocalizationService.Get("Settings.Pressure");
PressureCheckBox.Content = LocalizationService.Get("Settings.Enabled");
PenOnlyLabelTextBlock.Text = LocalizationService.Get("Settings.PenOnly");
PenOnlyCheckBox.Content = LocalizationService.Get("Settings.Enabled");
SmoothingLabelTextBlock.Text = LocalizationService.Get("Settings.Smoothing");
PerformanceModeLabelTextBlock.Text = LocalizationService.Get("Settings.Performance");
ThemeLabelTextBlock.Text = LocalizationService.Get("Settings.Theme");
CancelButton.Content = LocalizationService.Get("Common.Cancel");
SaveButton.Content = LocalizationService.Get("Common.Save");
Title = LocalizationService.Get("Settings.Title");
SmoothingComboBox.ItemsSource = new[]
{
LocalizationService.Get("Editor.SmoothingOff"),
LocalizationService.Get("Editor.SmoothingLow"),
LocalizationService.Get("Editor.SmoothingMid"),
LocalizationService.Get("Editor.SmoothingHigh")
};
SmoothingComboBox.SelectedIndex = Math.Max(0, Math.Min(3, smoothingIndex));
PerformanceModeComboBox.ItemsSource = new[]
{
LocalizationService.Get("Settings.PerformanceBatterySaver"),
LocalizationService.Get("Settings.PerformanceBalanced"),
LocalizationService.Get("Settings.PerformanceBestQuality")
};
PerformanceModeComboBox.SelectedIndex = Math.Max(0, Math.Min(2, performanceModeIndex));
ThemeComboBox.ItemsSource = new[]
{
LocalizationService.Get("Settings.ThemeLight"),
LocalizationService.Get("Settings.ThemeDark"),
LocalizationService.Get("Settings.ThemeSystem"),
LocalizationService.Get("Settings.ThemeHighContrast")
};
ThemeComboBox.SelectedIndex = Math.Max(0, Math.Min(3, themeIndex));
WorkspaceBackdropLabelTextBlock.Text = LocalizationService.Get("Settings.WorkspaceBackdrop");
WorkspaceBackdropHintTextBlock.Text = LocalizationService.Get("Settings.WorkspaceBackdropHint");
AutomationProperties.SetName(WorkspaceBackdropComboBox, WorkspaceBackdropLabelTextBlock.Text);
AutomationProperties.SetHelpText(WorkspaceBackdropComboBox, WorkspaceBackdropHintTextBlock.Text);
WorkspaceBackdropComboBox.ItemsSource = CreateWorkspaceBackdropOptions();
WorkspaceBackdropComboBox.SelectedIndex = Math.Max(0, Math.Min(WorkspaceBackdropValues.Length - 1, workspaceBackdropIndex));
}
finally
{
_isApplyingLocalization = false;
}
}
public AppSettings GetSelectedSettings()
{
var selectedLanguage = LanguageComboBox.SelectedValue is AppLanguage language
? language
: AppLanguage.English;
int autoSaveInterval = AutoSaveIntervalComboBox.SelectedItem is int interval ? interval : 60;
int smoothing = Math.Max(0, Math.Min(3, SmoothingComboBox.SelectedIndex < 0 ? 2 : SmoothingComboBox.SelectedIndex));
var selected = CloneSettings(_originalSettings);
selected.Language = selectedLanguage;
selected.AutoSaveIntervalSeconds = autoSaveInterval;
selected.EnablePressure = PressureCheckBox.IsChecked != false;
selected.PenOnlyMode = PenOnlyCheckBox.IsChecked == true;
selected.StrokeSmoothing = smoothing;
selected.PerformanceMode = GetPerformanceModeValue(PerformanceModeComboBox.SelectedIndex);
selected.Theme = GetThemeValue(ThemeComboBox.SelectedIndex);
selected.WorkspaceBackdrop = GetWorkspaceBackdropValue(WorkspaceBackdropComboBox.SelectedIndex);
return selected;
}
private static int GetWorkspaceBackdropIndex(string value)
{
string normalized = ThemeService.NormalizeWorkspaceBackdrop(value);
int index = Array.FindIndex(WorkspaceBackdropValues,
candidate => string.Equals(candidate, normalized, StringComparison.Ordinal));
return Math.Max(0, index);
}
private static string GetWorkspaceBackdropValue(int index)
{
return index >= 0 && index < WorkspaceBackdropValues.Length
? WorkspaceBackdropValues[index]
: "Neutral";
}
private static WorkspaceBackdropOption[] CreateWorkspaceBackdropOptions()
{
string[] labels =
{
LocalizationService.Get("Settings.WorkspaceBackdropNeutral"),
LocalizationService.Get("Settings.WorkspaceBackdropPaper"),
LocalizationService.Get("Settings.WorkspaceBackdropMist"),
LocalizationService.Get("Settings.WorkspaceBackdropWarm"),
LocalizationService.Get("Settings.WorkspaceBackdropSlate"),
LocalizationService.Get("Settings.WorkspaceBackdropMidnight")
};
string[] previewColors = { "#FFFFFF", "#F5F3EE", "#EAF2F6", "#F1E7DA", "#D7DEE7", "#101722" };
return WorkspaceBackdropValues.Select((value, index) => new WorkspaceBackdropOption(
value,
labels[index],
new SolidColorBrush((Color)ColorConverter.ConvertFromString(previewColors[index])))).ToArray();
}
private static int GetPerformanceModeIndex(string value)
{
return PdfRenderPolicy.NormalizeMode(value) switch
{
PdfRenderPolicy.BatterySaver => 0,
PdfRenderPolicy.BestQuality => 2,
_ => 1
};
}
private static string GetPerformanceModeValue(int index)
{
return index switch
{
0 => PdfRenderPolicy.BatterySaver,
2 => PdfRenderPolicy.BestQuality,
_ => PdfRenderPolicy.Balanced
};
}
private static int GetThemeIndex(string theme)
{
if (string.Equals(theme, "Dark", StringComparison.OrdinalIgnoreCase))
return 1;
if (string.Equals(theme, "System", StringComparison.OrdinalIgnoreCase))
return 2;
if (string.Equals(theme, "HighContrast", StringComparison.OrdinalIgnoreCase)
|| string.Equals(theme, "High Contrast", StringComparison.OrdinalIgnoreCase))
return 3;
return 0;
}
private static string GetThemeValue(int index)
{
return index switch
{
1 => "Dark",
2 => "System",
3 => "HighContrast",
_ => "Light"
};
}
private sealed class WorkspaceBackdropOption
{
public WorkspaceBackdropOption(string value, string displayName, Brush previewBrush)
{
Value = value;
DisplayName = displayName;
PreviewBrush = previewBrush;
}
public string Value { get; }
public string DisplayName { get; }
public Brush PreviewBrush { get; }
public override string ToString() => DisplayName;
}
private static AppSettings CloneSettings(AppSettings source)
{
source ??= new AppSettings();
return new AppSettings
{
Language = source.Language,
EnablePressure = source.EnablePressure,
WholeStrokeEraser = source.WholeStrokeEraser,
InkSimulation = source.InkSimulation,
ShapeRecognition = source.ShapeRecognition,
PenOnlyMode = source.PenOnlyMode,
RecentPenColors = source.RecentPenColors == null ? new System.Collections.Generic.List<string>() : new System.Collections.Generic.List<string>(source.RecentPenColors),
RecentHighlighterColors = source.RecentHighlighterColors == null ? new System.Collections.Generic.List<string>() : new System.Collections.Generic.List<string>(source.RecentHighlighterColors),
RecentTextColors = source.RecentTextColors == null ? new System.Collections.Generic.List<string>() : new System.Collections.Generic.List<string>(source.RecentTextColors),
PenPresets = source.PenPresets == null ? new System.Collections.Generic.List<PenPreset>() : source.PenPresets.Where(p => p != null).Select(p => new PenPreset { Tool = p.Tool, ColorHex = p.ColorHex, Size = p.Size }).ToList(),
StrokeSmoothing = source.StrokeSmoothing,
AutoSaveIntervalSeconds = source.AutoSaveIntervalSeconds,
DefaultPenColorHex = source.DefaultPenColorHex,
DefaultPenSize = source.DefaultPenSize,
Theme = source.Theme,
WorkspaceBackdrop = source.WorkspaceBackdrop,
PerformanceMode = source.PerformanceMode
};
}
private void LanguageComboBox_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
if (!IsLoaded)
return;
var previewSettings = GetSelectedSettings();
LocalizationService.ApplyLanguage(previewSettings.Language);
if (Owner is MainWindow mainWindow)
mainWindow.PreviewSettings(previewSettings);
}
private void LocalizationService_LanguageChanged(object sender, EventArgs e)
{
if (IsLoaded)
ApplyLocalization();
}
private void SettingsControl_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
if (_isApplyingLocalization)
return;
PreviewCurrentSettings();
}
private void SettingsControl_Changed(object sender, RoutedEventArgs e)
{
if (_isApplyingLocalization)
return;
PreviewCurrentSettings();
}
private void PreviewCurrentSettings()
{
if (!IsLoaded)
return;
var previewSettings = GetSelectedSettings();
if (Owner is MainWindow mainWindow)
mainWindow.PreviewSettings(previewSettings);
}
private void SaveButton_Click(object sender, RoutedEventArgs e)
{
SelectedSettings = GetSelectedSettings();
DialogResult = true;
}
private void CancelButton_Click(object sender, RoutedEventArgs e)
{
if (Owner is MainWindow mainWindow)
mainWindow.PreviewSettings(_originalSettings);
DialogResult = false;
}
private void CloseButton_Click(object sender, RoutedEventArgs e)
{
if (Owner is MainWindow mainWindow)
mainWindow.PreviewSettings(_originalSettings);
DialogResult = false;
}
}
}