-
Notifications
You must be signed in to change notification settings - Fork 6k
[web] Move all DOM creation to DomManager #48123
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -4,8 +4,11 @@ | |
|
|
||
| import 'package:ui/ui.dart' as ui; | ||
|
|
||
| import '../configuration.dart'; | ||
| import '../dom.dart'; | ||
| import '../embedder.dart'; | ||
| import '../safe_browser_api.dart'; | ||
| import '../semantics/semantics.dart'; | ||
| import 'style_manager.dart'; | ||
|
|
||
| /// Manages DOM elements and the DOM structure for a [ui.FlutterView]. | ||
| /// | ||
|
|
@@ -19,24 +22,111 @@ import '../embedder.dart'; | |
| /// | | | | ||
| /// | | +- <flt-semantics-placeholder> | ||
| /// | | | | ||
| /// | | +- <flt-scene-host> | ||
| /// | | +- [sceneHost] <flt-scene-host> | ||
| /// | | | | | ||
| /// | | | +- <flt-scene> | ||
| /// | | | | ||
| /// | | +- [announcementsHost] <flt-announcement-host> | ||
| /// | | | | ||
| /// | | +- <style> | ||
| /// | | | ||
| /// | +- ...platform views | ||
| /// | | ||
| /// +- [textEditingHost] <text-editing-host> | ||
| /// +- [textEditingHost] <flt-text-editing-host> | ||
| /// | | | ||
| /// | +- ...text fields | ||
| /// | | ||
| /// +- [semanticsHost] <semantics-host> | ||
| /// | | ||
| /// +- ...semantics nodes | ||
| /// +- [semanticsHost] <flt-semantics-host> | ||
| /// | | | ||
| /// | +- ...semantics nodes | ||
| /// | | ||
| /// +- <style> | ||
| /// | ||
| class DomManager { | ||
| DomManager.fromFlutterViewEmbedderDEPRECATED(this._embedder); | ||
| factory DomManager({required double devicePixelRatio}) { | ||
| final DomElement rootElement = domDocument.createElement(DomManager.flutterViewTagName); | ||
| final DomElement platformViewsHost = domDocument.createElement(DomManager.glassPaneTagName); | ||
| final DomShadowRoot renderingHost = _attachShadowRoot(platformViewsHost); | ||
| final DomElement sceneHost = domDocument.createElement(DomManager.sceneHostTagName); | ||
| final DomElement textEditingHost = domDocument.createElement(DomManager.textEditingHostTagName); | ||
| final DomElement semanticsHost = domDocument.createElement(DomManager.semanticsHostTagName); | ||
| final DomElement announcementsHost = createDomElement(DomManager.announcementsHostTagName); | ||
|
|
||
| // Root element children. | ||
|
|
||
| rootElement.appendChild(platformViewsHost); | ||
| rootElement.appendChild(textEditingHost); | ||
| // The semantic host goes last because hit-test order-wise it must be | ||
| // first. If semantics goes under the scene host, platform views will | ||
| // obscure semantic elements. | ||
| // | ||
| // You may be wondering: wouldn't semantics obscure platform views and | ||
| // make then not accessible? At least with some careful planning, that | ||
| // should not be the case. The semantics tree makes all of its non-leaf | ||
| // elements transparent. This way, if a platform view appears among other | ||
| // interactive Flutter widgets, as long as those widgets do not intersect | ||
| // with the platform view, the platform view will be reachable. | ||
| rootElement.appendChild(semanticsHost); | ||
|
|
||
| // Rendering host (shadow root) children. | ||
|
|
||
| // TODO(yjbanov): In a multi-view world, we may want to have a separate | ||
| // semantics owner for each view. | ||
| // https://github.com/flutter/flutter/issues/137445 | ||
| final DomElement accessibilityPlaceholder = EngineSemanticsOwner | ||
| .instance.semanticsHelper | ||
| .prepareAccessibilityPlaceholder(); | ||
|
|
||
| renderingHost.append(accessibilityPlaceholder); | ||
| renderingHost.append(sceneHost); | ||
| renderingHost.append(announcementsHost); | ||
|
|
||
| // Styling. | ||
|
|
||
| StyleManager.attachGlobalStyles( | ||
| node: rootElement, | ||
| styleId: 'flt-text-editing-stylesheet', | ||
| styleNonce: configuration.nonce, | ||
| cssSelectorPrefix: DomManager.flutterViewTagName, | ||
| ); | ||
|
|
||
| StyleManager.attachGlobalStyles( | ||
| node: renderingHost, | ||
| styleId: 'flt-internals-stylesheet', | ||
| styleNonce: configuration.nonce, | ||
| cssSelectorPrefix: '', | ||
| ); | ||
|
|
||
| StyleManager.styleSceneHost( | ||
| sceneHost, | ||
| debugShowSemanticsNodes: configuration.debugShowSemanticsNodes, | ||
| ); | ||
|
|
||
| StyleManager.styleSemanticsHost( | ||
| semanticsHost, | ||
| devicePixelRatio, | ||
| ); | ||
|
|
||
| return DomManager._( | ||
| rootElement: rootElement, | ||
| platformViewsHost: platformViewsHost, | ||
| renderingHost: renderingHost, | ||
| sceneHost: sceneHost, | ||
| textEditingHost: textEditingHost, | ||
| semanticsHost: semanticsHost, | ||
| announcementsHost: announcementsHost, | ||
| ); | ||
| } | ||
|
|
||
| DomManager._({ | ||
| required this.rootElement, | ||
| required this.platformViewsHost, | ||
| required this.renderingHost, | ||
| required this.sceneHost, | ||
| required this.textEditingHost, | ||
| required this.semanticsHost, | ||
| required this.announcementsHost, | ||
| }); | ||
|
|
||
| /// The tag name for the Flutter View root element. | ||
| static const String flutterViewTagName = 'flutter-view'; | ||
|
|
@@ -47,38 +137,58 @@ class DomManager { | |
| /// The tag name for the scene host. | ||
| static const String sceneHostTagName = 'flt-scene-host'; | ||
|
|
||
| /// The tag name for the text editing host. | ||
| static const String textEditingHostTagName = 'flt-text-editing-host'; | ||
|
|
||
| /// The tag name for the semantics host. | ||
| static const String semanticsHostTagName = 'flt-semantics-host'; | ||
|
|
||
| /// The tag name for the accessibility announcements host. | ||
| static const String announcementsHostTagName = 'flt-announcement-host'; | ||
|
|
||
| final FlutterViewEmbedder _embedder; | ||
|
|
||
| /// The root DOM element for the entire Flutter View. | ||
| /// | ||
| /// This is where input events are captured, such as pointer events. | ||
| /// | ||
| /// If semantics is enabled, this element also contains the semantics DOM tree, | ||
| /// which captures semantics input events. | ||
| DomElement get rootElement => _embedder.flutterViewElementDEPRECATED; | ||
| DomElement rootElement; | ||
|
|
||
| /// Hosts all platform view elements. | ||
| DomElement get platformViewsHost => _embedder.glassPaneElementDEPRECATED; | ||
| DomElement platformViewsHost; | ||
|
|
||
| /// Hosts all rendering elements and canvases. | ||
| DomShadowRoot get renderingHost => _embedder.glassPaneShadowDEPRECATED; | ||
| DomShadowRoot renderingHost; | ||
|
|
||
| /// Hosts the <flt-scene> element. | ||
| /// | ||
| /// This element is created and inserted in the HTML DOM once. It is never | ||
| /// removed or moved. However the <flt-scene> inside of it may be replaced. | ||
| DomElement sceneHost; | ||
|
|
||
| /// Hosts all text editing elements. | ||
| DomElement get textEditingHost => _embedder.textEditingHostNodeDEPRECATED; | ||
| DomElement textEditingHost; | ||
|
|
||
| /// Hosts the semantics tree. | ||
| /// | ||
| /// This element is in front of the [renderingHost] and [platformViewsHost]. | ||
| /// Otherwise, the phone will disable focusing by touch, only by tabbing | ||
| /// around the UI. | ||
| DomElement get semanticsHost => _embedder.semanticsHostElementDEPRECATED; | ||
| DomElement semanticsHost; | ||
|
|
||
| /// This is where accessibility announcements are inserted. | ||
| DomElement get announcementsHost => _embedder.announcementsHostDEPRECATED; | ||
| DomElement announcementsHost; | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Can we make all DOM elements final?
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. YES! |
||
| } | ||
|
|
||
| DomShadowRoot _attachShadowRoot(DomElement element) { | ||
| if (getJsProperty<Object?>(element, 'attachShadow') == null) { | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think we can remove this check now. I believe @ditman removed the non-shadow DOM mode a long time ago.
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It was me who removed it, and I kept this check so that we show a somewhat meaningful error to the user instead of an obscure null check error. I'm happy to remove it if we think there's no value in having this error message.
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. There's probably some value, but also, this leaks into production code and increases code size, so there's also value in removing it.
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Ok, let me put it behind an assert. |
||
| throw UnsupportedError('ShadowDOM is not supported in this browser.'); | ||
| } | ||
|
|
||
| return element.attachShadow(<String, dynamic>{ | ||
| 'mode': 'open', | ||
| // This needs to stay false to prevent issues like this: | ||
| // - https://github.com/flutter/flutter/issues/85759 | ||
| 'delegatesFocus': false, | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think we don't need this either, since @htoor3 moved all focusables (text input, semantics) outside the shadow root.
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I would prefer to not make functional changes in this PR :) I'll file an issue though, so we make this change in the future.
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. |
||
| }); | ||
| } | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Looks like
devicePixelRatiois only used here, and also it seems like a bug? How do we update the semantics host whendevicePixelRatiochanges?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Not sure what you mean here. Yes, it's not used anywhere else in this method.
The code that listens for resize events (and updates the semantics host) is still in
FlutterViewEmbedderfor now:https://github.com/flutter/engine/blob/aae07e989b0a031c1b1fd2739cd10757e7cbd67f/lib/web_ui/lib/src/engine/embedder.dart#L220-L224
I do plan to move it out of there though. Stay tuned.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm just having a feeling that this is the wrong place for styling and scaling the semantics host. In fact, I'm thinking maybe the semantics host is the wrong element to style and scale. Should it be the responsibility of
EngineSemanticsOwner? Then we won't needDomManagerto be concerned aboutdevicePixelRatio.Having said that, I'm totally fine with not making this change in this PR.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yeah, I think it makes sense for the semantic owner to "own" the styling of the semantics tree.
I'll leave it as is for now, but feel free to move it to the semantics owner as part of your overhaul there.