Skip to content

Commit 7779ac4

Browse files
support for edge channels
1 parent 3bb2f8c commit 7779ac4

File tree

18 files changed

+429
-101
lines changed

18 files changed

+429
-101
lines changed

Community.PowerToys.Run.Plugin.EdgeFavorite.Tests/Community.PowerToys.Run.Plugin.EdgeFavorite.Tests.csproj

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
<Project Sdk="Microsoft.NET.Sdk">
22

33
<PropertyGroup>
4-
<TargetFramework>net8.0-windows</TargetFramework>
4+
<TargetFramework>net8.0-windows10.0.22621.0</TargetFramework>
55
<Nullable>enable</Nullable>
66
<IsPackable>false</IsPackable>
77
<TreatWarningsAsErrors>true</TreatWarningsAsErrors>
@@ -24,8 +24,8 @@
2424

2525
<ItemGroup>
2626
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.11.1" />
27-
<PackageReference Include="MSTest.TestAdapter" Version="3.5.2" />
28-
<PackageReference Include="MSTest.TestFramework" Version="3.5.2" />
27+
<PackageReference Include="MSTest.TestAdapter" Version="3.6.1" />
28+
<PackageReference Include="MSTest.TestFramework" Version="3.6.1" />
2929
<PackageReference Include="System.IO.Abstractions" Version="17.2.3" />
3030
</ItemGroup>
3131

Community.PowerToys.Run.Plugin.EdgeFavorite.Tests/FavoriteItemTest.cs

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33

44
using System.Globalization;
55
using Community.PowerToys.Run.Plugin.EdgeFavorite.Models;
6+
using Community.PowerToys.Run.Plugin.EdgeFavorite.Tests.Mocks;
67
using Microsoft.VisualStudio.TestTools.UnitTesting;
78
using Wox.Plugin;
89

