Skip to content

Commit

Permalink
Update layers when widget is updated without invalidating the layout
Browse files Browse the repository at this point in the history
  • Loading branch information
angelosilvestre committed Dec 11, 2024
1 parent 0714bcc commit ab05a09
Showing 1 changed file with 50 additions and 25 deletions.
75 changes: 50 additions & 25 deletions super_editor/lib/src/infrastructure/content_layers.dart
Original file line number Diff line number Diff line change
Expand Up @@ -275,30 +275,45 @@ class ContentLayersElement extends RenderObjectElement {
contentLayersLog.finer("ContentLayersElement - (re)building layers");

owner!.buildScope(this, () {
final List<Element> underlays = List<Element>.filled(widget.underlays.length, _NullElement.instance);
for (int i = 0; i < underlays.length; i += 1) {
late final Element child;
if (i > _underlays.length - 1) {
child = inflateWidget(widget.underlays[i](this), _UnderlaySlot(i));
} else {
child = super.updateChild(_underlays[i], widget.underlays[i](this), _UnderlaySlot(i))!;
}
underlays[i] = child;
_buildLayersWithExistingScope();
});
}

/// Builds the underlays and overlays without establishing a new build scope.
///
/// We build the layers in two situations:
///
/// 1. When the content's layout is dirty. This happens during layout phase, when we need to
/// establish a build scope. This is done when [buildLayers] is called.
/// 2. When the content's layout is clean. This happens when [update] is called, but only
/// non-layout changes happened, like changing a color. In this case, we are already
/// inside a build scope, so we can't try to establish a new one.
///
/// See [BuildOwner.buildScope] for more information.
void _buildLayersWithExistingScope() {
final List<Element> underlays = List<Element>.filled(widget.underlays.length, _NullElement.instance);
for (int i = 0; i < underlays.length; i += 1) {
late final Element child;
if (i > _underlays.length - 1) {
child = inflateWidget(widget.underlays[i](this), _UnderlaySlot(i));
} else {
child = super.updateChild(_underlays[i], widget.underlays[i](this), _UnderlaySlot(i))!;
}
_underlays = underlays;

final List<Element> overlays = List<Element>.filled(widget.overlays.length, _NullElement.instance);
for (int i = 0; i < overlays.length; i += 1) {
late final Element child;
if (i > _overlays.length - 1) {
child = inflateWidget(widget.overlays[i](this), _OverlaySlot(i));
} else {
child = super.updateChild(_overlays[i], widget.overlays[i](this), _OverlaySlot(i))!;
}
overlays[i] = child;
underlays[i] = child;
}
_underlays = underlays;

final List<Element> overlays = List<Element>.filled(widget.overlays.length, _NullElement.instance);
for (int i = 0; i < overlays.length; i += 1) {
late final Element child;
if (i > _overlays.length - 1) {
child = inflateWidget(widget.overlays[i](this), _OverlaySlot(i));
} else {
child = super.updateChild(_overlays[i], widget.overlays[i](this), _OverlaySlot(i))!;
}
_overlays = overlays;
});
overlays[i] = child;
}
_overlays = overlays;
}

@override
Expand Down Expand Up @@ -345,9 +360,19 @@ class ContentLayersElement extends RenderObjectElement {
assert(!debugChildrenHaveDuplicateKeys(widget, [newContent]));

_content = updateChild(_content, newContent, _contentSlot);
// super.update() and updateChild() is where the framework reparents
// forgotten children. Therefore, at this point, the framework is
// done with the concept of forgotten children, so we clear our

if (!renderObject.contentNeedsLayout) {
// Layout has already run. No layout bounds changed. There might be a
// non-layout change that needs to be painted, e.g., change to theme brightness.
// Re-build all layers, which is safe to do because no layout constraints changed.
_buildLayersWithExistingScope();
}
// Else, dirty content layout will cause this whole widget to re-layout. The
// layers will be re-built during that layout pass.

// super.update() and updateChild() is where the framework reparents
// forgotten children. Therefore, at this point, the framework is
// done with the concept of forgotten children, so we clear our
// local cache of them, too.
_forgottenChildren.clear();
}
Expand Down

0 comments on commit ab05a09

Please sign in to comment.