Repository navigation
Expand file tree
/
Copy pathCustomInputMethodWindow.xaml.cs
More file actions
165 lines (148 loc) · 5.64 KB
/
Copy pathCustomInputMethodWindow.xaml.cs
File metadata and controls
165 lines (148 loc) · 5.64 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
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Windows;
using System.Windows.Controls;
using ModernKey.Config;
using ModernKey.Models;
namespace ModernKey
{
/// <summary>
/// Interaction logic for CustomInputMethodWindow.xaml
/// </summary>
public partial class CustomInputMethodWindow : Window
{
private readonly AppSettings _settings;
private readonly ObservableCollection<CustomInputRule> _rules = new ObservableCollection<CustomInputRule>();
public CustomInputMethodWindow(AppSettings settings)
{
_settings = settings ?? throw new ArgumentNullException(nameof(settings));
InitializeComponent();
InitControls();
}
private void InitControls()
{
// Nạp danh sách Presets
CmbPresets.ItemsSource = CustomInputRule.PresetNames;
CmbPresets.SelectedIndex = 0;
// Nạp danh sách Actions
CmbAction.ItemsSource = CustomInputRule.ActionDescriptions;
CmbAction.SelectedIndex = 0;
// Nạp danh sách Rules hiện tại từ Settings (sao chép độc lập để không ảnh hưởng nếu hủy)
if (_settings.CustomRules != null && _settings.CustomRules.Count > 0)
{
foreach (var rule in _settings.CustomRules)
{
_rules.Add(new CustomInputRule(rule.Key, rule.Action));
}
}
else
{
// Mặc định nạp Telex nếu chưa có quy tắc nào
var defaultPreset = CustomInputRule.GetPreset(0);
foreach (var rule in defaultPreset)
{
_rules.Add(new CustomInputRule(rule.Key, rule.Action));
}
}
DgRules.ItemsSource = _rules;
}
private void BtnLoadPreset_Click(object sender, RoutedEventArgs e)
{
int sel = CmbPresets.SelectedIndex;
if (sel >= 0)
{
var preset = CustomInputRule.GetPreset(sel);
_rules.Clear();
foreach (var rule in preset)
{
_rules.Add(new CustomInputRule(rule.Key, rule.Action));
}
TxtKey.Text = string.Empty;
if (CmbAction.Items.Count > 0) CmbAction.SelectedIndex = 0;
}
}
private void BtnAdd_Click(object sender, RoutedEventArgs e)
{
string keyText = TxtKey.Text?.Trim();
if (string.IsNullOrEmpty(keyText))
{
MessageBox.Show("Hãy nhập phím!", "ModernKey", MessageBoxButton.OK, MessageBoxImage.Warning);
TxtKey.Focus();
return;
}
char key = keyText[0];
if (_rules.Any(r => r.Key == key))
{
MessageBox.Show("Phím đã tồn tại trong quy tắc!", "ModernKey", MessageBoxButton.OK, MessageBoxImage.Warning);
return;
}
int action = CmbAction.SelectedIndex >= 0 ? CmbAction.SelectedIndex : 0;
var newRule = new CustomInputRule(key, action);
_rules.Add(newRule);
DgRules.SelectedItem = newRule;
DgRules.ScrollIntoView(newRule);
}
private void BtnReplace_Click(object sender, RoutedEventArgs e)
{
string keyText = TxtKey.Text?.Trim();
if (string.IsNullOrEmpty(keyText))
{
MessageBox.Show("Hãy nhập phím!", "ModernKey", MessageBoxButton.OK, MessageBoxImage.Warning);
TxtKey.Focus();
return;
}
char key = keyText[0];
var existing = _rules.FirstOrDefault(r => r.Key == key);
if (existing == null)
{
MessageBox.Show("Không tìm thấy phím để thay thế!", "ModernKey", MessageBoxButton.OK, MessageBoxImage.Warning);
return;
}
int action = CmbAction.SelectedIndex >= 0 ? CmbAction.SelectedIndex : 0;
existing.Action = action;
}
private void BtnDelete_Click(object sender, RoutedEventArgs e)
{
if (DgRules.SelectedItem is CustomInputRule selected)
{
_rules.Remove(selected);
TxtKey.Text = string.Empty;
}
else if (!string.IsNullOrEmpty(TxtKey.Text))
{
char key = TxtKey.Text[0];
var rule = _rules.FirstOrDefault(r => r.Key == key);
if (rule != null)
{
_rules.Remove(rule);
TxtKey.Text = string.Empty;
}
}
}
private void DgRules_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
if (DgRules.SelectedItem is CustomInputRule selected)
{
TxtKey.Text = selected.Key.ToString();
if (selected.Action >= 0 && selected.Action < CmbAction.Items.Count)
{
CmbAction.SelectedIndex = selected.Action;
}
}
}
private void BtnSave_Click(object sender, RoutedEventArgs e)
{
_settings.CustomRules = new List<CustomInputRule>(_rules);
SettingsManager.SaveSettings(_settings);
DialogResult = true;
Close();
}
private void BtnClose_Click(object sender, RoutedEventArgs e)
{
DialogResult = false;
Close();
}
}
}