subScreensInBounds static method

Iterable<Rect> subScreensInBounds(
  1. Rect wantedBounds,
  2. Iterable<Rect> avoidBounds
)

Returns sub-screens resulted by dividing wantedBounds along items of avoidBounds that are at least as tall or as wide.

Implementation

static Iterable<Rect> subScreensInBounds(Rect wantedBounds, Iterable<Rect> avoidBounds) {
  Iterable<Rect> subScreens = <Rect>[wantedBounds];
  for (final Rect bounds in avoidBounds) {
    final List<Rect> newSubScreens = <Rect>[];
    for (final Rect screen in subScreens) {
      if (screen.top >= bounds.top && screen.bottom <= bounds.bottom) {
        // Display feature splits the screen vertically
        if (screen.left < bounds.left) {
          // There is a smaller sub-screen, left of the display feature
          newSubScreens.add(Rect.fromLTWH(
            screen.left,
            screen.top,
            bounds.left - screen.left,
            screen.height,
          ));
        }
        if (screen.right > bounds.right) {
          // There is a smaller sub-screen, right of the display feature
          newSubScreens.add(Rect.fromLTWH(
            bounds.right,
            screen.top,
            screen.right - bounds.right,
            screen.height,
          ));
        }
      } else if (screen.left >= bounds.left && screen.right <= bounds.right) {
        // Display feature splits the sub-screen horizontally
        if (screen.top < bounds.top) {
          // There is a smaller sub-screen, above the display feature
          newSubScreens.add(Rect.fromLTWH(
            screen.left,
            screen.top,
            screen.width,
            bounds.top - screen.top,
          ));
        }
        if (screen.bottom > bounds.bottom) {
          // There is a smaller sub-screen, below the display feature
          newSubScreens.add(Rect.fromLTWH(
            screen.left,
            bounds.bottom,
            screen.width,
            screen.bottom - bounds.bottom,
          ));
        }
      } else {
        newSubScreens.add(screen);
      }
    }
    subScreens = newSubScreens;
  }
  return subScreens;
}