-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSettingsWindow.xaml.cs
More file actions
199 lines (168 loc) · 7.46 KB
/
Copy pathSettingsWindow.xaml.cs
File metadata and controls
199 lines (168 loc) · 7.46 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
using System.Collections.Generic;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Input;
namespace MiniMacro
{
public partial class SettingsWindow : Window
{
private Button? _capturingButton;
private TextBlock? _capturingText;
private string _capturingAction = "";
private readonly Dictionary<Button, string> _buttonActions;
public SettingsWindow()
{
InitializeComponent();
_buttonActions = new Dictionary<Button, string>
{
{ HkRecord, "Record" },
{ HkPlay, "Play" },
{ HkPause, "Pause" },
{ HkStop, "Stop" }
};
LoadSettings();
}
private void TitleBar_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
{
if (e.ButtonState == MouseButtonState.Pressed)
DragMove();
}
private void Close_Click(object sender, RoutedEventArgs e)
{
CancelCapture();
Close();
}
// ── Загрузка настроек в UI ───────────────────────────────────────────
private void LoadSettings()
{
var s = SettingsManager.Current;
SetHotkeyText(HkRecordText, s.HotkeyRecord);
SetHotkeyText(HkPlayText, s.HotkeyPlay);
SetHotkeyText(HkPauseText, s.HotkeyPause);
SetHotkeyText(HkStopText, s.HotkeyStop);
UpdateThemeButtons();
}
private static void SetHotkeyText(TextBlock tb, string hotkey) =>
tb.Text = string.IsNullOrEmpty(hotkey) ? "—" : hotkey;
// ── Тема оформления ──────────────────────────────────────────────────
private void UpdateThemeButtons()
{
var active = (Style)FindResource("ThemeButtonActiveStyle");
var inactive = (Style)FindResource("ThemeButtonStyle");
var theme = SettingsManager.Current.Theme;
DarkButton.Style = theme == "Dark" ? active : inactive;
LightButton.Style = theme == "Light" ? active : inactive;
SystemButton.Style = theme == "System" ? active : inactive;
}
private void DarkTheme_Click(object sender, RoutedEventArgs e)
{
SettingsManager.Current.Theme = "Dark";
SettingsManager.Save();
ThemeManager.Apply("Dark");
UpdateThemeButtons();
}
private void LightTheme_Click(object sender, RoutedEventArgs e)
{
SettingsManager.Current.Theme = "Light";
SettingsManager.Save();
ThemeManager.Apply("Light");
UpdateThemeButtons();
}
private void SystemTheme_Click(object sender, RoutedEventArgs e)
{
SettingsManager.Current.Theme = "System";
SettingsManager.Save();
ThemeManager.Apply("System");
UpdateThemeButtons();
}
// ── Захват горячей клавиши ───────────────────────────────────────────
private void HotkeyButton_Click(object sender, RoutedEventArgs e)
{
CancelCapture();
_capturingButton = (Button)sender;
_capturingText = (TextBlock)_capturingButton.Content;
_capturingAction = _buttonActions[_capturingButton];
_capturingButton.Tag = "capturing";
_capturingText.Text = "Нажмите клавишу...";
}
private void Window_PreviewKeyDown(object sender, KeyEventArgs e)
{
if (_capturingButton == null) return;
e.Handled = true;
if (e.Key == Key.Escape)
{
// Очищаем — сохраняем пустую строку
_capturingText!.Text = "—";
SaveHotkey(_capturingAction, "");
}
else
{
// Определяем основную клавишу (игнорируем одиночные модификаторы)
var mainKey = e.Key == Key.System ? e.SystemKey : e.Key;
if (mainKey == Key.LeftCtrl || mainKey == Key.RightCtrl ||
mainKey == Key.LeftAlt || mainKey == Key.RightAlt ||
mainKey == Key.LeftShift || mainKey == Key.RightShift ||
mainKey == Key.LWin || mainKey == Key.RWin)
return;
// Собираем сочетание
var parts = new System.Collections.Generic.List<string>();
if ((Keyboard.Modifiers & ModifierKeys.Control) != 0) parts.Add("Ctrl");
if ((Keyboard.Modifiers & ModifierKeys.Alt) != 0) parts.Add("Alt");
if ((Keyboard.Modifiers & ModifierKeys.Shift) != 0) parts.Add("Shift");
if ((Keyboard.Modifiers & ModifierKeys.Windows) != 0) parts.Add("Win");
parts.Add(mainKey.ToString());
var hotkeyStr = string.Join("+", parts);
_capturingText!.Text = hotkeyStr;
SaveHotkey(_capturingAction, hotkeyStr);
}
_capturingButton.Tag = null;
_capturingButton = null;
}
private static void SaveHotkey(string action, string hotkey)
{
var s = SettingsManager.Current;
switch (action)
{
case "Record": s.HotkeyRecord = hotkey; break;
case "Play": s.HotkeyPlay = hotkey; break;
case "Pause": s.HotkeyPause = hotkey; break;
case "Stop": s.HotkeyStop = hotkey; break;
}
}
private void CancelCapture()
{
if (_capturingButton == null) return;
_capturingButton.Tag = null;
_capturingText!.Text = string.IsNullOrEmpty(GetCurrentHotkey(_capturingAction))
? "—" : GetCurrentHotkey(_capturingAction);
_capturingButton = null;
}
private static string GetCurrentHotkey(string action) => action switch
{
"Record" => SettingsManager.Current.HotkeyRecord,
"Play" => SettingsManager.Current.HotkeyPlay,
"Pause" => SettingsManager.Current.HotkeyPause,
"Stop" => SettingsManager.Current.HotkeyStop,
_ => ""
};
// ── Apply / Reset ────────────────────────────────────────────────────
private void Apply_Click(object sender, RoutedEventArgs e)
{
CancelCapture();
SettingsManager.Save();
HotkeyManager.UnregisterAll();
HotkeyManager.RegisterAll();
Close();
}
private void Reset_Click(object sender, RoutedEventArgs e)
{
CancelCapture();
SettingsManager.Reset();
SettingsManager.Save();
ThemeManager.Apply(SettingsManager.Current.Theme);
LoadSettings();
HotkeyManager.UnregisterAll();
HotkeyManager.RegisterAll();
}
}
}