Skip to content

Commit 8f26080

Browse files
GurliGebisdonho
authored andcommitted
Implement handling of both new and old install method at the same time
This PR does the following: - Creates two versions of the command handler (both inherits a base class where the implementation is). - Uses a memory mapped file along with a mutex to communicate between the two, to have a shared counter. - The counter value is used inside the classic version of the handler to determine if it should be shown or not. By doing this, we can register both the new MSIX and the old registry on Windows 11, and ensure the following: - If the MSIX registered entry is shown inside the classic menu, the old registry based one hides itself. - If for some reason the MSIX registered entry doesn't show up inside the classic menu, the old registry based on shows itself. From a users point of view - it "just works", and they always only see one entry in the old menu. Fix notepad-plus-plus/notepad-plus-plus#13399 Close #20
1 parent 0c660dc commit 8f26080

15 files changed

+223
-48
lines changed

EditWithNppExplorerCommandHandler.cpp renamed to BaseNppExplorerCommandHandler.cpp

Lines changed: 19 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,40 +1,45 @@
11
#include "pch.h"
2-
#include "EditWithNppExplorerCommandHandler.h"
2+
#include "BaseNppExplorerCommandHandler.h"
33

44
#include "PathHelper.h"
55

66
using namespace NppShell::CommandHandlers;
77
using namespace NppShell::Helpers;
88

9-
const wstring EditWithNppExplorerCommandHandler::GetNppExecutableFullPath()
9+
BaseNppExplorerCommandHandler::BaseNppExplorerCommandHandler()
10+
{
11+
counter = make_unique<SharedCounter>();
12+
}
13+
14+
const wstring BaseNppExplorerCommandHandler::GetNppExecutableFullPath()
1015
{
1116
const wstring path = GetApplicationPath();
1217
const wstring fileName = L"\\notepad++.exe";
1318

1419
return path + fileName;
1520
}
1621

17-
const wstring EditWithNppExplorerCommandHandler::Title()
22+
const wstring BaseNppExplorerCommandHandler::Title()
1823
{
1924
return L"Edit with Notepad++";
2025
}
2126

22-
const wstring EditWithNppExplorerCommandHandler::Icon()
27+
const wstring BaseNppExplorerCommandHandler::Icon()
2328
{
2429
const wstring fileName = GetNppExecutableFullPath();
2530

2631
return fileName;
2732
}
2833

29-
const wstring EditWithNppExplorerCommandHandler::GetCommandLine(const wstring& itemName)
34+
const wstring BaseNppExplorerCommandHandler::GetCommandLine(const wstring& itemName)
3035
{
3136
const wstring fileName = GetNppExecutableFullPath();
3237
const wstring parameters = L"\"" + itemName + L"\"";
3338

3439
return L"\"" + fileName + L"\" " + parameters;
3540
}
3641

