Skip to content

Commit

Permalink
platform-views: implement more APIs in platform views
Browse files Browse the repository at this point in the history
Signed-off-by: Hidenori Matsubayashi <Hidenori.Matsubayashi@gmail.com>
  • Loading branch information
HidenoriMatsubayashi committed Aug 20, 2023
1 parent 549415f commit 89febfa
Show file tree
Hide file tree
Showing 3 changed files with 141 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -21,12 +21,17 @@ constexpr char kAcceptGestureMethod[] = "acceptGesture";
constexpr char kRejectGestureMethod[] = "rejectGesture";
constexpr char kEnterMethod[] = "enter";
constexpr char kExitMethod[] = "exit";
constexpr char kOffsetMethod[] = "offset";

constexpr char kViewTypeKey[] = "viewType";
constexpr char kIdKey[] = "id";
constexpr char kWidthKey[] = "width";
constexpr char kHeightKey[] = "height";
constexpr char kParamsKey[] = "params";

constexpr char kEventKey[] = "event";
constexpr char kTopKey[] = "top";
constexpr char kLeftKey[] = "left";
} // namespace

PlatformViewsPlugin::PlatformViewsPlugin(BinaryMessenger* messenger)
Expand Down Expand Up @@ -63,6 +68,7 @@ void PlatformViewsPlugin::RegisterViewFactory(
<< view_type;
return;
}
ELINUX_LOG(DEBUG) << view_type << " was registered";
platform_view_factories_[view_type] = std::move(factory);
}

