Skip to content

Commit 96bb07d

Browse files
committed
Use dispatcher for performance
1 parent 236abc8 commit 96bb07d

File tree

2 files changed

+80
-121
lines changed

2 files changed

+80
-121
lines changed

Flow.Launcher.Core/Resource/Theme.cs

Lines changed: 31 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,7 @@ public Theme(IPublicAPI publicAPI, Settings settings)
7676
{
7777
_api.LogError(ClassName, "Current theme resource not found. Initializing with default theme.");
7878
_oldTheme = Constant.DefaultTheme;
79-
};
79+
}
8080
}
8181

8282
#endregion
@@ -126,7 +126,7 @@ public void UpdateFonts()
126126
// Load a ResourceDictionary for the specified theme.
127127
var themeName = _settings.Theme;
128128
var dict = GetThemeResourceDictionary(themeName);
129-
129+
130130
// Apply font settings to the theme resource.
131131
ApplyFontSettings(dict);
132132
UpdateResourceDictionary(dict);
@@ -152,11 +152,11 @@ private void ApplyFontSettings(ResourceDictionary dict)
152152
var fontStyle = FontHelper.GetFontStyleFromInvariantStringOrNormal(_settings.QueryBoxFontStyle);
153153
var fontWeight = FontHelper.GetFontWeightFromInvariantStringOrNormal(_settings.QueryBoxFontWeight);
154154
var fontStretch = FontHelper.GetFontStretchFromInvariantStringOrNormal(_settings.QueryBoxFontStretch);
155-
155+
156156
SetFontProperties(queryBoxStyle, fontFamily, fontStyle, fontWeight, fontStretch, true);
157157
SetFontProperties(querySuggestionBoxStyle, fontFamily, fontStyle, fontWeight, fontStretch, false);
158158
}
159-
159+
160160
if (dict["ItemTitleStyle"] is Style resultItemStyle &&
161161
dict["ItemTitleSelectedStyle"] is Style resultItemSelectedStyle &&
162162
dict["ItemHotkeyStyle"] is Style resultHotkeyItemStyle &&
@@ -172,7 +172,7 @@ private void ApplyFontSettings(ResourceDictionary dict)
172172
SetFontProperties(resultHotkeyItemStyle, fontFamily, fontStyle, fontWeight, fontStretch, false);
173173
SetFontProperties(resultHotkeyItemSelectedStyle, fontFamily, fontStyle, fontWeight, fontStretch, false);
174174
}
175-
175+
176176
if (dict["ItemSubTitleStyle"] is Style resultSubItemStyle &&
177177
dict["ItemSubTitleSelectedStyle"] is Style resultSubItemSelectedStyle)
178178
{
@@ -197,7 +197,7 @@ private static void SetFontProperties(Style style, FontFamily fontFamily, FontSt
197197
// First, find the setters to remove and store them in a list
198198
var settersToRemove = style.Setters
199199
.OfType<Setter>()
200-
.Where(setter =>
200+
.Where(setter =>
201201
setter.Property == Control.FontFamilyProperty ||
202202
setter.Property == Control.FontStyleProperty ||
203203
setter.Property == Control.FontWeightProperty ||
@@ -227,18 +227,18 @@ private static void SetFontProperties(Style style, FontFamily fontFamily, FontSt
227227
{
228228
var settersToRemove = style.Setters
229229
.OfType<Setter>()
230-
.Where(setter =>
230+
.Where(setter =>
231231
setter.Property == TextBlock.FontFamilyProperty ||
232232
setter.Property == TextBlock.FontStyleProperty ||
233233
setter.Property == TextBlock.FontWeightProperty ||
234234
setter.Property == TextBlock.FontStretchProperty)
235235
.ToList();
236-
236+
237237
foreach (var setter in settersToRemove)
238238
{
239239
style.Setters.Remove(setter);
240240
}
241-
241+
242242
style.Setters.Add(new Setter(TextBlock.FontFamilyProperty, fontFamily));
243243
style.Setters.Add(new Setter(TextBlock.FontStyleProperty, fontStyle));
244244
style.Setters.Add(new Setter(TextBlock.FontWeightProperty, fontWeight));
@@ -421,7 +421,7 @@ public bool ChangeTheme(string theme = null)
421421

422422
// Retrieve theme resource – always use the resource with font settings applied.
423423
var resourceDict = GetResourceDictionary(theme);
424-
424+
425425
UpdateResourceDictionary(resourceDict);
426426

427427
_settings.Theme = theme;
@@ -584,59 +584,42 @@ private void SetResizeBoarderThickness(Thickness? effectMargin)
584584
/// <summary>
585585
/// Refreshes the frame to apply the current theme settings.
586586
/// </summary>
587-
[System.Diagnostics.CodeAnalysis.SuppressMessage("Usage", "VSTHRD103:Call async methods when in an async method", Justification = "<Pending>")]
588587
public async Task RefreshFrameAsync()
589588
{
590-
// When application is exiting, the Application.Current will be null
591-
if (Application.Current == null) return;
592-
593-
// Must check access so that we will not block the UI thread which causes other issues
594-
if (!Application.Current.Dispatcher.CheckAccess())
589+
await Application.Current.Dispatcher.InvokeAsync(() =>
595590
{
596-
// When application is exiting, the Application.Current will be null
597-
await Application.Current?.Dispatcher.InvokeAsync(RefreshFrameAsync, DispatcherPriority.Render);
598-
return;
599-
}
600-
601-
// Get the actual backdrop type and drop shadow effect settings
602-
var (backdropType, useDropShadowEffect) = GetActualValue();
591+
// Get the actual backdrop type and drop shadow effect settings
592+
var (backdropType, useDropShadowEffect) = GetActualValue();
603593

604-
// Remove OS minimizing/maximizing animation
605-
// Methods.SetWindowAttribute(new WindowInteropHelper(mainWindow).Handle, DWMWINDOWATTRIBUTE.DWMWA_TRANSITIONS_FORCEDISABLED, 3);
594+
// Remove OS minimizing/maximizing animation
595+
// Methods.SetWindowAttribute(new WindowInteropHelper(mainWindow).Handle, DWMWINDOWATTRIBUTE.DWMWA_TRANSITIONS_FORCEDISABLED, 3);
606596

607-
// The timing of adding the shadow effect should vary depending on whether the theme is transparent.
608-
if (BlurEnabled)
609-
{
610-
AutoDropShadow(useDropShadowEffect);
611-
}
612-
SetBlurForWindow(_settings.Theme, backdropType);
597+
// The timing of adding the shadow effect should vary depending on whether the theme is transparent.
598+
if (BlurEnabled)
599+
{
600+
AutoDropShadow(useDropShadowEffect);
601+
}
602+
SetBlurForWindow(_settings.Theme, backdropType);
613603

614-
if (!BlurEnabled)
615-
{
616-
AutoDropShadow(useDropShadowEffect);
617-
}
604+
if (!BlurEnabled)
605+
{
606+
AutoDropShadow(useDropShadowEffect);
607+
}
608+
}, DispatcherPriority.Render);
618609
}
619610

620611
/// <summary>
621612
/// Sets the blur for a window via SetWindowCompositionAttribute
622613
/// </summary>
623614
public async Task SetBlurForWindowAsync()
624615
{
625-
// When application is exiting, the Application.Current will be null
626-
if (Application.Current == null) return;
627-
628-
// Must check access so that we will not block the UI thread which causes other issues
629-
if (!Application.Current.Dispatcher.CheckAccess())
616+
await Application.Current.Dispatcher.InvokeAsync(() =>
630617
{
631-
// When application is exiting, the Application.Current will be null
632-
await Application.Current?.Dispatcher.InvokeAsync(SetBlurForWindowAsync, DispatcherPriority.Render);
633-
return;
634-
}
635-
636-
// Get the actual backdrop type and drop shadow effect settings
637-
var (backdropType, _) = GetActualValue();
618+
// Get the actual backdrop type and drop shadow effect settings
619+
var (backdropType, _) = GetActualValue();
638620

639-
SetBlurForWindow(_settings.Theme, backdropType);
621+
SetBlurForWindow(_settings.Theme, backdropType);
622+
}, DispatcherPriority.Render);
640623
}
641624

642625
/// <summary>

Flow.Launcher/ViewModel/MainViewModel.cs

Lines changed: 49 additions & 73 deletions
Original file line numberDiff line numberDiff line change
@@ -1619,7 +1619,32 @@ public bool ShouldIgnoreHotkeys()
16191619

16201620
public void Show()
16211621
{
1622-
ShowWindow();
1622+
// When application is exiting, the Application.Current will be null
1623+
Application.Current?.Dispatcher.Invoke(() =>
1624+
{
1625+
// When application is exiting, the Application.Current will be null
1626+
if (Application.Current?.MainWindow is MainWindow mainWindow)
1627+
{
1628+
// 📌 Remove DWM Cloak (Make the window visible normally)
1629+
Win32Helper.DWMSetCloakForWindow(mainWindow, false);
1630+
1631+
// Set clock and search icon opacity
1632+
var opacity = Settings.UseAnimation ? 0.0 : 1.0;
1633+
ClockPanelOpacity = opacity;
1634+
SearchIconOpacity = opacity;
1635+
1636+
// Set clock and search icon visibility
1637+
ClockPanelVisibility = string.IsNullOrEmpty(QueryText) ? Visibility.Visible : Visibility.Collapsed;
1638+
if (PluginIconSource != null)
1639+
{
1640+
SearchIconOpacity = 0.0;
1641+
}
1642+
else
1643+
{
1644+
SearchIconVisibility = Visibility.Visible;
1645+
}
1646+
}
1647+
}, DispatcherPriority.Render);
16231648

16241649
// Update WPF properties
16251650
MainWindowVisibility = Visibility.Visible;
@@ -1633,43 +1658,6 @@ public void Show()
16331658
}
16341659
}
16351660

1636-
private void ShowWindow()
1637-
{
1638-
// When application is exiting, the Application.Current will be null
1639-
if (Application.Current == null) return;
1640-
1641-
// Must check access so that we will not block the UI thread which causes window visibility issue
1642-
if (!Application.Current.Dispatcher.CheckAccess())
1643-
{
1644-
// When application is exiting, the Application.Current will be null
1645-
Application.Current?.Dispatcher.Invoke(ShowWindow, DispatcherPriority.Render);
1646-
return;
1647-
}
1648-
1649-
// When application is exiting, the Application.Current will be null
1650-
if (Application.Current?.MainWindow is MainWindow mainWindow)
1651-
{
1652-
// 📌 Remove DWM Cloak (Make the window visible normally)
1653-
Win32Helper.DWMSetCloakForWindow(mainWindow, false);
1654-
1655-
// Set clock and search icon opacity
1656-
var opacity = Settings.UseAnimation ? 0.0 : 1.0;
1657-
ClockPanelOpacity = opacity;
1658-
SearchIconOpacity = opacity;
1659-
1660-
// Set clock and search icon visibility
1661-
ClockPanelVisibility = string.IsNullOrEmpty(QueryText) ? Visibility.Visible : Visibility.Collapsed;
1662-
if (PluginIconSource != null)
1663-
{
1664-
SearchIconOpacity = 0.0;
1665-
}
1666-
else
1667-
{
1668-
SearchIconVisibility = Visibility.Visible;
1669-
}
1670-
}
1671-
}
1672-
16731661
public async void Hide()
16741662
{
16751663
lastHistoryIndex = 1;
@@ -1706,7 +1694,29 @@ public async void Hide()
17061694
break;
17071695
}
17081696

1709-
HideWindow();
1697+
// When application is exiting, the Application.Current will be null
1698+
Application.Current?.Dispatcher.Invoke(() =>
1699+
{
1700+
// When application is exiting, the Application.Current will be null
1701+
if (Application.Current?.MainWindow is MainWindow mainWindow)
1702+
{
1703+
// Set clock and search icon opacity
1704+
var opacity = Settings.UseAnimation ? 0.0 : 1.0;
1705+
ClockPanelOpacity = opacity;
1706+
SearchIconOpacity = opacity;
1707+
1708+
// Set clock and search icon visibility
1709+
ClockPanelVisibility = Visibility.Hidden;
1710+
SearchIconVisibility = Visibility.Hidden;
1711+
1712+
// Force UI update
1713+
mainWindow.ClockPanel.UpdateLayout();
1714+
mainWindow.SearchIcon.UpdateLayout();
1715+
1716+
// 📌 Apply DWM Cloak (Completely hide the window)
1717+
Win32Helper.DWMSetCloakForWindow(mainWindow, true);
1718+
}
1719+
}, DispatcherPriority.Render);
17101720

17111721
// Switch keyboard layout
17121722
if (StartWithEnglishMode)
@@ -1723,40 +1733,6 @@ public async void Hide()
17231733
VisibilityChanged?.Invoke(this, new VisibilityChangedEventArgs { IsVisible = false });
17241734
}
17251735

1726-
private void HideWindow()
1727-
{
1728-
// When application is exiting, the Application.Current will be null
1729-
if (Application.Current == null) return;
1730-
1731-
// Must check access so that we will not block the UI thread which causes window visibility issue
1732-
if (!Application.Current.Dispatcher.CheckAccess())
1733-
{
1734-
// When application is exiting, the Application.Current will be null
1735-
Application.Current?.Dispatcher.Invoke(HideWindow, DispatcherPriority.Render);
1736-
return;
1737-
}
1738-
1739-
// When application is exiting, the Application.Current will be null
1740-
if (Application.Current?.MainWindow is MainWindow mainWindow)
1741-
{
1742-
// Set clock and search icon opacity
1743-
var opacity = Settings.UseAnimation ? 0.0 : 1.0;
1744-
ClockPanelOpacity = opacity;
1745-
SearchIconOpacity = opacity;
1746-
1747-
// Set clock and search icon visibility
1748-
ClockPanelVisibility = Visibility.Hidden;
1749-
SearchIconVisibility = Visibility.Hidden;
1750-
1751-
// Force UI update
1752-
mainWindow.ClockPanel.UpdateLayout();
1753-
mainWindow.SearchIcon.UpdateLayout();
1754-
1755-
// 📌 Apply DWM Cloak (Completely hide the window)
1756-
Win32Helper.DWMSetCloakForWindow(mainWindow, true);
1757-
}
1758-
}
1759-
17601736
#pragma warning restore VSTHRD100 // Avoid async void methods
17611737

17621738
/// <summary>

0 commit comments

Comments
 (0)