| 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"
17 #include "flutter/shell/platform/common/windowing.h"
18 #include "flutter/shell/platform/windows/window_manager.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 GetWindowPositionCallback get_position_callback,
74 HWND parent);
75
76 // Returns the instance pointer for |hwnd| or nullptr if invalid.
77 static HostWindow* GetThisFromHandle(HWND hwnd);
78
79 // Returns the backing window handle, or nullptr if the native window is not
80 // created or has already been destroyed.
81 HWND GetWindowHandle() const;
82
83 // Returns the HWND of the FlutterView hosted in this window.
84 HWND GetFlutterViewWindowHandle() const;
85
86 // Resizes the window to accommodate a client area of the given
87 // |size|. If the size does not satisfy the constraints, the window will be
88 // resized to the minimum or maximum size as appropriate.
89 void SetContentSize(const WindowSizeRequest& size);
90
91 // Sets the constaints on the client area of the window.
92 // If the current window size does not satisfy the new constraints,
93 // the window will be resized to satisy thew new constraints.
94 void SetConstraints(const WindowConstraints& constraints);
95
96 // Set the fullscreen state. |display_id| indicates the display where
97 // the window should be shown fullscreen; std::nullopt indicates
98 // that no display was specified, so the current display may be used.
99 virtual void SetFullscreen(bool fullscreen,
100 std::optional<FlutterEngineDisplayId> display_id);
101
102 // Returns |true| if this window is fullscreen, otherwise |false|.
103 virtual bool GetFullscreen() const;
104
105 // Given a window identifier, returns the window content size of the
106 // window.
107 static ActualWindowSize GetWindowContentSize(HWND hwnd);
108
109 // Returns the owner window, or nullptr if none.
110 HostWindow* GetOwnerWindow() const;
111
112 // This method is called when a dialog is created or destroyed.
113 // It walks the path of child windows to make sure that the right
114 // windows are enabled or disabled.
115 void UpdateModalStateLayer();
116
117 protected:
118 struct HostWindowInitializationParams {
119 WindowArchetype archetype;
120 DWORD window_style;
121 DWORD extended_window_style;
122 const BoxConstraints& box_constraints;
123 Rect const initial_window_rect;
124 LPCWSTR title;
125 std::optional<HWND> const& owner_window;
126 int nCmdShow = SW_SHOWNORMAL;
127 FlutterWindowsViewSizingDelegate* sizing_delegate = nullptr;
128 bool is_sized_to_content = false;
129 };
130
131 // Initialize the underlying native window and the Flutter view.
132 //
133 // See:
134 // - https://learn.microsoft.com/windows/win32/winmsg/window-styles
135 // - https://learn.microsoft.com/windows/win32/winmsg/extended-window-styles
136 void InitializeFlutterView(HostWindowInitializationParams const& params);
137
138 friend WindowManager;
139
140 // Information saved before going into fullscreen mode, used to restore the
141 // window afterwards.
142 struct SavedWindowInfo {
143 LONG style;
144 LONG ex_style;
145 RECT rect;
146 ActualWindowSize client_size;
147 int dpi;
148 HMONITOR monitor;
149 MONITORINFO monitor_info;
150 };
151
152 // Construct a host window.
153 //
154 // Derived classes should call InitializeFlutterView() after construction to
155 // set up the native window and the Flutter view.
156 HostWindow(WindowManager* window_manager, FlutterWindowsEngine* engine);
157
158 // Calculates the required window size, in physical coordinates, to
159 // accommodate the given |client_size|, in logical coordinates, constrained by
160 // optional |smallest| and |biggest|, for a window with the specified
161 // |window_style| and |extended_window_style|. If |owner_hwnd| is not null,
162 // the DPI of the display with the largest area of intersection with
163 // |owner_hwnd| is used for the calculation; otherwise, the primary display's
164 // DPI is used. The resulting size includes window borders, non-client areas,
165 // and drop shadows. On error, returns std::nullopt and logs an error message.
166 static std::optional<Size> GetWindowSizeForClientSize(
167 WindowsProcTable const& win32,
168 Size const& client_size,
169 std::optional<Size> smallest,
170 std::optional<Size> biggest,
171 DWORD window_style,
172 DWORD extended_window_style,
173 std::optional<HWND> const& owner_hwnd);
174
175 // Processes and routes salient window messages for mouse handling,
176 // size change and DPI. Delegates handling of these to member overloads that
177 // inheriting classes can handle.
178 virtual LRESULT HandleMessage(HWND hwnd,
179 UINT message,
180 WPARAM wparam,
181 LPARAM lparam);
182
183 // Sets the focus to the child view window of |window|.
184 static void FocusRootViewOf(HostWindow* window);
185
186 // Enables or disables mouse and keyboard input to this window and all its
187 // descendants.
188 void EnableRecursively(bool enable);
189
190 // Returns the first enabled descendant window. If the current window itself
191 // is enabled, returns the current window. If no window is enabled, returns
192 // `nullptr`.
193 HostWindow* FindFirstEnabledDescendant() const;
194
195 // Returns windows owned by this window.
196 std::vector<HostWindow*> GetOwnedWindows() const;
197
198 // Disables mouse and keyboard input to the window and all its descendants.
199 void DisableRecursively();
200
201 // OS callback called by message pump. Handles the WM_NCCREATE message which
202 // is passed when the non-client area is being created and enables automatic
203 // non-client DPI scaling so that the non-client area automatically
204 // responds to changes in DPI. Delegates other messages to the controller.
205 static LRESULT WndProc(HWND hwnd, UINT message, WPARAM wparam, LPARAM lparam);
206
207 // Controller for this window.
208 WindowManager* const window_manager_ = nullptr;
209
210 // The Flutter engine that owns this window.
211 FlutterWindowsEngine* engine_;
212
213 // Controller for the view hosted in this window. Value-initialized if the
214 // window is created from an existing top-level native window created by the
215 // runner.
216 std::unique_ptr<FlutterWindowsViewController> view_controller_;
217
218 // The window archetype.
219 WindowArchetype archetype_ = WindowArchetype::kRegular;
220
221 // Backing handle for this window.
222 HWND window_handle_;
223
224 // The constraints on the window's client area.
225 BoxConstraints box_constraints_;
226
227 // True while handling WM_DESTROY; used to detect in-progress destruction.
228 bool is_being_destroyed_ = false;
229
230 // Whether or not the window is currently in a fullscreen state.
231 bool is_fullscreen_ = false;
232
233 // Saved window information from before entering fullscreen mode.
234 SavedWindowInfo saved_window_info_;
235
236 // Used to mark a window as fullscreen.
237 Microsoft::WRL::ComPtr<ITaskbarList2> task_bar_list_;
238
239 FML_DISALLOW_COPY_AND_ASSIGN(HostWindow);
240 };
241
242 } // namespace flutter
243
244 #endif // FLUTTER_SHELL_PLATFORM_WINDOWS_HOST_WINDOW_H_
flutter::FlutterWindowsEngine
Definition: flutter_windows_engine.h:92
flutter::FlutterWindowsViewSizingDelegate
Definition: flutter_windows_view.h:34
flutter::HostWindow
Definition: host_window.h:29
flutter::HostWindow::task_bar_list_
Microsoft::WRL::ComPtr< ITaskbarList2 > task_bar_list_
Definition: host_window.h:237
flutter::HostWindow::GetWindowHandle
HWND GetWindowHandle() const
Definition: host_window.cc:352
flutter::HostWindow::box_constraints_
BoxConstraints box_constraints_
Definition: host_window.h:225
flutter::HostWindow::window_handle_
HWND window_handle_
Definition: host_window.h:222
flutter::HostWindow::archetype_
WindowArchetype archetype_
Definition: host_window.h:219
flutter::HostWindow::InitializeFlutterView
void InitializeFlutterView(HostWindowInitializationParams const &params)
Definition: host_window.cc:244
flutter::HostWindow::saved_window_info_
SavedWindowInfo saved_window_info_
Definition: host_window.h:234
flutter::HostWindow::FML_DISALLOW_COPY_AND_ASSIGN
FML_DISALLOW_COPY_AND_ASSIGN(HostWindow)
flutter::HostWindow::is_fullscreen_
bool is_fullscreen_
Definition: host_window.h:231
flutter::HostWindow::view_controller_
std::unique_ptr< FlutterWindowsViewController > view_controller_
Definition: host_window.h:216
flutter::HostWindow::DisableRecursively
void DisableRecursively()
Definition: host_window.cc:838
flutter::HostWindow::WndProc
static LRESULT WndProc(HWND hwnd, UINT message, WPARAM wparam, LPARAM lparam)
Definition: host_window.cc:367
flutter::HostWindow::GetOwnerWindow
HostWindow * GetOwnerWindow() const
Definition: host_window.cc:831
flutter::HostWindow::CreateTooltipWindow
static std::unique_ptr< HostWindow > CreateTooltipWindow(WindowManager *window_manager, FlutterWindowsEngine *engine, const WindowConstraints &preferred_constraints, GetWindowPositionCallback get_position_callback, HWND parent)
Definition: host_window.cc:229
flutter::HostWindow::GetWindowContentSize
static ActualWindowSize GetWindowContentSize(HWND hwnd)
Definition: host_window.cc:726
flutter::HostWindow::EnableRecursively
void EnableRecursively(bool enable)
Definition: host_window.cc:786
flutter::HostWindow::SetContentSize
void SetContentSize(const WindowSizeRequest &size)
Definition: host_window.cc:493
flutter::HostWindow::is_being_destroyed_
bool is_being_destroyed_
Definition: host_window.h:228
flutter::HostWindow::UpdateModalStateLayer
void UpdateModalStateLayer()
Definition: host_window.cc:847
flutter::HostWindow::FocusRootViewOf
static void FocusRootViewOf(HostWindow *window)
Definition: host_window.cc:360
flutter::HostWindow::GetFlutterViewWindowHandle
HWND GetFlutterViewWindowHandle() const
Definition: host_window.cc:356
flutter::HostWindow::engine_
FlutterWindowsEngine * engine_
Definition: host_window.h:211
flutter::HostWindow::~HostWindow
virtual ~HostWindow()
Definition: host_window.cc:326
flutter::HostWindow::GetThisFromHandle
static HostWindow * GetThisFromHandle(HWND hwnd)
Definition: host_window.cc:337
flutter::HostWindow::HostWindow
HostWindow(WindowManager *window_manager, FlutterWindowsEngine *engine)
Definition: host_window.cc:240
flutter::HostWindow::WindowManager
friend WindowManager
Definition: host_window.h:138
flutter::HostWindow::GetOwnedWindows
std::vector< HostWindow * > GetOwnedWindows() const
Definition: host_window.cc:808
flutter::HostWindow::GetFullscreen
virtual bool GetFullscreen() const
Definition: host_window.cc:722
flutter::HostWindow::SetConstraints
void SetConstraints(const WindowConstraints &constraints)
Definition: host_window.cc:535
flutter::HostWindow::SetFullscreen
virtual void SetFullscreen(bool fullscreen, std::optional< FlutterEngineDisplayId > display_id)
Definition: host_window.cc:576
flutter::HostWindow::HandleMessage
virtual LRESULT HandleMessage(HWND hwnd, UINT message, WPARAM wparam, LPARAM lparam)
Definition: host_window.cc:384
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:216
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:739
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:205
flutter::HostWindow::FindFirstEnabledDescendant
HostWindow * FindFirstEnabledDescendant() const
Definition: host_window.cc:794
flutter::HostWindow::window_manager_
WindowManager *const window_manager_
Definition: host_window.h:208
flutter::WindowManager
Definition: window_manager.h:108
flutter::WindowsProcTable
Definition: windows_proc_table.h:18
message
Win32Message message
Definition: keyboard_unittests.cc:137
flutter
Definition: accessibility_bridge_windows.cc:11
flutter::GetWindowPositionCallback
WindowRect *(* GetWindowPositionCallback)(const WindowSize &child_size, const WindowRect &parent_rect, const WindowRect &output_rect)
Definition: window_manager.h:69
flutter::WindowArchetype
WindowArchetype
Definition: windowing.h:11
flutter::WindowArchetype::kRegular
@ kRegular
flutter::ActualWindowSize
Definition: window_manager.h:95
flutter::HostWindow::HostWindowInitializationParams
Definition: host_window.h:118
flutter::HostWindow::HostWindowInitializationParams::title
LPCWSTR title
Definition: host_window.h:124
flutter::HostWindow::HostWindowInitializationParams::window_style
DWORD window_style
Definition: host_window.h:120
flutter::HostWindow::HostWindowInitializationParams::extended_window_style
DWORD extended_window_style
Definition: host_window.h:121
flutter::HostWindow::HostWindowInitializationParams::archetype
WindowArchetype archetype
Definition: host_window.h:119
flutter::HostWindow::HostWindowInitializationParams::nCmdShow
int nCmdShow
Definition: host_window.h:126
flutter::HostWindow::HostWindowInitializationParams::owner_window
std::optional< HWND > const & owner_window
Definition: host_window.h:125
flutter::HostWindow::HostWindowInitializationParams::initial_window_rect
Rect const initial_window_rect
Definition: host_window.h:123
flutter::HostWindow::HostWindowInitializationParams::sizing_delegate
FlutterWindowsViewSizingDelegate * sizing_delegate
Definition: host_window.h:127
flutter::HostWindow::HostWindowInitializationParams::is_sized_to_content
bool is_sized_to_content
Definition: host_window.h:128
flutter::HostWindow::HostWindowInitializationParams::box_constraints
const BoxConstraints & box_constraints
Definition: host_window.h:122
flutter::HostWindow::SavedWindowInfo
Definition: host_window.h:142
flutter::HostWindow::SavedWindowInfo::client_size
ActualWindowSize client_size
Definition: host_window.h:146
flutter::HostWindow::SavedWindowInfo::dpi
int dpi
Definition: host_window.h:147
flutter::HostWindow::SavedWindowInfo::ex_style
LONG ex_style
Definition: host_window.h:144
flutter::HostWindow::SavedWindowInfo::style
LONG style
Definition: host_window.h:143
flutter::HostWindow::SavedWindowInfo::monitor_info
MONITORINFO monitor_info
Definition: host_window.h:149
flutter::HostWindow::SavedWindowInfo::monitor
HMONITOR monitor
Definition: host_window.h:148
flutter::HostWindow::SavedWindowInfo::rect
RECT rect
Definition: host_window.h:145
flutter::WindowConstraints
Definition: window_manager.h:33
flutter::WindowSizeRequest
Definition: window_manager.h:26
window_manager.h
windowing.h