Use dependency injection instead of dependency property

This commit is contained in:
Jack251970 2025-03-05 14:42:08 +08:00
parent 92e6e53ab4
commit f2248e93b1
5 changed files with 137 additions and 55 deletions

View file

@ -103,7 +103,6 @@
HorizontalAlignment="Left"
VerticalAlignment="Center"
HorizontalContentAlignment="Left"
HotkeySettings="{Binding Settings}"
DefaultHotkey="" />
<TextBlock
Grid.Row="1"

View file

@ -4,25 +4,16 @@ using System.Collections.ObjectModel;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Input;
using CommunityToolkit.Mvvm.DependencyInjection;
using Flow.Launcher.Core.Resource;
using Flow.Launcher.Helper;
using Flow.Launcher.Infrastructure.Hotkey;
using Flow.Launcher.Infrastructure.UserSettings;
namespace Flow.Launcher
{
public partial class HotkeyControl
{
public IHotkeySettings HotkeySettings {
get { return (IHotkeySettings)GetValue(HotkeySettingsProperty); }
set { SetValue(HotkeySettingsProperty, value); }
}
public static readonly DependencyProperty HotkeySettingsProperty = DependencyProperty.Register(
nameof(HotkeySettings),
typeof(IHotkeySettings),
typeof(HotkeyControl),
new PropertyMetadata()
);
public string WindowTitle {
get { return (string)GetValue(WindowTitleProperty); }
set { SetValue(WindowTitleProperty, value); }
@ -71,8 +62,7 @@ namespace Flow.Launcher
return;
}
hotkeyControl.SetKeysToDisplay(new HotkeyModel(hotkeyControl.Hotkey));
hotkeyControl.CurrentHotkey = new HotkeyModel(hotkeyControl.Hotkey);
hotkeyControl.RefreshHotkeyInterface(hotkeyControl.Hotkey);
}
@ -90,17 +80,117 @@ namespace Flow.Launcher
}
public static readonly DependencyProperty HotkeyProperty = DependencyProperty.Register(
nameof(Hotkey),
typeof(string),
public static readonly DependencyProperty TypeProperty = DependencyProperty.Register(
nameof(Type),
typeof(HotkeyType),
typeof(HotkeyControl),
new FrameworkPropertyMetadata("", FrameworkPropertyMetadataOptions.BindsTwoWayByDefault, OnHotkeyChanged)
new FrameworkPropertyMetadata(HotkeyType.Hotkey, FrameworkPropertyMetadataOptions.BindsTwoWayByDefault, OnHotkeyChanged)
);
public HotkeyType Type
{
get { return (HotkeyType)GetValue(TypeProperty); }
set { SetValue(TypeProperty, value); }
}
public enum HotkeyType
{
Hotkey,
PreviewHotkey,
OpenContextMenuHotkey,
SettingWindowHotkey,
CycleHistoryUpHotkey,
CycleHistoryDownHotkey,
SelectPrevPageHotkey,
SelectNextPageHotkey,
AutoCompleteHotkey,
AutoCompleteHotkey2,
SelectPrevItemHotkey,
SelectPrevItemHotkey2,
SelectNextItemHotkey,
SelectNextItemHotkey2
}
// We can initialize settings in static field because it has been construct in App constuctor
// and it will not construct settings instances twice
private static readonly Settings _settings = Ioc.Default.GetRequiredService<Settings>();
public string Hotkey
{
get { return (string)GetValue(HotkeyProperty); }
set { SetValue(HotkeyProperty, value); }
get
{
return Type switch
{
HotkeyType.Hotkey => _settings.Hotkey,
HotkeyType.PreviewHotkey => _settings.PreviewHotkey,
HotkeyType.OpenContextMenuHotkey => _settings.OpenContextMenuHotkey,
HotkeyType.SettingWindowHotkey => _settings.SettingWindowHotkey,
HotkeyType.CycleHistoryUpHotkey => _settings.CycleHistoryUpHotkey,
HotkeyType.CycleHistoryDownHotkey => _settings.CycleHistoryDownHotkey,
HotkeyType.SelectPrevPageHotkey => _settings.SelectPrevPageHotkey,
HotkeyType.SelectNextPageHotkey => _settings.SelectNextPageHotkey,
HotkeyType.AutoCompleteHotkey => _settings.AutoCompleteHotkey,
HotkeyType.AutoCompleteHotkey2 => _settings.AutoCompleteHotkey2,
HotkeyType.SelectPrevItemHotkey => _settings.SelectPrevItemHotkey,
HotkeyType.SelectPrevItemHotkey2 => _settings.SelectPrevItemHotkey2,
HotkeyType.SelectNextItemHotkey => _settings.SelectNextItemHotkey,
HotkeyType.SelectNextItemHotkey2 => _settings.SelectNextItemHotkey2,
_ => string.Empty
};
}
set
{
switch (Type)
{
case HotkeyType.Hotkey:
_settings.Hotkey = value;
break;
case HotkeyType.PreviewHotkey:
_settings.PreviewHotkey = value;
break;
case HotkeyType.OpenContextMenuHotkey:
_settings.OpenContextMenuHotkey = value;
break;
case HotkeyType.SettingWindowHotkey:
_settings.SettingWindowHotkey = value;
break;
case HotkeyType.CycleHistoryUpHotkey:
_settings.CycleHistoryUpHotkey = value;
break;
case HotkeyType.CycleHistoryDownHotkey:
_settings.CycleHistoryDownHotkey = value;
break;
case HotkeyType.SelectPrevPageHotkey:
_settings.SelectPrevPageHotkey = value;
break;
case HotkeyType.SelectNextPageHotkey:
_settings.SelectNextPageHotkey = value;
break;
case HotkeyType.AutoCompleteHotkey:
_settings.AutoCompleteHotkey = value;
break;
case HotkeyType.AutoCompleteHotkey2:
_settings.AutoCompleteHotkey2 = value;
break;
case HotkeyType.SelectPrevItemHotkey:
_settings.SelectPrevItemHotkey = value;
break;
case HotkeyType.SelectNextItemHotkey:
_settings.SelectNextItemHotkey = value;
break;
case HotkeyType.SelectPrevItemHotkey2:
_settings.SelectPrevItemHotkey2 = value;
break;
case HotkeyType.SelectNextItemHotkey2:
_settings.SelectNextItemHotkey2 = value;
break;
default:
return;
}
// After setting the hotkey, we need to refresh the interface
RefreshHotkeyInterface(Hotkey);
}
}
public HotkeyControl()
@ -108,7 +198,14 @@ namespace Flow.Launcher
InitializeComponent();
HotkeyList.ItemsSource = KeysToDisplay;
SetKeysToDisplay(CurrentHotkey);
RefreshHotkeyInterface(Hotkey);
}
public void RefreshHotkeyInterface(string hotkey)
{
SetKeysToDisplay(new HotkeyModel(Hotkey));
CurrentHotkey = new HotkeyModel(Hotkey);
}
private static bool CheckHotkeyAvailability(HotkeyModel hotkey, bool validateKeyGesture) =>
@ -133,7 +230,7 @@ namespace Flow.Launcher
HotKeyMapper.RemoveHotkey(Hotkey);
}
var dialog = new HotkeyControlDialog(Hotkey, DefaultHotkey, HotkeySettings, WindowTitle);
var dialog = new HotkeyControlDialog(Hotkey, DefaultHotkey, WindowTitle);
await dialog.ShowAsync();
switch (dialog.ResultType)
{

View file

@ -4,9 +4,11 @@ using System.Linq;
using System.Windows;
using System.Windows.Input;
using ChefKeys;
using CommunityToolkit.Mvvm.DependencyInjection;
using Flow.Launcher.Core.Resource;
using Flow.Launcher.Helper;
using Flow.Launcher.Infrastructure.Hotkey;
using Flow.Launcher.Infrastructure.UserSettings;
using Flow.Launcher.Plugin;
using ModernWpf.Controls;
@ -16,7 +18,7 @@ namespace Flow.Launcher;
public partial class HotkeyControlDialog : ContentDialog
{
private IHotkeySettings _hotkeySettings;
private static readonly IHotkeySettings _hotkeySettings = Ioc.Default.GetRequiredService<Settings>();
private Action? _overwriteOtherHotkey;
private string DefaultHotkey { get; }
public string WindowTitle { get; }
@ -36,7 +38,7 @@ public partial class HotkeyControlDialog : ContentDialog
private static bool isOpenFlowHotkey;
public HotkeyControlDialog(string hotkey, string defaultHotkey, IHotkeySettings hotkeySettings, string windowTitle = "")
public HotkeyControlDialog(string hotkey, string defaultHotkey, string windowTitle = "")
{
WindowTitle = windowTitle switch
{
@ -45,7 +47,6 @@ public partial class HotkeyControlDialog : ContentDialog
};
DefaultHotkey = defaultHotkey;
CurrentHotkey = new HotkeyModel(hotkey);
_hotkeySettings = hotkeySettings;
SetKeysToDisplay(CurrentHotkey);
InitializeComponent();

View file

@ -114,8 +114,7 @@
Margin="0,8,0,0"
ChangeHotkey="{Binding SetTogglingHotkeyCommand}"
DefaultHotkey="Alt+Space"
Hotkey="{Binding Settings.Hotkey}"
HotkeySettings="{Binding Settings}"
Type="Hotkey"
ValidateKeyGesture="True"
WindowTitle="{DynamicResource flowlauncherHotkey}" />
</StackPanel>

View file

@ -34,8 +34,7 @@
<flowlauncher:HotkeyControl
ChangeHotkey="{Binding SetTogglingHotkeyCommand}"
DefaultHotkey="Alt+Space"
Hotkey="{Binding Settings.Hotkey}"
HotkeySettings="{Binding Settings}"
Type="Hotkey"
ValidateKeyGesture="True"
WindowTitle="{DynamicResource flowlauncherHotkey}" />
</cc:Card>
@ -46,8 +45,7 @@
Sub="{DynamicResource previewHotkeyToolTip}">
<flowlauncher:HotkeyControl
DefaultHotkey="F1"
Hotkey="{Binding Settings.PreviewHotkey}"
HotkeySettings="{Binding Settings}"
Type="PreviewHotkey"
ValidateKeyGesture="False"
WindowTitle="{DynamicResource previewHotkey}" />
</cc:Card>
@ -105,8 +103,7 @@
Type="Inside">
<flowlauncher:HotkeyControl
DefaultHotkey="Ctrl+I"
Hotkey="{Binding Settings.OpenContextMenuHotkey}"
HotkeySettings="{Binding Settings}"
Type="OpenContextMenuHotkey"
ValidateKeyGesture="False" />
</cc:Card>
<cc:Card
@ -127,8 +124,7 @@
Type="Inside">
<flowlauncher:HotkeyControl
DefaultHotkey="Ctrl+I"
Hotkey="{Binding Settings.SettingWindowHotkey}"
HotkeySettings="{Binding Settings}"
Type="SettingWindowHotkey"
ValidateKeyGesture="False" />
</cc:Card>
<cc:Card
@ -149,8 +145,7 @@
Type="Inside">
<flowlauncher:HotkeyControl
DefaultHotkey="Alt+Up"
Hotkey="{Binding Settings.CycleHistoryUpHotkey}"
HotkeySettings="{Binding Settings}"
Type="CycleHistoryUpHotkey"
ValidateKeyGesture="False" />
</cc:Card>
<cc:Card
@ -159,8 +154,7 @@
Type="Inside">
<flowlauncher:HotkeyControl
DefaultHotkey="Alt+Down"
Hotkey="{Binding Settings.CycleHistoryDownHotkey}"
HotkeySettings="{Binding Settings}"
Type="CycleHistoryDownHotkey"
ValidateKeyGesture="False" />
</cc:Card>
<cc:Card
@ -176,8 +170,7 @@
Type="Inside">
<flowlauncher:HotkeyControl
DefaultHotkey=""
Hotkey="{Binding Settings.SelectPrevPageHotkey}"
HotkeySettings="{Binding Settings}"
Type="SelectPrevPageHotkey"
ValidateKeyGesture="False" />
</cc:Card>
<cc:Card
@ -186,8 +179,7 @@
Type="Inside">
<flowlauncher:HotkeyControl
DefaultHotkey=""
Hotkey="{Binding Settings.SelectNextPageHotkey}"
HotkeySettings="{Binding Settings}"
Type="SelectNextPageHotkey"
ValidateKeyGesture="False" />
</cc:Card>
@ -221,8 +213,7 @@
<cc:ExCard.SideContent>
<flowlauncher:HotkeyControl
DefaultHotkey="Ctrl+Tab"
Hotkey="{Binding Settings.AutoCompleteHotkey}"
HotkeySettings="{Binding Settings}"
Type="AutoCompleteHotkey"
ValidateKeyGesture="False" />
</cc:ExCard.SideContent>
<cc:Card
@ -231,8 +222,7 @@
Type="InsideFit">
<flowlauncher:HotkeyControl
DefaultHotkey=""
Hotkey="{Binding Settings.AutoCompleteHotkey2}"
HotkeySettings="{Binding Settings}"
Type="AutoCompleteHotkey2"
ValidateKeyGesture="False" />
</cc:Card>
</cc:ExCard>
@ -244,8 +234,7 @@
<cc:ExCard.SideContent>
<flowlauncher:HotkeyControl
DefaultHotkey="Shift+Tab"
Hotkey="{Binding Settings.SelectPrevItemHotkey}"
HotkeySettings="{Binding Settings}"
Type="SelectPrevItemHotkey"
ValidateKeyGesture="False" />
</cc:ExCard.SideContent>
<cc:Card
@ -254,8 +243,7 @@
Type="InsideFit">
<flowlauncher:HotkeyControl
DefaultHotkey=""
Hotkey="{Binding Settings.SelectPrevItemHotkey2}"
HotkeySettings="{Binding Settings}"
Type="SelectPrevItemHotkey2"
ValidateKeyGesture="False" />
</cc:Card>
</cc:ExCard>
@ -267,8 +255,7 @@
<cc:ExCard.SideContent>
<flowlauncher:HotkeyControl
DefaultHotkey="Tab"
Hotkey="{Binding Settings.SelectNextItemHotkey}"
HotkeySettings="{Binding Settings}"
Type="SelectNextItemHotkey"
ValidateKeyGesture="False" />
</cc:ExCard.SideContent>
<cc:Card
@ -277,8 +264,7 @@
Type="InsideFit">
<flowlauncher:HotkeyControl
DefaultHotkey=""
Hotkey="{Binding Settings.SelectNextItemHotkey2}"
HotkeySettings="{Binding Settings}"
Type="SelectNextItemHotkey2"
ValidateKeyGesture="False" />
</cc:Card>
</cc:ExCard>