handleMouseEvent method

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

Intercepts mouse drag/press events over the divider.

Implementation

void handleMouseEvent(MouseEvent event, int localX, int localY) {
  if (_lastArea == null) return;
  _initLimits();
  final totalSize = direction == LayoutDirection.horizontal
      ? _lastArea!.width
      : _lastArea!.height;
  if (totalSize <= 0) return;

  final mousePos = direction == LayoutDirection.horizontal ? localX : localY;

  if (event.type == MouseEventType.press) {
    if (mousePos == _dividerX) {
      _isDragging = true;
    }
  } else if (event.type == MouseEventType.release) {
    _isDragging = false;
  } else if (event.type == MouseEventType.drag && _isDragging) {
    int newDividerPos = mousePos;

    // Solve bounds/limits
    int minW1 = _origMin1 ?? 0;
    int maxW1 = _origMax1 ?? (totalSize - 1);
    if (_origMin2 != null) {
      maxW1 = min(maxW1, totalSize - _origMin2! - 1);
    }
    if (_origMax2 != null) {
      minW1 = max(minW1, totalSize - _origMax2! - 1);
    }
    minW1 = minW1.clamp(0, totalSize - 1);
    maxW1 = maxW1.clamp(0, totalSize - 1);
    if (minW1 > maxW1) minW1 = maxW1;

    newDividerPos = newDividerPos.clamp(minW1, maxW1);

    // Mutate constraints in place
    final c1 = constraint1;
    if (c1 is LengthConstraint) {
      constraint1 = LengthConstraint(newDividerPos);
      constraint2 = LengthConstraint(totalSize - newDividerPos - 1);
    } else if (c1 is PercentageConstraint) {
      final p1 = (newDividerPos * 100 / totalSize).round().clamp(0, 100);
      constraint1 = PercentageConstraint(p1);
      constraint2 = PercentageConstraint(100 - p1);
    } else if (c1 is FlexConstraint) {
      constraint1 = FlexConstraint(newDividerPos);
      constraint2 = FlexConstraint(totalSize - newDividerPos - 1);
    } else if (c1 is MinMaxConstraint) {
      // Here we keep the min limits but update the current value via a new MinMaxConstraint.
      // Wait, since MinMaxConstraint uses min to represent the preferred size, we update the min.
      // But the original min limit is stored in _origMin1, so we still clamp correctly.
      constraint1 = MinMaxConstraint(
        min: newDividerPos,
        max: _origMax1 ?? 99999,
      );
      if (constraint2 is MinMaxConstraint) {
        constraint2 = MinMaxConstraint(
          min: totalSize - newDividerPos - 1,
          max: _origMax2 ?? 99999,
        );
      } else {
        constraint2 = MinMaxConstraint(min: totalSize - newDividerPos - 1);
      }
    }
    _dividerX = newDividerPos;
  }
}