1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-05-31 22:48:11 +00:00

LibGUI: Add effective and calculated sizes to Widget

Effective sizes are the ones that are actually to be used for layout.
They are just their respective propertys value, or the value returned
by the calculated_<min/preferred>_size, when the respective property
is set to shrink or fit.
The "calculated" values in turn are specific to the widget. Container
widgets for example can calculate their values depending on their
layout and child widget requirement.
Simple widgets like labels and buttons can for example calculate their
values based upon their current text.
This commit is contained in:
FrHun 2022-06-12 23:10:50 +02:00 committed by Sam Atkins
parent d825c8ad18
commit ec1e25929e
2 changed files with 41 additions and 0 deletions

View file

@ -9,6 +9,7 @@
#include <AK/EnumBits.h>
#include <AK/JsonObject.h>
#include <AK/NonnullRefPtr.h>
#include <AK/Optional.h>
#include <AK/String.h>
#include <AK/Variant.h>
#include <LibCore/Object.h>
@ -107,6 +108,28 @@ public:
UIDimension preferred_height() const { return m_preferred_size.height(); }
void set_preferred_width(UIDimension width) { set_preferred_size(width, preferred_height()); }
void set_preferred_height(UIDimension height) { set_preferred_size(preferred_width(), height); }
virtual Optional<UISize> calculated_preferred_size() const;
virtual Optional<UISize> calculated_min_size() const;
UISize effective_preferred_size() const
{
auto effective_preferred_size = preferred_size();
if (effective_preferred_size.either_is(SpecialDimension::Shrink))
effective_preferred_size.replace_component_if_matching_with(SpecialDimension::Shrink, effective_min_size());
if (effective_preferred_size.either_is(SpecialDimension::Fit) && calculated_preferred_size().has_value())
effective_preferred_size.replace_component_if_matching_with(SpecialDimension::Fit, calculated_preferred_size().value());
return effective_preferred_size;
}
UISize effective_min_size() const
{
auto effective_min_size = min_size();
if (effective_min_size.either_is(SpecialDimension::Shrink) && calculated_min_size().has_value())
effective_min_size.replace_component_if_matching_with(SpecialDimension::Shrink, calculated_min_size().value());
return effective_min_size;
}
void set_fixed_size(UISize const& size)
{
VERIFY(size.has_only_int_values());