Skip to content

Commit

Permalink
chore: lazy compute offset
Browse files Browse the repository at this point in the history
  • Loading branch information
yuanyan committed May 14, 2021
1 parent 480ccc1 commit 24e0a19
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 5 deletions.
2 changes: 1 addition & 1 deletion kraken/lib/src/dom/elements/img.dart
Original file line number Diff line number Diff line change
Expand Up @@ -262,7 +262,7 @@ class ImageElement extends Element {
}

// Delay image size setting to next frame to make sure image has been layouted
// to wait for percentage size to be caculated correctly in the case of image has been cached
// to wait for percentage size to be calculated correctly in the case of image has been cached
bool _hasImageLayoutCallbackPending = false;
void _handleImageResizeAfterLayout() {
if (_hasImageLayoutCallbackPending) return;
Expand Down
10 changes: 6 additions & 4 deletions kraken/lib/src/rendering/intersection_observer.dart
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import 'dart:ui' as ui;

import 'package:flutter/foundation.dart';
import 'package:flutter/rendering.dart';
import 'package:flutter/scheduler.dart';
import 'package:kraken/rendering.dart';

/// Returns a sequence containing the specified [Layer] and all of its
Expand Down Expand Up @@ -88,7 +89,7 @@ mixin RenderIntersectionObserverMixin on RenderBox {
context.pushLayer(intersectionObserverLayer, callback, offset);
}
}

int _id = 0;
class IntersectionObserverLayer extends ContainerLayer {
IntersectionObserverLayer(
{@required Size elementSize, @required Offset paintOffset, @required this.onIntersectionChange, @required this.rootRenderObject})
Expand All @@ -103,6 +104,7 @@ class IntersectionObserverLayer extends ContainerLayer {

/// The size of the corresponding element.
Size _elementSize;
int id = _id++;

/// Offset to the start of the element, in local coordinates.
Offset _elementOffset;
Expand Down Expand Up @@ -201,8 +203,7 @@ class IntersectionObserverLayer extends ContainerLayer {
if (!_isScheduled) {
_isScheduled = true;
scheduleMicrotask(() {
_processCallbacks();
_isScheduled = false;
SchedulerBinding.instance.scheduleTask<void>(_processCallbacks, Priority.touch);
});
}
}
Expand Down Expand Up @@ -233,7 +234,7 @@ class IntersectionObserverLayer extends ContainerLayer {

if (curClipRect != null) {
// This is O(n^2) WRT the depth of the tree since `_localRectToGlobal`
// also walks up the tree. In practice there probably will be a small
// also walks up the tree. In practice there probably will be a small
// number of clipping layers in the chain, so it might not be a problem.
// Alternatively we could cache transformations and clipping rectangles.
curClipRect = _localRectToGlobal(parentLayer, curClipRect);
Expand Down Expand Up @@ -279,6 +280,7 @@ class IntersectionObserverLayer extends ContainerLayer {

/// Executes visibility callbacks for all updated.
void _processCallbacks() {
_isScheduled = false;
if (!attached) {
_fireCallback(IntersectionObserverEntry(size: Size.zero));
return;
Expand Down

0 comments on commit 24e0a19

Please sign in to comment.