Skip to content

Commit 9b11d80

Browse files
authored
PlatformView: Add Offset method (#33)
Signed-off-by: swan.seo <[email protected]>
1 parent 55821cb commit 9b11d80

File tree

8 files changed

+80
-40
lines changed

8 files changed

+80
-40
lines changed

flutter/shell/platform/tizen/channels/platform_view_channel.cc

Lines changed: 40 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -17,11 +17,13 @@ constexpr char kChannelName[] = "flutter/platform_views";
1717

1818
} // namespace
1919

20-
PlatformViewChannel::PlatformViewChannel(BinaryMessenger* messenger)
20+
PlatformViewChannel::PlatformViewChannel(BinaryMessenger* messenger,
21+
double pixel_ratio)
2122
: channel_(std::make_unique<MethodChannel<EncodableValue>>(
2223
messenger,
2324
kChannelName,
24-
&StandardMethodCodec::GetInstance())) {
25+
&StandardMethodCodec::GetInstance())),
26+
pixel_ratio_(pixel_ratio) {
2527
channel_->SetMethodCallHandler(
2628
[this](const MethodCall<EncodableValue>& call,
2729
std::unique_ptr<MethodResult<EncodableValue>> result) {
@@ -104,6 +106,8 @@ void PlatformViewChannel::HandleMethodCall(
104106
OnClearFocus(arguments, std::move(result));
105107
} else if (method == "dispose") {
106108
OnDispose(arguments, std::move(result));
109+
} else if (method == "offset") {
110+
OnOffset(arguments, std::move(result));
107111
} else if (method == "resize") {
108112
OnResize(arguments, std::move(result));
109113
} else if (method == "touch") {
@@ -147,8 +151,8 @@ void PlatformViewChannel::OnCreate(
147151
if (focused_view) {
148152
focused_view->SetFocus(false);
149153
}
150-
PlatformView* view =
151-
iter->second->Create(*view_id, *width, *height, byte_message);
154+
PlatformView* view = iter->second->Create(
155+
*view_id, *width * pixel_ratio_, *height * pixel_ratio_, byte_message);
152156
if (view) {
153157
views_[*view_id] = view;
154158
result->Success(EncodableValue(view->GetTextureId()));
@@ -206,6 +210,33 @@ void PlatformViewChannel::OnDispose(
206210
result->Success();
207211
}
208212

213+
void PlatformViewChannel::OnOffset(
214+
const EncodableValue* arguments,
215+
std::unique_ptr<MethodResult<EncodableValue>>&& result) {
216+
auto* map = std::get_if<EncodableMap>(arguments);
217+
if (!map) {
218+
result->Error("Invalid arguments");
219+
return;
220+
}
221+
222+
EncodableValueHolder<int> view_id(map, "id");
223+
EncodableValueHolder<double> left(map, "left");
224+
EncodableValueHolder<double> top(map, "top");
225+
226+
if (!view_id || !left || !top) {
227+
result->Error("Invalid arguments");
228+
return;
229+
}
230+
PlatformView* view = FindViewById(*view_id);
231+
if (!view) {
232+
result->Error("Can't find view id");
233+
return;
234+
}
235+
view->Offset(*left * pixel_ratio_, *top * pixel_ratio_);
236+
237+
result->Success();
238+
}
239+
209240
void PlatformViewChannel::OnResize(
210241
const EncodableValue* arguments,
211242
std::unique_ptr<MethodResult<EncodableValue>>&& result) {
@@ -229,7 +260,7 @@ void PlatformViewChannel::OnResize(
229260
result->Error("Can't find view id");
230261
return;
231262
}
232-
view->Resize(*width, *height);
263+
view->Resize(*width * pixel_ratio_, *height * pixel_ratio_);
233264

234265
result->Success(*arguments);
235266
}
@@ -256,10 +287,10 @@ void PlatformViewChannel::OnTouch(
256287

257288
type = std::get<int>(event->at(0));
258289
button = std::get<int>(event->at(1));
259-
x = std::get<double>(event->at(2));
260-
y = std::get<double>(event->at(3));
261-
dx = std::get<double>(event->at(4));
262-
dy = std::get<double>(event->at(5));
290+
x = std::get<double>(event->at(2)) * pixel_ratio_;
291+
y = std::get<double>(event->at(3)) * pixel_ratio_;
292+
dx = std::get<double>(event->at(4)) * pixel_ratio_;
293+
dy = std::get<double>(event->at(5)) * pixel_ratio_;
263294

264295
PlatformView* view = FindViewById(*view_id);
265296
if (!view) {

flutter/shell/platform/tizen/channels/platform_view_channel.h

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111

1212
#include "flutter/shell/platform/common/client_wrapper/include/flutter/binary_messenger.h"
1313
#include "flutter/shell/platform/common/client_wrapper/include/flutter/method_channel.h"
14+
#include "flutter/shell/platform/tizen/tizen_view_base.h"
1415

1516
class PlatformView;
1617
class PlatformViewFactory;
@@ -19,7 +20,7 @@ namespace flutter {
1920

2021
class PlatformViewChannel {
2122
public:
22-
explicit PlatformViewChannel(BinaryMessenger* messenger);
23+
explicit PlatformViewChannel(BinaryMessenger* messenger, double pixel_ratio);
2324
virtual ~PlatformViewChannel();
2425

2526
void Dispose();
@@ -52,6 +53,8 @@ class PlatformViewChannel {
5253
std::unique_ptr<MethodResult<EncodableValue>>&& result);
5354
void OnDispose(const EncodableValue* arguments,
5455
std::unique_ptr<MethodResult<EncodableValue>>&& result);
56+
void OnOffset(const EncodableValue* arguments,
57+
std::unique_ptr<MethodResult<EncodableValue>>&& result);
5558
void OnResize(const EncodableValue* arguments,
5659
std::unique_ptr<MethodResult<EncodableValue>>&& result);
5760
void OnTouch(const EncodableValue* arguments,
@@ -60,6 +63,8 @@ class PlatformViewChannel {
6063
std::unique_ptr<MethodChannel<EncodableValue>> channel_;
6164
std::map<std::string, std::unique_ptr<PlatformViewFactory>> view_factories_;
6265
std::map<int, PlatformView*> views_;
66+
67+
double pixel_ratio_;
6368
};
6469

6570
} // namespace flutter

flutter/shell/platform/tizen/flutter_tizen.cc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -314,7 +314,7 @@ void FlutterDesktopRegisterViewFactory(
314314
FlutterDesktopPluginRegistrarRef registrar,
315315
const char* view_type,
316316
std::unique_ptr<PlatformViewFactory> view_factory) {
317-
registrar->engine->platform_view_channel()->ViewFactories().insert(
317+
registrar->engine->view()->platform_view_channel()->ViewFactories().insert(
318318
std::pair<std::string, std::unique_ptr<PlatformViewFactory>>(
319319
view_type, std::move(view_factory)));
320320
}

flutter/shell/platform/tizen/flutter_tizen_engine.cc

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -250,8 +250,6 @@ bool FlutterTizenEngine::RunEngine() {
250250
void* user_data) { SendKeyEvent(event, callback, user_data); });
251251
navigation_channel_ = std::make_unique<NavigationChannel>(
252252
internal_plugin_registrar_->messenger());
253-
platform_view_channel_ = std::make_unique<PlatformViewChannel>(
254-
internal_plugin_registrar_->messenger());
255253
}
256254

257255
accessibility_settings_ = std::make_unique<AccessibilitySettings>(this);
@@ -263,9 +261,6 @@ bool FlutterTizenEngine::RunEngine() {
263261

264262
bool FlutterTizenEngine::StopEngine() {
265263
if (engine_) {
266-
if (platform_view_channel_) {
267-
platform_view_channel_->Dispose();
268-
}
269264
for (const auto& [callback, registrar] :
270265
plugin_registrar_destruction_callbacks_) {
271266
callback(registrar);

flutter/shell/platform/tizen/flutter_tizen_engine.h

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -107,10 +107,6 @@ class FlutterTizenEngine {
107107

108108
NavigationChannel* navigation_channel() { return navigation_channel_.get(); }
109109

110-
PlatformViewChannel* platform_view_channel() {
111-
return platform_view_channel_.get();
112-
}
113-
114110
#ifndef WEARABLE_PROFILE
115111
std::weak_ptr<flutter::AccessibilityBridge> accessibility_bridge() {
116112
return accessibility_bridge_;
@@ -264,9 +260,6 @@ class FlutterTizenEngine {
264260
// A plugin that implements the Flutter navigation channel.
265261
std::unique_ptr<NavigationChannel> navigation_channel_;
266262

267-
// A plugin that implements the Flutter platform_views channel.
268-
std::unique_ptr<PlatformViewChannel> platform_view_channel_;
269-
270263
// A plugin that implements the Flutter settings channel.
271264
std::unique_ptr<SettingsChannel> settings_channel_;
272265

flutter/shell/platform/tizen/flutter_tizen_view.cc

Lines changed: 25 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,19 @@ const std::vector<std::string> kBindableSystemKeys = {
4949
// (ui/events/x/events_x_utils.cc).
5050
constexpr int32_t kScrollOffsetMultiplier = 53;
5151

52+
double ComputePixelRatio(flutter::TizenViewBase* view) {
53+
// The scale factor is computed based on the display DPI and the current
54+
// profile. A fixed DPI value (72) is used on TVs. See:
55+
// https://docs.tizen.org/application/native/guides/ui/efl/multiple-screens
56+
#ifdef TV_PROFILE
57+
double dpi = 72.0;
58+
#else
59+
double dpi = static_cast<double>(view->GetDpi());
60+
#endif
61+
double scale_factor = dpi / 90.0 * kProfileFactor;
62+
return std::max(scale_factor, 1.0);
63+
}
64+
5265
} // namespace
5366

5467
namespace flutter {
@@ -64,6 +77,9 @@ FlutterTizenView::FlutterTizenView(std::unique_ptr<TizenViewBase> tizen_view)
6477

6578
FlutterTizenView::~FlutterTizenView() {
6679
if (engine_) {
80+
if (platform_view_channel_) {
81+
platform_view_channel_->Dispose();
82+
}
6783
engine_->StopEngine();
6884
}
6985
DestroyRenderSurface();
@@ -84,6 +100,10 @@ void FlutterTizenView::SetEngine(std::unique_ptr<FlutterTizenEngine> engine) {
84100

85101
platform_channel_ =
86102
std::make_unique<PlatformChannel>(messenger, tizen_view_.get());
103+
104+
double pixel_ratio = ComputePixelRatio(tizen_view_.get());
105+
platform_view_channel_ =
106+
std::make_unique<PlatformViewChannel>(messenger, pixel_ratio);
87107
mouse_cursor_channel_ =
88108
std::make_unique<MouseCursorChannel>(messenger, tizen_view_.get());
89109
text_input_channel_ = std::make_unique<TextInputChannel>(
@@ -316,9 +336,9 @@ void FlutterTizenView::OnKey(const char* key,
316336
}
317337
}
318338

319-
if (engine_->platform_view_channel()) {
320-
if (engine_->platform_view_channel()->SendKey(
321-
key, string, compose, modifiers, scan_code, is_down)) {
339+
if (platform_view_channel_) {
340+
if (platform_view_channel_->SendKey(key, string, compose, modifiers,
341+
scan_code, is_down)) {
322342
return;
323343
}
324344
}
@@ -373,23 +393,11 @@ void FlutterTizenView::SendWindowMetrics(int32_t left,
373393
int32_t width,
374394
int32_t height,
375395
double pixel_ratio) {
376-
double computed_pixel_ratio = pixel_ratio;
377396
if (pixel_ratio == 0.0) {
378-
// The scale factor is computed based on the display DPI and the current
379-
// profile. A fixed DPI value (72) is used on TVs. See:
380-
// https://docs.tizen.org/application/native/guides/ui/efl/multiple-screens
381-
#ifdef TV_PROFILE
382-
double dpi = 72.0;
383-
#else
384-
double dpi = static_cast<double>(tizen_view_->GetDpi());
385-
#endif
386-
double scale_factor = dpi / 90.0 * kProfileFactor;
387-
computed_pixel_ratio = std::max(scale_factor, 1.0);
388-
} else {
389-
computed_pixel_ratio = pixel_ratio;
397+
pixel_ratio = ComputePixelRatio(tizen_view_.get());
390398
}
391399

392-
engine_->SendWindowMetrics(left, top, width, height, computed_pixel_ratio);
400+
engine_->SendWindowMetrics(left, top, width, height, pixel_ratio);
393401
}
394402

395403
void FlutterTizenView::SendFlutterPointerEvent(FlutterPointerPhase phase,

flutter/shell/platform/tizen/flutter_tizen_view.h

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -111,6 +111,10 @@ class FlutterTizenView : public TizenViewEventHandlerDelegate {
111111

112112
void SendInitialGeometry();
113113

114+
PlatformViewChannel* platform_view_channel() {
115+
return platform_view_channel_.get();
116+
}
117+
114118
TextInputChannel* text_input_channel() { return text_input_channel_.get(); }
115119

116120
private:
@@ -178,6 +182,9 @@ class FlutterTizenView : public TizenViewEventHandlerDelegate {
178182
// A plugin that implements the Flutter platform channel.
179183
std::unique_ptr<PlatformChannel> platform_channel_;
180184

185+
// A plugin that implements the Flutter platform_views channel.
186+
std::unique_ptr<PlatformViewChannel> platform_view_channel_;
187+
181188
// A plugin that implements the Flutter cursor channel.
182189
std::unique_ptr<MouseCursorChannel> mouse_cursor_channel_;
183190

flutter/shell/platform/tizen/public/flutter_platform_view.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ class PlatformView {
3030

3131
virtual void Dispose() = 0;
3232

33+
virtual void Offset(double left, double top) {}
3334
virtual void Resize(double width, double height) = 0;
3435
virtual void Touch(int type,
3536
int button,

0 commit comments

Comments
 (0)