Flutter Windows Embedder
host_window_satellite.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_SATELLITE_H_
6 #define FLUTTER_SHELL_PLATFORM_WINDOWS_HOST_WINDOW_SATELLITE_H_
7 
8 #include <cstdint>
9 #include <optional>
10 
13 
14 namespace flutter {
15 
16 // A satellite window is an auxiliary top-level window that is owned by, and
17 // follows the movement of, another window.
18 //
19 // Unlike a tooltip or a popup, a satellite is decorated and activatable. Its
20 // position callback runs only once, for the initial placement, after the first
21 // frame has been rendered and before the window is shown; afterwards the
22 // satellite keeps whatever offset it has from its parent's client area, moving
23 // with the parent. The offset is kept in the parent's logical pixels, so it
24 // scales when the parent moves to a monitor with a different DPI. A satellite
25 // cannot be minimized on its own, and may be reparented at runtime without
26 // changing its screen position.
28  public:
29  // Creates a satellite window anchored to |parent|.
30  HostWindowSatellite(WindowManager* window_manager,
31  FlutterWindowsEngine* engine,
32  const WindowSizeRequest& preferred_size,
33  const BoxConstraints& constraints,
34  GetWindowPositionCallback get_position_callback,
35  HWND parent,
36  LPCWSTR title,
37  bool sized_to_content,
38  bool resizable);
39 
40  ~HostWindowSatellite() override;
41 
42  // Called by the window this satellite is anchored to when it has moved,
43  // resized, or changed its frame. Moves the satellite so that it retains its
44  // offset from the parent's client area, scaled by the parent's current DPI.
45  void OnParentMoved();
46 
47  // Changes the window that this satellite is anchored to. The satellite keeps
48  // its current screen position; only future movement deltas are computed
49  // against |new_parent|.
50  void SetSatelliteParent(HWND new_parent);
51 
52  protected:
53  LRESULT HandleMessage(HWND hwnd,
54  UINT message,
55  WPARAM wparam,
56  LPARAM lparam) override;
57 
58  // HostWindow:
59  //
60  // Runs the initial placement, unless it has already run, before the window
61  // is shown for the first time.
62  void OnFirstFrame() override;
63 
64  // HostWindowSized:
65  void ApplyContentSize(int32_t physical_width,
66  int32_t physical_height) override;
67 
68  private:
69  // Returns the window style used for a satellite window. Satellites are
70  // decorated and activatable, but never minimizable.
71  static DWORD GetWindowStyleForSatellite(bool resizable);
72 
73  // Returns the rectangle the satellite window is created with.
74  //
75  // The size is derived from |preferred_size| (or, when |sized_to_content| is
76  // true, from the smallest size allowed by |constraints|). The origin is left
77  // at CW_USEDEFAULT: the satellite is placed by the positioner once its
78  // content has been rendered, while the window is still hidden.
79  static Rect GetInitialRect(FlutterWindowsEngine* engine,
80  const WindowSizeRequest& preferred_size,
81  const BoxConstraints& constraints,
82  HWND parent,
83  bool sized_to_content,
84  bool resizable);
85 
86  // FlutterWindowsViewSizingDelegate:
87  //
88  // Overridden to report the work area of the monitor that the parent window
89  // is on. Until the initial placement runs, the satellite itself may not yet
90  // be on the monitor it will end up on.
91  WindowRect GetWorkArea() const override;
92 
93  // Invokes |get_position_callback_| with the satellite's current frame size
94  // and moves the window to the returned rectangle. Does nothing after the
95  // first successful call.
96  void ApplyInitialPosition();
97 
98  // Computes the placement rectangle for a satellite of |window_size| anchored
99  // to |parent_|, by invoking |get_position_callback_| in |isolate_|. Returns
100  // std::nullopt if |get_position_callback_| is null.
101  std::optional<WindowRect> ComputePosition(
102  const WindowSize& window_size) const;
103 
104  // Returns the origin of |parent_|'s client area, in screen coordinates, or
105  // std::nullopt if |parent_| is minimized.
106  std::optional<POINT> GetParentClientOrigin() const;
107 
108  // Returns the ratio of |parent_|'s DPI to the default DPI.
109  double GetParentScaleFactor() const;
110 
111  // Records the satellite's current offset from |parent_|'s client area.
112  void UpdateParentOffset();
113 
114  GetWindowPositionCallback get_position_callback_;
115 
116  // The window this satellite is anchored to.
117  HWND parent_;
118 
119  Isolate isolate_;
120 
121  // The offset from the origin of |parent_|'s client area to the top-left
122  // corner of this satellite's window rectangle, in |parent_|'s logical pixels.
123  double parent_offset_x_ = 0;
124  double parent_offset_y_ = 0;
125 
126  // Whether the satellite is currently being moved to follow |parent_|. Moves
127  // made while this is set do not update the offset from the parent.
128  bool is_following_parent_ = false;
129 
130  // Whether the user is currently moving or resizing the satellite.
131  bool is_in_move_size_loop_ = false;
132 
133  // Whether the one-shot initial placement has already run.
134  bool initial_position_applied_ = false;
135 };
136 
137 } // namespace flutter
138 
139 #endif // FLUTTER_SHELL_PLATFORM_WINDOWS_HOST_WINDOW_SATELLITE_H_
HostWindowSatellite(WindowManager *window_manager, FlutterWindowsEngine *engine, const WindowSizeRequest &preferred_size, const BoxConstraints &constraints, GetWindowPositionCallback get_position_callback, HWND parent, LPCWSTR title, bool sized_to_content, bool resizable)
LRESULT HandleMessage(HWND hwnd, UINT message, WPARAM wparam, LPARAM lparam) override
void ApplyContentSize(int32_t physical_width, int32_t physical_height) override
void SetSatelliteParent(HWND new_parent)
Win32Message message
void(* GetWindowPositionCallback)(const WindowSize &child_size, const WindowRect &parent_rect, const WindowRect &display_rect, WindowRect &output_rect)