Skip to content
This repository was archived by the owner on Feb 25, 2025. It is now read-only.

Commit 52a9356

Browse files
committed
Merge KeyboardPlugin in KeyboardManager
1 parent 84397b1 commit 52a9356

14 files changed

+138
-379
lines changed

ci/licenses_golden/licenses_flutter

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -3028,11 +3028,6 @@ ORIGIN: ../../../flutter/shell/platform/linux/fl_key_responder.h + ../../../flut
30283028
ORIGIN: ../../../flutter/shell/platform/linux/fl_keyboard_manager.cc + ../../../flutter/LICENSE
30293029
ORIGIN: ../../../flutter/shell/platform/linux/fl_keyboard_manager.h + ../../../flutter/LICENSE
30303030
ORIGIN: ../../../flutter/shell/platform/linux/fl_keyboard_manager_test.cc + ../../../flutter/LICENSE
3031-
ORIGIN: ../../../flutter/shell/platform/linux/fl_keyboard_plugin.cc + ../../../flutter/LICENSE
3032-
ORIGIN: ../../../flutter/shell/platform/linux/fl_keyboard_plugin.h + ../../../flutter/LICENSE
3033-
ORIGIN: ../../../flutter/shell/platform/linux/fl_keyboard_plugin_test.cc + ../../../flutter/LICENSE
3034-
ORIGIN: ../../../flutter/shell/platform/linux/fl_keyboard_plugin_view_delegate.cc + ../../../flutter/LICENSE
3035-
ORIGIN: ../../../flutter/shell/platform/linux/fl_keyboard_plugin_view_delegate.h + ../../../flutter/LICENSE
30363031
ORIGIN: ../../../flutter/shell/platform/linux/fl_keyboard_view_delegate.cc + ../../../flutter/LICENSE
30373032
ORIGIN: ../../../flutter/shell/platform/linux/fl_keyboard_view_delegate.h + ../../../flutter/LICENSE
30383033
ORIGIN: ../../../flutter/shell/platform/linux/fl_message_codec.cc + ../../../flutter/LICENSE
@@ -5709,11 +5704,6 @@ FILE: ../../../flutter/shell/platform/linux/fl_key_responder.h
57095704
FILE: ../../../flutter/shell/platform/linux/fl_keyboard_manager.cc
57105705
FILE: ../../../flutter/shell/platform/linux/fl_keyboard_manager.h
57115706
FILE: ../../../flutter/shell/platform/linux/fl_keyboard_manager_test.cc
5712-
FILE: ../../../flutter/shell/platform/linux/fl_keyboard_plugin.cc
5713-
FILE: ../../../flutter/shell/platform/linux/fl_keyboard_plugin.h
5714-
FILE: ../../../flutter/shell/platform/linux/fl_keyboard_plugin_test.cc
5715-
FILE: ../../../flutter/shell/platform/linux/fl_keyboard_plugin_view_delegate.cc
5716-
FILE: ../../../flutter/shell/platform/linux/fl_keyboard_plugin_view_delegate.h
57175707
FILE: ../../../flutter/shell/platform/linux/fl_keyboard_view_delegate.cc
57185708
FILE: ../../../flutter/shell/platform/linux/fl_keyboard_view_delegate.h
57195709
FILE: ../../../flutter/shell/platform/linux/fl_message_codec.cc

