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