Skip to content

Commit 9b50a47

Browse files
0x5bfayaira2
authored andcommitted
Update ExecuteAddNewShortcutCommand for invert
1 parent 8710d43 commit 9b50a47

File tree

5 files changed

+71
-47
lines changed

5 files changed

+71
-47
lines changed

src/Files.App/Data/Commands/HotKey/HotKeyCollection.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ public string RawLabel
5151
/// <remarks>
5252
/// For example, this is "Ctrl+A, Ctrl+Alt+C"
5353
/// </remarks>
54-
public string HumanizedLabel
54+
public string LocalizedLabel
5555
=> string.Join(", ", hotKeys.Where(hotKey => hotKey.IsVisible).Select(hotKey => hotKey.LocalizedLabel));
5656

5757
// Constructors
@@ -107,7 +107,7 @@ public IEnumerator<HotKey> GetEnumerator()
107107
// Default methods
108108

109109
public override string ToString()
110-
=> HumanizedLabel;
110+
=> LocalizedLabel;
111111

112112
public override int GetHashCode()
113113
=> hotKeys.GetHashCode();

src/Files.App/Data/Commands/Manager/CommandManager.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -456,7 +456,7 @@ public string? HotKeyText
456456
{
457457
get
458458
{
459-
string text = HotKeys.HumanizedLabel;
459+
string text = HotKeys.LocalizedLabel;
460460
if (string.IsNullOrEmpty(text))
461461
return null;
462462
return text;

src/Files.App/ViewModels/Settings/ActionsViewModel.cs

Lines changed: 57 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,8 @@ public class ActionsViewModel : ObservableObject
1414

1515
// Properties
1616

17-
public ObservableCollection<ModifiableCommandHotKeyItem> KeyboardShortcuts { get; } = [];
18-
public ObservableCollection<ModifiableCommandHotKeyItem> ExcludedKeyboardShortcuts { get; } = [];
17+
public ObservableCollection<ModifiableCommandHotKeyItem> ValidKeyboardShortcuts { get; } = [];
18+
public ObservableCollection<ModifiableCommandHotKeyItem> AllKeyboardShortcuts { get; } = [];
1919

2020
private bool _IsResetAllConfirmationTeachingTipOpened;
2121
public bool IsResetAllConfirmationTeachingTipOpened
@@ -51,29 +51,29 @@ public ModifiableCommandHotKeyItem? SelectedNewShortcutItem
5151
public ICommand ShowResetAllConfirmationCommand { get; set; }
5252
public ICommand ShowAddNewShortcutGridCommand { get; set; }
5353
public ICommand HideAddNewShortcutGridCommand { get; set; }
54-
public ICommand AddNewShortcutGridCommand { get; set; }
54+
public ICommand AddNewShortcutCommand { get; set; }
5555
public ICommand ResetAllCommand { get; set; }
5656

5757
// Constructor
5858

5959
public ActionsViewModel()
6060
{
61-
LoadCommandsCommand = new AsyncRelayCommand(LoadCommands);
61+
LoadCommandsCommand = new AsyncRelayCommand(ExecuteLoadCommandsCommand);
6262
ShowResetAllConfirmationCommand = new RelayCommand(ExecuteShowResetAllConfirmationCommand);
6363
ShowAddNewShortcutGridCommand = new RelayCommand(ExecuteShowAddNewShortcutGridCommand);
6464
HideAddNewShortcutGridCommand = new RelayCommand(ExecuteHideAddNewShortcutGridCommand);
65-
AddNewShortcutGridCommand = new RelayCommand(ExecuteAddNewShortcutGridCommand);
65+
AddNewShortcutCommand = new RelayCommand(ExecuteAddNewShortcutCommand);
6666
ResetAllCommand = new RelayCommand(ExecuteResetAllCommand);
6767
}
6868

6969
// Command methods
7070

71-
private async Task LoadCommands()
71+
private async Task ExecuteLoadCommandsCommand()
7272
{
7373
await MainWindow.Instance.DispatcherQueue.EnqueueOrInvokeAsync(() =>
7474
{
75-
KeyboardShortcuts.Clear();
76-
ExcludedKeyboardShortcuts.Clear();
75+
ValidKeyboardShortcuts.Clear();
76+
AllKeyboardShortcuts.Clear();
7777

7878
foreach (var command in Commands)
7979
{
@@ -82,25 +82,20 @@ await MainWindow.Instance.DispatcherQueue.EnqueueOrInvokeAsync(() =>
8282
if (command is NoneCommand)
8383
continue;
8484

85-
if (command.HotKeys.IsEmpty)
85+
AllKeyboardShortcuts.Add(new()
8686
{
87-
ExcludedKeyboardShortcuts.Add(new()
88-
{
89-
CommandCode = command.Code,
90-
Label = command.Label,
91-
Description = command.Description,
92-
HotKey = new(),
93-
});
94-
95-
continue;
96-
}
87+
CommandCode = command.Code,
88+
Label = command.Label,
89+
Description = command.Description,
90+
HotKey = new(),
91+
});
9792

9893
foreach (var hotkey in command.HotKeys)
9994
{
10095
if (!hotkey.IsVisible)
10196
continue;
10297

103-
KeyboardShortcuts.Add(new()
98+
ValidKeyboardShortcuts.Add(new()
10499
{
105100
CommandCode = command.Code,
106101
Label = command.Label,
@@ -125,7 +120,7 @@ private void ExecuteShowAddNewShortcutGridCommand()
125120
ShowAddNewShortcutGrid = true;
126121

127122
// Reset edit mode for each item
128-
foreach (var hotkey in KeyboardShortcuts)
123+
foreach (var hotkey in ValidKeyboardShortcuts)
129124
{
130125
hotkey.IsEditMode = false;
131126
hotkey.HotKeyText = hotkey.HotKey.LocalizedLabel;
@@ -143,28 +138,57 @@ private void ExecuteHideAddNewShortcutGridCommand()
143138
SelectedNewShortcutItem = null;
144139
}
145140

146-
private void ExecuteAddNewShortcutGridCommand()
141+
private void ExecuteAddNewShortcutCommand()
147142
{
148143
if (SelectedNewShortcutItem is null)
149144
return;
150145

146+
// Check if this hot key is already taken
147+
foreach (var hotkey in ValidKeyboardShortcuts)
148+
{
149+
if (SelectedNewShortcutItem.HotKeyText == hotkey.PreviousHotKey)
150+
{
151+
IsAlreadyUsedTeachingTipOpened = true;
152+
return;
153+
}
154+
}
155+
151156
var actions =
152157
GeneralSettingsService.Actions is not null
153158
? new Dictionary<string, string>(GeneralSettingsService.Actions)
154159
: [];
155160

156-
// Remove existing setting
157-
foreach (var action in actions)
161+
// Get raw string keys stored in the user setting
162+
var storedKeys = actions.GetValueOrDefault(SelectedNewShortcutItem.CommandCode.ToString());
163+
164+
// Initialize
165+
var newHotKey = HotKey.Parse(SelectedNewShortcutItem.HotKeyText);
166+
var modifiedCollection = HotKeyCollection.Empty;
167+
168+
// The first time to customize
169+
if (string.IsNullOrEmpty(storedKeys))
170+
{
171+
// Replace with new one
172+
var modifiableDefaultCollection = SelectedNewShortcutItem.DefaultHotKeyCollection.ToList();
173+
modifiableDefaultCollection.RemoveAll(x => x.RawLabel == SelectedNewShortcutItem.PreviousHotKey.RawLabel);
174+
modifiableDefaultCollection.Add(newHotKey);
175+
modifiedCollection = new HotKeyCollection(modifiableDefaultCollection);
176+
}
177+
// Stored in the user setting
178+
else
158179
{
159-
if (Enum.TryParse(action.Key, true, out CommandCodes code) && code == SelectedNewShortcutItem.CommandCode)
160-
actions.Remove(action.Key);
180+
// Replace with new one
181+
var modifiableCollection = HotKeyCollection.Parse(storedKeys).ToList();
182+
modifiableCollection.RemoveAll(x => x.RawLabel == SelectedNewShortcutItem.PreviousHotKey.RawLabel || x.RawLabel == $"!{SelectedNewShortcutItem.PreviousHotKey.RawLabel}");
183+
modifiableCollection.Add(newHotKey);
184+
modifiedCollection = new HotKeyCollection(modifiableCollection);
161185
}
162186

163-
// Create a new one
164-
actions.Add(SelectedNewShortcutItem.CommandCode.ToString(), SelectedNewShortcutItem.HotKeyText);
187+
// Remove previous one and add new one
188+
actions.Remove(SelectedNewShortcutItem.CommandCode.ToString());
189+
actions.Add(SelectedNewShortcutItem.CommandCode.ToString(), modifiedCollection.RawLabel);
165190

166-
// Set
167-
SelectedNewShortcutItem.HotKey = HotKey.Parse(SelectedNewShortcutItem.HotKeyText);
191+
// Store
168192
GeneralSettingsService.Actions = actions;
169193

170194
// Create a clone
@@ -173,7 +197,7 @@ GeneralSettingsService.Actions is not null
173197
CommandCode = SelectedNewShortcutItem.CommandCode,
174198
Label = SelectedNewShortcutItem.Label,
175199
Description = SelectedNewShortcutItem.Description,
176-
HotKey = HotKey.Parse(SelectedNewShortcutItem.HotKey.RawLabel),
200+
HotKey = HotKey.Parse(SelectedNewShortcutItem.HotKeyText),
177201
DefaultHotKeyCollection = new(SelectedNewShortcutItem.DefaultHotKeyCollection),
178202
PreviousHotKey = HotKey.Parse(SelectedNewShortcutItem.PreviousHotKey.RawLabel),
179203
};
@@ -186,15 +210,15 @@ GeneralSettingsService.Actions is not null
186210
SelectedNewShortcutItem = null;
187211

188212
// Add to existing list
189-
KeyboardShortcuts.Insert(0, selectedNewItem);
213+
ValidKeyboardShortcuts.Insert(0, selectedNewItem);
190214
}
191215

192216
private void ExecuteResetAllCommand()
193217
{
194218
GeneralSettingsService.Actions = null;
195219
IsResetAllConfirmationTeachingTipOpened = false;
196220

197-
_ = LoadCommands();
221+
_ = ExecuteLoadCommandsCommand();
198222
}
199223
}
200224
}

src/Files.App/Views/Settings/ActionsPage.xaml

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -126,7 +126,7 @@
126126
<ComboBox
127127
x:Name="NewShortcutItemPickerComboBox"
128128
Padding="0"
129-
ItemsSource="{x:Bind ViewModel.ExcludedKeyboardShortcuts, Mode=OneWay}"
129+
ItemsSource="{x:Bind ViewModel.AllKeyboardShortcuts, Mode=OneWay}"
130130
PlaceholderText="{helpers:ResourceString Name=ChooseAnAction}"
131131
SelectedItem="{x:Bind ViewModel.SelectedNewShortcutItem, Mode=TwoWay}">
132132
<ComboBox.ItemTemplate>
@@ -176,7 +176,7 @@
176176
Height="32"
177177
Padding="0"
178178
AutomationProperties.Name="{helpers:ResourceString Name=Add}"
179-
Command="{x:Bind ViewModel.AddNewShortcutGridCommand, Mode=OneWay}"
179+
Command="{x:Bind ViewModel.AddNewShortcutCommand, Mode=OneWay}"
180180
IsEnabled="{x:Bind ViewModel.SelectedNewShortcutItem, Converter={StaticResource NullToBooleanConverter}, Mode=OneWay}"
181181
Style="{StaticResource AccentButtonStyle}"
182182
ToolTipService.ToolTip="{helpers:ResourceString Name=Add}">
@@ -206,8 +206,8 @@
206206
Background="{ThemeResource DividerStrokeColorDefaultBrush}" />
207207

208208
<ListView
209-
x:Name="KeyboardShortcutsListView"
210-
ItemsSource="{x:Bind ViewModel.KeyboardShortcuts, Mode=OneWay}"
209+
x:Name="ValidKeyboardShortcutsListView"
210+
ItemsSource="{x:Bind ViewModel.ValidKeyboardShortcuts, Mode=OneWay}"
211211
ScrollViewer.HorizontalScrollBarVisibility="Hidden"
212212
ScrollViewer.HorizontalScrollMode="Disabled"
213213
ScrollViewer.VerticalScrollBarVisibility="Hidden"

src/Files.App/Views/Settings/ActionsPage.xaml.cs

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ private void EditButton_Click(object sender, RoutedEventArgs e)
6262
}
6363

6464
// Reset edit mode for each item
65-
foreach (var hotkey in ViewModel.KeyboardShortcuts)
65+
foreach (var hotkey in ViewModel.ValidKeyboardShortcuts)
6666
{
6767
hotkey.IsEditMode = false;
6868
hotkey.HotKeyText = hotkey.HotKey.LocalizedLabel;
@@ -85,7 +85,7 @@ private void SaveButton_Click(object sender, RoutedEventArgs e)
8585
}
8686

8787
// Check if this hot key is already taken
88-
foreach (var hotkey in ViewModel.KeyboardShortcuts)
88+
foreach (var hotkey in ViewModel.ValidKeyboardShortcuts)
8989
{
9090
if (item.HotKeyText == hotkey.PreviousHotKey)
9191
{
@@ -137,7 +137,7 @@ GeneralSettingsService.Actions is not null
137137
item.HotKey = newHotKey;
138138

139139
// Set as customized
140-
foreach (var action in ViewModel.KeyboardShortcuts)
140+
foreach (var action in ViewModel.ValidKeyboardShortcuts)
141141
{
142142
if (action.CommandCode == item.CommandCode)
143143
action.IsCustomized = !item.DefaultHotKeyCollection.Contains(action.HotKey);
@@ -172,7 +172,7 @@ GeneralSettingsService.Actions is not null
172172
item.HotKey = newHotKey;
173173

174174
// Set as customized
175-
foreach (var action in ViewModel.KeyboardShortcuts)
175+
foreach (var action in ViewModel.ValidKeyboardShortcuts)
176176
{
177177
if (action.CommandCode == item.CommandCode)
178178
action.IsCustomized = !item.DefaultHotKeyCollection.Contains(action.HotKey);
@@ -243,7 +243,7 @@ GeneralSettingsService.Actions is not null
243243

244244
// Exit
245245
item.IsEditMode = false;
246-
ViewModel.KeyboardShortcuts.Remove(item);
246+
ViewModel.ValidKeyboardShortcuts.Remove(item);
247247

248248
return;
249249
}
@@ -257,15 +257,15 @@ GeneralSettingsService.Actions is not null
257257
// Remove previous
258258
actions.Remove(item.CommandCode.ToString());
259259

260-
if (modifiedCollection.HumanizedLabel != string.Empty)
260+
if (modifiedCollection.LocalizedLabel != string.Empty)
261261
actions.Add(item.CommandCode.ToString(), modifiedCollection.RawLabel);
262262

263263
// Save
264264
GeneralSettingsService.Actions = actions;
265265

266266
// Exit
267267
item.IsEditMode = false;
268-
ViewModel.KeyboardShortcuts.Remove(item);
268+
ViewModel.ValidKeyboardShortcuts.Remove(item);
269269

270270
return;
271271
}

0 commit comments

Comments
 (0)