23constexpr std::chrono::milliseconds kWindowResizeTimeout{100};
31bool 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;
45void 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()) {
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 "
80 engine.PostRasterThreadTask(update_vsync);
87void DestroyWindowSurface(
const FlutterWindowsEngine&
engine,
88 std::unique_ptr<egl::WindowSurface> surface) {
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) {
241 int modifiers_state) {
243 SendPointerMove(
x,
y, GetOrCreatePointerState(device_kind, device_id));
252 if (flutter_button != 0) {
253 auto state = GetOrCreatePointerState(device_kind, device_id);
254 state->buttons |= flutter_button;
255 SendPointerDown(
x,
y, state);
265 if (flutter_button != 0) {
266 auto state = GetOrCreatePointerState(device_kind, device_id);
267 state->buttons &= ~flutter_button;
268 SendPointerUp(
x,
y, state);
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);
312 SendFocus(focus_state, direction);
329 SendComposeChange(
text, cursor_pos);
336 int scroll_offset_multiplier,
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();
369void FlutterWindowsView::SendWindowMetrics(
size_t width,
371 double pixel_ratio)
const {
376 event.pixel_ratio = pixel_ratio;
377 event.view_id = view_id_;
383 double pixel_ratio = binding_handler_->GetDpiScale();
388 event.width = bounds.
width;
389 event.height = bounds.
height;
390 event.pixel_ratio = pixel_ratio;
391 event.display_id = display_id;
392 event.view_id = view_id_;
407FlutterWindowsView::PointerState* FlutterWindowsView::GetOrCreatePointerState(
413 int32_t pointer_id = (
static_cast<int32_t
>(device_kind) << 28) | device_id;
415 auto [it, added] = pointer_states_.try_emplace(pointer_id,
nullptr);
417 auto state = std::make_unique<PointerState>();
418 state->device_kind = device_kind;
419 state->pointer_id = pointer_id;
420 it->second = std::move(state);
423 return it->second.get();
428void FlutterWindowsView::SetEventPhaseFromCursorButtonState(
430 const PointerState* state)
const {
433 if (state->buttons == 0) {
434 event_data->
phase = state->flutter_state_is_down
438 event_data->
phase = state->flutter_state_is_down
444void FlutterWindowsView::SendPointerMove(
double x,
446 PointerState* state) {
451 SetEventPhaseFromCursorButtonState(&event, state);
452 SendPointerEventWithData(event, state);
455void FlutterWindowsView::SendPointerDown(
double x,
457 PointerState* state) {
462 SetEventPhaseFromCursorButtonState(&event, state);
463 SendPointerEventWithData(event, state);
465 state->flutter_state_is_down =
true;
468void FlutterWindowsView::SendPointerUp(
double x,
470 PointerState* state) {
475 SetEventPhaseFromCursorButtonState(&event, state);
476 SendPointerEventWithData(event, state);
478 state->flutter_state_is_down =
false;
482void FlutterWindowsView::SendPointerLeave(
double x,
484 PointerState* state) {
489 SendPointerEventWithData(event, state);
492void FlutterWindowsView::SendPointerPanZoomStart(int32_t device_id,
497 state->pan_zoom_start_x =
x;
498 state->pan_zoom_start_y =
y;
503 SendPointerEventWithData(event, state);
506void FlutterWindowsView::SendPointerPanZoomUpdate(int32_t device_id,
514 event.
x = state->pan_zoom_start_x;
515 event.y = state->pan_zoom_start_y;
519 event.rotation = rotation;
521 SendPointerEventWithData(event, state);
524void FlutterWindowsView::SendPointerPanZoomEnd(int32_t device_id) {
528 event.
x = state->pan_zoom_start_x;
529 event.y = state->pan_zoom_start_y;
531 SendPointerEventWithData(event, state);
534void FlutterWindowsView::SendText(
const std::u16string&
text) {
538void FlutterWindowsView::SendKey(
int key,
550 engine->text_input_plugin()->KeyboardHook(
563 event.view_id = view_id_;
564 event.state = focus_state;
565 event.direction = direction;
569void FlutterWindowsView::SendComposeBegin() {
573void FlutterWindowsView::SendComposeCommit() {
577void FlutterWindowsView::SendComposeEnd() {
581void FlutterWindowsView::SendComposeChange(
const std::u16string&
text,
586void FlutterWindowsView::SendScroll(
double x,
590 int scroll_offset_multiplier,
593 auto state = GetOrCreatePointerState(device_kind, device_id);
599 event.scroll_delta_x = delta_x * scroll_offset_multiplier;
600 event.scroll_delta_y = delta_y * scroll_offset_multiplier;
601 SetEventPhaseFromCursorButtonState(&event, state);
602 SendPointerEventWithData(event, state);
605void FlutterWindowsView::SendScrollInertiaCancel(int32_t device_id,
616 SetEventPhaseFromCursorButtonState(&event, state);
617 SendPointerEventWithData(event, state);
620void FlutterWindowsView::SendPointerEventWithData(
622 PointerState* state) {
625 if (!state->flutter_state_is_added &&
629 event.x = event_data.
x;
630 event.y = event_data.
y;
632 SendPointerEventWithData(event, state);
637 if (state->flutter_state_is_added &&
644 event.device = state->pointer_id;
645 event.buttons = state->buttons;
646 event.view_id = view_id_;
649 event.struct_size =
sizeof(event);
651 std::chrono::duration_cast<std::chrono::microseconds>(
652 std::chrono::high_resolution_clock::now().time_since_epoch())
658 state->flutter_state_is_added =
true;
660 auto it = pointer_states_.find(state->pointer_id);
661 if (it != pointer_states_.end()) {
662 pointer_states_.erase(it);
669 std::unique_lock<std::mutex> lock(resize_mutex_);
671 switch (resize_status_) {
672 case ResizeState::kResizeStarted:
683 case ResizeState::kFrameGenerated: {
685 resize_status_ = ResizeState::kDone;
693 windows_proc_table_->DwmFlush();
695 case ResizeState::kDone:
701 return binding_handler_->OnBitmapSurfaceCleared();
707 return binding_handler_->OnBitmapSurfaceUpdated(allocation, row_bytes,
727 UpdateVsync(*engine_, surface_.get(), NeedsVsync());
729 resize_target_width_ = bounds.
width;
730 resize_target_height_ = bounds.
height;
734bool FlutterWindowsView::ResizeRenderSurface(
size_t width,
size_t height) {
738 if (
width == surface_->width() &&
height == surface_->height()) {
742 auto const existing_vsync = surface_->vsync_enabled();
747 if (!surface_->Destroy()) {
748 FML_LOG(ERROR) <<
"View resize failed to destroy surface";
752 std::unique_ptr<egl::WindowSurface> resized_surface =
755 if (!resized_surface) {
756 FML_LOG(ERROR) <<
"View resize failed to create surface";
760 if (!resized_surface->MakeCurrent() ||
761 !resized_surface->SetVSyncEnabled(existing_vsync)) {
765 FML_LOG(ERROR) <<
"View resize failed to set vsync";
768 surface_ = std::move(resized_surface);
773 return surface_.get();
781 return binding_handler_->GetWindowHandle();
789 auto alert_delegate = binding_handler_->GetAlertDelegate();
790 if (!alert_delegate) {
794 ui::AXPlatformNodeWin* alert_node = binding_handler_->GetAlert();
801 node->NotifyAccessibilityEvent(event);
806 return accessibility_bridge_.get();
810 return binding_handler_->GetAlert();
813std::shared_ptr<AccessibilityBridgeWindows>
815 return std::make_shared<AccessibilityBridgeWindows>(
this);
819 if (semantics_enabled_ != enabled) {
820 semantics_enabled_ = enabled;
822 if (!semantics_enabled_ && accessibility_bridge_) {
823 accessibility_bridge_.reset();
824 }
else if (semantics_enabled_ && !accessibility_bridge_) {
831 UpdateVsync(*engine_, surface_.get(), NeedsVsync());
839 return binding_handler_->Focus();
842bool FlutterWindowsView::NeedsVsync()
const {
846 return !windows_proc_table_->DwmIsCompositionEnabled();
void UpdateHighContrastMode()
void OnWindowStateEvent(HWND hwnd, WindowStateEvent event)
void SendViewFocusEvent(const FlutterViewFocusEvent &event)
void UpdateSemanticsEnabled(bool enabled)
TaskRunner * task_runner()
egl::Manager * egl_manager() const
TextInputPlugin * text_input_plugin()
void SendPointerEvent(const FlutterPointerEvent &event)
void SendWindowMetricsEvent(const FlutterWindowMetricsEvent &event)
KeyboardHandlerBase * keyboard_key_handler()
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)
uint64_t FlutterEngineDisplayId
FlutterViewFocusState
Represents the focus state of a given [FlutterView].
FlutterViewFocusDirection
@ kPanZoomUpdate
The pan/zoom updated.
@ kHover
The pointer moved while up.
@ kPanZoomStart
A pan/zoom started on this pointer.
@ kPanZoomEnd
The pan/zoom ended.
FlutterPointerMouseButtons
@ kFlutterPointerSignalKindScrollInertiaCancel
@ kFlutterPointerSignalKindScroll
FlutterPointerDeviceKind
The device type that created a pointer event.
@ kFlutterPointerDeviceKindTrackpad
G_BEGIN_DECLS FlutterViewId view_id
FlutterDesktopBinaryReply callback
#define FML_LOG(severity)
#define FML_DCHECK(condition)
WindowStateEvent
An event representing a change in window state that may update the.
constexpr FlutterViewId kImplicitViewId
internal::CopyableLambda< T > MakeCopyable(T lambda)
std::u16string WideStringToUtf16(const std::wstring_view str)
UnimplementedNativeViewAccessible * NativeViewAccessible
double y
The y coordinate of the pointer event in physical pixels.
double x
The x coordinate of the pointer event in physical pixels.
FlutterPointerDeviceKind device_kind
FlutterPointerPhase phase
size_t struct_size
The size of this struct. Must be sizeof(FlutterWindowMetricsEvent).