Skip to content

Commit

Permalink
Fix: container_base with explicit minimum size can't shrink below that
Browse files Browse the repository at this point in the history
Composite widgets, i.e. widgets which inherit container_base, such as the
unit preview pane, can have an explicit minimum size set in their
definition. The widget will request either that size or however much space
the child widgets want, whichever is higher.

If
* the minimum size was higher,
* there isn't enough space for that size, and
* there would be enough space for the underlying grid not to shrink,

then the attempt to reduce the size of the composite widget doesn't do
anything. The container_base tells the grid to shrink, but the grid is
already small enough to fit inside the given space. Thus, the grid doesn't
do anything. In addition, the container_base doesn't change its own size
afterwards.

This commit fixes the issue and allows composite widgets to shrink.
  • Loading branch information
jyrkive committed Dec 17, 2016
1 parent e0ca557 commit 78f0262
Showing 1 changed file with 48 additions and 4 deletions.
52 changes: 48 additions & 4 deletions src/gui/widgets/container_base.cpp
Expand Up @@ -18,6 +18,8 @@

#include "gui/core/log.hpp"

#include <algorithm>

#define LOG_SCOPE_HEADER \
"tcontainer(" + get_control_type() + ") [" + id() + "] " + __func__
#define LOG_HEADER LOG_SCOPE_HEADER + ':'
Expand All @@ -40,12 +42,34 @@ void container_base::layout_initialise(const bool full_initialisation)

void container_base::reduce_width(const unsigned maximum_width)
{
grid_.reduce_width(maximum_width - border_space().x);
point size = get_best_size();
point grid_size = grid_.get_best_size();
if(static_cast<int>(maximum_width) - border_space().x < grid_size.x) {
grid_.reduce_width(maximum_width - border_space().x);
grid_size = grid_.get_best_size();
size.x = grid_size.x + border_space().x;
size.y = std::max(size.y, grid_size.y + border_space().y);
} else {
size.x = maximum_width;
}

set_layout_size(size);
}

void container_base::request_reduce_width(const unsigned maximum_width)
{
grid_.request_reduce_width(maximum_width - border_space().x);
point size = get_best_size();
point grid_size = grid_.get_best_size();
if(static_cast<int>(maximum_width)-border_space().x < grid_size.x) {
grid_.request_reduce_width(maximum_width - border_space().x);
grid_size = grid_.get_best_size();
size.x = grid_size.x + border_space().x;
size.y = std::max(size.y, grid_size.y + border_space().y);
} else {
size.x = maximum_width;
}

set_layout_size(size);
}

void container_base::demand_reduce_width(const unsigned maximum_width)
Expand All @@ -55,12 +79,32 @@ void container_base::demand_reduce_width(const unsigned maximum_width)

void container_base::reduce_height(const unsigned maximum_height)
{
grid_.reduce_height(maximum_height - border_space().y);
point size = get_best_size();
point grid_size = grid_.get_best_size();
if(static_cast<int>(maximum_height)-border_space().y < grid_size.y) {
grid_.reduce_height(maximum_height - border_space().y);
grid_size = grid_.get_best_size();
size.y = grid_size.y + border_space().y;
} else {
size.y = maximum_height;
}

set_layout_size(size);
}

void container_base::request_reduce_height(const unsigned maximum_height)
{
grid_.request_reduce_height(maximum_height - border_space().y);
point size = get_best_size();
point grid_size = grid_.get_best_size();
if(static_cast<int>(maximum_height)-border_space().y < grid_size.y) {
grid_.request_reduce_height(maximum_height - border_space().y);
grid_size = grid_.get_best_size();
size.y = grid_size.y + border_space().y;
} else {
size.y = maximum_height;
}

set_layout_size(size);
}

void container_base::demand_reduce_height(const unsigned maximum_height)
Expand Down

0 comments on commit 78f0262

Please sign in to comment.