Polish GridView

- Add some new tests
- Add javadocs
- Relates #805
This commit is contained in:
Janne Valkealahti
2023-07-21 18:06:57 +01:00
parent c074af314b
commit a19a50b3f3
2 changed files with 98 additions and 0 deletions

View File

@@ -123,6 +123,48 @@ public class GridView extends BoxView {
return this;
}
/**
* Adds a {@link View} and its position to the grid. The top-left corner of the
* {@link View} will be located in the top-left corner of the grid cell at
* the given row and column and will span "rowSpan" rows and "colSpan" columns.
* For example, for a primitive to occupy rows 2, 3, and 4 and columns 5 and 6:
*
* <p>
* grid.addItem(view, 2, 5, 3, 2, 0, 0)
*
* If {@code rowSpan} or colSpan is 0, the {@link View} will not be drawn.
*
* <p>
* You can add the same {@link View} multiple times with different grid
* positions. The {@code minGridWidth} and {@code minGridHeight} values will
* then determine which of those positions will be used. These minimum values
* refer to the overall size of the grid. If multiple items for the same
* primitive apply, the one that has at least one highest minimum value will
* be used, or the {@link View} added last if those values are the same. Example:
*
* <p>
* AddItem(view, 0, 0, 0, 0, 0, 0). // Hide in small grids.
* AddItem(view, 0, 0, 1, 2, 100, 0). // One-column layout for medium grids.
* AddItem(view, 1, 1, 3, 2, 300, 0) // Multi-column layout for large grids.
*
* <p>
* To use the same grid layout for all sizes, simply set {@code minGridWidth}
* and {@code minGridHeight} to 0.
*
* <p>
* If the item's focus is set to true, it will receive focus when the grid
* receives focus. If there are multiple items with a true focus flag, the
* last visible one that was added will receive focus.
*
* @param view the view to add
* @param row the row
* @param column the column
* @param rowSpan the row span
* @param colSpan the column span
* @param minGridHeight the mininum height
* @param minGridWidth the minimun width
* @return a grid view for chaining
*/
public GridView addItem(View view, int row, int column, int rowSpan, int colSpan, int minGridHeight,
int minGridWidth) {
GridItem gridItem = new GridItem(view, row, column, colSpan, rowSpan, minGridHeight,

View File

@@ -21,6 +21,7 @@ import org.junit.jupiter.api.Test;
import org.springframework.shell.component.view.screen.DefaultScreen;
import static org.assertj.core.api.Assertions.assertThat;
import static org.mockito.ArgumentMatchers.anyInt;
import static org.mockito.Mockito.never;
import static org.mockito.Mockito.spy;
import static org.mockito.Mockito.verify;
@@ -267,6 +268,61 @@ class GridViewTests extends AbstractViewTests {
}
@Nested
class DynamicLayoutMinWidthHeight {
@Test
void widthHides_1_base() {
BoxView sbox1 = spy(new BoxView());
BoxView sbox2 = spy(new BoxView());
BoxView sbox3 = spy(new BoxView());
GridView grid = new GridView();
grid.setRowSize(0);
grid.setColumnSize(0, 0, 0);
grid.addItem(sbox1, 0, 0, 1, 1, 0, 0);
grid.addItem(sbox2, 0, 1, 1, 1, 0, 0);
grid.addItem(sbox3, 0, 2, 1, 1, 0, 0);
grid.setRect(0, 0, 80, 24);
grid.draw(screen24x80);
verify(sbox1).setRect(0, 0, 26, 24);
verify(sbox2).setRect(26, 0, 27, 24);
verify(sbox3).setRect(53, 0, 27, 24);
}
@Test
void widthHides_1_shouldHide() {
BoxView sbox1 = spy(new BoxView());
BoxView sbox2 = spy(new BoxView());
BoxView sbox3 = spy(new BoxView());
GridView grid = new GridView();
grid.setRowSize(0);
grid.setColumnSize(0, 0, 0);
// this should get used
grid.addItem(sbox1, 0, 0, 0, 1, 0, 0);
grid.addItem(sbox2, 0, 0, 1, 3, 0, 0);
grid.addItem(sbox3, 0, 2, 0, 1, 0, 0);
// this should not get used
grid.addItem(sbox1, 0, 0, 1, 1, 0, 100);
grid.addItem(sbox2, 0, 1, 1, 1, 0, 100);
grid.addItem(sbox3, 0, 2, 1, 1, 0, 100);
grid.setRect(0, 0, 80, 24);
grid.draw(screen24x80);
verify(sbox1, never()).setRect(anyInt(), anyInt(), anyInt(), anyInt());
verify(sbox2).setRect(0, 0, 80, 24);
verify(sbox3, never()).setRect(anyInt(), anyInt(), anyInt(), anyInt());
}
}
@Nested
class Borders {