handleMouseEvent method

bool handleMouseEvent(
  1. MouseEvent event
)

Routes a mouse event, performing coordinate translation and triggering click/drag handlers.

Implementation

bool handleMouseEvent(MouseEvent event) {
  if (event.type == MouseEventType.release ||
      event.type == MouseEventType.press) {
    _draggingWindow = null;
    _resizingWindow = null;
    _resizeBottomLeft = false;
    _resizeBottomRight = false;
  }

  final sx = event.x - 1;
  final sy = event.y - 1;

  if (event.type == MouseEventType.drag) {
    if (_resizingWindow != null) {
      Tracer.record(_traceWindowResizeId, Phase.begin);
      try {
        final b = _resizingWindow!.bounds;
        if (_resizeBottomRight) {
          final newWidth = (sx - b.x + 1).clamp(10, 100);
          final newHeight = (sy - b.y + 1).clamp(5, 40);
          _resizingWindow!.bounds = Rect(b.x, b.y, newWidth, newHeight);
        } else if (_resizeBottomLeft) {
          final rightEdge = b.x + b.width;
          final newX = sx.clamp(0, rightEdge - 10);
          final newWidth = rightEdge - newX;
          final newHeight = (sy - b.y + 1).clamp(5, 40);
          _resizingWindow!.bounds = Rect(newX, b.y, newWidth, newHeight);
        }
      } finally {
        Tracer.record(_traceWindowResizeId, Phase.end);
      }
      return true;
    }

    if (_draggingWindow != null) {
      final newX = sx - _dragStartX;
      final newY = sy - _dragStartY;
      _draggingWindow!.bounds = Rect(
        newX,
        newY,
        _draggingWindow!.bounds.width,
        _draggingWindow!.bounds.height,
      );
      return true;
    }
  }

  final win = findWindowAt(sx, sy);
  if (win != null) {
    final localX = sx - win.bounds.x;
    final localY = sy - win.bounds.y;

    if (event.type == MouseEventType.press) {
      win.focusNode.requestFocus();
      bringToFront(win);

      // Check resize handles at bottom-left or bottom-right corners (with 2-cell tolerance)
      final isAtBottomBorder = localY == win.bounds.height - 1;
      final isAtLeftBorder = localX == 0;
      final isAtRightBorder = localX == win.bounds.width - 1;

      final isNearBottomLeft =
          (isAtBottomBorder && localX <= 2) ||
          (isAtLeftBorder && localY >= win.bounds.height - 3);
      final isNearBottomRight =
          (isAtBottomBorder && localX >= win.bounds.width - 3) ||
          (isAtRightBorder && localY >= win.bounds.height - 3);

      if (isNearBottomLeft) {
        _resizingWindow = win;
        _resizeBottomLeft = true;
        _resizeBottomRight = false;
        return true;
      } else if (isNearBottomRight) {
        _resizingWindow = win;
        _resizeBottomLeft = false;
        _resizeBottomRight = true;
        return true;
      }

      // Start dragging if clicked on the title
      if (win.isPositionOnTitle(localX, localY)) {
        _draggingWindow = win;
        _dragStartX = localX;
        _dragStartY = localY;
        return true;
      }
    }

    if (win.onMouseEvent != null) {
      win.onMouseEvent!(event, localX, localY);
    }
    return true;
  }
  return false;
}