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

Commit cb12a8c

Browse files
authored
[Windows] Add ID to views (#50788)
Adds an ID to a view: 1. `FlutterWindowsView` now has a ID 2. The `FlutterWindowsEngine::view(...)` accessor now requires a view ID parameter This is a refactoring with no semantic changes. Part of flutter/flutter#143765 [C++, Objective-C, Java style guides]: https://github.com/flutter/engine/blob/main/CONTRIBUTING.md#style
1 parent 1ae2c10 commit cb12a8c

20 files changed

+94
-31
lines changed

shell/platform/windows/accessibility_bridge_windows_unittests.cc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,7 @@ class FlutterWindowsViewSpy : public FlutterWindowsView {
9191
public:
9292
FlutterWindowsViewSpy(FlutterWindowsEngine* engine,
9393
std::unique_ptr<WindowBindingHandler> handler)
94-
: FlutterWindowsView(engine, std::move(handler)) {}
94+
: FlutterWindowsView(kImplicitViewId, engine, std::move(handler)) {}
9595

9696
protected:
9797
virtual std::shared_ptr<AccessibilityBridgeWindows>

shell/platform/windows/compositor_opengl.cc

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
#include "flutter/shell/platform/windows/compositor_opengl.h"
66

77
#include "GLES3/gl3.h"
8+
#include "flutter/shell/platform/windows/flutter_windows_engine.h"
89
#include "flutter/shell/platform/windows/flutter_windows_view.h"
910

1011
namespace flutter {
@@ -93,7 +94,9 @@ bool CompositorOpenGL::CollectBackingStore(const FlutterBackingStore* store) {
9394

9495
bool CompositorOpenGL::Present(const FlutterLayer** layers,
9596
size_t layers_count) {
96-
FlutterWindowsView* view = engine_->view();
97+
// TODO(loicsharma): Remove implicit view assumption.
98+
// https://github.com/flutter/flutter/issues/142845
99+
FlutterWindowsView* view = engine_->view(kImplicitViewId);
97100
if (!view) {
98101
return false;
99102
}

shell/platform/windows/compositor_opengl_unittests.cc

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -95,8 +95,8 @@ class CompositorOpenGLTest : public WindowsTest {
9595
EXPECT_CALL(*window.get(), SetView).Times(1);
9696
EXPECT_CALL(*window.get(), GetWindowHandle).WillRepeatedly(Return(nullptr));
9797

98-
view_ =
99-
std::make_unique<FlutterWindowsView>(engine_.get(), std::move(window));
98+
view_ = std::make_unique<FlutterWindowsView>(kImplicitViewId, engine_.get(),
99+
std::move(window));
100100

101101
if (add_surface) {
102102
auto surface = std::make_unique<egl::MockWindowSurface>();

shell/platform/windows/compositor_software.cc

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
#include "flutter/shell/platform/windows/compositor_software.h"
66

7+
#include "flutter/shell/platform/windows/flutter_windows_engine.h"
78
#include "flutter/shell/platform/windows/flutter_windows_view.h"
89

910
namespace flutter {
@@ -39,13 +40,16 @@ bool CompositorSoftware::CollectBackingStore(const FlutterBackingStore* store) {
3940

4041
bool CompositorSoftware::Present(const FlutterLayer** layers,
4142
size_t layers_count) {
42-
if (!engine_->view()) {
43+
// TODO(loicsharma): Remove implicit view assumption.
44+
// https://github.com/flutter/flutter/issues/142845
45+
FlutterWindowsView* view = engine_->view(kImplicitViewId);
46+
if (!view) {
4347
return false;
4448
}
4549

4650
// Clear the view if there are no layers to present.
4751
if (layers_count == 0) {
48-
return engine_->view()->ClearSoftwareBitmap();
52+
return view->ClearSoftwareBitmap();
4953
}
5054

5155
// TODO: Support compositing layers and platform views.
@@ -58,7 +62,7 @@ bool CompositorSoftware::Present(const FlutterLayer** layers,
5862

5963
const auto& backing_store = layers[0]->backing_store->software;
6064

61-
return engine_->view()->PresentSoftwareBitmap(
65+
return view->PresentSoftwareBitmap(
6266
backing_store.allocation, backing_store.row_bytes, backing_store.height);
6367
}
6468

shell/platform/windows/compositor_software_unittests.cc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ class MockFlutterWindowsView : public FlutterWindowsView {
2424
public:
2525
MockFlutterWindowsView(FlutterWindowsEngine* engine,
2626
std::unique_ptr<WindowBindingHandler> window)
27-
: FlutterWindowsView(engine, std::move(window)) {}
27+
: FlutterWindowsView(kImplicitViewId, engine, std::move(window)) {}
2828
virtual ~MockFlutterWindowsView() = default;
2929

3030
MOCK_METHOD(bool,

shell/platform/windows/cursor_handler.cc

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,10 @@ void CursorHandler::HandleMethodCall(
7474
return;
7575
}
7676
const auto& kind = std::get<std::string>(kind_iter->second);
77-
FlutterWindowsView* view = engine_->view();
77+
78+
// TODO(loicsharma): Remove implicit view assumption.
79+
// https://github.com/flutter/flutter/issues/142845
80+
FlutterWindowsView* view = engine_->view(kImplicitViewId);
7881
if (view == nullptr) {
7982
result->Error(kCursorError,
8083
"Cursor is not available in Windows headless mode");
@@ -164,7 +167,10 @@ void CursorHandler::HandleMethodCall(
164167
return;
165168
}
166169
HCURSOR cursor = custom_cursors_[name];
167-
FlutterWindowsView* view = engine_->view();
170+
171+
// TODO(loicsharma): Remove implicit view assumption.
172+
// https://github.com/flutter/flutter/issues/142845
173+
FlutterWindowsView* view = engine_->view(kImplicitViewId);
168174
if (view == nullptr) {
169175
result->Error(kCursorError,
170176
"Cursor is not available in Windows headless mode");

shell/platform/windows/cursor_handler_unittests.cc

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -79,8 +79,8 @@ class CursorHandlerTest : public WindowsTest {
7979

8080
window_ = window.get();
8181
engine_ = builder.Build();
82-
view_ =
83-
std::make_unique<FlutterWindowsView>(engine_.get(), std::move(window));
82+
view_ = std::make_unique<FlutterWindowsView>(kImplicitViewId, engine_.get(),
83+
std::move(window));
8484

8585
EngineModifier modifier{engine_.get()};
8686
modifier.SetImplicitView(view_.get());

shell/platform/windows/flutter_window_unittests.cc

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,8 @@ class MockFlutterWindowsView : public FlutterWindowsView {
9898
public:
9999
MockFlutterWindowsView(FlutterWindowsEngine* engine,
100100
std::unique_ptr<WindowBindingHandler> window_binding)
101-
: FlutterWindowsView(engine, std::move(window_binding)) {}
101+
: FlutterWindowsView(kImplicitViewId, engine, std::move(window_binding)) {
102+
}
102103
~MockFlutterWindowsView() {}
103104

104105
MOCK_METHOD(void,

shell/platform/windows/flutter_windows.cc

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -247,7 +247,10 @@ bool FlutterDesktopEngineProcessExternalWindowMessage(
247247

248248
FlutterDesktopViewRef FlutterDesktopPluginRegistrarGetView(
249249
FlutterDesktopPluginRegistrarRef registrar) {
250-
return HandleForView(registrar->engine->view());
250+
// TODO(loicsharma): Add |FlutterDesktopPluginRegistrarGetViewById| and
251+
// deprecate this API as it makes single view assumptions.
252+
// https://github.com/flutter/flutter/issues/143767
253+
return HandleForView(registrar->engine->view(flutter::kImplicitViewId));
251254
}
252255

253256
void FlutterDesktopPluginRegistrarRegisterTopLevelWindowProcDelegate(

shell/platform/windows/flutter_windows_engine.cc

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -340,7 +340,10 @@ bool FlutterWindowsEngine::Run(std::string_view entrypoint) {
340340
args.update_semantics_callback2 = [](const FlutterSemanticsUpdate2* update,
341341
void* user_data) {
342342
auto host = static_cast<FlutterWindowsEngine*>(user_data);
343-
auto view = host->view();
343+
344+
// TODO(loicsharma): Remove implicit view assumption.
345+
// https://github.com/flutter/flutter/issues/142845
346+
auto view = host->view(kImplicitViewId);
344347
if (!view) {
345348
return;
346349
}
@@ -485,8 +488,10 @@ bool FlutterWindowsEngine::Stop() {
485488

486489
std::unique_ptr<FlutterWindowsView> FlutterWindowsEngine::CreateView(
487490
std::unique_ptr<WindowBindingHandler> window) {
488-
auto view = std::make_unique<FlutterWindowsView>(this, std::move(window),
489-
windows_proc_table_);
491+
// TODO(loicsharma): Remove implicit view assumption.
492+
// https://github.com/flutter/flutter/issues/142845
493+
auto view = std::make_unique<FlutterWindowsView>(
494+
kImplicitViewId, this, std::move(window), windows_proc_table_);
490495

491496
view_ = view.get();
492497
InitializeKeyboard();
@@ -522,6 +527,12 @@ std::chrono::nanoseconds FlutterWindowsEngine::FrameInterval() {
522527
return std::chrono::nanoseconds(interval);
523528
}
524529

530+
FlutterWindowsView* FlutterWindowsEngine::view(FlutterViewId view_id) const {
531+
FML_DCHECK(view_id == kImplicitViewId);
532+
533+
return view_;
534+
}
535+
525536
// Returns the currently configured Plugin Registrar.
526537
FlutterDesktopPluginRegistrarRef FlutterWindowsEngine::GetRegistrar() {
527538
return plugin_registrar_.get();

shell/platform/windows/flutter_windows_engine.h

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,12 @@
4444

4545
namespace flutter {
4646

47+
// The implicit view's ID.
48+
//
49+
// See:
50+
// https://api.flutter.dev/flutter/dart-ui/PlatformDispatcher/implicitView.html
51+
constexpr FlutterViewId kImplicitViewId = 0;
52+
4753
class FlutterWindowsView;
4854

4955
// Update the thread priority for the Windows engine.
@@ -117,7 +123,7 @@ class FlutterWindowsEngine {
117123

118124
// The view displaying this engine's content, if any. This will be null for
119125
// headless engines.
120-
FlutterWindowsView* view() { return view_; }
126+
FlutterWindowsView* view(FlutterViewId view_id) const;
121127

122128
// Returns the currently configured Plugin Registrar.
123129
FlutterDesktopPluginRegistrarRef GetRegistrar();

shell/platform/windows/flutter_windows_engine_unittests.cc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -623,7 +623,7 @@ class MockFlutterWindowsView : public FlutterWindowsView {
623623
public:
624624
MockFlutterWindowsView(FlutterWindowsEngine* engine,
625625
std::unique_ptr<WindowBindingHandler> wbh)
626-
: FlutterWindowsView(engine, std::move(wbh)) {}
626+
: FlutterWindowsView(kImplicitViewId, engine, std::move(wbh)) {}
627627
~MockFlutterWindowsView() {}
628628

629629
MOCK_METHOD(void,

shell/platform/windows/flutter_windows_view.cc

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -84,10 +84,13 @@ void UpdateVsync(const FlutterWindowsEngine& engine,
8484
} // namespace
8585

8686
FlutterWindowsView::FlutterWindowsView(
87+
FlutterViewId view_id,
8788
FlutterWindowsEngine* engine,
8889
std::unique_ptr<WindowBindingHandler> window_binding,
8990
std::shared_ptr<WindowsProcTable> windows_proc_table)
90-
: engine_(engine), windows_proc_table_(std::move(windows_proc_table)) {
91+
: view_id_(view_id),
92+
engine_(engine),
93+
windows_proc_table_(std::move(windows_proc_table)) {
9194
if (windows_proc_table_ == nullptr) {
9295
windows_proc_table_ = std::make_shared<WindowsProcTable>();
9396
}
@@ -652,6 +655,10 @@ bool FlutterWindowsView::PresentSoftwareBitmap(const void* allocation,
652655
height);
653656
}
654657

658+
FlutterViewId FlutterWindowsView::view_id() const {
659+
return view_id_;
660+
}
661+
655662
void FlutterWindowsView::CreateRenderSurface() {
656663
FML_DCHECK(surface_ == nullptr);
657664

shell/platform/windows/flutter_windows_view.h

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,19 +26,26 @@
2626

2727
namespace flutter {
2828

29+
// A unique identifier for a view.
30+
using FlutterViewId = int64_t;
31+
2932
// An OS-windowing neutral abstration for a Flutter view that works
3033
// with win32 HWNDs.
3134
class FlutterWindowsView : public WindowBindingHandlerDelegate {
3235
public:
3336
// Creates a FlutterWindowsView with the given implementor of
3437
// WindowBindingHandler.
3538
FlutterWindowsView(
39+
FlutterViewId view_id,
3640
FlutterWindowsEngine* engine,
3741
std::unique_ptr<WindowBindingHandler> window_binding,
3842
std::shared_ptr<WindowsProcTable> windows_proc_table = nullptr);
3943

4044
virtual ~FlutterWindowsView();
4145

46+
// Get the view's unique identifier.
47+
FlutterViewId view_id() const;
48+
4249
// Creates rendering surface for Flutter engine to draw into.
4350
// Should be called before calling FlutterEngineRun using this view.
4451
void CreateRenderSurface();
@@ -379,6 +386,9 @@ class FlutterWindowsView : public WindowBindingHandlerDelegate {
379386
// to prevent screen tearing.
380387
bool NeedsVsync() const;
381388

389+
// The view's unique identifier.
390+
FlutterViewId view_id_;
391+
382392
// The engine associated with this view.
383393
FlutterWindowsEngine* engine_ = nullptr;
384394

shell/platform/windows/flutter_windows_view_unittests.cc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1029,7 +1029,7 @@ TEST(FlutterWindowsViewTest, WindowRepaintTests) {
10291029
std::unique_ptr<FlutterWindowsEngine> engine = GetTestEngine();
10301030
EngineModifier modifier(engine.get());
10311031

1032-
FlutterWindowsView view{engine.get(),
1032+
FlutterWindowsView view{kImplicitViewId, engine.get(),
10331033
std::make_unique<flutter::FlutterWindow>(100, 100)};
10341034

10351035
bool schedule_frame_called = false;

shell/platform/windows/keyboard_unittests.cc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -340,7 +340,7 @@ class TestFlutterWindowsView : public FlutterWindowsView {
340340
std::unique_ptr<WindowBindingHandler> window,
341341
std::function<void(KeyCall)> on_key_call)
342342
: on_key_call_(on_key_call),
343-
FlutterWindowsView(engine, std::move(window)) {}
343+
FlutterWindowsView(kImplicitViewId, engine, std::move(window)) {}
344344

345345
void OnText(const std::u16string& text) override {
346346
on_key_call_(KeyCall{

shell/platform/windows/platform_handler.cc

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -248,7 +248,9 @@ PlatformHandler::~PlatformHandler() = default;
248248
void PlatformHandler::GetPlainText(
249249
std::unique_ptr<MethodResult<rapidjson::Document>> result,
250250
std::string_view key) {
251-
const FlutterWindowsView* view = engine_->view();
251+
// TODO(loicsharma): Remove implicit view assumption.
252+
// https://github.com/flutter/flutter/issues/142845
253+
const FlutterWindowsView* view = engine_->view(kImplicitViewId);
252254
if (view == nullptr) {
253255
result->Error(kClipboardError,
254256
"Clipboard is not available in Windows headless mode");
@@ -291,7 +293,9 @@ void PlatformHandler::GetPlainText(
291293

292294
void PlatformHandler::GetHasStrings(
293295
std::unique_ptr<MethodResult<rapidjson::Document>> result) {
294-
const FlutterWindowsView* view = engine_->view();
296+
// TODO(loicsharma): Remove implicit view assumption.
297+
// https://github.com/flutter/flutter/issues/142845
298+
const FlutterWindowsView* view = engine_->view(kImplicitViewId);
295299
if (view == nullptr) {
296300
result->Error(kClipboardError,
297301
"Clipboard is not available in Windows headless mode");
@@ -329,7 +333,9 @@ void PlatformHandler::GetHasStrings(
329333
void PlatformHandler::SetPlainText(
330334
const std::string& text,
331335
std::unique_ptr<MethodResult<rapidjson::Document>> result) {
332-
const FlutterWindowsView* view = engine_->view();
336+
// TODO(loicsharma): Remove implicit view assumption.
337+
// https://github.com/flutter/flutter/issues/142845
338+
const FlutterWindowsView* view = engine_->view(kImplicitViewId);
333339
if (view == nullptr) {
334340
result->Error(kClipboardError,
335341
"Clipboard is not available in Windows headless mode");

shell/platform/windows/platform_handler_unittests.cc

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -155,8 +155,8 @@ class PlatformHandlerTest : public WindowsTest {
155155
auto window = std::make_unique<NiceMock<MockWindowBindingHandler>>();
156156

157157
engine_ = builder.Build();
158-
view_ =
159-
std::make_unique<FlutterWindowsView>(engine_.get(), std::move(window));
158+
view_ = std::make_unique<FlutterWindowsView>(kImplicitViewId, engine_.get(),
159+
std::move(window));
160160

161161
EngineModifier modifier{engine_.get()};
162162
modifier.SetImplicitView(view_.get());

shell/platform/windows/text_input_plugin.cc

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -216,7 +216,9 @@ void TextInputPlugin::HandleMethodCall(
216216
if (method.compare(kShowMethod) == 0 || method.compare(kHideMethod) == 0) {
217217
// These methods are no-ops.
218218
} else if (method.compare(kClearClientMethod) == 0) {
219-
FlutterWindowsView* view = engine_->view();
219+
// TODO(loicsharma): Remove implicit view assumption.
220+
// https://github.com/flutter/flutter/issues/142845
221+
FlutterWindowsView* view = engine_->view(kImplicitViewId);
220222
if (view == nullptr) {
221223
result->Error(kInternalConsistencyError,
222224
"Text input is not available in Windows headless mode");
@@ -326,7 +328,9 @@ void TextInputPlugin::HandleMethodCall(
326328
TextRange(composing_base, composing_extent), cursor_offset);
327329
}
328330
} else if (method.compare(kSetMarkedTextRect) == 0) {
329-
FlutterWindowsView* view = engine_->view();
331+
// TODO(loicsharma): Remove implicit view assumption.
332+
// https://github.com/flutter/flutter/issues/142845
333+
FlutterWindowsView* view = engine_->view(kImplicitViewId);
330334
if (view == nullptr) {
331335
result->Error(kInternalConsistencyError,
332336
"Text input is not available in Windows headless mode");
@@ -355,7 +359,9 @@ void TextInputPlugin::HandleMethodCall(
355359
Rect transformed_rect = GetCursorRect();
356360
view->OnCursorRectUpdated(transformed_rect);
357361
} else if (method.compare(kSetEditableSizeAndTransform) == 0) {
358-
FlutterWindowsView* view = engine_->view();
362+
// TODO(loicsharma): Remove implicit view assumption.
363+
// https://github.com/flutter/flutter/issues/142845
364+
FlutterWindowsView* view = engine_->view(kImplicitViewId);
359365
if (view == nullptr) {
360366
result->Error(kInternalConsistencyError,
361367
"Text input is not available in Windows headless mode");

shell/platform/windows/text_input_plugin_unittest.cc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -107,7 +107,7 @@ class MockFlutterWindowsView : public FlutterWindowsView {
107107
public:
108108
MockFlutterWindowsView(FlutterWindowsEngine* engine,
109109
std::unique_ptr<WindowBindingHandler> window)
110-
: FlutterWindowsView(engine, std::move(window)) {}
110+
: FlutterWindowsView(kImplicitViewId, engine, std::move(window)) {}
111111
virtual ~MockFlutterWindowsView() = default;
112112

113113
MOCK_METHOD(void, OnCursorRectUpdated, (const Rect&), (override));

0 commit comments

Comments
 (0)