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 {
377 event.pixel_ratio = pixel_ratio;
378 event.display_id = display_id;
379 event.view_id = view_id_;
385 double pixel_ratio = binding_handler_->GetDpiScale();
390 event.width = bounds.
width;
391 event.height = bounds.
height;
392 event.pixel_ratio = pixel_ratio;
393 event.display_id = display_id;
394 event.view_id = view_id_;
409FlutterWindowsView::PointerState* FlutterWindowsView::GetOrCreatePointerState(
415 int32_t pointer_id = (
static_cast<int32_t
>(device_kind) << 28) | device_id;
417 auto [it, added] = pointer_states_.try_emplace(pointer_id,
nullptr);
419 auto state = std::make_unique<PointerState>();
420 state->device_kind = device_kind;
421 state->pointer_id = pointer_id;
422 it->second = std::move(state);
425 return it->second.get();
430void FlutterWindowsView::SetEventPhaseFromCursorButtonState(
432 const PointerState* state)
const {
435 if (state->buttons == 0) {
436 event_data->
phase = state->flutter_state_is_down
440 event_data->
phase = state->flutter_state_is_down
446void FlutterWindowsView::SendPointerMove(
double x,
448 PointerState* state) {
453 SetEventPhaseFromCursorButtonState(&event, state);
454 SendPointerEventWithData(event, state);
457void FlutterWindowsView::SendPointerDown(
double x,
459 PointerState* state) {
464 SetEventPhaseFromCursorButtonState(&event, state);
465 SendPointerEventWithData(event, state);
467 state->flutter_state_is_down =
true;
470void FlutterWindowsView::SendPointerUp(
double x,
472 PointerState* state) {
477 SetEventPhaseFromCursorButtonState(&event, state);
478 SendPointerEventWithData(event, state);
480 state->flutter_state_is_down =
false;
484void FlutterWindowsView::SendPointerLeave(
double x,
486 PointerState* state) {
491 SendPointerEventWithData(event, state);
494void FlutterWindowsView::SendPointerPanZoomStart(int32_t device_id,
499 state->pan_zoom_start_x =
x;
500 state->pan_zoom_start_y =
y;
505 SendPointerEventWithData(event, state);
508void FlutterWindowsView::SendPointerPanZoomUpdate(int32_t device_id,
516 event.
x = state->pan_zoom_start_x;
517 event.y = state->pan_zoom_start_y;
521 event.rotation = rotation;
523 SendPointerEventWithData(event, state);
526void FlutterWindowsView::SendPointerPanZoomEnd(int32_t device_id) {
530 event.
x = state->pan_zoom_start_x;
531 event.y = state->pan_zoom_start_y;
533 SendPointerEventWithData(event, state);
536void FlutterWindowsView::SendText(
const std::u16string&
text) {
540void FlutterWindowsView::SendKey(
int key,
552 engine->text_input_plugin()->KeyboardHook(
565 event.view_id = view_id_;
566 event.state = focus_state;
567 event.direction = direction;
571void FlutterWindowsView::SendComposeBegin() {
575void FlutterWindowsView::SendComposeCommit() {
579void FlutterWindowsView::SendComposeEnd() {
583void FlutterWindowsView::SendComposeChange(
const std::u16string&
text,
588void FlutterWindowsView::SendScroll(
double x,
592 int scroll_offset_multiplier,
595 auto state = GetOrCreatePointerState(device_kind, device_id);
601 event.scroll_delta_x = delta_x * scroll_offset_multiplier;
602 event.scroll_delta_y = delta_y * scroll_offset_multiplier;
603 SetEventPhaseFromCursorButtonState(&event, state);
604 SendPointerEventWithData(event, state);
607void FlutterWindowsView::SendScrollInertiaCancel(int32_t device_id,
618 SetEventPhaseFromCursorButtonState(&event, state);
619 SendPointerEventWithData(event, state);
622void FlutterWindowsView::SendPointerEventWithData(
624 PointerState* state) {
627 if (!state->flutter_state_is_added &&
631 event.x = event_data.
x;
632 event.y = event_data.
y;
634 SendPointerEventWithData(event, state);
639 if (state->flutter_state_is_added &&
646 event.device = state->pointer_id;
647 event.buttons = state->buttons;
648 event.view_id = view_id_;
651 event.struct_size =
sizeof(event);
653 std::chrono::duration_cast<std::chrono::microseconds>(
654 std::chrono::high_resolution_clock::now().time_since_epoch())
660 state->flutter_state_is_added =
true;
662 auto it = pointer_states_.find(state->pointer_id);
663 if (it != pointer_states_.end()) {
664 pointer_states_.erase(it);
671 std::unique_lock<std::mutex> lock(resize_mutex_);
673 switch (resize_status_) {
674 case ResizeState::kResizeStarted:
685 case ResizeState::kFrameGenerated: {
687 resize_status_ = ResizeState::kDone;
695 windows_proc_table_->DwmFlush();
697 case ResizeState::kDone:
703 return binding_handler_->OnBitmapSurfaceCleared();
709 return binding_handler_->OnBitmapSurfaceUpdated(allocation, row_bytes,
729 UpdateVsync(*engine_, surface_.get(), NeedsVsync());
731 resize_target_width_ = bounds.
width;
732 resize_target_height_ = bounds.
height;
736bool FlutterWindowsView::ResizeRenderSurface(
size_t width,
size_t height) {
740 if (
width == surface_->width() &&
height == surface_->height()) {
744 auto const existing_vsync = surface_->vsync_enabled();
749 if (!surface_->Destroy()) {
750 FML_LOG(ERROR) <<
"View resize failed to destroy surface";
754 std::unique_ptr<egl::WindowSurface> resized_surface =
757 if (!resized_surface) {
758 FML_LOG(ERROR) <<
"View resize failed to create surface";
762 if (!resized_surface->MakeCurrent() ||
763 !resized_surface->SetVSyncEnabled(existing_vsync)) {
767 FML_LOG(ERROR) <<
"View resize failed to set vsync";
770 surface_ = std::move(resized_surface);
775 return surface_.get();
783 return binding_handler_->GetWindowHandle();
791 auto alert_delegate = binding_handler_->GetAlertDelegate();
792 if (!alert_delegate) {
796 ui::AXPlatformNodeWin* alert_node = binding_handler_->GetAlert();
803 node->NotifyAccessibilityEvent(event);
808 return accessibility_bridge_.get();
812 return binding_handler_->GetAlert();
815std::shared_ptr<AccessibilityBridgeWindows>
817 return std::make_shared<AccessibilityBridgeWindows>(
this);
821 if (semantics_enabled_ != enabled) {
822 semantics_enabled_ = enabled;
824 if (!semantics_enabled_ && accessibility_bridge_) {
825 accessibility_bridge_.reset();
826 }
else if (semantics_enabled_ && !accessibility_bridge_) {
833 UpdateVsync(*engine_, surface_.get(), NeedsVsync());
841 return binding_handler_->Focus();
844bool FlutterWindowsView::NeedsVsync()
const {
848 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).