| 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"
16 #include "flutter/shell/platform/common/windowing.h"
17 #include "flutter/shell/platform/windows/window_manager.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:
106 friend WindowManager;
107
108 // Information saved before going into fullscreen mode, used to restore the
109 // window afterwards.
110 struct SavedWindowInfo {
111 LONG style;
112 LONG ex_style;
113 RECT rect;
114 ActualWindowSize client_size;
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`.
170 HostWindow* FindFirstEnabledDescendant() const;
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.
188 FlutterWindowsEngine* engine_;
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.
196 WindowArchetype archetype_ = WindowArchetype::kRegular;
197
198 // Backing handle for this window.
199 HWND window_handle_;
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.
211 SavedWindowInfo saved_window_info_;
212
213 // Used to mark a window as fullscreen.
214 Microsoft::WRL::ComPtr<ITaskbarList2> task_bar_list_;
215
216 FML_DISALLOW_COPY_AND_ASSIGN(HostWindow);
217 };
218
219 } // namespace flutter
220
221 #endif // FLUTTER_SHELL_PLATFORM_WINDOWS_HOST_WINDOW_H_
flutter::FlutterWindowsEngine
Definition: flutter_windows_engine.h:92
flutter::HostWindow
Definition: host_window.h:27
flutter::HostWindow::task_bar_list_
Microsoft::WRL::ComPtr< ITaskbarList2 > task_bar_list_
Definition: host_window.h:214
flutter::HostWindow::GetWindowHandle
HWND GetWindowHandle() const
Definition: host_window.cc:342
flutter::HostWindow::box_constraints_
BoxConstraints box_constraints_
Definition: host_window.h:202
flutter::HostWindow::window_handle_
HWND window_handle_
Definition: host_window.h:199
flutter::HostWindow::archetype_
WindowArchetype archetype_
Definition: host_window.h:196
flutter::HostWindow::saved_window_info_
SavedWindowInfo saved_window_info_
Definition: host_window.h:211
flutter::HostWindow::FML_DISALLOW_COPY_AND_ASSIGN
FML_DISALLOW_COPY_AND_ASSIGN(HostWindow)
flutter::HostWindow::is_fullscreen_
bool is_fullscreen_
Definition: host_window.h:208
flutter::HostWindow::view_controller_
std::unique_ptr< FlutterWindowsViewController > view_controller_
Definition: host_window.h:193
flutter::HostWindow::DisableRecursively
void DisableRecursively()
Definition: host_window.cc:824
flutter::HostWindow::WndProc
static LRESULT WndProc(HWND hwnd, UINT message, WPARAM wparam, LPARAM lparam)
Definition: host_window.cc:353
flutter::HostWindow::GetOwnerWindow
HostWindow * GetOwnerWindow() const
Definition: host_window.cc:817
flutter::HostWindow::GetWindowContentSize
static ActualWindowSize GetWindowContentSize(HWND hwnd)
Definition: host_window.cc:712
flutter::HostWindow::EnableRecursively
void EnableRecursively(bool enable)
Definition: host_window.cc:772
flutter::HostWindow::SetContentSize
void SetContentSize(const WindowSizeRequest &size)
Definition: host_window.cc:479
flutter::HostWindow::is_being_destroyed_
bool is_being_destroyed_
Definition: host_window.h:205
flutter::HostWindow::UpdateModalStateLayer
void UpdateModalStateLayer()
Definition: host_window.cc:833
flutter::HostWindow::FocusRootViewOf
static void FocusRootViewOf(HostWindow *window)
Definition: host_window.cc:346
flutter::HostWindow::HostWindow
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
flutter::HostWindow::engine_
FlutterWindowsEngine * engine_
Definition: host_window.h:188
flutter::HostWindow::~HostWindow
virtual ~HostWindow()
Definition: host_window.cc:316
flutter::HostWindow::GetThisFromHandle
static HostWindow * GetThisFromHandle(HWND hwnd)
Definition: host_window.cc:327
flutter::HostWindow::WindowManager
friend WindowManager
Definition: host_window.h:106
flutter::HostWindow::GetOwnedWindows
std::vector< HostWindow * > GetOwnedWindows() const
Definition: host_window.cc:794
flutter::HostWindow::GetFullscreen
virtual bool GetFullscreen() const
Definition: host_window.cc:708
flutter::HostWindow::SetConstraints
void SetConstraints(const WindowConstraints &constraints)
Definition: host_window.cc:521
flutter::HostWindow::SetFullscreen
virtual void SetFullscreen(bool fullscreen, std::optional< FlutterEngineDisplayId > display_id)
Definition: host_window.cc:562
flutter::HostWindow::HandleMessage
virtual LRESULT HandleMessage(HWND hwnd, UINT message, WPARAM wparam, LPARAM lparam)
Definition: host_window.cc:370
flutter::HostWindow::CreateDialogWindow
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
flutter::HostWindow::GetWindowSizeForClientSize
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
flutter::HostWindow::CreateRegularWindow
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
flutter::HostWindow::FindFirstEnabledDescendant
HostWindow * FindFirstEnabledDescendant() const
Definition: host_window.cc:780
flutter::HostWindow::window_manager_
WindowManager *const window_manager_
Definition: host_window.h:185
flutter::WindowManager
Definition: window_manager.h:84
flutter::WindowsProcTable
Definition: windows_proc_table.h:18
message
Win32Message message
Definition: keyboard_unittests.cc:137
flutter
Definition: accessibility_bridge_windows.cc:11
flutter::WindowArchetype
WindowArchetype
Definition: windowing.h:11
flutter::WindowArchetype::kRegular
@ kRegular
flutter::ActualWindowSize
Definition: window_manager.h:71
flutter::HostWindow::SavedWindowInfo
Definition: host_window.h:110
flutter::HostWindow::SavedWindowInfo::client_size
ActualWindowSize client_size
Definition: host_window.h:114
flutter::HostWindow::SavedWindowInfo::dpi
int dpi
Definition: host_window.h:115
flutter::HostWindow::SavedWindowInfo::ex_style
LONG ex_style
Definition: host_window.h:112
flutter::HostWindow::SavedWindowInfo::style
LONG style
Definition: host_window.h:111
flutter::HostWindow::SavedWindowInfo::monitor_info
MONITORINFO monitor_info
Definition: host_window.h:117
flutter::HostWindow::SavedWindowInfo::monitor
HMONITOR monitor
Definition: host_window.h:116
flutter::HostWindow::SavedWindowInfo::rect
RECT rect
Definition: host_window.h:113
flutter::WindowConstraints
Definition: window_manager.h:33
flutter::WindowSizeRequest
Definition: window_manager.h:26
window_manager.h
windowing.h