importStateHistory method

void importStateHistory(
  1. ImportStateHistory import
)

Imports state history and performs necessary recalculations.

If ImportStateHistory.configs.recalculateSizeAndPosition is true, it recalculates the position and size of layers. It adjusts the scale and offset of each layer based on the image size and the editor's dimensions.

If ImportStateHistory.configs.mergeMode is ImportEditorMergeMode.replace, it replaces the current state history with the imported one. Otherwise, it merges the imported state history with the current one based on the merge mode.

After importing, it updates the UI by calling setState() and the optional onUpdateUI callback.

Implementation

void importStateHistory(ImportStateHistory import) {
  /// Recalculate position and size
  if (import.configs.recalculateSizeAndPosition ||
      import.version == ExportImportVersion.version_1_0_0) {
    Size imgSize = import.imgSize;
    for (EditorStateHistory el in import.stateHistory) {
      for (Layer layer in el.layers) {
        if (import.configs.recalculateSizeAndPosition) {
          // Calculate scaling factors for width and height
          double scaleWidth =
              _sizesManager.decodedImageSize.width / imgSize.width;
          double scaleHeight =
              _sizesManager.decodedImageSize.height / imgSize.height;

          if (scaleWidth == 0 || scaleWidth.isInfinite) scaleWidth = 1;
          if (scaleHeight == 0 || scaleHeight.isInfinite) scaleHeight = 1;

          // Choose the middle value between scaleWidth and scaleHeight
          double scale = (scaleWidth + scaleHeight) / 2;

          // Adjust the scale
          layer.scale *= scale;

          // Adjust the offset
          layer.offset = Offset(
            layer.offset.dx * scaleWidth,
            layer.offset.dy * scaleHeight,
          );
        }
        if (import.version == ExportImportVersion.version_1_0_0) {
          layer.offset -= Offset(
            _sizesManager.bodySize.width / 2 -
                _sizesManager.imageScreenGaps.left,
            _sizesManager.bodySize.height / 2 -
                _sizesManager.imageScreenGaps.top,
          );
        }
      }
    }
  }

  if (import.configs.mergeMode == ImportEditorMergeMode.replace) {
    _stateManager.position = import.editorPosition + 1;
    _stateManager.stateHistory = [
      EditorStateHistory(
        transformConfigs: TransformConfigs.empty(),
        blur: 0,
        filters: [],
        layers: [],
      ),
      ...import.stateHistory
    ];
    for (var i = 0; i < import.stateHistory.length; i++) {
      if (i < import.stateHistory.length - 1) {
        _controllers.screenshot
            .addEmptyScreenshot(screenshots: _stateManager.screenshots);
      } else {
        _takeScreenshot();
      }
    }
  } else {
    for (var el in import.stateHistory) {
      if (import.configs.mergeMode == ImportEditorMergeMode.merge) {
        el.layers.insertAll(0, stateHistory.last.layers);
        el.filters.insertAll(0, stateHistory.last.filters);
      }
    }

    for (var i = 0; i < import.stateHistory.length; i++) {
      stateHistory.add(import.stateHistory[i]);
      if (i < import.stateHistory.length - 1) {
        _controllers.screenshot
            .addEmptyScreenshot(screenshots: _stateManager.screenshots);
      } else {
        _takeScreenshot();
      }
    }
    _stateManager.position = stateHistory.length - 1;
  }

  setState(() {});
  mainEditorCallbacks?.handleUpdateUI();
}