Skip to content
This repository was archived by the owner on Feb 25, 2025. It is now read-only.
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
139 changes: 5 additions & 134 deletions lib/web_ui/lib/src/engine/embedder.dart
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

import 'package:ui/src/engine/safe_browser_api.dart';
import 'package:ui/ui.dart' as ui;

import '../engine.dart' show buildMode, renderer;
Expand All @@ -12,9 +11,7 @@ import 'dom.dart';
import 'keyboard_binding.dart';
import 'platform_dispatcher.dart';
import 'pointer_binding.dart';
import 'semantics.dart';
import 'text_editing/text_editing.dart';
import 'view_embedder/dom_manager.dart';
import 'view_embedder/style_manager.dart';
import 'window.dart';

Expand Down Expand Up @@ -42,35 +39,13 @@ class FlutterViewEmbedder {
reset();
}

/// The element that contains the [sceneElement].
///
/// This element is created and inserted in the HTML DOM once. It is never
/// removed or moved. However the [sceneElement] may be replaced inside it.
///
/// This element is inserted after the [semanticsHostElement] so that
/// platform views take precedence in DOM event handling.
DomElement get sceneHostElement => _sceneHostElement;
late DomElement _sceneHostElement;
DomElement get _sceneHostElement => window.dom.sceneHost;

/// A child element of body outside the shadowroot that hosts
/// global resources such svg filters and clip paths when using webkit.
DomElement? _resourcesHost;

/// The element that contains the semantics tree.
///
/// This element is created and inserted in the HTML DOM once. It is never
/// removed or moved.
///
/// Render semantics inside the glasspane for proper focus and event
/// handling. If semantics is behind the glasspane, the phone will disable
/// focusing by touch, only by tabbing around the UI. If semantics is in
/// front of glasspane, then DOM event won't bubble up to the glasspane so
/// it can forward events to the framework.
///
/// This element is inserted before the [semanticsHostElement] so that
/// platform views take precedence in DOM event handling.
DomElement get semanticsHostElementDEPRECATED => _semanticsHostElement;
late DomElement _semanticsHostElement;
DomElement get _semanticsHostElement => window.dom.semanticsHost;

/// The last scene element rendered by the [render] method.
DomElement? get sceneElement => _sceneElement;
Expand All @@ -88,27 +63,8 @@ class FlutterViewEmbedder {
}
}

/// The element that captures input events, such as pointer events.
///
/// If semantics is enabled this element also contains the semantics DOM tree,
/// which captures semantics input events. The semantics DOM tree must be a
/// child of the glass pane element so that events bubble up to the glass pane
/// if they are not handled by semantics.
DomElement get flutterViewElementDEPRECATED => _flutterViewElement;
late DomElement _flutterViewElement;

DomElement get glassPaneElementDEPRECATED => _glassPaneElement;
late DomElement _glassPaneElement;

/// The shadow root of the [glassPaneElement], which contains the whole Flutter app.
DomShadowRoot get glassPaneShadowDEPRECATED => _glassPaneShadow;
late DomShadowRoot _glassPaneShadow;

DomElement get textEditingHostNodeDEPRECATED => _textEditingHostNode;
late DomElement _textEditingHostNode;

DomElement get announcementsHostDEPRECATED => _announcementsHost;
late DomElement _announcementsHost;
DomElement get _flutterViewElement => window.dom.rootElement;
DomShadowRoot get _glassPaneShadow => window.dom.renderingHost;

void reset() {
// How was the current renderer selected?
Expand All @@ -127,77 +83,9 @@ class FlutterViewEmbedder {
},
);

// Create and inject the [_glassPaneElement].
_flutterViewElement = domDocument.createElement(DomManager.flutterViewTagName);
_glassPaneElement = domDocument.createElement(DomManager.glassPaneTagName);

// This must be attached to the DOM now, so the engine can create a host
// node (ShadowDOM or a fallback) next.
//
// The embeddingStrategy will take care of cleaning up the glassPane on hot
// restart.
window.embeddingStrategy.attachGlassPane(_flutterViewElement);
_flutterViewElement.appendChild(_glassPaneElement);

if (getJsProperty<Object?>(_glassPaneElement, 'attachShadow') == null) {
throw UnsupportedError('ShadowDOM is not supported in this browser.');
}

