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 FlutterWindowsView;
23 class FlutterWindowsViewController;
24 
25 // A Win32 window that hosts a |FlutterWindow| in its client area.
26 class HostWindow {
27  public:
28  virtual ~HostWindow();
29 
30  // Creates a native Win32 window with a child view confined to its client
31  // area. |controller| is a pointer to the controller that manages the
32  // |HostWindow|. |engine| is a pointer to the engine that manages
33  // the controller. On success, a valid window handle can be retrieved
34  // via |HostWindow::GetWindowHandle|. |nullptr| will be returned
35  // on failure.
36  static std::unique_ptr<HostWindow> CreateRegularWindow(
37  WindowManager* controller,
38  FlutterWindowsEngine* engine,
39  const WindowSizeRequest& preferred_size,
40  const WindowConstraints& preferred_constraints,
41  LPCWSTR title);
42 
43  // Returns the instance pointer for |hwnd| or nullptr if invalid.
44  static HostWindow* GetThisFromHandle(HWND hwnd);
45 
46  // Returns the backing window handle, or nullptr if the native window is not
47  // created or has already been destroyed.
48  HWND GetWindowHandle() const;
49 
50  // Resizes the window to accommodate a client area of the given
51  // |size|. If the size does not satisfy the constraints, the window will be
52  // resized to the minimum or maximum size as appropriate.
53  void SetContentSize(const WindowSizeRequest& size);
54 
55  // Sets the constaints on the client area of the window.
56  // If the current window size does not satisfy the new constraints,
57  // the window will be resized to satisy thew new constraints.
58  void SetConstraints(const WindowConstraints& constraints);
59 
60  // Set the fullscreen state. |display_id| indicates the display where
61  // the window should be shown fullscreen; std::nullopt indicates
62  // that no display was specified, so the current display may be used.
63  void SetFullscreen(bool fullscreen,
64  std::optional<FlutterEngineDisplayId> display_id);
65 
66  // Returns |true| if this window is fullscreen, otherwise |false|.
67  bool GetFullscreen() const;
68 
69  // Given a window identifier, returns the window content size of the
70  // window.
71  static ActualWindowSize GetWindowContentSize(HWND hwnd);
72 
73  private:
74  friend WindowManager;
75 
76  // Information saved before going into fullscreen mode, used to restore the
77  // window afterwards.
78  struct SavedWindowInfo {
79  LONG style;
80  LONG ex_style;
81  RECT rect;
82  ActualWindowSize client_size;
83  int dpi;
84  HMONITOR monitor;
85  MONITORINFO monitor_info;
86  };
87 
88  HostWindow(WindowManager* controller,
89  FlutterWindowsEngine* engine,
90  WindowArchetype archetype,
91  std::unique_ptr<FlutterWindowsViewController> view_controller,
92  const BoxConstraints& constraints,
93  HWND hwnd);
94 
95  // Sets the focus to the child view window of |window|.
96  static void FocusViewOf(HostWindow* window);
97 
98  // OS callback called by message pump. Handles the WM_NCCREATE message which
99  // is passed when the non-client area is being created and enables automatic
100  // non-client DPI scaling so that the non-client area automatically
101  // responds to changes in DPI. Delegates other messages to the controller.
102  static LRESULT WndProc(HWND hwnd, UINT message, WPARAM wparam, LPARAM lparam);
103 
104  // Processes and routes salient window messages for mouse handling,
105  // size change and DPI. Delegates handling of these to member overloads that
106  // inheriting classes can handle.
107  LRESULT HandleMessage(HWND hwnd, UINT message, WPARAM wparam, LPARAM lparam);
108 
109  // Controller for this window.
110  WindowManager* const window_manager_ = nullptr;
111 
112  // The Flutter engine that owns this window.
113  FlutterWindowsEngine* engine_;
114 
115  // Controller for the view hosted in this window. Value-initialized if the
116  // window is created from an existing top-level native window created by the
117  // runner.
118  std::unique_ptr<FlutterWindowsViewController> view_controller_;
119 
120  // The window archetype.
122 
123  // Backing handle for this window.
124  HWND window_handle_ = nullptr;
125 
126  // The constraints on the window's client area.
127  BoxConstraints box_constraints_;
128 
129  // Whether or not the window is currently in a fullscreen state.
130  bool is_fullscreen_ = false;
131 
132  // Saved window information from before entering fullscreen mode.
133  SavedWindowInfo saved_window_info_;
134 
135  // Used to mark a window as fullscreen.
136  Microsoft::WRL::ComPtr<ITaskbarList2> task_bar_list_;
137 
138  FML_DISALLOW_COPY_AND_ASSIGN(HostWindow);
139 };
140 
141 } // namespace flutter
142 
143 #endif // FLUTTER_SHELL_PLATFORM_WINDOWS_HOST_WINDOW_H_
HWND GetWindowHandle() const
Definition: host_window.cc:400
static std::unique_ptr< HostWindow > CreateRegularWindow(WindowManager *controller, FlutterWindowsEngine *engine, const WindowSizeRequest &preferred_size, const WindowConstraints &preferred_constraints, LPCWSTR title)
Definition: host_window.cc:257
static ActualWindowSize GetWindowContentSize(HWND hwnd)
Definition: host_window.cc:752
void SetContentSize(const WindowSizeRequest &size)
Definition: host_window.cc:519
static HostWindow * GetThisFromHandle(HWND hwnd)
Definition: host_window.cc:396
bool GetFullscreen() const
Definition: host_window.cc:748
void SetConstraints(const WindowConstraints &constraints)
Definition: host_window.cc:561
void SetFullscreen(bool fullscreen, std::optional< FlutterEngineDisplayId > display_id)
Definition: host_window.cc:602
Win32Message message
WindowArchetype
Definition: windowing.h:13