Avi Drissman | 3e1a26c | 2022-09-15 20:26:03 | [diff] [blame] | 1 | // Copyright 2015 The Chromium Authors |
robliao | 305070a07 | 2015-05-01 23:47:19 | [diff] [blame] | 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 UI_GFX_WIN_SINGLETON_HWND_OBSERVER_H_ |
| 6 | #define UI_GFX_WIN_SINGLETON_HWND_OBSERVER_H_ |
| 7 | |
| 8 | #include <windows.h> |
| 9 | |
Avi Drissman | c149c16 | 2023-01-12 02:16:59 | [diff] [blame] | 10 | #include "base/functional/callback.h" |
robliao | 305070a07 | 2015-05-01 23:47:19 | [diff] [blame] | 11 | #include "ui/gfx/gfx_export.h" |
| 12 | |
| 13 | namespace gfx { |
| 14 | |
| 15 | class SingletonHwnd; |
| 16 | |
| 17 | // Singleton lifetime management is tricky. This observer handles the correct |
| 18 | // cleanup if either the SingletonHwnd or forwarded object is destroyed first. |
Tommy Steimel | 2d655f6 | 2019-01-11 06:25:44 | [diff] [blame] | 19 | // Note that if you want to register a hot key on the SingletonHwnd, you need to |
| 20 | // use a SingletonHwndHotKeyObserver instead for each hot key. |
robliao | 305070a07 | 2015-05-01 23:47:19 | [diff] [blame] | 21 | class GFX_EXPORT SingletonHwndObserver { |
| 22 | public: |
Sylvain Defresne | 1361644 | 2019-10-01 17:12:26 | [diff] [blame] | 23 | using WndProc = base::RepeatingCallback<void(HWND, UINT, WPARAM, LPARAM)>; |
robliao | 305070a07 | 2015-05-01 23:47:19 | [diff] [blame] | 24 | |
| 25 | explicit SingletonHwndObserver(const WndProc& wnd_proc); |
Peter Boström | c8c1235 | 2021-09-21 23:37:15 | [diff] [blame] | 26 | |
| 27 | SingletonHwndObserver(const SingletonHwndObserver&) = delete; |
| 28 | SingletonHwndObserver& operator=(const SingletonHwndObserver&) = delete; |
| 29 | |
robliao | 305070a07 | 2015-05-01 23:47:19 | [diff] [blame] | 30 | ~SingletonHwndObserver(); |
| 31 | |
| 32 | private: |
| 33 | friend class SingletonHwnd; |
| 34 | |
| 35 | void ClearWndProc(); |
| 36 | void OnWndProc(HWND hwnd, UINT message, WPARAM wparam, LPARAM lparam); |
| 37 | |
| 38 | WndProc wnd_proc_; |
robliao | 305070a07 | 2015-05-01 23:47:19 | [diff] [blame] | 39 | }; |
| 40 | |
| 41 | } // namespace gfx |
| 42 | |
| 43 | #endif // UI_GFX_WIN_SINGLETON_HWND_OBSERVER_H_ |