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

Commit 090d45c

Browse files
authored
[Windows] Add view ID runner APIs (#51020)
Allow Flutter Windows apps and plugins to get the view's ID. Design doc: https://flutter.dev/go/desktop-multi-view-runner-apis Part of flutter/flutter#143767 Part of flutter/flutter#142845 [C++, Objective-C, Java style guides]: https://github.com/flutter/engine/blob/main/CONTRIBUTING.md#style
1 parent 7b3ef43 commit 090d45c

File tree

8 files changed

+56
-4
lines changed

8 files changed

+56
-4
lines changed

shell/platform/windows/client_wrapper/flutter_view_controller.cc

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,12 @@ FlutterViewController::~FlutterViewController() {
2929
}
3030
}
3131

32+
FlutterViewId FlutterViewController::view_id() const {
33+
auto view_id = FlutterDesktopViewControllerGetViewId(controller_);
34+
35+
return static_cast<FlutterViewId>(view_id);
36+
}
37+
3238
void FlutterViewController::ForceRedraw() {
3339
FlutterDesktopViewControllerForceRedraw(controller_);
3440
}

shell/platform/windows/client_wrapper/flutter_view_controller_unittests.cc

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,15 @@ TEST(FlutterViewControllerTest, CreateDestroy) {
7070
EXPECT_FALSE(test_api->engine_destroyed());
7171
}
7272

73+
TEST(FlutterViewControllerTest, GetViewId) {
74+
DartProject project(L"data");
75+
testing::ScopedStubFlutterWindowsApi scoped_api_stub(
76+
std::make_unique<TestWindowsApi>());
77+
auto test_api = static_cast<TestWindowsApi*>(scoped_api_stub.stub());
78+
FlutterViewController controller(100, 100, project);
79+
EXPECT_EQ(controller.view_id(), 1);
80+
}
81+
7382
TEST(FlutterViewControllerTest, GetEngine) {
7483
DartProject project(L"data");
7584
testing::ScopedStubFlutterWindowsApi scoped_api_stub(

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

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,9 @@
99

1010
namespace flutter {
1111

12+
// The unique identifier for a view.
13+
typedef int64_t FlutterViewId;
14+
1215
// A view displaying Flutter content.
1316
class FlutterView {
1417
public:

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

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -38,11 +38,14 @@ class FlutterViewController {
3838
FlutterViewController(FlutterViewController const&) = delete;
3939
FlutterViewController& operator=(FlutterViewController const&) = delete;
4040

41+
// Returns the view controller's view ID.
42+
FlutterViewId view_id() const;
43+
4144
// Returns the engine running Flutter content in this view.
42-
FlutterEngine* engine() { return engine_.get(); }
45+
FlutterEngine* engine() const { return engine_.get(); }
4346

4447
// Returns the view managed by this controller.
45-
FlutterView* view() { return view_.get(); }
48+
FlutterView* view() const { return view_.get(); }
4649

4750
// Requests new frame from the engine and repaints the view.
4851
void ForceRedraw();

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

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,12 @@ void FlutterDesktopViewControllerDestroy(
5252
}
5353
}
5454

55+
FlutterDesktopViewId FlutterDesktopViewControllerGetViewId(
56+
FlutterDesktopViewControllerRef controller) {
57+
// The stub ignores this, so just return an arbitrary non-zero value.
58+
return static_cast<FlutterDesktopViewId>(1);
59+
}
60+
5561
FlutterDesktopEngineRef FlutterDesktopViewControllerGetEngine(
5662
FlutterDesktopViewControllerRef controller) {
5763
// The stub ignores this, so just return an arbitrary non-zero value.

shell/platform/windows/flutter_windows.cc

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -110,6 +110,12 @@ void FlutterDesktopViewControllerDestroy(FlutterDesktopViewControllerRef ref) {
110110
delete controller;
111111
}
112112

113+
FlutterDesktopViewId FlutterDesktopViewControllerGetViewId(
114+
FlutterDesktopViewControllerRef ref) {
115+
auto controller = ViewControllerFromHandle(ref);
116+
return static_cast<FlutterDesktopViewId>(controller->view()->view_id());
117+
}
118+
113119
FlutterDesktopEngineRef FlutterDesktopViewControllerGetEngine(
114120
FlutterDesktopViewControllerRef ref) {
115121
auto controller = ViewControllerFromHandle(ref);

shell/platform/windows/flutter_windows_unittests.cc

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -307,6 +307,18 @@ TEST_F(WindowsTest, NextFrameCallback) {
307307
captures.frame_drawn_latch.Wait();
308308
}
309309

310+
// Implicit view has the implicit view ID.
311+
TEST_F(WindowsTest, GetViewId) {
312+
auto& context = GetContext();
313+
WindowsConfigBuilder builder(context);
314+
ViewControllerPtr controller{builder.Run()};
315+
ASSERT_NE(controller, nullptr);
316+
FlutterDesktopViewId view_id =
317+
FlutterDesktopViewControllerGetViewId(controller.get());
318+
319+
ASSERT_EQ(view_id, static_cast<FlutterDesktopViewId>(kImplicitViewId));
320+
}
321+
310322
TEST_F(WindowsTest, GetGraphicsAdapter) {
311323
auto& context = GetContext();
312324
WindowsConfigBuilder builder(context);

shell/platform/windows/public/flutter_windows.h

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,9 @@ typedef struct FlutterDesktopView* FlutterDesktopViewRef;
3232
struct FlutterDesktopEngine;
3333
typedef struct FlutterDesktopEngine* FlutterDesktopEngineRef;
3434

35+
// The unique identifier for a view.
36+
typedef int64_t FlutterDesktopViewId;
37+
3538
// Properties for configuring a Flutter engine instance.
3639
typedef struct {
3740
// The path to the flutter_assets folder for the application to be run.
@@ -97,13 +100,17 @@ FlutterDesktopViewControllerCreate(int width,
97100
FLUTTER_EXPORT void FlutterDesktopViewControllerDestroy(
98101
FlutterDesktopViewControllerRef controller);
99102

103+
// Returns the view controller's view ID.
104+
FLUTTER_EXPORT FlutterDesktopViewId FlutterDesktopViewControllerGetViewId(
105+
FlutterDesktopViewControllerRef view_controller);
106+
100107
// Returns the handle for the engine running in FlutterDesktopViewControllerRef.
101108
//
102109
// Its lifetime is the same as the |controller|'s.
103110
FLUTTER_EXPORT FlutterDesktopEngineRef FlutterDesktopViewControllerGetEngine(
104111
FlutterDesktopViewControllerRef controller);
105-
// Returns the view managed by the given controller.
106112

113+
// Returns the view managed by the given controller.
107114
FLUTTER_EXPORT FlutterDesktopViewRef FlutterDesktopViewControllerGetView(
108115
FlutterDesktopViewControllerRef controller);
109116

@@ -205,7 +212,7 @@ FLUTTER_EXPORT void FlutterDesktopEngineSetNextFrameCallback(
205212

206213
// ========== View ==========
207214

208-
// Return backing HWND for manipulation in host application.
215+
// Returns the backing HWND for manipulation in host application.
209216
FLUTTER_EXPORT HWND FlutterDesktopViewGetHWND(FlutterDesktopViewRef view);
210217

211218
// Returns the DXGI adapter used for rendering or nullptr in case of error.

0 commit comments

Comments
 (0)