Refactor Code (Move all operation for mainviewmodel to ToggleFlowLauncher)

This commit is contained in:
Kevin Zhang 2021-10-19 22:10:37 -05:00
parent 364845a14c
commit 9a53015786
3 changed files with 28 additions and 51 deletions

View file

@ -21,10 +21,15 @@ namespace Flow.Launcher.Helper
mainViewModel = mainVM;
settings = mainViewModel._settings;
SetHotkey(settings.Hotkey, mainViewModel.OnHotkey);
SetHotkey(settings.Hotkey, OnToggleHotkey);
LoadCustomPluginHotkey();
}
internal static void OnToggleHotkey(object sender, HotkeyEventArgs args)
{
mainViewModel.ToggleFlowLauncher();
}
private static void SetHotkey(string hotkeyStr, EventHandler<HotkeyEventArgs> action)
{
var hotkey = new HotkeyModel(hotkeyStr);

View file

@ -127,7 +127,7 @@ namespace Flow.Launcher
if (HotkeyControl.CurrentHotkeyAvailable)
{
HotKeyMapper.SetHotkey(HotkeyControl.CurrentHotkey, mainViewModel.OnHotkey);
HotKeyMapper.SetHotkey(HotkeyControl.CurrentHotkey, HotKeyMapper.OnToggleHotkey);
HotKeyMapper.RemoveHotkey(settings.Hotkey);
settings.Hotkey = HotkeyControl.CurrentHotkey.ToString();
}

View file

@ -52,8 +52,6 @@ namespace Flow.Launcher.ViewModel
private ChannelWriter<ResultsForUpdate> _resultsUpdateChannelWriter;
private Task _resultsViewUpdateTask;
#endregion
#region Constructor
@ -112,7 +110,9 @@ namespace Flow.Launcher.ViewModel
}
Log.Error("MainViewModel", "Unexpected ResultViewUpdate ends");
};
}
;
void continueAction(Task t)
{
@ -693,7 +693,7 @@ namespace Flow.Launcher.ViewModel
OpenResultCommandModifiers = _settings.OpenResultModifiers ?? DefaultOpenResultModifiers;
}
public void ToggleFlowLauncher()
public async void ToggleFlowLauncher()
{
if (MainWindowVisibility != Visibility.Visible)
{
@ -701,25 +701,24 @@ namespace Flow.Launcher.ViewModel
}
else
{
if (_settings.LastQueryMode == LastQueryMode.Empty)
switch (_settings.LastQueryMode)
{
Application.Current.MainWindow.Opacity = 0; // Trick for no delay
ClearQueryCommand.Execute(null);
Task.Run(() =>
{
Thread.Sleep(100);
Application.Current.Dispatcher.BeginInvoke(DispatcherPriority.Normal, new Action(() =>
{
MainWindowVisibility = Visibility.Collapsed;
Application.Current.MainWindow.Opacity = 1;
}));
});
}
else
{
MainWindowVisibility = Visibility.Collapsed;
case LastQueryMode.Empty:
ChangeQueryText(string.Empty);
Application.Current.MainWindow.Opacity = 0; // Trick for no delay
await Task.Delay(100);
Application.Current.MainWindow.Opacity = 1;
break;
case LastQueryMode.Preserved:
LastQuerySelected = true;
break;
case LastQueryMode.Selected:
LastQuerySelected = false;
break;
default:
throw new ArgumentException($"wrong LastQueryMode: <{_settings.LastQueryMode}>");
}
MainWindowVisibility = Visibility.Collapsed;
}
}
@ -731,40 +730,13 @@ namespace Flow.Launcher.ViewModel
}
}
#endregion
public void OnHotkey(object sender, HotkeyEventArgs e)
{
if (!ShouldIgnoreHotkeys())
{
if (_settings.LastQueryMode == LastQueryMode.Empty)
{
ChangeQueryText(string.Empty);
}
else if (_settings.LastQueryMode == LastQueryMode.Preserved)
{
LastQuerySelected = true;
}
else if (_settings.LastQueryMode == LastQueryMode.Selected)
{
LastQuerySelected = false;
}
else
{
throw new ArgumentException($"wrong LastQueryMode: <{_settings.LastQueryMode}>");
}
ToggleFlowLauncher();
}
}
/// <summary>
/// Checks if Flow Launcher should ignore any hotkeys
/// </summary>
public bool ShouldIgnoreHotkeys()
public bool ShouldIgnoreHotkeys()
{
return _settings.IgnoreHotkeysOnFullscreen && WindowsInteropHelper.IsWindowFullscreen();
}