Skip to content

Commit f2248e9

Browse files
committed
Use dependency injection instead of dependency property
1 parent 92e6e53 commit f2248e9

File tree

5 files changed

+137
-55
lines changed

5 files changed

+137
-55
lines changed

Flow.Launcher/CustomQueryHotkeySetting.xaml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -103,7 +103,6 @@
103103
HorizontalAlignment="Left"
104104
VerticalAlignment="Center"
105105
HorizontalContentAlignment="Left"
106-
HotkeySettings="{Binding Settings}"
107106
DefaultHotkey="" />
108107
<TextBlock
109108
Grid.Row="1"

Flow.Launcher/HotkeyControl.xaml.cs

Lines changed: 118 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -4,25 +4,16 @@
44
using System.Threading.Tasks;
55
using System.Windows;
66
using System.Windows.Input;
7+
using CommunityToolkit.Mvvm.DependencyInjection;
78
using Flow.Launcher.Core.Resource;
89
using Flow.Launcher.Helper;
910
using Flow.Launcher.Infrastructure.Hotkey;
11+
using Flow.Launcher.Infrastructure.UserSettings;
1012

1113
namespace Flow.Launcher
1214
{
1315
public partial class HotkeyControl
1416
{
15-
public IHotkeySettings HotkeySettings {
16-
get { return (IHotkeySettings)GetValue(HotkeySettingsProperty); }
17-
set { SetValue(HotkeySettingsProperty, value); }
18-
}
19-
20-
public static readonly DependencyProperty HotkeySettingsProperty = DependencyProperty.Register(
21-
nameof(HotkeySettings),
22-
typeof(IHotkeySettings),
23-
typeof(HotkeyControl),
24-
new PropertyMetadata()
25-
);
2617
public string WindowTitle {
2718
get { return (string)GetValue(WindowTitleProperty); }
2819
set { SetValue(WindowTitleProperty, value); }
@@ -71,8 +62,7 @@ private static void OnHotkeyChanged(DependencyObject d, DependencyPropertyChange
7162
return;
7263
}
7364

74-
hotkeyControl.SetKeysToDisplay(new HotkeyModel(hotkeyControl.Hotkey));
75-
hotkeyControl.CurrentHotkey = new HotkeyModel(hotkeyControl.Hotkey);
65+
hotkeyControl.RefreshHotkeyInterface(hotkeyControl.Hotkey);
7666
}
7767

7868

@@ -90,25 +80,132 @@ public ICommand? ChangeHotkey
9080
}
9181

9282

93-
public static readonly DependencyProperty HotkeyProperty = DependencyProperty.Register(
94-
nameof(Hotkey),
95-
typeof(string),
83+
public static readonly DependencyProperty TypeProperty = DependencyProperty.Register(
84+
nameof(Type),
85+
typeof(HotkeyType),
9686
typeof(HotkeyControl),
97-
new FrameworkPropertyMetadata("", FrameworkPropertyMetadataOptions.BindsTwoWayByDefault, OnHotkeyChanged)
87+
new FrameworkPropertyMetadata(HotkeyType.Hotkey, FrameworkPropertyMetadataOptions.BindsTwoWayByDefault, OnHotkeyChanged)
9888
);
9989

90+
public HotkeyType Type
91+
{
92+
get { return (HotkeyType)GetValue(TypeProperty); }
93+
set { SetValue(TypeProperty, value); }
94+
}
95+
96+
public enum HotkeyType
97+
{
98+
Hotkey,
99+
PreviewHotkey,
100+
OpenContextMenuHotkey,
101+
SettingWindowHotkey,
102+
CycleHistoryUpHotkey,
103+
CycleHistoryDownHotkey,
104+
SelectPrevPageHotkey,
105+
SelectNextPageHotkey,
106+
AutoCompleteHotkey,
107+
AutoCompleteHotkey2,
108+
SelectPrevItemHotkey,
109+
SelectPrevItemHotkey2,
110+
SelectNextItemHotkey,
111+
SelectNextItemHotkey2
112+
}
113+
114+
// We can initialize settings in static field because it has been construct in App constuctor
115+
// and it will not construct settings instances twice
116+
private static readonly Settings _settings = Ioc.Default.GetRequiredService<Settings>();
117+
100118
public string Hotkey
101119
{
102-
get { return (string)GetValue(HotkeyProperty); }
103-
set { SetValue(HotkeyProperty, value); }
120+
get
121+
{
122+
return Type switch
123+
{
124+
HotkeyType.Hotkey => _settings.Hotkey,
125+
HotkeyType.PreviewHotkey => _settings.PreviewHotkey,
126+
HotkeyType.OpenContextMenuHotkey => _settings.OpenContextMenuHotkey,
127+
HotkeyType.SettingWindowHotkey => _settings.SettingWindowHotkey,
128+
HotkeyType.CycleHistoryUpHotkey => _settings.CycleHistoryUpHotkey,
129+
HotkeyType.CycleHistoryDownHotkey => _settings.CycleHistoryDownHotkey,
130+
HotkeyType.SelectPrevPageHotkey => _settings.SelectPrevPageHotkey,
131+
HotkeyType.SelectNextPageHotkey => _settings.SelectNextPageHotkey,
132+
HotkeyType.AutoCompleteHotkey => _settings.AutoCompleteHotkey,
133+
HotkeyType.AutoCompleteHotkey2 => _settings.AutoCompleteHotkey2,
134+
HotkeyType.SelectPrevItemHotkey => _settings.SelectPrevItemHotkey,
135+
HotkeyType.SelectPrevItemHotkey2 => _settings.SelectPrevItemHotkey2,
136+
HotkeyType.SelectNextItemHotkey => _settings.SelectNextItemHotkey,
137+
HotkeyType.SelectNextItemHotkey2 => _settings.SelectNextItemHotkey2,
138+
_ => string.Empty
139+
};
140+
}
141+
set
142+
{
143+
switch (Type)
144+
{
145+
case HotkeyType.Hotkey:
146+
_settings.Hotkey = value;
147+
break;
148+
case HotkeyType.PreviewHotkey:
149+
_settings.PreviewHotkey = value;
150+
break;
151+
case HotkeyType.OpenContextMenuHotkey:
152+
_settings.OpenContextMenuHotkey = value;
153+
break;
154+
case HotkeyType.SettingWindowHotkey:
155+
_settings.SettingWindowHotkey = value;
156+
break;
157+
case HotkeyType.CycleHistoryUpHotkey:
158+
_settings.CycleHistoryUpHotkey = value;
159+
break;
160+
case HotkeyType.CycleHistoryDownHotkey:
161+
_settings.CycleHistoryDownHotkey = value;
162+
break;
163+
case HotkeyType.SelectPrevPageHotkey:
164+
_settings.SelectPrevPageHotkey = value;
165+
break;
166+
case HotkeyType.SelectNextPageHotkey:
167+
_settings.SelectNextPageHotkey = value;
168+
break;
169+
case HotkeyType.AutoCompleteHotkey:
170+
_settings.AutoCompleteHotkey = value;
171+
break;
172+
case HotkeyType.AutoCompleteHotkey2:
173+
_settings.AutoCompleteHotkey2 = value;
174+
break;
175+
case HotkeyType.SelectPrevItemHotkey:
176+
_settings.SelectPrevItemHotkey = value;
177+
break;
178+
case HotkeyType.SelectNextItemHotkey:
179+
_settings.SelectNextItemHotkey = value;
180+
break;
181+
case HotkeyType.SelectPrevItemHotkey2:
182+
_settings.SelectPrevItemHotkey2 = value;
183+
break;
184+
case HotkeyType.SelectNextItemHotkey2:
185+
_settings.SelectNextItemHotkey2 = value;
186+
break;
187+
default:
188+
return;
189+
}
190+
191+
// After setting the hotkey, we need to refresh the interface
192+
RefreshHotkeyInterface(Hotkey);
193+
}
104194
}
105195

106196
public HotkeyControl()
107197
{
108198
InitializeComponent();
109199

110200
HotkeyList.ItemsSource = KeysToDisplay;
111-
SetKeysToDisplay(CurrentHotkey);
201+
202+
RefreshHotkeyInterface(Hotkey);
203+
}
204+
205+
public void RefreshHotkeyInterface(string hotkey)
206+
{
207+
SetKeysToDisplay(new HotkeyModel(Hotkey));
208+
CurrentHotkey = new HotkeyModel(Hotkey);
112209
}
113210

114211
private static bool CheckHotkeyAvailability(HotkeyModel hotkey, bool validateKeyGesture) =>
@@ -133,7 +230,7 @@ private async Task OpenHotkeyDialog()
133230
HotKeyMapper.RemoveHotkey(Hotkey);
134231
}
135232

136-
var dialog = new HotkeyControlDialog(Hotkey, DefaultHotkey, HotkeySettings, WindowTitle);
233+
var dialog = new HotkeyControlDialog(Hotkey, DefaultHotkey, WindowTitle);
137234
await dialog.ShowAsync();
138235
switch (dialog.ResultType)
139236
{

Flow.Launcher/HotkeyControlDialog.xaml.cs

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,11 @@
44
using System.Windows;
55
using System.Windows.Input;
66
using ChefKeys;
7+
using CommunityToolkit.Mvvm.DependencyInjection;
78
using Flow.Launcher.Core.Resource;
89
using Flow.Launcher.Helper;
910
using Flow.Launcher.Infrastructure.Hotkey;
11+
using Flow.Launcher.Infrastructure.UserSettings;
1012
using Flow.Launcher.Plugin;
1113
using ModernWpf.Controls;
1214

@@ -16,7 +18,7 @@ namespace Flow.Launcher;
1618

1719
public partial class HotkeyControlDialog : ContentDialog
1820
{
19-
private IHotkeySettings _hotkeySettings;
21+
private static readonly IHotkeySettings _hotkeySettings = Ioc.Default.GetRequiredService<Settings>();
2022
private Action? _overwriteOtherHotkey;
2123
private string DefaultHotkey { get; }
2224
public string WindowTitle { get; }
@@ -36,7 +38,7 @@ public enum EResultType
3638

3739
private static bool isOpenFlowHotkey;
3840

39-
public HotkeyControlDialog(string hotkey, string defaultHotkey, IHotkeySettings hotkeySettings, string windowTitle = "")
41+
public HotkeyControlDialog(string hotkey, string defaultHotkey, string windowTitle = "")
4042
{
4143
WindowTitle = windowTitle switch
4244
{
@@ -45,7 +47,6 @@ public HotkeyControlDialog(string hotkey, string defaultHotkey, IHotkeySettings
4547
};
4648
DefaultHotkey = defaultHotkey;
4749
CurrentHotkey = new HotkeyModel(hotkey);
48-
_hotkeySettings = hotkeySettings;
4950
SetKeysToDisplay(CurrentHotkey);
5051

5152
InitializeComponent();

Flow.Launcher/Resources/Pages/WelcomePage2.xaml

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -114,8 +114,7 @@
114114
Margin="0,8,0,0"
115115
ChangeHotkey="{Binding SetTogglingHotkeyCommand}"
116116
DefaultHotkey="Alt+Space"
117-
Hotkey="{Binding Settings.Hotkey}"
118-
HotkeySettings="{Binding Settings}"
117+
Type="Hotkey"
119118
ValidateKeyGesture="True"
120119
WindowTitle="{DynamicResource flowlauncherHotkey}" />
121120
</StackPanel>

Flow.Launcher/SettingPages/Views/SettingsPaneHotkey.xaml

Lines changed: 14 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -34,8 +34,7 @@
3434
<flowlauncher:HotkeyControl
3535
ChangeHotkey="{Binding SetTogglingHotkeyCommand}"
3636
DefaultHotkey="Alt+Space"
37-
Hotkey="{Binding Settings.Hotkey}"
38-
HotkeySettings="{Binding Settings}"
37+
Type="Hotkey"
3938
ValidateKeyGesture="True"
4039
WindowTitle="{DynamicResource flowlauncherHotkey}" />
4140
</cc:Card>
@@ -46,8 +45,7 @@
4645
Sub="{DynamicResource previewHotkeyToolTip}">
4746
<flowlauncher:HotkeyControl
4847
DefaultHotkey="F1"
49-
Hotkey="{Binding Settings.PreviewHotkey}"
50-
HotkeySettings="{Binding Settings}"
48+
Type="PreviewHotkey"
5149
ValidateKeyGesture="False"
5250
WindowTitle="{DynamicResource previewHotkey}" />
5351
</cc:Card>
@@ -105,8 +103,7 @@
105103
Type="Inside">
106104
<flowlauncher:HotkeyControl
107105
DefaultHotkey="Ctrl+I"
108-
Hotkey="{Binding Settings.OpenContextMenuHotkey}"
109-
HotkeySettings="{Binding Settings}"
106+
Type="OpenContextMenuHotkey"
110107
ValidateKeyGesture="False" />
111108
</cc:Card>
112109
<cc:Card
@@ -127,8 +124,7 @@
127124
Type="Inside">
128125
<flowlauncher:HotkeyControl
129126
DefaultHotkey="Ctrl+I"
130-
Hotkey="{Binding Settings.SettingWindowHotkey}"
131-
HotkeySettings="{Binding Settings}"
127+
Type="SettingWindowHotkey"
132128
ValidateKeyGesture="False" />
133129
</cc:Card>
134130
<cc:Card
@@ -149,8 +145,7 @@
149145
Type="Inside">
150146
<flowlauncher:HotkeyControl
151147
DefaultHotkey="Alt+Up"
152-
Hotkey="{Binding Settings.CycleHistoryUpHotkey}"
153-
HotkeySettings="{Binding Settings}"
148+
Type="CycleHistoryUpHotkey"
154149
ValidateKeyGesture="False" />
155150
</cc:Card>
156151
<cc:Card
@@ -159,8 +154,7 @@
159154
Type="Inside">
160155
<flowlauncher:HotkeyControl
161156
DefaultHotkey="Alt+Down"
162-
Hotkey="{Binding Settings.CycleHistoryDownHotkey}"
163-
HotkeySettings="{Binding Settings}"
157+
Type="CycleHistoryDownHotkey"
164158
ValidateKeyGesture="False" />
165159
</cc:Card>
166160
<cc:Card
@@ -176,8 +170,7 @@
176170
Type="Inside">
177171
<flowlauncher:HotkeyControl
178172
DefaultHotkey=""
179-
Hotkey="{Binding Settings.SelectPrevPageHotkey}"
180-
HotkeySettings="{Binding Settings}"
173+
Type="SelectPrevPageHotkey"
181174
ValidateKeyGesture="False" />
182175
</cc:Card>
183176
<cc:Card
@@ -186,8 +179,7 @@
186179
Type="Inside">
187180
<flowlauncher:HotkeyControl
188181
DefaultHotkey=""
189-
Hotkey="{Binding Settings.SelectNextPageHotkey}"
190-
HotkeySettings="{Binding Settings}"
182+
Type="SelectNextPageHotkey"
191183
ValidateKeyGesture="False" />
192184
</cc:Card>
193185

@@ -221,8 +213,7 @@
221213
<cc:ExCard.SideContent>
222214
<flowlauncher:HotkeyControl
223215
DefaultHotkey="Ctrl+Tab"
224-
Hotkey="{Binding Settings.AutoCompleteHotkey}"
225-
HotkeySettings="{Binding Settings}"
216+
Type="AutoCompleteHotkey"
226217
ValidateKeyGesture="False" />
227218
</cc:ExCard.SideContent>
228219
<cc:Card
@@ -231,8 +222,7 @@
231222
Type="InsideFit">
232223
<flowlauncher:HotkeyControl
233224
DefaultHotkey=""
234-
Hotkey="{Binding Settings.AutoCompleteHotkey2}"
235-
HotkeySettings="{Binding Settings}"
225+
Type="AutoCompleteHotkey2"
236226
ValidateKeyGesture="False" />
237227
</cc:Card>
238228
</cc:ExCard>
@@ -244,8 +234,7 @@
244234
<cc:ExCard.SideContent>
245235
<flowlauncher:HotkeyControl
246236
DefaultHotkey="Shift+Tab"
247-
Hotkey="{Binding Settings.SelectPrevItemHotkey}"
248-
HotkeySettings="{Binding Settings}"
237+
Type="SelectPrevItemHotkey"
249238
ValidateKeyGesture="False" />
250239
</cc:ExCard.SideContent>
251240
<cc:Card
@@ -254,8 +243,7 @@
254243
Type="InsideFit">
255244
<flowlauncher:HotkeyControl
256245
DefaultHotkey=""
257-
Hotkey="{Binding Settings.SelectPrevItemHotkey2}"
258-
HotkeySettings="{Binding Settings}"
246+
Type="SelectPrevItemHotkey2"
259247
ValidateKeyGesture="False" />
260248
</cc:Card>
261249
</cc:ExCard>
@@ -267,8 +255,7 @@
267255
<cc:ExCard.SideContent>
268256
<flowlauncher:HotkeyControl
269257
DefaultHotkey="Tab"
270-
Hotkey="{Binding Settings.SelectNextItemHotkey}"
271-
HotkeySettings="{Binding Settings}"
258+
Type="SelectNextItemHotkey"
272259
ValidateKeyGesture="False" />
273260
</cc:ExCard.SideContent>
274261
<cc:Card
@@ -277,8 +264,7 @@
277264
Type="InsideFit">
278265
<flowlauncher:HotkeyControl
279266
DefaultHotkey=""
280-
Hotkey="{Binding Settings.SelectNextItemHotkey2}"
281-
HotkeySettings="{Binding Settings}"
267+
Type="SelectNextItemHotkey2"
282268
ValidateKeyGesture="False" />
283269
</cc:Card>
284270
</cc:ExCard>

0 commit comments

Comments
 (0)