Skip to content

Commit 9b28161

Browse files
authored
Merge pull request #3310 from Jack251970/welcome_backspace_issue
Fix Welcome Backspace Issue & Fix Hotkey Control Settings Null Exception
2 parents 424b575 + 7868703 commit 9b28161

16 files changed

+285
-138
lines changed

Flow.Launcher/App.xaml.cs

+1
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,7 @@ public App()
6464
.AddSingleton<IPublicAPI, PublicAPIInstance>()
6565
.AddSingleton<MainViewModel>()
6666
.AddSingleton<Theme>()
67+
.AddSingleton<WelcomeViewModel>()
6768
).Build();
6869
Ioc.Default.ConfigureServices(host.Services);
6970
}

Flow.Launcher/CustomQueryHotkeySetting.xaml

-1
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

+118-21
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 constructed 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+
private 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

+4-3
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/MainWindow.xaml.cs

+1-1
Original file line numberDiff line numberDiff line change
@@ -445,7 +445,7 @@ private void CheckFirstLaunch()
445445

446446
private void OpenWelcomeWindow()
447447
{
448-
var WelcomeWindow = new WelcomeWindow(_settings);
448+
var WelcomeWindow = new WelcomeWindow();
449449
WelcomeWindow.Show();
450450
}
451451

Flow.Launcher/Resources/Pages/WelcomePage1.xaml.cs

+8-7
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,20 @@
1-
using System;
2-
using System.Collections.Generic;
1+
using System.Collections.Generic;
32
using System.Windows.Navigation;
43
using Flow.Launcher.Infrastructure.UserSettings;
54
using Flow.Launcher.Core.Resource;
5+
using CommunityToolkit.Mvvm.DependencyInjection;
6+
using Flow.Launcher.ViewModel;
67

78
namespace Flow.Launcher.Resources.Pages
89
{
910
public partial class WelcomePage1
1011
{
1112
protected override void OnNavigatedTo(NavigationEventArgs e)
1213
{
13-
if (e.ExtraData is Settings settings)
14-
Settings = settings;
15-
else
16-
throw new ArgumentException("Unexpected Navigation Parameter for Settings");
14+
Settings = Ioc.Default.GetRequiredService<Settings>();
15+
// Sometimes the navigation is not triggered by button click,
16+
// so we need to reset the page number
17+
Ioc.Default.GetRequiredService<WelcomeViewModel>().PageNum = 1;
1718
InitializeComponent();
1819
}
1920
private Internationalization _translater => InternationalizationManager.Instance;
@@ -37,4 +38,4 @@ public string CustomLanguage
3738
}
3839

3940
}
40-
}
41+
}

Flow.Launcher/Resources/Pages/WelcomePage2.xaml

+1-2
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/Resources/Pages/WelcomePage2.xaml.cs

+5-6
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
using Flow.Launcher.Helper;
22
using Flow.Launcher.Infrastructure.Hotkey;
33
using Flow.Launcher.Infrastructure.UserSettings;
4-
using System;
54
using System.Windows.Navigation;
65
using CommunityToolkit.Mvvm.Input;
76
using Flow.Launcher.ViewModel;
87
using System.Windows.Media;
8+
using CommunityToolkit.Mvvm.DependencyInjection;
99

1010
namespace Flow.Launcher.Resources.Pages
1111
{
@@ -15,11 +15,10 @@ public partial class WelcomePage2
1515

1616
protected override void OnNavigatedTo(NavigationEventArgs e)
1717
{
18-
if (e.ExtraData is Settings settings)
19-
Settings = settings;
20-
else
21-
throw new ArgumentException("Unexpected Parameter setting.");
22-
18+
Settings = Ioc.Default.GetRequiredService<Settings>();
19+
// Sometimes the navigation is not triggered by button click,
20+
// so we need to reset the page number
21+
Ioc.Default.GetRequiredService<WelcomeViewModel>().PageNum = 2;
2322
InitializeComponent();
2423
}
2524

Flow.Launcher/Resources/Pages/WelcomePage3.xaml.cs

+7-6
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,18 @@
1-
using System;
2-
using System.Windows.Navigation;
1+
using System.Windows.Navigation;
2+
using CommunityToolkit.Mvvm.DependencyInjection;
33
using Flow.Launcher.Infrastructure.UserSettings;
4+
using Flow.Launcher.ViewModel;
45

56
namespace Flow.Launcher.Resources.Pages
67
{
78
public partial class WelcomePage3
89
{
910
protected override void OnNavigatedTo(NavigationEventArgs e)
1011
{
11-
if (e.ExtraData is Settings settings)
12-
Settings = settings;
13-
else if(Settings is null)
14-
throw new ArgumentException("Unexpected Navigation Parameter for Settings");
12+
Settings = Ioc.Default.GetRequiredService<Settings>();
13+
// Sometimes the navigation is not triggered by button click,
14+
// so we need to reset the page number
15+
Ioc.Default.GetRequiredService<WelcomeViewModel>().PageNum = 3;
1516
InitializeComponent();
1617
}
1718

Flow.Launcher/Resources/Pages/WelcomePage4.xaml.cs

+7-6
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
1-
using Flow.Launcher.Infrastructure.UserSettings;
2-
using System;
1+
using CommunityToolkit.Mvvm.DependencyInjection;
2+
using Flow.Launcher.Infrastructure.UserSettings;
3+
using Flow.Launcher.ViewModel;
34
using System.Windows.Navigation;
45

56
namespace Flow.Launcher.Resources.Pages
@@ -8,10 +9,10 @@ public partial class WelcomePage4
89
{
910
protected override void OnNavigatedTo(NavigationEventArgs e)
1011
{
11-
if (e.ExtraData is Settings settings)
12-
Settings = settings;
13-
else
14-
throw new ArgumentException("Unexpected Navigation Parameter for Settings");
12+
Settings = Ioc.Default.GetRequiredService<Settings>();
13+
// Sometimes the navigation is not triggered by button click,
14+
// so we need to reset the page number
15+
Ioc.Default.GetRequiredService<WelcomeViewModel>().PageNum = 4;
1516
InitializeComponent();
1617
}
1718

Flow.Launcher/Resources/Pages/WelcomePage5.xaml.cs

+7-6
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
1-
using System;
2-
using System.Windows;
1+
using System.Windows;
32
using System.Windows.Navigation;
43
using Flow.Launcher.Infrastructure.UserSettings;
54
using Microsoft.Win32;
65
using Flow.Launcher.Infrastructure;
6+
using CommunityToolkit.Mvvm.DependencyInjection;
7+
using Flow.Launcher.ViewModel;
78

89
namespace Flow.Launcher.Resources.Pages
910
{
@@ -15,10 +16,10 @@ public partial class WelcomePage5
1516

1617
protected override void OnNavigatedTo(NavigationEventArgs e)
1718
{
18-
if (e.ExtraData is Settings settings)
19-
Settings = settings;
20-
else
21-
throw new ArgumentException("Unexpected Navigation Parameter for Settings");
19+
Settings = Ioc.Default.GetRequiredService<Settings>();
20+
// Sometimes the navigation is not triggered by button click,
21+
// so we need to reset the page number
22+
Ioc.Default.GetRequiredService<WelcomeViewModel>().PageNum = 5;
2223
InitializeComponent();
2324
}
2425

0 commit comments

Comments
 (0)