Add support to reduce all children when a ui::Grid is too small

This commit is contained in:
David Capello 2016-09-09 20:20:44 -03:00
parent 52260c466d
commit 4a6d6951e3
2 changed files with 124 additions and 46 deletions

View File

@ -1,5 +1,5 @@
// Aseprite UI Library
// Copyright (C) 2001-2013, 2015 David Capello
// Copyright (C) 2001-2016 David Capello
//
// This file is released under the terms of the MIT license.
// Read LICENSE.txt for more information.
@ -191,20 +191,17 @@ void Grid::onResize(ResizeEvent& ev)
void Grid::onSizeHint(SizeHintEvent& ev)
{
int w, h;
w = h = 0;
calculateSize();
// Calculate the total
sumStripSize(m_colstrip, w);
sumStripSize(m_rowstrip, h);
gfx::Size sz(0, 0);
sumStripSize(m_colstrip, sz.w);
sumStripSize(m_rowstrip, sz.h);
w += border().width();
h += border().height();
sz.w += border().width();
sz.h += border().height();
ev.setSizeHint(Size(w, h));
ev.setSizeHint(sz);
}
void Grid::onPaint(PaintEvent& ev)
@ -374,7 +371,6 @@ void Grid::expandStrip(std::vector<Strip>& colstrip,
else
size = cell->h; // Transposed
}
(this->*incCol)(i, size);
}
}
@ -423,38 +419,40 @@ void Grid::distributeStripSize(std::vector<Strip>& colstrip,
}
total_req += border_size;
if (wantmore_count > 0) {
int extra_total = rect_size - total_req;
if (extra_total > 0) {
// If a expandable column-strip was empty (size=0) then we have
// to reduce the extra_total size because a new child-spacing is
// added by this column
for (i=0; i<(int)colstrip.size(); ++i) {
if ((colstrip[i].size == 0) &&
(colstrip[i].expand_count == max_expand_count || same_width)) {
extra_total -= this->childSpacing();
}
int extra_total = (rect_size - total_req);
// Expand or reduce "expandable" strip
if ((wantmore_count > 0) &&
((extra_total > 0 && (max_expand_count > 0 || same_width)) ||
(extra_total < 0))) {
// If a expandable column-strip was empty (size=0) then we have
// to reduce the extra_total size because a new child-spacing is
// added by this column
for (i=0; i<(int)colstrip.size(); ++i) {
if ((colstrip[i].size == 0) &&
(colstrip[i].expand_count == max_expand_count || same_width)) {
extra_total -= SGN(extra_total)*this->childSpacing();
}
int extra_foreach = extra_total / wantmore_count;
for (i=0; i<(int)colstrip.size(); ++i) {
if (colstrip[i].expand_count == max_expand_count || same_width) {
ASSERT(wantmore_count > 0);
colstrip[i].size += extra_foreach;
extra_total -= extra_foreach;
if (--wantmore_count == 0) {
colstrip[i].size += extra_total;
extra_total = 0;
}
}
}
ASSERT(wantmore_count == 0);
ASSERT(extra_total == 0);
}
int extra_foreach = extra_total / wantmore_count;
for (i=0; i<(int)colstrip.size(); ++i) {
if (colstrip[i].expand_count == max_expand_count || same_width) {
ASSERT(wantmore_count > 0);
colstrip[i].size += extra_foreach;
extra_total -= extra_foreach;
if (--wantmore_count == 0) {
colstrip[i].size += extra_total;
extra_total = 0;
}
}
}
ASSERT(wantmore_count == 0);
ASSERT(extra_total == 0);
}
}

View File

@ -1,11 +1,12 @@
// Aseprite
// Copyright (C) 2001-2015 David Capello
// Aseprite UI Library
// Copyright (C) 2001-2016 David Capello
//
// This program is distributed under the terms of
// the End-User License Agreement for Aseprite.
// This file is released under the terms of the MIT license.
// Read LICENSE.txt for more information.
#define TEST_GUI
#include "tests/test.h"
#include "gfx/rect_io.h"
#include "gfx/size.h"
using namespace gfx;
@ -144,7 +145,7 @@ TEST(Grid, SameWidth2x1Grid)
// | | |
// +----------------------------+---+
//
TEST(Grid, Intrincate3x3Grid)
TEST(Grid, Intrincate2x2Grid)
{
Grid* grid = new Grid(3, false);
Widget* w1 = new Widget;
@ -226,3 +227,82 @@ TEST(Grid, FourColumns)
EXPECT_EQ(gfx::Rect(30, 0, 10, 10), d.bounds());
EXPECT_EQ(gfx::Rect(0, 10, 40, 10), e.bounds());
}
TEST(Grid, OneFixedReduceThree)
{
Grid grid(1, false);
grid.noBorderNoChildSpacing();
Widget a;
Widget b;
Widget c;
Widget d;
a.setMinSize(gfx::Size(10, 10));
b.setMinSize(gfx::Size(10, 10));
c.setMinSize(gfx::Size(10, 10));
d.setMinSize(gfx::Size(10, 10));
grid.addChildInCell(&a, 1, 1, HORIZONTAL);
grid.addChildInCell(&b, 1, 1, HORIZONTAL | VERTICAL);
grid.addChildInCell(&c, 1, 1, HORIZONTAL | VERTICAL);
grid.addChildInCell(&d, 1, 1, HORIZONTAL | VERTICAL);
// Test request size
grid.setChildSpacing(0);
EXPECT_EQ(gfx::Size(10, 40), grid.sizeHint());
// Test bigger layout
grid.setBounds(gfx::Rect(0, 0, 10, 100));
EXPECT_EQ(gfx::Rect(0, 0, 10, 10), a.bounds());
EXPECT_EQ(gfx::Rect(0, 10, 10, 30), b.bounds());
EXPECT_EQ(gfx::Rect(0, 40, 10, 30), c.bounds());
EXPECT_EQ(gfx::Rect(0, 70, 10, 30), d.bounds());
// Test perfect layout
grid.setBounds(gfx::Rect(0, 0, 10, 40));
EXPECT_EQ(gfx::Rect(0, 0, 10, 10), a.bounds());
EXPECT_EQ(gfx::Rect(0, 10, 10, 10), b.bounds());
EXPECT_EQ(gfx::Rect(0, 20, 10, 10), c.bounds());
EXPECT_EQ(gfx::Rect(0, 30, 10, 10), d.bounds());
// Test reduced layout
grid.setBounds(gfx::Rect(0, 0, 10, 16));
EXPECT_EQ(gfx::Rect(0, 0, 10, 10), a.bounds());
EXPECT_EQ(gfx::Rect(0, 10, 10, 2), b.bounds());
EXPECT_EQ(gfx::Rect(0, 12, 10, 2), c.bounds());
EXPECT_EQ(gfx::Rect(0, 14, 10, 2), d.bounds());
}
TEST(Grid, ReduceThree)
{
Grid grid(1, false);
grid.noBorderNoChildSpacing();
Widget a;
Widget b;
Widget c;
a.setMinSize(gfx::Size(10, 10));
b.setMinSize(gfx::Size(10, 10));
c.setMinSize(gfx::Size(10, 10));
grid.addChildInCell(&a, 1, 1, HORIZONTAL);
grid.addChildInCell(&b, 1, 1, HORIZONTAL);
grid.addChildInCell(&c, 1, 1, HORIZONTAL);
// Test request size
grid.setChildSpacing(0);
EXPECT_EQ(gfx::Size(10, 30), grid.sizeHint());
// Test bigger layout (as these widgets aren't expandible, they
// should have height=10)
grid.setBounds(gfx::Rect(0, 0, 10, 90));
EXPECT_EQ(gfx::Rect(0, 0, 10, 10), a.bounds());
EXPECT_EQ(gfx::Rect(0, 10, 10, 10), b.bounds());
EXPECT_EQ(gfx::Rect(0, 20, 10, 10), c.bounds());
// Test reduced layout
grid.setBounds(gfx::Rect(0, 0, 10, 12));
EXPECT_EQ(gfx::Rect(0, 0, 10, 4), a.bounds());
EXPECT_EQ(gfx::Rect(0, 4, 10, 4), b.bounds());
EXPECT_EQ(gfx::Rect(0, 8, 10, 4), c.bounds());
}