Skip to content

Commit 8f3fbb5

Browse files
authored
Let the app handle the back key event first (#126)
* Let the app handle the back key event first * Tidy up
1 parent 7e058f9 commit 8f3fbb5

File tree

3 files changed

+51
-23
lines changed

3 files changed

+51
-23
lines changed

shell/platform/tizen/channels/key_event_channel.cc

+13-5
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ constexpr char kTypeKey[] = "type";
2121
constexpr char kModifiersKey[] = "modifiers";
2222
constexpr char kToolkitKey[] = "toolkit";
2323
constexpr char kUnicodeScalarValuesKey[] = "unicodeScalarValues";
24+
constexpr char kHandledKey[] = "handled";
2425

2526
constexpr char kKeyUp[] = "keyup";
2627
constexpr char kKeyDown[] = "keydown";
@@ -226,10 +227,9 @@ KeyEventChannel::KeyEventChannel(BinaryMessenger* messenger)
226227

227228
KeyEventChannel::~KeyEventChannel() {}
228229

229-
void KeyEventChannel::SendKeyEvent(Ecore_Event_Key* key, bool is_down) {
230-
FT_LOGI("code: %d, name: %s, mods: %d, type: %s", key->keycode, key->keyname,
231-
key->modifiers, is_down ? kKeyDown : kKeyUp);
232-
230+
void KeyEventChannel::SendKeyEvent(Ecore_Event_Key* key,
231+
bool is_down,
232+
std::function<void(bool)> callback) {
233233
int gtk_keycode = 0;
234234
if (kKeyCodeMap.count(key->keycode) > 0) {
235235
gtk_keycode = kKeyCodeMap.at(key->keycode);
@@ -254,7 +254,15 @@ void KeyEventChannel::SendKeyEvent(Ecore_Event_Key* key, bool is_down) {
254254
} else {
255255
event.AddMember(kTypeKey, kKeyUp, allocator);
256256
}
257-
channel_->Send(event);
257+
channel_->Send(event, [callback = std::move(callback)](const uint8_t* reply,
258+
size_t reply_size) {
259+
if (reply != nullptr) {
260+
auto decoded = flutter::JsonMessageCodec::GetInstance().DecodeMessage(
261+
reply, reply_size);
262+
bool handled = (*decoded)[kHandledKey].GetBool();
263+
callback(handled);
264+
}
265+
});
258266
}
259267

260268
} // namespace flutter

shell/platform/tizen/channels/key_event_channel.h

+3-1
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,9 @@ class KeyEventChannel {
2121
explicit KeyEventChannel(BinaryMessenger* messenger);
2222
virtual ~KeyEventChannel();
2323

24-
void SendKeyEvent(Ecore_Event_Key* key, bool is_down);
24+
void SendKeyEvent(Ecore_Event_Key* key,
25+
bool is_down,
26+
std::function<void(bool)> callback);
2527

2628
private:
2729
std::unique_ptr<BasicMessageChannel<rapidjson::Document>> channel_;

shell/platform/tizen/key_event_handler.cc

+35-17
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,20 @@
44

55
#include "key_event_handler.h"
66

7-
#include "flutter/shell/platform/tizen/flutter_tizen_engine.h"
7+
#include <app.h>
88

9-
static constexpr char kPlatformBackButtonName[] = "XF86Back";
9+
#include "flutter/shell/platform/tizen/flutter_tizen_engine.h"
10+
#include "flutter/shell/platform/tizen/tizen_log.h"
1011

1112
namespace flutter {
1213

14+
namespace {
15+
16+
constexpr char kBackKey[] = "XF86Back";
17+
constexpr char kExitKey[] = "XF86Exit";
18+
19+
} // namespace
20+
1321
KeyEventHandler::KeyEventHandler(FlutterTizenEngine* engine) : engine_(engine) {
1422
key_event_handlers_.push_back(
1523
ecore_event_handler_add(ECORE_EVENT_KEY_DOWN, OnKey, this));
@@ -30,24 +38,34 @@ Eina_Bool KeyEventHandler::OnKey(void* data, int type, void* event) {
3038
auto* engine = self->engine_;
3139
auto is_down = type == ECORE_EVENT_KEY_DOWN;
3240

33-
if (strcmp(key->keyname, kPlatformBackButtonName) == 0) {
34-
// The device back button was pressed.
35-
if (engine->navigation_channel && !is_down) {
36-
engine->navigation_channel->PopRoute();
37-
}
38-
} else {
39-
if (engine->text_input_channel) {
40-
if (is_down) {
41-
engine->text_input_channel->OnKeyDown(key);
42-
}
43-
if (engine->text_input_channel->IsSoftwareKeyboardShowing()) {
44-
return ECORE_CALLBACK_PASS_ON;
45-
}
41+
FT_LOGI("Keycode: %d, name: %s, mods: %d, is_down: %d", key->keycode,
42+
key->keyname, key->modifiers, is_down);
43+
44+
if (engine->text_input_channel) {
45+
if (is_down) {
46+
engine->text_input_channel->OnKeyDown(key);
4647
}
47-
if (engine->key_event_channel) {
48-
engine->key_event_channel->SendKeyEvent(key, is_down);
48+
if (engine->text_input_channel->IsSoftwareKeyboardShowing()) {
49+
return ECORE_CALLBACK_PASS_ON;
4950
}
5051
}
52+
53+
if (engine->key_event_channel) {
54+
engine->key_event_channel->SendKeyEvent(
55+
key, is_down,
56+
[engine, keyname = std::string(key->keyname), is_down](bool handled) {
57+
if (handled) {
58+
return;
59+
}
60+
if (keyname == kBackKey && !is_down) {
61+
if (engine->navigation_channel) {
62+
engine->navigation_channel->PopRoute();
63+
}
64+
} else if (keyname == kExitKey && !is_down) {
65+
ui_app_exit();
66+
}
67+
});
68+
}
5169
return ECORE_CALLBACK_PASS_ON;
5270
}
5371

0 commit comments

Comments
 (0)