Skip to content

Commit

Permalink
[Windows] Add view ID runner APIs (#51020)
Browse files Browse the repository at this point in the history
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
  • Loading branch information
loic-sharma authored Feb 29, 2024
1 parent 7b3ef43 commit 090d45c
Show file tree
Hide file tree
Showing 8 changed files with 56 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,12 @@ FlutterViewController::~FlutterViewController() {
}
}

FlutterViewId FlutterViewController::view_id() const {
auto view_id = FlutterDesktopViewControllerGetViewId(controller_);

return static_cast<FlutterViewId>(view_id);
}

void FlutterViewController::ForceRedraw() {
FlutterDesktopViewControllerForceRedraw(controller_);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,15 @@ TEST(FlutterViewControllerTest, CreateDestroy) {
EXPECT_FALSE(test_api->engine_destroyed());
}

TEST(FlutterViewControllerTest, GetViewId) {
DartProject project(L"data");
testing::ScopedStubFlutterWindowsApi scoped_api_stub(
std::make_unique<TestWindowsApi>());
auto test_api = static_cast<TestWindowsApi*>(scoped_api_stub.stub());
FlutterViewController controller(100, 100, project);
EXPECT_EQ(controller.view_id(), 1);
}

TEST(FlutterViewControllerTest, GetEngine) {
DartProject project(L"data");
testing::ScopedStubFlutterWindowsApi scoped_api_stub(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,9 @@

namespace flutter {

// The unique identifier for a view.
typedef int64_t FlutterViewId;

// A view displaying Flutter content.
class FlutterView {
public:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,11 +38,14 @@ class FlutterViewController {
FlutterViewController(FlutterViewController const&) = delete;
FlutterViewController& operator=(FlutterViewController const&) = delete;

// Returns the view controller's view ID.
FlutterViewId view_id() const;

// Returns the engine running Flutter content in this view.
FlutterEngine* engine() { return engine_.get(); }
FlutterEngine* engine() const { return engine_.get(); }

// Returns the view managed by this controller.
FlutterView* view() { return view_.get(); }
FlutterView* view() const { return view_.get(); }

// Requests new frame from the engine and repaints the view.
void ForceRedraw();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,12 @@ void FlutterDesktopViewControllerDestroy(
}
}

FlutterDesktopViewId FlutterDesktopViewControllerGetViewId(
FlutterDesktopViewControllerRef controller) {
// The stub ignores this, so just return an arbitrary non-zero value.
return static_cast<FlutterDesktopViewId>(1);
}

FlutterDesktopEngineRef FlutterDesktopViewControllerGetEngine(
FlutterDesktopViewControllerRef controller) {
// The stub ignores this, so just return an arbitrary non-zero value.
Expand Down
6 changes: 6 additions & 0 deletions shell/platform/windows/flutter_windows.cc
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,12 @@ void FlutterDesktopViewControllerDestroy(FlutterDesktopViewControllerRef ref) {
delete controller;
}

FlutterDesktopViewId FlutterDesktopViewControllerGetViewId(
FlutterDesktopViewControllerRef ref) {
auto controller = ViewControllerFromHandle(ref);
return static_cast<FlutterDesktopViewId>(controller->view()->view_id());
}

FlutterDesktopEngineRef FlutterDesktopViewControllerGetEngine(
FlutterDesktopViewControllerRef ref) {
auto controller = ViewControllerFromHandle(ref);
Expand Down
12 changes: 12 additions & 0 deletions shell/platform/windows/flutter_windows_unittests.cc
Original file line number Diff line number Diff line change
Expand Up @@ -307,6 +307,18 @@ TEST_F(WindowsTest, NextFrameCallback) {
captures.frame_drawn_latch.Wait();
}

// Implicit view has the implicit view ID.
TEST_F(WindowsTest, GetViewId) {
auto& context = GetContext();
WindowsConfigBuilder builder(context);
ViewControllerPtr controller{builder.Run()};
ASSERT_NE(controller, nullptr);
FlutterDesktopViewId view_id =
FlutterDesktopViewControllerGetViewId(controller.get());

ASSERT_EQ(view_id, static_cast<FlutterDesktopViewId>(kImplicitViewId));
}

TEST_F(WindowsTest, GetGraphicsAdapter) {
auto& context = GetContext();
WindowsConfigBuilder builder(context);
Expand Down
11 changes: 9 additions & 2 deletions shell/platform/windows/public/flutter_windows.h
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,9 @@ typedef struct FlutterDesktopView* FlutterDesktopViewRef;
struct FlutterDesktopEngine;
typedef struct FlutterDesktopEngine* FlutterDesktopEngineRef;

// The unique identifier for a view.
typedef int64_t FlutterDesktopViewId;

// Properties for configuring a Flutter engine instance.
typedef struct {
// The path to the flutter_assets folder for the application to be run.
Expand Down Expand Up @@ -97,13 +100,17 @@ FlutterDesktopViewControllerCreate(int width,
FLUTTER_EXPORT void FlutterDesktopViewControllerDestroy(
FlutterDesktopViewControllerRef controller);

// Returns the view controller's view ID.
FLUTTER_EXPORT FlutterDesktopViewId FlutterDesktopViewControllerGetViewId(
FlutterDesktopViewControllerRef view_controller);

// Returns the handle for the engine running in FlutterDesktopViewControllerRef.
//
// Its lifetime is the same as the |controller|'s.
FLUTTER_EXPORT FlutterDesktopEngineRef FlutterDesktopViewControllerGetEngine(
FlutterDesktopViewControllerRef controller);
// Returns the view managed by the given controller.

// Returns the view managed by the given controller.
FLUTTER_EXPORT FlutterDesktopViewRef FlutterDesktopViewControllerGetView(
FlutterDesktopViewControllerRef controller);

Expand Down Expand Up @@ -205,7 +212,7 @@ FLUTTER_EXPORT void FlutterDesktopEngineSetNextFrameCallback(

// ========== View ==========

// Return backing HWND for manipulation in host application.
// Returns the backing HWND for manipulation in host application.
FLUTTER_EXPORT HWND FlutterDesktopViewGetHWND(FlutterDesktopViewRef view);

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

0 comments on commit 090d45c

Please sign in to comment.