Compare commits

...

4 Commits

Author SHA1 Message Date
Hao Liu
712b31aecd Revert "[PowerAccent] Cancel previous ShowToolbar task if a new one is triggered" (#39563)
Revert "[PowerAccent] Cancel previous ShowToolbar task if a new one is triggered (#37757)"

This reverts commit e1ad7e39c6.
2025-05-19 13:02:09 +08:00
Gordon Lam
99076697d0 Fix Cmdpal launch without admin mode - both runner and setting. (#39494)
* Change to path and args

* Fix both Setting launch and runner launch
2025-05-19 12:00:35 +08:00
Gordon Lam
f9fe88696b Fix RunAsAdmin For CmdPal when PowerToys is running as Admin (#39448)
<!-- Enter a brief description/summary of your PR here. What does it fix/what does it change/how was it tested (even manually, if necessary)? -->
## Summary of the Pull Request
Since we change the launch method by this PR #39269 , we will start cmdpal as admin too if powertoys run as admin.
The fix is leveraging explorer (which will not run as admin) to start the cmdpal

Moreover, without this fix, some of extension cannot be "loaded" when cmdpal run as admin, e.g. winget will be missing, and my own new extension developed by myself will not be loaded successful as well.
2025-05-19 12:00:23 +08:00
Kai Tao
a71d7e9282 workspaces: shell:appsfolder launch does not support the command line (#39433)
shell:appsfolder launch does not respect the command line
2025-05-19 12:00:05 +08:00
7 changed files with 43 additions and 68 deletions

View File

@@ -25,6 +25,7 @@ namespace AppLauncher
const std::wstring ChromeFilename = L"chrome.exe";
const std::wstring ChromePwaFilename = L"chrome_proxy.exe";
const std::wstring PwaCommandLineAddition = L"--profile-directory=Default --app-id=";
const std::wstring SteamProtocolPrefix = L"steam:";
}
Result<SHELLEXECUTEINFO, std::wstring> LaunchApp(const std::wstring& appPath, const std::wstring& commandLineArgs, bool elevated)
@@ -134,12 +135,11 @@ namespace AppLauncher
}
}
// win32 app with appUserModelId:
// usage example: steam games
if (!launched && !app.appUserModelId.empty())
// protocol launch for steam
if (!launched && !app.appUserModelId.empty() && app.appUserModelId.contains(NonLocalizable::SteamProtocolPrefix))
{
Logger::trace(L"Launching {} as {}", app.name, app.appUserModelId);
auto res = LaunchApp(L"shell:AppsFolder\\" + app.appUserModelId, app.commandLineArgs, app.isElevated);
auto res = LaunchApp(app.appUserModelId, app.commandLineArgs, app.isElevated);
if (res.isOk())
{
launched = true;

View File

@@ -217,7 +217,9 @@ public:
CmdPal::m_enabled.store(true);
std::wstring packageName = L"Microsoft.CommandPalette";
std::wstring launchPath = L"x-cmdpal://background";
// Launch CmdPal as normal user using explorer
std::wstring launchPath = L"explorer.exe";
std::wstring launchArgs = L"x-cmdpal://background";
#ifdef IS_DEV_BRANDING
packageName = L"Microsoft.CommandPalette.Dev";
#endif
@@ -268,13 +270,13 @@ public:
if (!firstEnableCall)
{
Logger::trace("Not first attempt, try to launch");
LaunchApp(launchPath, L"", false /*no elevated*/, false /*error pop up*/);
LaunchApp(launchPath, launchArgs, false /*no elevated*/, false /*error pop up*/);
}
else
{
// If first time enable, do retry launch.
Logger::trace("First attempt, try to launch");
std::thread launchThread(&CmdPal::RetryLaunch, launchPath);
std::thread launchThread(&CmdPal::RetryLaunch, launchPath, launchArgs);
launchThread.detach();
}
@@ -289,14 +291,14 @@ public:
CmdPal::m_enabled.store(false);
}
static void RetryLaunch(std::wstring path)
static void RetryLaunch(std::wstring path, std::wstring cmdArgs)
{
const int base_delay_milliseconds = 1000;
int max_retry = 9; // 2**9 - 1 seconds. Control total wait time within 10 min.
int retry = 0;
do
{
auto launch_result = LaunchApp(path, L"", false, retry < max_retry);
auto launch_result = LaunchApp(path, cmdArgs, false, retry < max_retry);
if (launch_result)
{
Logger::info(L"CmdPal launched successfully after {} retries.", retry);
@@ -348,4 +350,4 @@ std::atomic<bool> CmdPal::m_launched{ false };
extern "C" __declspec(dllexport) PowertoyModuleIface* __cdecl powertoy_create()
{
return new CmdPal();
}
}

View File

@@ -62,11 +62,11 @@ public partial class PowerAccent : IDisposable
private void SetEvents()
{
_keyboardListener.SetShowToolbarEvent(new PowerToys.PowerAccentKeyboardService.ShowToolbar((LetterKey letterKey, TriggerKey trigger ) =>
_keyboardListener.SetShowToolbarEvent(new PowerToys.PowerAccentKeyboardService.ShowToolbar((LetterKey letterKey) =>
{
System.Windows.Application.Current.Dispatcher.Invoke(() =>
{
ShowToolbar(letterKey, trigger);
ShowToolbar(letterKey);
});
}));
@@ -92,15 +92,23 @@ public partial class PowerAccent : IDisposable
}));
}
private void ShowToolbar(LetterKey letterKey, TriggerKey trigger)
private void ShowToolbar(LetterKey letterKey)
{
_visible = true;
_characters = GetCharacters(letterKey);
_characterDescriptions = GetCharacterDescriptions(_characters);
_showUnicodeDescription = _settingService.ShowUnicodeDescription;
OnChangeDisplay?.Invoke(true, _characters);
ProcessNextChar(trigger, false);
Task.Delay(_settingService.InputTime).ContinueWith(
t =>
{
if (_visible)
{
OnChangeDisplay?.Invoke(true, _characters);
}
},
TaskScheduler.FromCurrentSynchronizationContext());
}
private string[] GetCharacters(LetterKey letterKey)

View File

@@ -13,7 +13,7 @@
namespace winrt::PowerToys::PowerAccentKeyboardService::implementation
{
KeyboardListener::KeyboardListener() :
m_toolbarVisible(false), m_activationKeyHold(false), m_triggeredWithSpace(false), m_leftShiftPressed(false), m_rightShiftPressed(false), m_triggeredWithLeftArrow(false), m_triggeredWithRightArrow(false)
m_toolbarVisible(false), m_triggeredWithSpace(false), m_leftShiftPressed(false), m_rightShiftPressed(false), m_triggeredWithLeftArrow(false), m_triggeredWithRightArrow(false)
{
s_instance = this;
LoggerHelpers::init_logger(L"PowerAccent", L"PowerAccentKeyboardService", "PowerAccent");
@@ -53,8 +53,8 @@ namespace winrt::PowerToys::PowerAccentKeyboardService::implementation
void KeyboardListener::SetShowToolbarEvent(ShowToolbar showToolbarEvent)
{
m_showToolbarCb = [trigger = std::move(showToolbarEvent)](LetterKey key, TriggerKey triggerKey) {
trigger(key, triggerKey);
m_showToolbarCb = [trigger = std::move(showToolbarEvent)](LetterKey key) {
trigger(key);
};
}
@@ -152,17 +152,6 @@ namespace winrt::PowerToys::PowerAccentKeyboardService::implementation
return false;
}
void KeyboardListener::BeginShowToolbar(std::chrono::milliseconds delay, LetterKey key, TriggerKey trigger)
{
std::unique_lock<std::mutex> lock(toolbarMutex);
auto result = toolbarCV.wait_for(lock, delay);
if (result == std::cv_status::timeout)
{
m_toolbarVisible = true;
m_showToolbarCb(key, trigger);
}
}
bool KeyboardListener::OnKeyDown(KBDLLHOOKSTRUCT info) noexcept
{
auto letterKey = static_cast<LetterKey>(info.vkCode);
@@ -210,7 +199,7 @@ namespace winrt::PowerToys::PowerAccentKeyboardService::implementation
}
}
if (!m_toolbarVisible && !m_activationKeyHold && letterPressed != LetterKey::None && triggerPressed && !IsSuppressedByGameMode() && !IsForegroundAppExcluded())
if (!m_toolbarVisible && letterPressed != LetterKey::None && triggerPressed && !IsSuppressedByGameMode() && !IsForegroundAppExcluded())
{
Logger::debug(L"Show toolbar. Letter: {}, Trigger: {}", letterPressed, triggerPressed);
@@ -218,21 +207,11 @@ namespace winrt::PowerToys::PowerAccentKeyboardService::implementation
m_triggeredWithSpace = triggerPressed == VK_SPACE;
m_triggeredWithLeftArrow = triggerPressed == VK_LEFT;
m_triggeredWithRightArrow = triggerPressed == VK_RIGHT;
m_activationKeyHold = true;
m_bothKeysPressed = true;
if (toolbarThread != nullptr)
{
toolbarCV.notify_all();
toolbarThread->join();
}
toolbarThread = std::make_unique<std::thread>(std::bind(&KeyboardListener::BeginShowToolbar, this, m_settings.inputTime, letterPressed,static_cast<TriggerKey>(triggerPressed)));
m_toolbarVisible = true;
m_showToolbarCb(letterPressed);
}
if (m_activationKeyHold && triggerPressed && !m_toolbarVisible)
{
return true;
}
else if (m_toolbarVisible && triggerPressed)
if (m_toolbarVisible && triggerPressed)
{
if (triggerPressed == VK_LEFT)
{
@@ -272,9 +251,8 @@ namespace winrt::PowerToys::PowerAccentKeyboardService::implementation
{
letterPressed = LetterKey::None;
if (m_toolbarVisible || m_bothKeysPressed)
if (m_toolbarVisible)
{
m_bothKeysPressed = false;
if (m_stopwatch.elapsed() < m_settings.inputTime)
{
Logger::debug(L"Activation too fast. Do nothing.");
@@ -302,18 +280,11 @@ namespace winrt::PowerToys::PowerAccentKeyboardService::implementation
Logger::debug(L"Hide toolbar event and input char");
m_hideToolbarCb(InputType::Char);
m_toolbarVisible = false;
}
}
auto triggerPressed = info.vkCode;
if (m_activationKeyHold && (letterPressed == LetterKey::None || (triggerPressed == VK_SPACE || triggerPressed == VK_LEFT || triggerPressed == VK_RIGHT)))
{
m_activationKeyHold = false;
toolbarCV.notify_all();
}
return false;
}

View File

@@ -2,7 +2,6 @@
#include "KeyboardListener.g.h"
#include <mutex>
#include <thread>
#include <spdlog/stopwatch.h>
namespace winrt::PowerToys::PowerAccentKeyboardService::implementation
@@ -45,7 +44,6 @@ namespace winrt::PowerToys::PowerAccentKeyboardService::implementation
static LRESULT CALLBACK LowLevelKeyboardProc(int nCode, WPARAM wParam, LPARAM lParam);
private:
void BeginShowToolbar(std::chrono::milliseconds delay, LetterKey key, TriggerKey trigger);
bool OnKeyDown(KBDLLHOOKSTRUCT info) noexcept;
bool OnKeyUp(KBDLLHOOKSTRUCT info) noexcept;
bool IsSuppressedByGameMode();
@@ -53,14 +51,9 @@ namespace winrt::PowerToys::PowerAccentKeyboardService::implementation
static inline KeyboardListener* s_instance;
HHOOK s_llKeyboardHook = nullptr;
std::atomic<bool> m_toolbarVisible;
bool m_activationKeyHold;
bool m_bothKeysPressed = false;
std::unique_ptr<std::thread> toolbarThread;
std::mutex toolbarMutex;
std::condition_variable toolbarCV;
bool m_toolbarVisible;
PowerAccentSettings m_settings;
std::function<void(LetterKey, TriggerKey)> m_showToolbarCb;
std::function<void(LetterKey)> m_showToolbarCb;
std::function<void(InputType)> m_hideToolbarCb;
std::function<void(TriggerKey, bool)> m_nextCharCb;
std::function<bool(LetterKey)> m_isLanguageLetterCb;

View File

@@ -67,7 +67,7 @@ namespace PowerToys
Char
};
[version(1.0), uuid(37197089-5438-4479-af57-30ab3f3c8be4)] delegate void ShowToolbar(LetterKey key, TriggerKey trigger);
[version(1.0), uuid(37197089-5438-4479-af57-30ab3f3c8be4)] delegate void ShowToolbar(LetterKey key);
[version(1.0), uuid(8eb79d6b-1826-424f-9fbc-af21ae19725e)] delegate void HideToolbar(InputType inputType);
[version(1.0), uuid(db72d45c-a5a2-446f-bdc1-506e9121764a)] delegate void NextChar(TriggerKey inputSpace, boolean shiftPressed);
[version(1.0), uuid(20be2919-2b91-4313-b6e0-4c3484fe91ef)] delegate void IsLanguageLetter(LetterKey key, [out] boolean* result);

View File

@@ -34,7 +34,7 @@ namespace Microsoft.PowerToys.Settings.UI.Views
ViewModel.RefreshEnabledState();
}
private void LaunchApp(string appPath)
private void LaunchApp(string appPath, string args)
{
try
{
@@ -43,7 +43,7 @@ namespace Microsoft.PowerToys.Settings.UI.Views
var processStartInfo = new ProcessStartInfo
{
FileName = appPath,
Arguments = string.Empty,
Arguments = args,
WorkingDirectory = dir,
UseShellExecute = true,
Verb = "open",
@@ -64,9 +64,10 @@ namespace Microsoft.PowerToys.Settings.UI.Views
private void CmdPalSettingsDeeplink_Click(object sender, Microsoft.UI.Xaml.RoutedEventArgs e)
{
// Launch CmdPal settings window
string launchPath = "x-cmdpal://settings";
LaunchApp(launchPath);
// Launch CmdPal settings window as normal user using explorer
string launchPath = "explorer.exe";
string launchArgs = "x-cmdpal://settings";
LaunchApp(launchPath, launchArgs);
}
}
}