/**
   * 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);
  }
Esempio n. 2
0
  @Override
  public void doLayout(TerminalSize area, List<Component> components) {
    // Sanity check, if the area is way too small, just return
    Component[][] table = buildTable(components);
    table = eliminateUnusedRowsAndColumns(table);

    if (area.equals(TerminalSize.ZERO)
        || table.length == 0
        || area.getColumns()
            <= leftMarginSize + rightMarginSize + ((table[0].length - 1) * horizontalSpacing)
        || area.getRows()
            <= bottomMarginSize + topMarginSize + ((table.length - 1) * verticalSpacing)) {
      return;
    }

    // Adjust area to the margins
    area = area.withRelative(-leftMarginSize - rightMarginSize, -topMarginSize - bottomMarginSize);

    Map<Component, TerminalSize> sizeMap = new IdentityHashMap<Component, TerminalSize>();
    Map<Component, TerminalPosition> positionMap =
        new IdentityHashMap<Component, TerminalPosition>();

    // Figure out each column first, this can be done independently of the row heights
    int[] columnWidths = getPreferredColumnWidths(table);

    // Take notes of which columns we can expand if the usable area is larger than what the
    // components want
    Set<Integer> expandableColumns = getExpandableColumns(table);

    // Next, start shrinking to make sure it fits the size of the area we are trying to lay out on.
    // Notice we subtract the horizontalSpacing to take the space between components into account
    TerminalSize areaWithoutHorizontalSpacing =
        area.withRelativeColumns(-horizontalSpacing * (table[0].length - 1));
    int totalWidth = shrinkWidthToFitArea(areaWithoutHorizontalSpacing, columnWidths);

    // Finally, if there is extra space, make the expandable columns larger
    while (areaWithoutHorizontalSpacing.getColumns() > totalWidth && !expandableColumns.isEmpty()) {
      totalWidth =
          grabExtraHorizontalSpace(
              areaWithoutHorizontalSpacing, columnWidths, expandableColumns, totalWidth);
    }

    // Now repeat for rows
    int[] rowHeights = getPreferredRowHeights(table);
    Set<Integer> expandableRows = getExpandableRows(table);
    TerminalSize areaWithoutVerticalSpacing =
        area.withRelativeRows(-verticalSpacing * (table.length - 1));
    int totalHeight = shrinkHeightToFitArea(areaWithoutVerticalSpacing, rowHeights);
    while (areaWithoutVerticalSpacing.getRows() > totalHeight && !expandableRows.isEmpty()) {
      totalHeight =
          grabExtraVerticalSpace(
              areaWithoutVerticalSpacing, rowHeights, expandableRows, totalHeight);
    }

    // Ok, all constraints are in place, we can start placing out components. To simplify, do it
    // horizontally first
    // and vertically after
    TerminalPosition tableCellTopLeft = TerminalPosition.TOP_LEFT_CORNER;
    for (int y = 0; y < table.length; y++) {
      tableCellTopLeft = tableCellTopLeft.withColumn(0);
      for (int x = 0; x < table[y].length; x++) {
        Component component = table[y][x];
        if (component != null && !positionMap.containsKey(component)) {
          GridLayoutData layoutData = getLayoutData(component);
          TerminalSize size = component.getPreferredSize();
          TerminalPosition position = tableCellTopLeft;

          int availableHorizontalSpace = 0;
          int availableVerticalSpace = 0;
          for (int i = 0; i < layoutData.horizontalSpan; i++) {
            availableHorizontalSpace += columnWidths[x + i] + (i > 0 ? horizontalSpacing : 0);
          }
          for (int i = 0; i < layoutData.verticalSpan; i++) {
            availableVerticalSpace += rowHeights[y + i] + (i > 0 ? verticalSpacing : 0);
          }

          // Make sure to obey the size restrictions
          size = size.withColumns(Math.min(size.getColumns(), availableHorizontalSpace));
          size = size.withRows(Math.min(size.getRows(), availableVerticalSpace));

          switch (layoutData.horizontalAlignment) {
            case CENTER:
              position =
                  position.withRelativeColumn((availableHorizontalSpace - size.getColumns()) / 2);
              break;
            case END:
              position = position.withRelativeColumn(availableHorizontalSpace - size.getColumns());
              break;
            case FILL:
              size = size.withColumns(availableHorizontalSpace);
              break;
            default:
              break;
          }
          switch (layoutData.verticalAlignment) {
            case CENTER:
              position = position.withRelativeRow((availableVerticalSpace - size.getRows()) / 2);
              break;
            case END:
              position = position.withRelativeRow(availableVerticalSpace - size.getRows());
              break;
            case FILL:
              size = size.withRows(availableVerticalSpace);
              break;
            default:
              break;
          }

          sizeMap.put(component, size);
          positionMap.put(component, position);
        }
        tableCellTopLeft = tableCellTopLeft.withRelativeColumn(columnWidths[x] + horizontalSpacing);
      }
      tableCellTopLeft = tableCellTopLeft.withRelativeRow(rowHeights[y] + verticalSpacing);
    }

    // Apply the margins here
    for (Component component : components) {
      component.setPosition(positionMap.get(component).withRelative(leftMarginSize, topMarginSize));
      component.setSize(sizeMap.get(component));
    }
    this.changed = false;
  }