Skip to content

Commit

Permalink
[Clipboard] setData and getData implementations added (flutter#76)
Browse files Browse the repository at this point in the history
* [Clipboard] setData and getData implementations added

and also removed code for hasStrings, as this API is not present in documentation.

[Before] Clipboard.setData and Clipboard.getData feature was not implemented

[After] Naive implementation of clipboard shared only inside Flutter application
and only via Clipboard API. Such decision was done because of supporting partial
functionality on all profiles. CBHM API is supported only on mobile and currently
it is not possible to support it in other way.

Dart code below is able to set/get internal clipboard data:
      ClipboardData cd = ClipboardData(text: 'some text');
      Clipboard.setData(cd);

      Future<ClipboardData?> d = Clipboard.getData(Clipboard.kTextPlain);
      void dataCallback(ClipboardData? d) {
        if (d != null) {
          String? text = d.text;
          if(text != null) {
            print("Clipboard data $text");
          }
        }
      }
      d.then(dataCallback);

* [Clipboard] Fixes after review

* Removed unnecessary mutex
* Cleaned temporary logs
  • Loading branch information
pkosko authored and swift-kim committed Dec 9, 2021
1 parent 6dfe2d7 commit e320819
Show file tree
Hide file tree
Showing 2 changed files with 61 additions and 4 deletions.
56 changes: 52 additions & 4 deletions shell/platform/tizen/channels/platform_channel.cc
Original file line number Diff line number Diff line change
Expand Up @@ -214,11 +214,9 @@ void PlatformChannel::HandleMethodCall(

result->Error(error_cause, error_message);
} else if (method == "Clipboard.getData") {
result->NotImplemented();
Clipboard::GetData(call, std::move(result));
} else if (method == "Clipboard.setData") {
result->NotImplemented();
} else if (method == "Clipboard.hasStrings") {
result->NotImplemented();
Clipboard::SetData(call, std::move(result));
} else if (method == "SystemChrome.setPreferredOrientations") {
if (renderer_) {
static const std::string kPortraitUp = "DeviceOrientation.portraitUp";
Expand Down Expand Up @@ -268,3 +266,53 @@ void PlatformChannel::HandleMethodCall(
result->NotImplemented();
}
}

// Clipboard constants and variables
namespace Clipboard {

// naive implementation using std::string as a container of internal clipboard
// data
std::string string_clipboard = "";

static constexpr char kTextKey[] = "text";
static constexpr char kTextPlainFormat[] = "text/plain";
static constexpr char kUnknownClipboardFormatError[] =
"Unknown clipboard format error";
static constexpr char kUnknownClipboardError[] =
"Unknown error during clipboard data retrieval";

void GetData(
const flutter::MethodCall<rapidjson::Document>& call,
std::unique_ptr<flutter::MethodResult<rapidjson::Document>> result) {
const rapidjson::Value& format = call.arguments()[0];

// https://api.flutter.dev/flutter/services/Clipboard/kTextPlain-constant.html
// API supports only kTextPlain format
if (strcmp(format.GetString(), kTextPlainFormat) != 0) {
result->Error(kUnknownClipboardFormatError,
"Clipboard API only supports text.");
return;
}

rapidjson::Document document;
document.SetObject();
rapidjson::Document::AllocatorType& allocator = document.GetAllocator();
document.AddMember(rapidjson::Value(kTextKey, allocator),
rapidjson::Value(string_clipboard, allocator),
allocator);
result->Success(document);
}

void SetData(
const flutter::MethodCall<rapidjson::Document>& call,
std::unique_ptr<flutter::MethodResult<rapidjson::Document>> result) {
const rapidjson::Value& document = *call.arguments();
rapidjson::Value::ConstMemberIterator itr = document.FindMember(kTextKey);
if (itr == document.MemberEnd()) {
result->Error(kUnknownClipboardError, "Invalid message format");
return;
}
string_clipboard = itr->value.GetString();
result->Success();
}
} // namespace Clipboard
9 changes: 9 additions & 0 deletions shell/platform/tizen/channels/platform_channel.h
Original file line number Diff line number Diff line change
Expand Up @@ -28,4 +28,13 @@ class PlatformChannel {
TizenRenderer* renderer_;
};

namespace Clipboard {
void GetData(
const flutter::MethodCall<rapidjson::Document>& call,
std::unique_ptr<flutter::MethodResult<rapidjson::Document>> result);
void SetData(
const flutter::MethodCall<rapidjson::Document>& call,
std::unique_ptr<flutter::MethodResult<rapidjson::Document>> result);
};

#endif // EMBEDDER_PLATFORM_CHANNEL_H_

0 comments on commit e320819

Please sign in to comment.