forked from flutter/engine
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[glfw] Implement clipboard support from GLFW api (flutter#9361)
- Loading branch information
1 parent
66db3d5
commit 0ca0a41
Showing
5 changed files
with
128 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,80 @@ | ||
// Copyright 2013 The Flutter Authors. All rights reserved. | ||
// Use of this source code is governed by a BSD-style license that can be | ||
// found in the LICENSE file. | ||
|
||
#include "flutter/shell/platform/glfw/platform_handler.h" | ||
|
||
#include <iostream> | ||
|
||
#include "flutter/shell/platform/common/cpp/client_wrapper/include/flutter/json_method_codec.h" | ||
|
||
static constexpr char kChannelName[] = "flutter/platform"; | ||
|
||
static constexpr char kGetClipboardDataMethod[] = "Clipboard.getData"; | ||
static constexpr char kSetClipboardDataMethod[] = "Clipboard.setData"; | ||
|
||
static constexpr char kTextPlainFormat[] = "text/plain"; | ||
static constexpr char kTextKey[] = "text"; | ||
|
||
static constexpr char kUnknownClipboardFormatError[] = | ||
"Unknown clipboard format error"; | ||
|
||
namespace flutter { | ||
|
||
PlatformHandler::PlatformHandler(flutter::BinaryMessenger* messenger, | ||
GLFWwindow* window) | ||
: channel_(std::make_unique<flutter::MethodChannel<rapidjson::Document>>( | ||
messenger, | ||
kChannelName, | ||
&flutter::JsonMethodCodec::GetInstance())), | ||
window_(window) { | ||
channel_->SetMethodCallHandler( | ||
[this]( | ||
const flutter::MethodCall<rapidjson::Document>& call, | ||
std::unique_ptr<flutter::MethodResult<rapidjson::Document>> result) { | ||
HandleMethodCall(call, std::move(result)); | ||
}); | ||
} | ||
|
||
void PlatformHandler::HandleMethodCall( | ||
const flutter::MethodCall<rapidjson::Document>& method_call, | ||
std::unique_ptr<flutter::MethodResult<rapidjson::Document>> result) { | ||
const std::string& method = method_call.method_name(); | ||
|
||
if (method.compare(kGetClipboardDataMethod) == 0) { | ||
// Only one string argument is expected. | ||
const rapidjson::Value& format = method_call.arguments()[0]; | ||
|
||
if (strcmp(format.GetString(), kTextPlainFormat) != 0) { | ||
result->Error(kUnknownClipboardFormatError, | ||
"GLFW clipboard API only supports text."); | ||
return; | ||
} | ||
|
||
const char* clipboardData = glfwGetClipboardString(window_); | ||
if (clipboardData == nullptr) { | ||
result->Error(kUnknownClipboardFormatError, | ||
"Failed to retrieve clipboard data from GLFW api."); | ||
return; | ||
} | ||
rapidjson::Document document; | ||
document.SetObject(); | ||
rapidjson::Document::AllocatorType& allocator = document.GetAllocator(); | ||
document.AddMember(rapidjson::Value(kTextKey, allocator), | ||
rapidjson::Value(clipboardData, allocator), allocator); | ||
result->Success(&document); | ||
} else if (method.compare(kSetClipboardDataMethod) == 0) { | ||
const rapidjson::Value& document = *method_call.arguments(); | ||
rapidjson::Value::ConstMemberIterator itr = document.FindMember(kTextKey); | ||
if (itr == document.MemberEnd()) { | ||
result->Error(kUnknownClipboardFormatError, | ||
"Missing text to store on clipboard."); | ||
return; | ||
} | ||
glfwSetClipboardString(window_, itr->value.GetString()); | ||
result->Success(); | ||
} else { | ||
result->NotImplemented(); | ||
} | ||
} | ||
} // namespace flutter |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
// Copyright 2013 The Flutter Authors. All rights reserved. | ||
// Use of this source code is governed by a BSD-style license that can be | ||
// found in the LICENSE file. | ||
|
||
#ifndef FLUTTER_SHELL_PLATFORM_GLFW_PLATFORM_HANDLER_H_ | ||
#define FLUTTER_SHELL_PLATFORM_GLFW_PLATFORM_HANDLER_H_ | ||
|
||
#include <GLFW/glfw3.h> | ||
|
||
#include "flutter/shell/platform/common/cpp/client_wrapper/include/flutter/binary_messenger.h" | ||
#include "flutter/shell/platform/common/cpp/client_wrapper/include/flutter/method_channel.h" | ||
#include "flutter/shell/platform/glfw/public/flutter_glfw.h" | ||
#include "rapidjson/document.h" | ||
|
||
namespace flutter { | ||
|
||
// Handler for internal system channels. | ||
class PlatformHandler { | ||
public: | ||
explicit PlatformHandler(flutter::BinaryMessenger* messenger, | ||
GLFWwindow* window); | ||
|
||
private: | ||
// Called when a method is called on |channel_|; | ||
void HandleMethodCall( | ||
const flutter::MethodCall<rapidjson::Document>& method_call, | ||
std::unique_ptr<flutter::MethodResult<rapidjson::Document>> result); | ||
|
||
// The MethodChannel used for communication with the Flutter engine. | ||
std::unique_ptr<flutter::MethodChannel<rapidjson::Document>> channel_; | ||
|
||
// A reference to the GLFW window. | ||
GLFWwindow* window_; | ||
}; | ||
|
||
} // namespace flutter | ||
|
||
#endif // FLUTTER_SHELL_PLATFORM_GLFW_PLATFORM_HANDLER_H_ |