Skip to content
This repository was archived by the owner on Feb 25, 2025. It is now read-only.
Closed
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
14 changes: 11 additions & 3 deletions shell/platform/windows/flutter_windows_engine.cc
Original file line number Diff line number Diff line change
Expand Up @@ -544,7 +544,11 @@ void FlutterWindowsEngine::SendKeyEvent(const FlutterKeyEvent& event,
FlutterKeyEventCallback callback,
void* user_data) {
if (engine_) {
embedder_api_.SendKeyEvent(engine_, &event, callback, user_data);
if (canSendKeyboardEvents_) {
embedder_api_.SendKeyEvent(engine_, &event, callback, user_data);
} else {
callback(false, user_data);
}
}
}

Expand Down Expand Up @@ -656,6 +660,8 @@ void FlutterWindowsEngine::InitializeKeyboard() {
FML_LOG(ERROR) << "Cannot initialize keyboard on Windows headless mode.";
}

canSendKeyboardEvents_ = false;

auto internal_plugin_messenger = internal_plugin_registrar_->messenger();
KeyboardKeyEmbedderHandler::GetKeyStateHandler get_key_state = GetKeyState;
KeyboardKeyEmbedderHandler::MapVirtualKeyToScanCode map_vk_to_scan =
Expand All @@ -682,8 +688,8 @@ FlutterWindowsEngine::CreateKeyboardKeyHandler(
return SendKeyEvent(event, callback, user_data);
},
get_key_state, map_vk_to_scan));
keyboard_key_handler->AddDelegate(
std::make_unique<KeyboardKeyChannelHandler>(messenger));
keyboard_key_handler->AddDelegate(std::make_unique<KeyboardKeyChannelHandler>(
messenger, [this]() { return canSendKeyboardEvents_; }));
keyboard_key_handler->InitKeyboardChannel();
return keyboard_key_handler;
}
Expand Down Expand Up @@ -851,6 +857,8 @@ void FlutterWindowsEngine::OnChannelUpdate(std::string name, bool listening) {
lifecycle_manager_->BeginProcessingExit();
} else if (name == "flutter/lifecycle" && listening) {
lifecycle_manager_->BeginProcessingLifecycle();
} else if (name == "flutter/keydata" && listening) {
canSendKeyboardEvents_ = true;
}
}

Expand Down
3 changes: 3 additions & 0 deletions shell/platform/windows/flutter_windows_engine.h
Original file line number Diff line number Diff line change
Expand Up @@ -383,6 +383,9 @@ class FlutterWindowsEngine {
// Handlers for keyboard events from Windows.
std::unique_ptr<KeyboardHandlerBase> keyboard_key_handler_;

// Wether the framework is ready to receive keyboard events.
bool canSendKeyboardEvents_ = false;

// Handlers for text events from Windows.
std::unique_ptr<TextInputPlugin> text_input_plugin_;

Expand Down
26 changes: 17 additions & 9 deletions shell/platform/windows/keyboard_key_channel_handler.cc
Original file line number Diff line number Diff line change
Expand Up @@ -100,12 +100,14 @@ int GetModsForKeyState() {
} // namespace

KeyboardKeyChannelHandler::KeyboardKeyChannelHandler(
flutter::BinaryMessenger* messenger)
flutter::BinaryMessenger* messenger,
CanSendHandler can_send)
: channel_(
std::make_unique<flutter::BasicMessageChannel<rapidjson::Document>>(
messenger,
kChannelName,
&flutter::JsonMessageCodec::GetInstance())) {}
&flutter::JsonMessageCodec::GetInstance())),
can_send_(can_send) {}

KeyboardKeyChannelHandler::~KeyboardKeyChannelHandler() = default;

Expand Down Expand Up @@ -154,13 +156,19 @@ void KeyboardKeyChannelHandler::KeyboardHook(
callback(false);
return;
}
channel_->Send(event, [callback = std::move(callback)](const uint8_t* reply,
size_t reply_size) {
auto decoded = flutter::JsonMessageCodec::GetInstance().DecodeMessage(
reply, reply_size);
bool handled = decoded ? (*decoded)[kHandledKey].GetBool() : false;
callback(handled);
});

if (can_send_()) {
channel_->Send(event, [callback = std::move(callback)](const uint8_t* reply,
size_t reply_size) {
auto decoded = flutter::JsonMessageCodec::GetInstance().DecodeMessage(
reply, reply_size);
bool handled = decoded ? (*decoded)[kHandledKey].GetBool() : false;
callback(handled);
});
} else {
// std::cerr << " >>>>>> CAN NOT SEND >>>>>> \n";
callback(false);
}
}

} // namespace flutter
8 changes: 7 additions & 1 deletion shell/platform/windows/keyboard_key_channel_handler.h
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,12 @@ namespace flutter {
class KeyboardKeyChannelHandler
: public KeyboardKeyHandler::KeyboardKeyHandlerDelegate {
public:
using CanSendHandler = std::function<bool()>;

// Create a |KeyboardKeyChannelHandler| by specifying the messenger
// through which the events are sent.
explicit KeyboardKeyChannelHandler(flutter::BinaryMessenger* messenger);
explicit KeyboardKeyChannelHandler(flutter::BinaryMessenger* messenger,
CanSendHandler can_send);

~KeyboardKeyChannelHandler();

Expand All @@ -47,6 +50,9 @@ class KeyboardKeyChannelHandler
// The Flutter system channel for key event messages.
std::unique_ptr<flutter::BasicMessageChannel<rapidjson::Document>> channel_;

// A callback which returns a bool indicating if key events can be sent.
CanSendHandler can_send_;

FML_DISALLOW_COPY_AND_ASSIGN(KeyboardKeyChannelHandler);
};

Expand Down
10 changes: 6 additions & 4 deletions shell/platform/windows/keyboard_key_channel_handler_unittests.cc
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ TEST(KeyboardKeyChannelHandlerTest, KeyboardHookHandling) {
}
});

KeyboardKeyChannelHandler handler(&messenger);
KeyboardKeyChannelHandler handler(&messenger, []() { return true; });
bool last_handled = false;

handler.KeyboardHook(
Expand Down Expand Up @@ -106,7 +106,7 @@ TEST(KeyboardKeyChannelHandlerTest, ExtendedKeysAreSentToRedispatch) {
}
});

KeyboardKeyChannelHandler handler(&messenger);
KeyboardKeyChannelHandler handler(&messenger, []() { return true; });
bool last_handled = true;

// Extended key flag is passed to redispatched events if set.
Expand Down Expand Up @@ -140,7 +140,8 @@ TEST(KeyboardKeyChannelHandlerTest, DeadKeysDoNotCrash) {
return true;
});

KeyboardKeyChannelHandler handler(&messenger);
KeyboardKeyChannelHandler handler(&messenger, []() { return true; });

// Extended key flag is passed to redispatched events if set.
handler.KeyboardHook(0xDD, 0x1a, WM_KEYDOWN, 0x8000005E, false, false,
[](bool handled) {});
Expand All @@ -164,7 +165,8 @@ TEST(KeyboardKeyChannelHandlerTest, EmptyResponsesDoNotCrash) {
return true;
});

KeyboardKeyChannelHandler handler(&messenger);
KeyboardKeyChannelHandler handler(&messenger, []() { return true; });

handler.KeyboardHook(64, kUnhandledScanCode, WM_KEYDOWN, L'b', false, false,
[](bool handled) {});

Expand Down