Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 10 additions & 7 deletions Flow.Launcher/CustomShortcutSetting.xaml.cs
Original file line number Diff line number Diff line change
@@ -1,31 +1,34 @@
using Flow.Launcher.Core.Resource;
using Flow.Launcher.ViewModel;
using System;
using System.Windows;
using System.Windows.Input;
using Flow.Launcher.SettingPages.ViewModels;

namespace Flow.Launcher
{
public partial class CustomShortcutSetting : Window
{
private readonly SettingsPaneHotkeyViewModel _hotkeyVm;
public string Key { get; set; } = String.Empty;
public string Value { get; set; } = String.Empty;
private string originalKey { get; init; } = null;
private string originalValue { get; init; } = null;
private bool update { get; init; } = false;
private string originalKey { get; } = null;
private string originalValue { get; } = null;
private bool update { get; } = false;

public CustomShortcutSetting(SettingWindowViewModel vm)
public CustomShortcutSetting(SettingsPaneHotkeyViewModel vm)
{
_hotkeyVm = vm;
InitializeComponent();
}

public CustomShortcutSetting(string key, string value)
public CustomShortcutSetting(string key, string value, SettingsPaneHotkeyViewModel vm)
{
Key = key;
Value = value;
originalKey = key;
originalValue = value;
update = true;
_hotkeyVm = vm;
InitializeComponent();
}

Expand All @@ -43,7 +46,7 @@ private void BtnAdd_OnClick(object sender, RoutedEventArgs e)
return;
}
// Check if key is modified or adding a new one
if ((update && originalKey != Key) || !update)
if (((update && originalKey != Key) || !update) && _hotkeyVm.DoesShortcutExist(Key))
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The logic to check for duplicate shortcuts is well-placed. However, consider adding a null check for _hotkeyVm to prevent potential runtime exceptions if the dependency is not properly injected.

if (_hotkeyVm != null && ((update && originalKey != Key) || !update) && _hotkeyVm.DoesShortcutExist(Key))
Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
if (((update && originalKey != Key) || !update) && _hotkeyVm.DoesShortcutExist(Key))
if (_hotkeyVm != null && ((update && originalKey != Key) || !update) && _hotkeyVm.DoesShortcutExist(Key))

{
MessageBox.Show(InternationalizationManager.Instance.GetTranslation("duplicateShortcut"));
return;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System.Windows;
using System.Linq;
using System.Windows;
using CommunityToolkit.Mvvm.Input;
using Flow.Launcher.Core.Resource;
using Flow.Launcher.Helper;
Expand Down Expand Up @@ -114,7 +115,7 @@ private void CustomShortcutEdit()
return;
}

var window = new CustomShortcutSetting(item.Key, item.Value);
var window = new CustomShortcutSetting(item.Key, item.Value, this);
if (window.ShowDialog() is not true) return;

var index = Settings.CustomShortcuts.IndexOf(item);
Expand All @@ -124,11 +125,17 @@ private void CustomShortcutEdit()
[RelayCommand]
private void CustomShortcutAdd()
{
var window = new CustomShortcutSetting(null);
var window = new CustomShortcutSetting(this);
if (window.ShowDialog() is true)
{
var shortcut = new CustomShortcutModel(window.Key, window.Value);
Settings.CustomShortcuts.Add(shortcut);
}
}

internal bool DoesShortcutExist(string key)
{
return Settings.CustomShortcuts.Any(v => v.Key == key) ||
Settings.BuiltinShortcuts.Any(v => v.Key == key);
}
}