Skip to content
This repository was archived by the owner on Feb 25, 2025. It is now read-only.
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
85 changes: 5 additions & 80 deletions shell/platform/windows/flutter_window_win32_unittests.cc
Original file line number Diff line number Diff line change
Expand Up @@ -93,13 +93,9 @@ class SpyTextInputPlugin : public TextInputPlugin,
std::unique_ptr<TextInputPlugin> real_implementation_;
};

class MockFlutterWindowWin32 : public FlutterWindowWin32,
public MockMessageQueue {
class MockFlutterWindowWin32 : public FlutterWindowWin32 {
public:
MockFlutterWindowWin32(WPARAM virtual_key = 0, bool is_printable = true)
: virtual_key_(virtual_key),
is_printable_(is_printable),
FlutterWindowWin32(800, 600) {
MockFlutterWindowWin32() : FlutterWindowWin32(800, 600) {
ON_CALL(*this, GetDpiScale())
.WillByDefault(Return(this->FlutterWindowWin32::GetDpiScale()));
}
Expand All @@ -112,25 +108,6 @@ class MockFlutterWindowWin32 : public FlutterWindowWin32,
// Wrapper for GetCurrentDPI() which is a protected method.
UINT GetDpi() { return GetCurrentDPI(); }

// Simulates a WindowProc message from the OS.
LRESULT InjectWindowMessage(UINT const message,
WPARAM const wparam,
LPARAM const lparam) {
return Win32SendMessage(message, wparam, lparam);
}

void InjectMessages(int count, Win32Message message1, ...) {
Win32Message messages[count];
messages[0] = message1;
va_list args;
va_start(args, message1);
for (int i = 1; i < count; i += 1) {
messages[i] = va_arg(args, Win32Message);
}
va_end(args);
InjectMessageList(count, messages);
}

MOCK_METHOD1(OnDpiScale, void(unsigned int));
MOCK_METHOD2(OnResize, void(unsigned int, unsigned int));
MOCK_METHOD4(OnPointerMove,
Expand All @@ -147,70 +124,18 @@ class MockFlutterWindowWin32 : public FlutterWindowWin32,
MOCK_METHOD0(IsVisible, bool());
MOCK_METHOD1(UpdateCursorRect, void(const Rect&));
MOCK_METHOD0(OnResetImeComposing, void());
MOCK_METHOD3(Win32DispatchMessage, UINT(UINT, WPARAM, LPARAM));
MOCK_METHOD4(Win32PeekMessage, BOOL(LPMSG, UINT, UINT, UINT));
MOCK_METHOD1(Win32MapVkToChar, uint32_t(uint32_t));

protected:
// |KeyboardManagerWin32::WindowDelegate|
BOOL Win32PeekMessage(LPMSG lpMsg,
UINT wMsgFilterMin,
UINT wMsgFilterMax,
UINT wRemoveMsg) override {
return MockMessageQueue::Win32PeekMessage(lpMsg, wMsgFilterMin,
wMsgFilterMax, wRemoveMsg);
}

// |KeyboardManagerWin32::WindowDelegate|
LRESULT Win32DefWindowProc(HWND hWnd,
UINT Msg,
WPARAM wParam,
LPARAM lParam) override {
return kWmResultDefault;
}

// |KeyboardManagerWin32::WindowDelegate|
UINT Win32DispatchEvent(UINT cInputs, LPINPUT pInputs, int cbSize) override {
for (UINT input_idx = 0; input_idx < cInputs; input_idx += 1) {
SendInput(pInputs[input_idx].ki);
}
return 1;
}

// |MockMessageQueue|
LRESULT Win32SendMessage(UINT const message,
WPARAM const wparam,
LPARAM const lparam) override {
return HandleMessage(message, wparam, lparam);
}

private:
UINT SendInput(KEYBDINPUT kbdinput) {
// Simulate the event loop by just sending the event sent to
// "SendInput" directly to the window.
const bool is_key_up = kbdinput.dwFlags & KEYEVENTF_KEYUP;
const UINT message = is_key_up ? WM_KEYUP : WM_KEYDOWN;

const LPARAM lparam = CreateKeyEventLparam(
kbdinput.wScan, kbdinput.dwFlags & KEYEVENTF_EXTENDEDKEY, is_key_up);
// Windows would normally fill in the virtual key code for us, so we
// simulate it for the test with the key we know is in the test. The
// KBDINPUT we're passed doesn't have it filled in (on purpose, so that
// Windows will fill it in).
//
// TODO(dkwingsmt): Don't check the message results for redispatched
// messages for now, because making them work takes non-trivial rework
// to our current structure. https://github.com/flutter/flutter/issues/87843
// If this is resolved, change them to kWmResultDefault.
pending_responds_.push_back(
Win32Message{message, virtual_key_, lparam, kWmResultDontCheck});
if (is_printable_ && (kbdinput.dwFlags & KEYEVENTF_KEYUP) == 0) {
pending_responds_.push_back(
Win32Message{WM_CHAR, virtual_key_, lparam, kWmResultDontCheck});
}
return 1;
}

std::vector<Win32Message> pending_responds_;
WPARAM virtual_key_;
bool is_printable_;
};

class MockWindowBindingHandlerDelegate : public WindowBindingHandlerDelegate {
Expand Down
141 changes: 68 additions & 73 deletions shell/platform/windows/keyboard_manager_win32.cc
Original file line number Diff line number Diff line change
Expand Up @@ -133,68 +133,40 @@ KeyboardManagerWin32::KeyboardManagerWin32(WindowDelegate* delegate)
last_key_is_ctrl_left_down(false),
should_synthesize_ctrl_left_up(false) {}

void KeyboardManagerWin32::DispatchEvent(const PendingEvent& event) {
assert(event.action != WM_SYSKEYDOWN && event.action != WM_SYSKEYUP &&
"Unexpectedly dispatching a SYS event. SYS events can't be dispatched "
"and should have been prevented in earlier code.");

char32_t character = event.character;

INPUT input_event{
.type = INPUT_KEYBOARD,
.ki =
KEYBDINPUT{
.wVk = static_cast<WORD>(event.key),
.wScan = static_cast<WORD>(event.scancode),
.dwFlags = static_cast<WORD>(
KEYEVENTF_SCANCODE |
(event.extended ? KEYEVENTF_EXTENDEDKEY : 0x0) |
(event.action == WM_KEYUP ? KEYEVENTF_KEYUP : 0x0)),
},
};

UINT accepted = window_delegate_->Win32DispatchEvent(1, &input_event,
sizeof(input_event));
if (accepted != 1) {
std::cerr << "Unable to synthesize event for keyboard event with scancode "
<< event.scancode;
if (character != 0) {
std::cerr << " (character " << character << ")";
}
std::cerr << std::endl;
}
}

void KeyboardManagerWin32::RedispatchEvent(
std::unique_ptr<PendingEvent> event) {
DispatchEvent(*event);
for (const Win32Message& message : event->session) {
pending_redispatches_.push_back(message);
UINT result = window_delegate_->Win32DispatchMessage(
message.action, message.wparam, message.lparam);
if (result != 0) {
std::cerr << "Unable to synthesize event for keyboard event."
<< std::endl;
}
}
if (pending_redispatches_.size() > kMaxPendingEvents) {
std::cerr
<< "There are " << pending_redispatches_.size()
<< " keyboard events that have not yet received a response from the "
<< "framework. Are responses being sent?" << std::endl;
}
pending_redispatches_.push_back(std::move(event));
}

bool KeyboardManagerWin32::RemoveRedispatchedEvent(
const PendingEvent& incoming) {
bool KeyboardManagerWin32::RemoveRedispatchedMessage(UINT const action,
WPARAM const wparam,
LPARAM const lparam) {
for (auto iter = pending_redispatches_.begin();
iter != pending_redispatches_.end(); ++iter) {
if ((*iter)->Hash() == incoming.Hash()) {
if (action == iter->action && wparam == iter->wparam) {
pending_redispatches_.erase(iter);
return true;
}
}
return false;
}

bool KeyboardManagerWin32::OnKey(std::unique_ptr<PendingEvent> event,
void KeyboardManagerWin32::OnKey(std::unique_ptr<PendingEvent> event,
OnKeyCallback callback) {
if (RemoveRedispatchedEvent(*event)) {
return false;
}

if (IsKeyDownAltRight(event->action, event->key, event->extended)) {
if (last_key_is_ctrl_left_down) {
should_synthesize_ctrl_left_up = true;
Expand All @@ -210,13 +182,11 @@ bool KeyboardManagerWin32::OnKey(std::unique_ptr<PendingEvent> event,
if (IsKeyUpAltRight(event->action, event->key, event->extended)) {
if (should_synthesize_ctrl_left_up) {
should_synthesize_ctrl_left_up = false;
PendingEvent ctrl_left_up{
.key = VK_LCONTROL,
.scancode = ctrl_left_scancode,
.action = WM_KEYUP,
.was_down = true,
};
DispatchEvent(ctrl_left_up);
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_LCONTROL, lParam);
}
}

Expand All @@ -228,14 +198,21 @@ bool KeyboardManagerWin32::OnKey(std::unique_ptr<PendingEvent> event,
callback(std::unique_ptr<PendingEvent>(event),
handled);
});
return true;
}

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,
int char_action,
std::u16string text) {
std::list<PendingText>::iterator pending_text) {
// First, patch |handled|, because some key events must always be treated as
// handled.
//
Expand All @@ -253,6 +230,9 @@ void KeyboardManagerWin32::HandleOnKeyResult(

// For handled events, that's all.
if (real_handled) {
if (pending_text != pending_texts_.end()) {
pending_texts_.erase(pending_text);
}
return;
}

Expand All @@ -263,8 +243,10 @@ void KeyboardManagerWin32::HandleOnKeyResult(
// will be incorporated into a later WM_CHAR with the full character.
// Non-printable event characters have been filtered out before being passed
// to OnKey.
if (char_action == WM_CHAR && event->character != 0) {
window_delegate_->OnText(text);
if (char_action == WM_CHAR && event->character != 0 &&
pending_text != pending_texts_.end()) {
pending_text->ready = true;
DispatchReadyTexts();
}

RedispatchEvent(std::move(event));
Expand All @@ -273,6 +255,9 @@ void KeyboardManagerWin32::HandleOnKeyResult(
bool KeyboardManagerWin32::HandleMessage(UINT const action,
WPARAM const wparam,
LPARAM const lparam) {
if (RemoveRedispatchedMessage(action, wparam, lparam)) {
return false;
}
switch (action) {
case WM_DEADCHAR:
case WM_SYSDEADCHAR:
Expand Down Expand Up @@ -314,7 +299,6 @@ bool KeyboardManagerWin32::HandleMessage(UINT const action,
// OnText if the key down event is not handled.
if (current_session_.front().IsGeneralKeyDown()) {
const Win32Message first_message = current_session_.front();
current_session_.clear();
const uint8_t scancode = (lparam >> 16) & 0xff;
const uint16_t key_code = first_message.wparam;
const bool extended = ((lparam >> 24) & 0x01) == 0x01;
Expand Down Expand Up @@ -342,16 +326,22 @@ bool KeyboardManagerWin32::HandleMessage(UINT const action,
.was_down = was_down,
.session = std::move(current_session_),
});
const bool is_unmet_event = OnKey(
std::move(event),
[this, char_action = action, text](
std::unique_ptr<PendingEvent> event, bool handled) {
HandleOnKeyResult(std::move(event), handled, char_action, text);
});
const bool is_syskey = action == WM_SYSCHAR;
// For system characters, always pass them to the default WndProc so
// that system keys like the ALT-TAB are processed correctly.
return is_unmet_event && !is_syskey;
pending_texts_.push_back(PendingText{
.ready = false,
.content = text,
});
auto pending_text = std::prev(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, char_action = action, pending_text, is_syskey](
std::unique_ptr<PendingEvent> event, bool handled) {
bool real_handled = handled || is_syskey;
HandleOnKeyResult(std::move(event), handled, char_action,
pending_text);
});
return !is_syskey;
}

// If the charcter session is not preceded by a key down message, dispatch
Expand All @@ -365,7 +355,11 @@ bool KeyboardManagerWin32::HandleMessage(UINT const action,
// events for all control key shortcuts.
current_session_.clear();
if (action == WM_CHAR && IsPrintable(wparam)) {
window_delegate_->OnText(text);
pending_texts_.push_back(PendingText{
.ready = true,
.content = text,
});
DispatchReadyTexts();
}
return true;
}
Expand Down Expand Up @@ -415,15 +409,16 @@ bool KeyboardManagerWin32::HandleMessage(UINT const action,
.was_down = was_down,
.session = std::move(current_session_),
});
const bool is_unmet_event = OnKey(
std::move(event),
[this](std::unique_ptr<PendingEvent> event, bool handled) {
HandleOnKeyResult(std::move(event), handled, 0, std::u16string());
});
// SYS messages must not be handled by `HandleMessage` or be
// redispatched.
const bool is_syskey = action == WM_SYSKEYDOWN || action == WM_SYSKEYUP;
// For system keys, always pass them to the default WndProc so that keys
// like the ALT-TAB or Kanji switches are processed correctly.
return is_unmet_event && !is_syskey;
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, 0, pending_texts_.end());
});
return !is_syskey;
}
default:
assert(false);
Expand Down
Loading