@@ -199,14 +199,14 @@ int ScopedClipboard::SetString(const std::wstring string) {
199199
200200PlatformHandler::PlatformHandler (
201201 BinaryMessenger* messenger,
202- FlutterWindowsView* view ,
202+ FlutterWindowsEngine* engine ,
203203 std::optional<std::function<std::unique_ptr<ScopedClipboardInterface>()>>
204204 scoped_clipboard_provider)
205205 : channel_(std::make_unique<MethodChannel<rapidjson::Document>>(
206206 messenger,
207207 kChannelName ,
208208 &JsonMethodCodec::GetInstance ())),
209- view_(view ) {
209+ engine_(engine ) {
210210 channel_->SetMethodCallHandler (
211211 [this ](const MethodCall<rapidjson::Document>& call,
212212 std::unique_ptr<MethodResult<rapidjson::Document>> result) {
@@ -226,10 +226,17 @@ PlatformHandler::~PlatformHandler() = default;
226226void PlatformHandler::GetPlainText (
227227 std::unique_ptr<MethodResult<rapidjson::Document>> result,
228228 std::string_view key) {
229+ const FlutterWindowsView* view = engine_->view ();
230+ if (view == nullptr ) {
231+ result->Error (kClipboardError ,
232+ " Clipboard is not available in Windows headless mode" );
233+ return ;
234+ }
235+
229236 std::unique_ptr<ScopedClipboardInterface> clipboard =
230237 scoped_clipboard_provider_ ();
231238
232- int open_result = clipboard->Open (std::get<HWND>(*view_ ->GetRenderTarget ()));
239+ int open_result = clipboard->Open (std::get<HWND>(*view ->GetRenderTarget ()));
233240 if (open_result != kErrorSuccess ) {
234241 rapidjson::Document error_code;
235242 error_code.SetInt (open_result);
@@ -262,11 +269,18 @@ void PlatformHandler::GetPlainText(
262269
263270void PlatformHandler::GetHasStrings (
264271 std::unique_ptr<MethodResult<rapidjson::Document>> result) {
272+ const FlutterWindowsView* view = engine_->view ();
273+ if (view == nullptr ) {
274+ result->Error (kClipboardError ,
275+ " Clipboard is not available in Windows headless mode" );
276+ return ;
277+ }
278+
265279 std::unique_ptr<ScopedClipboardInterface> clipboard =
266280 scoped_clipboard_provider_ ();
267281
268282 bool hasStrings;
269- int open_result = clipboard->Open (std::get<HWND>(*view_ ->GetRenderTarget ()));
283+ int open_result = clipboard->Open (std::get<HWND>(*view ->GetRenderTarget ()));
270284 if (open_result != kErrorSuccess ) {
271285 // Swallow errors of type ERROR_ACCESS_DENIED. These happen when the app is
272286 // not in the foreground and GetHasStrings is irrelevant.
@@ -293,10 +307,17 @@ void PlatformHandler::GetHasStrings(
293307void PlatformHandler::SetPlainText (
294308 const std::string& text,
295309 std::unique_ptr<MethodResult<rapidjson::Document>> result) {
310+ const FlutterWindowsView* view = engine_->view ();
311+ if (view == nullptr ) {
312+ result->Error (kClipboardError ,
313+ " Clipboard is not available in Windows headless mode" );
314+ return ;
315+ }
316+
296317 std::unique_ptr<ScopedClipboardInterface> clipboard =
297318 scoped_clipboard_provider_ ();
298319
299- int open_result = clipboard->Open (std::get<HWND>(*view_ ->GetRenderTarget ()));
320+ int open_result = clipboard->Open (std::get<HWND>(*view ->GetRenderTarget ()));
300321 if (open_result != kErrorSuccess ) {
301322 rapidjson::Document error_code;
302323 error_code.SetInt (open_result);
0 commit comments