// Create a [HostNode] under the glass pane element, and attach everything
// there, instead of directly underneath the glass panel.
final DomShadowRoot shadowRoot = _glassPaneElement.attachShadow(<String, dynamic>{
'mode': 'open',
// This needs to stay false to prevent issues like this:
// - https://github.com/flutter/flutter/issues/85759
'delegatesFocus': false,
});
_glassPaneShadow = shadowRoot;

StyleManager.attachGlobalStyles(
node: shadowRoot,
styleId: 'flt-internals-stylesheet',
styleNonce: configuration.nonce,
cssSelectorPrefix: '',
);

_textEditingHostNode =
createTextEditingHostNode(_flutterViewElement, configuration.nonce);

_sceneHostElement = domDocument.createElement(DomManager.sceneHostTagName);
StyleManager.styleSceneHost(
_sceneHostElement,
debugShowSemanticsNodes: configuration.debugShowSemanticsNodes,
);

renderer.reset(this);

_semanticsHostElement = domDocument.createElement(DomManager.semanticsHostTagName);
StyleManager.styleSemanticsHost(
_semanticsHostElement,
window.devicePixelRatio,
);

final DomElement accessibilityPlaceholder = EngineSemanticsOwner
.instance.semanticsHelper
.prepareAccessibilityPlaceholder();

_announcementsHost = createDomElement(DomManager.announcementsHostTagName);

shadowRoot.append(accessibilityPlaceholder);
shadowRoot.append(_sceneHostElement);
shadowRoot.append(_announcementsHost);

// 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.
_flutterViewElement.appendChild(_semanticsHostElement);
// TODO(mdebbar): Move these to `engine/initialization.dart`.

KeyboardBinding.initInstance();
PointerBinding.initInstance(
Expand Down Expand Up @@ -294,20 +182,3 @@ FlutterViewEmbedder ensureFlutterViewEmbedderInitialized() {
ensureImplicitViewInitialized();
return _flutterViewEmbedder ??= FlutterViewEmbedder();
}

/// Creates a node to host text editing elements and applies a stylesheet
/// to Flutter nodes that exist outside of the shadowDOM.
DomElement createTextEditingHostNode(DomElement root, String? nonce) {
StyleManager.attachGlobalStyles(
node: root,
styleId: 'flt-text-editing-stylesheet',
styleNonce: nonce,
cssSelectorPrefix: DomManager.flutterViewTagName,
);

final DomElement domElement =
domDocument.createElement('flt-text-editing-host');
root.appendChild(domElement);

return domElement;
}
141 changes: 126 additions & 15 deletions lib/web_ui/lib/src/engine/view_embedder/dom_manager.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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].
///
Expand All @@ -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,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks like devicePixelRatio is only used here, and also it seems like a bug? How do we update the semantics host when devicePixelRatio changes?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks like devicePixelRatio is only used here

Not sure what you mean here. Yes, it's not used anywhere else in this method.

and also it seems like a bug? How do we update the semantics host when devicePixelRatio changes?

The code that listens for resize events (and updates the semantics host) is still in FlutterViewEmbedder for now:

void _metricsDidChange(ui.Size? newSize) {
StyleManager.scaleSemanticsHost(
_semanticsHostElement,
window.devicePixelRatio,
);

I do plan to move it out of there though. Stay tuned.

Copy link
Contributor

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 need DomManager to be concerned about devicePixelRatio.

Having said that, I'm totally fine with not making this change in this PR.

Copy link
Contributor Author

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.

);

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';
Expand All @@ -47,38 +137,59 @@ 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;
final DomElement rootElement;

/// Hosts all platform view elements.
DomElement get platformViewsHost => _embedder.glassPaneElementDEPRECATED;
final DomElement platformViewsHost;

/// Hosts all rendering elements and canvases.
DomShadowRoot get renderingHost => _embedder.glassPaneShadowDEPRECATED;
final 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.
final DomElement sceneHost;

/// Hosts all text editing elements.
DomElement get textEditingHost => _embedder.textEditingHostNodeDEPRECATED;
final 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;
final DomElement semanticsHost;

/// This is where accessibility announcements are inserted.
DomElement get announcementsHost => _embedder.announcementsHostDEPRECATED;
final DomElement announcementsHost;
}

DomShadowRoot _attachShadowRoot(DomElement element) {
assert(
getJsProperty<Object?>(element, 'attachShadow') != null,
'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,
Copy link
Contributor

Choose a reason for hiding this comment

The 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.

Copy link
Contributor Author

Choose a reason for hiding this comment

The 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.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

});
}
Loading