@@ -17,16 +17,13 @@ constexpr wchar_t kWindowClassName[] = L"FLUTTER_HOST_WINDOW";
1717
1818// RAII wrapper for global Win32 ATOMs.
1919struct AtomRAII {
20- AtomRAII (wchar_t const * name) : atom(GlobalAddAtom(name)) {}
20+ explicit AtomRAII (wchar_t const * name) : atom(GlobalAddAtom(name)) {}
2121 ~AtomRAII () { GlobalDeleteAtom (atom); }
2222 ATOM const atom;
2323};
2424
25- // Atom representing a window property that stores a pointer to this host
26- // window. This property serves as an alternative way to access the window in
27- // |FlutterHostWindow::GetThisFromHandle| for windows created from existing
28- // views, since the `GWLP_USERDATA` of such windows may point to something other
29- // than a |FlutterHostWindow|.
25+ // Atom used as the identifier for a window property that stores a pointer to a
26+ // |FlutterHostWindow| instance.
3027AtomRAII const kWindowPropAtom (kWindowClassName );
3128
3229// Clamps |size| to the size of the virtual screen. Both the parameter and
@@ -226,7 +223,7 @@ bool IsClassRegistered(LPCWSTR class_name) {
226223 0 ;
227224}
228225
229- // Convert std::string to std::wstring.
226+ // Converts std::string to std::wstring.
230227std::wstring StringToWstring (std::string_view str) {
231228 if (str.empty ()) {
232229 return {};
@@ -252,7 +249,7 @@ std::wstring StringToWstring(std::string_view str) {
252249#define DWMWA_USE_IMMERSIVE_DARK_MODE 20
253250#endif
254251
255- // Update the window frame's theme to match the system theme.
252+ // Updates the window frame's theme to match the system theme.
256253void UpdateTheme (HWND window) {
257254 // Registry key for app theme preference.
258255 const wchar_t kGetPreferredBrightnessRegKey [] =
@@ -275,6 +272,31 @@ void UpdateTheme(HWND window) {
275272 }
276273}
277274
275+ // Associates |instance| with the window |hwnd| as a window property.
276+ // Can be retrieved later using GetInstanceProperty.
277+ // Logs an error if setting the property fails.
278+ void SetInstanceProperty (HWND hwnd, flutter::FlutterHostWindow* instance) {
279+ if (!SetProp (hwnd, MAKEINTATOM (kWindowPropAtom .atom ), instance)) {
280+ FML_LOG (ERROR) << " Failed to set up instance entry in the property list: "
281+ << GetLastErrorAsString ();
282+ }
283+ }
284+
285+ // Retrieves the instance pointer set with SetInstanceProperty, or returns
286+ // nullptr if the property was not set.
287+ flutter::FlutterHostWindow* GetInstanceProperty (HWND hwnd) {
288+ return reinterpret_cast <flutter::FlutterHostWindow*>(
289+ GetProp (hwnd, MAKEINTATOM (kWindowPropAtom .atom )));
290+ }
291+
292+ // Removes the instance property associated with |hwnd| previously set with
293+ // SetInstanceProperty. Logs an error if the property is not found.
294+ void RemoveInstanceProperty (HWND hwnd) {
295+ if (!RemoveProp (hwnd, MAKEINTATOM (kWindowPropAtom .atom ))) {
296+ FML_LOG (ERROR) << " Failed to locate instance entry in the property list" ;
297+ }
298+ }
299+
278300} // namespace
279301
280302namespace flutter {
@@ -440,16 +462,14 @@ FlutterHostWindow::FlutterHostWindow(FlutterHostWindowController* controller,
440462 HWND hwnd,
441463 FlutterWindowsView* view)
442464 : window_controller_(controller), window_handle_(hwnd) {
443- if (!SetProp (hwnd, MAKEINTATOM (kWindowPropAtom .atom ), this )) {
444- FML_LOG (ERROR) << " Failed to set up entry in the window property list" ;
445- return ;
446- }
465+ SetInstanceProperty (hwnd, this );
447466 child_content_ = view->GetWindowHandle ();
448467}
449468
450469FlutterHostWindow::~FlutterHostWindow () {
451- HWND const hwnd = window_handle_;
452- window_handle_ = nullptr ;
470+ RemoveInstanceProperty (window_handle_);
471+ HWND const hwnd = std::exchange (window_handle_, nullptr );
472+
453473 if (view_controller_) {
454474 DestroyWindow (hwnd);
455475 // Unregister the window class. Fail silently if other windows are still
@@ -462,18 +482,7 @@ FlutterHostWindow::~FlutterHostWindow() {
462482}
463483
464484FlutterHostWindow* FlutterHostWindow::GetThisFromHandle (HWND hwnd) {
465- // For native windows created by the runner, retrieve the instance pointer
466- // from a window property.
467- if (HANDLE const data = GetProp (hwnd, MAKEINTATOM (kWindowPropAtom .atom ))) {
468- return reinterpret_cast <FlutterHostWindow*>(data);
469- }
470- // Otherwise, retrieve the instance pointer from the window's user data.
471- return reinterpret_cast <FlutterHostWindow*>(
472- GetWindowLongPtr (hwnd, GWLP_USERDATA));
473- }
474-
475- bool FlutterHostWindow::HasThisAsProperty (HWND hwnd) {
476- return GetProp (hwnd, MAKEINTATOM (kWindowPropAtom .atom )) != nullptr ;
485+ return GetInstanceProperty (hwnd);
477486}
478487
479488HWND FlutterHostWindow::GetWindowHandle () const {
@@ -496,10 +505,9 @@ LRESULT FlutterHostWindow::WndProc(HWND hwnd,
496505 LPARAM lparam) {
497506 if (message == WM_NCCREATE) {
498507 auto * const create_struct = reinterpret_cast <CREATESTRUCT*>(lparam);
499- SetWindowLongPtr (hwnd, GWLP_USERDATA,
500- reinterpret_cast <LONG_PTR>(create_struct->lpCreateParams ));
501508 auto * const window =
502509 static_cast <FlutterHostWindow*>(create_struct->lpCreateParams );
510+ SetInstanceProperty (hwnd, window);
503511 window->window_handle_ = hwnd;
504512
505513 EnableFullDpiSupportIfAvailable (hwnd);
0 commit comments