From c42525b8988f79169ada484bf7b93ac0018876b3 Mon Sep 17 00:00:00 2001 From: Swift Kim Date: Fri, 11 Mar 2022 12:22:54 +0900 Subject: [PATCH] Refactor PlatformViewChannel (#256) * Clean up platform_view_channel.cc * Replace CurrentFocusedViewId with FocusedViewInstance --- .../tizen/channels/platform_view_channel.cc | 186 +++++++++--------- .../tizen/channels/platform_view_channel.h | 17 +- 2 files changed, 103 insertions(+), 100 deletions(-) diff --git a/shell/platform/tizen/channels/platform_view_channel.cc b/shell/platform/tizen/channels/platform_view_channel.cc index 4ca1e1d40b44f..c015d2ce68dc3 100644 --- a/shell/platform/tizen/channels/platform_view_channel.cc +++ b/shell/platform/tizen/channels/platform_view_channel.cc @@ -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) @@ -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& call, std::unique_ptr> 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)); @@ -113,44 +117,43 @@ void PlatformViewChannel::HandleMethodCall( void PlatformViewChannel::OnCreate( const EncodableValue* arguments, std::unique_ptr>&& result) { - auto map_ptr = std::get_if(arguments); - if (!map_ptr) { + auto* map = std::get_if(arguments); + if (!map) { result->Error("Invalid arguments"); return; } - EncodableValueHolder view_type(map_ptr, "viewType"); - EncodableValueHolder view_id(map_ptr, "id"); - EncodableValueHolder width(map_ptr, "width"); - EncodableValueHolder height(map_ptr, "height"); + EncodableValueHolder view_type(map, "viewType"); + EncodableValueHolder view_id(map, "id"); + EncodableValueHolder width(map, "width"); + EncodableValueHolder 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 params(map_ptr, "params"); + EncodableValueHolder 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(*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; @@ -161,81 +164,81 @@ void PlatformViewChannel::OnCreate( void PlatformViewChannel::OnClearFocus( const EncodableValue* arguments, std::unique_ptr>&& result) { - auto view_id_ptr = std::get_if(arguments); - if (!view_id_ptr) { + const int* view_id = std::get_if(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>&& result) { - auto map_ptr = std::get_if(arguments); - if (!map_ptr) { + auto* map = std::get_if(arguments); + if (!map) { result->Error("Invalid arguments"); return; } - EncodableValueHolder view_id(map_ptr, "id"); - + EncodableValueHolder 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>&& result) { - auto map_ptr = std::get_if(arguments); - if (!map_ptr) { + auto* map = std::get_if(arguments); + if (!map) { result->Error("Invalid arguments"); return; } - EncodableValueHolder view_id(map_ptr, "id"); - EncodableValueHolder width(map_ptr, "width"); - EncodableValueHolder height(map_ptr, "height"); + EncodableValueHolder view_id(map, "id"); + EncodableValueHolder width(map, "width"); + EncodableValueHolder 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>&& result) { - auto map_ptr = std::get_if(arguments); - if (!map_ptr) { + auto* map = std::get_if(arguments); + if (!map) { result->Error("Invalid arguments"); return; } @@ -243,17 +246,11 @@ void PlatformViewChannel::OnTouch( int type = 0, button = 0; double x = 0.0, y = 0.0, dx = 0.0, dy = 0.0; - EncodableValueHolder event(map_ptr, "event"); - EncodableValueHolder view_id(map_ptr, "id"); + EncodableValueHolder event(map, "event"); + EncodableValueHolder 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; } @@ -264,18 +261,23 @@ void PlatformViewChannel::OnTouch( dx = std::get(event->at(4)); dy = std::get(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(*view_id); - channel_->InvokeMethod("viewFocused", std::move(id)); + auto args = std::make_unique(*view_id); + channel_->InvokeMethod("viewFocused", std::move(args)); } } diff --git a/shell/platform/tizen/channels/platform_view_channel.h b/shell/platform/tizen/channels/platform_view_channel.h index 5fb80c5560235..0ef3ef8b26fda 100644 --- a/shell/platform/tizen/channels/platform_view_channel.h +++ b/shell/platform/tizen/channels/platform_view_channel.h @@ -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; @@ -26,19 +25,21 @@ class PlatformViewChannel { virtual ~PlatformViewChannel(); void Dispose(); - void RemoveViewInstanceIfNeeded(int view_id); - void ClearViewInstances(); - void ClearViewFactories(); std::map>& ViewFactories() { return view_factories_; } - std::map& 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& call, std::unique_ptr> result); @@ -55,7 +56,7 @@ class PlatformViewChannel { std::unique_ptr> channel_; std::map> view_factories_; - std::map view_instances_; + std::map views_; }; } // namespace flutter