setNextElementPosition method

void setNextElementPosition(
  1. PCanvasElement2D element
)

Implementation

void setNextElementPosition(PCanvasElement2D element) {
  if (!hasElements) {
    element.setXY(spacing, spacing);
  } else {
    var lastIdx = elementsLength - 1;
    var last = getElement(lastIdx);

    var p = last.getXY();

    var p2 = p.setX(p.x + last.width + spacing);

    if (p2.x + element.width > width) {
      var lastLine = <PCanvasElement2D>[];
      for (var i = lastIdx; i >= 0; --i) {
        var e = getElement(i);

        if (e.y == last.y) {
          lastLine.add(e);
        } else {
          break;
        }
      }

      assert(lastLine.isNotEmpty);

      var lineHeight = lastLine.map((e) => e.height).max;

      p2 = p.setXY(spacing, p2.y + lineHeight + spacing);
    }

    element.setXY(p2.x, p2.y);
  }
}