getOffsetToReveal method
- RenderObject target,
- double alignment, {
- Rect? rect,
- Axis? axis,
Returns the offset that would be needed to reveal the target
RenderObject.
This is used by RenderViewportBase.showInViewport, which is itself used by RenderObject.showOnScreen for RenderViewportBase, which is in turn used by the semantics system to implement scrolling for accessibility tools.
The optional rect
parameter describes which area of that target
object
should be revealed in the viewport. If rect
is null, the entire
target
RenderObject (as defined by its RenderObject.paintBounds)
will be revealed. If rect
is provided it has to be given in the
coordinate system of the target
object.
The alignment
argument describes where the target should be positioned
after applying the returned offset. If alignment
is 0.0, the child must
be positioned as close to the leading edge of the viewport as possible. If
alignment
is 1.0, the child must be positioned as close to the trailing
edge of the viewport as possible. If alignment
is 0.5, the child must be
positioned as close to the center of the viewport as possible.
The target
might not be a direct child of this viewport but it must be a
descendant of the viewport. Other viewports in between this viewport and
the target
will not be adjusted.
This method assumes that the content of the viewport moves linearly, i.e.
when the offset of the viewport is changed by x then target
also moves
by x within the viewport.
The optional Axis is used by
RenderTwoDimensionalViewport.getOffsetToReveal to
determine which of the two axes to compute an offset for. One dimensional
subclasses like RenderViewportBase and RenderListWheelViewport
will ignore the axis
value if provided, since there is only one Axis.
If the axis
is omitted when called on RenderTwoDimensionalViewport,
the RenderTwoDimensionalViewport.mainAxis is used. To reveal an object
properly in both axes, this method should be called for each Axis as the
returned RevealedOffset.offset only represents the offset of one of the
the two ScrollPositions.
See also:
- RevealedOffset, which describes the return value of this method.
Implementation
@override
RevealedOffset getOffsetToReveal(
RenderObject target,
double alignment, {
Rect? rect,
Axis? axis,
}) {
// One dimensional viewport has only one axis, override if it was
// provided/may be mismatched.
axis = this.axis;
// Steps to convert `rect` (from a RenderBox coordinate system) to its
// scroll offset within this viewport (not in the exact order):
//
// 1. Pick the outermost RenderBox (between which, and the viewport, there
// is nothing but RenderSlivers) as an intermediate reference frame
// (the `pivot`), convert `rect` to that coordinate space.
//
// 2. Convert `rect` from the `pivot` coordinate space to its sliver
// parent's sliver coordinate system (i.e., to a scroll offset), based on
// the axis direction and growth direction of the parent.
//
// 3. Convert the scroll offset to its sliver parent's coordinate space
// using `childScrollOffset`, until we reach the viewport.
//
// 4. Make the final conversion from the outmost sliver to the viewport
// using `scrollOffsetOf`.
double leadingScrollOffset = 0.0;
// Starting at `target` and walking towards the root:
// - `child` will be the last object before we reach this viewport, and
// - `pivot` will be the last RenderBox before we reach this viewport.
RenderObject child = target;
RenderBox? pivot;
bool onlySlivers = target is RenderSliver; // ... between viewport and `target` (`target` included).
while (child.parent != this) {
final RenderObject parent = child.parent!;
if (child is RenderBox) {
pivot = child;
}
if (parent is RenderSliver) {
leadingScrollOffset += parent.childScrollOffset(child)!;
} else {
onlySlivers = false;
leadingScrollOffset = 0.0;
}
child = parent;
}
// `rect` in the new intermediate coordinate system.
final Rect rectLocal;
// Our new reference frame render object's main axis extent.
final double pivotExtent;
final GrowthDirection growthDirection;
// `leadingScrollOffset` is currently the scrollOffset of our new reference
// frame (`pivot` or `target`), within `child`.
if (pivot != null) {
assert(pivot.parent != null);
assert(pivot.parent != this);
assert(pivot != this);
assert(pivot.parent is RenderSliver); // TODO(abarth): Support other kinds of render objects besides slivers.
final RenderSliver pivotParent = pivot.parent! as RenderSliver;
growthDirection = pivotParent.constraints.growthDirection;
pivotExtent = switch (axis) {
Axis.horizontal => pivot.size.width,
Axis.vertical => pivot.size.height,
};
rect ??= target.paintBounds;
rectLocal = MatrixUtils.transformRect(target.getTransformTo(pivot), rect);
} else if (onlySlivers) {
// `pivot` does not exist. We'll have to make up one from `target`, the
// innermost sliver.
final RenderSliver targetSliver = target as RenderSliver;
growthDirection = targetSliver.constraints.growthDirection;
// TODO(LongCatIsLooong): make sure this works if `targetSliver` is a
// persistent header, when #56413 relands.
pivotExtent = targetSliver.geometry!.scrollExtent;
if (rect == null) {
switch (axis) {
case Axis.horizontal:
rect = Rect.fromLTWH(
0, 0,
targetSliver.geometry!.scrollExtent,
targetSliver.constraints.crossAxisExtent,
);
case Axis.vertical:
rect = Rect.fromLTWH(
0, 0,
targetSliver.constraints.crossAxisExtent,
targetSliver.geometry!.scrollExtent,
);
}
}
rectLocal = rect;
} else {
assert(rect != null);
return RevealedOffset(offset: offset.pixels, rect: rect!);
}
assert(child.parent == this);
assert(child is RenderSliver);
final RenderSliver sliver = child as RenderSliver;
// The scroll offset of `rect` within `child`.
leadingScrollOffset += switch (applyGrowthDirectionToAxisDirection(axisDirection, growthDirection)) {
AxisDirection.up => pivotExtent - rectLocal.bottom,
AxisDirection.left => pivotExtent - rectLocal.right,
AxisDirection.right => rectLocal.left,
AxisDirection.down => rectLocal.top,
};
// So far leadingScrollOffset is the scroll offset of `rect` in the `child`
// sliver's sliver coordinate system. The sign of this value indicates
// whether the `rect` protrudes the leading edge of the `child` sliver. When
// this value is non-negative and `child`'s `maxScrollObstructionExtent` is
// greater than 0, we assume `rect` can't be obstructed by the leading edge
// of the viewport (i.e. its pinned to the leading edge).
final bool isPinned = sliver.geometry!.maxScrollObstructionExtent > 0 && leadingScrollOffset >= 0;
// The scroll offset in the viewport to `rect`.
leadingScrollOffset = scrollOffsetOf(sliver, leadingScrollOffset);
// This step assumes the viewport's layout is up-to-date, i.e., if
// offset.pixels is changed after the last performLayout, the new scroll
// position will not be accounted for.
final Matrix4 transform = target.getTransformTo(this);
Rect targetRect = MatrixUtils.transformRect(transform, rect);
final double extentOfPinnedSlivers = maxScrollObstructionExtentBefore(sliver);
switch (sliver.constraints.growthDirection) {
case GrowthDirection.forward:
if (isPinned && alignment <= 0) {
return RevealedOffset(offset: double.infinity, rect: targetRect);
}
leadingScrollOffset -= extentOfPinnedSlivers;
case GrowthDirection.reverse:
if (isPinned && alignment >= 1) {
return RevealedOffset(offset: double.negativeInfinity, rect: targetRect);
}
// If child's growth direction is reverse, when viewport.offset is
// `leadingScrollOffset`, it is positioned just outside of the leading
// edge of the viewport.
leadingScrollOffset -= switch (axis) {
Axis.vertical => targetRect.height,
Axis.horizontal => targetRect.width,
};
}
final double mainAxisExtentDifference = switch (axis) {
Axis.horizontal => size.width - extentOfPinnedSlivers - rectLocal.width,
Axis.vertical => size.height - extentOfPinnedSlivers - rectLocal.height,
};
final double targetOffset = leadingScrollOffset - mainAxisExtentDifference * alignment;
final double offsetDifference = offset.pixels - targetOffset;
targetRect = switch (axisDirection) {
AxisDirection.up => targetRect.translate(0.0, -offsetDifference),
AxisDirection.down => targetRect.translate(0.0, offsetDifference),
AxisDirection.left => targetRect.translate(-offsetDifference, 0.0),
AxisDirection.right => targetRect.translate( offsetDifference, 0.0),
};
return RevealedOffset(offset: targetOffset, rect: targetRect);
}