Skip to content

Commit 6de6e28

Browse files
Keylon DuranKeylon Duran
authored andcommitted
140525_1040AM
-------------------- Solutions to issue codenameone#3692 reported in 2023
1 parent c235f40 commit 6de6e28

File tree

1 file changed

+96
-52
lines changed

1 file changed

+96
-52
lines changed

CodenameOne/src/com/codename1/ui/layouts/GridLayout.java

Lines changed: 96 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -67,10 +67,8 @@ public class GridLayout extends Layout{
6767
private boolean fillLastRow;
6868
private int portraitRows;
6969
private int portraitColumns;
70-
7170
private int landscapeRows = -1;
7271
private int landscapeColumns = -1;
73-
7472
/**
7573
* When set to true components that have 0 size will be hidden and won't occupy a cell within the grid. This
7674
* makes animating a grid layout component MUCH easier.
@@ -209,55 +207,68 @@ private int autoSizeCols(Container parent, int width, boolean landscapeMode) {
209207
/**
210208
* {@inheritDoc}
211209
*/
210+
@Override
212211
public void layoutContainer(Container parent) {
213-
Style s = parent.getStyle();
214-
int width = parent.getLayoutWidth() - parent.getSideGap() - s.getHorizontalPadding();
215-
int height = parent.getLayoutHeight() - parent.getBottomGap() - s.getVerticalPadding();
212+
Style s = parent.getStyle();
213+
int width = parent.getLayoutWidth() - parent.getSideGap() - s.getHorizontalPadding();
214+
int height = parent.getLayoutHeight() - parent.getBottomGap() - s.getVerticalPadding();
216215
int numOfcomponents = parent.getComponentCount();
217216

218217
boolean landscapeMode = isLandscapeMode();
219218
autoSizeCols(parent, width, landscapeMode);
220-
221-
int rows, columns;
222-
if(landscapeMode) {
223-
rows = landscapeRows;
224-
columns = landscapeColumns;
225-
} else {
226-
rows = portraitRows;
227-
columns = portraitColumns;
219+
//-----------------------------------------------------
220+
//Checking the hidden components to recalculate rows.
221+
//-----------------------------------------------------
222+
int totalComponentCountVisible = numOfcomponents;
223+
for (int i = 0; i < numOfcomponents; i++) {
224+
Component cmp = parent.getComponentAt(i);
225+
if (hideZeroSized && cmp.isHidden()) {
226+
totalComponentCountVisible--;
227+
}
228228
}
229-
229+
//--------------------------
230+
//Calculating dinamic rows
231+
// Solution to issue : https://github.com/codenameone/CodenameOne/issues/3692
232+
// Date: 12-04-23
233+
//--------------------------
234+
CalculateDinamicRowsLayout rc = new CalculateDinamicRowsLayout(totalComponentCountVisible, (landscapeMode ? landscapeColumns : portraitColumns));
235+
int rows = rc.getRows();
236+
int columns = rc.getColumns();
237+
//--------------------------
230238
int x = s.getPaddingLeft(parent.isRTL());
231239
int y = s.getPaddingTop();
232240

233241
boolean rtl = parent.isRTL();
234242
if (rtl) {
235-
x += parent.getSideGap();
243+
x += parent.getSideGap();
236244
}
237245
int localColumns = columns;
238246
int cmpWidth = width / columns;
239-
int cmpHeight;
240-
if (numOfcomponents > rows * columns) {
241-
// actual rows number
242-
cmpHeight = height / (numOfcomponents / columns + (numOfcomponents % columns == 0 ? 0 : 1));
243-
} else {
244-
cmpHeight = height / rows;
245-
}
246-
int row = 0;
247-
247+
int cmpHeight= cmpHeight = height / rows;
248+
249+
int row = 0;
248250
int offset = 0;
249251
for(int iter = 0 ; iter < numOfcomponents ; iter++){
250-
Component cmp = parent.getComponentAt(iter);
251-
Style cmpStyle = cmp.getStyle();
252-
int marginLeft = cmpStyle.getMarginLeft(parent.isRTL());
253-
int marginTop = cmpStyle.getMarginTop();
252+
Component cmp = parent.getComponentAt(iter);
253+
Style cmpStyle = cmp.getStyle();
254+
int marginLeft = cmpStyle.getMarginLeft(parent.isRTL());
255+
int marginTop = cmpStyle.getMarginTop();
256+
int marginRight = cmpStyle.getMarginRight(parent.isRTL());
257+
int marginBottom = cmpStyle.getMarginBottom();
258+
254259
if(hideZeroSized) {
255260
if(cmp.isHidden()) {
256261
continue;
257262
}
258263
}
259-
cmp.setWidth(cmpWidth - marginLeft - cmpStyle.getMarginRight(parent.isRTL()));
260-
cmp.setHeight(cmpHeight - marginTop - cmpStyle.getMarginBottom());
264+
//---------------------------------------------------
265+
//Setting component size
266+
//---------------------------------------------------
267+
cmp.setWidth(cmpWidth - marginLeft - marginRight);
268+
cmp.setHeight(cmpHeight - marginTop - marginBottom);
269+
//---------------------------------------------------
270+
//Setting component position
271+
//---------------------------------------------------
261272
if (rtl) {
262273
cmp.setX(x + (localColumns - 1 - (offset % localColumns)) * cmpWidth + marginLeft);
263274
} else {
@@ -266,7 +277,6 @@ public void layoutContainer(Container parent) {
266277
cmp.setY(y + row * cmpHeight + marginTop);
267278
if((offset + 1) % columns == 0){
268279
row++;
269-
270280
// check if we need to recalculate component widths
271281
if(fillLastRow && row == rows - 1) {
272282
localColumns = numOfcomponents % columns;
@@ -282,49 +292,51 @@ public void layoutContainer(Container parent) {
282292

283293
/**
284294
* {@inheritDoc}
285-
*/
286-
public Dimension getPreferredSize(Container parent) {
295+
*/
296+
@Override
297+
public Dimension getPreferredSize(Container parent) {
287298
int width = 0;
288299
int height = 0;
289-
290300
int numOfcomponents = parent.getComponentCount();
291-
int totalComponentCount = numOfcomponents;
301+
int totalComponentCountVisible = numOfcomponents;
302+
//-----------------------------------------------------
303+
//Checking the hidden components to recalculate rows.
304+
//-----------------------------------------------------
292305
for(int i=0; i< numOfcomponents; i++){
293306
Component cmp = parent.getComponentAt(i);
294307
if(hideZeroSized && cmp.isHidden()) {
295-
totalComponentCount--;
308+
totalComponentCountVisible--;
296309
} else {
297310
width = Math.max(width, cmp.getPreferredW() + cmp.getStyle().getMarginLeftNoRTL() + cmp.getStyle().getMarginRightNoRTL());
298311
height = Math.max(height, cmp.getPreferredH() + cmp.getStyle().getMarginTop() + cmp.getStyle().getMarginBottom());
299312
}
300313
}
314+
//-----------------------------------------------------
301315

302-
boolean landscapeMode = isLandscapeMode();
316+
boolean landscapeMode = isLandscapeMode();
303317
autoSizeCols(parent, parent.getWidth(), landscapeMode);
304-
int rows, columns;
305-
if(landscapeMode) {
306-
rows = landscapeRows;
307-
columns = landscapeColumns;
308-
} else {
309-
rows = portraitRows;
310-
columns = portraitColumns;
311-
}
312-
318+
//--------------------------
319+
//Calculating dinamic rows
320+
// Solution to issue : https://github.com/codenameone/CodenameOne/issues/3692
321+
// Date: 12-04-23
322+
//--------------------------
323+
CalculateDinamicRowsLayout rc = new CalculateDinamicRowsLayout(totalComponentCountVisible, (landscapeMode ? landscapeColumns : portraitColumns));
324+
int rows = rc.getRows();
325+
int columns = rc.getColumns();
326+
//--------------------------
313327
if(columns > 1){
314328
width = width*columns;
315329
}
316-
317330
if(rows > 1){
318-
if(totalComponentCount>rows*columns){ //if there are more components than planned
319-
height = height * (totalComponentCount/columns + (totalComponentCount%columns == 0 ? 0 : 1));
331+
if(totalComponentCountVisible>rows*columns){ //if there are more components than planned
332+
height = height * (totalComponentCountVisible/columns + (totalComponentCountVisible%columns == 0 ? 0 : 1));
320333
}else{
321334
height = height*rows;
322335
}
323336
}
324-
337+
325338
Style s = parent.getStyle();
326-
return new Dimension(width + s.getHorizontalPadding(),
327-
height + s.getVerticalPadding());
339+
return new Dimension(width + s.getHorizontalPadding(),height + s.getVerticalPadding());
328340
}
329341

330342
/**
@@ -416,4 +428,36 @@ public boolean isHideZeroSized() {
416428
public void setHideZeroSized(boolean hideZeroSized) {
417429
this.hideZeroSized = hideZeroSized;
418430
}
431+
432+
/**
433+
* used to calculate the dinamic rows
434+
*/
435+
private class CalculateDinamicRowsLayout {
436+
int rows = 1;
437+
int columns = 1;
438+
/**
439+
*
440+
* @param qtyComponents quantity of components in layout
441+
* @param maxColums max columns
442+
*/
443+
//-------------------------------
444+
public CalculateDinamicRowsLayout(int qtyComponents, int maxColums) {
445+
if (qtyComponents > maxColums) {
446+
rows = qtyComponents / maxColums;
447+
if (!(qtyComponents % maxColums == 0)) {
448+
rows++;
449+
}
450+
}
451+
columns = maxColums;
452+
}
453+
//-------------------------------
454+
public int getRows() {
455+
return rows;
456+
}
457+
//-------------------------------
458+
public int getColumns() {
459+
return columns;
460+
}
461+
//-------------------------------
462+
}//endClass
419463
}

0 commit comments

Comments
 (0)