Skip to content

Commit

Permalink
Handle back button events (#19)
Browse files Browse the repository at this point in the history
* Create channels directory

* Handle back button events
  • Loading branch information
swift-kim authored and GitHub Enterprise committed Oct 12, 2020
1 parent e45fefe commit d5b9405
Show file tree
Hide file tree
Showing 17 changed files with 248 additions and 24 deletions.
12 changes: 7 additions & 5 deletions shell/platform/tizen/BUILD.gn
Original file line number Diff line number Diff line change
Expand Up @@ -50,18 +50,20 @@ source_set("flutter_tizen_headers") {

source_set("flutter_tizen") {
sources = [
"channels/key_event_channel.cc",
"channels/lifecycle_channel.cc",
"channels/localization_channel.cc",
"channels/navigation_channel.cc",
"channels/platform_channel.cc",
"channels/text_input_plugin.cc",
"channels/touch_event_channel.cc",
"flutter_tizen.cc",
"key_event_channel.cc",
"lifecycle_channel.cc",
"localization_channel.cc",
"text_input_plugin.cc",
"tizen_embedder_engine.cc",
"tizen_event_loop.cc",
"tizen_surface.cc",
"tizen_surface_gl.cc",
"tizen_surface_software.cc",
"tizen_vsync_waiter.cc",
"touch_event_channel.cc",
]

defines = [ "USE_RAPID_JSON" ]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,12 @@

#include "key_event_channel.h"

#include "flutter/shell/platform/tizen/channels/navigation_channel.h"
#include "flutter/shell/platform/tizen/channels/text_input_plugin.h"
#include "flutter/shell/platform/tizen/logger.h"
#include "flutter/shell/platform/tizen/text_input_plugin.h"

static constexpr char kChannelName[] = "flutter/keyevent";

static constexpr char kKeyMapKey[] = "keymap";
static constexpr char kKeyCodeKey[] = "keyCode";
static constexpr char kTypeKey[] = "type";
Expand All @@ -30,6 +32,8 @@ static constexpr char kTizenKeyMap[] = "tizen";
static constexpr char kKeyUp[] = "keyup";
static constexpr char kKeyDown[] = "keydown";

static constexpr char kPlatformBackButtonName[] = "XF86Back";

KeyEventChannel::KeyEventChannel(flutter::BinaryMessenger* messenger)
: channel_(
std::make_unique<flutter::BasicMessageChannel<rapidjson::Document>>(
Expand All @@ -41,8 +45,13 @@ KeyEventChannel::KeyEventChannel(flutter::BinaryMessenger* messenger)
[](void* data, int type, void* event) -> Eina_Bool {
Ecore_Event_Key* keyEvent = reinterpret_cast<Ecore_Event_Key*>(event);
auto self = reinterpret_cast<KeyEventChannel*>(data);
if (!self->textInputPlugin_ ||
!self->textInputPlugin_->isSoftwareKeyboardShowing()) {

if (strcmp(keyEvent->keyname, kPlatformBackButtonName) == 0) {
// The device back button was pressed. Do nothing.
} else if (self->textInputPlugin_ &&
self->textInputPlugin_->isSoftwareKeyboardShowing()) {
// Handled by software keyboard.
} else {
self->OnKeyDown(keyEvent);
}
return ECORE_CALLBACK_PASS_ON;
Expand All @@ -54,8 +63,16 @@ KeyEventChannel::KeyEventChannel(flutter::BinaryMessenger* messenger)
[](void* data, int type, void* event) -> Eina_Bool {
Ecore_Event_Key* keyEvent = reinterpret_cast<Ecore_Event_Key*>(event);
auto self = reinterpret_cast<KeyEventChannel*>(data);
if (!self->textInputPlugin_ ||
!self->textInputPlugin_->isSoftwareKeyboardShowing()) {

if (strcmp(keyEvent->keyname, kPlatformBackButtonName) == 0) {
// The device back button was pressed.
if (self->navigation_channel_) {
self->navigation_channel_->PopRoute();
}
} else if (self->textInputPlugin_ &&
self->textInputPlugin_->isSoftwareKeyboardShowing()) {
// Handled by software keyboard.
} else {
self->OnKeyUp(keyEvent);
}
return ECORE_CALLBACK_PASS_ON;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,24 +24,31 @@
#include "flutter/shell/platform/common/cpp/client_wrapper/include/flutter/basic_message_channel.h"
#include "flutter/shell/platform/common/cpp/client_wrapper/include/flutter/binary_messenger.h"
#include "flutter/shell/platform/common/cpp/json_message_codec.h"
#include "logger.h"
#include "rapidjson/document.h"

class NavigationChannel;
class TextInputPlugin;

class KeyEventChannel {
public:
explicit KeyEventChannel(flutter::BinaryMessenger* messenger);
virtual ~KeyEventChannel();

void setNavigationChannel(NavigationChannel* channel) {
navigation_channel_ = channel;
}
void setTextInputPlugin(TextInputPlugin* textInputPlugin) {
textInputPlugin_ = textInputPlugin;
}

private:
std::unique_ptr<flutter::BasicMessageChannel<rapidjson::Document>> channel_;
std::vector<Ecore_Event_Handler*> key_event_handlers;

void OnKeyDown(Ecore_Event_Key* keyDownEvent);
void OnKeyUp(Ecore_Event_Key* keyDownEvent);

NavigationChannel* navigation_channel_;
TextInputPlugin* textInputPlugin_;
};

Expand Down
File renamed without changes.
File renamed without changes.
File renamed without changes.
54 changes: 54 additions & 0 deletions shell/platform/tizen/channels/navigation_channel.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
/*
* Copyright (c) 2020 Samsung Electronics Co., Ltd All Rights Reserved
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

#include "navigation_channel.h"

#include "flutter/shell/platform/common/cpp/json_method_codec.h"
#include "flutter/shell/platform/tizen/logger.h"

static constexpr char kChannelName[] = "flutter/navigation";

static constexpr char kSetInitialRouteMethod[] = "setInitialRoute";
static constexpr char kPushRouteMethod[] = "pushRoute";
static constexpr char kPopRouteMethod[] = "popRoute";

NavigationChannel::NavigationChannel(flutter::BinaryMessenger* messenger)
: channel_(std::make_unique<flutter::MethodChannel<rapidjson::Document>>(
messenger, kChannelName, &flutter::JsonMethodCodec::GetInstance())) {}

NavigationChannel::~NavigationChannel() {}

void NavigationChannel::SetInitialRoute(const std::string& initialRoute) {
auto args = std::make_unique<rapidjson::Document>(rapidjson::kObjectType);
args->Parse("\"" + initialRoute + "\"");

if (!args->HasParseError()) {
channel_->InvokeMethod(kSetInitialRouteMethod, std::move(args));
}
}

void NavigationChannel::PushRoute(const std::string& route) {
auto args = std::make_unique<rapidjson::Document>(rapidjson::kObjectType);
args->Parse("\"" + route + "\"");

if (!args->HasParseError()) {
channel_->InvokeMethod(kPushRouteMethod, std::move(args));
}
}

void NavigationChannel::PopRoute() {
channel_->InvokeMethod(kPopRouteMethod, nullptr);
}
37 changes: 37 additions & 0 deletions shell/platform/tizen/channels/navigation_channel.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
/*
* Copyright (c) 2020 Samsung Electronics Co., Ltd All Rights Reserved
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

#ifndef EMBEDDER_NAVIGATION_CHANNEL_H_
#define EMBEDDER_NAVIGATION_CHANNEL_H_

#include "flutter/shell/platform/common/cpp/client_wrapper/include/flutter/binary_messenger.h"
#include "flutter/shell/platform/common/cpp/client_wrapper/include/flutter/method_channel.h"
#include "rapidjson/document.h"

class NavigationChannel {
public:
explicit NavigationChannel(flutter::BinaryMessenger* messenger);
virtual ~NavigationChannel();

void SetInitialRoute(const std::string& initialRoute);
void PushRoute(const std::string& route);
void PopRoute();

private:
std::unique_ptr<flutter::MethodChannel<rapidjson::Document>> channel_;
};

#endif // EMBEDDER_NAVIGATION_CHANNEL_H_
69 changes: 69 additions & 0 deletions shell/platform/tizen/channels/platform_channel.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
/*
* Copyright (c) 2020 Samsung Electronics Co., Ltd All Rights Reserved
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

#include "platform_channel.h"

#include "flutter/shell/platform/common/cpp/json_method_codec.h"
#include "flutter/shell/platform/tizen/logger.h"

static constexpr char kChannelName[] = "flutter/platform";

PlatformChannel::PlatformChannel(flutter::BinaryMessenger* messenger)
: channel_(std::make_unique<flutter::MethodChannel<rapidjson::Document>>(
messenger, kChannelName, &flutter::JsonMethodCodec::GetInstance())) {
channel_->SetMethodCallHandler(
[this](
const flutter::MethodCall<rapidjson::Document>& call,
std::unique_ptr<flutter::MethodResult<rapidjson::Document>> result) {
HandleMethodCall(call, std::move(result));
});
}

PlatformChannel::~PlatformChannel() {}

void PlatformChannel::HandleMethodCall(
const flutter::MethodCall<rapidjson::Document>& call,
std::unique_ptr<flutter::MethodResult<rapidjson::Document>> result) {
const auto method = call.method_name();

if (method == "SystemNavigator.pop") {
exit(EXIT_SUCCESS);
result->Success();
} else if (method == "SystemSound.play") {
result->NotImplemented();
} else if (method == "HapticFeedback.vibrate") {
result->NotImplemented();
} else if (method == "Clipboard.getData") {
result->NotImplemented();
} else if (method == "Clipboard.setData") {
result->NotImplemented();
} else if (method == "Clipboard.hasStrings") {
result->NotImplemented();
} else if (method == "SystemChrome.setPreferredOrientations") {
result->NotImplemented();
} else if (method == "SystemChrome.setApplicationSwitcherDescription") {
result->NotImplemented();
} else if (method == "SystemChrome.setEnabledSystemUIOverlays") {
result->NotImplemented();
} else if (method == "SystemChrome.restoreSystemUIOverlays") {
result->NotImplemented();
} else if (method == "SystemChrome.setSystemUIOverlayStyle") {
result->NotImplemented();
} else {
LoggerI("Unimplemented method: %s", method.c_str());
result->NotImplemented();
}
}
37 changes: 37 additions & 0 deletions shell/platform/tizen/channels/platform_channel.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
/*
* Copyright (c) 2020 Samsung Electronics Co., Ltd All Rights Reserved
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

#ifndef EMBEDDER_PLATFORM_CHANNEL_H_
#define EMBEDDER_PLATFORM_CHANNEL_H_

#include "flutter/shell/platform/common/cpp/client_wrapper/include/flutter/binary_messenger.h"
#include "flutter/shell/platform/common/cpp/client_wrapper/include/flutter/method_channel.h"
#include "rapidjson/document.h"

class PlatformChannel {
public:
explicit PlatformChannel(flutter::BinaryMessenger* messenger);
virtual ~PlatformChannel();

private:
std::unique_ptr<flutter::MethodChannel<rapidjson::Document>> channel_;

void HandleMethodCall(
const flutter::MethodCall<rapidjson::Document>& call,
std::unique_ptr<flutter::MethodResult<rapidjson::Document>> result);
};

#endif // EMBEDDER_PLATFORM_CHANNEL_H_
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
13 changes: 5 additions & 8 deletions shell/platform/tizen/tizen_embedder_engine.cc
Original file line number Diff line number Diff line change
Expand Up @@ -39,11 +39,6 @@ void TizenEmbedderEngine::EmbedderDestoryWindow() {
flutter_engine_ = nullptr;
}

if (touch_event_channel_.get()) {
auto touch_event_channel = touch_event_channel_.release();
delete touch_event_channel;
}

if (tizen_surface_.get()) {
auto tizen_surface = tizen_surface_.release();
delete tizen_surface;
Expand Down Expand Up @@ -182,16 +177,18 @@ TizenEmbedderEngine::RunFlutterEngine(
key_event_channel_ = std::make_unique<KeyEventChannel>(
internal_plugin_registrar_->messenger());
tizen_vsync_waiter_->AsyncWaitForRunEngineSuccess(flutter_engine_);
navigation_channel_ = std::make_unique<NavigationChannel>(
internal_plugin_registrar_->messenger());
text_input_plugin_ = std::make_unique<TextInputPlugin>(
internal_plugin_registrar_->messenger(),
((TizenSurfaceGL*)tizen_surface)->wl2_window());

key_event_channel_->setNavigationChannel(navigation_channel_.get());
key_event_channel_->setTextInputPlugin(text_input_plugin_.get());

platform_channel_ = std::make_unique<PlatformChannel>(
internal_plugin_registrar_->messenger());
localization_channel_ =
std::make_unique<LocalizationChannel>(flutter_engine_);
localization_channel_->SendLocales();

lifecycle_channel_ = std::make_unique<LifecycleChannel>(flutter_engine_);

return flutter_engine_;
Expand Down
14 changes: 9 additions & 5 deletions shell/platform/tizen/tizen_embedder_engine.h
Original file line number Diff line number Diff line change
Expand Up @@ -23,16 +23,18 @@

#include "flutter/shell/platform/common/cpp/client_wrapper/include/flutter/plugin_registrar.h"
#include "flutter/shell/platform/common/cpp/incoming_message_dispatcher.h"
#include "flutter/shell/platform/tizen/key_event_channel.h"
#include "flutter/shell/platform/tizen/lifecycle_channel.h"
#include "flutter/shell/platform/tizen/localization_channel.h"
#include "flutter/shell/platform/tizen/channels/key_event_channel.h"
#include "flutter/shell/platform/tizen/channels/lifecycle_channel.h"
#include "flutter/shell/platform/tizen/channels/localization_channel.h"
#include "flutter/shell/platform/tizen/channels/navigation_channel.h"
#include "flutter/shell/platform/tizen/channels/platform_channel.h"
#include "flutter/shell/platform/tizen/channels/text_input_plugin.h"
#include "flutter/shell/platform/tizen/channels/touch_event_channel.h"
#include "flutter/shell/platform/tizen/public/flutter_tizen.h"
#include "flutter/shell/platform/tizen/text_input_plugin.h"
#include "flutter/shell/platform/tizen/tizen_event_loop.h"
#include "flutter/shell/platform/tizen/tizen_surface.h"
#include "flutter/shell/platform/tizen/tizen_surface_gl.h"
#include "flutter/shell/platform/tizen/tizen_vsync_waiter.h"
#include "flutter/shell/platform/tizen/touch_event_channel.h"

// State associated with the plugin registrar.
struct FlutterDesktopPluginRegistrar {
Expand Down Expand Up @@ -94,6 +96,8 @@ class TizenEmbedderEngine {
std::unique_ptr<TextInputPlugin> text_input_plugin_;
std::unique_ptr<LocalizationChannel> localization_channel_;
std::unique_ptr<LifecycleChannel> lifecycle_channel_;
std::unique_ptr<NavigationChannel> navigation_channel_;
std::unique_ptr<PlatformChannel> platform_channel_;

// The plugin registrar handle given to API clients.
std::unique_ptr<FlutterDesktopPluginRegistrar> plugin_registrar_;
Expand Down

0 comments on commit d5b9405

Please sign in to comment.