Files
pipelines/app/lib/features/chat/stack/chat_stack_pose.dart
T

203 lines
5.4 KiB
Dart

import 'dart:math' as math;
import 'dart:ui' show lerpDouble;
import 'package:flutter/rendering.dart';
import 'package:flutter/widgets.dart';
/// Where one avatar of the chat stack sits relative to the stack's slot: a
/// paint-only offset, a turn about its centre and a scale.
@immutable
class ChatStackPose {
const ChatStackPose({
this.offset = Offset.zero,
this.turn = 0,
this.scale = 1,
});
static const rest = ChatStackPose();
final Offset offset;
/// Radians, clockwise.
final double turn;
final double scale;
/// The same pose for a right-to-left layout: offsets and turns mirror.
ChatStackPose mirrored() => ChatStackPose(
offset: Offset(-offset.dx, offset.dy),
turn: -turn,
scale: scale,
);
@override
bool operator ==(Object other) =>
other is ChatStackPose &&
other.offset == offset &&
other.turn == turn &&
other.scale == scale;
@override
int get hashCode => Object.hash(offset, turn, scale);
}
/// The collapsed stack's layers, top first: each further layer peeks out a
/// little higher, turned a few degrees the other way and slightly smaller,
/// so the pile reads as cards rather than one disc.
const List<ChatStackPose> chatStackLayers = [
ChatStackPose(),
ChatStackPose(offset: Offset(-4, -6), turn: -7 * math.pi / 180, scale: .94),
ChatStackPose(offset: Offset(4, -11), turn: 6 * math.pi / 180, scale: .88),
ChatStackPose(offset: Offset(-1, -15), turn: -4 * math.pi / 180, scale: .82),
];
/// How many avatars the collapsed stack draws.
int get chatStackVisibleLayers => chatStackLayers.length;
/// The pose of the column's item [index] (0 nearest the thumb), [pitch]
/// apart.
ChatStackPose chatStackColumnPose(int index, double pitch) =>
ChatStackPose(offset: Offset(0, -index * pitch));
/// Paints [child] moved from [from] to [to] as [progress] runs, without
/// rebuilding or relaying out: each frame only repaints the transform, and
/// hit tests follow it. The matrix and the layer are reused across frames.
class ChatStackPoseTransition extends SingleChildRenderObjectWidget {
const ChatStackPoseTransition({
super.key,
required this.progress,
required this.from,
required this.to,
super.child,
});
final Animation<double> progress;
final ChatStackPose from;
final ChatStackPose to;
@override
RenderChatStackPose createRenderObject(BuildContext context) =>
RenderChatStackPose(progress: progress, from: from, to: to);
@override
void updateRenderObject(
BuildContext context,
RenderChatStackPose renderObject,
) {
renderObject
..progress = progress
..from = from
..to = to;
}
}
class RenderChatStackPose extends RenderProxyBox {
RenderChatStackPose({
required Animation<double> progress,
required ChatStackPose from,
required ChatStackPose to,
}) {
_progress = progress;
_from = from;
_to = to;
}
late Animation<double> _progress;
set progress(Animation<double> value) {
if (identical(value, _progress)) return;
if (attached) _progress.removeListener(markNeedsPaint);
_progress = value;
if (attached) _progress.addListener(markNeedsPaint);
markNeedsPaint();
}
late ChatStackPose _from;
set from(ChatStackPose value) {
if (value == _from) return;
_from = value;
markNeedsPaint();
}
late ChatStackPose _to;
set to(ChatStackPose value) {
if (value == _to) return;
_to = value;
markNeedsPaint();
}
final Matrix4 _matrix = Matrix4.identity();
final LayerHandle<TransformLayer> _layer = LayerHandle<TransformLayer>();
@override
void attach(PipelineOwner owner) {
super.attach(owner);
_progress.addListener(markNeedsPaint);
}
@override
void detach() {
_progress.removeListener(markNeedsPaint);
super.detach();
}
@override
void dispose() {
_layer.layer = null;
super.dispose();
}
@override
bool get alwaysNeedsCompositing => child != null;
/// Fills the reused matrix for the current progress, about the centre.
Matrix4 _current() {
final t = _progress.value;
final dx = lerpDouble(_from.offset.dx, _to.offset.dx, t)!;
final dy = lerpDouble(_from.offset.dy, _to.offset.dy, t)!;
final turn = lerpDouble(_from.turn, _to.turn, t)!;
final scale = lerpDouble(_from.scale, _to.scale, t)!;
final cx = size.width / 2;
final cy = size.height / 2;
return _matrix
..setIdentity()
..translateByDouble(dx + cx, dy + cy, 0, 1)
..rotateZ(turn)
..scaleByDouble(scale, scale, 1, 1)
..translateByDouble(-cx, -cy, 0, 1);
}
@override
void paint(PaintingContext context, Offset offset) {
if (child == null) return;
_layer.layer = context.pushTransform(
needsCompositing,
offset,
_current(),
super.paint,
oldLayer: _layer.layer,
);
}
@override
bool hitTest(BoxHitTestResult result, {required Offset position}) =>
hitTestChildren(result, position: position);
@override
bool hitTestChildren(BoxHitTestResult result, {required Offset position}) {
final child = this.child;
if (child == null) return false;
return result.addWithPaintTransform(
transform: _current(),
position: position,
hitTest: (result, position) {
if (!child.size.contains(position)) return false;
return child.hitTest(result, position: position);
},
);
}
@override
void applyPaintTransform(RenderBox child, Matrix4 transform) {
transform.multiply(_current());
}
}