9 #include "flutter/common/constants.h"
10 #include "flutter/fml/platform/win/wstring_conversion.h"
14 #include "flutter/third_party/accessibility/ax/platform/ax_platform_node_win.h"
21 constexpr std::chrono::milliseconds kWindowResizeTimeout{100};
29 bool SurfaceWillUpdate(
size_t cur_width,
32 size_t target_height) {
35 bool non_zero_target_dims = target_height > 0 && target_width > 0;
37 (cur_height != target_height) || (cur_width != target_width);
38 return non_zero_target_dims && not_same_size;
43 void UpdateVsync(
const FlutterWindowsEngine& engine,
44 egl::WindowSurface* surface,
46 egl::Manager* egl_manager = engine.egl_manager();
51 auto update_vsync = [egl_manager, surface, needs_vsync]() {
52 if (!surface || !surface->IsValid()) {
56 if (!surface->MakeCurrent()) {
57 FML_LOG(ERROR) <<
"Unable to make the render surface current to update "
62 if (!surface->SetVSyncEnabled(needs_vsync)) {
63 FML_LOG(ERROR) <<
"Unable to update the render surface's swap interval";
66 if (!egl_manager->render_context()->ClearCurrent()) {
67 FML_LOG(ERROR) <<
"Unable to clear current surface after updating "
77 if (engine.running()) {
78 engine.PostRasterThreadTask(update_vsync);
89 std::unique_ptr<WindowBindingHandler> window_binding,
90 std::shared_ptr<WindowsProcTable> windows_proc_table)
93 windows_proc_table_(std::move(windows_proc_table)) {
94 if (windows_proc_table_ ==
nullptr) {
95 windows_proc_table_ = std::make_shared<WindowsProcTable>();
99 binding_handler_ = std::move(window_binding);
100 binding_handler_->SetView(
this);
117 std::unique_lock<std::mutex> lock(resize_mutex_);
119 if (surface_ ==
nullptr || !surface_->IsValid()) {
123 if (resize_status_ != ResizeState::kResizeStarted) {
127 if (!ResizeRenderSurface(resize_target_height_, resize_target_width_)) {
133 resize_status_ = ResizeState::kFrameGenerated;
139 std::unique_lock<std::mutex> lock(resize_mutex_);
141 if (surface_ ==
nullptr || !surface_->IsValid()) {
145 if (resize_status_ != ResizeState::kResizeStarted) {
149 if (resize_target_width_ != width || resize_target_height_ != height) {
153 if (!ResizeRenderSurface(resize_target_width_, resize_target_height_)) {
159 resize_status_ = ResizeState::kFrameGenerated;
164 binding_handler_->UpdateFlutterCursor(cursor_name);
168 binding_handler_->SetFlutterCursor(cursor);
172 if (resize_status_ == ResizeState::kDone) {
180 std::unique_lock<std::mutex> lock(resize_mutex_);
183 SendWindowMetrics(width, height, binding_handler_->GetDpiScale());
187 if (!surface_ || !surface_->IsValid()) {
188 SendWindowMetrics(width, height, binding_handler_->GetDpiScale());
194 bool surface_will_update =
195 SurfaceWillUpdate(surface_->width(), surface_->height(), width, height);
196 if (!surface_will_update) {
197 SendWindowMetrics(width, height, binding_handler_->GetDpiScale());
201 resize_status_ = ResizeState::kResizeStarted;
202 resize_target_width_ = width;
203 resize_target_height_ = height;
205 SendWindowMetrics(width, height, binding_handler_->GetDpiScale());
210 return resize_cv_.wait_for(lock, kWindowResizeTimeout,
211 [&resize_status = resize_status_] {
212 return resize_status == ResizeState::kDone;
222 FlutterPointerDeviceKind device_kind,
224 int modifiers_state) {
226 SendPointerMove(x, y, GetOrCreatePointerState(device_kind, device_id));
232 FlutterPointerDeviceKind device_kind,
234 FlutterPointerMouseButtons flutter_button) {
235 if (flutter_button != 0) {
236 auto state = GetOrCreatePointerState(device_kind, device_id);
237 state->buttons |= flutter_button;
238 SendPointerDown(x, y, state);
245 FlutterPointerDeviceKind device_kind,
247 FlutterPointerMouseButtons flutter_button) {
248 if (flutter_button != 0) {
249 auto state = GetOrCreatePointerState(device_kind, device_id);
250 state->buttons &= ~flutter_button;
251 SendPointerUp(x, y, state);
257 FlutterPointerDeviceKind device_kind,
259 SendPointerLeave(x, y, GetOrCreatePointerState(device_kind, device_id));
264 SendPointerPanZoomStart(device_id, point.
x, point.
y);
272 SendPointerPanZoomUpdate(device_id, pan_x, pan_y, scale, rotation);
276 SendPointerPanZoomEnd(device_id);
307 SendComposeChange(
text, cursor_pos);
314 int scroll_offset_multiplier,
315 FlutterPointerDeviceKind device_kind,
317 SendScroll(x, y, delta_x, delta_y, scroll_offset_multiplier, device_kind,
323 SendScrollInertiaCancel(device_id, point.
x, point.
y);
331 if (!accessibility_bridge_) {
335 return accessibility_bridge_->GetChildOfAXFragmentRoot();
339 binding_handler_->OnCursorRectUpdated(rect);
343 binding_handler_->OnResetImeComposing();
347 void FlutterWindowsView::SendWindowMetrics(
size_t width,
349 double dpiScale)
const {
350 FlutterWindowMetricsEvent
event = {};
351 event.struct_size =
sizeof(event);
353 event.height = height;
354 event.pixel_ratio = dpiScale;
362 binding_handler_->GetDpiScale());
365 FlutterWindowsView::PointerState* FlutterWindowsView::GetOrCreatePointerState(
366 FlutterPointerDeviceKind device_kind,
371 int32_t pointer_id = (
static_cast<int32_t
>(device_kind) << 28) | device_id;
373 auto [it, added] = pointer_states_.try_emplace(pointer_id,
nullptr);
375 auto state = std::make_unique<PointerState>();
376 state->device_kind = device_kind;
377 state->pointer_id = pointer_id;
378 it->second = std::move(state);
381 return it->second.get();
386 void FlutterWindowsView::SetEventPhaseFromCursorButtonState(
387 FlutterPointerEvent* event_data,
388 const PointerState* state)
const {
391 if (state->buttons == 0) {
392 event_data->phase = state->flutter_state_is_down
393 ? FlutterPointerPhase::kUp
394 : FlutterPointerPhase::kHover;
396 event_data->phase = state->flutter_state_is_down
397 ? FlutterPointerPhase::kMove
398 : FlutterPointerPhase::kDown;
402 void FlutterWindowsView::SendPointerMove(
double x,
404 PointerState* state) {
405 FlutterPointerEvent
event = {};
409 SetEventPhaseFromCursorButtonState(&event, state);
410 SendPointerEventWithData(event, state);
413 void FlutterWindowsView::SendPointerDown(
double x,
415 PointerState* state) {
416 FlutterPointerEvent
event = {};
420 SetEventPhaseFromCursorButtonState(&event, state);
421 SendPointerEventWithData(event, state);
423 state->flutter_state_is_down =
true;
426 void FlutterWindowsView::SendPointerUp(
double x,
428 PointerState* state) {
429 FlutterPointerEvent
event = {};
433 SetEventPhaseFromCursorButtonState(&event, state);
434 SendPointerEventWithData(event, state);
435 if (event.phase == FlutterPointerPhase::kUp) {
436 state->flutter_state_is_down =
false;
440 void FlutterWindowsView::SendPointerLeave(
double x,
442 PointerState* state) {
443 FlutterPointerEvent
event = {};
446 event.phase = FlutterPointerPhase::kRemove;
447 SendPointerEventWithData(event, state);
450 void FlutterWindowsView::SendPointerPanZoomStart(int32_t device_id,
454 GetOrCreatePointerState(kFlutterPointerDeviceKindTrackpad, device_id);
455 state->pan_zoom_start_x = x;
456 state->pan_zoom_start_y = y;
457 FlutterPointerEvent
event = {};
460 event.phase = FlutterPointerPhase::kPanZoomStart;
461 SendPointerEventWithData(event, state);
464 void FlutterWindowsView::SendPointerPanZoomUpdate(int32_t device_id,
470 GetOrCreatePointerState(kFlutterPointerDeviceKindTrackpad, device_id);
471 FlutterPointerEvent
event = {};
472 event.x = state->pan_zoom_start_x;
473 event.y = state->pan_zoom_start_y;
477 event.rotation = rotation;
478 event.phase = FlutterPointerPhase::kPanZoomUpdate;
479 SendPointerEventWithData(event, state);
482 void FlutterWindowsView::SendPointerPanZoomEnd(int32_t device_id) {
484 GetOrCreatePointerState(kFlutterPointerDeviceKindTrackpad, device_id);
485 FlutterPointerEvent
event = {};
486 event.x = state->pan_zoom_start_x;
487 event.y = state->pan_zoom_start_y;
488 event.phase = FlutterPointerPhase::kPanZoomEnd;
489 SendPointerEventWithData(event, state);
492 void FlutterWindowsView::SendText(
const std::u16string&
text) {
496 void FlutterWindowsView::SendKey(
int key,
514 void FlutterWindowsView::SendComposeBegin() {
518 void FlutterWindowsView::SendComposeCommit() {
522 void FlutterWindowsView::SendComposeEnd() {
526 void FlutterWindowsView::SendComposeChange(
const std::u16string&
text,
531 void FlutterWindowsView::SendScroll(
double x,
535 int scroll_offset_multiplier,
536 FlutterPointerDeviceKind device_kind,
538 auto state = GetOrCreatePointerState(device_kind, device_id);
540 FlutterPointerEvent
event = {};
543 event.signal_kind = FlutterPointerSignalKind::kFlutterPointerSignalKindScroll;
544 event.scroll_delta_x = delta_x * scroll_offset_multiplier;
545 event.scroll_delta_y = delta_y * scroll_offset_multiplier;
546 SetEventPhaseFromCursorButtonState(&event, state);
547 SendPointerEventWithData(event, state);
550 void FlutterWindowsView::SendScrollInertiaCancel(int32_t device_id,
554 GetOrCreatePointerState(kFlutterPointerDeviceKindTrackpad, device_id);
556 FlutterPointerEvent
event = {};
560 FlutterPointerSignalKind::kFlutterPointerSignalKindScrollInertiaCancel;
561 SetEventPhaseFromCursorButtonState(&event, state);
562 SendPointerEventWithData(event, state);
565 void FlutterWindowsView::SendPointerEventWithData(
566 const FlutterPointerEvent& event_data,
567 PointerState* state) {
570 if (!state->flutter_state_is_added &&
571 event_data.phase != FlutterPointerPhase::kAdd) {
572 FlutterPointerEvent
event = {};
573 event.phase = FlutterPointerPhase::kAdd;
574 event.x = event_data.x;
575 event.y = event_data.y;
577 SendPointerEventWithData(event, state);
582 if (state->flutter_state_is_added &&
583 event_data.phase == FlutterPointerPhase::kAdd) {
587 FlutterPointerEvent
event = event_data;
588 event.device_kind = state->device_kind;
589 event.device = state->pointer_id;
590 event.buttons = state->buttons;
594 event.view_id = flutter::kFlutterImplicitViewId;
597 event.struct_size =
sizeof(event);
599 std::chrono::duration_cast<std::chrono::microseconds>(
600 std::chrono::high_resolution_clock::now().time_since_epoch())
605 if (event_data.phase == FlutterPointerPhase::kAdd) {
606 state->flutter_state_is_added =
true;
607 }
else if (event_data.phase == FlutterPointerPhase::kRemove) {
608 auto it = pointer_states_.find(state->pointer_id);
609 if (it != pointer_states_.end()) {
610 pointer_states_.erase(it);
617 std::unique_lock<std::mutex> lock(resize_mutex_);
619 switch (resize_status_) {
620 case ResizeState::kResizeStarted:
631 case ResizeState::kFrameGenerated: {
634 resize_status_ = ResizeState::kDone;
636 resize_cv_.notify_all();
640 windows_proc_table_->DwmFlush();
642 case ResizeState::kDone:
648 return binding_handler_->OnBitmapSurfaceCleared();
654 return binding_handler_->OnBitmapSurfaceUpdated(allocation, row_bytes,
663 FML_DCHECK(surface_ ==
nullptr);
670 UpdateVsync(*engine_, surface_.get(), NeedsVsync());
672 resize_target_width_ = bounds.
width;
673 resize_target_height_ = bounds.
height;
677 bool FlutterWindowsView::ResizeRenderSurface(
size_t width,
size_t height) {
678 FML_DCHECK(surface_ !=
nullptr);
681 if (width == surface_->width() && height == surface_->height()) {
685 auto const existing_vsync = surface_->vsync_enabled();
690 if (!surface_->Destroy()) {
691 FML_LOG(ERROR) <<
"View resize failed to destroy surface";
695 std::unique_ptr<egl::WindowSurface> resized_surface =
698 if (!resized_surface) {
699 FML_LOG(ERROR) <<
"View resize failed to create surface";
703 if (!resized_surface->MakeCurrent() ||
704 !resized_surface->SetVSyncEnabled(existing_vsync)) {
708 FML_LOG(ERROR) <<
"View resize failed to set vsync";
711 surface_ = std::move(resized_surface);
722 return surface_.get();
730 return binding_handler_->GetWindowHandle();
738 auto alert_delegate = binding_handler_->GetAlertDelegate();
739 if (!alert_delegate) {
742 alert_delegate->SetText(fml::WideStringToUtf16(
text));
743 ui::AXPlatformNodeWin* alert_node = binding_handler_->GetAlert();
748 ax::mojom::Event event) {
750 node->NotifyAccessibilityEvent(event);
755 return accessibility_bridge_.get();
759 return binding_handler_->GetAlert();
762 std::shared_ptr<AccessibilityBridgeWindows>
764 return std::make_shared<AccessibilityBridgeWindows>(
this);
768 if (semantics_enabled_ != enabled) {
769 semantics_enabled_ = enabled;
771 if (!semantics_enabled_ && accessibility_bridge_) {
772 accessibility_bridge_.reset();
773 }
else if (semantics_enabled_ && !accessibility_bridge_) {
780 UpdateVsync(*engine_, surface_.get(), NeedsVsync());
787 bool FlutterWindowsView::NeedsVsync()
const {
791 return !windows_proc_table_->DwmIsCompositionEnabled();