Flutter Engine
The Flutter Engine
plugin_registrar_windows.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_CLIENT_WRAPPER_INCLUDE_FLUTTER_PLUGIN_REGISTRAR_WINDOWS_H_
6#define FLUTTER_SHELL_PLATFORM_WINDOWS_CLIENT_WRAPPER_INCLUDE_FLUTTER_PLUGIN_REGISTRAR_WINDOWS_H_
7
8#include <flutter_windows.h>
9#include <windows.h>
10
11#include <memory>
12#include <optional>
13
14#include "flutter_view.h"
15#include "plugin_registrar.h"
16
17namespace flutter {
18
19// A delegate callback for WindowProc delegation.
20//
21// Implementations should return a value only if they have handled the message
22// and want to stop all further handling.
23using WindowProcDelegate = std::function<std::optional<
24 LRESULT>(HWND hwnd, UINT message, WPARAM wparam, LPARAM lparam)>;
25
26// An extension to PluginRegistrar providing access to Windows-specific
27// functionality.
29 public:
30 // Creates a new PluginRegistrar. |core_registrar| and the messenger it
31 // provides must remain valid as long as this object exists.
34 : PluginRegistrar(core_registrar) {
35 FlutterDesktopViewRef implicit_view =
37 if (implicit_view) {
38 implicit_view_ = std::make_unique<FlutterView>(implicit_view);
39 }
40 }
41
43 // Must be the first call.
45 // Explicitly cleared to facilitate destruction order testing.
46 implicit_view_.reset();
47 }
48
49 // Prevent copying.
52
53 // Returns the implicit view, or nullptr if there is no implicit view.
54 //
55 // See:
56 // https://api.flutter.dev/flutter/dart-ui/PlatformDispatcher/implicitView.html
57 //
58 // DEPRECATED: Use |GetViewById| instead.
59 FlutterView* GetView() { return implicit_view_.get(); }
60
61 // Returns the view with the given ID, or nullptr if the view does not exist.
62 //
63 // Destroying the shared pointer destroys the reference to the view; it does
64 // not destroy the underlying view.
65 std::shared_ptr<FlutterView> GetViewById(FlutterViewId view_id) const {
68 if (!view) {
69 return nullptr;
70 }
71
72 return std::make_shared<FlutterView>(view);
73 }
74
75 // Registers |delegate| to receive WindowProc callbacks for the top-level
76 // window containing this Flutter instance. Returns an ID that can be used to
77 // unregister the handler.
78 //
79 // Delegates are not guaranteed to be called:
80 // - The application may choose not to delegate WindowProc calls.
81 // - If multiple plugins are registered, the first one that returns a value
82 // from the delegate message will "win", and others will not be called.
83 // The order of delegate calls is not defined.
84 //
85 // Delegates should be implemented as narrowly as possible, only returning
86 // a value in cases where it's important that other delegates not run, to
87 // minimize the chances of conflicts between plugins.
89 if (window_proc_delegates_.empty()) {
91 registrar(), PluginRegistrarWindows::OnTopLevelWindowProc, this);
92 }
93 int delegate_id = next_window_proc_delegate_id_++;
94 window_proc_delegates_.emplace(delegate_id, std::move(delegate));
95 return delegate_id;
96 }
97
98 // Unregisters a previously registered delegate.
100 window_proc_delegates_.erase(proc_id);
101 if (window_proc_delegates_.empty()) {
103 registrar(), PluginRegistrarWindows::OnTopLevelWindowProc);
104 }
105 }
106
107 private:
108 // A FlutterDesktopWindowProcCallback implementation that forwards back to
109 // a PluginRegistarWindows instance provided as |user_data|.
110 static bool OnTopLevelWindowProc(HWND hwnd,
112 WPARAM wparam,
113 LPARAM lparam,
114 void* user_data,
115 LRESULT* result) {
116 const auto* registrar = static_cast<PluginRegistrarWindows*>(user_data);
117 std::optional optional_result = registrar->CallTopLevelWindowProcDelegates(
118 hwnd, message, wparam, lparam);
119 if (optional_result) {
120 *result = *optional_result;
121 }
122 return optional_result.has_value();
123 }
124
125 std::optional<LRESULT> CallTopLevelWindowProcDelegates(HWND hwnd,
127 WPARAM wparam,
128 LPARAM lparam) const {
129 std::optional<LRESULT> result;
130 for (const auto& pair : window_proc_delegates_) {
131 result = pair.second(hwnd, message, wparam, lparam);
132 // Stop as soon as any delegate indicates that it has handled the message.
133 if (result) {
134 break;
135 }
136 }
137 return result;
138 }
139
140 // The associated FlutterView, if any.
141 std::unique_ptr<FlutterView> implicit_view_;
142
143 // The next ID to return from RegisterWindowProcDelegate.
144 int next_window_proc_delegate_id_ = 1;
145
146 std::map<int, WindowProcDelegate> window_proc_delegates_;
147};
148
149} // namespace flutter
150
151#endif // FLUTTER_SHELL_PLATFORM_WINDOWS_CLIENT_WRAPPER_INCLUDE_FLUTTER_PLUGIN_REGISTRAR_WINDOWS_H_
int RegisterTopLevelWindowProcDelegate(WindowProcDelegate delegate)
std::shared_ptr< FlutterView > GetViewById(FlutterViewId view_id) const
PluginRegistrarWindows & operator=(PluginRegistrarWindows const &)=delete
void UnregisterTopLevelWindowProcDelegate(int proc_id)
PluginRegistrarWindows(FlutterDesktopPluginRegistrarRef core_registrar)
PluginRegistrarWindows(PluginRegistrarWindows const &)=delete
FlutterDesktopPluginRegistrarRef registrar() const
GAsyncResult * result
struct FlutterDesktopView * FlutterDesktopViewRef
Dart_NativeFunction function
Definition: fuchsia.cc:51
Win32Message message
std::function< std::optional< LRESULT >(HWND hwnd, UINT message, WPARAM wparam, LPARAM lparam)> WindowProcDelegate
int64_t FlutterViewId
Definition: flutter_view.h:13
void FlutterDesktopPluginRegistrarUnregisterTopLevelWindowProcDelegate(FlutterDesktopPluginRegistrarRef registrar, FlutterDesktopWindowProcCallback delegate)
void FlutterDesktopPluginRegistrarRegisterTopLevelWindowProcDelegate(FlutterDesktopPluginRegistrarRef registrar, FlutterDesktopWindowProcCallback delegate, void *user_data)
FlutterDesktopViewRef FlutterDesktopPluginRegistrarGetView(FlutterDesktopPluginRegistrarRef controller)
FlutterDesktopViewRef FlutterDesktopPluginRegistrarGetViewById(FlutterDesktopPluginRegistrarRef controller, FlutterDesktopViewId view_id)
void * user_data
LONG_PTR LRESULT
Definition: windows_types.h:61
unsigned int UINT
Definition: windows_types.h:32
LONG_PTR LPARAM
Definition: windows_types.h:60
UINT_PTR WPARAM
Definition: windows_types.h:59