Flutter Windows Embedder
host_window.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_HOST_WINDOW_H_
6 #define FLUTTER_SHELL_PLATFORM_WINDOWS_HOST_WINDOW_H_
7 
8 #include <shobjidl.h>
9 #include <windows.h>
10 #include <wrl/client.h>
11 #include <memory>
12 #include <optional>
13 #include <string>
14 
15 #include "flutter/fml/macros.h"
16 #include "flutter/shell/geometry/geometry.h"
19 
20 namespace flutter {
21 
22 class WindowManager;
23 class WindowsProcTable;
24 class FlutterWindowsView;
25 class FlutterWindowsViewSizingDelegate;
26 class FlutterWindowsViewController;
27 
28 // A Win32 window that hosts a |FlutterWindow| in its client area.
29 class HostWindow {
30  public:
31  virtual ~HostWindow();
32 
33  // Creates a regular Win32 window with a child view confined to its client
34  // area. |window_manager| is a pointer to the window manager that manages the
35  // |HostWindow|. |engine| is a pointer to the engine that manages
36  // the window manager. |preferred_size| is the preferred size of the window.
37  // |preferred_constraints| are the constraints set on the window's size.
38  // |title| is the title of the window.
39  //
40  // On success, a valid window handle can be retrieved
41  // via |HostWindow::GetWindowHandle|. |nullptr| will be returned
42  // on failure.
43  static std::unique_ptr<HostWindow> CreateRegularWindow(
44  WindowManager* window_manager,
45  FlutterWindowsEngine* engine,
46  const WindowSizeRequest& preferred_size,
47  const WindowConstraints& preferred_constraints,
48  LPCWSTR title);
49 
50  // Creates a dialog Win32 window with a child view confined to its client
51  // area. |window_manager| is a pointer to the window manager that manages the
52  // |HostWindow|. |engine| is a pointer to the engine that manages
53  // the window manager. |preferred_size| is the preferred size of the window.
54  // |preferred_constraints| are the constraints set on the window's size.
55  // |title| is the title of the window. |parent| is the parent of this dialog,
56  // which can be `nullptr`.
57  //
58  // On success, a valid window handle can be retrieved
59  // via |HostWindow::GetWindowHandle|. `nullptr` will be returned
60  // on failure.
61  static std::unique_ptr<HostWindow> CreateDialogWindow(
62  WindowManager* window_manager,
63  FlutterWindowsEngine* engine,
64  const WindowSizeRequest& preferred_size,
65  const WindowConstraints& preferred_constraints,
66  LPCWSTR title,
67  HWND parent);
68 
69  static std::unique_ptr<HostWindow> CreateTooltipWindow(
70  WindowManager* window_manager,
71  FlutterWindowsEngine* engine,
72  const WindowConstraints& preferred_constraints,
73  bool is_sized_to_content,
74  GetWindowPositionCallback get_position_callback,
75  HWND parent);
76 
77  // Returns the instance pointer for |hwnd| or nullptr if invalid.
78  static HostWindow* GetThisFromHandle(HWND hwnd);
79 
80  // Returns the backing window handle, or nullptr if the native window is not
81  // created or has already been destroyed.
82  HWND GetWindowHandle() const;
83 
84  // Returns the HWND of the FlutterView hosted in this window.
85  HWND GetFlutterViewWindowHandle() const;
86 
87  // Resizes the window to accommodate a client area of the given
88  // |size|. If the size does not satisfy the constraints, the window will be
89  // resized to the minimum or maximum size as appropriate.
90  void SetContentSize(const WindowSizeRequest& size);
91 
92  // Sets the constaints on the client area of the window.
93  // If the current window size does not satisfy the new constraints,
94  // the window will be resized to satisy thew new constraints.
95  void SetConstraints(const WindowConstraints& constraints);
96 
97  // Set the fullscreen state. |display_id| indicates the display where
98  // the window should be shown fullscreen; std::nullopt indicates
99  // that no display was specified, so the current display may be used.
100  virtual void SetFullscreen(bool fullscreen,
101  std::optional<FlutterEngineDisplayId> display_id);
102 
103  // Returns |true| if this window is fullscreen, otherwise |false|.
104  virtual bool GetFullscreen() const;
105 
106  // Given a window identifier, returns the window content size of the
107  // window.
108  static ActualWindowSize GetWindowContentSize(HWND hwnd);
109 
110  // Returns the owner window, or nullptr if none.
111  HostWindow* GetOwnerWindow() const;
112 
113  // This method is called when a dialog is created or destroyed.
114  // It walks the path of child windows to make sure that the right
115  // windows are enabled or disabled.
116  void UpdateModalStateLayer();
117 
118  protected:
123  const BoxConstraints& box_constraints;
125  LPCWSTR title;
126  std::optional<HWND> const& owner_window;
127  int nCmdShow = SW_SHOWNORMAL;
129  bool is_sized_to_content = false;
130  };
131 
132  // Initialize the underlying native window and the Flutter view.
133  //
134  // See:
135  // - https://learn.microsoft.com/windows/win32/winmsg/window-styles
136  // - https://learn.microsoft.com/windows/win32/winmsg/extended-window-styles
138 
140 
141  // Information saved before going into fullscreen mode, used to restore the
142  // window afterwards.
144  LONG style;
145  LONG ex_style;
146  RECT rect;
148  int dpi;
149  HMONITOR monitor;
150  MONITORINFO monitor_info;
151  };
152 
153  // Construct a host window.
154  //
155  // Derived classes should call InitializeFlutterView() after construction to
156  // set up the native window and the Flutter view.
157  HostWindow(WindowManager* window_manager, FlutterWindowsEngine* engine);
158 
159  // Calculates the required window size, in physical coordinates, to
160  // accommodate the given |client_size|, in logical coordinates, constrained by
161  // optional |smallest| and |biggest|, for a window with the specified
162  // |window_style| and |extended_window_style|. If |owner_hwnd| is not null,
163  // the DPI of the display with the largest area of intersection with
164  // |owner_hwnd| is used for the calculation; otherwise, the primary display's
165  // DPI is used. The resulting size includes window borders, non-client areas,
166  // and drop shadows. On error, returns std::nullopt and logs an error message.
167  static std::optional<Size> GetWindowSizeForClientSize(
168  WindowsProcTable const& win32,
169  Size const& client_size,
170  std::optional<Size> smallest,
171  std::optional<Size> biggest,
172  DWORD window_style,
173  DWORD extended_window_style,
174  std::optional<HWND> const& owner_hwnd);
175 
176  // Processes and routes salient window messages for mouse handling,
177  // size change and DPI. Delegates handling of these to member overloads that
178  // inheriting classes can handle.
179  virtual LRESULT HandleMessage(HWND hwnd,
180  UINT message,
181  WPARAM wparam,
182  LPARAM lparam);
183 
184  // Sets the focus to the child view window of |window|.
185  static void FocusRootViewOf(HostWindow* window);
186 
187  // Enables or disables mouse and keyboard input to this window and all its
188  // descendants.
189  void EnableRecursively(bool enable);
190 
191  // Returns the first enabled descendant window. If the current window itself
192  // is enabled, returns the current window. If no window is enabled, returns
193  // `nullptr`.
195 
196  // Returns windows owned by this window.
197  std::vector<HostWindow*> GetOwnedWindows() const;
198 
199  // Disables mouse and keyboard input to the window and all its descendants.
200  void DisableRecursively();
201 
202  // OS callback called by message pump. Handles the WM_NCCREATE message which
203  // is passed when the non-client area is being created and enables automatic
204  // non-client DPI scaling so that the non-client area automatically
205  // responds to changes in DPI. Delegates other messages to the controller.
206  static LRESULT WndProc(HWND hwnd, UINT message, WPARAM wparam, LPARAM lparam);
207 
208  // Controller for this window.
209  WindowManager* const window_manager_ = nullptr;
210 
211  // The Flutter engine that owns this window.
213 
214  // Controller for the view hosted in this window. Value-initialized if the
215  // window is created from an existing top-level native window created by the
216  // runner.
217  std::unique_ptr<FlutterWindowsViewController> view_controller_;
218 
219  // The window archetype.
221 
222  // Backing handle for this window.
224 
225  // The constraints on the window's client area.
226  BoxConstraints box_constraints_;
227 
228  // True while handling WM_DESTROY; used to detect in-progress destruction.
229  bool is_being_destroyed_ = false;
230 
231  // Whether or not the window is currently in a fullscreen state.
232  bool is_fullscreen_ = false;
233 
234  // Saved window information from before entering fullscreen mode.
236 
237  // Used to mark a window as fullscreen.
238  Microsoft::WRL::ComPtr<ITaskbarList2> task_bar_list_;
239 
241 };
242 
243 } // namespace flutter
244 
245 #endif // FLUTTER_SHELL_PLATFORM_WINDOWS_HOST_WINDOW_H_
Microsoft::WRL::ComPtr< ITaskbarList2 > task_bar_list_
Definition: host_window.h:238
HWND GetWindowHandle() const
Definition: host_window.cc:350
BoxConstraints box_constraints_
Definition: host_window.h:226
WindowArchetype archetype_
Definition: host_window.h:220
void InitializeFlutterView(HostWindowInitializationParams const &params)
Definition: host_window.cc:245
SavedWindowInfo saved_window_info_
Definition: host_window.h:235
FML_DISALLOW_COPY_AND_ASSIGN(HostWindow)
std::unique_ptr< FlutterWindowsViewController > view_controller_
Definition: host_window.h:217
static LRESULT WndProc(HWND hwnd, UINT message, WPARAM wparam, LPARAM lparam)
Definition: host_window.cc:365
HostWindow * GetOwnerWindow() const
Definition: host_window.cc:829
static ActualWindowSize GetWindowContentSize(HWND hwnd)
Definition: host_window.cc:724
void EnableRecursively(bool enable)
Definition: host_window.cc:784
void SetContentSize(const WindowSizeRequest &size)
Definition: host_window.cc:491
void UpdateModalStateLayer()
Definition: host_window.cc:845
static void FocusRootViewOf(HostWindow *window)
Definition: host_window.cc:358
HWND GetFlutterViewWindowHandle() const
Definition: host_window.cc:354
FlutterWindowsEngine * engine_
Definition: host_window.h:212
static HostWindow * GetThisFromHandle(HWND hwnd)
Definition: host_window.cc:335
HostWindow(WindowManager *window_manager, FlutterWindowsEngine *engine)
Definition: host_window.cc:241
std::vector< HostWindow * > GetOwnedWindows() const
Definition: host_window.cc:806
virtual bool GetFullscreen() const
Definition: host_window.cc:720
void SetConstraints(const WindowConstraints &constraints)
Definition: host_window.cc:533
static std::unique_ptr< HostWindow > CreateTooltipWindow(WindowManager *window_manager, FlutterWindowsEngine *engine, const WindowConstraints &preferred_constraints, bool is_sized_to_content, GetWindowPositionCallback get_position_callback, HWND parent)
Definition: host_window.cc:229
virtual void SetFullscreen(bool fullscreen, std::optional< FlutterEngineDisplayId > display_id)
Definition: host_window.cc:574
virtual LRESULT HandleMessage(HWND hwnd, UINT message, WPARAM wparam, LPARAM lparam)
Definition: host_window.cc:382
static std::unique_ptr< HostWindow > CreateDialogWindow(WindowManager *window_manager, FlutterWindowsEngine *engine, const WindowSizeRequest &preferred_size, const WindowConstraints &preferred_constraints, LPCWSTR title, HWND parent)
Definition: host_window.cc:216
static std::optional< Size > GetWindowSizeForClientSize(WindowsProcTable const &win32, Size const &client_size, std::optional< Size > smallest, std::optional< Size > biggest, DWORD window_style, DWORD extended_window_style, std::optional< HWND > const &owner_hwnd)
Definition: host_window.cc:737
static std::unique_ptr< HostWindow > CreateRegularWindow(WindowManager *window_manager, FlutterWindowsEngine *engine, const WindowSizeRequest &preferred_size, const WindowConstraints &preferred_constraints, LPCWSTR title)
Definition: host_window.cc:205
HostWindow * FindFirstEnabledDescendant() const
Definition: host_window.cc:792
WindowManager *const window_manager_
Definition: host_window.h:209
Win32Message message
WindowRect *(* GetWindowPositionCallback)(const WindowSize &child_size, const WindowRect &parent_rect, const WindowRect &output_rect)
WindowArchetype
Definition: windowing.h:11
FlutterWindowsViewSizingDelegate * sizing_delegate
Definition: host_window.h:128