Skip to content

Commit

Permalink
Refactor PlatformViewChannel (#256)
Browse files Browse the repository at this point in the history
* Clean up platform_view_channel.cc

* Replace CurrentFocusedViewId with FocusedViewInstance
  • Loading branch information
swift-kim authored Mar 11, 2022
1 parent f493869 commit c42525b
Show file tree
Hide file tree
Showing 2 changed files with 103 additions and 100 deletions.
186 changes: 94 additions & 92 deletions shell/platform/tizen/channels/platform_view_channel.cc
Original file line number Diff line number Diff line change
Expand Up @@ -7,16 +7,16 @@
#include "flutter/shell/platform/common/client_wrapper/include/flutter/plugin_registrar.h"
#include "flutter/shell/platform/common/client_wrapper/include/flutter/standard_message_codec.h"
#include "flutter/shell/platform/common/client_wrapper/include/flutter/standard_method_codec.h"
#include "flutter/shell/platform/common/json_method_codec.h"
#include "flutter/shell/platform/tizen/channels/encodable_value_holder.h"
#include "flutter/shell/platform/tizen/flutter_tizen_engine.h"
#include "flutter/shell/platform/tizen/logger.h"
#include "flutter/shell/platform/tizen/public/flutter_platform_view.h"

namespace flutter {

namespace {

constexpr char kChannelName[] = "flutter/platform_views";

} // namespace

PlatformViewChannel::PlatformViewChannel(BinaryMessenger* messenger)
Expand All @@ -36,63 +36,67 @@ PlatformViewChannel::~PlatformViewChannel() {
}

void PlatformViewChannel::Dispose() {
ClearViewInstances();
ClearViews();
ClearViewFactories();
}

void PlatformViewChannel::RemoveViewInstanceIfNeeded(int view_id) {
auto it = view_instances_.find(view_id);
if (view_id >= 0 && it != view_instances_.end()) {
auto view_instance = it->second;
view_instance->Dispose();
delete view_instance;
view_instances_.erase(it);
PlatformView* PlatformViewChannel::FindViewById(int view_id) {
auto it = views_.find(view_id);
if (it != views_.end()) {
return it->second;
}
return nullptr;
}

PlatformView* PlatformViewChannel::FindFocusedView() {
for (const auto& [view_id, view] : views_) {
if (view->IsFocused()) {
return view;
}
}
return nullptr;
}

void PlatformViewChannel::RemoveViewIfExists(int view_id) {
PlatformView* view = FindViewById(view_id);
if (view) {
view->Dispose();
delete view;
views_.erase(view_id);
}
}

void PlatformViewChannel::ClearViewInstances() {
// Clean-up view_instances_
for (auto const& [view_id, view_instance] : view_instances_) {
view_instance->Dispose();
delete view_instance;
void PlatformViewChannel::ClearViews() {
for (const auto& [view_id, view] : views_) {
view->Dispose();
delete view;
}
view_instances_.clear();
views_.clear();
}

void PlatformViewChannel::ClearViewFactories() {
// Clean-up view_factories_
for (auto const& [view_type, view_factory] : view_factories_) {
for (const auto& [view_type, view_factory] : view_factories_) {
view_factory->Dispose();
}
view_factories_.clear();
}

void PlatformViewChannel::SendKeyEvent(Ecore_Event_Key* key, bool is_down) {
auto instances = ViewInstances();
auto it = instances.find(CurrentFocusedViewId());
if (it != instances.end()) {
void PlatformViewChannel::SendKeyEvent(Ecore_Event_Key* event, bool is_down) {
PlatformView* view = FindFocusedView();
if (view) {
if (is_down) {
it->second->DispatchKeyDownEvent(key);
view->DispatchKeyDownEvent(event);
} else {
it->second->DispatchKeyUpEvent(key);
view->DispatchKeyUpEvent(event);
}
}
}

int PlatformViewChannel::CurrentFocusedViewId() {
for (auto it = view_instances_.begin(); it != view_instances_.end(); it++) {
if (it->second->IsFocused()) {
return it->second->GetViewId();
}
}
return -1;
}

void PlatformViewChannel::HandleMethodCall(
const MethodCall<EncodableValue>& call,
std::unique_ptr<MethodResult<EncodableValue>> result) {
const auto method = call.method_name();
const auto arguments = call.arguments();
const std::string& method = call.method_name();
const EncodableValue* arguments = call.arguments();

if (method == "create") {
OnCreate(arguments, std::move(result));
Expand All @@ -113,44 +117,43 @@ void PlatformViewChannel::HandleMethodCall(
void PlatformViewChannel::OnCreate(
const EncodableValue* arguments,
std::unique_ptr<MethodResult<EncodableValue>>&& result) {
auto map_ptr = std::get_if<EncodableMap>(arguments);
if (!map_ptr) {
auto* map = std::get_if<EncodableMap>(arguments);
if (!map) {
result->Error("Invalid arguments");
return;
}

EncodableValueHolder<std::string> view_type(map_ptr, "viewType");
EncodableValueHolder<int> view_id(map_ptr, "id");
EncodableValueHolder<double> width(map_ptr, "width");
EncodableValueHolder<double> height(map_ptr, "height");
EncodableValueHolder<std::string> view_type(map, "viewType");
EncodableValueHolder<int> view_id(map, "id");
EncodableValueHolder<double> width(map, "width");
EncodableValueHolder<double> height(map, "height");

if (!view_type || !view_id || !width || !height) {
result->Error("Invalid arguments");
return;
}

FT_LOG(Info) << "Creating a platform view: " << *view_type.value;
RemoveViewInstanceIfNeeded(*view_id);
FT_LOG(Info) << "Creating a platform view: " << view_type.value;
RemoveViewIfExists(*view_id);

EncodableValueHolder<ByteMessage> params(map_ptr, "params");
EncodableValueHolder<ByteMessage> params(map, "params");
ByteMessage byte_message;
if (params) {
byte_message = *params;
}
auto it = view_factories_.find(*view_type);
if (it != view_factories_.end()) {
auto focused_view = view_instances_.find(CurrentFocusedViewId());
if (focused_view != view_instances_.end()) {
focused_view->second->SetFocus(false);
PlatformView* focused_view = FindFocusedView();
if (focused_view) {
focused_view->SetFocus(false);
}
auto view_instance =
PlatformView* view =
it->second->Create(*view_id, *width, *height, byte_message);
if (view_instance) {
view_instances_.insert(
std::pair<int, PlatformView*>(*view_id, view_instance));
result->Success(EncodableValue(view_instance->GetTextureId()));
if (view) {
views_[*view_id] = view;
result->Success(EncodableValue(view->GetTextureId()));
} else {
result->Error("Can't create a webview instance!!");
result->Error("Can't create view instance");
}
} else {
FT_LOG(Error) << "Can't find view type: " << *view_type;
Expand All @@ -161,99 +164,93 @@ void PlatformViewChannel::OnCreate(
void PlatformViewChannel::OnClearFocus(
const EncodableValue* arguments,
std::unique_ptr<MethodResult<EncodableValue>>&& result) {
auto view_id_ptr = std::get_if<int>(arguments);
if (!view_id_ptr) {
const int* view_id = std::get_if<int>(arguments);
if (!view_id) {
result->Error("Invalid arguments");
return;
}

auto it = view_instances_.find(*view_id_ptr);
if (it == view_instances_.end()) {
PlatformView* view = FindViewById(*view_id);
if (!view) {
result->Error("Can't find view id");
return;
}
view->SetFocus(false);
view->ClearFocus();

it->second->SetFocus(false);
it->second->ClearFocus();
result->Success();
}

void PlatformViewChannel::OnDispose(
const EncodableValue* arguments,
std::unique_ptr<MethodResult<EncodableValue>>&& result) {
auto map_ptr = std::get_if<EncodableMap>(arguments);
if (!map_ptr) {
auto* map = std::get_if<EncodableMap>(arguments);
if (!map) {
result->Error("Invalid arguments");
return;
}

EncodableValueHolder<int> view_id(map_ptr, "id");

EncodableValueHolder<int> view_id(map, "id");
if (!view_id) {
result->Error("Invalid arguments");
return;
}

if (view_instances_.find(*view_id) == view_instances_.end()) {
PlatformView* view = FindViewById(*view_id);
if (!view) {
result->Error("Can't find view id");
return;
}
RemoveViewIfExists(*view_id);

RemoveViewInstanceIfNeeded(*view_id);
result->Success();
}

void PlatformViewChannel::OnResize(
const EncodableValue* arguments,
std::unique_ptr<MethodResult<EncodableValue>>&& result) {
auto map_ptr = std::get_if<EncodableMap>(arguments);
if (!map_ptr) {
auto* map = std::get_if<EncodableMap>(arguments);
if (!map) {
result->Error("Invalid arguments");
return;
}

EncodableValueHolder<int> view_id(map_ptr, "id");
EncodableValueHolder<double> width(map_ptr, "width");
EncodableValueHolder<double> height(map_ptr, "height");
EncodableValueHolder<int> view_id(map, "id");
EncodableValueHolder<double> width(map, "width");
EncodableValueHolder<double> height(map, "height");

if (!view_id || !width || !height) {
result->Error("Invalid arguments");
return;
}

auto it = view_instances_.find(*view_id);
if (it == view_instances_.end()) {
PlatformView* view = FindViewById(*view_id);
if (!view) {
result->Error("Can't find view id");
return;
}
view->Resize(*width, *height);

it->second->Resize(*width, *height);
result->Success();
}

void PlatformViewChannel::OnTouch(
const EncodableValue* arguments,
std::unique_ptr<MethodResult<EncodableValue>>&& result) {
auto map_ptr = std::get_if<EncodableMap>(arguments);
if (!map_ptr) {
auto* map = std::get_if<EncodableMap>(arguments);
if (!map) {
result->Error("Invalid arguments");
return;
}

int type = 0, button = 0;
double x = 0.0, y = 0.0, dx = 0.0, dy = 0.0;

EncodableValueHolder<EncodableList> event(map_ptr, "event");
EncodableValueHolder<int> view_id(map_ptr, "id");
EncodableValueHolder<EncodableList> event(map, "event");
EncodableValueHolder<int> view_id(map, "id");

if (!view_id || !event || event->size() != 6) {
result->Error("Invalid Arguments");
return;
}

auto it = view_instances_.find(*view_id);
if (it == view_instances_.end()) {
result->Error("Can't find view id");
result->Error("Invalid arguments");
return;
}

Expand All @@ -264,18 +261,23 @@ void PlatformViewChannel::OnTouch(
dx = std::get<double>(event->at(4));
dy = std::get<double>(event->at(5));

it->second->Touch(type, button, x, y, dx, dy);
PlatformView* view = FindViewById(*view_id);
if (!view) {
result->Error("Can't find view id");
return;
}
view->Touch(type, button, x, y, dx, dy);

if (!it->second->IsFocused()) {
auto focused_view = view_instances_.find(CurrentFocusedViewId());
if (focused_view != view_instances_.end()) {
focused_view->second->SetFocus(false);
if (!view->IsFocused()) {
PlatformView* focused_view = FindFocusedView();
if (focused_view) {
focused_view->SetFocus(false);
}

it->second->SetFocus(true);
view->SetFocus(true);
if (channel_ != nullptr) {
auto id = std::make_unique<EncodableValue>(*view_id);
channel_->InvokeMethod("viewFocused", std::move(id));
auto args = std::make_unique<EncodableValue>(*view_id);
channel_->InvokeMethod("viewFocused", std::move(args));
}
}

Expand Down
17 changes: 9 additions & 8 deletions shell/platform/tizen/channels/platform_view_channel.h
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@

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

class PlatformView;
class PlatformViewFactory;
Expand All @@ -26,19 +25,21 @@ class PlatformViewChannel {
virtual ~PlatformViewChannel();

void Dispose();
void RemoveViewInstanceIfNeeded(int view_id);
void ClearViewInstances();
void ClearViewFactories();

std::map<std::string, std::unique_ptr<PlatformViewFactory>>& ViewFactories() {
return view_factories_;
}
std::map<int, PlatformView*>& ViewInstances() { return view_instances_; }

void SendKeyEvent(Ecore_Event_Key* key, bool is_down);
int CurrentFocusedViewId();
void SendKeyEvent(Ecore_Event_Key* event, bool is_down);

private:
PlatformView* FindViewById(int view_id);
PlatformView* FindFocusedView();

void RemoveViewIfExists(int view_id);
void ClearViews();
void ClearViewFactories();

void HandleMethodCall(const MethodCall<EncodableValue>& call,
std::unique_ptr<MethodResult<EncodableValue>> result);

Expand All @@ -55,7 +56,7 @@ class PlatformViewChannel {

std::unique_ptr<MethodChannel<EncodableValue>> channel_;
std::map<std::string, std::unique_ptr<PlatformViewFactory>> view_factories_;
std::map<int, PlatformView*> view_instances_;
std::map<int, PlatformView*> views_;
};

} // namespace flutter
Expand Down

0 comments on commit c42525b

Please sign in to comment.