@@ -12,11 +13,13 @@ namespace Community.PowerToys.Run.Plugin.EdgeFavorite.Tests
1213
public class FavoriteItemTest
1314
{
1415
private readonly PluginInitContext _context;
16+
private readonly MockEdgeManager _edgeManager;
1517
private readonly ProfileInfo _profileInfo;
1618

1719
public FavoriteItemTest()
1820
{
1921
_context = new PluginInitContext();
22+
_edgeManager = new MockEdgeManager();
2023
_profileInfo = new ProfileInfo("Default", "Default");
2124
}
2225

@@ -33,7 +36,7 @@ public static void Initialize(TestContext context)
3336
public void Assert_Folder_Item_Result_QueryTextDisplay(string name, string path, bool searchTree, string expectedQueryTextDisplay)
3437
{
3538
var item = new FavoriteItem(name, path, _profileInfo, false);
36-
var result = item.CreateResult(_context.API, string.Empty, false, searchTree);
39+
var result = item.CreateResult(_context.API, _edgeManager, string.Empty, false, searchTree);
3740
Assert.AreEqual(result.QueryTextDisplay, expectedQueryTextDisplay);
3841
}
3942

@@ -43,7 +46,7 @@ public void Assert_Folder_Item_Result_QueryTextDisplay(string name, string path,
4346
public void Assert_Url_Item_Result_QueryTextDisplay(string name, string url, string path, bool searchTree, string expectedQueryTextDisplay)
4447
{
4548
var item = new FavoriteItem(name, url, path, _profileInfo);
46-
var result = item.CreateResult(_context.API, string.Empty, false, searchTree);
49+
var result = item.CreateResult(_context.API, _edgeManager, string.Empty, false, searchTree);
4750
Assert.AreEqual(result.QueryTextDisplay, expectedQueryTextDisplay);
4851
}
4952

@@ -53,7 +56,7 @@ public void Assert_Url_Item_Result_QueryTextDisplay(string name, string url, str
5356
public void Assert_Url_Item_Result_SubTitle(string name, string url, string path, bool showProfileName, string expectedSubTitle)
5457
{
5558
var item = new FavoriteItem(name, url, path, _profileInfo);
56-
var result = item.CreateResult(_context.API, string.Empty, showProfileName, false);
59+
var result = item.CreateResult(_context.API, _edgeManager, string.Empty, showProfileName, false);
5760
Assert.AreEqual(result.SubTitle, expectedSubTitle);
5861
}
5962

@@ -63,7 +66,7 @@ public void Assert_Url_Item_Result_SubTitle(string name, string url, string path
6366
public void Assert_Folder_Item_Result_SubTitle(string name, string path, bool showProfileName, string expectedSubTitle)
6467
{
6568
var item = new FavoriteItem(name, path, _profileInfo, false);
66-
var result = item.CreateResult(_context.API, string.Empty, showProfileName, false);
69+
var result = item.CreateResult(_context.API, _edgeManager, string.Empty, showProfileName, false);
6770
Assert.AreEqual(result.SubTitle, expectedSubTitle);
6871
}
6972
}
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
// Copyright (c) Davide Giacometti. All rights reserved.
2+
// Licensed under the MIT license. See LICENSE file in the project root for full license information.
3+
4+
using Community.PowerToys.Run.Plugin.EdgeFavorite.Helpers;
5+
using Community.PowerToys.Run.Plugin.EdgeFavorite.Models;
6+
7+
namespace Community.PowerToys.Run.Plugin.EdgeFavorite.Tests.Mocks
8+
{
9+
public class MockEdgeManager : IEdgeManager
10+
{
11+
public string UserDataPath => string.Empty;
12+
13+
public bool ChannelDetected => false;
14+
15+
public void Initialize(Channel channel)
16+
{
17+
}
18+
19+
public void Open(FavoriteItem favorite, bool inPrivate, bool newWindow)
20+
{
21+
}
22+
23+
public void Open(FavoriteItem[] favorites, bool inPrivate, bool newWindow)
24+
{
25+
}
26+
}
27+
}

Community.PowerToys.Run.Plugin.EdgeFavorite/Community.PowerToys.Run.Plugin.EdgeFavorite.csproj

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,14 @@
11
<Project Sdk="Microsoft.NET.Sdk">
22

33
<PropertyGroup>
4-
<TargetFramework>net8.0-windows</TargetFramework>
4+
<TargetFramework>net8.0-windows10.0.22621.0</TargetFramework>
55
<Nullable>enable</Nullable>
66
<UseWPF>true</UseWPF>
77
<TreatWarningsAsErrors>true</TreatWarningsAsErrors>
88
<Platforms>x64;ARM64</Platforms>
99
<AppendTargetFrameworkToOutputPath>false</AppendTargetFrameworkToOutputPath>
1010
<AppendRuntimeIdentifierToOutputPath>false</AppendRuntimeIdentifierToOutputPath>
11+
<AllowUnsafeBlocks>true</AllowUnsafeBlocks>
1112
</PropertyGroup>
1213

1314
<PropertyGroup>
@@ -62,6 +63,12 @@
6263
<None Update="Images\Folder.light.png">
6364
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
6465
</None>
66+
<None Update="Images\Warning.dark.png">
67+
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
68+
</None>
69+
<None Update="Images\Warning.light.png">
70+
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
71+
</None>
6572
<None Update="plugin.json">
6673
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
6774
</None>

Community.PowerToys.Run.Plugin.EdgeFavorite/Helpers/EdgeHelpers.cs

Lines changed: 0 additions & 71 deletions
This file was deleted.
Lines changed: 122 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,122 @@
1+
// Copyright (c) Davide Giacometti. All rights reserved.
2+
// Licensed under the MIT license. See LICENSE file in the project root for full license information.
3+
4+
using System;
5+
using System.Collections.Generic;
6+
using System.IO;
7+
using System.Linq;
8+
using System.Security.Principal;
9+
using Community.PowerToys.Run.Plugin.EdgeFavorite.Models;
10+
using Windows.Management.Deployment;
11+
using Wox.Infrastructure;
12+
using Wox.Plugin.Logger;
13+
14+
namespace Community.PowerToys.Run.Plugin.EdgeFavorite.Helpers
15+
{
16+
public class EdgeManager : IEdgeManager
17+
{
18+
private readonly PackageManager _packageManager;
19+
20+
private readonly Dictionary<Channel, (string PackageName, string UserDataParentFolder)> _packages = new()
21+
{
22+
{ Channel.Stable, ("Microsoft.MicrosoftEdge.Stable", "Edge") },
23+
{ Channel.Beta, ("Microsoft.MicrosoftEdge.Beta", "Edge Beta") },
24+
{ Channel.Dev, ("Microsoft.MicrosoftEdge.Dev", "Edge Dev") },
25+
{ Channel.Canary, ("Microsoft.MicrosoftEdge.Canary", "Edge SxS") },
26+
};
27+
28+
private string? _openCommand;
29+
private string? _userDataPath;
30+
31+
public string UserDataPath => _userDataPath ?? string.Empty;
32+
33+
public bool ChannelDetected => _openCommand != null && _userDataPath != null;
34+
35+
public EdgeManager()
36+
{
37+
_packageManager = new PackageManager();
38+
}
39+
40+
public void Initialize(Channel channel)
41+
{
42+
_openCommand = null;
43+
_userDataPath = null;
44+
45+
var user = WindowsIdentity.GetCurrent().User;
46+
47+
if (user == null)
48+
{
49+
return;
50+
}
51+
52+
var (packageName, userDataParentFolder) = _packages[channel];
53+
54+
foreach (var p in _packageManager.FindPackagesForUser(user.Value))
55+
{
56+
if (p.Id.Name.Equals(packageName, StringComparison.OrdinalIgnoreCase))
57+
{
58+
_openCommand = $@"shell:AppsFolder\{p.Id.FamilyName}!App";
59+
_userDataPath = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData), "Microsoft", userDataParentFolder, "User Data");
60+
break;
61+
}
62+
}
63+
}
64+
65+
public void Open(FavoriteItem favorite, bool inPrivate, bool newWindow)
66+
{
67+
OpenInternal(favorite.Profile, favorite.Url!, inPrivate, newWindow);
68+
}
69+
70+
public void Open(FavoriteItem[] favorites, bool inPrivate, bool newWindow)
71+
{
72+
if (favorites.Length == 0)
73+
{
74+
throw new InvalidOperationException("Favorites cannot be empty.");
75+
}
76+
77+
// If there is no need to open in a new window, starting multiple processes is preferred to avoid long command line arguments
78+
if (newWindow)
79+
{
80+
Open(favorites[0].Profile, string.Join(" ", favorites.Select(f => f.Url!)), inPrivate, newWindow);
81+
}
82+
else
83+
{
84+
foreach (var favorite in favorites)
85+
{
86+
Open(favorite, inPrivate, false);
87+
}
88+
}
89+
}
90+
91+
private void Open(ProfileInfo profileInfo, string urls, bool inPrivate, bool newWindow)
92+
{
93+
OpenInternal(profileInfo, urls, inPrivate, newWindow);
94+
}
95+
96+
private void OpenInternal(ProfileInfo profileInfo, string urls, bool inPrivate, bool newWindow)
97+
{
98+
var args = urls;
99+
100+
if (inPrivate)
101+
{
102+
args += " -inprivate";
103+
}
104+
105+
if (newWindow)
106+
{
107+
args += " -new-window";
108+
}
109+
110+
args += $" -profile-directory=\"{profileInfo.Directory}\"";
111+
112+
try
113+
{
114+
Helper.OpenInShell(_openCommand, args);
115+
}
116+
catch (Exception ex)
117+
{
118+
Log.Exception("Failed to launch Microsoft Edge", ex, typeof(EdgeManager));
119+
}
120+
}
121+
}
122+
}
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
// Copyright (c) Davide Giacometti. All rights reserved.
2+
// Licensed under the MIT license. See LICENSE file in the project root for full license information.
3+
4+
using Community.PowerToys.Run.Plugin.EdgeFavorite.Models;
5+
6+
namespace Community.PowerToys.Run.Plugin.EdgeFavorite.Helpers
7+
{
8+
public interface IEdgeManager
9+
{
10+
string UserDataPath { get; }
11+
12+
bool ChannelDetected { get; }
13+
14+
void Initialize(Channel channel);
15+
16+
void Open(FavoriteItem favorite, bool inPrivate, bool newWindow);
17+
18+
void Open(FavoriteItem[] favorites, bool inPrivate, bool newWindow);
19+
}
20+
}

0 commit comments

Comments
 (0)