@@ -21,17 +21,29 @@ namespace record_windows {
21
21
return hr;
22
22
}
23
23
24
- void ErrorFromHR (HRESULT hr, MethodResult<EncodableValue>& result)
24
+ static void ErrorFromHR (HRESULT hr, MethodResult<EncodableValue>& result)
25
25
{
26
26
_com_error err (hr);
27
27
std::string errorText = Utf8FromUtf16 (err.ErrorMessage ());
28
28
29
29
result.Error (" Record" , " " , EncodableValue (errorText));
30
30
}
31
31
32
+ static HWND GetRootWindow (flutter::FlutterView* view) {
33
+ return ::GetAncestor (view->GetNativeWindow (), GA_ROOT);
34
+ }
35
+
32
36
// static, Register the plugin
33
37
void RecordWindowsPlugin::RegisterWithRegistrar (flutter::PluginRegistrarWindows* registrar) {
34
- auto plugin = std::make_unique<RecordWindowsPlugin>();
38
+ auto plugin = std::make_unique<RecordWindowsPlugin>(
39
+ [registrar](auto delegate) {
40
+ return registrar->RegisterTopLevelWindowProcDelegate (delegate);
41
+ },
42
+ [registrar](auto proc_id) {
43
+ registrar->UnregisterTopLevelWindowProcDelegate (proc_id);
44
+ },
45
+ [registrar] { return GetRootWindow (registrar->GetView ()); }
46
+ );
35
47
36
48
m_binaryMessenger = registrar->messenger ();
37
49
@@ -48,14 +60,56 @@ namespace record_windows {
48
60
registrar->AddPlugin (std::move (plugin));
49
61
}
50
62
51
- RecordWindowsPlugin::RecordWindowsPlugin () {
63
+ // static
64
+ std::queue<std::function<void ()>> RecordWindowsPlugin::callbacks{};
65
+
66
+ // static
67
+ FlutterRootWindowProvider RecordWindowsPlugin::get_root_window{};
68
+
69
+ // static
70
+ void RecordWindowsPlugin::RunOnMainThread (std::function<void ()> callback) {
71
+ callbacks.push (callback);
72
+ PostMessage (get_root_window (), WM_RUN_DELEGATE, 0 , 0 );
73
+ }
74
+
75
+ RecordWindowsPlugin::RecordWindowsPlugin (
76
+ WindowProcDelegateRegistrator registrator,
77
+ WindowProcDelegateUnregistrator unregistrator,
78
+ FlutterRootWindowProvider window_provider
79
+ ): m_win_proc_delegate_registrator(registrator),
80
+ m_win_proc_delegate_unregistrator (unregistrator) {
81
+
82
+ get_root_window = std::move (window_provider);
83
+
84
+ m_window_proc_id = m_win_proc_delegate_registrator (
85
+ [this ](HWND hwnd, UINT message, WPARAM wparam, LPARAM lparam) {
86
+ return HandleWindowProc (hwnd, message, wparam, lparam);
87
+ }
88
+ );
52
89
}
53
90
54
91
RecordWindowsPlugin::~RecordWindowsPlugin () {
55
92
for (const auto & [recorderId, recorder] : m_recorders)
56
93
{
57
94
recorder->Dispose ();
58
95
}
96
+
97
+ m_win_proc_delegate_unregistrator (m_window_proc_id);
98
+ }
99
+
100
+ std::optional<LRESULT> RecordWindowsPlugin::HandleWindowProc (HWND hwnd,
101
+ UINT message,
102
+ WPARAM wparam,
103
+ LPARAM lparam) {
104
+ std::optional<LRESULT> result;
105
+ switch (message) {
106
+ case WM_RUN_DELEGATE:
107
+ callbacks.front ()();
108
+ callbacks.pop ();
109
+ result = 0 ;
110
+ break ;
111
+ }
112
+ return result;
59
113
}
60
114
61
115
// Called when a method is called on this plugin's channel from Dart.
@@ -190,7 +244,8 @@ namespace record_windows {
190
244
EncodableMap ({
191
245
{EncodableValue (" current" ), EncodableValue (amp[" current" ])},
192
246
{EncodableValue (" max" ), EncodableValue (amp[" max" ])}
193
- }))
247
+ }
248
+ ))
194
249
);
195
250
}
196
251
else if (method_call.method_name ().compare (" isEncoderSupported" ) == 0 )
0 commit comments