fix some issues and change structure and verification place

This commit is contained in:
Hongtao Zhang 2024-04-13 12:49:57 -05:00
parent 22021c10ee
commit c9b5159ffd
3 changed files with 35 additions and 47 deletions

View file

@ -34,9 +34,9 @@
VerticalAlignment="Center" VerticalAlignment="Center"
FontSize="13" FontSize="13"
FontWeight="SemiBold" FontWeight="SemiBold"
Foreground="{DynamicResource Color05B}" Foreground="{Binding MessageColor}"
Text="{DynamicResource flowlauncherPressHotkey}" Text="{Binding Message}"
Visibility="Visible" /> Visibility="{Binding MessageVisibility}"/>
</Border> </Border>
</Popup> </Popup>
<Grid> <Grid>

View file

@ -2,6 +2,7 @@
using System.Collections; using System.Collections;
using System.Collections.Generic; using System.Collections.Generic;
using System.Collections.ObjectModel; using System.Collections.ObjectModel;
using System.ComponentModel;
using System.Linq; using System.Linq;
using System.Threading.Tasks; using System.Threading.Tasks;
using System.Windows; using System.Windows;
@ -32,12 +33,10 @@ namespace Flow.Launcher.ViewModel
public string DefaultHotkey { get; init; } public string DefaultHotkey { get; init; }
private void SetMessage(bool hotkeyAvailable) private void SetMessage(string messageKey, bool error)
{ {
Message = hotkeyAvailable Message = InternationalizationManager.Instance.GetTranslation(messageKey);
? InternationalizationManager.Instance.GetTranslation("success") MessageColor = error ? new SolidColorBrush(Colors.Green) : new SolidColorBrush(Colors.Red);
: InternationalizationManager.Instance.GetTranslation("hotkeyUnavailable");
MessageColor = hotkeyAvailable ? new SolidColorBrush(Colors.Green) : new SolidColorBrush(Colors.Red);
MessageVisibility = true; MessageVisibility = true;
} }
@ -47,6 +46,7 @@ namespace Flow.Launcher.ViewModel
Action<HotkeyModel>? hotkeyDelegate = null) Action<HotkeyModel>? hotkeyDelegate = null)
{ {
Hotkey = hotkey; Hotkey = hotkey;
CurrentHotkey = new HotkeyModel(hotkey);
DefaultHotkey = defaultHotkey; DefaultHotkey = defaultHotkey;
ValidateKeyGesture = validateKeyGesture; ValidateKeyGesture = validateKeyGesture;
HotkeyDelegate = hotkeyDelegate; HotkeyDelegate = hotkeyDelegate;
@ -104,8 +104,16 @@ namespace Flow.Launcher.ViewModel
{ {
Recording = false; Recording = false;
if (string.IsNullOrEmpty(Hotkey)) try
{ {
var converter = new KeyGestureConverter();
var key = (KeyGesture)converter.ConvertFromString(CurrentHotkey.ToString())!;
}
catch (Exception e) when (e is NotSupportedException or InvalidEnumArgumentException)
{
SetMessage("Hotkey Invalid", true);
CurrentHotkey = new HotkeyModel(Hotkey);
SetKeysToDisplay(KeysToDisplay, CurrentHotkey);
return; return;
} }
@ -120,8 +128,9 @@ namespace Flow.Launcher.ViewModel
if (!string.IsNullOrEmpty(Hotkey)) if (!string.IsNullOrEmpty(Hotkey))
HotKeyMapper.RemoveHotkey(Hotkey); HotKeyMapper.RemoveHotkey(Hotkey);
Hotkey = DefaultHotkey; Hotkey = DefaultHotkey;
CurrentHotkey = new HotkeyModel(Hotkey);
SetKeysToDisplay(KeysToDisplay, Hotkey.Split(KeySeparator)); SetKeysToDisplay(KeysToDisplay, CurrentHotkey);
await SetHotkeyAsync(Hotkey); await SetHotkeyAsync(Hotkey);
} }
@ -138,14 +147,14 @@ namespace Flow.Launcher.ViewModel
{ {
bool hotkeyAvailable = CheckHotkeyAvailability(keyModel, ValidateKeyGesture); bool hotkeyAvailable = CheckHotkeyAvailability(keyModel, ValidateKeyGesture);
CurrentHotkeyAvailable = hotkeyAvailable; CurrentHotkeyAvailable = hotkeyAvailable;
SetMessage(hotkeyAvailable); SetMessage(hotkeyAvailable ? "success" : "hotkeyUnavailable", !hotkeyAvailable);
HotkeyDelegate?.Invoke(keyModel); HotkeyDelegate?.Invoke(keyModel);
if (CurrentHotkeyAvailable) if (CurrentHotkeyAvailable)
{ {
CurrentHotkey = keyModel; CurrentHotkey = keyModel;
Hotkey = keyModel.ToString(); Hotkey = keyModel.ToString();
SetKeysToDisplay(KeysToDisplay, Hotkey.Split(KeySeparator)); SetKeysToDisplay(KeysToDisplay, CurrentHotkey);
} }
} }
else else
@ -165,7 +174,7 @@ namespace Flow.Launcher.ViewModel
SetKeysToDisplay(KeysToDisplay, new List<string>()); SetKeysToDisplay(KeysToDisplay, new List<string>());
} }
private void SetKeysToDisplay(HotkeyModel hotkey) private void SetKeysToDisplay(ICollection<string> container, HotkeyModel hotkey)
{ {
KeysToDisplay.Clear(); KeysToDisplay.Clear();
if (hotkey.Alt) if (hotkey.Alt)
@ -222,7 +231,7 @@ namespace Flow.Launcher.ViewModel
public void KeyDown(HotkeyModel hotkeyModel) public void KeyDown(HotkeyModel hotkeyModel)
{ {
CurrentHotkey = hotkeyModel; CurrentHotkey = hotkeyModel;
SetKeysToDisplay(KeysToDisplay, hotkeyModel.ToString().Split(KeySeparator)); SetKeysToDisplay(KeysToDisplay, CurrentHotkey);
} }
} }
} }