Expand All @@ -78,13 +84,13 @@ void PlatformViewsPlugin::HandleMethodCall(
} else if (method.compare(kDisposeMethod) == 0) {
PlatformViewsDispose(arguments, std::move(result));
} else if (method.compare(kResizeMethod) == 0) {
result->NotImplemented();
PlatformViewsResize(arguments, std::move(result));
} else if (method.compare(kSetDirectionMethod) == 0) {
result->NotImplemented();
} else if (method.compare(kClearFocusMethod) == 0) {
result->NotImplemented();
PlatformViewsClearFocus(arguments, std::move(result));
} else if (method.compare(kTouchMethod) == 0) {
result->NotImplemented();
PlatformViewsTouch(arguments, std::move(result));
} else if (method.compare(kAcceptGestureMethod) == 0) {
result->NotImplemented();
} else if (method.compare(kRejectGestureMethod) == 0) {
Expand All @@ -93,6 +99,8 @@ void PlatformViewsPlugin::HandleMethodCall(
result->NotImplemented();
} else if (method.compare(kExitMethod) == 0) {
result->NotImplemented();
} else if (method.compare(kOffsetMethod) == 0) {
PlatformViewsOffset(arguments, std::move(result));
} else {
ELINUX_LOG(WARNING) << "Platform Views unexpected method is called: "
<< method;
Expand All @@ -109,10 +117,6 @@ void PlatformViewsPlugin::PlatformViewsCreate(
return;
}
auto view_id = LookupEncodableMap<int>(arguments, kIdKey);
if (!view_id) {
result->Error("Couldn't find the view id in the arguments");
return;
}
auto view_width = LookupEncodableMap<double>(arguments, kWidthKey);
if (!view_width) {
result->Error("Couldn't find width in the arguments");
Expand Down Expand Up @@ -153,17 +157,109 @@ void PlatformViewsPlugin::PlatformViewsDispose(
const flutter::EncodableValue& arguments,
std::unique_ptr<flutter::MethodResult<flutter::EncodableValue>> result) {
auto view_id = LookupEncodableMap<int>(arguments, kIdKey);
if (!view_id) {
ELINUX_LOG(DEBUG) << "Dispose the platform view: id = " << view_id;
if (platform_views_.find(view_id) == platform_views_.end()) {
result->Error("Couldn't find the view id in the arguments");
return;
}
platform_views_[view_id]->Dispose();
result->Success();
}

ELINUX_LOG(DEBUG) << "Dispose the platform view: id = " << view_id;
void PlatformViewsPlugin::PlatformViewsClearFocus(
const flutter::EncodableValue& arguments,
std::unique_ptr<flutter::MethodResult<flutter::EncodableValue>> result) {
auto view_id = LookupEncodableMap<int>(arguments, kIdKey);
ELINUX_LOG(DEBUG) << "ClearFocus the platform view: id = " << view_id;
if (platform_views_.find(view_id) == platform_views_.end()) {
result->Error("Couldn't find the view id in the arguments");
return;
}
platform_views_[view_id]->Dispose();

platform_views_[view_id]->SetFocus(false);
platform_views_[view_id]->ClearFocus();

result->Success();
}

void PlatformViewsPlugin::PlatformViewsResize(
const flutter::EncodableValue& arguments,
std::unique_ptr<flutter::MethodResult<flutter::EncodableValue>> result) {
auto view_id = LookupEncodableMap<int>(arguments, kIdKey);
auto view_width = LookupEncodableMap<double>(arguments, kWidthKey);
if (!view_width) {
result->Error("Couldn't find width in the arguments");
return;
}
auto view_height = LookupEncodableMap<double>(arguments, kHeightKey);
if (!view_height) {
result->Error("Couldn't find height in the arguments");
return;
}

ELINUX_LOG(DEBUG) << "Resize the platform view: id = " << view_id
<< ", width = " << view_width
<< ", height = " << view_height;
if (platform_views_.find(view_id) == platform_views_.end()) {
result->Error("Couldn't find the view id in the arguments");
return;
}

if (!view_width || !view_height) {
result->Error("width and height must be greater than zero");
return;
}

platform_views_[view_id]->Resize(view_width, view_height);

result->Success();
}

void PlatformViewsPlugin::PlatformViewsTouch(
const flutter::EncodableValue& arguments,
std::unique_ptr<flutter::MethodResult<flutter::EncodableValue>> result) {
auto view_id = LookupEncodableMap<int>(arguments, kIdKey);
auto event = LookupEncodableMap<EncodableList>(arguments, kEventKey);
if (event.size() < 7) {
result->Error("Couldn't find event in the arguments");
return;
}

ELINUX_LOG(DEBUG) << "Touch the platform view: id = " << view_id;
if (platform_views_.find(view_id) == platform_views_.end()) {
result->Error("Couldn't find the view id in the arguments");
return;
}

auto event_type = std::get<int>(event[0]);
auto device_id = std::get<int>(event[1]);
auto buttons = std::get<int>(event[2]);
auto x = std::get<double>(event[3]);
auto y = std::get<double>(event[4]);
auto delta_x = std::get<double>(event[5]);
auto delta_y = std::get<double>(event[6]);
platform_views_[view_id]->Touch(device_id, event_type, x, y, delta_x,
delta_y);

result->Success();
}

void PlatformViewsPlugin::PlatformViewsOffset(
const flutter::EncodableValue& arguments,
std::unique_ptr<flutter::MethodResult<flutter::EncodableValue>> result) {
auto view_id = LookupEncodableMap<int>(arguments, kIdKey);
auto view_top = LookupEncodableMap<double>(arguments, kTopKey);
auto view_left = LookupEncodableMap<double>(arguments, kLeftKey);
ELINUX_LOG(DEBUG) << "Offset the platform view: view_type = "
<< ", id = " << view_id << ", top = " << view_top
<< ", left = " << view_left;
if (platform_views_.find(view_id) == platform_views_.end()) {
result->Error("Couldn't find the view id in the arguments");
return;
}

platform_views_[view_id]->Offset(view_top, view_left);

result->Success();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,26 @@ class PlatformViewsPlugin {
const flutter::EncodableValue& arguments,
std::unique_ptr<flutter::MethodResult<flutter::EncodableValue>> result);

// Called when "clearFocus" method is called
void PlatformViewsClearFocus(
const flutter::EncodableValue& arguments,
std::unique_ptr<flutter::MethodResult<flutter::EncodableValue>> result);

// Called when "resize" method is called
void PlatformViewsResize(
const flutter::EncodableValue& arguments,
std::unique_ptr<flutter::MethodResult<flutter::EncodableValue>> result);

// Called when "touch" method is called
void PlatformViewsTouch(
const flutter::EncodableValue& arguments,
std::unique_ptr<flutter::MethodResult<flutter::EncodableValue>> result);

// Called when "offset" method is called
void PlatformViewsOffset(
const flutter::EncodableValue& arguments,
std::unique_ptr<flutter::MethodResult<flutter::EncodableValue>> result);

// Method channel instance.
std::unique_ptr<flutter::MethodChannel<flutter::EncodableValue>> channel_;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,11 @@
#include <stdint.h>

#include <memory>
#include <vector>

#include <flutter/plugin_registrar.h>
#include "flutter_export.h"
#include "flutter_messenger.h"
#include "plugin_registrar.h"

class FlutterDesktopPlatformView {
public:
Expand All @@ -31,12 +32,25 @@ class FlutterDesktopPlatformView {

void SetFocus(bool focus) { focused_ = focus; }

virtual void ClearFocus() = 0;

bool IsFocused() const { return focused_; }

void SetTextureId(int texture_id) { texture_id_ = texture_id; }

int GetTextureId() const { return texture_id_; }

virtual void Resize(double width, double height) = 0;

virtual void Touch(int device_id,
int event_type,
double x,
double y,
double delta_x,
double delta_y) = 0;

virtual void Offset(double top, double left) = 0;

private:
flutter::PluginRegistrar* registrar_;
int view_id_;
Expand Down

0 comments on commit 89febfa

Please sign in to comment.