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.
This commit is contained in:
Hao Liu 2025-05-19 13:00:40 +08:00 committed by GitHub
parent 12e23e23a3
commit 5517c6d504
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
4 changed files with 25 additions and 53 deletions

View File

@ -62,11 +62,11 @@ public partial class PowerAccent : IDisposable
private void SetEvents() 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(() => 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; _visible = true;
_characters = GetCharacters(letterKey); _characters = GetCharacters(letterKey);
_characterDescriptions = GetCharacterDescriptions(_characters); _characterDescriptions = GetCharacterDescriptions(_characters);
_showUnicodeDescription = _settingService.ShowUnicodeDescription; _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) private string[] GetCharacters(LetterKey letterKey)

View File

@ -13,7 +13,7 @@
namespace winrt::PowerToys::PowerAccentKeyboardService::implementation namespace winrt::PowerToys::PowerAccentKeyboardService::implementation
{ {
KeyboardListener::KeyboardListener() : 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; s_instance = this;
LoggerHelpers::init_logger(L"PowerAccent", L"PowerAccentKeyboardService", "PowerAccent"); LoggerHelpers::init_logger(L"PowerAccent", L"PowerAccentKeyboardService", "PowerAccent");
@ -53,8 +53,8 @@ namespace winrt::PowerToys::PowerAccentKeyboardService::implementation
void KeyboardListener::SetShowToolbarEvent(ShowToolbar showToolbarEvent) void KeyboardListener::SetShowToolbarEvent(ShowToolbar showToolbarEvent)
{ {
m_showToolbarCb = [trigger = std::move(showToolbarEvent)](LetterKey key, TriggerKey triggerKey) { m_showToolbarCb = [trigger = std::move(showToolbarEvent)](LetterKey key) {
trigger(key, triggerKey); trigger(key);
}; };
} }
@ -152,17 +152,6 @@ namespace winrt::PowerToys::PowerAccentKeyboardService::implementation
return false; 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 bool KeyboardListener::OnKeyDown(KBDLLHOOKSTRUCT info) noexcept
{ {
auto letterKey = static_cast<LetterKey>(info.vkCode); 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); Logger::debug(L"Show toolbar. Letter: {}, Trigger: {}", letterPressed, triggerPressed);
@ -218,21 +207,11 @@ namespace winrt::PowerToys::PowerAccentKeyboardService::implementation
m_triggeredWithSpace = triggerPressed == VK_SPACE; m_triggeredWithSpace = triggerPressed == VK_SPACE;
m_triggeredWithLeftArrow = triggerPressed == VK_LEFT; m_triggeredWithLeftArrow = triggerPressed == VK_LEFT;
m_triggeredWithRightArrow = triggerPressed == VK_RIGHT; m_triggeredWithRightArrow = triggerPressed == VK_RIGHT;
m_activationKeyHold = true; m_toolbarVisible = true;
m_bothKeysPressed = true; m_showToolbarCb(letterPressed);
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)));
} }
if (m_activationKeyHold && triggerPressed && !m_toolbarVisible) if (m_toolbarVisible && triggerPressed)
{
return true;
}
else if (m_toolbarVisible && triggerPressed)
{ {
if (triggerPressed == VK_LEFT) if (triggerPressed == VK_LEFT)
{ {
@ -272,9 +251,8 @@ namespace winrt::PowerToys::PowerAccentKeyboardService::implementation
{ {
letterPressed = LetterKey::None; letterPressed = LetterKey::None;
if (m_toolbarVisible || m_bothKeysPressed) if (m_toolbarVisible)
{ {
m_bothKeysPressed = false;
if (m_stopwatch.elapsed() < m_settings.inputTime) if (m_stopwatch.elapsed() < m_settings.inputTime)
{ {
Logger::debug(L"Activation too fast. Do nothing."); 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"); Logger::debug(L"Hide toolbar event and input char");
m_hideToolbarCb(InputType::Char); m_hideToolbarCb(InputType::Char);
m_toolbarVisible = false; 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; return false;
} }

View File

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

View File

@ -67,7 +67,7 @@ namespace PowerToys
Char 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(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(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); [version(1.0), uuid(20be2919-2b91-4313-b6e0-4c3484fe91ef)] delegate void IsLanguageLetter(LetterKey key, [out] boolean* result);