@Override
  public void onAdded(WindowBasedTextGUI textGUI, Window window, List<Window> allWindows) {
    WindowDecorationRenderer decorationRenderer = getWindowDecorationRenderer(window);
    TerminalSize expectedDecoratedSize =
        decorationRenderer.getDecoratedSize(window, window.getPreferredSize());
    window.setDecoratedSize(expectedDecoratedSize);

    if (window.getHints().contains(Window.Hint.FIXED_POSITION)) {
      // Don't place the window, assume the position is already set
    } else if (allWindows.isEmpty()) {
      window.setPosition(TerminalPosition.OFFSET_1x1);
    } else if (window.getHints().contains(Window.Hint.CENTERED)) {
      int left = (lastKnownScreenSize.getColumns() - expectedDecoratedSize.getColumns()) / 2;
      int top = (lastKnownScreenSize.getRows() - expectedDecoratedSize.getRows()) / 2;
      window.setPosition(new TerminalPosition(left, top));
    } else {
      TerminalPosition nextPosition =
          allWindows.get(allWindows.size() - 1).getPosition().withRelative(2, 1);
      if (nextPosition.getColumn() + expectedDecoratedSize.getColumns()
              > lastKnownScreenSize.getColumns()
          || nextPosition.getRow() + expectedDecoratedSize.getRows()
              > lastKnownScreenSize.getRows()) {
        nextPosition = TerminalPosition.OFFSET_1x1;
      }
      window.setPosition(nextPosition);
    }

    // Finally, run through the usual calculations so the window manager's usual prepare method can
    // have it's say
    prepareWindow(lastKnownScreenSize, window);
  }
  static Window createWindow(
      final Capabilities caps,
      final int x,
      final int y,
      final int width,
      final int height,
      final boolean onscreen,
      final boolean undecorated)
      throws InterruptedException {
    final boolean userPos = x >= 0 && y >= 0; // user has specified a position

    Assert.assertNotNull(caps);
    caps.setOnscreen(onscreen);
    // System.out.println("Requested: "+caps);

    //
    // Create native windowing resources .. X11/Win/OSX
    //
    final Window window = NewtFactory.createWindow(caps);
    Assert.assertNotNull(window);
    final Screen screen = window.getScreen();
    final Display display = screen.getDisplay();
    window.setUndecorated(onscreen && undecorated);
    if (userPos) {
      window.setPosition(x, y);
    }
    window.setSize(width, height);
    Assert.assertEquals(false, window.isNativeValid());
    Assert.assertEquals(false, window.isVisible());
    window.setVisible(true);
    // System.err.println("************* Created: "+window);

    Assert.assertEquals(true, display.isNativeValid());
    Assert.assertEquals(true, screen.isNativeValid());
    Assert.assertEquals(true, window.isVisible());
    Assert.assertEquals(true, window.isNativeValid());
    Assert.assertEquals(width, window.getWidth());
    Assert.assertEquals(height, window.getHeight());

    /**
     * we don't sync on position - unreliable test Point p0 = window.getLocationOnScreen(null);
     * Assert.assertEquals(p0.getX(), window.getX()); Assert.assertEquals(p0.getY(), window.getY());
     * if(userPos) { Assert.assertEquals(x, window.getX()); Assert.assertEquals(y, window.getY()); }
     */
    final CapabilitiesImmutable chosenCapabilities =
        window.getGraphicsConfiguration().getChosenCapabilities();
    Assert.assertNotNull(chosenCapabilities);
    Assert.assertTrue(chosenCapabilities.getGreenBits() >= 5);
    Assert.assertTrue(chosenCapabilities.getBlueBits() >= 5);
    Assert.assertTrue(chosenCapabilities.getRedBits() >= 5);
    Assert.assertEquals(chosenCapabilities.isOnscreen(), onscreen);

    return window;
  }
  /**
   * Called by {@link DefaultWindowManager} when iterating through all windows to decide their size
   * and position. If you override {@link DefaultWindowManager} to add your own logic to how windows
   * are placed on the screen, you can override this method and selectively choose which window to
   * interfere with. Note that the two key properties that are read by the GUI system after
   * preparing all windows are the position and decorated size. Your custom implementation should
   * set these two fields directly on the window. You can infer the decorated size from the content
   * size by using the window decoration renderer that is attached to the window manager.
   *
   * @param screenSize Size of the terminal that is available to draw on
   * @param window Window to prepare decorated size and position for
   */
  protected void prepareWindow(TerminalSize screenSize, Window window) {
    WindowDecorationRenderer decorationRenderer = getWindowDecorationRenderer(window);
    TerminalSize contentAreaSize;
    if (window.getHints().contains(Window.Hint.FIXED_SIZE)) {
      contentAreaSize = window.getSize();
    } else {
      contentAreaSize = window.getPreferredSize();
    }
    TerminalSize size = decorationRenderer.getDecoratedSize(window, contentAreaSize);
    TerminalPosition position = window.getPosition();

    if (window.getHints().contains(Window.Hint.FULL_SCREEN)) {
      position = TerminalPosition.TOP_LEFT_CORNER;
      size = screenSize;
    } else if (window.getHints().contains(Window.Hint.EXPANDED)) {
      position = TerminalPosition.OFFSET_1x1;
      size =
          screenSize.withRelative(
              -Math.min(4, screenSize.getColumns()), -Math.min(3, screenSize.getRows()));
      if (!size.equals(window.getDecoratedSize())) {
        window.invalidate();
      }
    } else if (window.getHints().contains(Window.Hint.FIT_TERMINAL_WINDOW)
        || window.getHints().contains(Window.Hint.CENTERED)) {
      // If the window is too big for the terminal, move it up towards 0x0 and if that's not enough
      // then shrink
      // it instead
      while (position.getRow() > 0 && position.getRow() + size.getRows() > screenSize.getRows()) {
        position = position.withRelativeRow(-1);
      }
      while (position.getColumn() > 0
          && position.getColumn() + size.getColumns() > screenSize.getColumns()) {
        position = position.withRelativeColumn(-1);
      }
      if (position.getRow() + size.getRows() > screenSize.getRows()) {
        size = size.withRows(screenSize.getRows() - position.getRow());
      }
      if (position.getColumn() + size.getColumns() > screenSize.getColumns()) {
        size = size.withColumns(screenSize.getColumns() - position.getColumn());
      }
      if (window.getHints().contains(Window.Hint.CENTERED)) {
        int left = (lastKnownScreenSize.getColumns() - size.getColumns()) / 2;
        int top = (lastKnownScreenSize.getRows() - size.getRows()) / 2;
        position = new TerminalPosition(left, top);
      }
    }

    window.setPosition(position);
    window.setDecoratedSize(size);
  }