Skip to content

Commit 5d3e6e0

Browse files
Move dialog logic to vm
1 parent 977ec33 commit 5d3e6e0

File tree

2 files changed

+33
-40
lines changed

2 files changed

+33
-40
lines changed

Flow.Launcher/CustomShortcutSetting.xaml.cs

Lines changed: 19 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -1,32 +1,33 @@
11
using Flow.Launcher.Core.Resource;
2-
using Flow.Launcher.Infrastructure.UserSettings;
2+
using Flow.Launcher.ViewModel;
33
using System;
4-
using System.Linq;
54
using System.Windows;
65
using System.Windows.Input;
76

87
namespace Flow.Launcher
98
{
109
public partial class CustomShortcutSetting : Window
1110
{
12-
private Settings _settings;
13-
private bool update = false;
11+
private SettingWindowViewModel viewModel;
1412
public string Key { get; set; }
1513
public string Value { get; set; }
16-
public CustomShortcutModel ShortCut;
14+
private string originalKey { get; init; } = null;
15+
private string originalValue { get; init; } = null;
16+
bool update = false;
1717

18-
public CustomShortcutSetting(Settings settings)
18+
public CustomShortcutSetting(SettingWindowViewModel vm)
1919
{
20-
_settings = settings;
20+
viewModel = vm;
2121
InitializeComponent();
2222
}
2323

24-
public CustomShortcutSetting(CustomShortcutModel shortcut, Settings settings)
24+
public CustomShortcutSetting(string key, string value, SettingWindowViewModel vm)
2525
{
26-
Key = shortcut.Key;
27-
Value = shortcut.Value;
28-
ShortCut = shortcut;
29-
_settings = settings;
26+
viewModel = vm;
27+
Key = key;
28+
Value = value;
29+
originalKey = key;
30+
originalValue = value;
3031
update = true;
3132
InitializeComponent();
3233
}
@@ -39,32 +40,19 @@ private void BtnCancel_OnClick(object sender, RoutedEventArgs e)
3940

4041
private void BtnAdd_OnClick(object sender, RoutedEventArgs e)
4142
{
42-
bool modified = false;
4343
if (String.IsNullOrEmpty(Key) || String.IsNullOrEmpty(Value))
4444
{
4545
MessageBox.Show(InternationalizationManager.Instance.GetTranslation("emptyShortcut"));
4646
return;
4747
}
48-
if (!update)
48+
// Check if key is modified or adding a new one
49+
if (((update && originalKey != Key) || !update)
50+
&& viewModel.ShortcutExists(Key))
4951
{
50-
ShortCut = new CustomShortcutModel(Key, Value);
51-
if (_settings.CustomShortcuts.Any(x => x.Key == Key) || _settings.BuiltinShortcuts.Any(x => x.Key == Key))
52-
{
53-
MessageBox.Show(InternationalizationManager.Instance.GetTranslation("duplicateShortcut"));
54-
return;
55-
}
56-
modified = true;
57-
}
58-
else
59-
{
60-
if (ShortCut.Key != Key && _settings.CustomShortcuts.Any(x => x.Key == Key) || _settings.BuiltinShortcuts.Any(x => x.Key == Key))
61-
{
62-
MessageBox.Show(InternationalizationManager.Instance.GetTranslation("duplicateShortcut"));
63-
return;
64-
}
65-
modified = ShortCut.Key != Key || ShortCut.Value != Value;
52+
MessageBox.Show(InternationalizationManager.Instance.GetTranslation("duplicateShortcut"));
53+
return;
6654
}
67-
DialogResult = modified;
55+
DialogResult = !update || originalKey != Key || originalValue != Value;
6856
Close();
6957
}
7058

Flow.Launcher/ViewModel/SettingWindowViewModel.cs

Lines changed: 14 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -685,12 +685,12 @@ public FamilyTypeface SelectedResultFontFaces
685685

686686
public CustomPluginHotkey SelectedCustomPluginHotkey { get; set; }
687687

688-
public CustomShortcutModel? SelectedCustomShortcut { get; set; }
689-
690688
#endregion
691689

692690
#region shortcut
693691

692+
public CustomShortcutModel? SelectedCustomShortcut { get; set; }
693+
694694
public void DeleteSelectedCustomShortcut()
695695
{
696696
var item = SelectedCustomShortcut;
@@ -700,11 +700,10 @@ public void DeleteSelectedCustomShortcut()
700700
return;
701701
}
702702

703-
string deleteWarning =
704-
string.Format(InternationalizationManager.Instance.GetTranslation("deleteCustomShortcutWarning"),
703+
string deleteWarning = string.Format(
704+
InternationalizationManager.Instance.GetTranslation("deleteCustomShortcutWarning"),
705705
item?.Key, item?.Value);
706-
if (
707-
MessageBox.Show(deleteWarning, InternationalizationManager.Instance.GetTranslation("delete"),
706+
if (MessageBox.Show(deleteWarning, InternationalizationManager.Instance.GetTranslation("delete"),
708707
MessageBoxButton.YesNo) == MessageBoxResult.Yes)
709708
{
710709
Settings.CustomShortcuts.Remove(item);
@@ -720,7 +719,7 @@ public bool EditSelectedCustomShortcut()
720719
return false;
721720
}
722721

723-
var shortcutSettingWindow = new CustomShortcutSetting(item, Settings);
722+
var shortcutSettingWindow = new CustomShortcutSetting(item.Key, item.Value, this);
724723
if (shortcutSettingWindow.ShowDialog() == true)
725724
{
726725
item.Key = shortcutSettingWindow.Key;
@@ -732,13 +731,19 @@ public bool EditSelectedCustomShortcut()
732731

733732
public void AddCustomShortcut()
734733
{
735-
var shortcutSettingWindow = new CustomShortcutSetting(Settings);
734+
var shortcutSettingWindow = new CustomShortcutSetting(this);
736735
if (shortcutSettingWindow.ShowDialog() == true)
737736
{
738-
Settings.CustomShortcuts.Add(shortcutSettingWindow.ShortCut);
737+
var shortcut = new CustomShortcutModel(shortcutSettingWindow.Key, shortcutSettingWindow.Value);
738+
Settings.CustomShortcuts.Add(shortcut);
739739
}
740740
}
741741

742+
public bool ShortcutExists(string key)
743+
{
744+
return Settings.CustomShortcuts.Any(x => x.Key == key) || Settings.BuiltinShortcuts.Any(x => x.Key == key);
745+
}
746+
742747
#endregion
743748

744749
#region about

0 commit comments

Comments
 (0)