Skip to content

Commit

Permalink
Fix setString on Windows by dispatching to UI thread (#174)
Browse files Browse the repository at this point in the history
  • Loading branch information
chrisglein authored Jun 20, 2023
1 parent e0991ca commit 12bb50a
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 4 deletions.
10 changes: 6 additions & 4 deletions windows/Clipboard/Clipboard.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ namespace NativeClipboard {
if (dataPackageView.Contains(datatransfer::StandardDataFormats::Text())) {
dataPackageView.GetTextAsync().Completed([promise, dataPackageView](IAsyncOperation<winrt::hstring> info, AsyncStatus status) {
if (status == AsyncStatus::Completed) {
auto text = Microsoft::Common::Unicode::Utf16ToUtf8(info.GetResults());
auto text = winrt::to_string(info.GetResults());
promise.Resolve(text);
}
else {
Expand All @@ -22,8 +22,10 @@ namespace NativeClipboard {

void ClipboardModule::SetString(std::string const& str) noexcept
{
datatransfer::DataPackage dataPackage{};
dataPackage.SetText(Microsoft::Common::Unicode::Utf8ToUtf16(str));
datatransfer::Clipboard::SetContent(dataPackage);
_context.UIDispatcher().Post([str](){
datatransfer::DataPackage dataPackage{};
dataPackage.SetText(winrt::to_hstring(str));
datatransfer::Clipboard::SetContent(dataPackage);
});
}
}
8 changes: 8 additions & 0 deletions windows/Clipboard/Clipboard.h
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,19 @@ namespace NativeClipboard {
REACT_MODULE(ClipboardModule, L"RNCClipboard");
struct ClipboardModule
{
REACT_INIT(Initialize);
void Initialize(const winrt::Microsoft::ReactNative::ReactContext& reactContext) noexcept
{
_context = reactContext;
}

REACT_METHOD(GetString, L"getString");
void GetString(React::ReactPromise<std::string>&& result) noexcept;

REACT_METHOD(SetString, L"setString");
void SetString(std::string const& str) noexcept;

private:
ReactContext _context;
};
}

0 comments on commit 12bb50a

Please sign in to comment.