addPane method

void addPane({
  1. required Axis axis,
  2. required String anchorCellId,
  3. required String anchorPaneId,
  4. required String newPaneId,
  5. required bool insertPrev,
})

Implementation

void addPane({
  required Axis axis,
  required String anchorCellId,
  required String anchorPaneId,
  required String newPaneId,
  required bool insertPrev,
}) {
  // handle the case where there is only one pane
  if (api.workspace.workspaceConfigs.panesLayout is SinglePaneList) {
    final anchorCell = findCellById(anchorCellId);
    final items = splitIntoTwoPanes(
      paneIdA: insertPrev ? newPaneId : anchorPaneId,
      paneIdB: insertPrev ? anchorPaneId : newPaneId,
      width: anchorCell.width,
      height: anchorCell.height,
      cellIdA: insertPrev
          ? null
          : utils
              .generateId(), // generate a new ID for the previous root (given by anchorCellId)
      cellIdB: insertPrev ? utils.generateId() : null,
      axis: axis,
    );
    // to replace the root, its ID must be provided to the newly inserted cell
    api.workspace.workspaceConfigs.panesLayout = axis == Axis.horizontal
        ? HorizontalPaneList(
            items,
            width: anchorCell.width,
            height: anchorCell.height,
            id: api.workspace.workspaceConfigs.panesLayout.id,
          )
        : VerticalPaneList(
            items,
            width: anchorCell.width,
            height: anchorCell.height,
            id: api.workspace.workspaceConfigs.panesLayout.id,
          );
    AffogatoEvents.windowPaneCellRequestReloadEventsController.add(
        WindowPaneCellRequestReloadEvent(
            api.workspace.workspaceConfigs.panesLayout.id));
  } else {
    // find the path to the anchor pane
    final path =
        (api.workspace.workspaceConfigs.panesLayout as MultiplePaneList)
            .pathToPane(anchorPaneId);
    // determine the parent of the anchor
    MultiplePaneList parentOfAnchor =
        path?.last ?? (throw Exception('Parent of anchor not found'));

    // determine the index of the anchor cell
    final int index = parentOfAnchor.value.indexWhere(
        (pane) => pane is SinglePaneList && pane.paneId == anchorPaneId);
    final PaneList anchorCell = parentOfAnchor.value[index];
    // resolve any axis conflicts
    final Axis childAxis =
        parentOfAnchor is VerticalPaneList ? Axis.vertical : Axis.horizontal;
    final bool axisIsConflicting = axis != childAxis;
    if (axisIsConflicting) {
      // create a new pane along the insertion axis, `axis`
      final items = splitIntoTwoPanes(
        paneIdA: insertPrev ? newPaneId : anchorPaneId,
        paneIdB: insertPrev ? anchorPaneId : newPaneId,
        width: anchorCell.width,
        height: anchorCell.height,
        axis: axis,
      );
      // update the parent to resolve the axis conflict
      parentOfAnchor.value[index] = axis == Axis.horizontal
          ? HorizontalPaneList(
              items,
              width: anchorCell.width,
              height: anchorCell.height,
              id: parentOfAnchor.value[index].id,
            )
          : VerticalPaneList(
              items,
              width: anchorCell.width,
              height: anchorCell.height,
              id: parentOfAnchor.value[index].id,
            );
    } else {
      final double newWidth = axis == Axis.horizontal
          ? parentOfAnchor.width / (parentOfAnchor.value.length + 1)
          : anchorCell.width;
      final double newHeight = axis == Axis.horizontal
          ? anchorCell.height
          : parentOfAnchor.height / (parentOfAnchor.value.length + 1);

      parentOfAnchor.value.insert(
        insertPrev ? index : index + 1,
        SinglePaneList(
          newPaneId,
          width: newWidth,
          height: newHeight,
        ),
      );
      for (int i = 0; i < parentOfAnchor.value.length; i++) {
        if (i != index) {
          axis == Axis.horizontal
              ? parentOfAnchor.value[i].width = newWidth
              : parentOfAnchor.value[i].height = newHeight;
        }
      }
    }
    AffogatoEvents.windowPaneCellRequestReloadEventsController
        .add(WindowPaneCellRequestReloadEvent(parentOfAnchor.id));
  }
}