10 #include <shared_mutex>
13 #include "flutter/fml/logging.h"
14 #include "flutter/fml/paths.h"
15 #include "flutter/fml/platform/win/wstring_conversion.h"
16 #include "flutter/fml/synchronization/waitable_event.h"
20 #include "flutter/shell/platform/embedder/embedder_struct_macros.h"
30 #include "flutter/third_party/accessibility/ax/ax_node.h"
43 static std::chrono::nanoseconds SnapToNextTick(
44 std::chrono::nanoseconds value,
45 std::chrono::nanoseconds tick_phase,
46 std::chrono::nanoseconds tick_interval) {
47 std::chrono::nanoseconds offset = (tick_phase - value) % tick_interval;
48 if (offset != std::chrono::nanoseconds::zero())
49 offset = offset + tick_interval;
50 return value + offset;
57 FlutterRendererConfig GetOpenGLRendererConfig() {
58 FlutterRendererConfig config = {};
59 config.type = kOpenGL;
60 config.open_gl.struct_size =
sizeof(config.open_gl);
61 config.open_gl.make_current = [](
void*
user_data) ->
bool {
62 auto host =
static_cast<FlutterWindowsEngine*
>(
user_data);
63 if (!host->egl_manager()) {
66 return host->egl_manager()->render_context()->MakeCurrent();
68 config.open_gl.clear_current = [](
void*
user_data) ->
bool {
69 auto host =
static_cast<FlutterWindowsEngine*
>(
user_data);
70 if (!host->egl_manager()) {
73 return host->egl_manager()->render_context()->ClearCurrent();
75 config.open_gl.present = [](
void*
user_data) ->
bool { FML_UNREACHABLE(); };
76 config.open_gl.fbo_reset_after_present =
true;
77 config.open_gl.fbo_with_frame_info_callback =
78 [](
void*
user_data,
const FlutterFrameInfo* info) -> uint32_t {
81 config.open_gl.gl_proc_resolver = [](
void*
user_data,
82 const char* what) ->
void* {
83 return reinterpret_cast<void*
>(eglGetProcAddress(what));
85 config.open_gl.make_resource_current = [](
void*
user_data) ->
bool {
86 auto host =
static_cast<FlutterWindowsEngine*
>(
user_data);
87 if (!host->egl_manager()) {
90 return host->egl_manager()->resource_context()->MakeCurrent();
92 config.open_gl.gl_external_texture_frame_callback =
94 FlutterOpenGLTexture* texture) ->
bool {
95 auto host =
static_cast<FlutterWindowsEngine*
>(
user_data);
96 if (!host->texture_registrar()) {
99 return host->texture_registrar()->PopulateTexture(
texture_id, width, height,
109 FlutterRendererConfig GetSoftwareRendererConfig() {
110 FlutterRendererConfig config = {};
111 config.type = kSoftware;
112 config.software.struct_size =
sizeof(config.software);
113 config.software.surface_present_callback =
114 [](
void*
user_data,
const void* allocation,
size_t row_bytes,
124 const FlutterPlatformMessage& engine_message) {
127 message.channel = engine_message.channel;
128 message.message = engine_message.message;
129 message.message_size = engine_message.message_size;
130 message.response_handle = engine_message.response_handle;
136 FlutterLocale CovertToFlutterLocale(
const LanguageInfo& info) {
137 FlutterLocale locale = {};
138 locale.struct_size =
sizeof(FlutterLocale);
139 locale.language_code = info.language.c_str();
140 if (!info.region.empty()) {
141 locale.country_code = info.region.c_str();
143 if (!info.script.empty()) {
144 locale.script_code = info.script.c_str();
153 std::shared_ptr<WindowsProcTable> windows_proc_table)
155 windows_proc_table_(std::move(windows_proc_table)),
156 aot_data_(nullptr, nullptr),
158 if (windows_proc_table_ ==
nullptr) {
159 windows_proc_table_ = std::make_shared<WindowsProcTable>();
164 embedder_api_.struct_size =
sizeof(FlutterEngineProcTable);
165 FlutterEngineGetProcAddresses(&embedder_api_);
168 std::make_unique<TaskRunner>(
169 embedder_api_.GetCurrentTime, [
this](
const auto* task) {
172 <<
"Cannot post an engine task when engine is not running.";
175 if (embedder_api_.RunTask(engine_, task) != kSuccess) {
176 FML_LOG(ERROR) <<
"Failed to post an engine task.";
183 messenger_->SetEngine(
this);
184 plugin_registrar_ = std::make_unique<FlutterDesktopPluginRegistrar>();
185 plugin_registrar_->engine =
this;
188 std::make_unique<BinaryMessengerImpl>(messenger_->ToRef());
189 message_dispatcher_ =
190 std::make_unique<IncomingMessageDispatcher>(messenger_->ToRef());
193 std::make_unique<FlutterWindowsTextureRegistrar>(
this, gl_);
196 auto& switches = project_->GetSwitches();
197 bool enable_impeller =
true;
199 enable_impeller =
true;
201 enable_impeller =
false;
203 for (
const auto& env_switch : switches) {
204 if (env_switch ==
"--enable-impeller" ||
205 env_switch ==
"--enable-impeller=true") {
206 enable_impeller =
true;
207 }
else if (env_switch ==
"--enable-impeller=false") {
208 enable_impeller =
false;
211 enable_impeller_ = enable_impeller;
215 window_proc_delegate_manager_ = std::make_unique<WindowProcDelegateManager>();
217 display_manager_ = std::make_shared<DisplayManagerWin32>(
this);
219 window_proc_delegate_manager_->RegisterTopLevelWindowProcDelegate(
220 [](HWND hwnd, UINT msg, WPARAM wpar, LPARAM lpar,
void*
user_data,
223 FlutterWindowsEngine* that =
224 static_cast<FlutterWindowsEngine*
>(
user_data);
226 BASE_DCHECK(that->display_manager_);
227 if (that->display_manager_->HandleWindowMessage(hwnd, msg, wpar, lpar,
232 BASE_DCHECK(that->lifecycle_manager_);
234 that->lifecycle_manager_->WindowProc(hwnd, msg, wpar, lpar, result);
238 auto message_result =
239 that->window_manager_->HandleMessage(hwnd, msg, wpar, lpar);
240 if (message_result) {
241 *result = *message_result;
246 static_cast<void*
>(
this));
251 internal_plugin_registrar_ =
252 std::make_unique<PluginRegistrar>(plugin_registrar_.get());
254 accessibility_plugin_ = std::make_unique<AccessibilityPlugin>(
this);
256 accessibility_plugin_.get());
259 std::make_unique<CursorHandler>(messenger_wrapper_.get(),
this);
261 std::make_unique<PlatformHandler>(messenger_wrapper_.get(),
this);
262 window_manager_ = std::make_unique<WindowManager>(
this);
263 settings_plugin_ = std::make_unique<SettingsPlugin>(messenger_wrapper_.get(),
267 FlutterWindowsEngine::~FlutterWindowsEngine() {
268 messenger_->SetEngine(
nullptr);
276 void FlutterWindowsEngine::SetSwitches(
277 const std::vector<std::string>& switches) {
281 bool FlutterWindowsEngine::Run() {
285 bool FlutterWindowsEngine::Run(std::string_view entrypoint) {
286 if (!project_->HasValidPaths()) {
287 FML_LOG(ERROR) <<
"Missing or unresolvable paths to assets.";
290 std::string assets_path_string = fml::PathToUtf8(project_->assets_path());
291 std::string icu_path_string = fml::PathToUtf8(project_->icu_path());
292 if (embedder_api_.RunsAOTCompiledDartCode()) {
293 aot_data_ = project_->LoadAotData(embedder_api_);
295 FML_LOG(ERROR) <<
"Unable to start engine without AOT data.";
303 std::string executable_name = GetExecutableName();
304 std::vector<const char*> argv = {executable_name.c_str()};
305 std::vector<std::string> switches = project_->GetSwitches();
306 if (enable_impeller_) {
307 if (std::find(switches.begin(), switches.end(),
308 "--impeller-use-sdfs=true") == switches.end() &&
309 std::find(switches.begin(), switches.end(),
310 "--impeller-use-sdfs=false") == switches.end()) {
311 switches.push_back(
"--impeller-use-sdfs=true");
313 if (std::find(switches.begin(), switches.end(),
"--enable-impeller") ==
315 std::find(switches.begin(), switches.end(),
"--enable-impeller=true") ==
319 switches.push_back(
"--enable-impeller");
321 }
else if (project_->impeller_switch() == FlutterImpellerSwitch::Disabled) {
322 if (std::find(switches.begin(), switches.end(),
323 "--enable-impeller=false") == switches.end()) {
326 switches.push_back(
"--enable-impeller=false");
330 switches.begin(), switches.end(), std::back_inserter(argv),
331 [](
const std::string& arg) ->
const char* { return arg.c_str(); });
333 const std::vector<std::string>& entrypoint_args =
334 project_->dart_entrypoint_arguments();
335 std::vector<const char*> entrypoint_argv;
337 entrypoint_args.begin(), entrypoint_args.end(),
338 std::back_inserter(entrypoint_argv),
339 [](
const std::string& arg) ->
const char* { return arg.c_str(); });
342 FlutterTaskRunnerDescription platform_task_runner = {};
343 platform_task_runner.struct_size =
sizeof(FlutterTaskRunnerDescription);
344 platform_task_runner.user_data = task_runner_.get();
345 platform_task_runner.runs_task_on_current_thread_callback =
349 platform_task_runner.post_task_callback = [](FlutterTask task,
350 uint64_t target_time_nanos,
355 FlutterCustomTaskRunners custom_task_runners = {};
356 custom_task_runners.struct_size =
sizeof(FlutterCustomTaskRunners);
357 custom_task_runners.platform_task_runner = &platform_task_runner;
358 custom_task_runners.thread_priority_setter =
361 if (project_->ui_thread_policy() !=
363 custom_task_runners.ui_task_runner = &platform_task_runner;
365 FML_LOG(WARNING) <<
"Running with unmerged platform and UI threads. This "
366 "will be removed in future.";
369 FlutterProjectArgs args = {};
370 args.struct_size =
sizeof(FlutterProjectArgs);
371 args.shutdown_dart_vm_when_done =
true;
372 args.assets_path = assets_path_string.c_str();
373 args.icu_data_path = icu_path_string.c_str();
374 args.command_line_argc =
static_cast<int>(argv.size());
375 args.command_line_argv = argv.empty() ? nullptr : argv.data();
376 args.engine_id =
reinterpret_cast<int64_t
>(
this);
384 if (!project_->dart_entrypoint().empty() && !entrypoint.empty() &&
385 project_->dart_entrypoint() != entrypoint) {
386 FML_LOG(ERROR) <<
"Conflicting entrypoints were specified in "
387 "FlutterDesktopEngineProperties.dart_entrypoint and "
388 "FlutterDesktopEngineRun(engine, entry_point). ";
391 if (!entrypoint.empty()) {
392 args.custom_dart_entrypoint = entrypoint.data();
393 }
else if (!project_->dart_entrypoint().empty()) {
394 args.custom_dart_entrypoint = project_->dart_entrypoint().c_str();
396 args.dart_entrypoint_argc =
static_cast<int>(entrypoint_argv.size());
397 args.dart_entrypoint_argv =
398 entrypoint_argv.empty() ? nullptr : entrypoint_argv.data();
399 args.platform_message_callback =
400 [](
const FlutterPlatformMessage* engine_message,
405 args.vsync_callback = [](
void*
user_data, intptr_t baton) ->
void {
409 args.on_pre_engine_restart_callback = [](
void*
user_data) {
413 args.update_semantics_callback2 = [](
const FlutterSemanticsUpdate2* update,
417 auto view = host->
view(update->view_id);
423 if (!accessibility_bridge) {
427 for (
size_t i = 0; i < update->node_count; i++) {
428 const FlutterSemanticsNode2* node = update->nodes[i];
429 accessibility_bridge->AddFlutterSemanticsNodeUpdate(*node);
432 for (
size_t i = 0; i < update->custom_action_count; i++) {
433 const FlutterSemanticsCustomAction2*
action = update->custom_actions[i];
434 accessibility_bridge->AddFlutterSemanticsCustomActionUpdate(*
action);
437 accessibility_bridge->CommitUpdates();
439 args.root_isolate_create_callback = [](
void*
user_data) {
441 if (host->root_isolate_create_callback_) {
442 host->root_isolate_create_callback_();
445 args.channel_update_callback = [](
const FlutterChannelUpdate* update,
448 if (SAFE_ACCESS(update, channel,
nullptr) !=
nullptr) {
449 std::string channel_name(update->channel);
450 host->OnChannelUpdate(std::move(channel_name),
451 SAFE_ACCESS(update, listening,
false));
454 args.view_focus_change_request_callback =
455 [](
const FlutterViewFocusChangeRequest* request,
void*
user_data) {
460 args.custom_task_runners = &custom_task_runners;
462 if (!platform_view_plugin_) {
463 platform_view_plugin_ = std::make_unique<PlatformViewPlugin>(
464 messenger_wrapper_.get(), task_runner_.get());
467 auto resolver = [](
const char* name) ->
void* {
468 return reinterpret_cast<void*
>(::eglGetProcAddress(name));
474 std::make_unique<CompositorOpenGL>(
this, resolver, enable_impeller_);
476 compositor_ = std::make_unique<CompositorSoftware>();
479 FlutterCompositor compositor = {};
480 compositor.struct_size =
sizeof(FlutterCompositor);
481 compositor.user_data =
this;
482 compositor.create_backing_store_callback =
483 [](
const FlutterBackingStoreConfig* config,
484 FlutterBackingStore* backing_store_out,
void*
user_data) ->
bool {
487 return host->compositor_->CreateBackingStore(*config, backing_store_out);
490 compositor.collect_backing_store_callback =
491 [](
const FlutterBackingStore* backing_store,
void*
user_data) ->
bool {
494 return host->compositor_->CollectBackingStore(backing_store);
497 compositor.present_view_callback =
498 [](
const FlutterPresentViewInfo* info) ->
bool {
501 return host->Present(info);
503 args.compositor = &compositor;
506 args.aot_data = aot_data_.get();
511 FML_DCHECK(!egl_manager_ || !egl_manager_->HasContextCurrent());
513 FlutterRendererConfig renderer_config;
515 if (enable_impeller_) {
519 FML_LOG(ERROR) <<
"Could not create surface manager. Impeller backend "
520 "does not support software rendering.";
523 renderer_config = GetOpenGLRendererConfig();
526 egl_manager_ ? GetOpenGLRendererConfig() : GetSoftwareRendererConfig();
529 auto result = embedder_api_.Run(FLUTTER_ENGINE_VERSION, &renderer_config,
530 &args,
this, &engine_);
531 if (result != kSuccess || engine_ ==
nullptr) {
532 FML_LOG(ERROR) <<
"Failed to start Flutter engine: error " << result;
536 display_manager_->UpdateDisplays();
540 settings_plugin_->StartWatching();
541 settings_plugin_->SendSettings();
543 InitializeKeyboard();
548 bool FlutterWindowsEngine::Stop() {
550 window_manager_->OnEngineShutdown();
551 for (
const auto& [
callback, registrar] :
552 plugin_registrar_destruction_callbacks_) {
555 FlutterEngineResult result = embedder_api_.Shutdown(engine_);
557 return (result == kSuccess);
562 std::unique_ptr<FlutterWindowsView> FlutterWindowsEngine::CreateView(
563 std::unique_ptr<WindowBindingHandler> window,
564 bool is_sized_to_content,
565 const BoxConstraints& box_constraints,
567 auto view_id = next_view_id_;
568 auto view = std::make_unique<FlutterWindowsView>(
569 view_id,
this, std::move(window), is_sized_to_content, box_constraints,
570 sizing_delegate, windows_proc_table_);
572 view->CreateRenderSurface();
573 view->UpdateSemanticsEnabled(semantics_enabled_);
580 std::unique_lock write_lock(views_mutex_);
581 FML_DCHECK(views_.find(view_id) == views_.end());
582 views_[view_id] = view.get();
585 if (!view->IsImplicitView()) {
586 FML_DCHECK(running());
589 fml::AutoResetWaitableEvent latch;
592 Captures captures = {};
594 FlutterWindowMetricsEvent metrics = view->CreateWindowMetricsEvent();
596 FlutterAddViewInfo info = {};
597 info.struct_size =
sizeof(FlutterAddViewInfo);
598 info.view_id = view_id;
599 info.view_metrics = &metrics;
600 info.user_data = &captures;
601 info.add_view_callback = [](
const FlutterAddViewResult* result) {
602 Captures* captures =
reinterpret_cast<Captures*
>(result->user_data);
603 captures->added = result->added;
604 captures->latch.Signal();
607 FlutterEngineResult result = embedder_api_.AddView(engine_, &info);
608 if (result != kSuccess) {
610 <<
"Starting the add view operation failed. FlutterEngineAddView "
611 "returned an unexpected result: "
612 << result <<
". This indicates a bug in the Windows embedder.";
622 captures.latch.Wait();
624 if (!captures.added) {
628 FML_LOG(ERROR) <<
"FlutterEngineAddView failed to add view";
629 std::unique_lock write_lock(views_mutex_);
630 views_.erase(view_id);
635 return std::move(view);
639 FML_DCHECK(running());
646 fml::AutoResetWaitableEvent latch;
649 Captures captures = {};
651 FlutterRemoveViewInfo info = {};
652 info.struct_size =
sizeof(FlutterRemoveViewInfo);
653 info.view_id = view_id;
654 info.user_data = &captures;
655 info.remove_view_callback = [](
const FlutterRemoveViewResult* result) {
659 Captures* captures =
reinterpret_cast<Captures*
>(result->user_data);
660 captures->removed = result->removed;
661 captures->latch.Signal();
664 FlutterEngineResult result = embedder_api_.RemoveView(engine_, &info);
665 if (result != kSuccess) {
666 FML_LOG(ERROR) <<
"Starting the remove view operation failed. "
667 "FlutterEngineRemoveView "
668 "returned an unexpected result: "
670 <<
". This indicates a bug in the Windows embedder.";
680 captures.latch.Wait();
682 if (!captures.removed) {
685 FML_LOG(ERROR) <<
"FlutterEngineRemoveView failed to remove view";
693 std::unique_lock write_lock(views_mutex_);
695 FML_DCHECK(views_.find(view_id) != views_.end());
699 if (text_input_plugin_) {
700 text_input_plugin_->OnViewRemoved(view_id);
703 views_.erase(view_id);
707 void FlutterWindowsEngine::OnVsync(intptr_t baton) {
708 std::chrono::nanoseconds current_time =
709 std::chrono::nanoseconds(embedder_api_.GetCurrentTime());
710 std::chrono::nanoseconds frame_interval = FrameInterval();
711 auto next = SnapToNextTick(current_time, start_time_, frame_interval);
712 embedder_api_.OnVsync(engine_, baton, next.count(),
713 (next + frame_interval).count());
716 std::chrono::nanoseconds FlutterWindowsEngine::FrameInterval() {
717 if (frame_interval_override_.has_value()) {
718 return frame_interval_override_.value();
720 uint64_t interval = 16600000;
722 DWM_TIMING_INFO timing_info = {};
723 timing_info.cbSize =
sizeof(timing_info);
724 HRESULT result = DwmGetCompositionTimingInfo(NULL, &timing_info);
725 if (result == S_OK && timing_info.rateRefresh.uiDenominator > 0 &&
726 timing_info.rateRefresh.uiNumerator > 0) {
727 interval =
static_cast<double>(timing_info.rateRefresh.uiDenominator *
729 static_cast<double>(timing_info.rateRefresh.uiNumerator);
732 return std::chrono::nanoseconds(interval);
736 std::shared_lock read_lock(views_mutex_);
738 auto iterator = views_.find(view_id);
739 if (iterator == views_.end()) {
743 return iterator->second;
748 return plugin_registrar_.get();
751 void FlutterWindowsEngine::AddPluginRegistrarDestructionCallback(
754 plugin_registrar_destruction_callbacks_[
callback] = registrar;
757 void FlutterWindowsEngine::UpdateDisplay(
758 const std::vector<FlutterEngineDisplay>&
displays) {
760 embedder_api_.NotifyDisplayUpdate(engine_,
761 kFlutterEngineDisplaysUpdateTypeStartup,
766 void FlutterWindowsEngine::SendWindowMetricsEvent(
767 const FlutterWindowMetricsEvent& event) {
769 embedder_api_.SendWindowMetricsEvent(engine_, &event);
773 void FlutterWindowsEngine::SendPointerEvent(
const FlutterPointerEvent& event) {
775 embedder_api_.SendPointerEvent(engine_, &event, 1);
779 void FlutterWindowsEngine::SendKeyEvent(
const FlutterKeyEvent& event,
787 void FlutterWindowsEngine::SendViewFocusEvent(
788 const FlutterViewFocusEvent& event) {
790 embedder_api_.SendViewFocusEvent(engine_, &event);
794 bool FlutterWindowsEngine::SendPlatformMessage(
797 const size_t message_size,
800 FlutterPlatformMessageResponseHandle* response_handle =
nullptr;
801 if (reply !=
nullptr &&
user_data !=
nullptr) {
802 FlutterEngineResult result =
803 embedder_api_.PlatformMessageCreateResponseHandle(
804 engine_, reply,
user_data, &response_handle);
805 if (result != kSuccess) {
806 FML_LOG(ERROR) <<
"Failed to create response handle";
811 FlutterPlatformMessage platform_message = {
812 sizeof(FlutterPlatformMessage),
819 FlutterEngineResult message_result =
820 embedder_api_.SendPlatformMessage(engine_, &platform_message);
821 if (response_handle !=
nullptr) {
822 embedder_api_.PlatformMessageReleaseResponseHandle(engine_,
825 return message_result == kSuccess;
828 void FlutterWindowsEngine::SendPlatformMessageResponse(
831 size_t data_length) {
832 embedder_api_.SendPlatformMessageResponse(engine_, handle, data, data_length);
835 void FlutterWindowsEngine::HandlePlatformMessage(
836 const FlutterPlatformMessage* engine_message) {
837 if (engine_message->struct_size !=
sizeof(FlutterPlatformMessage)) {
838 FML_LOG(ERROR) <<
"Invalid message size received. Expected: "
839 <<
sizeof(FlutterPlatformMessage) <<
" but received "
840 << engine_message->struct_size;
844 auto message = ConvertToDesktopMessage(*engine_message);
846 message_dispatcher_->HandleMessage(
message, [
this] {}, [
this] {});
849 void FlutterWindowsEngine::ReloadSystemFonts() {
850 embedder_api_.ReloadSystemFonts(engine_);
853 void FlutterWindowsEngine::ScheduleFrame() {
854 embedder_api_.ScheduleFrame(engine_);
857 void FlutterWindowsEngine::SetNextFrameCallback(fml::closure
callback) {
858 next_frame_callback_ = std::move(
callback);
860 embedder_api_.SetNextFrameCallback(
868 self->task_runner_->PostTask(std::move(self->next_frame_callback_));
873 HCURSOR FlutterWindowsEngine::GetCursorByName(
874 const std::string& cursor_name)
const {
875 static auto* cursors =
new std::map<std::string, const wchar_t*>{
876 {
"allScroll", IDC_SIZEALL},
877 {
"basic", IDC_ARROW},
879 {
"forbidden", IDC_NO},
881 {
"move", IDC_SIZEALL},
884 {
"precise", IDC_CROSS},
885 {
"progress", IDC_APPSTARTING},
887 {
"resizeColumn", IDC_SIZEWE},
888 {
"resizeDown", IDC_SIZENS},
889 {
"resizeDownLeft", IDC_SIZENESW},
890 {
"resizeDownRight", IDC_SIZENWSE},
891 {
"resizeLeft", IDC_SIZEWE},
892 {
"resizeLeftRight", IDC_SIZEWE},
893 {
"resizeRight", IDC_SIZEWE},
894 {
"resizeRow", IDC_SIZENS},
895 {
"resizeUp", IDC_SIZENS},
896 {
"resizeUpDown", IDC_SIZENS},
897 {
"resizeUpLeft", IDC_SIZENWSE},
898 {
"resizeUpRight", IDC_SIZENESW},
899 {
"resizeUpLeftDownRight", IDC_SIZENWSE},
900 {
"resizeUpRightDownLeft", IDC_SIZENESW},
903 const wchar_t* idc_name = IDC_ARROW;
904 auto it = cursors->find(cursor_name);
905 if (it != cursors->end()) {
906 idc_name = it->second;
908 return windows_proc_table_->LoadCursor(
nullptr, idc_name);
913 std::shared_lock read_lock(views_mutex_);
914 auto const iterator =
915 std::find_if(views_.begin(), views_.end(), [hwnd](
auto const& pair) {
916 FlutterWindowsView* const view = pair.second;
917 return GetAncestor(view->GetWindowHandle(), GA_ROOT) == hwnd;
919 if (iterator != views_.end()) {
920 return iterator->second;
925 void FlutterWindowsEngine::SendSystemLocales() {
926 std::vector<LanguageInfo> languages =
928 std::vector<FlutterLocale> flutter_locales;
929 flutter_locales.reserve(languages.size());
930 for (
const auto& info : languages) {
931 flutter_locales.push_back(CovertToFlutterLocale(info));
934 std::vector<const FlutterLocale*> flutter_locale_list;
935 flutter_locale_list.reserve(flutter_locales.size());
936 std::transform(flutter_locales.begin(), flutter_locales.end(),
937 std::back_inserter(flutter_locale_list),
938 [](
const auto& arg) ->
const auto* { return &arg; });
939 embedder_api_.UpdateLocales(engine_, flutter_locale_list.data(),
940 flutter_locale_list.size());
943 void FlutterWindowsEngine::InitializeKeyboard() {
944 auto internal_plugin_messenger = internal_plugin_registrar_->messenger();
945 KeyboardKeyEmbedderHandler::GetKeyStateHandler get_key_state = GetKeyState;
946 KeyboardKeyEmbedderHandler::MapVirtualKeyToScanCode map_vk_to_scan =
947 [](UINT virtual_key,
bool extended) {
948 return MapVirtualKey(virtual_key,
949 extended ? MAPVK_VK_TO_VSC_EX : MAPVK_VK_TO_VSC);
951 keyboard_key_handler_ = std::move(CreateKeyboardKeyHandler(
952 internal_plugin_messenger, get_key_state, map_vk_to_scan));
954 std::move(CreateTextInputPlugin(internal_plugin_messenger));
957 std::unique_ptr<KeyboardHandlerBase>
958 FlutterWindowsEngine::CreateKeyboardKeyHandler(
962 auto keyboard_key_handler = std::make_unique<KeyboardKeyHandler>(messenger);
963 keyboard_key_handler->AddDelegate(
964 std::make_unique<KeyboardKeyEmbedderHandler>(
965 [
this](
const FlutterKeyEvent& event, FlutterKeyEventCallback
callback,
969 get_key_state, map_vk_to_scan));
970 keyboard_key_handler->AddDelegate(
971 std::make_unique<KeyboardKeyChannelHandler>(messenger));
972 keyboard_key_handler->InitKeyboardChannel();
973 return keyboard_key_handler;
976 std::unique_ptr<TextInputPlugin> FlutterWindowsEngine::CreateTextInputPlugin(
978 return std::make_unique<TextInputPlugin>(messenger,
this);
981 bool FlutterWindowsEngine::RegisterExternalTexture(int64_t
texture_id) {
982 return (embedder_api_.RegisterExternalTexture(engine_,
texture_id) ==
986 bool FlutterWindowsEngine::UnregisterExternalTexture(int64_t
texture_id) {
987 return (embedder_api_.UnregisterExternalTexture(engine_,
texture_id) ==
991 bool FlutterWindowsEngine::MarkExternalTextureFrameAvailable(
993 return (embedder_api_.MarkExternalTextureFrameAvailable(
997 bool FlutterWindowsEngine::PostRasterThreadTask(fml::closure
callback)
const {
1001 auto captures =
new Captures();
1002 captures->callback = std::move(
callback);
1003 if (embedder_api_.PostRenderThreadTask(
1006 auto captures = reinterpret_cast<Captures*>(opaque);
1007 captures->callback();
1010 captures) == kSuccess) {
1017 bool FlutterWindowsEngine::DispatchSemanticsAction(
1020 FlutterSemanticsAction
action,
1021 fml::MallocMapping data) {
1022 FlutterSendSemanticsActionInfo info{
1023 .struct_size =
sizeof(FlutterSendSemanticsActionInfo),
1027 .data = data.GetMapping(),
1028 .data_length = data.GetSize(),
1030 return (embedder_api_.SendSemanticsAction(engine_, &info));
1033 void FlutterWindowsEngine::UpdateSemanticsEnabled(
bool enabled) {
1034 if (engine_ && semantics_enabled_ != enabled) {
1035 std::shared_lock read_lock(views_mutex_);
1037 semantics_enabled_ = enabled;
1038 embedder_api_.UpdateSemanticsEnabled(engine_, enabled);
1039 for (
auto iterator = views_.begin(); iterator != views_.end(); iterator++) {
1040 iterator->second->UpdateSemanticsEnabled(enabled);
1045 void FlutterWindowsEngine::OnPreEngineRestart() {
1047 InitializeKeyboard();
1050 std::string FlutterWindowsEngine::GetExecutableName()
const {
1051 std::pair<bool, std::string> result = fml::paths::GetExecutablePath();
1053 const std::string& executable_path = result.second;
1054 size_t last_separator = executable_path.find_last_of(
"/\\");
1055 if (last_separator == std::string::npos ||
1056 last_separator == executable_path.size() - 1) {
1057 return executable_path;
1059 return executable_path.substr(last_separator + 1);
1064 void FlutterWindowsEngine::UpdateAccessibilityFeatures() {
1065 UpdateHighContrastMode();
1068 void FlutterWindowsEngine::UpdateHighContrastMode() {
1069 high_contrast_enabled_ = windows_proc_table_->GetHighContrastEnabled();
1071 SendAccessibilityFeatures();
1072 settings_plugin_->UpdateHighContrastMode(high_contrast_enabled_);
1075 void FlutterWindowsEngine::SendAccessibilityFeatures() {
1078 if (high_contrast_enabled_) {
1080 FlutterAccessibilityFeature::kFlutterAccessibilityFeatureHighContrast;
1083 embedder_api_.UpdateAccessibilityFeatures(
1084 engine_,
static_cast<FlutterAccessibilityFeature
>(flags));
1087 void FlutterWindowsEngine::RequestApplicationQuit(HWND hwnd,
1091 platform_handler_->RequestAppExit(hwnd, wparam, lparam, exit_type, 0);
1094 void FlutterWindowsEngine::OnQuit(std::optional<HWND> hwnd,
1095 std::optional<WPARAM> wparam,
1096 std::optional<LPARAM> lparam,
1098 lifecycle_manager_->Quit(hwnd, wparam, lparam, exit_code);
1101 void FlutterWindowsEngine::OnDwmCompositionChanged() {
1102 if (display_manager_) {
1103 display_manager_->UpdateDisplays();
1106 std::shared_lock read_lock(views_mutex_);
1107 for (
auto iterator = views_.begin(); iterator != views_.end(); iterator++) {
1108 iterator->second->OnDwmCompositionChanged();
1112 void FlutterWindowsEngine::OnWindowStateEvent(HWND hwnd,
1114 lifecycle_manager_->OnWindowStateEvent(hwnd, event);
1117 std::optional<LRESULT> FlutterWindowsEngine::ProcessExternalWindowMessage(
1122 if (lifecycle_manager_) {
1123 return lifecycle_manager_->ExternalWindowMessage(hwnd,
message, wparam,
1126 return std::nullopt;
1129 void FlutterWindowsEngine::UpdateFlutterCursor(
1130 const std::string& cursor_name)
const {
1131 SetFlutterCursor(GetCursorByName(cursor_name));
1134 void FlutterWindowsEngine::SetFlutterCursor(HCURSOR cursor)
const {
1135 windows_proc_table_->SetCursor(cursor);
1138 void FlutterWindowsEngine::OnChannelUpdate(std::string name,
bool listening) {
1139 if (name ==
"flutter/platform" && listening) {
1140 lifecycle_manager_->BeginProcessingExit();
1141 }
else if (name ==
"flutter/lifecycle" && listening) {
1142 lifecycle_manager_->BeginProcessingLifecycle();
1146 void FlutterWindowsEngine::OnViewFocusChangeRequest(
1147 const FlutterViewFocusChangeRequest* request) {
1148 std::shared_lock read_lock(views_mutex_);
1150 auto iterator = views_.find(request->view_id);
1151 if (iterator == views_.end()) {
1159 bool FlutterWindowsEngine::Present(
const FlutterPresentViewInfo* info) {
1163 std::shared_lock read_lock(views_mutex_);
1165 auto iterator = views_.find(info->view_id);
1166 if (iterator == views_.end()) {
1170 FlutterWindowsView* view = iterator->second;
1172 return compositor_->Present(view, info->layers, info->layers_count);
1175 bool FlutterWindowsEngine::HandleDisplayMonitorMessage(HWND hwnd,
1180 if (!display_manager_) {
1184 return display_manager_->HandleWindowMessage(hwnd,
message, wparam, lparam,
static void SetUp(BinaryMessenger *binary_messenger, AccessibilityPlugin *plugin)
FlutterWindowsEngine(const FlutterProjectBundle &project, std::shared_ptr< WindowsProcTable > windows_proc_table=nullptr)
FlutterWindowsView * view(FlutterViewId view_id) const
void OnPreEngineRestart()
virtual void OnViewFocusChangeRequest(const FlutterViewFocusChangeRequest *request)
void HandlePlatformMessage(const FlutterPlatformMessage *)
void OnVsync(intptr_t baton)
void SetSwitches(const std::vector< std::string > &switches)
std::weak_ptr< AccessibilityBridgeWindows > accessibility_bridge()
std::function< SHORT(UINT, bool)> MapVirtualKeyToScanCode
std::function< SHORT(int)> GetKeyStateHandler
static std::unique_ptr< Manager > Create(GpuPreference gpu_preference)
static std::shared_ptr< ProcTable > Create()
std::vector< FlutterEngineDisplay > * displays
void(* FlutterDesktopBinaryReply)(const uint8_t *data, size_t data_size, void *user_data)
struct _FlutterPlatformMessageResponseHandle FlutterDesktopMessageResponseHandle
void(* FlutterDesktopOnPluginRegistrarDestroyed)(FlutterDesktopPluginRegistrarRef)
static constexpr char kAccessibilityChannelName[]
FlutterDesktopBinaryReply callback
std::vector< LanguageInfo > GetPreferredLanguageInfo(const WindowsProcTable &windows_proc_table)
WindowStateEvent
An event representing a change in window state that may update the.
static void WindowsPlatformThreadPrioritySetter(FlutterThreadPriority priority)
constexpr FlutterViewId kImplicitViewId