Skip to content

Commit 9a53015

Browse files
committed
Refactor Code (Move all operation for mainviewmodel to ToggleFlowLauncher)
1 parent 364845a commit 9a53015

File tree

3 files changed

+28
-51
lines changed

3 files changed

+28
-51
lines changed

Flow.Launcher/Helper/HotKeyMapper.cs

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,10 +21,15 @@ internal static void Initialize(MainViewModel mainVM)
2121
mainViewModel = mainVM;
2222
settings = mainViewModel._settings;
2323

24-
SetHotkey(settings.Hotkey, mainViewModel.OnHotkey);
24+
SetHotkey(settings.Hotkey, OnToggleHotkey);
2525
LoadCustomPluginHotkey();
2626
}
2727

28+
internal static void OnToggleHotkey(object sender, HotkeyEventArgs args)
29+
{
30+
mainViewModel.ToggleFlowLauncher();
31+
}
32+
2833
private static void SetHotkey(string hotkeyStr, EventHandler<HotkeyEventArgs> action)
2934
{
3035
var hotkey = new HotkeyModel(hotkeyStr);

Flow.Launcher/SettingWindow.xaml.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -127,7 +127,7 @@ void OnHotkeyChanged(object sender, EventArgs e)
127127
if (HotkeyControl.CurrentHotkeyAvailable)
128128
{
129129

130-
HotKeyMapper.SetHotkey(HotkeyControl.CurrentHotkey, mainViewModel.OnHotkey);
130+
HotKeyMapper.SetHotkey(HotkeyControl.CurrentHotkey, HotKeyMapper.OnToggleHotkey);
131131
HotKeyMapper.RemoveHotkey(settings.Hotkey);
132132
settings.Hotkey = HotkeyControl.CurrentHotkey.ToString();
133133
}

Flow.Launcher/ViewModel/MainViewModel.cs

Lines changed: 21 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -52,8 +52,6 @@ public class MainViewModel : BaseModel, ISavable
5252
private ChannelWriter<ResultsForUpdate> _resultsUpdateChannelWriter;
5353
private Task _resultsViewUpdateTask;
5454

55-
56-
5755
#endregion
5856

5957
#region Constructor
@@ -112,7 +110,9 @@ async Task updateAction()
112110
}
113111

114112
Log.Error("MainViewModel", "Unexpected ResultViewUpdate ends");
115-
};
113+
}
114+
115+
;
116116

117117
void continueAction(Task t)
118118
{
@@ -693,33 +693,32 @@ private void SetOpenResultModifiers()
693693
OpenResultCommandModifiers = _settings.OpenResultModifiers ?? DefaultOpenResultModifiers;
694694
}
695695

696-
public void ToggleFlowLauncher()
696+
public async void ToggleFlowLauncher()
697697
{
698698
if (MainWindowVisibility != Visibility.Visible)
699699
{
700700
MainWindowVisibility = Visibility.Visible;
701701
}
702702
else
703703
{
704-
if (_settings.LastQueryMode == LastQueryMode.Empty)
704+
switch (_settings.LastQueryMode)
705705
{
706-
Application.Current.MainWindow.Opacity = 0; // Trick for no delay
707-
ClearQueryCommand.Execute(null);
708-
Task.Run(() =>
709-
{
710-
Thread.Sleep(100);
711-
Application.Current.Dispatcher.BeginInvoke(DispatcherPriority.Normal, new Action(() =>
712-
{
713-
MainWindowVisibility = Visibility.Collapsed;
714-
Application.Current.MainWindow.Opacity = 1;
715-
}));
716-
});
717-
}
718-
else
719-
{
720-
721-
MainWindowVisibility = Visibility.Collapsed;
706+
case LastQueryMode.Empty:
707+
ChangeQueryText(string.Empty);
708+
Application.Current.MainWindow.Opacity = 0; // Trick for no delay
709+
await Task.Delay(100);
710+
Application.Current.MainWindow.Opacity = 1;
711+
break;
712+
case LastQueryMode.Preserved:
713+
LastQuerySelected = true;
714+
break;
715+
case LastQueryMode.Selected:
716+
LastQuerySelected = false;
717+
break;
718+
default:
719+
throw new ArgumentException($"wrong LastQueryMode: <{_settings.LastQueryMode}>");
722720
}
721+
MainWindowVisibility = Visibility.Collapsed;
723722
}
724723
}
725724

@@ -731,40 +730,13 @@ public void Hide()
731730
}
732731
}
733732

734-
735733
#endregion
736734

737-
public void OnHotkey(object sender, HotkeyEventArgs e)
738-
{
739-
if (!ShouldIgnoreHotkeys())
740-
{
741-
742-
if (_settings.LastQueryMode == LastQueryMode.Empty)
743-
{
744-
ChangeQueryText(string.Empty);
745-
}
746-
else if (_settings.LastQueryMode == LastQueryMode.Preserved)
747-
{
748-
LastQuerySelected = true;
749-
}
750-
else if (_settings.LastQueryMode == LastQueryMode.Selected)
751-
{
752-
LastQuerySelected = false;
753-
}
754-
else
755-
{
756-
throw new ArgumentException($"wrong LastQueryMode: <{_settings.LastQueryMode}>");
757-
}
758-
759-
ToggleFlowLauncher();
760-
}
761-
}
762-
763735

764736
/// <summary>
765737
/// Checks if Flow Launcher should ignore any hotkeys
766738
/// </summary>
767-
public bool ShouldIgnoreHotkeys()
739+
public bool ShouldIgnoreHotkeys()
768740
{
769741
return _settings.IgnoreHotkeysOnFullscreen && WindowsInteropHelper.IsWindowFullscreen();
770742
}

0 commit comments

Comments
 (0)