[web] New platform view API to get view by ID (flutter/engine#41784)
In order to make `HtmlElementView` more useful, we need to expose an API for getting the created platform view element. ~~This would allow `HtmlElementView` to set attributes and styles on the element.~~ This would allow us to add a new callback to the `HtmlElementView` that would return the result of calling the registered platformViewFactory to the user, after it's been created and added to the DOM, so the user can do with it as they please. Required by https://github.com/flutter/flutter/pull/126176
This commit is contained in:
@@ -520,6 +520,8 @@ extension DomElementExtension on DomElement {
|
||||
Iterable<DomElement> get children =>
|
||||
createDomListWrapper<DomElement>(_children);
|
||||
|
||||
external DomElement? get firstElementChild;
|
||||
|
||||
@JS('clientHeight')
|
||||
external JSNumber get _clientHeight;
|
||||
double get clientHeight => _clientHeight.toDart;
|
||||
|
||||
@@ -60,6 +60,17 @@ class PlatformViewManager {
|
||||
return _contents.containsKey(viewId);
|
||||
}
|
||||
|
||||
/// Returns the HTML element created by a registered factory for [viewId].
|
||||
///
|
||||
/// Throws an [AssertionError] if [viewId] hasn't been rendered before.
|
||||
DomElement getViewById(int viewId) {
|
||||
assert(knowsViewId(viewId), 'No view has been rendered for viewId: $viewId');
|
||||
// `_contents[viewId]` is the <flt-platform-view> element created by us. The
|
||||
// first (and only) child of that is the element created by the user-supplied
|
||||
// factory function.
|
||||
return _contents[viewId]!.firstElementChild!;
|
||||
}
|
||||
|
||||
/// Registers a `factoryFunction` that knows how to render a Platform View of `viewType`.
|
||||
///
|
||||
/// `viewType` is selected by the programmer, but it can't be overridden once
|
||||
@@ -119,12 +130,13 @@ class PlatformViewManager {
|
||||
..setAttribute('slot', slotName);
|
||||
|
||||
final Function factoryFunction = _factories[viewType]!;
|
||||
late DomElement content;
|
||||
final DomElement content;
|
||||
|
||||
if (factoryFunction is ParameterizedPlatformViewFactory) {
|
||||
content = factoryFunction(viewId, params: params);
|
||||
} else {
|
||||
content = (factoryFunction as PlatformViewFactory).call(viewId);
|
||||
factoryFunction as PlatformViewFactory;
|
||||
content = factoryFunction(viewId);
|
||||
}
|
||||
|
||||
_ensureContentCorrectlySized(content, viewType);
|
||||
|
||||
@@ -23,4 +23,11 @@ class PlatformViewRegistry {
|
||||
isVisible: isVisible,
|
||||
);
|
||||
}
|
||||
|
||||
/// Returns the view previously created for [viewId].
|
||||
///
|
||||
/// Throws if no view has been created for [viewId].
|
||||
Object getViewById(int viewId) {
|
||||
return platformViewManager.getViewById(viewId);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -145,5 +145,63 @@ void testMain() {
|
||||
expect(firstRender, same(anotherRender));
|
||||
});
|
||||
});
|
||||
|
||||
group('getViewById', () {
|
||||
test('finds created views', () async {
|
||||
final Map<int, DomElement> views1 = <int, DomElement>{
|
||||
1: createDomHTMLDivElement(),
|
||||
2: createDomHTMLDivElement(),
|
||||
5: createDomHTMLDivElement(),
|
||||
};
|
||||
final Map<int, DomElement> views2 = <int, DomElement>{
|
||||
3: createDomHTMLDivElement(),
|
||||
4: createDomHTMLDivElement(),
|
||||
};
|
||||
|
||||
contentManager.registerFactory('forTest1', (int id) => views1[id]!);
|
||||
contentManager.registerFactory('forTest2', (int id) => views2[id]!);
|
||||
|
||||
// Render all 5 views.
|
||||
for (final int id in views1.keys) {
|
||||
contentManager.renderContent('forTest1', id, null);
|
||||
}
|
||||
for (final int id in views2.keys) {
|
||||
contentManager.renderContent('forTest2', id, null);
|
||||
}
|
||||
|
||||
// Check all 5 views.
|
||||
for (final int id in views1.keys) {
|
||||
expect(contentManager.getViewById(id), views1[id]);
|
||||
}
|
||||
for (final int id in views2.keys) {
|
||||
expect(contentManager.getViewById(id), views2[id]);
|
||||
}
|
||||
|
||||
// Throws for unknown viewId.
|
||||
expect(() {
|
||||
contentManager.getViewById(99);
|
||||
}, throwsA(isA<AssertionError>()));
|
||||
});
|
||||
|
||||
test('throws if view has been cleared', () {
|
||||
final DomHTMLDivElement view = createDomHTMLDivElement();
|
||||
contentManager.registerFactory(viewType, (int id) => view);
|
||||
|
||||
// Throws before viewId is rendered.
|
||||
expect(() {
|
||||
contentManager.getViewById(viewId);
|
||||
}, throwsA(isA<AssertionError>()));
|
||||
|
||||
contentManager.renderContent(viewType, viewId, null);
|
||||
// Succeeds after viewId is rendered.
|
||||
expect(contentManager.getViewById(viewId), view);
|
||||
|
||||
contentManager.clearPlatformView(viewId);
|
||||
// Throws after viewId is cleared.
|
||||
expect(() {
|
||||
contentManager.getViewById(viewId);
|
||||
}, throwsA(isA<AssertionError>()));
|
||||
});
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user