• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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_WINDOW_H_
6 #define FLUTTER_SHELL_PLATFORM_WINDOWS_FLUTTER_WINDOW_H_
7 
8 #include <windowsx.h>
9 
10 #include <string>
11 #include <vector>
12 
13 #include "flutter/shell/platform/common/cpp/client_wrapper/include/flutter/plugin_registrar.h"
14 #include "flutter/shell/platform/common/cpp/incoming_message_dispatcher.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/key_event_handler.h"
18 #include "flutter/shell/platform/windows/keyboard_hook_handler.h"
19 #include "flutter/shell/platform/windows/platform_handler.h"
20 #include "flutter/shell/platform/windows/public/flutter_windows.h"
21 #include "flutter/shell/platform/windows/text_input_plugin.h"
22 #include "flutter/shell/platform/windows/win32_window.h"
23 #include "flutter/shell/platform/windows/window_state.h"
24 
25 namespace flutter {
26 
27 // A win32 flutter window.  In the future, there will likely be a
28 // CoreWindow-based FlutterWindow as well.  At the point may make sense to
29 // dependency inject the native window rather than inherit.
30 class Win32FlutterWindow : public Win32Window {
31  public:
32   Win32FlutterWindow();
33   Win32FlutterWindow(const char* title,
34                      const int x,
35                      const int y,
36                      const int width,
37                      const int height) noexcept;
38   ~Win32FlutterWindow();
39 
40   static FlutterDesktopWindowControllerRef CreateWin32FlutterWindow(
41       const char* title,
42       const int x,
43       const int y,
44       const int width,
45       const int height);
46 
47   // Run a Windows message pump that also pumps plugin messages.
48   void FlutterMessageLoop();
49 
50   // |Win32Window|
51   void OnDpiScale(unsigned int dpi) override;
52 
53   // |Win32Window|
54   void OnResize(unsigned int width, unsigned int height) override;
55 
56   // |Win32Window|
57   void OnPointerMove(double x, double y) override;
58 
59   // |Win32Window|
60   void OnPointerDown(double x, double y) override;
61 
62   // |Win32Window|
63   void OnPointerUp(double x, double y) override;
64 
65   // |Win32Window|
66   void OnChar(unsigned int code_point) override;
67 
68   // |Win32Window|
69   void OnKey(int key, int scancode, int action, int mods) override;
70 
71   // |Win32Window|
72   void OnScroll(double delta_x, double delta_y) override;
73 
74   // |Win32Window|
75   void OnClose();
76 
77   // Configures the window instance with an instance of a running Flutter engine
78   // returning a configured FlutterDesktopWindowControllerRef.
79   void SetState(FLUTTER_API_SYMBOL(FlutterEngine) state);
80 
81   // Returns the currently configured Plugin Registrar.
82   FlutterDesktopPluginRegistrarRef GetRegistrar();
83 
84   // Callback passed to Flutter engine for notifying window of platform
85   // messages.
86   void HandlePlatformMessage(const FlutterPlatformMessage*);
87 
88   // Create a surface for Flutter engine to render into.
89   void CreateRenderSurface();
90 
91   // Destroy current rendering surface if one has been allocated.
92   void DestroyRenderSurface();
93 
94   // Callbacks for clearing context, settings context and swapping buffers.
95   bool ClearContext();
96   bool MakeCurrent();
97   bool SwapBuffers();
98 
99   // Sends a window metrics update to the Flutter engine using current window
100   // dimensions in physical
101   void SendWindowMetrics();
102 
103  private:
104   // Reports a mouse movement to Flutter engine.
105   void SendPointerMove(double x, double y);
106 
107   // Reports mouse press to Flutter engine.
108   void SendPointerDown(double x, double y);
109 
110   // Reports mouse release to Flutter engine.
111   void SendPointerUp(double x, double y);
112 
113   // Reports a keyboard character to Flutter engine.
114   void SendChar(unsigned int code_point);
115 
116   // Reports a raw keyboard message to Flutter engine.
117   void SendKey(int key, int scancode, int action, int mods);
118 
119   // Reports scroll wheel events to Flutter engine.
120   void SendScroll(double delta_x, double delta_y);
121 
122   // Updates |event_data| with the current location of the mouse cursor.
123   void SetEventLocationFromCursorPosition(FlutterPointerEvent* event_data);
124 
125   // Set's |event_data|'s phase to either kMove or kHover depending on the
126   // current
127   // primary mouse button state.
128   void SetEventPhaseFromCursorButtonState(FlutterPointerEvent* event_data);
129 
130   // Sends a pointer event to the Flutter engine based on givern data.  Since
131   // all input messages are passed in physical pixel values, no translation is
132   // needed before passing on to engine.
133   void SendPointerEventWithData(const FlutterPointerEvent& event_data);
134 
135   std::unique_ptr<AngleSurfaceManager> surface_manager = nullptr;
136   EGLSurface render_surface = EGL_NO_SURFACE;
137 
138   // state of the mouse button
139   bool pointer_is_down_ = false;
140 
141   // The handle to the Flutter engine instance.
142   FLUTTER_API_SYMBOL(FlutterEngine) engine_ = nullptr;
143 
144   // Whether or not to track mouse movements to send kHover events.
145   bool hover_tracking_is_enabled_ = false;
146 
147   // Whether or not the pointer has been added (or if tracking is enabled, has
148   // been added since it was last removed).
149   bool pointer_currently_added_ = false;
150 
151   // The window handle given to API clients.
152   std::unique_ptr<FlutterDesktopWindow> window_wrapper_;
153 
154   // The plugin registrar handle given to API clients.
155   std::unique_ptr<FlutterDesktopPluginRegistrar> plugin_registrar_;
156 
157   // Message dispatch manager for messages from the Flutter engine.
158   std::unique_ptr<flutter::IncomingMessageDispatcher> message_dispatcher_;
159 
160   // The plugin registrar managing internal plugins.
161   std::unique_ptr<flutter::PluginRegistrar> internal_plugin_registrar_;
162 
163   // Handlers for keyboard events from Windows.
164   std::vector<std::unique_ptr<flutter::KeyboardHookHandler>>
165       keyboard_hook_handlers_;
166 
167   // Handler for the flutter/platform channel.
168   std::unique_ptr<flutter::PlatformHandler> platform_handler_;
169 
170   // should we forword input messages or not
171   bool process_events_ = false;
172 
173   // flag indicating if the message loop should be running
174   bool messageloop_running_ = false;
175 };
176 
177 }  // namespace flutter
178 
179 #endif  // FLUTTER_SHELL_PLATFORM_WINDOWS_FLUTTER_WINDOW_H_