Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
43 changes: 22 additions & 21 deletions src/modules/cmdpal/CmdPalModuleInterface/dllmain.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
#include <common/utils/process_path.h>
#include <Psapi.h>
#include <TlHelp32.h>
#include <common/utils/winapi_error.h>

HINSTANCE g_hInst_cmdPal = 0;

Expand Down Expand Up @@ -42,28 +43,24 @@ class CmdPal : public PowertoyModuleIface
//contains the non localized key of the powertoy
std::wstring app_key;

void LaunchApp()
void LaunchApp(const std::wstring& appPath, const std::wstring& commandLineArgs, bool elevated)
{
auto package = package::GetRegisteredPackage(L"Microsoft.CommandPalette", false);

if (package.has_value())
std::wstring dir = std::filesystem::path(appPath).parent_path();

SHELLEXECUTEINFO sei = { 0 };
sei.cbSize = sizeof(SHELLEXECUTEINFO);
sei.hwnd = nullptr;
sei.fMask = SEE_MASK_NOCLOSEPROCESS | SEE_MASK_NO_CONSOLE;
sei.lpVerb = elevated ? L"runas" : L"open";
sei.lpFile = appPath.c_str();
sei.lpParameters = commandLineArgs.c_str();
sei.lpDirectory = dir.c_str();
sei.nShow = SW_SHOWNORMAL;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

if we instead stick SW_HIDE in here, and get rid of the arg, and instead get the PInvoke.GetStartupInfo(), can we see if SW_HIDE was set there, and not show the window in that case?

(I feel like I don't really want to add a magic hidden arg to our exe, but I'll accept it for now if it's the only way)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'll give it a try

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

doesn't work. even if I pass SW_HIDE when launching, PInvoke says SW_SHOW is there. No idea why

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

damn. Mmk. We can come up with something more clever in the future. Thanks for taking a look!


if (!ShellExecuteEx(&sei))
{
auto getAppListEntriesOperation = package->GetAppListEntriesAsync();
auto appEntries = getAppListEntriesOperation.get();

if (appEntries.Size() > 0)
{
winrt::Windows::Foundation::IAsyncOperation<bool> launchOperation = appEntries.GetAt(0).LaunchAsync();
launchOperation.get();
}
else
{
Logger::error(L"No app entries found for the package.");
}
}
else
{
Logger::error(L"CmdPal package is not registered.");
std::wstring error = get_last_error_or_default(GetLastError());
Logger::error(L"Failed to launch process. {}", error);
}
}

Expand Down Expand Up @@ -235,7 +232,11 @@ class CmdPal : public PowertoyModuleIface
Logger::error(errorMessage);
}

LaunchApp();
#if _DEBUG
LaunchApp(std::wstring{ L"shell:AppsFolder\\" } + L"Microsoft.CommandPalette.Dev_8wekyb3d8bbwe!App", L"RunFromPT", false);
#else
LaunchApp(std::wstring{ L"shell:AppsFolder\\" } + L"Microsoft.CommandPalette_8wekyb3d8bbwe!App", L"RunFromPT", false);
#endif
}

virtual void disable()
Expand Down
18 changes: 17 additions & 1 deletion src/modules/cmdpal/Microsoft.CmdPal.UI/App.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,23 @@ public App()
protected override void OnLaunched(LaunchActivatedEventArgs args)
{
AppWindow = new MainWindow();
AppWindow.Activate();

var cmdArgs = Environment.GetCommandLineArgs();

bool runFromPT = false;
foreach (var arg in cmdArgs)
{
if (arg == "RunFromPT")
{
runFromPT = true;
break;
}
}

if (!runFromPT)
{
AppWindow.Activate();
}
}

/// <summary>
Expand Down
2 changes: 2 additions & 0 deletions src/modules/cmdpal/Microsoft.CmdPal.UI/MainWindow.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -466,6 +466,8 @@ private LRESULT HotKeyPrc(
// so that we can bind hotkeys to individual commands
if (!this.Visible || !isRootHotkey)
{
Activate();

Summon(hotkey.CommandId);
}
else if (isRootHotkey)
Expand Down
Loading