Skip to content

Commit 0176295

Browse files
authored
[Win32, Keyboard] TextInputPlugin is no longer a KeyboardHandlerBase (flutter#30456)
* Impl * format * Comment
1 parent acb60b4 commit 0176295

9 files changed

+92
-176
lines changed

shell/platform/windows/flutter_window_win32_unittests.cc

Lines changed: 17 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -35,17 +35,13 @@ static constexpr int32_t kDefaultPointerDeviceId = 0;
3535
class SpyKeyboardKeyHandler : public KeyboardHandlerBase {
3636
public:
3737
SpyKeyboardKeyHandler(flutter::BinaryMessenger* messenger,
38-
KeyboardKeyHandler::EventDispatcher redispatch_event) {
39-
real_implementation_ =
40-
std::make_unique<KeyboardKeyHandler>(redispatch_event);
38+
KeyboardKeyHandler::EventDispatcher dispatch_event) {
39+
real_implementation_ = std::make_unique<KeyboardKeyHandler>(dispatch_event);
4140
real_implementation_->AddDelegate(
4241
std::make_unique<KeyboardKeyChannelHandler>(messenger));
4342
ON_CALL(*this, KeyboardHook(_, _, _, _, _, _))
4443
.WillByDefault(Invoke(real_implementation_.get(),
4544
&KeyboardKeyHandler::KeyboardHook));
46-
ON_CALL(*this, TextHook(_))
47-
.WillByDefault(
48-
Invoke(real_implementation_.get(), &KeyboardKeyHandler::TextHook));
4945
}
5046

5147
MOCK_METHOD6(KeyboardHook,
@@ -55,7 +51,6 @@ class SpyKeyboardKeyHandler : public KeyboardHandlerBase {
5551
char32_t character,
5652
bool extended,
5753
bool was_down));
58-
MOCK_METHOD1(TextHook, void(const std::u16string& text));
5954
MOCK_METHOD0(ComposeBeginHook, void());
6055
MOCK_METHOD0(ComposeCommitHook, void());
6156
MOCK_METHOD0(ComposeEndHook, void());
@@ -68,10 +63,11 @@ class SpyKeyboardKeyHandler : public KeyboardHandlerBase {
6863

6964
// A text input plugin that can be spied on while it forwards calls to the real
7065
// text input plugin.
71-
class SpyTextInputPlugin : public KeyboardHandlerBase,
66+
class SpyTextInputPlugin : public TextInputPlugin,
7267
public TextInputPluginDelegate {
7368
public:
74-
SpyTextInputPlugin(flutter::BinaryMessenger* messenger) {
69+
SpyTextInputPlugin(flutter::BinaryMessenger* messenger)
70+
: TextInputPlugin(messenger, this) {
7571
real_implementation_ = std::make_unique<TextInputPlugin>(messenger, this);
7672
ON_CALL(*this, KeyboardHook(_, _, _, _, _, _))
7773
.WillByDefault(
@@ -82,7 +78,7 @@ class SpyTextInputPlugin : public KeyboardHandlerBase,
8278
}
8379

8480
MOCK_METHOD6(KeyboardHook,
85-
bool(int key,
81+
void(int key,
8682
int scancode,
8783
int action,
8884
char32_t character,
@@ -246,7 +242,7 @@ class TestFlutterWindowsView : public FlutterWindowsView {
246242
}
247243

248244
protected:
249-
void RegisterKeyboardHandlers(
245+
std::unique_ptr<KeyboardHandlerBase> CreateKeyboardKeyHandler(
250246
flutter::BinaryMessenger* messenger,
251247
flutter::KeyboardKeyHandler::EventDispatcher dispatch_event,
252248
flutter::KeyboardKeyEmbedderHandler::GetKeyStateHandler get_key_state)
@@ -255,12 +251,16 @@ class TestFlutterWindowsView : public FlutterWindowsView {
255251
messenger, [this](UINT cInputs, LPINPUT pInputs, int cbSize) -> UINT {
256252
return this->SendInput(cInputs, pInputs, cbSize);
257253
});
258-
auto spy_text_input_plugin =
259-
std::make_unique<SpyTextInputPlugin>(messenger);
260254
key_event_handler = spy_key_event_handler.get();
261-
text_input_plugin = spy_text_input_plugin.get();
262-
AddKeyboardHandler(std::move(spy_key_event_handler));
263-
AddKeyboardHandler(std::move(spy_text_input_plugin));
255+
return spy_key_event_handler;
256+
}
257+
258+
std::unique_ptr<TextInputPlugin> CreateTextInputPlugin(
259+
flutter::BinaryMessenger* messenger) override {
260+
auto spy_key_event_handler =
261+
std::make_unique<SpyTextInputPlugin>(messenger);
262+
text_input_plugin = spy_key_event_handler.get();
263+
return spy_key_event_handler;
264264
}
265265

266266
private:
@@ -357,7 +357,6 @@ TEST(FlutterWindowWin32Test, NonPrintableKeyDownPropagation) {
357357
KeyboardHook(_, _, _, _, _, _))
358358
.Times(1)
359359
.RetiresOnSaturation();
360-
EXPECT_CALL(*flutter_windows_view.key_event_handler, TextHook(_)).Times(0);
361360
EXPECT_CALL(*flutter_windows_view.text_input_plugin, TextHook(_)).Times(0);
362361
win32window.InjectMessages(1,
363362
Win32Message{WM_KEYDOWN, virtual_key, lparam});
@@ -410,7 +409,6 @@ TEST(FlutterWindowWin32Test, SystemKeyDownPropagation) {
410409
KeyboardHook(_, _, _, _, _, _))
411410
.Times(1)
412411
.RetiresOnSaturation();
413-
EXPECT_CALL(*flutter_windows_view.key_event_handler, TextHook(_)).Times(0);
414412
EXPECT_CALL(*flutter_windows_view.text_input_plugin, TextHook(_)).Times(0);
415413
win32window.InjectMessages(
416414
1, Win32Message{WM_SYSKEYDOWN, virtual_key, lparam, kWmResultDefault});
@@ -436,7 +434,7 @@ TEST(FlutterWindowWin32Test, SystemKeyDownPropagation) {
436434
// differ from non-printable characters in that they follow a different code
437435
// path in the WndProc (HandleMessage), producing a follow-on WM_CHAR event.
438436
TEST(FlutterWindowWin32Test, CharKeyDownPropagation) {
439-
// ::testing::InSequence in_sequence;
437+
::testing::InSequence in_sequence;
440438

441439
constexpr WPARAM virtual_key = 65; // The "A" key, which produces a character
442440
constexpr WPARAM scan_code = 30;
@@ -463,9 +461,6 @@ TEST(FlutterWindowWin32Test, CharKeyDownPropagation) {
463461
KeyboardHook(_, _, _, _, _, _))
464462
.Times(1)
465463
.RetiresOnSaturation();
466-
EXPECT_CALL(*flutter_windows_view.key_event_handler, TextHook(_))
467-
.Times(1)
468-
.RetiresOnSaturation();
469464
EXPECT_CALL(*flutter_windows_view.text_input_plugin, TextHook(_))
470465
.Times(1)
471466
.RetiresOnSaturation();
@@ -485,7 +480,6 @@ TEST(FlutterWindowWin32Test, CharKeyDownPropagation) {
485480
EXPECT_CALL(*flutter_windows_view.text_input_plugin,
486481
KeyboardHook(_, _, _, _, _, _))
487482
.Times(0);
488-
EXPECT_CALL(*flutter_windows_view.key_event_handler, TextHook(_)).Times(0);
489483
EXPECT_CALL(*flutter_windows_view.text_input_plugin, TextHook(_)).Times(0);
490484
win32window.InjectMessages(2, Win32Message{WM_KEYDOWN, virtual_key, lparam},
491485
Win32Message{WM_CHAR, virtual_key, lparam});
@@ -521,7 +515,6 @@ TEST(FlutterWindowWin32Test, ModifierKeyDownPropagation) {
521515
KeyboardHook(_, _, _, _, _, _))
522516
.Times(1)
523517
.RetiresOnSaturation();
524-
EXPECT_CALL(*flutter_windows_view.key_event_handler, TextHook(_)).Times(0);
525518
EXPECT_CALL(*flutter_windows_view.text_input_plugin, TextHook(_)).Times(0);
526519
EXPECT_EQ(win32window.InjectWindowMessage(WM_KEYDOWN, virtual_key, lparam),
527520
0);

shell/platform/windows/flutter_windows_view.cc

Lines changed: 42 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -52,25 +52,26 @@ void FlutterWindowsView::SetEngine(
5252
engine_->SetView(this);
5353

5454
internal_plugin_registrar_ =
55-
std::make_unique<flutter::PluginRegistrar>(engine_->GetRegistrar());
55+
std::make_unique<PluginRegistrar>(engine_->GetRegistrar());
5656

5757
// Set up the system channel handlers.
5858
auto internal_plugin_messenger = internal_plugin_registrar_->messenger();
5959
InitializeKeyboard();
6060
platform_handler_ = PlatformHandler::Create(internal_plugin_messenger, this);
61-
cursor_handler_ = std::make_unique<flutter::CursorHandler>(
62-
internal_plugin_messenger, binding_handler_.get());
61+
cursor_handler_ = std::make_unique<CursorHandler>(internal_plugin_messenger,
62+
binding_handler_.get());
6363

6464
PhysicalWindowBounds bounds = binding_handler_->GetPhysicalWindowBounds();
6565

6666
SendWindowMetrics(bounds.width, bounds.height,
6767
binding_handler_->GetDpiScale());
6868
}
6969

70-
void FlutterWindowsView::RegisterKeyboardHandlers(
71-
flutter::BinaryMessenger* messenger,
72-
flutter::KeyboardKeyHandler::EventDispatcher dispatch_event,
73-
flutter::KeyboardKeyEmbedderHandler::GetKeyStateHandler get_key_state) {
70+
std::unique_ptr<KeyboardHandlerBase>
71+
FlutterWindowsView::CreateKeyboardKeyHandler(
72+
BinaryMessenger* messenger,
73+
KeyboardKeyHandler::EventDispatcher dispatch_event,
74+
KeyboardKeyEmbedderHandler::GetKeyStateHandler get_key_state) {
7475
// There must be only one handler that receives |SendInput|, i.e. only one
7576
// handler that might redispatch events. (See the documentation of
7677
// |KeyboardKeyHandler| to learn about redispatching.)
@@ -79,24 +80,23 @@ void FlutterWindowsView::RegisterKeyboardHandlers(
7980
// of the event. In order to allow the same real event in the future, the
8081
// handler is "toggled" when events pass through, therefore the redispatching
8182
// algorithm does not allow more than 1 handler that takes |SendInput|.
82-
auto key_handler =
83-
std::make_unique<flutter::KeyboardKeyHandler>(dispatch_event);
84-
key_handler->AddDelegate(std::make_unique<KeyboardKeyEmbedderHandler>(
85-
[this](const FlutterKeyEvent& event, FlutterKeyEventCallback callback,
86-
void* user_data) {
87-
return engine_->SendKeyEvent(event, callback, user_data);
88-
},
89-
get_key_state));
90-
key_handler->AddDelegate(
83+
auto keyboard_key_handler =
84+
std::make_unique<KeyboardKeyHandler>(dispatch_event);
85+
keyboard_key_handler->AddDelegate(
86+
std::make_unique<KeyboardKeyEmbedderHandler>(
87+
[this](const FlutterKeyEvent& event, FlutterKeyEventCallback callback,
88+
void* user_data) {
89+
return engine_->SendKeyEvent(event, callback, user_data);
90+
},
91+
get_key_state));
92+
keyboard_key_handler->AddDelegate(
9193
std::make_unique<KeyboardKeyChannelHandler>(messenger));
92-
AddKeyboardHandler(std::move(key_handler));
93-
AddKeyboardHandler(
94-
std::make_unique<flutter::TextInputPlugin>(messenger, this));
94+
return keyboard_key_handler;
9595
}
9696

97-
void FlutterWindowsView::AddKeyboardHandler(
98-
std::unique_ptr<flutter::KeyboardHandlerBase> handler) {
99-
keyboard_handlers_.push_back(std::move(handler));
97+
std::unique_ptr<TextInputPlugin> FlutterWindowsView::CreateTextInputPlugin(
98+
BinaryMessenger* messenger) {
99+
return std::make_unique<TextInputPlugin>(messenger, this);
100100
}
101101

102102
uint32_t FlutterWindowsView::GetFrameBufferId(size_t width, size_t height) {
@@ -129,7 +129,6 @@ void FlutterWindowsView::ForceRedraw() {
129129
}
130130

131131
void FlutterWindowsView::OnPreEngineRestart() {
132-
keyboard_handlers_.clear();
133132
InitializeKeyboard();
134133
}
135134

@@ -268,16 +267,16 @@ void FlutterWindowsView::OnResetImeComposing() {
268267
void FlutterWindowsView::InitializeKeyboard() {
269268
auto internal_plugin_messenger = internal_plugin_registrar_->messenger();
270269
#ifdef WINUWP
271-
flutter::KeyboardKeyHandler::EventDispatcher dispatch_event = nullptr;
272-
flutter::KeyboardKeyEmbedderHandler::GetKeyStateHandler get_key_state =
273-
nullptr;
270+
KeyboardKeyHandler::EventDispatcher dispatch_event = nullptr;
271+
KeyboardKeyEmbedderHandler::GetKeyStateHandler get_key_state = nullptr;
274272
#else
275-
flutter::KeyboardKeyHandler::EventDispatcher dispatch_event = SendInput;
276-
flutter::KeyboardKeyEmbedderHandler::GetKeyStateHandler get_key_state =
277-
GetKeyState;
273+
KeyboardKeyHandler::EventDispatcher dispatch_event = SendInput;
274+
KeyboardKeyEmbedderHandler::GetKeyStateHandler get_key_state = GetKeyState;
278275
#endif
279-
RegisterKeyboardHandlers(internal_plugin_messenger, dispatch_event,
280-
get_key_state);
276+
keyboard_key_handler_ = std::move(CreateKeyboardKeyHandler(
277+
internal_plugin_messenger, dispatch_event, get_key_state));
278+
text_input_plugin_ =
279+
std::move(CreateTextInputPlugin(internal_plugin_messenger));
281280
}
282281

283282
// Sends new size information to FlutterEngine.
@@ -381,9 +380,7 @@ void FlutterWindowsView::SendPointerLeave(PointerState* state) {
381380
}
382381

383382
void FlutterWindowsView::SendText(const std::u16string& text) {
384-
for (const auto& handler : keyboard_handlers_) {
385-
handler->TextHook(text);
386-
}
383+
text_input_plugin_->TextHook(text);
387384
}
388385

389386
bool FlutterWindowsView::SendKey(int key,
@@ -392,39 +389,32 @@ bool FlutterWindowsView::SendKey(int key,
392389
char32_t character,
393390
bool extended,
394391
bool was_down) {
395-
for (const auto& handler : keyboard_handlers_) {
396-
if (handler->KeyboardHook(key, scancode, action, character, extended,
397-
was_down)) {
398-
// key event was handled, so don't send to other handlers.
399-
return true;
400-
}
392+
if (keyboard_key_handler_->KeyboardHook(key, scancode, action, character,
393+
extended, was_down)) {
394+
return true;
401395
}
396+
397+
text_input_plugin_->KeyboardHook(key, scancode, action, character, extended,
398+
was_down);
399+
402400
return false;
403401
}
404402

405403
void FlutterWindowsView::SendComposeBegin() {
406-
for (const auto& handler : keyboard_handlers_) {
407-
handler->ComposeBeginHook();
408-
}
404+
text_input_plugin_->ComposeBeginHook();
409405
}
410406

411407
void FlutterWindowsView::SendComposeCommit() {
412-
for (const auto& handler : keyboard_handlers_) {
413-
handler->ComposeCommitHook();
414-
}
408+
text_input_plugin_->ComposeCommitHook();
415409
}
416410

417411
void FlutterWindowsView::SendComposeEnd() {
418-
for (const auto& handler : keyboard_handlers_) {
419-
handler->ComposeEndHook();
420-
}
412+
text_input_plugin_->ComposeEndHook();
421413
}
422414

423415
void FlutterWindowsView::SendComposeChange(const std::u16string& text,
424416
int cursor_pos) {
425-
for (const auto& handler : keyboard_handlers_) {
426-
handler->ComposeChangeHook(text, cursor_pos);
427-
}
417+
text_input_plugin_->ComposeChangeHook(text, cursor_pos);
428418
}
429419

430420
void FlutterWindowsView::SendScroll(double x,

shell/platform/windows/flutter_windows_view.h

Lines changed: 17 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -22,9 +22,9 @@
2222
#include "flutter/shell/platform/windows/flutter_windows_engine.h"
2323
#include "flutter/shell/platform/windows/keyboard_handler_base.h"
2424
#include "flutter/shell/platform/windows/keyboard_key_embedder_handler.h"
25-
#include "flutter/shell/platform/windows/keyboard_key_handler.h"
2625
#include "flutter/shell/platform/windows/platform_handler.h"
2726
#include "flutter/shell/platform/windows/public/flutter_windows.h"
27+
#include "flutter/shell/platform/windows/text_input_plugin.h"
2828
#include "flutter/shell/platform/windows/text_input_plugin_delegate.h"
2929
#include "flutter/shell/platform/windows/window_binding_handler.h"
3030
#include "flutter/shell/platform/windows/window_binding_handler_delegate.h"
@@ -171,20 +171,20 @@ class FlutterWindowsView : public WindowBindingHandlerDelegate,
171171
void OnResetImeComposing() override;
172172

173173
protected:
174-
// Called to create the keyboard hook handlers.
174+
// Called to create keyboard key handler.
175175
//
176176
// The provided |dispatch_event| is where to inject events into the system,
177177
// while |get_key_state| is where to acquire keyboard states. They will be
178178
// the system APIs in production classes, but might be replaced with mock
179179
// functions in unit tests.
180-
virtual void RegisterKeyboardHandlers(
181-
flutter::BinaryMessenger* messenger,
182-
flutter::KeyboardKeyHandler::EventDispatcher dispatch_event,
183-
flutter::KeyboardKeyEmbedderHandler::GetKeyStateHandler get_key_state);
180+
virtual std::unique_ptr<KeyboardHandlerBase> CreateKeyboardKeyHandler(
181+
BinaryMessenger* messenger,
182+
KeyboardKeyHandler::EventDispatcher dispatch_event,
183+
KeyboardKeyEmbedderHandler::GetKeyStateHandler get_key_state);
184184

185-
// Used by RegisterKeyboardHandlers to add a new keyboard hook handler.
186-
void AddKeyboardHandler(
187-
std::unique_ptr<flutter::KeyboardHandlerBase> handler);
185+
// Called to create text input plugin.
186+
virtual std::unique_ptr<TextInputPlugin> CreateTextInputPlugin(
187+
BinaryMessenger* messenger);
188188

189189
private:
190190
// Struct holding the state of an individual pointer. The engine doesn't keep
@@ -322,19 +322,22 @@ class FlutterWindowsView : public WindowBindingHandlerDelegate,
322322
std::unordered_map<int32_t, std::unique_ptr<PointerState>> pointer_states_;
323323

324324
// The plugin registrar managing internal plugins.
325-
std::unique_ptr<flutter::PluginRegistrar> internal_plugin_registrar_;
325+
std::unique_ptr<PluginRegistrar> internal_plugin_registrar_;
326326

327327
// Handlers for keyboard events from Windows.
328-
std::vector<std::unique_ptr<flutter::KeyboardHandlerBase>> keyboard_handlers_;
328+
std::unique_ptr<KeyboardHandlerBase> keyboard_key_handler_;
329+
330+
// Handlers for text events from Windows.
331+
std::unique_ptr<TextInputPlugin> text_input_plugin_;
329332

330333
// Handler for the flutter/platform channel.
331-
std::unique_ptr<flutter::PlatformHandler> platform_handler_;
334+
std::unique_ptr<PlatformHandler> platform_handler_;
332335

333336
// Handler for cursor events.
334-
std::unique_ptr<flutter::CursorHandler> cursor_handler_;
337+
std::unique_ptr<CursorHandler> cursor_handler_;
335338

336339
// Currently configured WindowBindingHandler for view.
337-
std::unique_ptr<flutter::WindowBindingHandler> binding_handler_;
340+
std::unique_ptr<WindowBindingHandler> binding_handler_;
338341

339342
// Resize events are synchronized using this mutex and the corresponding
340343
// condition variable.

0 commit comments

Comments
 (0)