9 #include "flutter/common/constants.h"
10 #include "flutter/fml/make_copyable.h"
11 #include "flutter/fml/platform/win/wstring_conversion.h"
12 #include "flutter/fml/synchronization/waitable_event.h"
16 #include "flutter/third_party/accessibility/ax/platform/ax_platform_node_win.h"
23 constexpr std::chrono::milliseconds kWindowResizeTimeout{100};
31 bool SurfaceWillUpdate(
size_t cur_width,
34 size_t target_height) {
37 bool non_zero_target_dims = target_height > 0 && target_width > 0;
39 (cur_height != target_height) || (cur_width != target_width);
40 return non_zero_target_dims && not_same_size;
45 void UpdateVsync(
const FlutterWindowsEngine& engine,
46 egl::WindowSurface* surface,
48 egl::Manager* egl_manager = engine.egl_manager();
53 auto update_vsync = [egl_manager, surface, needs_vsync]() {
54 if (!surface || !surface->IsValid()) {
58 if (!surface->MakeCurrent()) {
59 FML_LOG(ERROR) <<
"Unable to make the render surface current to update "
64 if (!surface->SetVSyncEnabled(needs_vsync)) {
65 FML_LOG(ERROR) <<
"Unable to update the render surface's swap interval";
68 if (!egl_manager->render_context()->ClearCurrent()) {
69 FML_LOG(ERROR) <<
"Unable to clear current surface after updating "
79 if (engine.running()) {
80 engine.PostRasterThreadTask(update_vsync);
87 void DestroyWindowSurface(
const FlutterWindowsEngine& engine,
88 std::unique_ptr<egl::WindowSurface> surface) {
92 if (engine.running()) {
93 engine.PostRasterThreadTask(fml::MakeCopyable(
94 [surface = std::move(surface)] { surface->Destroy(); }));
107 std::unique_ptr<WindowBindingHandler> window_binding,
108 std::shared_ptr<WindowsProcTable> windows_proc_table)
111 windows_proc_table_(std::move(windows_proc_table)) {
112 if (windows_proc_table_ ==
nullptr) {
113 windows_proc_table_ = std::make_shared<WindowsProcTable>();
117 binding_handler_ = std::move(window_binding);
118 binding_handler_->SetView(
this);
127 DestroyWindowSurface(*engine_, std::move(surface_));
133 std::unique_lock<std::mutex> lock(resize_mutex_);
135 if (surface_ ==
nullptr || !surface_->IsValid()) {
139 if (resize_status_ != ResizeState::kResizeStarted) {
143 if (!ResizeRenderSurface(resize_target_height_, resize_target_width_)) {
149 resize_status_ = ResizeState::kFrameGenerated;
155 std::unique_lock<std::mutex> lock(resize_mutex_);
157 if (surface_ ==
nullptr || !surface_->IsValid()) {
161 if (resize_status_ != ResizeState::kResizeStarted) {
165 if (resize_target_width_ != width || resize_target_height_ != height) {
169 if (!ResizeRenderSurface(resize_target_width_, resize_target_height_)) {
175 resize_status_ = ResizeState::kFrameGenerated;
180 if (resize_status_ == ResizeState::kDone) {
189 SendWindowMetrics(width, height, binding_handler_->GetDpiScale());
193 if (!surface_ || !surface_->IsValid()) {
194 SendWindowMetrics(width, height, binding_handler_->GetDpiScale());
200 bool surface_will_update =
201 SurfaceWillUpdate(surface_->width(), surface_->height(), width, height);
202 if (!surface_will_update) {
203 SendWindowMetrics(width, height, binding_handler_->GetDpiScale());
208 std::unique_lock<std::mutex> lock(resize_mutex_);
209 resize_status_ = ResizeState::kResizeStarted;
210 resize_target_width_ = width;
211 resize_target_height_ = height;
214 SendWindowMetrics(width, height, binding_handler_->GetDpiScale());
216 std::chrono::time_point<std::chrono::steady_clock> start_time =
217 std::chrono::steady_clock::now();
220 if (std::chrono::steady_clock::now() > start_time + kWindowResizeTimeout) {
223 std::unique_lock<std::mutex> lock(resize_mutex_);
224 if (resize_status_ == ResizeState::kDone) {
239 FlutterPointerDeviceKind device_kind,
241 int modifiers_state) {
243 SendPointerMove(x, y, GetOrCreatePointerState(device_kind, device_id));
249 FlutterPointerDeviceKind device_kind,
251 FlutterPointerMouseButtons flutter_button) {
252 if (flutter_button != 0) {
253 auto state = GetOrCreatePointerState(device_kind, device_id);
254 state->buttons |= flutter_button;
255 SendPointerDown(x, y, state);
262 FlutterPointerDeviceKind device_kind,
264 FlutterPointerMouseButtons flutter_button) {
265 if (flutter_button != 0) {
266 auto state = GetOrCreatePointerState(device_kind, device_id);
267 state->buttons &= ~flutter_button;
268 SendPointerUp(x, y, state);
274 FlutterPointerDeviceKind device_kind,
276 SendPointerLeave(x, y, GetOrCreatePointerState(device_kind, device_id));
281 SendPointerPanZoomStart(device_id, point.
x, point.
y);
289 SendPointerPanZoomUpdate(device_id, pan_x, pan_y, scale, rotation);
293 SendPointerPanZoomEnd(device_id);
311 FlutterViewFocusDirection direction) {
312 SendFocus(focus_state, direction);
329 SendComposeChange(
text, cursor_pos);
336 int scroll_offset_multiplier,
337 FlutterPointerDeviceKind device_kind,
339 SendScroll(x, y, delta_x, delta_y, scroll_offset_multiplier, device_kind,
345 SendScrollInertiaCancel(device_id, point.
x, point.
y);
353 if (!accessibility_bridge_) {
357 return accessibility_bridge_->GetChildOfAXFragmentRoot();
361 binding_handler_->OnCursorRectUpdated(rect);
365 binding_handler_->OnResetImeComposing();
369 void FlutterWindowsView::SendWindowMetrics(
size_t width,
371 double pixel_ratio)
const {
372 FlutterWindowMetricsEvent
event = {};
373 event.struct_size =
sizeof(event);
375 event.height = height;
376 event.pixel_ratio = pixel_ratio;
377 event.view_id = view_id_;
383 double pixel_ratio = binding_handler_->GetDpiScale();
385 FlutterWindowMetricsEvent
event = {};
386 event.struct_size =
sizeof(event);
387 event.width = bounds.
width;
388 event.height = bounds.
height;
389 event.pixel_ratio = pixel_ratio;
390 event.view_id = view_id_;
405 FlutterWindowsView::PointerState* FlutterWindowsView::GetOrCreatePointerState(
406 FlutterPointerDeviceKind device_kind,
411 int32_t pointer_id = (
static_cast<int32_t
>(device_kind) << 28) | device_id;
413 auto [it, added] = pointer_states_.try_emplace(pointer_id,
nullptr);
415 auto state = std::make_unique<PointerState>();
416 state->device_kind = device_kind;
417 state->pointer_id = pointer_id;
418 it->second = std::move(state);
421 return it->second.get();
426 void FlutterWindowsView::SetEventPhaseFromCursorButtonState(
427 FlutterPointerEvent* event_data,
428 const PointerState* state)
const {
431 if (state->buttons == 0) {
432 event_data->phase = state->flutter_state_is_down
433 ? FlutterPointerPhase::kUp
434 : FlutterPointerPhase::kHover;
436 event_data->phase = state->flutter_state_is_down
437 ? FlutterPointerPhase::kMove
438 : FlutterPointerPhase::kDown;
442 void FlutterWindowsView::SendPointerMove(
double x,
444 PointerState* state) {
445 FlutterPointerEvent
event = {};
449 SetEventPhaseFromCursorButtonState(&event, state);
450 SendPointerEventWithData(event, state);
453 void FlutterWindowsView::SendPointerDown(
double x,
455 PointerState* state) {
456 FlutterPointerEvent
event = {};
460 SetEventPhaseFromCursorButtonState(&event, state);
461 SendPointerEventWithData(event, state);
463 state->flutter_state_is_down =
true;
466 void FlutterWindowsView::SendPointerUp(
double x,
468 PointerState* state) {
469 FlutterPointerEvent
event = {};
473 SetEventPhaseFromCursorButtonState(&event, state);
474 SendPointerEventWithData(event, state);
475 if (event.phase == FlutterPointerPhase::kUp) {
476 state->flutter_state_is_down =
false;
480 void FlutterWindowsView::SendPointerLeave(
double x,
482 PointerState* state) {
483 FlutterPointerEvent
event = {};
486 event.phase = FlutterPointerPhase::kRemove;
487 SendPointerEventWithData(event, state);
490 void FlutterWindowsView::SendPointerPanZoomStart(int32_t device_id,
494 GetOrCreatePointerState(kFlutterPointerDeviceKindTrackpad, device_id);
495 state->pan_zoom_start_x = x;
496 state->pan_zoom_start_y = y;
497 FlutterPointerEvent
event = {};
500 event.phase = FlutterPointerPhase::kPanZoomStart;
501 SendPointerEventWithData(event, state);
504 void FlutterWindowsView::SendPointerPanZoomUpdate(int32_t device_id,
510 GetOrCreatePointerState(kFlutterPointerDeviceKindTrackpad, device_id);
511 FlutterPointerEvent
event = {};
512 event.x = state->pan_zoom_start_x;
513 event.y = state->pan_zoom_start_y;
517 event.rotation = rotation;
518 event.phase = FlutterPointerPhase::kPanZoomUpdate;
519 SendPointerEventWithData(event, state);
522 void FlutterWindowsView::SendPointerPanZoomEnd(int32_t device_id) {
524 GetOrCreatePointerState(kFlutterPointerDeviceKindTrackpad, device_id);
525 FlutterPointerEvent
event = {};
526 event.x = state->pan_zoom_start_x;
527 event.y = state->pan_zoom_start_y;
528 event.phase = FlutterPointerPhase::kPanZoomEnd;
529 SendPointerEventWithData(event, state);
532 void FlutterWindowsView::SendText(
const std::u16string&
text) {
536 void FlutterWindowsView::SendKey(
int key,
548 engine->text_input_plugin()->KeyboardHook(
557 void FlutterWindowsView::SendFocus(FlutterViewFocusState focus_state,
558 FlutterViewFocusDirection direction) {
559 FlutterViewFocusEvent
event = {};
560 event.struct_size =
sizeof(event);
561 event.view_id = view_id_;
562 event.state = focus_state;
563 event.direction = direction;
567 void FlutterWindowsView::SendComposeBegin() {
571 void FlutterWindowsView::SendComposeCommit() {
575 void FlutterWindowsView::SendComposeEnd() {
579 void FlutterWindowsView::SendComposeChange(
const std::u16string&
text,
584 void FlutterWindowsView::SendScroll(
double x,
588 int scroll_offset_multiplier,
589 FlutterPointerDeviceKind device_kind,
591 auto state = GetOrCreatePointerState(device_kind, device_id);
593 FlutterPointerEvent
event = {};
596 event.signal_kind = FlutterPointerSignalKind::kFlutterPointerSignalKindScroll;
597 event.scroll_delta_x = delta_x * scroll_offset_multiplier;
598 event.scroll_delta_y = delta_y * scroll_offset_multiplier;
599 SetEventPhaseFromCursorButtonState(&event, state);
600 SendPointerEventWithData(event, state);
603 void FlutterWindowsView::SendScrollInertiaCancel(int32_t device_id,
607 GetOrCreatePointerState(kFlutterPointerDeviceKindTrackpad, device_id);
609 FlutterPointerEvent
event = {};
613 FlutterPointerSignalKind::kFlutterPointerSignalKindScrollInertiaCancel;
614 SetEventPhaseFromCursorButtonState(&event, state);
615 SendPointerEventWithData(event, state);
618 void FlutterWindowsView::SendPointerEventWithData(
619 const FlutterPointerEvent& event_data,
620 PointerState* state) {
623 if (!state->flutter_state_is_added &&
624 event_data.phase != FlutterPointerPhase::kAdd) {
625 FlutterPointerEvent
event = {};
626 event.phase = FlutterPointerPhase::kAdd;
627 event.x = event_data.x;
628 event.y = event_data.y;
630 SendPointerEventWithData(event, state);
635 if (state->flutter_state_is_added &&
636 event_data.phase == FlutterPointerPhase::kAdd) {
640 FlutterPointerEvent
event = event_data;
641 event.device_kind = state->device_kind;
642 event.device = state->pointer_id;
643 event.buttons = state->buttons;
644 event.view_id = view_id_;
647 event.struct_size =
sizeof(event);
649 std::chrono::duration_cast<std::chrono::microseconds>(
650 std::chrono::high_resolution_clock::now().time_since_epoch())
655 if (event_data.phase == FlutterPointerPhase::kAdd) {
656 state->flutter_state_is_added =
true;
657 }
else if (event_data.phase == FlutterPointerPhase::kRemove) {
658 auto it = pointer_states_.find(state->pointer_id);
659 if (it != pointer_states_.end()) {
660 pointer_states_.erase(it);
667 std::unique_lock<std::mutex> lock(resize_mutex_);
669 switch (resize_status_) {
670 case ResizeState::kResizeStarted:
681 case ResizeState::kFrameGenerated: {
683 resize_status_ = ResizeState::kDone;
691 windows_proc_table_->DwmFlush();
693 case ResizeState::kDone:
699 return binding_handler_->OnBitmapSurfaceCleared();
705 return binding_handler_->OnBitmapSurfaceUpdated(allocation, row_bytes,
718 FML_DCHECK(surface_ ==
nullptr);
725 UpdateVsync(*engine_, surface_.get(), NeedsVsync());
727 resize_target_width_ = bounds.
width;
728 resize_target_height_ = bounds.
height;
732 bool FlutterWindowsView::ResizeRenderSurface(
size_t width,
size_t height) {
733 FML_DCHECK(surface_ !=
nullptr);
736 if (width == surface_->width() && height == surface_->height()) {
740 auto const existing_vsync = surface_->vsync_enabled();
745 if (!surface_->Destroy()) {
746 FML_LOG(ERROR) <<
"View resize failed to destroy surface";
750 std::unique_ptr<egl::WindowSurface> resized_surface =
753 if (!resized_surface) {
754 FML_LOG(ERROR) <<
"View resize failed to create surface";
758 if (!resized_surface->MakeCurrent() ||
759 !resized_surface->SetVSyncEnabled(existing_vsync)) {
763 FML_LOG(ERROR) <<
"View resize failed to set vsync";
766 surface_ = std::move(resized_surface);
771 return surface_.get();
779 return binding_handler_->GetWindowHandle();
787 auto alert_delegate = binding_handler_->GetAlertDelegate();
788 if (!alert_delegate) {
791 alert_delegate->SetText(fml::WideStringToUtf16(
text));
792 ui::AXPlatformNodeWin* alert_node = binding_handler_->GetAlert();
797 ax::mojom::Event event) {
799 node->NotifyAccessibilityEvent(event);
804 return accessibility_bridge_.get();
808 return binding_handler_->GetAlert();
811 std::shared_ptr<AccessibilityBridgeWindows>
813 return std::make_shared<AccessibilityBridgeWindows>(
this);
817 if (semantics_enabled_ != enabled) {
818 semantics_enabled_ = enabled;
820 if (!semantics_enabled_ && accessibility_bridge_) {
821 accessibility_bridge_.reset();
822 }
else if (semantics_enabled_ && !accessibility_bridge_) {
829 UpdateVsync(*engine_, surface_.get(), NeedsVsync());
837 return binding_handler_->Focus();
840 bool FlutterWindowsView::NeedsVsync()
const {
844 return !windows_proc_table_->DwmIsCompositionEnabled();
void UpdateHighContrastMode()
TaskRunner * task_runner()
void OnWindowStateEvent(HWND hwnd, WindowStateEvent event)
void SendViewFocusEvent(const FlutterViewFocusEvent &event)
void UpdateSemanticsEnabled(bool enabled)
KeyboardHandlerBase * keyboard_key_handler()
TextInputPlugin * text_input_plugin()
void SendPointerEvent(const FlutterPointerEvent &event)
void SendWindowMetricsEvent(const FlutterWindowMetricsEvent &event)
egl::Manager * egl_manager() const
virtual void OnFramePresented()
virtual void OnPointerPanZoomStart(int32_t device_id) override
void CreateRenderSurface()
void OnPointerUp(double x, double y, FlutterPointerDeviceKind device_kind, int32_t device_id, FlutterPointerMouseButtons button) override
FlutterWindowsView(FlutterViewId view_id, FlutterWindowsEngine *engine, std::unique_ptr< WindowBindingHandler > window_binding, std::shared_ptr< WindowsProcTable > windows_proc_table=nullptr)
virtual void UpdateSemanticsEnabled(bool enabled)
virtual ui::AXFragmentRootDelegateWin * GetAxFragmentRootDelegate() override
void OnPointerMove(double x, double y, FlutterPointerDeviceKind device_kind, int32_t device_id, int modifiers_state) override
bool IsImplicitView() const
void OnScrollInertiaCancel(int32_t device_id) override
virtual std::shared_ptr< AccessibilityBridgeWindows > CreateAccessibilityBridge()
void OnComposeBegin() override
ui::AXPlatformNodeWin * AlertNode() const
virtual void OnResetImeComposing()
virtual void OnUpdateSemanticsEnabled(bool enabled) override
void OnPointerLeave(double x, double y, FlutterPointerDeviceKind device_kind, int32_t device_id=0) override
virtual void NotifyWinEventWrapper(ui::AXPlatformNodeWin *node, ax::mojom::Event event)
FlutterWindowsEngine * GetEngine() const
void OnScroll(double x, double y, double delta_x, double delta_y, int scroll_offset_multiplier, FlutterPointerDeviceKind device_kind, int32_t device_id) override
void OnWindowStateEvent(HWND hwnd, WindowStateEvent event) override
virtual void OnPointerPanZoomEnd(int32_t device_id) override
FlutterWindowMetricsEvent CreateWindowMetricsEvent() const
FlutterViewId view_id() const
void AnnounceAlert(const std::wstring &text)
virtual bool PresentSoftwareBitmap(const void *allocation, size_t row_bytes, size_t height)
void OnFocus(FlutterViewFocusState focus_state, FlutterViewFocusDirection direction) override
virtual HWND GetWindowHandle() const
egl::WindowSurface * surface() const
bool OnWindowSizeChanged(size_t width, size_t height) override
void OnText(const std::u16string &) override
void OnWindowRepaint() override
virtual void OnPointerPanZoomUpdate(int32_t device_id, double pan_x, double pan_y, double scale, double rotation) override
void OnComposeChange(const std::u16string &text, int cursor_pos) override
void OnDwmCompositionChanged()
void OnComposeCommit() override
virtual bool ClearSoftwareBitmap()
void OnPointerDown(double x, double y, FlutterPointerDeviceKind device_kind, int32_t device_id, FlutterPointerMouseButtons button) override
virtual gfx::NativeViewAccessible GetNativeViewAccessible() override
virtual ~FlutterWindowsView()
void OnHighContrastChanged() override
virtual void OnCursorRectUpdated(const Rect &rect)
void OnKey(int key, int scancode, int action, char32_t character, bool extended, bool was_down, KeyEventCallback callback) override
void OnComposeEnd() override
bool OnEmptyFrameGenerated()
bool OnFrameGenerated(size_t width, size_t height)
virtual void SyncModifiersIfNeeded(int modifiers_state)=0
virtual void KeyboardHook(int key, int scancode, int action, char32_t character, bool extended, bool was_down, KeyEventCallback callback)=0
void PollOnce(std::chrono::milliseconds timeout)
void PostTask(TaskClosure task)
virtual void ComposeCommitHook()
virtual void ComposeChangeHook(const std::u16string &text, int cursor_pos)
virtual void TextHook(const std::u16string &text)
virtual void ComposeEndHook()
virtual void ComposeBeginHook()
std::function< void(bool)> KeyEventCallback
virtual std::unique_ptr< WindowSurface > CreateWindowSurface(HWND hwnd, size_t width, size_t height)
FlutterDesktopBinaryReply callback
WindowStateEvent
An event representing a change in window state that may update the.
constexpr FlutterViewId kImplicitViewId