Skip to content

Commit

Permalink
disposable and description
Browse files Browse the repository at this point in the history
  • Loading branch information
davidegiacometti committed Apr 15, 2024
1 parent 7e935a1 commit 10ecb16
Show file tree
Hide file tree
Showing 5 changed files with 57 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,8 @@

<ItemGroup>
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.9.0" />
<PackageReference Include="MSTest.TestAdapter" Version="3.2.2" />
<PackageReference Include="MSTest.TestFramework" Version="3.2.2" />
<PackageReference Include="MSTest.TestAdapter" Version="3.3.1" />
<PackageReference Include="MSTest.TestFramework" Version="3.3.1" />
<PackageReference Include="System.IO.Abstractions" Version="17.2.3" />
</ItemGroup>

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,12 @@

namespace Community.PowerToys.Run.Plugin.EdgeFavorite.Helpers
{
public class FavoriteProvider : IFavoriteProvider
public sealed class FavoriteProvider : IFavoriteProvider, IDisposable
{
private readonly string _path;
private readonly FileSystemWatcher _watcher;
private FavoriteItem _root;
private bool _disposed;

public FavoriteItem Root => _root;

Expand All @@ -37,6 +38,17 @@ public FavoriteProvider(string path, ProfileInfo profileInfo)
_watcher.EnableRaisingEvents = true;
}

public void Dispose()
{
if (_disposed)
{
return;
}

_watcher?.Dispose();
_disposed = true;
}

private void InitFavorites()
{
try
Expand Down Expand Up @@ -103,7 +115,8 @@ private void ProcessFavorites(JsonElement element, FavoriteItem parent, string p

if (children.ValueKind == JsonValueKind.Array)
{
foreach (var child in children.EnumerateArray())
using var childEnumerator = children.EnumerateArray();
foreach (var child in childEnumerator)
{
ProcessFavorites(child, folder, path, false);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,21 +12,19 @@

namespace Community.PowerToys.Run.Plugin.EdgeFavorite.Helpers
{
public class ProfileManager : IProfileManager
public sealed class ProfileManager : IProfileManager, IDisposable
{
private static readonly string _userDataPath = Environment.ExpandEnvironmentVariables(@"%LOCALAPPDATA%\Microsoft\Edge\User Data");
private readonly List<IFavoriteProvider> _favoriteProviders = new();
private bool _disposed;

public ReadOnlyCollection<IFavoriteProvider> FavoriteProviders => _favoriteProviders.AsReadOnly();

public ProfileManager()
{
}

public void ReloadProfiles(bool defaultOnly)
{
if (_favoriteProviders.Count > 0)
{
DisposeFavoriteProviders();
_favoriteProviders.Clear();
}

Expand Down Expand Up @@ -56,6 +54,17 @@ public void ReloadProfiles(bool defaultOnly)
}
}

public void Dispose()
{
if (_disposed)
{
return;
}

DisposeFavoriteProviders();
_disposed = true;
}

private static void TrySetProfileName(ProfileInfo profileInfo, string directoryPath)
{
_ = Task.Run(() =>
Expand Down Expand Up @@ -96,5 +105,16 @@ private static void TrySetProfileName(ProfileInfo profileInfo, string directoryP
}
});
}

private void DisposeFavoriteProviders()
{
foreach (var privider in _favoriteProviders)
{
if (privider is IDisposable disposableProvider)
{
disposableProvider.Dispose();
}
}
}
}
}
16 changes: 14 additions & 2 deletions Community.PowerToys.Run.Plugin.EdgeFavorite/Main.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@

namespace Community.PowerToys.Run.Plugin.EdgeFavorite
{
public class Main : IPlugin, ISettingProvider, IContextMenu
public sealed class Main : IPlugin, ISettingProvider, IContextMenu, IDisposable
{
public static string PluginID => "D73A7EF0633F4C82A14454FFD848F447";

Expand All @@ -27,10 +27,11 @@ public class Main : IPlugin, ISettingProvider, IContextMenu
private PluginInitContext? _context;
private bool _searchTree;
private bool _defaultOnly;
private bool _disposed;

public string Name => "Edge Favorite";

public string Description => "Open Microsoft Edge favorites.";
public string Description => "Opens Microsoft Edge favorites";

public IEnumerable<PluginAdditionalOption> AdditionalOptions => new List<PluginAdditionalOption>
{
Expand Down Expand Up @@ -133,6 +134,17 @@ public List<ContextMenuResult> LoadContextMenus(Result selectedResult)
return favorite.CreateContextMenuResult();
}

public void Dispose()
{
if (_disposed)
{
return;
}

_profileManager?.Dispose();
_disposed = true;
}

private void OnThemeChanged(Theme currentTheme, Theme newTheme)
{
UpdateIconsPath(newTheme);
Expand Down
2 changes: 1 addition & 1 deletion Directory.Build.props
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<Project>
<PropertyGroup>
<Version>0.4.0</Version>
<Version>0.4.1</Version>
</PropertyGroup>

<ItemGroup>
Expand Down

0 comments on commit 10ecb16

Please sign in to comment.