9 #include "flutter/fml/logging.h"
13 TaskRunnerWindow::TaskRunnerWindow() {
14 WNDCLASS window_class = RegisterWindowClass();
16 CreateWindowEx(0, window_class.lpszClassName, L
"", 0, 0, 0, 0, 0,
17 HWND_MESSAGE,
nullptr, window_class.hInstance,
nullptr);
20 SetWindowLongPtr(window_handle_, GWLP_USERDATA,
21 reinterpret_cast<LONG_PTR
>(
this));
23 auto error = GetLastError();
25 size_t size = FormatMessageW(
26 FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM |
27 FORMAT_MESSAGE_IGNORE_INSERTS,
28 NULL, error, MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),
29 reinterpret_cast<LPWSTR
>(&
message), 0, NULL);
37 DestroyWindow(window_handle_);
38 window_handle_ =
nullptr;
40 UnregisterClass(window_class_name_.c_str(),
nullptr);
44 static std::weak_ptr<TaskRunnerWindow> instance;
45 auto res = instance.lock();
55 if (!PostMessage(window_handle_, WM_NULL, 0, 0)) {
56 FML_LOG(ERROR) <<
"Failed to post message to main thread.";
61 delegates_.push_back(delegate);
62 SetTimer(std::chrono::nanoseconds::zero());
66 auto i = std::find(delegates_.begin(), delegates_.end(), delegate);
67 if (i != delegates_.end()) {
72 void TaskRunnerWindow::ProcessTasks() {
73 auto next = std::chrono::nanoseconds::max();
74 auto delegates_copy(delegates_);
75 for (
auto delegate : delegates_copy) {
77 if (std::find(delegates_.begin(), delegates_.end(), delegate) !=
79 next = std::min(next, delegate->ProcessTasks());
85 void TaskRunnerWindow::SetTimer(std::chrono::nanoseconds when) {
86 if (when == std::chrono::nanoseconds::max()) {
87 KillTimer(window_handle_, 0);
89 auto millis = std::chrono::duration_cast<std::chrono::milliseconds>(when);
90 ::SetTimer(window_handle_, 0, millis.count() + 1,
nullptr);
94 WNDCLASS TaskRunnerWindow::RegisterWindowClass() {
95 window_class_name_ = L
"FlutterTaskRunnerWindow";
97 WNDCLASS window_class{};
98 window_class.hCursor =
nullptr;
99 window_class.lpszClassName = window_class_name_.c_str();
100 window_class.style = 0;
101 window_class.cbClsExtra = 0;
102 window_class.cbWndExtra = 0;
103 window_class.hInstance = GetModuleHandle(
nullptr);
104 window_class.hIcon =
nullptr;
105 window_class.hbrBackground = 0;
106 window_class.lpszMenuName =
nullptr;
107 window_class.lpfnWndProc = WndProc;
108 RegisterClass(&window_class);
113 TaskRunnerWindow::HandleMessage(UINT
const message,
115 LPARAM
const lparam) noexcept {
122 return DefWindowProcW(window_handle_,
message, wparam, lparam);
125 LRESULT TaskRunnerWindow::WndProc(HWND
const window,
128 LPARAM
const lparam) noexcept {
129 if (
auto* that =
reinterpret_cast<TaskRunnerWindow*
>(
130 GetWindowLongPtr(window, GWLP_USERDATA))) {
131 return that->HandleMessage(
message, wparam, lparam);
133 return DefWindowProc(window,
message, wparam, lparam);