Skip to content

Commit 3ff7566

Browse files
Use databinding for preview hotkey
1 parent 979aa3d commit 3ff7566

File tree

3 files changed

+37
-15
lines changed

3 files changed

+37
-15
lines changed
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
using System;
2+
using System.Globalization;
3+
using System.Windows.Data;
4+
using System.Windows.Input;
5+
6+
namespace Flow.Launcher.Converters
7+
{
8+
class StringToKeyBindingConverter : IValueConverter
9+
{
10+
public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
11+
{
12+
var mode = parameter as string;
13+
var hotkeyStr = value as string;
14+
var converter = new KeyGestureConverter();
15+
var key = (KeyGesture)converter.ConvertFromString(hotkeyStr);
16+
if (mode == "key")
17+
{
18+
return key.Key;
19+
}
20+
else if (mode == "modifiers")
21+
{
22+
return key.Modifiers;
23+
}
24+
return null;
25+
}
26+
27+
public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
28+
{
29+
throw new NotImplementedException();
30+
}
31+
}
32+
}

Flow.Launcher/MainWindow.xaml

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@
4040
<converters:BoolToVisibilityConverter x:Key="BoolToVisibilityConverter" />
4141
<converters:BoolToIMEConversionModeConverter x:Key="BoolToIMEConversionModeConverter"/>
4242
<converters:BoolToIMEStateConverter x:Key="BoolToIMEStateConverter"/>
43+
<converters:StringToKeyBindingConverter x:Key="StringToKeyBindingConverter"/>
4344
</Window.Resources>
4445
<Window.InputBindings>
4546
<KeyBinding Key="Escape" Command="{Binding EscCommand}" />
@@ -181,9 +182,9 @@
181182
Command="{Binding ToggleGameModeCommand}"
182183
Modifiers="Ctrl"/>
183184
<KeyBinding
184-
Key="{Binding TogglePreviewHotkey}"
185+
Key="{Binding PreviewHotkey, Converter={StaticResource StringToKeyBindingConverter}, ConverterParameter='key'}"
185186
Command="{Binding TogglePreviewCommand}"
186-
Modifiers="{Binding TogglePreviewModifiers}"/>
187+
Modifiers="{Binding PreviewHotkey, Converter={StaticResource StringToKeyBindingConverter}, ConverterParameter='modifiers'}"/>
187188
</Window.InputBindings>
188189
<Grid>
189190
<Border MouseDown="OnMouseDown" Style="{DynamicResource WindowBorderStyle}">

Flow.Launcher/ViewModel/MainViewModel.cs

Lines changed: 2 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@ public MainViewModel(Settings settings)
7575
OnPropertyChanged(nameof(OpenResultCommandModifiers));
7676
break;
7777
case nameof(Settings.PreviewHotkey):
78-
UpdatePreviewHotkey();
78+
OnPropertyChanged(nameof(PreviewHotkey));
7979
break;
8080
}
8181
};
@@ -115,7 +115,6 @@ public MainViewModel(Settings settings)
115115
RegisterViewUpdate();
116116
RegisterResultsUpdatedEvent();
117117
_ = RegisterClockAndDateUpdateAsync();
118-
UpdatePreviewHotkey();
119118
}
120119

121120
private void RegisterViewUpdate()
@@ -584,9 +583,7 @@ public double MainWindowWidth
584583

585584
public string OpenResultCommandModifiers => Settings.OpenResultModifiers;
586585

587-
public Key TogglePreviewHotkey { get; set; }
588-
589-
public ModifierKeys TogglePreviewModifiers { get; set; }
586+
public string PreviewHotkey => Settings.PreviewHotkey;
590587

591588
public string Image => Constant.QueryTextBoxIconImagePath;
592589

@@ -1018,14 +1015,6 @@ public bool ShouldIgnoreHotkeys()
10181015
return Settings.IgnoreHotkeysOnFullscreen && WindowsInteropHelper.IsWindowFullscreen() || GameModeStatus;
10191016
}
10201017

1021-
private void UpdatePreviewHotkey()
1022-
{
1023-
var converter = new KeyGestureConverter();
1024-
var key = (KeyGesture)converter.ConvertFromString(Settings.PreviewHotkey);
1025-
TogglePreviewHotkey = key.Key;
1026-
TogglePreviewModifiers = key.Modifiers;
1027-
}
1028-
10291018
#endregion
10301019

10311020
#region Public Methods

0 commit comments

Comments
 (0)