mirror of
https://github.com/flutter/flutter.git
synced 2026-02-20 02:29:02 +08:00
Fixes several bugs in the clipboard code, and makes some structural
improvements:
- Adds scoped wrappers for clipboard open/close and global lock/unlock,
to prevent missing cleanup, fixing at least one case where the lock
was not released.
- Adds the relevant window handle to the clipboard calls, since the docs
suggest that some operations won't work without one.
- Adds a missing clear step to setting the clipboard data.
- Switches from TEXT to UNICODETEXT to handle non-ASCII text correctly.
- To enable that, adds UTF-16/-8 conversion utilities built on the
Win32 APIs (rather than the deprecated std::codecvt functions, as
have been previously used in the engine).
- Fixes handling of getting data when the clipboard is empty, correctly
returning null.
- Passes more errors back through the method channel, with details, for
easier debugging of future issues.
Fixes https://github.com/flutter/flutter/issues/54226
56 lines
1.7 KiB
C++
56 lines
1.7 KiB
C++
// 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/windows/string_conversion.h"
|
|
|
|
#include <Windows.h>
|
|
|
|
namespace flutter {
|
|
|
|
std::string Utf8FromUtf16(const std::wstring& utf16_string) {
|
|
if (utf16_string.empty()) {
|
|
return std::string();
|
|
}
|
|
int target_length = ::WideCharToMultiByte(
|
|
CP_UTF8, WC_ERR_INVALID_CHARS, utf16_string.data(),
|
|
static_cast<int>(utf16_string.length()), nullptr, 0, nullptr, nullptr);
|
|
if (target_length == 0) {
|
|
return std::string();
|
|
}
|
|
std::string utf8_string;
|
|
utf8_string.resize(target_length);
|
|
int converted_length = ::WideCharToMultiByte(
|
|
CP_UTF8, WC_ERR_INVALID_CHARS, utf16_string.data(),
|
|
static_cast<int>(utf16_string.length()), utf8_string.data(),
|
|
target_length, nullptr, nullptr);
|
|
if (converted_length == 0) {
|
|
return std::string();
|
|
}
|
|
return utf8_string;
|
|
}
|
|
|
|
std::wstring Utf16FromUtf8(const std::string& utf8_string) {
|
|
if (utf8_string.empty()) {
|
|
return std::wstring();
|
|
}
|
|
int target_length =
|
|
::MultiByteToWideChar(CP_UTF8, MB_ERR_INVALID_CHARS, utf8_string.data(),
|
|
static_cast<int>(utf8_string.length()), nullptr, 0);
|
|
if (target_length == 0) {
|
|
return std::wstring();
|
|
}
|
|
std::wstring utf16_string;
|
|
utf16_string.resize(target_length);
|
|
int converted_length =
|
|
::MultiByteToWideChar(CP_UTF8, MB_ERR_INVALID_CHARS, utf8_string.data(),
|
|
static_cast<int>(utf8_string.length()),
|
|
utf16_string.data(), target_length);
|
|
if (converted_length == 0) {
|
|
return std::wstring();
|
|
}
|
|
return utf16_string;
|
|
}
|
|
|
|
} // namespace flutter
|