Skip to content
This repository was archived by the owner on Feb 25, 2025. It is now read-only.

Commit c3a423f

Browse files
committed
[Windows] Allow plugins to get views
1 parent 090d45c commit c3a423f

File tree

8 files changed

+114
-9
lines changed

8 files changed

+114
-9
lines changed

shell/platform/common/client_wrapper/include/flutter/plugin_registrar.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ class PluginRegistrar {
5454
void AddPlugin(std::unique_ptr<Plugin> plugin);
5555

5656
protected:
57-
FlutterDesktopPluginRegistrarRef registrar() { return registrar_; }
57+
FlutterDesktopPluginRegistrarRef registrar() const { return registrar_; }
5858

5959
// Destroys all owned plugins. Subclasses should call this at the beginning of
6060
// their destructors to prevent the possibility of an owned plugin trying to

shell/platform/windows/client_wrapper/include/flutter/flutter_view.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ class FlutterView {
1717
public:
1818
explicit FlutterView(FlutterDesktopViewRef view) : view_(view) {}
1919

20+
// Destroys this reference to the view. The underlying view is not destroyed.
2021
virtual ~FlutterView() = default;
2122

2223
// Prevent copying.

shell/platform/windows/client_wrapper/include/flutter/plugin_registrar_windows.h

Lines changed: 19 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -32,22 +32,37 @@ class PluginRegistrarWindows : public PluginRegistrar {
3232
explicit PluginRegistrarWindows(
3333
FlutterDesktopPluginRegistrarRef core_registrar)
3434
: PluginRegistrar(core_registrar) {
35-
view_ = std::make_unique<FlutterView>(
35+
implicit_view_ = std::make_unique<FlutterView>(
3636
FlutterDesktopPluginRegistrarGetView(core_registrar));
3737
}
3838

3939
virtual ~PluginRegistrarWindows() {
4040
// Must be the first call.
4141
ClearPlugins();
4242
// Explicitly cleared to facilitate destruction order testing.
43-
view_.reset();
43+
implicit_view_.reset();
4444
}
4545

4646
// Prevent copying.
4747
PluginRegistrarWindows(PluginRegistrarWindows const&) = delete;
4848
PluginRegistrarWindows& operator=(PluginRegistrarWindows const&) = delete;
4949

50-
FlutterView* GetView() { return view_.get(); }
50+
// Returns the implicit view, or nullptr if there is no implicit view.
51+
//
52+
// See:
53+
// https://api.flutter.dev/flutter/dart-ui/PlatformDispatcher/implicitView.html
54+
//
55+
// DEPRECATED: Use |GetViewById| instead.
56+
FlutterView* GetView() { return implicit_view_.get(); }
57+
58+
// Returns the view with the given ID, or nullptr if the view does not exist.
59+
//
60+
// Destroying the shared pointer destroys the reference to the view; it does
61+
// not destroy the underlying view.
62+
std::shared_ptr<FlutterView> GetViewById(FlutterViewId view_id) const {
63+
return std::make_shared<FlutterView>(
64+
FlutterDesktopPluginRegistrarGetViewById(registrar(), view_id));
65+
}
5166

5267
// Registers |delegate| to receive WindowProc callbacks for the top-level
5368
// window containing this Flutter instance. Returns an ID that can be used to
@@ -115,7 +130,7 @@ class PluginRegistrarWindows : public PluginRegistrar {
115130
}
116131

117132
// The associated FlutterView, if any.
118-
std::unique_ptr<FlutterView> view_;
133+
std::unique_ptr<FlutterView> implicit_view_;
119134

120135
// The next ID to return from RegisterWindowProcDelegate.
121136
int next_window_proc_delegate_id_ = 1;

shell/platform/windows/client_wrapper/plugin_registrar_windows_unittests.cc

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,15 @@ TEST(PluginRegistrarWindowsTest, GetView) {
7373
EXPECT_NE(registrar.GetView(), nullptr);
7474
}
7575

76+
TEST(PluginRegistrarWindowsTest, GetViewById) {
77+
testing::ScopedStubFlutterWindowsApi scoped_api_stub(
78+
std::make_unique<TestWindowsApi>());
79+
auto test_api = static_cast<TestWindowsApi*>(scoped_api_stub.stub());
80+
PluginRegistrarWindows registrar(
81+
reinterpret_cast<FlutterDesktopPluginRegistrarRef>(1));
82+
EXPECT_NE(registrar.GetViewById(123), nullptr);
83+
}
84+
7685
// Tests that the registrar runs plugin destructors before its own teardown.
7786
TEST(PluginRegistrarWindowsTest, PluginDestroyedBeforeRegistrar) {
7887
auto dummy_registrar_handle =

shell/platform/windows/client_wrapper/testing/stub_flutter_windows_api.cc

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -198,6 +198,13 @@ FlutterDesktopViewRef FlutterDesktopPluginRegistrarGetView(
198198
return reinterpret_cast<FlutterDesktopViewRef>(1);
199199
}
200200

201+
FlutterDesktopViewRef FlutterDesktopPluginRegistrarGetViewById(
202+
FlutterDesktopPluginRegistrarRef controller,
203+
FlutterDesktopViewId view_id) {
204+
// The stub ignores this, so just return an arbitrary non-zero value.
205+
return reinterpret_cast<FlutterDesktopViewRef>(1);
206+
}
207+
201208
void FlutterDesktopPluginRegistrarRegisterTopLevelWindowProcDelegate(
202209
FlutterDesktopPluginRegistrarRef registrar,
203210
FlutterDesktopWindowProcCallback delegate,

shell/platform/windows/flutter_windows.cc

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -261,12 +261,15 @@ void FlutterDesktopEngineRegisterPlatformViewType(
261261

262262
FlutterDesktopViewRef FlutterDesktopPluginRegistrarGetView(
263263
FlutterDesktopPluginRegistrarRef registrar) {
264-
// TODO(loicsharma): Add |FlutterDesktopPluginRegistrarGetViewById| and
265-
// deprecate this API as it makes single view assumptions.
266-
// https://github.com/flutter/flutter/issues/143767
267264
return HandleForView(registrar->engine->view(flutter::kImplicitViewId));
268265
}
269266

267+
FlutterDesktopViewRef FlutterDesktopPluginRegistrarGetViewById(
268+
FlutterDesktopPluginRegistrarRef registrar,
269+
FlutterDesktopViewId view_id) {
270+
return HandleForView(registrar->engine->view(view_id));
271+
}
272+
270273
void FlutterDesktopPluginRegistrarRegisterTopLevelWindowProcDelegate(
271274
FlutterDesktopPluginRegistrarRef registrar,
272275
FlutterDesktopWindowProcCallback delegate,

shell/platform/windows/flutter_windows_unittests.cc

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -333,6 +333,64 @@ TEST_F(WindowsTest, GetGraphicsAdapter) {
333333
ASSERT_TRUE(SUCCEEDED(dxgi_adapter->GetDesc(&desc)));
334334
}
335335

336+
// Implicit view has the implicit view ID.
337+
TEST_F(WindowsTest, PluginRegistrarGetImplicitView) {
338+
auto& context = GetContext();
339+
WindowsConfigBuilder builder(context);
340+
ViewControllerPtr controller{builder.Run()};
341+
ASSERT_NE(controller, nullptr);
342+
343+
FlutterDesktopEngineRef engine =
344+
FlutterDesktopViewControllerGetEngine(controller.get());
345+
FlutterDesktopPluginRegistrarRef registrar =
346+
FlutterDesktopEngineGetPluginRegistrar(engine, "foo_bar");
347+
FlutterDesktopViewRef implicit_view =
348+
FlutterDesktopPluginRegistrarGetView(registrar);
349+
350+
ASSERT_NE(implicit_view, nullptr);
351+
}
352+
353+
TEST_F(WindowsTest, PluginRegistrarGetView) {
354+
auto& context = GetContext();
355+
WindowsConfigBuilder builder(context);
356+
ViewControllerPtr controller{builder.Run()};
357+
ASSERT_NE(controller, nullptr);
358+
359+
FlutterDesktopEngineRef engine =
360+
FlutterDesktopViewControllerGetEngine(controller.get());
361+
FlutterDesktopPluginRegistrarRef registrar =
362+
FlutterDesktopEngineGetPluginRegistrar(engine, "foo_bar");
363+
364+
FlutterDesktopViewId view_id =
365+
FlutterDesktopViewControllerGetViewId(controller.get());
366+
FlutterDesktopViewRef view =
367+
FlutterDesktopPluginRegistrarGetViewById(registrar, view_id);
368+
369+
FlutterDesktopViewRef view_123 = FlutterDesktopPluginRegistrarGetViewById(
370+
registrar, static_cast<FlutterDesktopViewId>(123));
371+
372+
ASSERT_NE(view, nullptr);
373+
ASSERT_EQ(view_123, nullptr);
374+
}
375+
376+
TEST_F(WindowsTest, PluginRegistrarGetViewHeadless) {
377+
auto& context = GetContext();
378+
WindowsConfigBuilder builder(context);
379+
EnginePtr engine{builder.RunHeadless()};
380+
ASSERT_NE(engine, nullptr);
381+
382+
FlutterDesktopPluginRegistrarRef registrar =
383+
FlutterDesktopEngineGetPluginRegistrar(engine.get(), "foo_bar");
384+
385+
FlutterDesktopViewRef implicit_view =
386+
FlutterDesktopPluginRegistrarGetView(registrar);
387+
FlutterDesktopViewRef view_123 = FlutterDesktopPluginRegistrarGetViewById(
388+
registrar, static_cast<FlutterDesktopViewId>(123));
389+
390+
ASSERT_EQ(implicit_view, nullptr);
391+
ASSERT_EQ(view_123, nullptr);
392+
}
393+
336394
// Verify the app does not crash if EGL initializes successfully but
337395
// the rendering surface cannot be created.
338396
TEST_F(WindowsTest, SurfaceOptional) {

shell/platform/windows/public/flutter_windows.h

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -249,10 +249,22 @@ typedef bool (*FlutterDesktopWindowProcCallback)(HWND /* hwnd */,
249249
void* /* user data */,
250250
LRESULT* result);
251251

252-
// Returns the view associated with this registrar's engine instance.
252+
// Returns the implicit view associated with this registrar's engine instance,
253+
// or null if there is no implicit view.
254+
//
255+
// See:
256+
// https://api.flutter.dev/flutter/dart-ui/PlatformDispatcher/implicitView.html
257+
//
258+
// DEPRECATED: Use |FlutterDesktopPluginRegistrarGetViewById| instead.
253259
FLUTTER_EXPORT FlutterDesktopViewRef FlutterDesktopPluginRegistrarGetView(
254260
FlutterDesktopPluginRegistrarRef registrar);
255261

262+
// Returns the view associated with the registrar's engine instance, or null if
263+
// the view does not exist.
264+
FLUTTER_EXPORT FlutterDesktopViewRef FlutterDesktopPluginRegistrarGetViewById(
265+
FlutterDesktopPluginRegistrarRef registrar,
266+
FlutterDesktopViewId view_id);
267+
256268
FLUTTER_EXPORT void
257269
FlutterDesktopPluginRegistrarRegisterTopLevelWindowProcDelegate(
258270
FlutterDesktopPluginRegistrarRef registrar,

0 commit comments

Comments
 (0)