Skip to content

Commit b88057c

Browse files
committed
Code quality & Async support for show / hide
1 parent 40bb4ef commit b88057c

File tree

1 file changed

+65
-71
lines changed

1 file changed

+65
-71
lines changed

Flow.Launcher/ViewModel/MainViewModel.cs

Lines changed: 65 additions & 71 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,14 @@
11
using System;
22
using System.Collections.Generic;
33
using System.ComponentModel;
4-
using System.Diagnostics;
54
using System.Globalization;
6-
using System.Windows.Input;
75
using System.Linq;
86
using System.Text;
97
using System.Threading;
108
using System.Threading.Channels;
119
using System.Threading.Tasks;
1210
using System.Windows;
11+
using System.Windows.Input;
1312
using System.Windows.Media;
1413
using System.Windows.Threading;
1514
using CommunityToolkit.Mvvm.DependencyInjection;
@@ -631,7 +630,15 @@ private void DecreaseMaxResult()
631630
/// <param name="isReQuery">Force query even when Query Text doesn't change</param>
632631
public void ChangeQueryText(string queryText, bool isReQuery = false)
633632
{
634-
Application.Current.Dispatcher.Invoke(() =>
633+
_ = ChangeQueryTextAsync(queryText, isReQuery);
634+
}
635+
636+
/// <summary>
637+
/// Async version of <see cref="ChangeQueryText"/>
638+
/// </summary>
639+
private async Task ChangeQueryTextAsync(string queryText, bool isReQuery = false)
640+
{
641+
await Application.Current.Dispatcher.InvokeAsync(async () =>
635642
{
636643
BackToQueryResults();
637644

@@ -645,7 +652,7 @@ public void ChangeQueryText(string queryText, bool isReQuery = false)
645652
}
646653
else if (isReQuery)
647654
{
648-
Query(isReQuery: true);
655+
await QueryAsync(isReQuery: true);
649656
}
650657

651658
QueryTextCursorMovedToEnd = true;
@@ -1017,10 +1024,15 @@ private bool QueryResultsPreviewed()
10171024
#region Query
10181025

10191026
private void Query(bool isReQuery = false)
1027+
{
1028+
_ = QueryAsync(isReQuery);
1029+
}
1030+
1031+
private async Task QueryAsync(bool isReQuery = false)
10201032
{
10211033
if (QueryResultsSelected())
10221034
{
1023-
_ = QueryResultsAsync(isReQuery);
1035+
await QueryResultsAsync(isReQuery);
10241036
}
10251037
else if (ContextMenuSelected())
10261038
{
@@ -1054,10 +1066,10 @@ private void QueryContextMenu()
10541066
(
10551067
r =>
10561068
{
1057-
var match = StringMatcher.FuzzySearch(query, r.Title);
1069+
var match = App.API.FuzzySearch(query, r.Title);
10581070
if (!match.IsSearchPrecisionScoreMet())
10591071
{
1060-
match = StringMatcher.FuzzySearch(query, r.SubTitle);
1072+
match = App.API.FuzzySearch(query, r.SubTitle);
10611073
}
10621074

10631075
if (!match.IsSearchPrecisionScoreMet()) return false;
@@ -1099,7 +1111,7 @@ private void QueryHistory()
10991111
Action = _ =>
11001112
{
11011113
SelectedResults = Results;
1102-
ChangeQueryText(h.Query);
1114+
App.API.ChangeQuery(h.Query);
11031115
return false;
11041116
}
11051117
};
@@ -1110,8 +1122,8 @@ private void QueryHistory()
11101122
{
11111123
var filtered = results.Where
11121124
(
1113-
r => StringMatcher.FuzzySearch(query, r.Title).IsSearchPrecisionScoreMet() ||
1114-
StringMatcher.FuzzySearch(query, r.SubTitle).IsSearchPrecisionScoreMet()
1125+
r => App.API.FuzzySearch(query, r.Title).IsSearchPrecisionScoreMet() ||
1126+
App.API.FuzzySearch(query, r.SubTitle).IsSearchPrecisionScoreMet()
11151127
).ToList();
11161128
History.AddResults(filtered, id);
11171129
}
@@ -1430,35 +1442,31 @@ public bool ShouldIgnoreHotkeys()
14301442

14311443
#region Public Methods
14321444

1433-
public void Show()
1445+
#pragma warning disable VSTHRD100 // Avoid async void methods
1446+
1447+
public async void Show()
14341448
{
1435-
Application.Current.Dispatcher.Invoke(() =>
1449+
await Application.Current.Dispatcher.InvokeAsync(() =>
14361450
{
14371451
if (Application.Current.MainWindow is MainWindow mainWindow)
14381452
{
14391453
// 📌 Remove DWM Cloak (Make the window visible normally)
14401454
Win32Helper.DWMSetCloakForWindow(mainWindow, false);
1441-
1442-
//Clock and SearchIcon hide when show situation
1443-
if(Settings.UseAnimation)
1444-
{
1445-
mainWindow.ClockPanel.Opacity = 0;
1446-
mainWindow.SearchIcon.Opacity = 0;
1447-
}
1448-
else
1449-
{
1450-
mainWindow.ClockPanel.Opacity = 1;
1451-
mainWindow.SearchIcon.Opacity = 1;
1452-
}
14531455

1454-
if (mainWindow.QueryTextBox.Text.Length != 0)
1456+
// Clock and SearchIcon hide when show situation
1457+
var opacity = Settings.UseAnimation ? 0.0 : 1.0;
1458+
mainWindow.ClockPanel.Opacity = opacity;
1459+
mainWindow.SearchIcon.Opacity = opacity;
1460+
1461+
if (QueryText.Length != 0)
14551462
{
14561463
mainWindow.ClockPanel.Visibility = Visibility.Collapsed;
14571464
}
14581465
else
14591466
{
14601467
mainWindow.ClockPanel.Visibility = Visibility.Visible;
14611468
}
1469+
14621470
if (PluginIconSource != null)
14631471
{
14641472
mainWindow.SearchIcon.Opacity = 0;
@@ -1467,30 +1475,28 @@ public void Show()
14671475
{
14681476
SearchIconVisibility = Visibility.Visible;
14691477
}
1478+
14701479
// 📌 Restore UI elements
14711480
//mainWindow.SearchIcon.Visibility = Visibility.Visible;
14721481
if (Settings.UseAnimation)
14731482
{
1474-
Application.Current.Dispatcher.BeginInvoke(() =>
1475-
mainWindow.WindowAnimation());
1483+
mainWindow.WindowAnimation();
14761484
}
14771485
}
1486+
}, DispatcherPriority.Render);
14781487

1479-
// Update WPF properties
1480-
MainWindowVisibility = Visibility.Visible;
1481-
MainWindowOpacity = 1;
1482-
MainWindowVisibilityStatus = true;
1483-
VisibilityChanged?.Invoke(this, new VisibilityChangedEventArgs { IsVisible = true });
1488+
// Update WPF properties
1489+
MainWindowVisibility = Visibility.Visible;
1490+
MainWindowOpacity = 1;
1491+
MainWindowVisibilityStatus = true;
1492+
VisibilityChanged?.Invoke(this, new VisibilityChangedEventArgs { IsVisible = true });
14841493

1485-
if (StartWithEnglishMode)
1486-
{
1487-
Win32Helper.SwitchToEnglishKeyboardLayout(true);
1488-
}
1489-
});
1494+
if (StartWithEnglishMode)
1495+
{
1496+
Win32Helper.SwitchToEnglishKeyboardLayout(true);
1497+
}
14901498
}
14911499

1492-
#pragma warning disable VSTHRD100 // Avoid async void methods
1493-
14941500
public async void Hide()
14951501
{
14961502
lastHistoryIndex = 1;
@@ -1505,59 +1511,47 @@ public async void Hide()
15051511
SelectedResults = Results;
15061512
}
15071513

1508-
// 📌 Immediately apply text reset + force UI update
1509-
/*if (Settings.LastQueryMode == LastQueryMode.Empty)
1510-
{
1511-
ChangeQueryText(string.Empty);
1512-
await Task.Delay(1); // Wait for one frame to ensure UI reflects changes
1513-
Application.Current.Dispatcher.Invoke(Application.Current.MainWindow.UpdateLayout); // Force UI update
1514-
}*/
1515-
15161514
switch (Settings.LastQueryMode)
15171515
{
15181516
case LastQueryMode.Empty:
1519-
ChangeQueryText(string.Empty);
1520-
await Task.Delay(1);
1517+
await ChangeQueryTextAsync(string.Empty);
15211518
break;
15221519
case LastQueryMode.Preserved:
15231520
case LastQueryMode.Selected:
1524-
LastQuerySelected = (Settings.LastQueryMode == LastQueryMode.Preserved);
1521+
LastQuerySelected = Settings.LastQueryMode == LastQueryMode.Preserved;
15251522
break;
1526-
15271523
case LastQueryMode.ActionKeywordPreserved:
15281524
case LastQueryMode.ActionKeywordSelected:
15291525
var newQuery = _lastQuery.ActionKeyword;
1526+
15301527
if (!string.IsNullOrEmpty(newQuery))
15311528
newQuery += " ";
1532-
ChangeQueryText(newQuery);
1529+
await ChangeQueryTextAsync(newQuery);
15331530

15341531
if (Settings.LastQueryMode == LastQueryMode.ActionKeywordSelected)
15351532
LastQuerySelected = false;
15361533
break;
15371534
}
15381535

1539-
if (Application.Current.MainWindow is MainWindow mainWindow)
1536+
await Application.Current.Dispatcher.InvokeAsync(() =>
15401537
{
1541-
// 📌 Set Opacity of icon and clock to 0 and apply Visibility.Hidden
1542-
if(Settings.UseAnimation)
1543-
{
1544-
mainWindow.ClockPanel.Opacity = 0;
1545-
mainWindow.SearchIcon.Opacity = 0;
1546-
}
1547-
else
1538+
if (Application.Current.MainWindow is MainWindow mainWindow)
15481539
{
1549-
mainWindow.ClockPanel.Opacity = 1;
1550-
mainWindow.SearchIcon.Opacity = 1;
1540+
// 📌 Set Opacity of icon and clock to 0 and apply Visibility.Hidden
1541+
var opacity = Settings.UseAnimation ? 0.0 : 1.0;
1542+
mainWindow.ClockPanel.Opacity = opacity;
1543+
mainWindow.SearchIcon.Opacity = opacity;
1544+
mainWindow.ClockPanel.Visibility = Visibility.Hidden;
1545+
SearchIconVisibility = Visibility.Hidden;
1546+
1547+
// Force UI update
1548+
mainWindow.ClockPanel.UpdateLayout();
1549+
mainWindow.SearchIcon.UpdateLayout();
1550+
1551+
// 📌 Apply DWM Cloak (Completely hide the window)
1552+
Win32Helper.DWMSetCloakForWindow(mainWindow, true);
15511553
}
1552-
mainWindow.ClockPanel.Visibility = Visibility.Hidden;
1553-
SearchIconVisibility = Visibility.Hidden;
1554-
1555-
// Force UI update
1556-
mainWindow.ClockPanel.UpdateLayout();
1557-
mainWindow.SearchIcon.UpdateLayout();
1558-
// 📌 Apply DWM Cloak (Completely hide the window)
1559-
Win32Helper.DWMSetCloakForWindow(mainWindow, true);
1560-
}
1554+
});
15611555

15621556
if (StartWithEnglishMode)
15631557
{

0 commit comments

Comments
 (0)