Skip to content

Commit

Permalink
Let the app handle the back key event first (#126)
Browse files Browse the repository at this point in the history
* Let the app handle the back key event first

* Tidy up
  • Loading branch information
swift-kim committed Dec 9, 2021
1 parent e3e6f15 commit 3fada05
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 23 deletions.
18 changes: 13 additions & 5 deletions shell/platform/tizen/channels/key_event_channel.cc
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ constexpr char kTypeKey[] = "type";
constexpr char kModifiersKey[] = "modifiers";
constexpr char kToolkitKey[] = "toolkit";
constexpr char kUnicodeScalarValuesKey[] = "unicodeScalarValues";
constexpr char kHandledKey[] = "handled";

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

KeyEventChannel::~KeyEventChannel() {}

void KeyEventChannel::SendKeyEvent(Ecore_Event_Key* key, bool is_down) {
FT_LOGI("code: %d, name: %s, mods: %d, type: %s", key->keycode, key->keyname,
key->modifiers, is_down ? kKeyDown : kKeyUp);

void KeyEventChannel::SendKeyEvent(Ecore_Event_Key* key,
bool is_down,
std::function<void(bool)> callback) {
int gtk_keycode = 0;
if (kKeyCodeMap.count(key->keycode) > 0) {
gtk_keycode = kKeyCodeMap.at(key->keycode);
Expand All @@ -254,7 +254,15 @@ void KeyEventChannel::SendKeyEvent(Ecore_Event_Key* key, bool is_down) {
} else {
event.AddMember(kTypeKey, kKeyUp, allocator);
}
channel_->Send(event);
channel_->Send(event, [callback = std::move(callback)](const uint8_t* reply,
size_t reply_size) {
if (reply != nullptr) {
auto decoded = flutter::JsonMessageCodec::GetInstance().DecodeMessage(
reply, reply_size);
bool handled = (*decoded)[kHandledKey].GetBool();
callback(handled);
}
});
}

} // namespace flutter
4 changes: 3 additions & 1 deletion shell/platform/tizen/channels/key_event_channel.h
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,9 @@ class KeyEventChannel {
explicit KeyEventChannel(BinaryMessenger* messenger);
virtual ~KeyEventChannel();

void SendKeyEvent(Ecore_Event_Key* key, bool is_down);
void SendKeyEvent(Ecore_Event_Key* key,
bool is_down,
std::function<void(bool)> callback);

private:
std::unique_ptr<BasicMessageChannel<rapidjson::Document>> channel_;
Expand Down
52 changes: 35 additions & 17 deletions shell/platform/tizen/key_event_handler.cc
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,20 @@

#include "key_event_handler.h"

#include "flutter/shell/platform/tizen/flutter_tizen_engine.h"
#include <app.h>

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

namespace flutter {

namespace {

constexpr char kBackKey[] = "XF86Back";
constexpr char kExitKey[] = "XF86Exit";

} // namespace

KeyEventHandler::KeyEventHandler(FlutterTizenEngine* engine) : engine_(engine) {
key_event_handlers_.push_back(
ecore_event_handler_add(ECORE_EVENT_KEY_DOWN, OnKey, this));
Expand All @@ -30,24 +38,34 @@ Eina_Bool KeyEventHandler::OnKey(void* data, int type, void* event) {
auto* engine = self->engine_;
auto is_down = type == ECORE_EVENT_KEY_DOWN;

if (strcmp(key->keyname, kPlatformBackButtonName) == 0) {
// The device back button was pressed.
if (engine->navigation_channel && !is_down) {
engine->navigation_channel->PopRoute();
}
} else {
if (engine->text_input_channel) {
if (is_down) {
engine->text_input_channel->OnKeyDown(key);
}
if (engine->text_input_channel->IsSoftwareKeyboardShowing()) {
return ECORE_CALLBACK_PASS_ON;
}
FT_LOGI("Keycode: %d, name: %s, mods: %d, is_down: %d", key->keycode,
key->keyname, key->modifiers, is_down);

if (engine->text_input_channel) {
if (is_down) {
engine->text_input_channel->OnKeyDown(key);
}
if (engine->key_event_channel) {
engine->key_event_channel->SendKeyEvent(key, is_down);
if (engine->text_input_channel->IsSoftwareKeyboardShowing()) {
return ECORE_CALLBACK_PASS_ON;
}
}

if (engine->key_event_channel) {
engine->key_event_channel->SendKeyEvent(
key, is_down,
[engine, keyname = std::string(key->keyname), is_down](bool handled) {
if (handled) {
return;
}
if (keyname == kBackKey && !is_down) {
if (engine->navigation_channel) {
engine->navigation_channel->PopRoute();
}
} else if (keyname == kExitKey && !is_down) {
ui_app_exit();
}
});
}
return ECORE_CALLBACK_PASS_ON;
}

Expand Down

0 comments on commit 3fada05

Please sign in to comment.