Flutter Engine
flutter_windows_view.h
Go to the documentation of this file.
1 // Copyright 2013 The Flutter Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4 
5 #ifndef FLUTTER_SHELL_PLATFORM_WINDOWS_FLUTTER_WINDOWS_VIEW_H_
6 #define FLUTTER_SHELL_PLATFORM_WINDOWS_FLUTTER_WINDOWS_VIEW_H_
7 
8 #include <windowsx.h>
9 
10 #include <memory>
11 #include <string>
12 #include <vector>
13 
14 #include "flutter/shell/platform/common/cpp/client_wrapper/include/flutter/plugin_registrar.h"
15 #include "flutter/shell/platform/embedder/embedder.h"
16 #include "flutter/shell/platform/windows/angle_surface_manager.h"
17 #include "flutter/shell/platform/windows/cursor_handler.h"
18 #include "flutter/shell/platform/windows/flutter_windows_engine.h"
19 #include "flutter/shell/platform/windows/key_event_handler.h"
20 #include "flutter/shell/platform/windows/keyboard_hook_handler.h"
21 #include "flutter/shell/platform/windows/public/flutter_windows.h"
22 #include "flutter/shell/platform/windows/text_input_plugin.h"
23 #include "flutter/shell/platform/windows/win32_platform_handler.h"
24 #include "flutter/shell/platform/windows/window_binding_handler.h"
25 #include "flutter/shell/platform/windows/window_binding_handler_delegate.h"
26 #include "flutter/shell/platform/windows/window_state.h"
27 
28 namespace flutter {
29 
30 // An OS-windowing neutral abstration for flutter
31 // view that works with win32 hwnds and Windows::UI::Composition visuals.
33  public:
34  // Creates a FlutterWindowsView with the given implementator of
35  // WindowBindingHandler.
36  //
37  // In order for object to render Flutter content the SetEngine method must be
38  // called with a valid FlutterWindowsEngine instance.
39  FlutterWindowsView(std::unique_ptr<WindowBindingHandler> window_binding);
40 
42 
43  // Configures the window instance with an instance of a running Flutter
44  // engine.
45  void SetEngine(std::unique_ptr<FlutterWindowsEngine> engine);
46 
47  // Creates rendering surface for Flutter engine to draw into.
48  // Should be called before calling FlutterEngineRun using this view.
49  void CreateRenderSurface();
50 
51  // Destroys current rendering surface if one has been allocated.
52  void DestroyRenderSurface();
53 
54  // Return the currently configured WindowsRenderTarget.
56 
57  // Returns the engine backing this view.
59 
60  // Callbacks for clearing context, settings context and swapping buffers.
61  bool ClearContext();
62  bool MakeCurrent();
63  bool MakeResourceCurrent();
64  bool SwapBuffers();
65 
66  // Send initial bounds to embedder. Must occur after engine has initialized.
67  void SendInitialBounds();
68 
69  // |WindowBindingHandlerDelegate|
70  void OnWindowSizeChanged(size_t width, size_t height) const override;
71 
72  // |WindowBindingHandlerDelegate|
73  void OnPointerMove(double x, double y) override;
74 
75  // |WindowBindingHandlerDelegate|
76  void OnPointerDown(double x,
77  double y,
78  FlutterPointerMouseButtons button) override;
79 
80  // |WindowBindingHandlerDelegate|
81  void OnPointerUp(double x,
82  double y,
83  FlutterPointerMouseButtons button) override;
84 
85  // |WindowBindingHandlerDelegate|
86  void OnPointerLeave() override;
87 
88  // |WindowBindingHandlerDelegate|
89  void OnText(const std::u16string&) override;
90 
91  // |WindowBindingHandlerDelegate|
92  void OnKey(int key, int scancode, int action, char32_t character) override;
93 
94  // |WindowBindingHandlerDelegate|
95  void OnScroll(double x,
96  double y,
97  double delta_x,
98  double delta_y,
99  int scroll_offset_multiplier) override;
100 
101  private:
102  // Struct holding the mouse state. The engine doesn't keep track of which
103  // mouse buttons have been pressed, so it's the embedding's responsibility.
104  struct MouseState {
105  // True if the last event sent to Flutter had at least one mouse button.
106  // pressed.
107  bool flutter_state_is_down = false;
108 
109  // True if kAdd has been sent to Flutter. Used to determine whether
110  // to send a kAdd event before sending an incoming mouse event, since
111  // Flutter expects pointers to be added before events are sent for them.
112  bool flutter_state_is_added = false;
113 
114  // The currently pressed buttons, as represented in FlutterPointerEvent.
115  uint64_t buttons = 0;
116  };
117 
118  // Sends a window metrics update to the Flutter engine using current window
119  // dimensions in physical
120  void SendWindowMetrics(size_t width, size_t height, double dpiscale) const;
121 
122  // Reports a mouse movement to Flutter engine.
123  void SendPointerMove(double x, double y);
124 
125  // Reports mouse press to Flutter engine.
126  void SendPointerDown(double x, double y);
127 
128  // Reports mouse release to Flutter engine.
129  void SendPointerUp(double x, double y);
130 
131  // Reports mouse left the window client area.
132  //
133  // Win32 api doesn't have "mouse enter" event. Therefore, there is no
134  // SendPointerEnter method. A mouse enter event is tracked then the "move"
135  // event is called.
136  void SendPointerLeave();
137 
138  // Reports a keyboard character to Flutter engine.
139  void SendText(const std::u16string&);
140 
141  // Reports a raw keyboard message to Flutter engine.
142  void SendKey(int key, int scancode, int action, char32_t character);
143 
144  // Reports scroll wheel events to Flutter engine.
145  void SendScroll(double x,
146  double y,
147  double delta_x,
148  double delta_y,
149  int scroll_offset_multiplier);
150 
151  // Sets |event_data|'s phase to either kMove or kHover depending on the
152  // current primary mouse button state.
153  void SetEventPhaseFromCursorButtonState(
154  FlutterPointerEvent* event_data) const;
155 
156  // Sends a pointer event to the Flutter engine based on given data. Since
157  // all input messages are passed in physical pixel values, no translation is
158  // needed before passing on to engine.
159  void SendPointerEventWithData(const FlutterPointerEvent& event_data);
160 
161  // Resets the mouse state to its default values.
162  void ResetMouseState() { mouse_state_ = MouseState(); }
163 
164  // Updates the mouse state to whether the last event to Flutter had at least
165  // one mouse button pressed.
166  void SetMouseFlutterStateDown(bool is_down) {
167  mouse_state_.flutter_state_is_down = is_down;
168  }
169 
170  // Updates the mouse state to whether the last event to Flutter was a kAdd
171  // event.
172  void SetMouseFlutterStateAdded(bool is_added) {
173  mouse_state_.flutter_state_is_added = is_added;
174  }
175 
176  // Updates the currently pressed buttons.
177  void SetMouseButtons(uint64_t buttons) { mouse_state_.buttons = buttons; }
178 
179  // Currently configured WindowsRenderTarget for this view used by
180  // surface_manager for creation of render surfaces and bound to the physical
181  // os window.
182  std::unique_ptr<WindowsRenderTarget> render_target_;
183 
184  // An object used for intializing Angle and creating / destroying render
185  // surfaces. Surface creation functionality requires a valid render_target.
186  std::unique_ptr<AngleSurfaceManager> surface_manager_;
187 
188  // The engine associated with this view.
189  std::unique_ptr<FlutterWindowsEngine> engine_;
190 
191  // Keeps track of mouse state in relation to the window.
192  MouseState mouse_state_;
193 
194  // The plugin registrar managing internal plugins.
195  std::unique_ptr<flutter::PluginRegistrar> internal_plugin_registrar_;
196 
197  // Handlers for keyboard events from Windows.
198  std::vector<std::unique_ptr<flutter::KeyboardHookHandler>>
199  keyboard_hook_handlers_;
200 
201  // Handler for the flutter/platform channel.
202  std::unique_ptr<flutter::PlatformHandler> platform_handler_;
203 
204  // Handler for cursor events.
205  std::unique_ptr<flutter::CursorHandler> cursor_handler_;
206 
207  // Currently configured WindowBindingHandler for view.
208  std::unique_ptr<flutter::WindowBindingHandler> binding_handler_;
209 };
210 
211 } // namespace flutter
212 
213 #endif // FLUTTER_SHELL_PLATFORM_WINDOWS_FLUTTER_WINDOWS_VIEW_H_
FlutterPointerMouseButtons
Definition: embedder.h:514
void OnScroll(double x, double y, double delta_x, double delta_y, int scroll_offset_multiplier) override
FlutterWindowsView(std::unique_ptr< WindowBindingHandler > window_binding)
void OnWindowSizeChanged(size_t width, size_t height) const override
std::variant< HWND > WindowsRenderTarget
void OnPointerDown(double x, double y, FlutterPointerMouseButtons button) override
void OnPointerMove(double x, double y) override
void SetEngine(std::unique_ptr< FlutterWindowsEngine > engine)
void OnText(const std::u16string &) override
SemanticsAction action
int32_t height
int32_t width
void OnPointerUp(double x, double y, FlutterPointerMouseButtons button) override
WindowsRenderTarget * GetRenderTarget() const
void OnKey(int key, int scancode, int action, char32_t character) override
FlutterWindowsEngine * GetEngine()