Skip to content

Commit

Permalink
Fix theme load causing import dialog to freeze
Browse files Browse the repository at this point in the history
  • Loading branch information
t1m0thyj committed Feb 27, 2024
1 parent 4cba3ee commit 5929a31
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 38 deletions.
2 changes: 1 addition & 1 deletion scripts/package.ps1
Expand Up @@ -5,7 +5,7 @@ if (-Not (Get-Command msbuild -ErrorAction SilentlyContinue)) {

Set-Location "$(Split-Path $MyInvocation.MyCommand.Path)\.."

$appPlatforms = @("x86", "x64", "arm64")
$appPlatforms = if ($args) { $args } else { @("x86", "x64", "arm64") }
$appVersion = ([Xml](Get-Content -Path "src\WinDynamicDesktop.csproj")).Project.PropertyGroup.Version

Remove-Item "dist" -ErrorAction Ignore -Recurse
Expand Down
68 changes: 31 additions & 37 deletions src/ThemeDialog.cs
Expand Up @@ -11,6 +11,7 @@
using System.IO;
using System.Linq;
using System.Runtime.InteropServices;
using System.Threading;
using System.Threading.Tasks;
using System.Windows.Forms;
using WindowsDisplayAPI.DisplayConfig;
Expand All @@ -20,6 +21,7 @@ namespace WinDynamicDesktop
public partial class ThemeDialog : Form
{
private int selectedIndex;
private SemaphoreSlim loadSemaphore = new SemaphoreSlim(1);

private static readonly Func<string, string> _ = Localization.GetTranslation;
private const string themeLink = "https://windd.info/themes/";
Expand Down Expand Up @@ -135,53 +137,44 @@ private Bitmap GetMeatballIcon()

private void LoadThemes(List<ThemeConfig> themes, string activeTheme = null, string focusTheme = null)
{
this.loadSemaphore.Wait(60000);
Size thumbnailSize = ThemeThumbLoader.GetThumbnailSize(this);
ListViewItem focusedItem = null;

foreach (ThemeConfig theme in themes.ToList())
{
ListViewItem tempItem = this.Invoke(new Func<ListViewItem>(() =>
try
{
string itemText = ThemeManager.GetThemeName(theme);
if (JsonConfig.settings.favoriteThemes != null &&
JsonConfig.settings.favoriteThemes.Contains(theme.themeId))
using (Image thumbnailImage = ThemeThumbLoader.GetThumbnailImage(theme, thumbnailSize, true))
{
itemText = "" + itemText;
}
ListViewItem newItem = listView1.Items.Add(itemText);
newItem.Tag = theme.themeId;
if (activeTheme != null && activeTheme == theme.themeId)
{
newItem.Font = new Font(newItem.Font, FontStyle.Bold);
}
if (focusTheme == null || focusTheme == theme.themeId)
{
focusedItem = newItem;
}
return newItem;
}));

this.BeginInvoke(new Action<ListViewItem>((newItem) =>
{
try
{
using (Image thumbnailImage = ThemeThumbLoader.GetThumbnailImage(theme, thumbnailSize, true))
this.Invoke(new Action(() =>
{
listView1.LargeImageList.Images.Add(thumbnailImage);
newItem.ImageIndex = listView1.LargeImageList.Images.Count - 1;
}
string itemText = ThemeManager.GetThemeName(theme);
if (JsonConfig.settings.favoriteThemes != null &&
JsonConfig.settings.favoriteThemes.Contains(theme.themeId))
{
itemText = "" + itemText;
}
ListViewItem newItem = listView1.Items.Add(itemText,
listView1.LargeImageList.Images.Count - 1);
newItem.Tag = theme.themeId;
if (activeTheme != null && activeTheme == theme.themeId)
{
newItem.Font = new Font(newItem.Font, FontStyle.Bold);
}
if (focusTheme == null || focusTheme == theme.themeId)
{
focusedItem = newItem;
}
}));
}
catch (OutOfMemoryException)
{
ThemeLoader.HandleError(new FailedToCreateThumbnail(theme.themeId));
if (ThemeManager.themeSettings.Find(t => t.themeId == theme.themeId) == null)
{
listView1.Items.RemoveAt(newItem.Index);
}
}
}), tempItem);
}
catch (OutOfMemoryException)
{
ThemeLoader.HandleError(new FailedToCreateThumbnail(theme.themeId));
}
}

this.Invoke(new Action(() =>
Expand All @@ -198,6 +191,7 @@ private void LoadThemes(List<ThemeConfig> themes, string activeTheme = null, str
ThemeThumbLoader.CacheThumbnails(listView1);
}));
this.loadSemaphore.Release();
}

private void LoadImportedThemes(List<ThemeConfig> themes, ImportDialog importDialog)
Expand Down

0 comments on commit 5929a31

Please sign in to comment.