arrangedOrder method

List<CanvasChildId>? arrangedOrder(
  1. Iterable<CanvasChildId> ids,
  2. CanvasArrange action
)

Proposes the complete back-to-front order without changing the canvas. Returns null for no change; validation matches canArrange.

Implementation

List<CanvasChildId>? arrangedOrder(
  Iterable<CanvasChildId> ids,
  CanvasArrange action,
) {
  final targets = ids.toSet();
  for (final id in targets) {
    _requireChild(id);
  }
  if (targets.isEmpty) return null;
  final order = childOrder;
  final bundle = order.where(targets.contains).toList();
  final others = order.where((id) => !targets.contains(id)).toList();
  var insertion = action == CanvasArrange.front ? others.length : 0;
  if (action == CanvasArrange.forward || action == CanvasArrange.backward) {
    final forward = action == CanvasArrange.forward;
    final step = forward ? 1 : -1;
    final boundary = order.indexOf(forward ? bundle.last : bundle.first);
    if (boundary + step < 0 || boundary + step >= order.length) return null;
    final footprints = bundle.map(_childFootprint).toList();
    CanvasChildId? crossed;
    for (
      var index = boundary + step;
      index >= 0 && index < order.length;
      index += step
    ) {
      final candidate = order[index];
      if (targets.contains(candidate)) continue;
      final footprint = _childFootprint(candidate);
      if (footprints.any((path) => _footprintsOverlap(path, footprint))) {
        crossed = candidate;
        break;
      }
    }
    if (crossed == null) return null;
    insertion = others.indexOf(crossed) + (forward ? 1 : 0);
  }
  final result = others..insertAll(insertion, bundle);
  return listEquals(order, result) ? null : result;
}