37-
IFACEMETHODIMP EditWithNppExplorerCommandHandler::Invoke(IShellItemArray* psiItemArray, IBindCtx* pbc) noexcept try
42+
IFACEMETHODIMP BaseNppExplorerCommandHandler::Invoke(IShellItemArray* psiItemArray, IBindCtx* pbc) noexcept try
3843
{
3944
UNREFERENCED_PARAMETER(pbc);
4045

@@ -87,4 +92,11 @@ IFACEMETHODIMP EditWithNppExplorerCommandHandler::Invoke(IShellItemArray* psiIte
8792

8893
return S_OK;
8994
}
90-
CATCH_RETURN();
95+
CATCH_RETURN();
96+
97+
const EXPCMDSTATE BaseNppExplorerCommandHandler::State(IShellItemArray* psiItemArray)
98+
{
99+
UNREFERENCED_PARAMETER(psiItemArray);
100+
101+
throw L"State must be overridden in all implementations";
102+
}
Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,28 @@
11
#pragma once
2-
#include "pch.h"
3-
42
#include "ExplorerCommandBase.h"
3+
#include "SharedCounter.h"
4+
5+
using namespace NppShell::Helpers;
56

67
namespace NppShell::CommandHandlers
78
{
8-
#ifdef WIN64
9-
class __declspec(uuid("B298D29A-A6ED-11DE-BA8C-A68E55D89593")) EditWithNppExplorerCommandHandler : public ExplorerCommandBase
10-
#else
11-
class __declspec(uuid("00F3C2EC-A6EE-11DE-A03A-EF8F55D89593")) EditWithNppExplorerCommandHandler : public ExplorerCommandBase
12-
#endif
9+
class BaseNppExplorerCommandHandler : public ExplorerCommandBase
1310
{
1411
public:
12+
BaseNppExplorerCommandHandler();
13+
1514
const wstring Title() override;
1615
const wstring Icon() override;
1716

1817
IFACEMETHODIMP Invoke(IShellItemArray* psiItemArray, IBindCtx* pbc) noexcept override;
1918

19+
virtual const EXPCMDSTATE State(IShellItemArray* psiItemArray) override;
20+
2021
private:
2122
const wstring GetNppExecutableFullPath();
2223
const wstring GetCommandLine(const wstring& itemName);
24+
25+
protected:
26+
unique_ptr<SharedCounter> counter;
2327
};
2428
}
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
#include "pch.h"
2+
#include "ClassicEditWithNppExplorerCommandHandler.h"
3+
4+
using namespace NppShell::CommandHandlers;
5+
6+
const EXPCMDSTATE ClassicEditWithNppExplorerCommandHandler::State(IShellItemArray* psiItemArray)
7+
{
8+
UNREFERENCED_PARAMETER(psiItemArray);
9+
10+
int state = counter->GetValue();
11+
12+
if (state == 3 || state == 5)
13+
{
14+
// This is on Windows 11, with both the modern and classic being shows, so we hide this one.
15+
return ECS_HIDDEN;
16+
}
17+
18+
return ECS_ENABLED;
19+
}
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
#pragma once
2+
#include "BaseNppExplorerCommandHandler.h"
3+
4+
using namespace NppShell::Helpers;
5+
6+
namespace NppShell::CommandHandlers
7+
{
8+
#ifdef WIN64
9+
class __declspec(uuid("B298D29A-A6ED-11DE-BA8C-A68E55D89593")) ClassicEditWithNppExplorerCommandHandler : public BaseNppExplorerCommandHandler
10+
#else
11+
class __declspec(uuid("00F3C2EC-A6EE-11DE-A03A-EF8F55D89593")) ClassicEditWithNppExplorerCommandHandler : public BaseNppExplorerCommandHandler
12+
#endif
13+
{
14+
public:
15+
const EXPCMDSTATE State(IShellItemArray* psiItemArray);
16+
};
17+
}

ExplorerCommandBase.cpp

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -8,13 +8,6 @@ const EXPCMDFLAGS ExplorerCommandBase::Flags()
88
return ECF_DEFAULT;
99
}
1010

11-
const EXPCMDSTATE ExplorerCommandBase::State(IShellItemArray* psiItemArray)
12-
{
13-
UNREFERENCED_PARAMETER(psiItemArray);
14-
15-
return ECS_ENABLED;
16-
}
17-
1811
IFACEMETHODIMP ExplorerCommandBase::GetTitle(IShellItemArray* psiItemArray, LPWSTR* ppszName)
1912
{
2013
UNREFERENCED_PARAMETER(psiItemArray);
@@ -57,7 +50,8 @@ IFACEMETHODIMP ExplorerCommandBase::GetFlags(EXPCMDFLAGS* flags)
5750
return S_OK;
5851
}
5952