shell/platform/linux/BUILD.gn

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,6 @@ source_set("flutter_linux_sources") {
8080
"fl_dart_project_private.h",
8181
"fl_engine_private.h",
8282
"fl_keyboard_manager.h",
83-
"fl_keyboard_plugin_view_delegate.h",
8483
"fl_keyboard_view_delegate.h",
8584
"fl_key_event.h",
8685
"fl_key_responder.h",
@@ -117,8 +116,6 @@ source_set("flutter_linux_sources") {
117116
"fl_key_event.cc",
118117
"fl_key_responder.cc",
119118
"fl_keyboard_manager.cc",
120-
"fl_keyboard_plugin.cc",
121-
"fl_keyboard_plugin_view_delegate.cc",
122119
"fl_keyboard_view_delegate.cc",
123120
"fl_message_codec.cc",
124121
"fl_method_call.cc",
@@ -210,13 +207,11 @@ executable("flutter_linux_unittests") {
210207
"fl_key_channel_responder_test.cc",
211208
"fl_key_embedder_responder_test.cc",
212209
"fl_keyboard_manager_test.cc",
213-
"fl_keyboard_plugin_test.cc",
214210
"fl_message_codec_test.cc",
215211
"fl_method_channel_test.cc",
216212
"fl_method_codec_test.cc",
217213
"fl_method_response_test.cc",
218214
"fl_pixel_buffer_texture_test.cc",
219-
"fl_platform_plugin_test.cc",
220215
"fl_plugin_registrar_test.cc",
221216
"fl_scrolling_manager_test.cc",
222217
"fl_settings_plugin_test.cc",

shell/platform/linux/fl_key_embedder_responder.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,8 @@ void fl_key_embedder_responder_sync_modifiers_if_needed(
6868
* fl_key_embedder_responder_get_pressed_state:
6969
* @responder: the #FlKeyEmbedderResponder self.
7070
*
71-
* Returns the keyboard pressed state.
71+
* Returns the keyboard pressed state. The hash table contains one entry per
72+
* pressed keys, mapping from the logical key to the physical key.
7273
*/
7374
GHashTable* fl_key_embedder_responder_get_pressed_state(
7475
FlKeyEmbedderResponder* responder);

shell/platform/linux/fl_keyboard_manager.cc

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,11 +12,16 @@
1212
#include "flutter/shell/platform/linux/fl_key_channel_responder.h"
1313
#include "flutter/shell/platform/linux/fl_key_embedder_responder.h"
1414
#include "flutter/shell/platform/linux/key_mapping.h"
15+
#include "flutter/shell/platform/linux/public/flutter_linux/fl_method_channel.h"
16+
#include "flutter/shell/platform/linux/public/flutter_linux/fl_standard_method_codec.h"
1517

1618
// Turn on this flag to print complete layout data when switching IMEs. The data
1719
// is used in unit tests.
1820
#define DEBUG_PRINT_LAYOUT
1921

22+
static constexpr char kChannelName[] = "flutter/keyboard";
23+
static constexpr char kGetKeyboardStateMethod[] = "getKeyboardState";
24+
2025
/* Declarations of private classes */
2126

2227
G_DECLARE_FINAL_TYPE(FlKeyboardPendingEvent,
@@ -287,6 +292,9 @@ struct _FlKeyboardManager {
287292
// It is set up when the manager is initialized and is not changed ever after.
288293
std::unique_ptr<std::map<uint64_t, const LayoutGoal*>>
289294
logical_to_mandatory_goals;
295+
296+
// The channel used by the framework to query the keyboard pressed state.
297+
FlMethodChannel* channel;
290298
};
291299

292300
G_DEFINE_TYPE(FlKeyboardManager, fl_keyboard_manager, G_TYPE_OBJECT);
@@ -532,7 +540,50 @@ static void guarantee_layout(FlKeyboardManager* self, FlKeyEvent* event) {
532540
}
533541
}
534542

543+
// Returns the keyboard pressed state.
544+
FlMethodResponse* get_keyboard_state(FlKeyboardManager* self) {
545+
g_autoptr(FlValue) result = fl_value_new_map();
546+
547+
GHashTable* pressing_records =
548+
fl_keyboard_view_delegate_get_keyboard_state(self->view_delegate);
549+
550+
g_hash_table_foreach(
551+
pressing_records,
552+
[](gpointer key, gpointer value, gpointer user_data) {
553+
int64_t physical_key = reinterpret_cast<int64_t>(key);
554+
int64_t logical_key = reinterpret_cast<int64_t>(value);
555+
FlValue* fl_value_map = reinterpret_cast<FlValue*>(user_data);
556+
557+
fl_value_set_take(fl_value_map, fl_value_new_int(physical_key),
558+
fl_value_new_int(logical_key));
559+
},
560+
result);
561+
return FL_METHOD_RESPONSE(fl_method_success_response_new(result));
562+
}
563+
564+
// Called when a method call on flutter/keyboard is received from Flutter.
565+
static void method_call_handler(FlMethodChannel* channel,
566+
FlMethodCall* method_call,
567+
gpointer user_data) {
568+
FlKeyboardManager* self = FL_KEYBOARD_MANAGER(user_data);
569+
570+
const gchar* method = fl_method_call_get_name(method_call);
571+
572+
g_autoptr(FlMethodResponse) response = nullptr;
573+
if (strcmp(method, kGetKeyboardStateMethod) == 0) {
574+
response = get_keyboard_state(self);
575+
} else {
576+
response = FL_METHOD_RESPONSE(fl_method_not_implemented_response_new());
577+
}
578+
579+
g_autoptr(GError) error = nullptr;
580+
if (!fl_method_call_respond(method_call, response, &error)) {
581+
g_warning("Failed to send method call response: %s", error->message);
582+
}
583+
}
584+
535585
FlKeyboardManager* fl_keyboard_manager_new(
586+
FlBinaryMessenger* messenger,
536587
FlKeyboardViewDelegate* view_delegate) {
537588
g_return_val_if_fail(FL_IS_KEYBOARD_VIEW_DELEGATE(view_delegate), nullptr);
538589

@@ -560,6 +611,13 @@ FlKeyboardManager* fl_keyboard_manager_new(
560611

561612
fl_keyboard_view_delegate_subscribe_to_layout_change(
562613
self->view_delegate, [self]() { self->derived_layout->clear(); });
614+
615+
// Setup the flutter/keyboard channel.
616+
g_autoptr(FlStandardMethodCodec) codec = fl_standard_method_codec_new();
617+
self->channel =
618+
fl_method_channel_new(messenger, kChannelName, FL_METHOD_CODEC(codec));
619+
fl_method_channel_set_method_call_handler(self->channel, method_call_handler,
620+
self, nullptr);
563621
return self;
564622
}
565623

shell/platform/linux/fl_keyboard_manager.h

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@ G_DECLARE_FINAL_TYPE(FlKeyboardManager,
4444
* Returns: a new #FlKeyboardManager.
4545
*/
4646
FlKeyboardManager* fl_keyboard_manager_new(
47+
FlBinaryMessenger* messenger,
4748
FlKeyboardViewDelegate* view_delegate);
4849

4950
/**
@@ -87,7 +88,8 @@ void fl_keyboard_manager_sync_modifier_if_needed(FlKeyboardManager* manager,
8788
* fl_keyboard_manager_get_pressed_state:
8889
* @manager: the #FlKeyboardManager self.
8990
*
90-
* Returns the keyboard pressed state.
91+
* Returns the keyboard pressed state. The hash table contains one entry per
92+
* pressed keys, mapping from the logical key to the physical key.*
9193
*/
9294
GHashTable* fl_keyboard_manager_get_pressed_state(FlKeyboardManager* responder);
9395

shell/platform/linux/fl_keyboard_manager_test.cc

Lines changed: 53 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,18 @@
88
#include <vector>
99

1010
#include "flutter/shell/platform/embedder/test_utils/key_codes.g.h"
11+
#include "flutter/shell/platform/linux/fl_binary_messenger_private.h"
12+
#include "flutter/shell/platform/linux/fl_method_codec_private.h"
1113
#include "flutter/shell/platform/linux/key_mapping.h"
1214
#include "flutter/shell/platform/linux/public/flutter_linux/fl_json_message_codec.h"
15+
#include "flutter/shell/platform/linux/public/flutter_linux/fl_method_codec.h"
16+
#include "flutter/shell/platform/linux/public/flutter_linux/fl_standard_method_codec.h"
17+
#include "flutter/shell/platform/linux/testing/fl_test.h"
18+
#include "flutter/shell/platform/linux/testing/mock_binary_messenger.h"
1319
#include "flutter/shell/platform/linux/testing/mock_text_input_plugin.h"
20+
#include "flutter/testing/testing.h"
21+
22+
#include "gmock/gmock.h"
1423
#include "gtest/gtest.h"
1524

1625
// Define compound `expect` in macros. If they were defined in functions, the
@@ -101,6 +110,10 @@ constexpr guint16 kKeyCodeSemicolon = 0x2fu;
101110
constexpr guint16 kKeyCodeKeyLeftBracket = 0x22u;
102111

103112
static constexpr char kKeyEventChannelName[] = "flutter/keyevent";
113+
static constexpr char kKeyboardChannelName[] = "flutter/keyboard";
114+
static constexpr char kGetKeyboardStateMethod[] = "getKeyboardState";
115+
static constexpr uint64_t kMockPhysicalKey = 42;
116+
static constexpr uint64_t kMockLogicalKey = 42;
104117

105118
// All key clues for a keyboard layout.
106119
//
@@ -130,6 +143,19 @@ G_DECLARE_FINAL_TYPE(FlMockKeyBinaryMessenger,
130143

131144
G_END_DECLS
132145

146+
MATCHER_P(MethodSuccessResponse, result, "") {
147+
g_autoptr(FlStandardMethodCodec) codec = fl_standard_method_codec_new();
148+
g_autoptr(FlMethodResponse) response =
149+
fl_method_codec_decode_response(FL_METHOD_CODEC(codec), arg, nullptr);
150+
fl_method_response_get_result(response, nullptr);
151+
if (fl_value_equal(fl_method_response_get_result(response, nullptr),
152+
result)) {
153+
return true;
154+
}
155+
*result_listener << ::testing::PrintToString(response);
156+
return false;
157+
}
158+
133159
/***** FlMockKeyBinaryMessenger *****/
134160
/* Mock a binary messenger that only processes messages from the embedding on
135161
* the key event channel, and does so according to the callback set by
@@ -407,13 +433,16 @@ static FlKeyEvent* fl_key_event_new_by_mock(bool is_press,
407433
class KeyboardTester {
408434
public:
409435
KeyboardTester() {
436+
::testing::NiceMock<flutter::testing::MockBinaryMessenger> messenger;
437+
410438
view_ = fl_mock_view_delegate_new();
411439
respondToEmbedderCallsWith(false);
412440
respondToChannelCallsWith(false);
413441
respondToTextInputWith(false);
414442
setLayout(kLayoutUs);
415443

416-
manager_ = fl_keyboard_manager_new(FL_KEYBOARD_VIEW_DELEGATE(view_));
444+
manager_ =
445+
fl_keyboard_manager_new(messenger, FL_KEYBOARD_VIEW_DELEGATE(view_));
417446
}
418447

419448
~KeyboardTester() {
@@ -945,6 +974,29 @@ TEST(FlKeyboardManagerTest, GetPressedState) {
945974
EXPECT_EQ(gpointer_to_uint64(physical_key), kLogicalKeyA);
946975
}
947976

977+
TEST(FlKeyboardPluginTest, KeyboardChannelGetPressedState) {
978+
::testing::NiceMock<flutter::testing::MockBinaryMessenger> messenger;
979+
980+
g_autoptr(FlKeyboardManager) manager = fl_keyboard_manager_new(
981+
messenger, FL_KEYBOARD_VIEW_DELEGATE(fl_mock_view_delegate_new()));
982+
EXPECT_NE(manager, nullptr);
983+
984+
g_autoptr(FlStandardMethodCodec) codec = fl_standard_method_codec_new();
985+
g_autoptr(GBytes) message = fl_method_codec_encode_method_call(
986+
FL_METHOD_CODEC(codec), kGetKeyboardStateMethod, nullptr, nullptr);
987+
988+
g_autoptr(FlValue) response = fl_value_new_map();
989+
fl_value_set_take(response, fl_value_new_int(kMockPhysicalKey),
990+
fl_value_new_int(kMockLogicalKey));
991+
EXPECT_CALL(messenger,
992+
fl_binary_messenger_send_response(
993+
::testing::Eq<FlBinaryMessenger*>(messenger), ::testing::_,
994+
MethodSuccessResponse(response), ::testing::_))
995+
.WillOnce(::testing::Return(true));
996+
997+
messenger.ReceiveMessage(kKeyboardChannelName, message);
998+
}
999+
9481000
// The following layout data is generated using DEBUG_PRINT_LAYOUT.
9491001

9501002
const MockGroupLayoutData kLayoutUs0{{

shell/platform/linux/fl_keyboard_plugin.cc

Lines changed: 0 additions & 111 deletions
This file was deleted.

0 commit comments

Comments
 (0)