handleMouseEvent method

  1. @override
void handleMouseEvent(
  1. MouseEvent event,
  2. int localX,
  3. int localY
)
override

Handles a mouse event at local coordinates.

Implementation

@override
void handleMouseEvent(MouseEvent event, int localX, int localY) {
  final win = widget as Window;

  if (event.type == MouseEventType.press) {
    _dragStartedOnTitle = false;
    _resizeStartedOnBottomLeft = false;
    _resizeStartedOnBottomRight = false;

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

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

    if (isNearBottomLeft) {
      _resizeStartedOnBottomLeft = true;
      _lastX = event.globalX ?? event.x;
      _lastY = event.globalY ?? event.y;
    } else if (isNearBottomRight) {
      _resizeStartedOnBottomRight = true;
      _lastX = event.globalX ?? event.x;
      _lastY = event.globalY ?? event.y;
    } else if (win.isPositionOnTitle(localX, localY)) {
      _dragStartedOnTitle = true;
      _lastX = event.globalX ?? event.x;
      _lastY = event.globalY ?? event.y;
    }

    if (win.onMouseEvent != null) {
      win.onMouseEvent!(event, localX, localY);
    }
  } else if (event.type == MouseEventType.drag) {
    final mouseX = event.globalX ?? event.x;
    final mouseY = event.globalY ?? event.y;

    if (_dragStartedOnTitle) {
      final dx = mouseX - _lastX;
      final dy = mouseY - _lastY;
      _lastX = mouseX;
      _lastY = mouseY;
      win.onPan?.call(dx, dy);
    } else if (_resizeStartedOnBottomRight) {
      final dx = mouseX - _lastX;
      final dy = mouseY - _lastY;
      _lastX = mouseX;
      _lastY = mouseY;
      final newWidth = max(10, win.width + dx);
      final newHeight = max(5, win.height + dy);
      win.onResize?.call(newWidth, newHeight);
    } else if (_resizeStartedOnBottomLeft) {
      final dx = mouseX - _lastX;
      final dy = mouseY - _lastY;
      _lastX = mouseX;
      _lastY = mouseY;
      final newWidth = max(10, win.width - dx);
      final actualDx = win.width - newWidth;
      final newHeight = max(5, win.height + dy);
      win.onPan?.call(actualDx, 0);
      win.onResize?.call(newWidth, newHeight);
    }

    if (win.onMouseEvent != null) {
      win.onMouseEvent!(event, localX, localY);
    }
  } else {
    if (event.type == MouseEventType.release) {
      _dragStartedOnTitle = false;
      _resizeStartedOnBottomLeft = false;
      _resizeStartedOnBottomRight = false;
    }
    if (win.onMouseEvent != null) {
      win.onMouseEvent!(event, localX, localY);
    }
  }
}