[Windows, Keyboard] Enqueue keyboard message (flutter/engine#34222)
This commit is contained in:
@@ -2333,7 +2333,9 @@ FILE: ../../../flutter/shell/platform/windows/keyboard_key_handler.h
|
||||
FILE: ../../../flutter/shell/platform/windows/keyboard_key_handler_unittests.cc
|
||||
FILE: ../../../flutter/shell/platform/windows/keyboard_manager_win32.cc
|
||||
FILE: ../../../flutter/shell/platform/windows/keyboard_manager_win32.h
|
||||
FILE: ../../../flutter/shell/platform/windows/keyboard_win32_common.cc
|
||||
FILE: ../../../flutter/shell/platform/windows/keyboard_win32_common.h
|
||||
FILE: ../../../flutter/shell/platform/windows/keyboard_win32_common_unittests.cc
|
||||
FILE: ../../../flutter/shell/platform/windows/keyboard_win32_unittests.cc
|
||||
FILE: ../../../flutter/shell/platform/windows/platform_handler.cc
|
||||
FILE: ../../../flutter/shell/platform/windows/platform_handler.h
|
||||
|
||||
@@ -79,6 +79,7 @@ source_set("flutter_windows_source") {
|
||||
"keyboard_key_handler.h",
|
||||
"keyboard_manager_win32.cc",
|
||||
"keyboard_manager_win32.h",
|
||||
"keyboard_win32_common.cc",
|
||||
"keyboard_win32_common.h",
|
||||
"platform_handler.cc",
|
||||
"platform_handler.h",
|
||||
@@ -183,6 +184,7 @@ executable("flutter_windows_unittests") {
|
||||
"keyboard_key_channel_handler_unittests.cc",
|
||||
"keyboard_key_embedder_handler_unittests.cc",
|
||||
"keyboard_key_handler_unittests.cc",
|
||||
"keyboard_win32_common_unittests.cc",
|
||||
"keyboard_win32_unittests.cc",
|
||||
"platform_handler_unittests.cc",
|
||||
"platform_handler_win32_unittests.cc",
|
||||
|
||||
@@ -97,21 +97,25 @@ static bool IsPrintable(uint32_t c) {
|
||||
return c >= kMinPrintable && c != kDelete;
|
||||
}
|
||||
|
||||
static bool IsSysAction(UINT action) {
|
||||
return action == WM_SYSKEYDOWN || action == WM_SYSKEYUP ||
|
||||
action == WM_SYSCHAR || action == WM_SYSDEADCHAR;
|
||||
}
|
||||
|
||||
} // namespace
|
||||
|
||||
KeyboardManagerWin32::KeyboardManagerWin32(WindowDelegate* delegate)
|
||||
: window_delegate_(delegate),
|
||||
last_key_is_ctrl_left_down(false),
|
||||
should_synthesize_ctrl_left_up(false) {}
|
||||
should_synthesize_ctrl_left_up(false),
|
||||
processing_event_(false) {}
|
||||
|
||||
void KeyboardManagerWin32::RedispatchEvent(
|
||||
std::unique_ptr<PendingEvent> event) {
|
||||
for (const Win32Message& message : event->session) {
|
||||
// Never redispatch sys keys, because their original messages have been
|
||||
// passed to the system default processor.
|
||||
const bool is_syskey =
|
||||
message.action == WM_SYSKEYDOWN || message.action == WM_SYSKEYUP;
|
||||
if (is_syskey) {
|
||||
if (IsSysAction(message.action)) {
|
||||
continue;
|
||||
}
|
||||
pending_redispatches_.push_back(message);
|
||||
@@ -143,69 +147,6 @@ bool KeyboardManagerWin32::RemoveRedispatchedMessage(UINT const action,
|
||||
return false;
|
||||
}
|
||||
|
||||
void KeyboardManagerWin32::OnKey(std::unique_ptr<PendingEvent> event,
|
||||
OnKeyCallback callback) {
|
||||
if (IsKeyDownAltRight(event->action, event->key, event->extended)) {
|
||||
if (last_key_is_ctrl_left_down) {
|
||||
should_synthesize_ctrl_left_up = true;
|
||||
}
|
||||
}
|
||||
if (IsKeyDownCtrlLeft(event->action, event->key)) {
|
||||
last_key_is_ctrl_left_down = true;
|
||||
ctrl_left_scancode = event->scancode;
|
||||
should_synthesize_ctrl_left_up = false;
|
||||
} else {
|
||||
last_key_is_ctrl_left_down = false;
|
||||
}
|
||||
if (IsKeyUpAltRight(event->action, event->key, event->extended)) {
|
||||
if (should_synthesize_ctrl_left_up) {
|
||||
should_synthesize_ctrl_left_up = false;
|
||||
const LPARAM lParam =
|
||||
(1 /* repeat_count */ << 0) | (ctrl_left_scancode << 16) |
|
||||
(0 /* extended */ << 24) | (1 /* prev_state */ << 30) |
|
||||
(1 /* transition */ << 31);
|
||||
window_delegate_->Win32DispatchMessage(WM_KEYUP, VK_CONTROL, lParam);
|
||||
}
|
||||
}
|
||||
|
||||
const PendingEvent clone = *event;
|
||||
window_delegate_->OnKey(clone.key, clone.scancode, clone.action,
|
||||
clone.character, clone.extended, clone.was_down,
|
||||
[this, event = event.release(),
|
||||
callback = std::move(callback)](bool handled) {
|
||||
callback(std::unique_ptr<PendingEvent>(event),
|
||||
handled);
|
||||
});
|
||||
}
|
||||
|
||||
void KeyboardManagerWin32::DispatchReadyTexts() {
|
||||
auto front = pending_texts_.begin();
|
||||
for (; front != pending_texts_.end() && front->ready; ++front) {
|
||||
window_delegate_->OnText(front->content);
|
||||
}
|
||||
pending_texts_.erase(pending_texts_.begin(), front);
|
||||
}
|
||||
|
||||
void KeyboardManagerWin32::HandleOnKeyResult(
|
||||
std::unique_ptr<PendingEvent> event,
|
||||
bool handled,
|
||||
std::list<PendingText>::iterator pending_text) {
|
||||
if (pending_text != pending_texts_.end()) {
|
||||
if (pending_text->placeholder || handled) {
|
||||
pending_texts_.erase(pending_text);
|
||||
} else {
|
||||
pending_text->ready = true;
|
||||
}
|
||||
DispatchReadyTexts();
|
||||
}
|
||||
|
||||
if (handled) {
|
||||
return;
|
||||
}
|
||||
|
||||
RedispatchEvent(std::move(event));
|
||||
}
|
||||
|
||||
bool KeyboardManagerWin32::HandleMessage(UINT const action,
|
||||
WPARAM const wparam,
|
||||
LPARAM const lparam) {
|
||||
@@ -221,7 +162,6 @@ bool KeyboardManagerWin32::HandleMessage(UINT const action,
|
||||
Win32Message{.action = action, .wparam = wparam, .lparam = lparam};
|
||||
current_session_.push_back(message);
|
||||
|
||||
std::u16string text;
|
||||
char32_t code_point;
|
||||
if (message.IsHighSurrogate()) {
|
||||
// A high surrogate is always followed by a low surrogate. Process the
|
||||
@@ -237,14 +177,11 @@ bool KeyboardManagerWin32::HandleMessage(UINT const action,
|
||||
}
|
||||
// A low surrogate always follows a high surrogate, marking the end of
|
||||
// a char session. Process the session after the if clause.
|
||||
text.push_back(static_cast<wchar_t>(last_message->wparam));
|
||||
text.push_back(static_cast<wchar_t>(message.wparam));
|
||||
code_point =
|
||||
CodePointFromSurrogatePair(last_message->wparam, message.wparam);
|
||||
} else {
|
||||
// A non-surrogate character always appears alone. Process the session
|
||||
// after the if clause.
|
||||
text.push_back(static_cast<wchar_t>(message.wparam));
|
||||
code_point = static_cast<wchar_t>(message.wparam);
|
||||
}
|
||||
|
||||
@@ -280,53 +217,30 @@ bool KeyboardManagerWin32::HandleMessage(UINT const action,
|
||||
.was_down = was_down,
|
||||
.session = std::move(current_session_),
|
||||
});
|
||||
// Compute the text that might be dispatched later.
|
||||
//
|
||||
// Of the messages handled here, only WM_CHAR should be treated as
|
||||
// characters. WM_SYS*CHAR are not part of text input, and WM_DEADCHAR
|
||||
// will be incorporated into a later WM_CHAR with the full character.
|
||||
//
|
||||
// Checking `character` filters out non-printable event characters.
|
||||
std::list<PendingText>::iterator pending_text;
|
||||
if (action == WM_CHAR) {
|
||||
bool valid = character != 0 && IsPrintable(wparam);
|
||||
pending_texts_.push_back(PendingText{
|
||||
.ready = false,
|
||||
.content = text,
|
||||
.placeholder = !valid,
|
||||
});
|
||||
pending_text = std::prev(pending_texts_.end());
|
||||
} else {
|
||||
pending_text = pending_texts_.end();
|
||||
}
|
||||
// SYS messages must not be handled by `HandleMessage` or be
|
||||
// redispatched.
|
||||
const bool is_syskey = action == WM_SYSCHAR || action == WM_SYSDEADCHAR;
|
||||
OnKey(std::move(event),
|
||||
[this, pending_text, is_syskey](
|
||||
std::unique_ptr<PendingEvent> event, bool handled) {
|
||||
bool real_handled = handled || is_syskey;
|
||||
HandleOnKeyResult(std::move(event), handled, pending_text);
|
||||
});
|
||||
return !is_syskey;
|
||||
|
||||
pending_events_.push_back(std::move(event));
|
||||
ProcessNextEvent();
|
||||
|
||||
// SYS messages must not be consumed by `HandleMessage` so that they are
|
||||
// forwarded to the system.
|
||||
return !IsSysAction(action);
|
||||
}
|
||||
|
||||
// If the charcter session is not preceded by a key down message, dispatch
|
||||
// the OnText immediately.
|
||||
|
||||
// If the charcter session is not preceded by a key down message,
|
||||
// mark PendingEvent::action as WM_CHAR, informing |PerformProcessEvent|
|
||||
// to dispatch the text content immediately.
|
||||
//
|
||||
// Only WM_CHAR should be treated as characters. WM_SYS*CHAR are not part
|
||||
// of text input, and WM_DEADCHAR will be incorporated into a later
|
||||
// WM_CHAR with the full character.
|
||||
//
|
||||
// Also filter out ASCII control characters, which are sent as WM_CHAR
|
||||
// events for all control key shortcuts.
|
||||
current_session_.clear();
|
||||
if (action == WM_CHAR) {
|
||||
pending_texts_.push_back(PendingText{
|
||||
.ready = true,
|
||||
.content = text,
|
||||
auto event = std::make_unique<PendingEvent>(PendingEvent{
|
||||
.action = WM_CHAR,
|
||||
.character = code_point,
|
||||
.session = std::move(current_session_),
|
||||
});
|
||||
DispatchReadyTexts();
|
||||
pending_events_.push_back(std::move(event));
|
||||
ProcessNextEvent();
|
||||
}
|
||||
return true;
|
||||
}
|
||||
@@ -338,38 +252,64 @@ bool KeyboardManagerWin32::HandleMessage(UINT const action,
|
||||
if (wparam == VK_PACKET) {
|
||||
return false;
|
||||
}
|
||||
current_session_.clear();
|
||||
current_session_.push_back(
|
||||
Win32Message{.action = action, .wparam = wparam, .lparam = lparam});
|
||||
const bool is_keydown_message =
|
||||
(action == WM_KEYDOWN || action == WM_SYSKEYDOWN);
|
||||
// Check if this key produces a character. If so, the key press should
|
||||
// be sent with the character produced at WM_CHAR. Store the produced
|
||||
// keycode (it's not accessible from WM_CHAR) to be used in WM_CHAR.
|
||||
//
|
||||
// Messages with Control or Win modifiers down are never considered as
|
||||
// character messages. This allows key combinations such as "CTRL + Digit"
|
||||
// to properly produce key down events even though `MapVirtualKey` returns
|
||||
// a valid character. See https://github.com/flutter/flutter/issues/85587.
|
||||
unsigned int character = window_delegate_->Win32MapVkToChar(wparam);
|
||||
UINT next_key_message = PeekNextMessageType(WM_KEYFIRST, WM_KEYLAST);
|
||||
bool has_wm_char =
|
||||
(next_key_message == WM_DEADCHAR ||
|
||||
next_key_message == WM_SYSDEADCHAR || next_key_message == WM_CHAR ||
|
||||
next_key_message == WM_SYSCHAR);
|
||||
if (character > 0 && is_keydown_message && has_wm_char) {
|
||||
// This key down message has following char events. Process later,
|
||||
// because the character for the OnKey should be decided by the char
|
||||
// events. Consider this event as handled.
|
||||
return true;
|
||||
}
|
||||
|
||||
// Resolve session: A non-char key event.
|
||||
const uint8_t scancode = (lparam >> 16) & 0xff;
|
||||
const bool extended = ((lparam >> 24) & 0x01) == 0x01;
|
||||
// If the key is a modifier, get its side.
|
||||
const uint16_t key_code = ResolveKeyCode(wparam, extended, scancode);
|
||||
const bool was_down = lparam & 0x40000000;
|
||||
|
||||
// Detect a pattern of key events in order to forge a CtrlLeft up event.
|
||||
// See |IsKeyDownAltRight| for explanation.
|
||||
if (IsKeyDownAltRight(action, key_code, extended)) {
|
||||
if (last_key_is_ctrl_left_down) {
|
||||
should_synthesize_ctrl_left_up = true;
|
||||
}
|
||||
}
|
||||
if (IsKeyDownCtrlLeft(action, key_code)) {
|
||||
last_key_is_ctrl_left_down = true;
|
||||
ctrl_left_scancode = scancode;
|
||||
should_synthesize_ctrl_left_up = false;
|
||||
} else {
|
||||
last_key_is_ctrl_left_down = false;
|
||||
}
|
||||
if (IsKeyUpAltRight(action, key_code, extended)) {
|
||||
if (should_synthesize_ctrl_left_up) {
|
||||
should_synthesize_ctrl_left_up = false;
|
||||
const LPARAM lParam =
|
||||
(1 /* repeat_count */ << 0) | (ctrl_left_scancode << 16) |
|
||||
(0 /* extended */ << 24) | (1 /* prev_state */ << 30) |
|
||||
(1 /* transition */ << 31);
|
||||
window_delegate_->Win32DispatchMessage(WM_KEYUP, VK_CONTROL, lParam);
|
||||
}
|
||||
}
|
||||
|
||||
current_session_.clear();
|
||||
current_session_.push_back(
|
||||
Win32Message{.action = action, .wparam = wparam, .lparam = lparam});
|
||||
const bool is_keydown_message =
|
||||
(action == WM_KEYDOWN || action == WM_SYSKEYDOWN);
|
||||
// Check if this key produces a character by peeking if this key down
|
||||
// message has a following char message. Certain key messages are not
|
||||
// followed by char messages even though `MapVirtualKey` returns a valid
|
||||
// character (such as Ctrl + Digit, see
|
||||
// https://github.com/flutter/flutter/issues/85587 ).
|
||||
unsigned int character = window_delegate_->Win32MapVkToChar(wparam);
|
||||
UINT next_key_action = PeekNextMessageType(WM_KEYFIRST, WM_KEYLAST);
|
||||
bool has_char_action =
|
||||
(next_key_action == WM_DEADCHAR ||
|
||||
next_key_action == WM_SYSDEADCHAR || next_key_action == WM_CHAR ||
|
||||
next_key_action == WM_SYSCHAR);
|
||||
if (character > 0 && is_keydown_message && has_char_action) {
|
||||
// This key down message has a following char message. Process this
|
||||
// session in the char message, because the character for the key call
|
||||
// should be decided by the char events. Consider this message as
|
||||
// handled.
|
||||
return true;
|
||||
}
|
||||
|
||||
// This key down message is not followed by a char message. Conclude this
|
||||
// session.
|
||||
auto event = std::make_unique<PendingEvent>(PendingEvent{
|
||||
.key = key_code,
|
||||
.scancode = scancode,
|
||||
@@ -379,16 +319,11 @@ bool KeyboardManagerWin32::HandleMessage(UINT const action,
|
||||
.was_down = was_down,
|
||||
.session = std::move(current_session_),
|
||||
});
|
||||
// SYS messages must not be handled by `HandleMessage` or be
|
||||
// redispatched.
|
||||
const bool is_syskey = action == WM_SYSKEYDOWN || action == WM_SYSKEYUP;
|
||||
OnKey(
|
||||
std::move(event),
|
||||
[this, is_syskey](std::unique_ptr<PendingEvent> event, bool handled) {
|
||||
bool real_handled = handled || is_syskey;
|
||||
HandleOnKeyResult(std::move(event), handled, pending_texts_.end());
|
||||
});
|
||||
return !is_syskey;
|
||||
pending_events_.push_back(std::move(event));
|
||||
ProcessNextEvent();
|
||||
// SYS messages must not be consumed by `HandleMessage` so that they are
|
||||
// forwarded to the system.
|
||||
return !IsSysAction(action);
|
||||
}
|
||||
default:
|
||||
assert(false);
|
||||
@@ -396,6 +331,81 @@ bool KeyboardManagerWin32::HandleMessage(UINT const action,
|
||||
return false;
|
||||
}
|
||||
|
||||
void KeyboardManagerWin32::ProcessNextEvent() {
|
||||
if (processing_event_ || pending_events_.empty()) {
|
||||
return;
|
||||
}
|
||||
processing_event_ = true;
|
||||
auto pending_event = std::move(pending_events_.front());
|
||||
pending_events_.pop_front();
|
||||
PerformProcessEvent(std::move(pending_event), [this] {
|
||||
assert(processing_event_);
|
||||
processing_event_ = false;
|
||||
ProcessNextEvent();
|
||||
});
|
||||
}
|
||||
|
||||
void KeyboardManagerWin32::PerformProcessEvent(
|
||||
std::unique_ptr<PendingEvent> event,
|
||||
std::function<void()> callback) {
|
||||
// PendingEvent::action being WM_CHAR means this is a char message without
|
||||
// a preceding key message, and should be dispatched immediately.
|
||||
if (event->action == WM_CHAR) {
|
||||
DispatchText(*event);
|
||||
callback();
|
||||
return;
|
||||
}
|
||||
|
||||
// A unique_ptr can't be sent into a lambda without C++23's
|
||||
// move_only_function. Until then, `event` is sent as a raw pointer, hoping
|
||||
// WindowDelegate::OnKey to correctly call it once and only once.
|
||||
PendingEvent* event_p = event.release();
|
||||
window_delegate_->OnKey(
|
||||
event_p->key, event_p->scancode, event_p->action, event_p->character,
|
||||
event_p->extended, event_p->was_down,
|
||||
[this, event_p, callback = std::move(callback)](bool handled) {
|
||||
HandleOnKeyResult(std::unique_ptr<PendingEvent>(event_p), handled);
|
||||
callback();
|
||||
});
|
||||
}
|
||||
|
||||
void KeyboardManagerWin32::HandleOnKeyResult(
|
||||
std::unique_ptr<PendingEvent> event,
|
||||
bool framework_handled) {
|
||||
const UINT last_action = event->session.back().action;
|
||||
// SYS messages must not be redispached, and their text content is not
|
||||
// dispatched either.
|
||||
bool handled = framework_handled || IsSysAction(last_action);
|
||||
|
||||
if (handled) {
|
||||
return;
|
||||
}
|
||||
|
||||
// Only WM_CHAR should be treated as characters. WM_SYS*CHAR are not part of
|
||||
// text input, and WM_DEADCHAR will be incorporated into a later WM_CHAR with
|
||||
// the full character.
|
||||
if (last_action == WM_CHAR) {
|
||||
DispatchText(*event);
|
||||
}
|
||||
|
||||
RedispatchEvent(std::move(event));
|
||||
}
|
||||
|
||||
void KeyboardManagerWin32::DispatchText(const PendingEvent& event) {
|
||||
// Check if the character is printable based on the last wparam, which works
|
||||
// even if the last wparam is a low surrogate, because the only unprintable
|
||||
// keys defined by `IsPrintable` are certain characters at lower ASCII range.
|
||||
// These ASCII control characters are sent as WM_CHAR events for all control
|
||||
// key shortcuts.
|
||||
assert(!event.session.empty());
|
||||
bool is_printable = IsPrintable(event.session.back().wparam);
|
||||
bool valid = event.character != 0 && is_printable;
|
||||
if (valid) {
|
||||
auto text = EncodeUtf16(event.character);
|
||||
window_delegate_->OnText(text);
|
||||
}
|
||||
}
|
||||
|
||||
UINT KeyboardManagerWin32::PeekNextMessageType(UINT wMsgFilterMin,
|
||||
UINT wMsgFilterMax) {
|
||||
MSG next_message;
|
||||
|
||||
@@ -6,6 +6,7 @@
|
||||
#define FLUTTER_SHELL_PLATFORM_WINDOWS_KEYBOARD_MANAGER_H_
|
||||
|
||||
#include <windows.h>
|
||||
#include <atomic>
|
||||
#include <deque>
|
||||
#include <functional>
|
||||
#include <map>
|
||||
@@ -15,16 +16,16 @@ namespace flutter {
|
||||
// Handles keyboard and text messages on Win32.
|
||||
//
|
||||
// |KeyboardManagerWin32| consumes raw Win32 messages related to key and chars,
|
||||
// and converts them to |OnKey| or |OnText| calls suitable for
|
||||
// |KeyboardKeyHandler|.
|
||||
// and converts them to key calls (|WindowDelegate::OnKey|) and possibly
|
||||
// text calls (|WindowDelegate::OnText|).
|
||||
//
|
||||
// |KeyboardManagerWin32| requires a |WindowDelegate| to define how to
|
||||
// access Win32 system calls (to allow mocking) and where to send the results
|
||||
// of |OnKey| and |OnText| to.
|
||||
// of key calls and text calls to.
|
||||
//
|
||||
// Typically, |KeyboardManagerWin32| is owned by a |WindowWin32|, which also
|
||||
// implements the window delegate. The |OnKey| and |OnText| results are
|
||||
// passed to those of |WindowWin32|'s, and consequently, those of
|
||||
// implements the window delegate. The key calls and text calls are forwarded
|
||||
// to those of |WindowWin32|'s, and consequently, those of
|
||||
// |FlutterWindowsView|'s.
|
||||
//
|
||||
// ## Terminology
|
||||
@@ -38,10 +39,12 @@ namespace flutter {
|
||||
// * Session: One to three messages that should be processed together, such
|
||||
// as a key down message followed by char messages.
|
||||
// * Event: A FlutterKeyEvent/ui.KeyData sent to the framework.
|
||||
// * Call: A call to |WindowDelegate::OnKey| or |WindowDelegate::OnText|,
|
||||
// which contains semi-processed messages.
|
||||
class KeyboardManagerWin32 {
|
||||
public:
|
||||
// Define how the keyboard manager accesses Win32 system calls (to allow
|
||||
// mocking) and sends the results of |OnKey| and |OnText|.
|
||||
// mocking) and sends key calls and and text calls.
|
||||
//
|
||||
// Typically implemented by |WindowWin32|.
|
||||
class WindowDelegate {
|
||||
@@ -54,6 +57,8 @@ class KeyboardManagerWin32 {
|
||||
virtual void OnText(const std::u16string& text) = 0;
|
||||
|
||||
// Called when raw keyboard input occurs.
|
||||
//
|
||||
// The `callback` must be called exactly once.
|
||||
virtual void OnKey(int key,
|
||||
int scancode,
|
||||
int action,
|
||||
@@ -141,21 +146,36 @@ class KeyboardManagerWin32 {
|
||||
bool placeholder = false;
|
||||
};
|
||||
|
||||
// Returns true if it's a new event, or false if it's a redispatched event.
|
||||
void OnKey(std::unique_ptr<PendingEvent> event, OnKeyCallback callback);
|
||||
// Resume processing the pending events.
|
||||
//
|
||||
// If there is at least one pending event and no event is being processed,
|
||||
// the oldest pending event will be handed over to |PerformProcessEvent|.
|
||||
// After the event is processed, the next pending event will be automatically
|
||||
// started, until there are no pending events left.
|
||||
//
|
||||
// Otherwise, this call is a no-op.
|
||||
void ProcessNextEvent();
|
||||
|
||||
// From `pending_texts_`, pop all front elements that are ready, dispatch
|
||||
// them to |OnText|, and remove them.
|
||||
void DispatchReadyTexts();
|
||||
// Process an event and call `callback` when it's completed.
|
||||
//
|
||||
// The `callback` is constructed by |ProcessNextEvent| to start the next
|
||||
// event, and must be called exactly once.
|
||||
void PerformProcessEvent(std::unique_ptr<PendingEvent> event,
|
||||
std::function<void()> callback);
|
||||
|
||||
// Handle the result of |OnKey|, which might dispatch the text result to
|
||||
// |OnText|.
|
||||
// Handle the result of |WindowDelegate::OnKey|, possibly dispatching the text
|
||||
// result to |WindowDelegate::OnText| and then redispatching.
|
||||
//
|
||||
// The `pending_text` is either a valid iterator of `pending_texts`, or its
|
||||
// end(). In the latter case, this OnKey message does not contain a text.
|
||||
void HandleOnKeyResult(std::unique_ptr<PendingEvent> event,
|
||||
bool handled,
|
||||
std::list<PendingText>::iterator pending_text);
|
||||
bool framework_handled);
|
||||
|
||||
// Dispatch the text content of a |PendingEvent| to |WindowDelegate::OnText|.
|
||||
//
|
||||
// If the content is empty of invalid, |WindowDelegate::OnText| will not be
|
||||
// called.
|
||||
void DispatchText(const PendingEvent& event);
|
||||
|
||||
// Returns the type of the next WM message.
|
||||
//
|
||||
@@ -176,7 +196,7 @@ class KeyboardManagerWin32 {
|
||||
// Keeps track of all messages during the current session.
|
||||
//
|
||||
// At the end of a session, it is moved to the `PendingEvent`, which is
|
||||
// passed to `OnKey`.
|
||||
// passed to `WindowDelegate::OnKey`.
|
||||
std::vector<Win32Message> current_session_;
|
||||
|
||||
// Whether the last event is a CtrlLeft key down.
|
||||
@@ -194,13 +214,14 @@ class KeyboardManagerWin32 {
|
||||
// This is used to resolve a corner case described in |IsKeyDownAltRight|.
|
||||
bool should_synthesize_ctrl_left_up;
|
||||
|
||||
// A queue of potential texts derived from char messages.
|
||||
// Store the messages coming from |HandleMessage|.
|
||||
//
|
||||
// The text might or might not be ready when they're added, and they might
|
||||
// become ready or removed later. `DispatchReadyTexts` is used to dispatch all
|
||||
// ready texts from the front to `OnText`. This queue is used to ensure
|
||||
// they're dispatched in their arrival order.
|
||||
std::list<PendingText> pending_texts_;
|
||||
// Only one message is processed at a time. The next one will not start
|
||||
// until the framework has responded to the previous message.
|
||||
std::deque<std::unique_ptr<PendingEvent>> pending_events_;
|
||||
|
||||
// Whether a message is being processed.
|
||||
std::atomic<bool> processing_event_;
|
||||
|
||||
// The queue of messages that have been redispatched to the system but have
|
||||
// not yet been received for a second time.
|
||||
|
||||
@@ -0,0 +1,28 @@
|
||||
// Copyright 2013 The Flutter Authors. All rights reserved.
|
||||
// Use of this source code is governed by a BSD-style license that can be
|
||||
// found in the LICENSE file.
|
||||
|
||||
#include "keyboard_win32_common.h"
|
||||
|
||||
namespace flutter {
|
||||
|
||||
std::u16string EncodeUtf16(char32_t character) {
|
||||
// Algorithm: https://en.wikipedia.org/wiki/UTF-16#Description
|
||||
std::u16string result;
|
||||
// Invalid value.
|
||||
assert(!(character >= 0xD800 && character <= 0xDFFF) &&
|
||||
!(character > 0x10FFFF));
|
||||
if ((character >= 0xD800 && character <= 0xDFFF) || (character > 0x10FFFF)) {
|
||||
return result;
|
||||
}
|
||||
if (character <= 0xD7FF || (character >= 0xE000 && character <= 0xFFFF)) {
|
||||
result.push_back((char16_t)character);
|
||||
return result;
|
||||
}
|
||||
uint32_t remnant = character - 0x10000;
|
||||
result.push_back((remnant >> 10) + 0xD800);
|
||||
result.push_back((remnant & 0x3FF) + 0xDC00);
|
||||
return result;
|
||||
}
|
||||
|
||||
} // namespace flutter
|
||||
@@ -5,7 +5,9 @@
|
||||
#ifndef FLUTTER_SHELL_PLATFORM_WINDOWS_KEYBOARD_WIN32_COMMON_H_
|
||||
#define FLUTTER_SHELL_PLATFORM_WINDOWS_KEYBOARD_WIN32_COMMON_H_
|
||||
|
||||
#include <assert.h>
|
||||
#include <stdint.h>
|
||||
#include <string>
|
||||
|
||||
namespace flutter {
|
||||
|
||||
@@ -24,6 +26,12 @@ inline uint32_t UndeadChar(uint32_t ch) {
|
||||
return ch & ~kDeadKeyCharMask;
|
||||
}
|
||||
|
||||
// Encode a Unicode codepoint into a UTF-16 string.
|
||||
//
|
||||
// If the codepoint is invalid, this function throws an assertion error, and
|
||||
// returns an empty string.
|
||||
std::u16string EncodeUtf16(char32_t character);
|
||||
|
||||
} // namespace flutter
|
||||
|
||||
#endif // FLUTTER_SHELL_PLATFORM_WINDOWS_KEYBOARD_WIN32_COMMON_H_
|
||||
|
||||
@@ -0,0 +1,34 @@
|
||||
// Copyright 2013 The Flutter Authors. All rights reserved.
|
||||
// Use of this source code is governed by a BSD-style license that can be
|
||||
// found in the LICENSE file.
|
||||
#include "flutter/shell/platform/windows/keyboard_win32_common.h"
|
||||
|
||||
#include "gtest/gtest.h"
|
||||
|
||||
namespace flutter {
|
||||
namespace testing {
|
||||
|
||||
TEST(KeyboardWin32CommonTest, EncodeUtf16) {
|
||||
std::u16string result;
|
||||
|
||||
result = EncodeUtf16(0x24);
|
||||
EXPECT_EQ(result.size(), 1);
|
||||
EXPECT_EQ(result[0], 0x24);
|
||||
|
||||
result = EncodeUtf16(0x20AC);
|
||||
EXPECT_EQ(result.size(), 1);
|
||||
EXPECT_EQ(result[0], 0x20AC);
|
||||
|
||||
result = EncodeUtf16(0x10437);
|
||||
EXPECT_EQ(result.size(), 2);
|
||||
EXPECT_EQ(result[0], 0xD801);
|
||||
EXPECT_EQ(result[1], 0xDC37);
|
||||
|
||||
result = EncodeUtf16(0x24B62);
|
||||
EXPECT_EQ(result.size(), 2);
|
||||
EXPECT_EQ(result[0], 0xD852);
|
||||
EXPECT_EQ(result[1], 0xDF62);
|
||||
}
|
||||
|
||||
} // namespace testing
|
||||
} // namespace flutter
|
||||
@@ -1954,57 +1954,6 @@ TEST(KeyboardTest, UpOnlyImeEventsAreCorrectlyHandled) {
|
||||
clear_key_calls();
|
||||
}
|
||||
|
||||
TEST(KeyboardTest, DisorderlyRespondedEvents) {
|
||||
KeyboardTester tester;
|
||||
|
||||
// Store callbacks to manually call them.
|
||||
std::vector<MockKeyResponseController::ResponseCallback> recorded_callbacks;
|
||||
tester.LateResponding(
|
||||
[&recorded_callbacks](
|
||||
const FlutterKeyEvent* event,
|
||||
MockKeyResponseController::ResponseCallback callback) {
|
||||
recorded_callbacks.push_back(callback);
|
||||
});
|
||||
|
||||
// Press A
|
||||
tester.InjectKeyboardChanges(std::vector<KeyboardChange>{
|
||||
WmKeyDownInfo{kVirtualKeyA, kScanCodeKeyA, kNotExtended, kWasUp}.Build(
|
||||
kWmResultZero),
|
||||
WmCharInfo{'a', kScanCodeKeyA, kNotExtended, kWasUp}.Build(
|
||||
kWmResultZero)});
|
||||
|
||||
// Press B
|
||||
tester.InjectKeyboardChanges(std::vector<KeyboardChange>{
|
||||
WmKeyDownInfo{kVirtualKeyB, kScanCodeKeyB, kNotExtended, kWasUp}.Build(
|
||||
kWmResultZero),
|
||||
WmCharInfo{'b', kScanCodeKeyB, kNotExtended, kWasUp}.Build(
|
||||
kWmResultZero)});
|
||||
|
||||
EXPECT_EQ(key_calls.size(), 2);
|
||||
EXPECT_EQ(recorded_callbacks.size(), 2);
|
||||
clear_key_calls();
|
||||
EXPECT_EQ(tester.RedispatchedMessageCountAndClear(), 0);
|
||||
|
||||
// Resolve the second event first to test disordered responses.
|
||||
recorded_callbacks.back()(false);
|
||||
|
||||
EXPECT_EQ(key_calls.size(), 0);
|
||||
clear_key_calls();
|
||||
// TODO(dkwingsmt): This should probably be 0. Redispatching the messages of
|
||||
// the second event this early means that the messages are not redispatched
|
||||
// in the order of arrival. https://github.com/flutter/flutter/issues/98308
|
||||
EXPECT_EQ(tester.RedispatchedMessageCountAndClear(), 2);
|
||||
|
||||
// Resolve the first event.
|
||||
recorded_callbacks.front()(false);
|
||||
|
||||
EXPECT_EQ(key_calls.size(), 2);
|
||||
EXPECT_CALL_IS_TEXT(key_calls[0], u"a");
|
||||
EXPECT_CALL_IS_TEXT(key_calls[1], u"b");
|
||||
clear_key_calls();
|
||||
EXPECT_EQ(tester.RedispatchedMessageCountAndClear(), 2);
|
||||
}
|
||||
|
||||
// Regression test for a crash in an earlier implementation.
|
||||
//
|
||||
// In real life, the framework responds slowly. The next real event might
|
||||
@@ -2038,24 +1987,27 @@ TEST(KeyboardTest, SlowFrameworkResponse) {
|
||||
WmCharInfo{'a', kScanCodeKeyA, kNotExtended, kWasDown}.Build(
|
||||
kWmResultZero)});
|
||||
|
||||
EXPECT_EQ(key_calls.size(), 2);
|
||||
EXPECT_EQ(recorded_callbacks.size(), 2);
|
||||
clear_key_calls();
|
||||
EXPECT_EQ(key_calls.size(), 1);
|
||||
EXPECT_CALL_IS_EVENT(key_calls[0], kFlutterKeyEventTypeDown, kPhysicalKeyA,
|
||||
kLogicalKeyA, "a", kNotSynthesized);
|
||||
EXPECT_EQ(recorded_callbacks.size(), 1);
|
||||
EXPECT_EQ(tester.RedispatchedMessageCountAndClear(), 0);
|
||||
|
||||
// The first response.
|
||||
recorded_callbacks.front()(false);
|
||||
|
||||
EXPECT_EQ(key_calls.size(), 1);
|
||||
EXPECT_CALL_IS_TEXT(key_calls[0], u"a");
|
||||
clear_key_calls();
|
||||
EXPECT_EQ(key_calls.size(), 3);
|
||||
EXPECT_EQ(recorded_callbacks.size(), 2);
|
||||
EXPECT_CALL_IS_TEXT(key_calls[1], u"a");
|
||||
EXPECT_CALL_IS_EVENT(key_calls[2], kFlutterKeyEventTypeRepeat, kPhysicalKeyA,
|
||||
kLogicalKeyA, "a", kNotSynthesized);
|
||||
EXPECT_EQ(tester.RedispatchedMessageCountAndClear(), 2);
|
||||
|
||||
// The second response.
|
||||
recorded_callbacks.back()(false);
|
||||
|
||||
EXPECT_EQ(key_calls.size(), 1);
|
||||
EXPECT_CALL_IS_TEXT(key_calls[0], u"a");
|
||||
EXPECT_EQ(key_calls.size(), 4);
|
||||
EXPECT_CALL_IS_TEXT(key_calls[3], u"a");
|
||||
clear_key_calls();
|
||||
EXPECT_EQ(tester.RedispatchedMessageCountAndClear(), 2);
|
||||
}
|
||||
@@ -2102,18 +2054,17 @@ TEST(KeyboardTest, SlowFrameworkResponseForIdenticalEvents) {
|
||||
WmKeyUpInfo{kVirtualKeyA, kScanCodeKeyA, kNotExtended}.Build(
|
||||
kWmResultZero)});
|
||||
|
||||
EXPECT_EQ(key_calls.size(), 1);
|
||||
EXPECT_CALL_IS_EVENT(key_calls[0], kFlutterKeyEventTypeUp, kPhysicalKeyA,
|
||||
kLogicalKeyA, "", kNotSynthesized);
|
||||
clear_key_calls();
|
||||
EXPECT_EQ(key_calls.size(), 0);
|
||||
EXPECT_EQ(tester.RedispatchedMessageCountAndClear(), 0);
|
||||
|
||||
// The first down event responded with false.
|
||||
EXPECT_EQ(recorded_callbacks.size(), 2);
|
||||
EXPECT_EQ(recorded_callbacks.size(), 1);
|
||||
recorded_callbacks.front()(false);
|
||||
|
||||
EXPECT_EQ(key_calls.size(), 1);
|
||||
EXPECT_EQ(key_calls.size(), 2);
|
||||
EXPECT_CALL_IS_TEXT(key_calls[0], u"a");
|
||||
EXPECT_CALL_IS_EVENT(key_calls[1], kFlutterKeyEventTypeUp, kPhysicalKeyA,
|
||||
kLogicalKeyA, "", kNotSynthesized);
|
||||
clear_key_calls();
|
||||
EXPECT_EQ(tester.RedispatchedMessageCountAndClear(), 2);
|
||||
|
||||
@@ -2124,21 +2075,28 @@ TEST(KeyboardTest, SlowFrameworkResponseForIdenticalEvents) {
|
||||
WmCharInfo{'a', kScanCodeKeyA, kNotExtended, kWasUp}.Build(
|
||||
kWmResultZero)});
|
||||
|
||||
// Nothing more was dispatched because the first up event hasn't been
|
||||
// responded yet.
|
||||
EXPECT_EQ(recorded_callbacks.size(), 2);
|
||||
EXPECT_EQ(key_calls.size(), 0);
|
||||
EXPECT_EQ(tester.RedispatchedMessageCountAndClear(), 0);
|
||||
|
||||
// The first up event responded with false, which was redispatched, and caused
|
||||
// the down event to be dispatched.
|
||||
recorded_callbacks.back()(false);
|
||||
EXPECT_EQ(key_calls.size(), 1);
|
||||
EXPECT_CALL_IS_EVENT(key_calls[0], kFlutterKeyEventTypeDown, kPhysicalKeyA,
|
||||
kLogicalKeyA, "a", kNotSynthesized);
|
||||
clear_key_calls();
|
||||
EXPECT_EQ(tester.RedispatchedMessageCountAndClear(), 0);
|
||||
EXPECT_EQ(recorded_callbacks.size(), 3);
|
||||
EXPECT_EQ(tester.RedispatchedMessageCountAndClear(), 1);
|
||||
|
||||
// Release A again
|
||||
tester.InjectKeyboardChanges(std::vector<KeyboardChange>{
|
||||
WmKeyUpInfo{kVirtualKeyA, kScanCodeKeyA, kNotExtended}.Build(
|
||||
kWmResultZero)});
|
||||
|
||||
EXPECT_EQ(key_calls.size(), 1);
|
||||
EXPECT_CALL_IS_EVENT(key_calls[0], kFlutterKeyEventTypeUp, kPhysicalKeyA,
|
||||
kLogicalKeyA, "", kNotSynthesized);
|
||||
clear_key_calls();
|
||||
EXPECT_EQ(key_calls.size(), 0);
|
||||
EXPECT_EQ(tester.RedispatchedMessageCountAndClear(), 0);
|
||||
}
|
||||
|
||||
@@ -2359,26 +2317,27 @@ void VietnameseTelexAddDiacriticWithSlowResponse(bool backspace_response) {
|
||||
// it will send a setEditingState message that updates the text state that has
|
||||
// the last character deleted (denoted by `string1`). Processing the char
|
||||
// message before then will cause the final text to set to `string1`.
|
||||
EXPECT_EQ(key_calls.size(), 2);
|
||||
EXPECT_EQ(key_calls.size(), 1);
|
||||
EXPECT_CALL_IS_EVENT(key_calls[0], kFlutterKeyEventTypeDown,
|
||||
kPhysicalBackspace, kLogicalBackspace, "",
|
||||
kNotSynthesized);
|
||||
EXPECT_CALL_IS_EVENT(key_calls[1], kFlutterKeyEventTypeUp, kPhysicalBackspace,
|
||||
kLogicalBackspace, "", kNotSynthesized);
|
||||
clear_key_calls();
|
||||
EXPECT_EQ(tester.RedispatchedMessageCountAndClear(), 0);
|
||||
|
||||
EXPECT_EQ(recorded_callbacks.size(), 2);
|
||||
EXPECT_EQ(recorded_callbacks.size(), 1);
|
||||
recorded_callbacks[0](backspace_response);
|
||||
|
||||
EXPECT_EQ(key_calls.size(), 1);
|
||||
EXPECT_CALL_IS_TEXT(key_calls[0], u"à");
|
||||
EXPECT_CALL_IS_EVENT(key_calls[0], kFlutterKeyEventTypeUp, kPhysicalBackspace,
|
||||
kLogicalBackspace, "", kNotSynthesized);
|
||||
clear_key_calls();
|
||||
EXPECT_EQ(tester.RedispatchedMessageCountAndClear(),
|
||||
backspace_response ? 0 : 2);
|
||||
|
||||
recorded_callbacks[1](false);
|
||||
EXPECT_EQ(key_calls.size(), 0);
|
||||
EXPECT_EQ(key_calls.size(), 1);
|
||||
EXPECT_CALL_IS_TEXT(key_calls[0], u"à");
|
||||
clear_key_calls();
|
||||
EXPECT_EQ(tester.RedispatchedMessageCountAndClear(), 1);
|
||||
|
||||
tester.Responding(false);
|
||||
|
||||
@@ -206,7 +206,14 @@ TEST(MockWin32Window, KeyDownPrintable) {
|
||||
MockWin32Window window;
|
||||
LPARAM lparam = CreateKeyEventLparam(30, false, false);
|
||||
|
||||
EXPECT_CALL(window, OnKey(65, 30, WM_KEYDOWN, 0, false, false, _)).Times(1);
|
||||
auto respond_false = [](int key, int scancode, int action, char32_t character,
|
||||
bool extended, bool was_down,
|
||||
std::function<void(bool)> callback) {
|
||||
callback(false);
|
||||
};
|
||||
EXPECT_CALL(window, OnKey(65, 30, WM_KEYDOWN, 0, false, false, _))
|
||||
.Times(1)
|
||||
.WillOnce(respond_false);
|
||||
EXPECT_CALL(window, OnText(_)).Times(1);
|
||||
Win32Message messages[] = {{WM_KEYDOWN, 65, lparam, kWmResultDontCheck},
|
||||
{WM_CHAR, 65, lparam, kWmResultDontCheck}};
|
||||
@@ -238,6 +245,12 @@ TEST(MockWin32Window, KeyDownWithCtrl) {
|
||||
TEST(MockWin32Window, KeyDownWithCtrlToggled) {
|
||||
MockWin32Window window;
|
||||
|
||||
auto respond_false = [](int key, int scancode, int action, char32_t character,
|
||||
bool extended, bool was_down,
|
||||
std::function<void(bool)> callback) {
|
||||
callback(false);
|
||||
};
|
||||
|
||||
// Simulate CONTROL toggled
|
||||
BYTE keyboard_state[256];
|
||||
memset(keyboard_state, 0, 256);
|
||||
@@ -246,7 +259,9 @@ TEST(MockWin32Window, KeyDownWithCtrlToggled) {
|
||||
|
||||
LPARAM lparam = CreateKeyEventLparam(30, false, false);
|
||||
|
||||
EXPECT_CALL(window, OnKey(65, 30, WM_KEYDOWN, 0, false, false, _)).Times(1);
|
||||
EXPECT_CALL(window, OnKey(65, 30, WM_KEYDOWN, 0, false, false, _))
|
||||
.Times(1)
|
||||
.WillOnce(respond_false);
|
||||
EXPECT_CALL(window, OnText(_)).Times(1);
|
||||
|
||||
// send a "A" key down event.
|
||||
|
||||
Reference in New Issue
Block a user