Skip to content

Commit

Permalink
Clean up the embedder API and refactor (#83)
Browse files Browse the repository at this point in the history
* Rename types and functions

* Clean up engine APIs

* Add FlutterDesktopEngineGetMessenger API

* Refactor FlutterTizenEngine a bit

* Refactor channels
  • Loading branch information
swift-kim committed Sep 27, 2021
1 parent a70e833 commit e42dbe6
Show file tree
Hide file tree
Showing 27 changed files with 303 additions and 364 deletions.
2 changes: 1 addition & 1 deletion shell/platform/tizen/BUILD.gn
Original file line number Diff line number Diff line change
Expand Up @@ -94,9 +94,9 @@ template("embedder_for_profile") {
"channels/settings_channel.cc",
"channels/text_input_channel.cc",
"external_texture_gl.cc",
"flutter_tizen_engine.cc",
"flutter_tizen.cc",
"key_event_handler.cc",
"tizen_embedder_engine.cc",
"tizen_event_loop.cc",
"tizen_log.cc",
"tizen_renderer.cc",
Expand Down
1 change: 1 addition & 0 deletions shell/platform/tizen/channels/lifecycle_channel.h
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ class LifecycleChannel {
public:
explicit LifecycleChannel(FLUTTER_API_SYMBOL(FlutterEngine) flutter_engine);
virtual ~LifecycleChannel();

void AppIsInactive();
void AppIsResumed();
void AppIsPaused();
Expand Down
61 changes: 31 additions & 30 deletions shell/platform/tizen/channels/localization_channel.cc
Original file line number Diff line number Diff line change
Expand Up @@ -73,8 +73,8 @@ void LocalizationChannel::SendPlatformResolvedLocale() {
return;
}

FlutterLocale* flutterLocale = GetFlutterLocale(locale);
if (!flutterLocale) {
FlutterLocale* flutter_locale = GetFlutterLocale(locale);
if (!flutter_locale) {
FT_LOGE("Language code is required but not present.");
return;
}
Expand All @@ -86,14 +86,15 @@ void LocalizationChannel::SendPlatformResolvedLocale() {
document.AddMember("method", "setPlatformResolvedLocale", allocator);

rapidjson::Value language_code, country_code, script_code, variant_code;
language_code.SetString(flutterLocale->language_code, allocator);
language_code.SetString(flutter_locale->language_code, allocator);
country_code.SetString(
flutterLocale->country_code ? flutterLocale->country_code : "",
flutter_locale->country_code ? flutter_locale->country_code : "",
allocator);
script_code.SetString(
flutterLocale->script_code ? flutterLocale->script_code : "", allocator);
flutter_locale->script_code ? flutter_locale->script_code : "",
allocator);
variant_code.SetString(
flutterLocale->variant_code ? flutterLocale->variant_code : "",
flutter_locale->variant_code ? flutter_locale->variant_code : "",
allocator);

rapidjson::Value args(rapidjson::kArrayType);
Expand All @@ -118,7 +119,7 @@ void LocalizationChannel::SendPlatformResolvedLocale() {
message.response_handle = nullptr;
FlutterEngineSendPlatformMessage(flutter_engine_, &message);

DestroyFlutterLocale(flutterLocale);
DestroyFlutterLocale(flutter_locale);
}

FlutterLocale* LocalizationChannel::GetFlutterLocale(const char* locale) {
Expand Down Expand Up @@ -166,39 +167,39 @@ FlutterLocale* LocalizationChannel::GetFlutterLocale(const char* locale) {
variant[bufSize] = '\0';
}

FlutterLocale* flutterLocale = new FlutterLocale;
flutterLocale->struct_size = sizeof(FlutterLocale);
flutterLocale->language_code = language;
flutterLocale->country_code = country;
flutterLocale->script_code = script;
flutterLocale->variant_code = variant;
FlutterLocale* flutter_locale = new FlutterLocale;
flutter_locale->struct_size = sizeof(FlutterLocale);
flutter_locale->language_code = language;
flutter_locale->country_code = country;
flutter_locale->script_code = script;
flutter_locale->variant_code = variant;

return flutterLocale;
return flutter_locale;
}

void LocalizationChannel::DestroyFlutterLocale(FlutterLocale* flutterLocale) {
if (flutterLocale) {
if (flutterLocale->language_code) {
delete[] flutterLocale->language_code;
flutterLocale->language_code = nullptr;
void LocalizationChannel::DestroyFlutterLocale(FlutterLocale* flutter_locale) {
if (flutter_locale) {
if (flutter_locale->language_code) {
delete[] flutter_locale->language_code;
flutter_locale->language_code = nullptr;
}

if (flutterLocale->country_code) {
delete[] flutterLocale->country_code;
flutterLocale->country_code = nullptr;
if (flutter_locale->country_code) {
delete[] flutter_locale->country_code;
flutter_locale->country_code = nullptr;
}

if (flutterLocale->script_code) {
delete[] flutterLocale->script_code;
flutterLocale->script_code = nullptr;
if (flutter_locale->script_code) {
delete[] flutter_locale->script_code;
flutter_locale->script_code = nullptr;
}

if (flutterLocale->variant_code) {
delete[] flutterLocale->variant_code;
flutterLocale->variant_code = nullptr;
if (flutter_locale->variant_code) {
delete[] flutter_locale->variant_code;
flutter_locale->variant_code = nullptr;
}

delete flutterLocale;
flutterLocale = nullptr;
delete flutter_locale;
flutter_locale = nullptr;
}
}
4 changes: 3 additions & 1 deletion shell/platform/tizen/channels/localization_channel.h
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,15 @@ class LocalizationChannel {
explicit LocalizationChannel(FLUTTER_API_SYMBOL(FlutterEngine)
flutter_engine);
virtual ~LocalizationChannel();

void SendLocales();

private:
void SendPlatformResolvedLocale();
FlutterLocale* GetFlutterLocale(const char* locale);
void DestroyFlutterLocale(FlutterLocale* flutterLocale);
void DestroyFlutterLocale(FlutterLocale* flutter_locale);

FLUTTER_API_SYMBOL(FlutterEngine) flutter_engine_;
};

#endif // EMBEDDER_LOCALIZATION_CHANNEL_H_
10 changes: 4 additions & 6 deletions shell/platform/tizen/channels/platform_channel.cc
Original file line number Diff line number Diff line change
Expand Up @@ -16,16 +16,14 @@ static constexpr char kChannelName[] = "flutter/platform";
PlatformChannel::PlatformChannel(flutter::BinaryMessenger* messenger,
TizenRenderer* renderer)
: channel_(std::make_unique<flutter::MethodChannel<rapidjson::Document>>(
messenger, kChannelName, &flutter::JsonMethodCodec::GetInstance())) {
messenger, kChannelName, &flutter::JsonMethodCodec::GetInstance())),
renderer_(renderer) {
channel_->SetMethodCallHandler(
[this](
const flutter::MethodCall<rapidjson::Document>& call,
std::unique_ptr<flutter::MethodResult<rapidjson::Document>> result) {
HandleMethodCall(call, std::move(result));
});
// renderer pointer is managed by TizenEmbedderEngine
// !! can be nullptr in case of service application !!
tizen_renderer_ = renderer;
}

PlatformChannel::~PlatformChannel() {}
Expand All @@ -49,7 +47,7 @@ void PlatformChannel::HandleMethodCall(
} else if (method == "Clipboard.hasStrings") {
result->NotImplemented();
} else if (method == "SystemChrome.setPreferredOrientations") {
if (tizen_renderer_) {
if (renderer_) {
static const std::string kPortraitUp = "DeviceOrientation.portraitUp";
static const std::string kPortraitDown = "DeviceOrientation.portraitDown";
static const std::string kLandscapeLeft =
Expand Down Expand Up @@ -79,7 +77,7 @@ void PlatformChannel::HandleMethodCall(
FT_LOGD("No rotations passed, using default values");
rotations = {0, 90, 180, 270};
}
tizen_renderer_->SetPreferredOrientations(rotations);
renderer_->SetPreferredOrientations(rotations);
result->Success();
} else {
result->Error("Not supported for service applications");
Expand Down
9 changes: 6 additions & 3 deletions shell/platform/tizen/channels/platform_channel.h
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,15 @@ class PlatformChannel {
virtual ~PlatformChannel();

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

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

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

// A reference to the renderer object managed by FlutterTizenEngine.
// This can be nullptr if the engine is running in headless mode.
TizenRenderer* renderer_;
};

#endif // EMBEDDER_PLATFORM_CHANNEL_H_
4 changes: 2 additions & 2 deletions shell/platform/tizen/channels/platform_view_channel.cc
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@
#include "flutter/shell/platform/common/cpp/client_wrapper/include/flutter/standard_method_codec.h"
#include "flutter/shell/platform/common/cpp/json_method_codec.h"
#include "flutter/shell/platform/tizen/channels/text_input_channel.h"
#include "flutter/shell/platform/tizen/flutter_tizen_engine.h"
#include "flutter/shell/platform/tizen/public/flutter_platform_view.h"
#include "flutter/shell/platform/tizen/tizen_embedder_engine.h"
#include "flutter/shell/platform/tizen/tizen_log.h"

static constexpr char kChannelName[] = "flutter/platform_views";
Expand Down Expand Up @@ -68,7 +68,7 @@ flutter::EncodableList ExtractListFromMap(
}

PlatformViewChannel::PlatformViewChannel(flutter::BinaryMessenger* messenger,
TizenEmbedderEngine* engine)
FlutterTizenEngine* engine)
: engine_(engine),
channel_(
std::make_unique<flutter::MethodChannel<flutter::EncodableValue>>(
Expand Down
16 changes: 9 additions & 7 deletions shell/platform/tizen/channels/platform_view_channel.h
Original file line number Diff line number Diff line change
Expand Up @@ -13,15 +13,17 @@
#include "flutter/shell/platform/common/cpp/client_wrapper/include/flutter/method_channel.h"
#include "rapidjson/document.h"

class TizenEmbedderEngine;
class FlutterTizenEngine;
class PlatformView;
class PlatformViewFactory;
class PlatformViewChannel {
public:
explicit PlatformViewChannel(flutter::BinaryMessenger* messenger,
TizenEmbedderEngine* engine);
FlutterTizenEngine* engine);
virtual ~PlatformViewChannel();

void Dispose();

std::map<std::string, std::unique_ptr<PlatformViewFactory>>& ViewFactories() {
return view_factories_;
}
Expand All @@ -34,14 +36,14 @@ class PlatformViewChannel {
void DispatchCompositionEndEvent(const std::string& key);

private:
TizenEmbedderEngine* engine_;
std::unique_ptr<flutter::MethodChannel<flutter::EncodableValue>> channel_;
std::map<std::string, std::unique_ptr<PlatformViewFactory>> view_factories_;
std::map<int, PlatformView*> view_instances_;

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

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

#endif // EMBEDDER_PLATFORM_VIEW_CHANNEL_H_
21 changes: 10 additions & 11 deletions shell/platform/tizen/channels/settings_channel.cc
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,15 @@

#include "settings_channel.h"

static constexpr char CHANNEL_NAME[] = "flutter/settings";
static constexpr char TEXT_SCALE_FACTOR[] = "textScaleFactor";
static constexpr char ALWAYS_USE_24_HOUR_FORMAT[] = "alwaysUse24HourFormat";
static constexpr char PLATFORM_BRIGHTNESS[] = "platformBrightness";
static constexpr char kChannelName[] = "flutter/settings";
static constexpr char kTextScaleFactorKey[] = "textScaleFactor";
static constexpr char kAlwaysUse24HourFormatKey[] = "alwaysUse24HourFormat";
static constexpr char kPlatformBrightnessKey[] = "platformBrightness";

SettingsChannel::SettingsChannel(flutter::BinaryMessenger* messenger)
: channel_(
std::make_unique<flutter::BasicMessageChannel<rapidjson::Document>>(
messenger, CHANNEL_NAME,
messenger, kChannelName,
&flutter::JsonMessageCodec::GetInstance())) {
system_settings_set_changed_cb(SYSTEM_SETTINGS_KEY_LOCALE_TIMEFORMAT_24HOUR,
OnSettingsChangedCallback, this);
Expand All @@ -31,16 +31,15 @@ void SettingsChannel::SendSettingsEvent() {
int ret = system_settings_get_value_bool(
SYSTEM_SETTINGS_KEY_LOCALE_TIMEFORMAT_24HOUR, &value);
if (ret == SYSTEM_SETTINGS_ERROR_NONE) {
event.AddMember(TEXT_SCALE_FACTOR, 1.0, allocator);
event.AddMember(PLATFORM_BRIGHTNESS, "light", allocator);
event.AddMember(ALWAYS_USE_24_HOUR_FORMAT, value, allocator);
event.AddMember(kTextScaleFactorKey, 1.0, allocator);
event.AddMember(kPlatformBrightnessKey, "light", allocator);
event.AddMember(kAlwaysUse24HourFormatKey, value, allocator);
channel_->Send(event);
}
}

void SettingsChannel::OnSettingsChangedCallback(system_settings_key_e key,
void* user_data) {
SettingsChannel* settingsChannel =
reinterpret_cast<SettingsChannel*>(user_data);
settingsChannel->SendSettingsEvent();
auto settings_channel = reinterpret_cast<SettingsChannel*>(user_data);
settings_channel->SendSettingsEvent();
}
3 changes: 2 additions & 1 deletion shell/platform/tizen/channels/settings_channel.h
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,11 @@ class SettingsChannel {
virtual ~SettingsChannel();

private:
std::unique_ptr<flutter::BasicMessageChannel<rapidjson::Document>> channel_;
static void OnSettingsChangedCallback(system_settings_key_e key,
void* user_data);
void SendSettingsEvent();

std::unique_ptr<flutter::BasicMessageChannel<rapidjson::Document>> channel_;
};

#endif // EMBEDDER_SETTINGS_CHANNEL_H_
6 changes: 3 additions & 3 deletions shell/platform/tizen/channels/text_input_channel.cc
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
#include <Ecore.h>
#include <Ecore_IMF_Evas.h>

#include "flutter/shell/platform/tizen/tizen_embedder_engine.h"
#include "flutter/shell/platform/tizen/flutter_tizen_engine.h"
#include "flutter/shell/platform/tizen/tizen_log.h"

static constexpr char kSetEditingStateMethod[] = "TextInput.setEditingState";
Expand Down Expand Up @@ -203,7 +203,7 @@ Ecore_IMF_Keyboard_Locks EcoreInputModifierToEcoreIMFLock(
}

TextInputChannel::TextInputChannel(flutter::BinaryMessenger* messenger,
TizenEmbedderEngine* engine)
FlutterTizenEngine* engine)
: channel_(std::make_unique<flutter::MethodChannel<rapidjson::Document>>(
messenger, kChannelName, &flutter::JsonMethodCodec::GetInstance())),
engine_(engine) {
Expand All @@ -223,7 +223,7 @@ TextInputChannel::TextInputChannel(flutter::BinaryMessenger* messenger,
}
if (imf_context_) {
ecore_imf_context_client_window_set(
imf_context_, (void*)engine_->tizen_renderer->GetWindowId());
imf_context_, (void*)engine_->renderer->GetWindowId());
RegisterIMFCallback();
} else {
FT_LOGE("Failed to create imfContext");
Expand Down
9 changes: 6 additions & 3 deletions shell/platform/tizen/channels/text_input_channel.h
Original file line number Diff line number Diff line change
Expand Up @@ -16,16 +16,19 @@
#include "flutter/shell/platform/common/cpp/json_method_codec.h"
#include "flutter/shell/platform/common/cpp/text_input_model.h"

class TizenEmbedderEngine;
class FlutterTizenEngine;
class TextInputChannel {
public:
struct SoftwareKeyboardGeometry {
int32_t x = 0, y = 0, w = 0, h = 0;
};

enum EditStatus { kNone, kPreeditStart, kPreeditEnd, kCommit };

explicit TextInputChannel(flutter::BinaryMessenger* messenger,
TizenEmbedderEngine* engine);
FlutterTizenEngine* engine);
virtual ~TextInputChannel();

void OnKeyDown(Ecore_Event_Key* key);
void OnCommit(std::string str);
void OnPreedit(std::string str, int cursor_pos);
Expand Down Expand Up @@ -91,7 +94,7 @@ class TextInputChannel {
int preedit_end_pos_{0};
int preedit_start_pos_{0};
std::string last_handled_ecore_event_keyname_;
TizenEmbedderEngine* engine_{nullptr};
FlutterTizenEngine* engine_{nullptr};
Ecore_IMF_Context* imf_context_{nullptr};
};

Expand Down
Loading

0 comments on commit e42dbe6

Please sign in to comment.