Use databinding for preview hotkey

This commit is contained in:
Vic 2022-12-28 01:09:32 +08:00
parent 979aa3ddff
commit 3ff7566daa
3 changed files with 37 additions and 15 deletions

View file

@ -0,0 +1,32 @@
using System;
using System.Globalization;
using System.Windows.Data;
using System.Windows.Input;
namespace Flow.Launcher.Converters
{
class StringToKeyBindingConverter : IValueConverter
{
public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
{
var mode = parameter as string;
var hotkeyStr = value as string;
var converter = new KeyGestureConverter();
var key = (KeyGesture)converter.ConvertFromString(hotkeyStr);
if (mode == "key")
{
return key.Key;
}
else if (mode == "modifiers")
{
return key.Modifiers;
}
return null;
}
public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
{
throw new NotImplementedException();
}
}
}

View file

@ -40,6 +40,7 @@
<converters:BoolToVisibilityConverter x:Key="BoolToVisibilityConverter" />
<converters:BoolToIMEConversionModeConverter x:Key="BoolToIMEConversionModeConverter"/>
<converters:BoolToIMEStateConverter x:Key="BoolToIMEStateConverter"/>
<converters:StringToKeyBindingConverter x:Key="StringToKeyBindingConverter"/>
</Window.Resources>
<Window.InputBindings>
<KeyBinding Key="Escape" Command="{Binding EscCommand}" />
@ -181,9 +182,9 @@
Command="{Binding ToggleGameModeCommand}"
Modifiers="Ctrl"/>
<KeyBinding
Key="{Binding TogglePreviewHotkey}"
Key="{Binding PreviewHotkey, Converter={StaticResource StringToKeyBindingConverter}, ConverterParameter='key'}"
Command="{Binding TogglePreviewCommand}"
Modifiers="{Binding TogglePreviewModifiers}"/>
Modifiers="{Binding PreviewHotkey, Converter={StaticResource StringToKeyBindingConverter}, ConverterParameter='modifiers'}"/>
</Window.InputBindings>
<Grid>
<Border MouseDown="OnMouseDown" Style="{DynamicResource WindowBorderStyle}">

View file

@ -75,7 +75,7 @@ namespace Flow.Launcher.ViewModel
OnPropertyChanged(nameof(OpenResultCommandModifiers));
break;
case nameof(Settings.PreviewHotkey):
UpdatePreviewHotkey();
OnPropertyChanged(nameof(PreviewHotkey));
break;
}
};
@ -115,7 +115,6 @@ namespace Flow.Launcher.ViewModel
RegisterViewUpdate();
RegisterResultsUpdatedEvent();
_ = RegisterClockAndDateUpdateAsync();
UpdatePreviewHotkey();
}
private void RegisterViewUpdate()
@ -584,9 +583,7 @@ namespace Flow.Launcher.ViewModel
public string OpenResultCommandModifiers => Settings.OpenResultModifiers;
public Key TogglePreviewHotkey { get; set; }
public ModifierKeys TogglePreviewModifiers { get; set; }
public string PreviewHotkey => Settings.PreviewHotkey;
public string Image => Constant.QueryTextBoxIconImagePath;
@ -1018,14 +1015,6 @@ namespace Flow.Launcher.ViewModel
return Settings.IgnoreHotkeysOnFullscreen && WindowsInteropHelper.IsWindowFullscreen() || GameModeStatus;
}
private void UpdatePreviewHotkey()
{
var converter = new KeyGestureConverter();
var key = (KeyGesture)converter.ConvertFromString(Settings.PreviewHotkey);
TogglePreviewHotkey = key.Key;
TogglePreviewModifiers = key.Modifiers;
}
#endregion
#region Public Methods