Skip to content

Commit

Permalink
GUI2/Size Lock: ensure formula sizes are recalculated as necessary.
Browse files Browse the repository at this point in the history
This is similar to the change made for spacers in da5f00c.
It also fixes an issue with the MP Lobby chat box (and other such widgets that use formulas
for their fixed dimensions) where it would stay too small after a window resize (the chat
box formulas in all three dialogs it appears in are a percentage of window height).
  • Loading branch information
Vultraz committed May 5, 2018
1 parent ac44c91 commit 293add8
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 48 deletions.
1 change: 1 addition & 0 deletions changelog.md
Expand Up @@ -15,6 +15,7 @@
* Highlight the titles of MP games with vacant slots.
* Improved MP Lobby layout on low resolutions.
* Improved reporting of network errors in the MP lobby (issue #3005).
* Ensure the chat widget remains the correct size even after a window resize.
### Miscellaneous and bug fixes
* Removed misleading tooltip text stating registered nicknames are optional for
the official MP server.
Expand Down
67 changes: 31 additions & 36 deletions src/gui/widgets/size_lock.cpp
Expand Up @@ -25,40 +25,35 @@

namespace gui2
{

REGISTER_WIDGET(size_lock)

size_lock::size_lock(const implementation::builder_size_lock& builder)
: container_base(builder, get_control_type())
, widget_(nullptr)
, width_(builder.width_)
, height_(builder.height_)
{
}

void size_lock::place(const point& origin, const point& size)
{
point content_size = widget_->get_best_size();

if (content_size.x > size.x)
{
if(content_size.x > size.x) {
reduce_width(size.x);
content_size = widget_->get_best_size();
}

if (content_size.y > size.y)
{
try
{
if(content_size.y > size.y) {
try {
reduce_height(size.y);
}
catch(layout_exception_width_modified&)
{
} catch(layout_exception_width_modified&) {
}

content_size = widget_->get_best_size();
}

if (content_size.x > size.x)
{
if(content_size.x > size.x) {
reduce_width(size.x);
content_size = widget_->get_best_size();
}
Expand All @@ -78,13 +73,23 @@ void size_lock::finalize(builder_widget_const_ptr widget_builder)
set_rows_cols(1u, 1u);

widget_ = widget_builder->build();
set_child(widget_, 0u, 0u,
grid::VERTICAL_GROW_SEND_TO_CLIENT | grid::HORIZONTAL_GROW_SEND_TO_CLIENT,
0u);
set_child(widget_, 0u, 0u, grid::VERTICAL_GROW_SEND_TO_CLIENT | grid::HORIZONTAL_GROW_SEND_TO_CLIENT, 0u);
}

point size_lock::calculate_best_size() const
{
const wfl::map_formula_callable& size = get_screen_size_variables();

unsigned width = width_(size);
unsigned height = height_(size);

VALIDATE(width > 0 || height > 0, _("Invalid size."));

return point(width, height);
}

size_lock_definition::size_lock_definition(const config& cfg) :
styled_widget_definition(cfg)
size_lock_definition::size_lock_definition(const config& cfg)
: styled_widget_definition(cfg)
{
DBG_GUI_P << "Parsing fixed size widget " << id << '\n';

Expand All @@ -108,8 +113,9 @@ size_lock_definition::size_lock_definition(const config& cfg) :
* @end{tag}{name="size_lock_definition"}
* @end{tag}{name="gui/"}
*/
size_lock_definition::resolution::resolution(const config& cfg) :
resolution_definition(cfg), grid(nullptr)
size_lock_definition::resolution::resolution(const config& cfg)
: resolution_definition(cfg)
, grid(nullptr)
{
// Add a dummy state since every widget needs a state.
static config dummy("draw");
Expand Down Expand Up @@ -147,9 +153,11 @@ size_lock_definition::resolution::resolution(const config& cfg) :

namespace implementation
{

builder_size_lock::builder_size_lock(const config& cfg) :
builder_styled_widget(cfg), content_(nullptr), width_(cfg["width"]), height_(cfg["height"])
builder_size_lock::builder_size_lock(const config& cfg)
: builder_styled_widget(cfg)
, content_(nullptr)
, width_(cfg["width"])
, height_(cfg["height"])
{
VALIDATE(cfg.has_child("widget"), _("No widget defined."));
content_ = create_widget_builder(cfg.child("widget"));
Expand All @@ -159,28 +167,15 @@ widget* builder_size_lock::build() const
{
size_lock* widget = new size_lock(*this);

DBG_GUI_G << "Window builder: placed fixed size widget '" << id <<
"' with definition '" << definition << "'.\n";
DBG_GUI_G << "Window builder: placed fixed size widget '" << id << "' with definition '" << definition << "'.\n";

const auto conf = widget->cast_config_to<size_lock_definition>();
assert(conf != nullptr);

widget->init_grid(conf->grid);

wfl::map_formula_callable size = get_screen_size_variables();

const unsigned width = width_(size);
const unsigned height = height_(size);

VALIDATE(width > 0 || height > 0, _("Invalid size."));

widget->set_target_size(point(width, height));

widget->finalize(content_);

return widget;
}

}

}
18 changes: 6 additions & 12 deletions src/gui/widgets/size_lock.hpp
Expand Up @@ -56,19 +56,12 @@ class size_lock : public container_base
/** See @ref widget::layout_children. */
void layout_children() override;

void set_target_size(const point& size)
{
size_ = size;
}

protected:
point calculate_best_size() const override
{
return size_;
}
point calculate_best_size() const override;

private:
point size_;
typed_formula<unsigned> width_;
typed_formula<unsigned> height_;

/**
* Points to the actual widget.
Expand Down Expand Up @@ -118,10 +111,11 @@ struct builder_size_lock : public builder_styled_widget

widget* build() const;

private:
builder_widget_const_ptr content_;
typed_formula<unsigned> width_;
typed_formula<unsigned> height_;

private:
builder_widget_const_ptr content_;
};
}
}

0 comments on commit 293add8

Please sign in to comment.