60-
IFACEMETHODIMP ExplorerCommandBase::GetCanonicalName(GUID* pguidCommandName) {
53+
IFACEMETHODIMP ExplorerCommandBase::GetCanonicalName(GUID* pguidCommandName)
54+
{
6155
*pguidCommandName = GUID_NULL;
6256
return S_OK;
6357
}

ExplorerCommandBase.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ namespace NppShell::CommandHandlers
99
virtual const wstring Title() = 0;
1010
virtual const wstring Icon() = 0;
1111
virtual const EXPCMDFLAGS Flags();
12-
virtual const EXPCMDSTATE State(IShellItemArray* psiItemArray);
12+
virtual const EXPCMDSTATE State(IShellItemArray* psiItemArray) = 0;
1313

1414
IFACEMETHODIMP GetTitle(IShellItemArray* psiItemArray, LPWSTR* ppszName);
1515
IFACEMETHODIMP GetIcon(IShellItemArray* psiItemArray, LPWSTR* ppszIcon);

Installer.cpp

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
#include "pch.h"
22
#include "Installer.h"
33

4-
#include "EditWithNppExplorerCommandHandler.h"
4+
#include "ClassicEditWithNppExplorerCommandHandler.h"
55
#include "PathHelper.h"
66
#include "AclHelper.h"
77

@@ -65,7 +65,7 @@ bool IsWindows11Installation()
6565

6666
wstring GetCLSIDString()
6767
{
68-
const auto uuid = __uuidof(NppShell::CommandHandlers::EditWithNppExplorerCommandHandler);
68+
const auto uuid = __uuidof(NppShell::CommandHandlers::ClassicEditWithNppExplorerCommandHandler);
6969

7070
LPOLESTR guidString = 0;
7171
const HRESULT result = StringFromCLSID(uuid, &guidString);
@@ -360,10 +360,8 @@ HRESULT NppShell::Installer::Install()
360360

361361
result = RegisterSparsePackage();
362362
}
363-
else
364-
{
365-
result = RegisterOldContextMenu();
366-
}
363+
364+
result = RegisterOldContextMenu();
367365

368366
// Ensure we schedule old files for removal on next reboot.
369367
MoveFileToTempAndScheduleDeletion(GetApplicationPath() + L"\\NppShell_01.dll", false);
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
#include "pch.h"
2+
#include "ModernEditWithNppExplorerCommandHandler.h"
3+
4+
using namespace NppShell::CommandHandlers;
5+
6+
const EXPCMDSTATE ModernEditWithNppExplorerCommandHandler::State(IShellItemArray* psiItemArray)
7+
{
8+
UNREFERENCED_PARAMETER(psiItemArray);
9+
10+
return ECS_ENABLED;
11+
}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
#pragma once
2+
#include "BaseNppExplorerCommandHandler.h"
3+
4+
using namespace NppShell::Helpers;
5+
6+
namespace NppShell::CommandHandlers
7+
{
8+
class __declspec(uuid("E6950302-61F0-4FEB-97DB-855E30D4A991")) ModernEditWithNppExplorerCommandHandler : public BaseNppExplorerCommandHandler
9+
{
10+
public:
11+
const EXPCMDSTATE State(IShellItemArray* psiItemArray);
12+
};
13+
}

NppShell.vcxproj

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -263,19 +263,24 @@
263263
</ItemDefinitionGroup>
264264
<ItemGroup>
265265
<ClInclude Include="AclHelper.h" />
266-
<ClInclude Include="EditWithNppExplorerCommandHandler.h" />
266+
<ClInclude Include="BaseNppExplorerCommandHandler.h" />
267+
<ClInclude Include="ClassicEditWithNppExplorerCommandHandler.h" />
267268
<ClInclude Include="ExplorerCommandBase.h" />
268269
<ClInclude Include="framework.h" />
270+
<ClInclude Include="ModernEditWithNppExplorerCommandHandler.h" />
269271
<ClInclude Include="PathHelper.h" />
270272
<ClInclude Include="Installer.h" />
271273
<ClInclude Include="pch.h" />
274+
<ClInclude Include="SharedCounter.h" />
272275
<ClInclude Include="SimpleFactory.h" />
273276
</ItemGroup>
274277
<ItemGroup>
275278
<ClCompile Include="AclHelper.cpp" />
279+
<ClCompile Include="BaseNppExplorerCommandHandler.cpp" />
280+
<ClCompile Include="ClassicEditWithNppExplorerCommandHandler.cpp" />
276281
<ClCompile Include="dllmain.cpp" />
277-
<ClCompile Include="EditWithNppExplorerCommandHandler.cpp" />
278282
<ClCompile Include="ExplorerCommandBase.cpp" />
283+
<ClCompile Include="ModernEditWithNppExplorerCommandHandler.cpp" />
279284
<ClCompile Include="PathHelper.cpp" />
280285
<ClCompile Include="Installer.cpp" />
281286
<ClCompile Include="pch.cpp">
@@ -286,6 +291,7 @@
286291
<PrecompiledHeader Condition="'$(Configuration)|$(Platform)'=='Release|Win32'">Create</PrecompiledHeader>
287292
<PrecompiledHeader Condition="'$(Configuration)|$(Platform)'=='Release|ARM64'">Create</PrecompiledHeader>
288293
</ClCompile>
294+
<ClCompile Include="SharedCounter.cpp" />
289295
</ItemGroup>
290296
<ItemGroup>
291297
<None Include="packages.config" />

0 commit comments

Comments
 (0)