View file

@ -630,45 +630,24 @@ namespace Flow.Launcher.ViewModel
public string OpenResultCommandModifiers => Settings.OpenResultModifiers; public string OpenResultCommandModifiers => Settings.OpenResultModifiers;
public string PreviewHotkey public string VerifyOrSetDefaultHotkey(string hotkey, string defaultHotkey)
{ {
get try
{ {
// TODO try to patch issue #1755 var converter = new KeyGestureConverter();
// Added in v1.14.0, remove after v1.16.0. var key = (KeyGesture)converter.ConvertFromString(hotkey);
try
{
var converter = new KeyGestureConverter();
var key = (KeyGesture)converter.ConvertFromString(Settings.PreviewHotkey);
}
catch (Exception e) when (e is NotSupportedException || e is InvalidEnumArgumentException)
{
Settings.PreviewHotkey = "F1";
}
return Settings.PreviewHotkey;
} }
catch (Exception e) when (e is NotSupportedException || e is InvalidEnumArgumentException)
{
return defaultHotkey;
}
return hotkey;
} }
public string AutoCompleteHotkey public string PreviewHotkey => VerifyOrSetDefaultHotkey(Settings.PreviewHotkey, "F1");
{
get
{
// TODO try to patch issue #1755
// Added in v1.14.0, remove after v1.16.0.
try
{
var converter = new KeyGestureConverter();
var key = (KeyGesture)converter.ConvertFromString(Settings.AutoCompleteHotkey);
}
catch (Exception e) when (e is NotSupportedException || e is InvalidEnumArgumentException)
{
Settings.AutoCompleteHotkey = "F2";
}
return Settings.AutoCompleteHotkey; public string AutoCompleteHotkey => VerifyOrSetDefaultHotkey(Settings.AutoCompleteHotkey, "Tab");
}
}
public string Image => Constant.QueryTextBoxIconImagePath; public string Image => Constant.QueryTextBoxIconImagePath;