calculateResize()

in desktop/flipper-plugin/src/ui/Interactive.tsx [406:494]


  calculateResize(event: MouseEvent) {
    const {resizingInitialCursor, resizingInitialRect, resizingSides} =
      this.state;
    if (!resizingSides || !resizingInitialCursor) {
      return;
    }

    const deltaLeft = resizingInitialCursor.left - event.clientX;
    const deltaTop = resizingInitialCursor.top - event.clientY;

    let newLeft = resizingInitialRect!.left;
    let newTop = resizingInitialRect!.top;

    let newWidth = resizingInitialRect!.width;
    let newHeight = resizingInitialRect!.height;

    // right
    if (resizingSides.right === true) {
      newWidth -= deltaLeft;
    }

    // bottom
    if (resizingSides.bottom === true) {
      newHeight -= deltaTop;
    }

    const rect = this.getRect();

    // left
    if (resizingSides.left === true) {
      newLeft -= deltaLeft;
      newWidth += deltaLeft;

      if (this.props.movable === true) {
        // prevent from being shrunk past the minimum width
        const right = rect.left + rect.width;
        const maxLeft = right - (this.props.minWidth || 0);

        let cleanLeft = Math.max(0, newLeft);
        cleanLeft = Math.min(cleanLeft, maxLeft);
        newWidth -= Math.abs(newLeft - cleanLeft);
        newLeft = cleanLeft;
      }
    }

    // top
    if (resizingSides.top === true) {
      newTop -= deltaTop;
      newHeight += deltaTop;

      if (this.props.movable === true) {
        // prevent from being shrunk past the minimum height
        const bottom = rect.top + rect.height;
        const maxTop = bottom - (this.props.minHeight || 0);

        let cleanTop = Math.max(0, newTop);
        cleanTop = Math.min(cleanTop, maxTop);
        newHeight += newTop - cleanTop;
        newTop = cleanTop;
      }
    }

    if (event.altKey) {
      const windows = this.getPossibleTargetWindows(rect);

      if (resizingSides.left === true) {
        const newLeft2 = maybeSnapLeft(rect, windows, newLeft);
        newWidth += newLeft - newLeft2;
        newLeft = newLeft2;
      }

      if (resizingSides.top === true) {
        const newTop2 = maybeSnapTop(rect, windows, newTop);
        newHeight += newTop - newTop2;
        newTop = newTop2;
      }

      if (resizingSides.bottom === true) {
        newHeight = maybeSnapTop(rect, windows, newTop + newHeight) - newTop;
      }

      if (resizingSides.right === true) {
        newWidth = maybeSnapLeft(rect, windows, newLeft + newWidth) - newLeft;
      }
    }

    this.move(newTop, newLeft, event);
    this.resize(newWidth, newHeight);
  }