From 96294e60ecf083a3ca1f4f13ea6eee0d098bf37e Mon Sep 17 00:00:00 2001 From: Vaxry Date: Mon, 17 Aug 2026 20:02:26 +0200 Subject: [PATCH] text: fix ellipsizing, layouting, etc. --- src/element/LinearLayout.hpp | 30 +++-- src/element/columnLayout/ColumnLayout.cpp | 32 ++++-- src/element/rowLayout/RowLayout.cpp | 32 ++++-- src/element/spinbox/SpinboxSpinner.cpp | 20 ++-- src/element/text/Text.cpp | 94 +++++++++------ src/element/text/Text.hpp | 8 +- tests/AccentButton.cpp | 2 +- tests/AutosizeTest.cpp | 14 +-- tests/Controls.cpp | 22 ++-- tests/unit/elements/LinearLayout.cpp | 132 ++++++++++++++++++++++ tests/unit/elements/Spinbox.cpp | 73 ++++++++++++ tests/unit/elements/Text.cpp | 78 ++++++++++--- 12 files changed, 427 insertions(+), 110 deletions(-) create mode 100644 tests/unit/elements/LinearLayout.cpp create mode 100644 tests/unit/elements/Spinbox.cpp diff --git a/src/element/LinearLayout.hpp b/src/element/LinearLayout.hpp index 6b5a875f..21656af5 100644 --- a/src/element/LinearLayout.hpp +++ b/src/element/LinearLayout.hpp @@ -31,6 +31,8 @@ namespace Hyprtoolkit::LinearLayout { std::vector sizes; sizes.resize(C.size()); + std::vector preferredSizes; + preferredSizes.resize(C.size()); size_t i = 0; for (i = 0; i < C.size(); ++i) { @@ -39,6 +41,7 @@ namespace Hyprtoolkit::LinearLayout { Vector2D cSize = childSize(child); if (cSize == Vector2D{-1, -1}) cSize = Horizontal ? Vector2D{1.F, box.h} : Vector2D{box.w, 1.F}; + preferredSizes.at(i) = axisPrimary(cSize); if (used + axisPrimary(cSize) > MAX + 1) { // we exceeded our available space. @@ -56,7 +59,7 @@ namespace Hyprtoolkit::LinearLayout { // (needs > 0 below), the current child is dropped and we // expand the previous one to cover the gap; minor visual // artefact in degenerate cases but the layout stays sane. - float needs = (used + axisPrimary(cSize)) - (MAX + 1); + float needs = (used + axisPrimary(cSize)) - MAX; for (int j = (int)i - 1; j >= 0; --j) { const auto& prevChild = C.at(j); const auto MIN = prevChild->minimumSize(box.size()); @@ -91,11 +94,12 @@ namespace Hyprtoolkit::LinearLayout { child->impl->setFailedPositioning(true); if (i != 0) { const auto& lastChild = C.at(i - 1); + const auto AVAILABLE = std::max(0.0, MAX - used); - if (lastChild->maximumSize(box.size()) && sizes.at(i - 1) + MAX - used > axisPrimary(*lastChild->maximumSize(box.size()))) + if (lastChild->maximumSize(box.size()) && sizes.at(i - 1) + AVAILABLE > axisPrimary(*lastChild->maximumSize(box.size()))) continue; // too bad, we'll have a gap - sizes.at(i - 1) += MAX - used; + sizes.at(i - 1) += AVAILABLE; } continue; } else { @@ -104,15 +108,19 @@ namespace Hyprtoolkit::LinearLayout { // recalc used, we changed prior sizes used = 0; - for (const auto& s : sizes) - used += s + gap; + for (size_t j = 0; j <= i; ++j) { + if (!C.at(j)->impl->failedPositioning) + used += sizes.at(j) + gap; + } continue; } } // squeeze the last element in - sizes.at(i) = MAX - used; + child->impl->setFailedPositioning(false); + sizes.at(i) = std::max(0.0, MAX - used); + used = MAX + gap; // the common final-gap removal below still runs continue; } @@ -142,17 +150,19 @@ namespace Hyprtoolkit::LinearLayout { if (child->impl->failedPositioning) continue; - Vector2D cSize = childSize(child); + Vector2D cSize = childSize(child); + const double ROOM = grows(child) ? 0 : std::max(0.0, MAX - used); + const bool SQUEEZED = sizes.at(i) + ROOM + 1 < preferredSizes.at(i); - CBox childBox; + CBox childBox; if constexpr (Horizontal) { cSize.y = std::clamp(cSize.y, 0.0, box.h); childBox = CBox{box.x + (double)cursor, box.y + ((box.h - cSize.y) / 2), (double)sizes.at(i), cSize.y}; - g_positioner->position(child, childBox, Vector2D{childBox.w + (MAX - used), box.h}); + g_positioner->position(child, childBox, Vector2D{SQUEEZED ? childBox.w + ROOM : -1, box.h}); } else { cSize.x = std::clamp(cSize.x, 0.0, box.w); childBox = CBox{box.x + ((box.w - cSize.x) / 2), box.y + (double)cursor, cSize.x, (double)sizes.at(i)}; - g_positioner->position(child, childBox, Vector2D{box.w, childBox.h + (MAX - used)}); + g_positioner->position(child, childBox, Vector2D{box.w, SQUEEZED ? childBox.h + ROOM : -1}); } cursor += (size_t)((Horizontal ? childBox.w : childBox.h) + gap); diff --git a/src/element/columnLayout/ColumnLayout.cpp b/src/element/columnLayout/ColumnLayout.cpp index aac676ed..235052da 100644 --- a/src/element/columnLayout/ColumnLayout.cpp +++ b/src/element/columnLayout/ColumnLayout.cpp @@ -8,9 +8,19 @@ #include "../Element.hpp" #include "../LinearLayout.hpp" +#include "../text/Text.hpp" using namespace Hyprtoolkit; +static Vector2D childSizeForConstraint(const SP& child, const Vector2D& constraint, const Vector2D& textConstraint) { + const auto CHILD_CONSTRAINT = dynamicPointerCast(child) ? textConstraint : constraint; + if (const auto PREFERRED = child->preferredSize(CHILD_CONSTRAINT)) + return *PREFERRED; + if (const auto MINIMUM = child->minimumSize(CHILD_CONSTRAINT)) + return *MINIMUM; + return {-1, -1}; +} + SP CColumnLayoutElement::create(const SColumnLayoutData& data) { auto p = SP(new CColumnLayoutElement(data)); p->impl->self = p; @@ -51,11 +61,8 @@ Hyprutils::Math::Vector2D CColumnLayoutElement::size() { } Hyprutils::Math::Vector2D CColumnLayoutElement::childSize(Hyprutils::Memory::CSharedPointer child) { - if (child->preferredSize(impl->position.size())) - return *child->preferredSize(impl->position.size()); - else if (child->minimumSize(impl->position.size())) - return *child->minimumSize(impl->position.size()); - return {-1, -1}; + const auto CONSTRAINT = impl->position.size(); + return childSizeForConstraint(child, CONSTRAINT, {CONSTRAINT.x, -1.F}); } std::optional CColumnLayoutElement::preferredSize(const Hyprutils::Math::Vector2D& parent) { @@ -64,10 +71,12 @@ std::optional CColumnLayoutElement::preferredSize(con if (calc.x != -1 && calc.y != -1) return calc; - Vector2D max; + const Vector2D TEXT_CONSTRAINT{calc.x > 0 ? std::max(1.0, calc.x - impl->margin * 2.F) : -1.F, -1.F}; + Vector2D max; for (const auto& child : impl->children) { - max.x = std::max(childSize(child).x, max.x); - max.y += childSize(child).y + m_impl->data.gap; + const auto CHILD_SIZE = childSizeForConstraint(child, parent, TEXT_CONSTRAINT); + max.x = std::max(CHILD_SIZE.x, max.x); + max.y += CHILD_SIZE.y + m_impl->data.gap; } if (!impl->children.empty()) @@ -88,9 +97,12 @@ std::optional CColumnLayoutElement::preferredSize(con } std::optional CColumnLayoutElement::minimumSize(const Hyprutils::Math::Vector2D& parent) { - Vector2D min; + const auto CALC = m_impl->data.size.calculate(parent); + const Vector2D TEXT_CONSTRAINT = {CALC.x > 0 ? std::max(1.0, CALC.x - impl->margin * 2.F) : -1.F, -1.F}; + Vector2D min; for (const auto& child : impl->children) { - min.x = std::max(min.x, childSize(child).x); + const auto CHILD_SIZE = childSizeForConstraint(child, parent, TEXT_CONSTRAINT); + min.x = std::max(min.x, CHILD_SIZE.x); } return min; diff --git a/src/element/rowLayout/RowLayout.cpp b/src/element/rowLayout/RowLayout.cpp index 51c63b13..6ab3c2b8 100644 --- a/src/element/rowLayout/RowLayout.cpp +++ b/src/element/rowLayout/RowLayout.cpp @@ -8,9 +8,19 @@ #include "../Element.hpp" #include "../LinearLayout.hpp" +#include "../text/Text.hpp" using namespace Hyprtoolkit; +static Vector2D childSizeForConstraint(const SP& child, const Vector2D& constraint, const Vector2D& textConstraint) { + const auto CHILD_CONSTRAINT = dynamicPointerCast(child) ? textConstraint : constraint; + if (const auto PREFERRED = child->preferredSize(CHILD_CONSTRAINT)) + return *PREFERRED; + if (const auto MINIMUM = child->minimumSize(CHILD_CONSTRAINT)) + return *MINIMUM; + return {-1, -1}; +} + SP CRowLayoutElement::create(const SRowLayoutData& data) { auto p = SP(new CRowLayoutElement(data)); p->impl->self = p; @@ -39,11 +49,8 @@ void CRowLayoutElement::reposition(const Hyprutils::Math::CBox& sbox, const Hypr } Hyprutils::Math::Vector2D CRowLayoutElement::childSize(Hyprutils::Memory::CSharedPointer child) { - if (child->preferredSize(impl->position.size())) - return *child->preferredSize(impl->position.size()); - else if (child->minimumSize(impl->position.size())) - return *child->minimumSize(impl->position.size()); - return {-1, -1}; + const auto CONSTRAINT = impl->position.size(); + return childSizeForConstraint(child, CONSTRAINT, {-1.F, CONSTRAINT.y}); } Hyprutils::Math::Vector2D CRowLayoutElement::size() { @@ -56,10 +63,12 @@ std::optional CRowLayoutElement::preferredSize(const if (calc.x != -1 && calc.y != -1) return calc; - Vector2D max; + const Vector2D TEXT_CONSTRAINT{-1.F, calc.y > 0 ? calc.y : -1.F}; + Vector2D max; for (const auto& child : impl->children) { - max.x += childSize(child).x + m_impl->data.gap; - max.y = std::max(max.y, childSize(child).y); + const auto CHILD_SIZE = childSizeForConstraint(child, parent, TEXT_CONSTRAINT); + max.x += CHILD_SIZE.x + m_impl->data.gap; + max.y = std::max(max.y, CHILD_SIZE.y); } if (!impl->children.empty()) @@ -80,9 +89,12 @@ std::optional CRowLayoutElement::preferredSize(const } std::optional CRowLayoutElement::minimumSize(const Hyprutils::Math::Vector2D& parent) { - Vector2D min; + const auto CALC = m_impl->data.size.calculate(parent); + const Vector2D TEXT_CONSTRAINT = {-1.F, CALC.y > 0 ? CALC.y : -1.F}; + Vector2D min; for (const auto& child : impl->children) { - min.y = std::max(min.y, childSize(child).y); + const auto CHILD_SIZE = childSizeForConstraint(child, parent, TEXT_CONSTRAINT); + min.y = std::max(min.y, CHILD_SIZE.y); } return min; diff --git a/src/element/spinbox/SpinboxSpinner.cpp b/src/element/spinbox/SpinboxSpinner.cpp index f93b50a7..0ad31c31 100644 --- a/src/element/spinbox/SpinboxSpinner.cpp +++ b/src/element/spinbox/SpinboxSpinner.cpp @@ -20,15 +20,17 @@ SP CSpinboxSpinner::create(SP data) { return p; } -constexpr float ANGLE_SIZE = 12.F; -constexpr float INNER_MARG = 2.F; +constexpr float ANGLE_SIZE = 12.F; +constexpr float INNER_MARG = 2.F; +constexpr float INNER_GAP = 6.F; +constexpr float INNER_FIXED_WIDTH = 2.F * ANGLE_SIZE + 4.F * INNER_GAP + 2.F * INNER_MARG; CSpinboxSpinner::CSpinboxSpinner(SP data) : IElement(), m_parent(data) { ; } void CSpinboxSpinner::init() { - m_layout = CRowLayoutBuilder::begin()->gap(6)->size({CDynamicSize::HT_SIZE_AUTO, CDynamicSize::HT_SIZE_AUTO, {1, 1}})->commence(); + m_layout = CRowLayoutBuilder::begin()->gap(INNER_GAP)->size({CDynamicSize::HT_SIZE_AUTO, CDynamicSize::HT_SIZE_AUTO, {1, 1}})->commence(); m_layout->setPositionFlag(HT_POSITION_FLAG_CENTER, true); m_layout->setPositionMode(HT_POSITION_ABSOLUTE); @@ -39,6 +41,7 @@ void CSpinboxSpinner::init() { ->color([] { return g_palette->m_colors.text; }) ->callback([this] { impl->window->scheduleReposition(impl->self); }) ->commence(); + m_label->setGrow(true, false); m_background = CRectangleBuilder::begin() ->color([] { return g_palette->m_colors.base; }) @@ -66,8 +69,8 @@ void CSpinboxSpinner::init() { .spinner = m_self, }); - m_leftPad = CNullBuilder::begin()->size({CDynamicSize::HT_SIZE_ABSOLUTE, CDynamicSize::HT_SIZE_PERCENT, {0.F, 1.F}})->commence(); - m_rightPad = CNullBuilder::begin()->size({CDynamicSize::HT_SIZE_ABSOLUTE, CDynamicSize::HT_SIZE_PERCENT, {0.F, 1.F}})->commence(); + m_leftPad = CNullBuilder::begin()->size({CDynamicSize::HT_SIZE_ABSOLUTE, CDynamicSize::HT_SIZE_ABSOLUTE, {0.F, 0.F}})->commence(); + m_rightPad = CNullBuilder::begin()->size({CDynamicSize::HT_SIZE_ABSOLUTE, CDynamicSize::HT_SIZE_ABSOLUTE, {0.F, 0.F}})->commence(); m_layout->addChild(m_leftPad); m_layout->addChild(m_left); @@ -115,7 +118,8 @@ void CSpinboxSpinner::paint() { void CSpinboxSpinner::reposition(const Hyprutils::Math::CBox& box, const Hyprutils::Math::Vector2D& maxSize) { IElement::reposition(box); - g_positioner->positionChildren(impl->self.lock()); + g_positioner->position(m_background, impl->position); + g_positioner->position(m_layout, impl->position, impl->position.size()); } Hyprutils::Math::Vector2D CSpinboxSpinner::size() { @@ -127,11 +131,11 @@ std::optional CSpinboxSpinner::preferredSize(const Hyprutils::Math::Ve } std::optional CSpinboxSpinner::minimumSize(const Hyprutils::Math::Vector2D& parent) { - return m_layout->preferredSize(parent); + return Vector2D{INNER_FIXED_WIDTH, ANGLE_SIZE + 2.F * INNER_MARG}; } std::optional CSpinboxSpinner::maximumSize(const Hyprutils::Math::Vector2D& parent) { - return m_layout->preferredSize(parent); + return preferredSize(parent); } bool CSpinboxSpinner::acceptsMouseInput() { diff --git a/src/element/text/Text.cpp b/src/element/text/Text.cpp index e7e5fdd7..27b45704 100644 --- a/src/element/text/Text.cpp +++ b/src/element/text/Text.cpp @@ -44,7 +44,7 @@ CTextElement::CTextElement(const STextData& data) : IElement(), m_impl(makeUniqu m_impl->data = data; m_impl->parseText(); m_impl->lastFontSizeUnscaled = m_impl->data.fontSize.ptSize(); - m_impl->preferred = m_impl->getTextSizePreferred(); + m_impl->updatePreferred(); impl->m_externalEvents.mouseMove.listenStatic([this](const Vector2D& pos) { m_impl->lastCursorPos = pos; @@ -67,7 +67,7 @@ void CTextElement::setText(std::string text) { m_impl->data.text = std::move(text); m_impl->parseText(); - m_impl->preferred = m_impl->getTextSizePreferred(); + m_impl->updatePreferred(); m_impl->scheduleTexRefresh(); if (impl->window) @@ -78,6 +78,9 @@ void CTextElement::replaceData(const STextData& data) { const bool TEXT_DIFFERENT = data.text != m_impl->data.text; const auto COLOR = data.color(); const bool COLOR_DIFFERENT = COLOR != m_impl->color->goal(); + const bool ALIGN_DIFFERENT = data.align != m_impl->data.align; + const bool MEASUREMENT_DIFFERENT = + TEXT_DIFFERENT || data.fontFamily != m_impl->data.fontFamily || data.noEllipsize != m_impl->data.noEllipsize || data.clampSize != m_impl->data.clampSize; m_impl->data = data; if (m_impl->colorAnimationEnabled) { @@ -90,12 +93,14 @@ void CTextElement::replaceData(const STextData& data) { m_impl->needsTexRefresh = m_impl->needsTexRefresh || COLOR_DIFFERENT; } - if (m_impl->lastFontSizeUnscaled != m_impl->data.fontSize.ptSize() || TEXT_DIFFERENT) { - m_impl->parseText(); + if (MEASUREMENT_DIFFERENT || m_impl->lastFontSizeUnscaled != m_impl->data.fontSize.ptSize()) { + if (TEXT_DIFFERENT) + m_impl->parseText(); m_impl->lastFontSizeUnscaled = m_impl->data.fontSize.ptSize(); - m_impl->preferred = m_impl->getTextSizePreferred(); + m_impl->updatePreferred(); + m_impl->scheduleTexRefresh(); + } else if (ALIGN_DIFFERENT) m_impl->scheduleTexRefresh(); - } if (impl->window) impl->window->scheduleReposition(impl->self); @@ -130,9 +135,12 @@ void CTextElement::paint() { return; } - if ((impl->window && impl->window->scale() != m_impl->lastScale) || m_impl->needsTexRefresh) { - m_impl->lastScale = impl->window ? impl->window->scale() : 1.F; - m_impl->preferred = m_impl->getTextSizePreferred(); + const bool SCALE_CHANGED = impl->window && impl->window->scale() != m_impl->lastScale; + if (SCALE_CHANGED || m_impl->needsTexRefresh) { + if (SCALE_CHANGED) { + m_impl->lastScale = impl->window->scale(); + m_impl->updatePreferred(); + } if (!m_impl->resource) m_impl->renderTex(); textureToUse = m_impl->oldTex; @@ -170,7 +178,12 @@ void CTextElement::reposition(const Hyprutils::Math::CBox& box, const Hyprutils: IElement::reposition(box); const auto DESIRED = m_impl->preferred; - if (DESIRED.x > 0 && DESIRED.y > 0 && !m_impl->data.noEllipsize) { + if (m_impl->data.noEllipsize) { + if (m_impl->lastMaxSize != Vector2D{-1, -1}) { + m_impl->lastMaxSize = {-1, -1}; + m_impl->needsTexRefresh = true; + } + } else if (DESIRED.x > 0 && DESIRED.y > 0) { const auto PREV = m_impl->lastMaxSize; m_impl->lastMaxSize = {-1, -1}; const auto SIZE = box.size(); @@ -178,23 +191,15 @@ void CTextElement::reposition(const Hyprutils::Math::CBox& box, const Hyprutils: m_impl->lastMaxSize.x = maxSize.x; if (maxSize.y > 0) m_impl->lastMaxSize.y = maxSize.y; - // clamp to the layout's available-room hint (maxSize), already applied above. only fall - // back to the actual box when no hint was given: an auto-sized label's box equals its own - // (clamped) preferred and feeds back, flip-flopping at the boundary, whereas the room hint - // is stable and reflects the real container, so the text settles and recovers. + // The room hint is the authoritative constraint. Only use the allocated box when its + // layout cannot provide one; neither constraint changes the independent measured result. if (maxSize.x <= 0 && SIZE.x + 1 < DESIRED.x) m_impl->lastMaxSize.x = SIZE.x; if (maxSize.y <= 0 && SIZE.y + 1 < DESIRED.y) m_impl->lastMaxSize.y = SIZE.y; - if (PREV != m_impl->lastMaxSize) { + if (PREV != m_impl->lastMaxSize) m_impl->needsTexRefresh = true; - const auto LAST_PREF = m_impl->preferred; - m_impl->lastScale = impl->window ? impl->window->scale() : 1.F; - m_impl->preferred = m_impl->getTextSizePreferred(); - if (impl->window && (std::abs(LAST_PREF.x - m_impl->preferred.x) > 2 || std::abs(LAST_PREF.y - m_impl->preferred.y) > 2)) - impl->window->scheduleReposition(impl->self.lock()); - } } g_positioner->positionChildren(impl->self.lock()); @@ -222,7 +227,10 @@ std::optional CTextElement::maximumSize(const Hyprutils::Math::Vector2 } std::optional CTextElement::preferredSize(const Hyprutils::Math::Vector2D& parent) { - return m_impl->preferred; + Vector2D constraint = parent; + if (constraint == Vector2D{0, 0}) + constraint = {-1, -1}; + return m_impl->measure(constraint); } std::optional CTextElement::minimumSize(const Hyprutils::Math::Vector2D& parent) { @@ -241,7 +249,7 @@ bool CTextElement::positioningDependsOnChild() { return m_impl->data.size.hasAuto(); } -std::tuple, cairo_t*, PangoLayout*, Vector2D> STextImpl::prepPangoLayout() { +std::tuple, cairo_t*, PangoLayout*, Vector2D> STextImpl::prepPangoLayout(const Vector2D& constraint) { auto CAIROSURFACE = makeUnique(cairo_image_surface_create(CAIRO_FORMAT_ARGB32, 1, 1 /* dummy value */)); auto CAIRO = cairo_create(CAIROSURFACE->cairo()); @@ -285,7 +293,7 @@ std::tuple, cairo_t*, PangoLayout*, Vector2D> ST PangoRectangle ink, logical; pango_layout_get_pixel_extents(layout, &ink, &logical); - std::optional maxSize = data.clampSize.value_or(lastMaxSize).round(); + std::optional maxSize = constraint.round(); if (maxSize == Vector2D{0, 0}) maxSize = std::nullopt; @@ -294,7 +302,7 @@ std::tuple, cairo_t*, PangoLayout*, Vector2D> ST if (maxSize.has_value()) { const auto CLAMP_SIZE = maxSize.value(); - if (!data.noEllipsize && maxSize.has_value() && maxSize->y >= 0) + if (!data.noEllipsize && maxSize->y >= 0) pango_layout_set_ellipsize(layout, PANGO_ELLIPSIZE_END); if (CLAMP_SIZE.x >= 0) pango_layout_set_width(layout, std::min(logical.width * PANGO_SCALE, sc(CLAMP_SIZE.x * PANGO_SCALE))); @@ -312,7 +320,11 @@ std::tuple, cairo_t*, PangoLayout*, Vector2D> ST } Hyprutils::Math::Vector2D STextImpl::getTextSizePreferred() { - auto [CAIROSURFACE, CAIRO, LAYOUT, LAYOUTSIZE] = prepPangoLayout(); + return getTextSizePreferred(data.clampSize.value_or(lastMaxSize)); +} + +Hyprutils::Math::Vector2D STextImpl::getTextSizePreferred(const Vector2D& maxSize) { + auto [CAIROSURFACE, CAIRO, LAYOUT, LAYOUTSIZE] = prepPangoLayout(maxSize); g_object_unref(LAYOUT); cairo_destroy(CAIRO); @@ -320,8 +332,23 @@ Hyprutils::Math::Vector2D STextImpl::getTextSizePreferred() { return LAYOUTSIZE / lastScale; } +Hyprutils::Math::Vector2D STextImpl::measure(const Vector2D& maxSize) { + const auto CONSTRAINT = data.clampSize.value_or(data.noEllipsize ? Vector2D{-1, -1} : maxSize).round(); + if (lastMeasureConstraint == CONSTRAINT) + return lastMeasuredSize; + + lastMeasureConstraint = CONSTRAINT; + lastMeasuredSize = getTextSizePreferred(CONSTRAINT); + return lastMeasuredSize; +} + +void STextImpl::updatePreferred() { + lastMeasureConstraint.reset(); + preferred = getTextSizePreferred(data.clampSize.value_or(Vector2D{-1, -1})); +} + CBox STextImpl::getCharBox(size_t offset) { - auto [CAIROSURFACE, CAIRO, LAYOUT, LAYOUTSIZE] = prepPangoLayout(); + auto [CAIROSURFACE, CAIRO, LAYOUT, LAYOUTSIZE] = prepPangoLayout(data.clampSize.value_or(lastMaxSize)); PangoRectangle rect; @@ -343,7 +370,7 @@ CBox STextImpl::getCharBox(size_t offset) { } std::optional STextImpl::vecToOffset(const Vector2D& vec) { - auto [CAIROSURFACE, CAIRO, LAYOUT, LAYOUTSIZE] = prepPangoLayout(); + auto [CAIROSURFACE, CAIRO, LAYOUT, LAYOUTSIZE] = prepPangoLayout(data.clampSize.value_or(lastMaxSize)); auto pangoX = sc(vec.x * PANGO_SCALE), // pangoY = sc(vec.y * PANGO_SCALE); @@ -383,10 +410,7 @@ Vector2D STextImpl::unscale(const Vector2D& x) { } void STextImpl::scheduleTexRefresh() { - if (data.async) { - needsTexRefresh = true; - return; - } + needsTexRefresh = true; } void STextImpl::renderTex() { @@ -399,7 +423,11 @@ void STextImpl::renderTex() { waitingForTex = true; - lastScale = self->impl->window ? self->impl->window->scale() : 1.F; + const auto SCALE = self->impl->window ? self->impl->window->scale() : 1.F; + if (SCALE != lastScale) { + lastScale = SCALE; + updatePreferred(); + } self->impl->damageEntire(); diff --git a/src/element/text/Text.hpp b/src/element/text/Text.hpp index 31d2306d..27155a95 100644 --- a/src/element/text/Text.hpp +++ b/src/element/text/Text.hpp @@ -47,13 +47,14 @@ namespace Hyprtoolkit { bool needsTexRefresh = false, newTex = false; Hyprutils::Math::Vector2D lastMaxSize; + std::optional lastMeasureConstraint; SP tex; SP oldTex; // while loading a new one PHLANIMVAR color; SP colorAnimationConfig; ASP resource; - Hyprutils::Math::Vector2D size, preferred; + Hyprutils::Math::Vector2D size, preferred, lastMeasuredSize; Hyprutils::Math::Vector2D lastCursorPos; @@ -62,12 +63,15 @@ namespace Hyprtoolkit { bool renderColorAtPaint = false; Hyprutils::Math::Vector2D getTextSizePreferred(); + Hyprutils::Math::Vector2D getTextSizePreferred(const Hyprutils::Math::Vector2D& maxSize); + Hyprutils::Math::Vector2D measure(const Hyprutils::Math::Vector2D& maxSize); + void updatePreferred(); Hyprutils::Math::CBox getCharBox(size_t offset); std::optional vecToOffset(const Hyprutils::Math::Vector2D& vec); float getCursorPos(size_t offset); float getCursorPos(const Hyprutils::Math::Vector2D& click); Hyprutils::Math::Vector2D unscale(const Hyprutils::Math::Vector2D& x); - std::tuple, cairo_t*, PangoLayout*, Hyprutils::Math::Vector2D> prepPangoLayout(); + std::tuple, cairo_t*, PangoLayout*, Hyprutils::Math::Vector2D> prepPangoLayout(const Hyprutils::Math::Vector2D& maxSize); void scheduleTexRefresh(); void renderTex(); void postTexLoad(); diff --git a/tests/AccentButton.cpp b/tests/AccentButton.cpp index f18762a3..6aa6fb07 100644 --- a/tests/AccentButton.cpp +++ b/tests/AccentButton.cpp @@ -14,7 +14,7 @@ using namespace Hyprtoolkit; static SP backend; -int main() { +int main() { backend = IBackend::create(); auto window = CWindowBuilder::begin() // diff --git a/tests/AutosizeTest.cpp b/tests/AutosizeTest.cpp index f2d1f021..ac15fa23 100644 --- a/tests/AutosizeTest.cpp +++ b/tests/AutosizeTest.cpp @@ -15,14 +15,11 @@ static SP backend; static SP anchor; static int step = 0; -static void schedule(); -static void tick(Hyprutils::Memory::CAtomicSharedPointer, void*) { +static void schedule(); +static void tick(Hyprutils::Memory::CAtomicSharedPointer, void*) { const float sizes[][2] = {{240, 80}, {300, 200}, {500, 350}, {180, 120}, {900, 700}}; const auto& s = sizes[step++ % 5]; - anchor->rebuild() - ->color([] { return CHyprColor{0.F, 0.F, 0.F, 0.F}; }) - ->size({CDynamicSize::HT_SIZE_ABSOLUTE, CDynamicSize::HT_SIZE_ABSOLUTE, {s[0], s[1]}}) - ->commence(); + anchor->rebuild()->color([] { return CHyprColor{0.F, 0.F, 0.F, 0.F}; })->size({CDynamicSize::HT_SIZE_ABSOLUTE, CDynamicSize::HT_SIZE_ABSOLUTE, {s[0], s[1]}})->commence(); schedule(); } static void schedule() { @@ -58,7 +55,10 @@ int main() { schedule(); - window->m_events.closeRequest.listenStatic([w = WP{window}] { w->close(); backend->destroy(); }); + window->m_events.closeRequest.listenStatic([w = WP{window}] { + w->close(); + backend->destroy(); + }); window->open(); backend->enterLoop(); diff --git a/tests/Controls.cpp b/tests/Controls.cpp index aa1f7c11..ac19645a 100644 --- a/tests/Controls.cpp +++ b/tests/Controls.cpp @@ -99,11 +99,11 @@ static void selectTextbox() { } static SP stretchLayout(std::string&& label, SP control) { - auto bg = CRectangleBuilder::begin() - ->color([] { return backend->getPalette()->m_colors.alternateBase; }) - ->size({CDynamicSize::HT_SIZE_PERCENT, CDynamicSize::HT_SIZE_AUTO, {1, 1}}) - ->rounding(4) - ->commence(); + auto bg = CRectangleBuilder::begin() + ->color([] { return backend->getPalette()->m_colors.alternateBase; }) + ->size({CDynamicSize::HT_SIZE_PERCENT, CDynamicSize::HT_SIZE_AUTO, {1, 1}}) + ->rounding(4) + ->commence(); auto layoutE = CRowLayoutBuilder::begin()->size({CDynamicSize::HT_SIZE_PERCENT, CDynamicSize::HT_SIZE_AUTO, {1, 1}})->commence(); auto labelE = CTextBuilder::begin()->text(std::move(label))->color([] { return backend->getPalette()->m_colors.text; })->commence(); auto nullE = CNullBuilder::begin()->commence(); @@ -220,8 +220,8 @@ int main(int argc, char** argv, char** envp) { auto slider2 = stretchLayout( "Big Slider", CSliderBuilder::begin()->max(10000)->val(2500)->size({CDynamicSize::HT_SIZE_PERCENT, CDynamicSize::HT_SIZE_ABSOLUTE, {0.5F, SLIDER_HEIGHT}})->commence()); - auto progress = stretchLayout( - "Progress", CProgressBarBuilder::begin()->value(0.42F)->size({CDynamicSize::HT_SIZE_PERCENT, CDynamicSize::HT_SIZE_ABSOLUTE, {0.5F, 14.F}})->commence()); + auto progress = + stretchLayout("Progress", CProgressBarBuilder::begin()->value(0.42F)->size({CDynamicSize::HT_SIZE_PERCENT, CDynamicSize::HT_SIZE_ABSOLUTE, {0.5F, 14.F}})->commence()); auto combo = stretchLayout( "Combo", @@ -248,11 +248,11 @@ int main(int argc, char** argv, char** envp) { hiddenSlider = CSliderBuilder::begin()->max(100)->val(69)->size({CDynamicSize::HT_SIZE_PERCENT, CDynamicSize::HT_SIZE_ABSOLUTE, {1.F, SLIDER_HEIGHT}})->commence(); hiddenText = CTextBuilder::begin() - ->text("hi hi overflow overflow overflow overflow overflow overflow overflow overflow overflow overflow overflow overflow overflow overflow overflow") - ->commence(); + ->text("hi hi overflow overflow overflow overflow overflow overflow overflow overflow overflow overflow overflow overflow overflow overflow overflow") + ->commence(); auto rowl = CRowLayoutBuilder::begin()->size({CDynamicSize::HT_SIZE_PERCENT, CDynamicSize::HT_SIZE_AUTO, {1, 1}})->commence(); - rowl->addChild(CTextBuilder::begin()->text("hello this will be ellipsized woooo oooo ooo oo oo oo oo o oo")->commence()); + rowl->addChild(CTextBuilder::begin()->text("hello this will be ellipsized woooo oooo ooo oo oo oo oo o oo oo oo o o o oo o o o o o o oo")->commence()); mainLayout->addChild(title); mainLayout->addChild(hr); @@ -288,4 +288,4 @@ int main(int argc, char** argv, char** envp) { backend->enterLoop(); return 0; -} \ No newline at end of file +} diff --git a/tests/unit/elements/LinearLayout.cpp b/tests/unit/elements/LinearLayout.cpp new file mode 100644 index 00000000..e077c62d --- /dev/null +++ b/tests/unit/elements/LinearLayout.cpp @@ -0,0 +1,132 @@ +#include + +#include +#include + +#include +#include +#include +#include +#include +#include + +#include "../tricks/Tricks.hpp" + +using namespace Hyprtoolkit; +using namespace Hyprutils::Math; + +TEST(Element, columnTextWrapsAndExpandsAutoHeight) { + Tests::Tricks::createBackendSupport(); + + auto column = CColumnLayoutBuilder::begin()->size({CDynamicSize::HT_SIZE_ABSOLUTE, CDynamicSize::HT_SIZE_AUTO, {100.F, 1.F}})->commence(); + auto text = CTextBuilder::begin()->text("This text is long enough to wrap onto several lines in a narrow column")->commence(); + column->setMargin(5.F); + column->addChild(text); + + const SP COLUMN_ELEMENT = column; + const auto NATURAL = text->preferredSize({}).value_or(Vector2D{}); + const auto WRAPPED = text->preferredSize({90.F, -1.F}).value_or(Vector2D{}); + const auto COLUMN_PREFERRED = COLUMN_ELEMENT->preferredSize({500.F, 500.F}).value_or(Vector2D{}); + ASSERT_FLOAT_EQ(COLUMN_PREFERRED.y, WRAPPED.y + 10.F); + ASSERT_GT(WRAPPED.y, NATURAL.y); + + const CBox BOX{{}, {100.F, COLUMN_PREFERRED.y}}; + g_positioner->position(column, BOX); + + const auto CONSTRAINT = text->m_impl->lastMaxSize; + EXPECT_FLOAT_EQ(CONSTRAINT.x, 90.F); + EXPECT_LT(CONSTRAINT.y, 0.F); + EXPECT_GT(text->m_impl->getTextSizePreferred().y, NATURAL.y); + + g_positioner->position(column, BOX); + EXPECT_EQ(text->m_impl->lastMaxSize, CONSTRAINT); + EXPECT_FLOAT_EQ(text->impl->position.h, WRAPPED.y); +} + +TEST(Element, rowTextEllipsizesOnlyWhenSqueezed) { + Tests::Tricks::createBackendSupport(); + + auto row = CRowLayoutBuilder::begin()->size({CDynamicSize::HT_SIZE_ABSOLUTE, CDynamicSize::HT_SIZE_AUTO, {80.F, 1.F}})->commence(); + auto text = CTextBuilder::begin()->text("This row text is much wider than eighty pixels")->commence(); + row->addChild(text); + + const SP ROW_ELEMENT = row; + const auto NATURAL = text->preferredSize({}).value_or(Vector2D{}); + const auto ROW_PREFERRED = ROW_ELEMENT->preferredSize({80.F, 100.F}).value_or(Vector2D{}); + ASSERT_FLOAT_EQ(ROW_PREFERRED.y, NATURAL.y); + + const CBox BOX{{}, {80.F, ROW_PREFERRED.y}}; + g_positioner->position(row, BOX); + + const auto CONSTRAINT = text->m_impl->lastMaxSize; + EXPECT_GT(CONSTRAINT.x, 0.F); + EXPECT_FLOAT_EQ(CONSTRAINT.y, ROW_PREFERRED.y); + EXPECT_LT(text->m_impl->getTextSizePreferred().x, NATURAL.x); + EXPECT_FLOAT_EQ(text->m_impl->getTextSizePreferred().y, NATURAL.y); + + g_positioner->position(row, BOX); + EXPECT_EQ(text->m_impl->lastMaxSize, CONSTRAINT); +} + +TEST(Element, rowUnsqueezedLabelsRemainNatural) { + Tests::Tricks::createBackendSupport(); + + auto row = CRowLayoutBuilder::begin()->gap(8)->size({CDynamicSize::HT_SIZE_ABSOLUTE, CDynamicSize::HT_SIZE_AUTO, {300.F, 1.F}})->commence(); + std::vector> labels; + for (const auto* label : {"first", "second", "third"}) { + row->addChild(CNullBuilder::begin()->size({CDynamicSize::HT_SIZE_ABSOLUTE, CDynamicSize::HT_SIZE_ABSOLUTE, {12.F, 12.F}})->commence()); + labels.emplace_back(CTextBuilder::begin()->text(std::string{label})->commence()); + row->addChild(labels.back()); + } + + const SP ROW_ELEMENT = row; + const auto ROW_PREFERRED = ROW_ELEMENT->preferredSize({300.F, 100.F}).value_or(Vector2D{}); + const CBox BOX{{}, {300.F, ROW_PREFERRED.y}}; + g_positioner->position(row, BOX); + + for (const auto& label : labels) { + const auto NATURAL = label->preferredSize({}).value_or(Vector2D{}); + EXPECT_LT(label->m_impl->lastMaxSize.x, 0.F); + EXPECT_EQ(label->m_impl->getTextSizePreferred(), NATURAL); + EXPECT_FALSE(label->impl->failedPositioning); + } + + g_positioner->position(row, BOX); + for (const auto& label : labels) + EXPECT_LT(label->m_impl->lastMaxSize.x, 0.F); +} + +TEST(Element, growSpacerKeepsLeadingTextNatural) { + Tests::Tricks::createBackendSupport(); + + auto row = CRowLayoutBuilder::begin()->size({CDynamicSize::HT_SIZE_ABSOLUTE, CDynamicSize::HT_SIZE_AUTO, {200.F, 1.F}})->commence(); + auto text = CTextBuilder::begin()->text("Label")->commence(); + auto spacer = CNullBuilder::begin()->commence(); + auto control = CNullBuilder::begin()->size({CDynamicSize::HT_SIZE_ABSOLUTE, CDynamicSize::HT_SIZE_ABSOLUTE, {50.F, 20.F}})->commence(); + spacer->setGrow(true); + row->addChild(text); + row->addChild(spacer); + row->addChild(control); + + g_positioner->position(row, {{}, {200.F, 20.F}}); + + EXPECT_LT(text->m_impl->lastMaxSize.x, 0.F); + EXPECT_EQ(text->m_impl->getTextSizePreferred(), text->preferredSize({}).value_or(Vector2D{})); + EXPECT_FLOAT_EQ(control->impl->position.x + control->impl->position.w, 200.F); +} + +TEST(Element, linearLayoutTextConstraintsPreservePercentageChildren) { + Tests::Tricks::createBackendSupport(); + + auto row = CRowLayoutBuilder::begin()->size({CDynamicSize::HT_SIZE_ABSOLUTE, CDynamicSize::HT_SIZE_ABSOLUTE, {200.F, 40.F}})->commence(); + auto rowChild = CNullBuilder::begin()->size({CDynamicSize::HT_SIZE_PERCENT, CDynamicSize::HT_SIZE_ABSOLUTE, {0.5F, 20.F}})->commence(); + row->addChild(rowChild); + g_positioner->position(row, {{}, {200.F, 40.F}}); + EXPECT_FLOAT_EQ(rowChild->impl->position.w, 100.F); + + auto column = CColumnLayoutBuilder::begin()->size({CDynamicSize::HT_SIZE_ABSOLUTE, CDynamicSize::HT_SIZE_ABSOLUTE, {40.F, 200.F}})->commence(); + auto columnChild = CNullBuilder::begin()->size({CDynamicSize::HT_SIZE_ABSOLUTE, CDynamicSize::HT_SIZE_PERCENT, {20.F, 0.5F}})->commence(); + column->addChild(columnChild); + g_positioner->position(column, {{}, {40.F, 200.F}}); + EXPECT_FLOAT_EQ(columnChild->impl->position.h, 100.F); +} diff --git a/tests/unit/elements/Spinbox.cpp b/tests/unit/elements/Spinbox.cpp new file mode 100644 index 00000000..b4efa87e --- /dev/null +++ b/tests/unit/elements/Spinbox.cpp @@ -0,0 +1,73 @@ +#include + +#include +#include +#include +#include + +#include "../tricks/Tricks.hpp" + +using namespace Hyprtoolkit; +using namespace Hyprutils::Math; + +TEST(Element, spinboxSelectedValueConstraintIsStable) { + Tests::Tricks::createBackendSupport(); + + auto spinbox = CSpinboxBuilder::begin()->label("Label")->items({"A selected value that is much wider than the spinbox"})->fill(true)->commence(); + + const auto outerLayout = spinbox->impl->children.at(0); + const auto caption = dynamicPointerCast(outerLayout->impl->children.at(0)); + const auto spinner = outerLayout->impl->children.at(2); + const auto spinnerLayout = spinner->impl->children.at(1); + const auto value = dynamicPointerCast(spinnerLayout->impl->children.at(2)); + ASSERT_TRUE(caption); + ASSERT_TRUE(value); + + const Vector2D natural = value->preferredSize({}).value_or(Vector2D{}); + ASSERT_GT(natural.x, 140.F); + + constexpr float SPINNER_CHROME_WIDTH = 52.F; + const auto naturalSpinner = spinner->preferredSize({}).value_or(Vector2D{}); + const auto constrainedPreference = spinner->preferredSize({100.F, -1.F}).value_or(Vector2D{}); + const auto boundedPreference = spinner->preferredSize({100.F, 100.F}).value_or(Vector2D{}); + EXPECT_FLOAT_EQ(naturalSpinner.x, natural.x + SPINNER_CHROME_WIDTH); + EXPECT_EQ(constrainedPreference, naturalSpinner); + EXPECT_EQ(boundedPreference, naturalSpinner); + + const CBox constrainedBox{{}, {140.F, 30.F}}; + g_positioner->position(spinbox, {{}, {20.F, 30.F}}); + ASSERT_TRUE(spinner->impl->failedPositioning); + + constexpr float MINIMUM_SPINBOX_WIDTH = 58.F; + g_positioner->position(spinbox, {{}, {MINIMUM_SPINBOX_WIDTH, 30.F}}); + ASSERT_FALSE(spinner->impl->failedPositioning); + ASSERT_LE(spinner->impl->position.x + spinner->impl->position.w, MINIMUM_SPINBOX_WIDTH); + for (const auto& child : spinnerLayout->impl->children) { + ASSERT_FALSE(child->impl->failedPositioning); + ASSERT_LE(child->impl->position.x + child->impl->position.w, spinner->impl->position.x + spinner->impl->position.w); + } + + g_positioner->position(spinbox, constrainedBox); + + ASSERT_FALSE(spinner->impl->failedPositioning); + ASSERT_LE(spinner->impl->position.x + spinner->impl->position.w, spinbox->impl->position.x + spinbox->impl->position.w); + ASSERT_GT(value->m_impl->lastMaxSize.x, 0.F); + ASSERT_LT(value->m_impl->lastMaxSize.x, natural.x); + ASSERT_LE(value->m_impl->lastMaxSize.x, value->impl->position.w); + ASSERT_LE(value->m_impl->getTextSizePreferred().x, value->m_impl->lastMaxSize.x); + + const Vector2D constraint = value->m_impl->lastMaxSize; + for (size_t i = 0; i < 8; ++i) { + g_positioner->position(spinbox, constrainedBox); + EXPECT_FALSE(spinner->impl->failedPositioning); + EXPECT_LE(spinner->impl->position.x + spinner->impl->position.w, spinbox->impl->position.x + spinbox->impl->position.w); + EXPECT_EQ(value->m_impl->lastMaxSize, constraint); + EXPECT_LE(value->m_impl->lastMaxSize.x, value->impl->position.w); + } + + g_positioner->position(spinbox, {{}, {natural.x + 100.F, 30.F}}); + EXPECT_FALSE(spinner->impl->failedPositioning); + EXPECT_LT(value->m_impl->lastMaxSize.x, 0.F); + EXPECT_EQ(value->m_impl->getTextSizePreferred(), natural); + EXPECT_LT(caption->m_impl->lastMaxSize.x, 0.F); +} diff --git a/tests/unit/elements/Text.cpp b/tests/unit/elements/Text.cpp index 7d34c4fc..f0e0f8ea 100644 --- a/tests/unit/elements/Text.cpp +++ b/tests/unit/elements/Text.cpp @@ -25,12 +25,8 @@ TEST(Element, text) { text.reset(); } -// an auto label gets no room hint and its box tracks its own clamped preferred, so the box feeds -// back into the next layout. a transient narrow box on first layout (e.g. before an async icon -// sibling reflows the row) must not lock the text ellipsized: once the box equals the clamped -// preferred the clamp has to release and the text recover. #94 compared the box against a cached -// natural size, so the box-feedback condition stayed true and the ellipsis locked forever. this is -// the hyprlauncher regression. +// A transient box constraint affects rendering, but not a later unconstrained measurement. Keeping +// those independent prevents the rendered result from becoming the next layout's input. TEST(Element, textEllipsizeRecoversFromTransientClamp) { Tests::Tricks::createBackendSupport(); @@ -43,16 +39,13 @@ TEST(Element, textEllipsizeRecoversFromTransientClamp) { // a transient narrow box on first layout clamps it g_positioner->position(text, {{}, {NATURAL * 0.3F, 20.F}}, {-1.F, -1.F}); - EXPECT_LT(text->preferredSize({}).value_or(Vector2D{}).x, NATURAL); - - // from here the layout hands the auto label a box equal to its own preferred. feed that back a - // few frames: it must converge back to NATURAL, not stay locked at the clamped width. - for (int i = 0; i < 4; ++i) { - const auto PREF = text->preferredSize({}).value_or(Vector2D{}); - g_positioner->position(text, {{}, PREF}, {-1.F, -1.F}); - } + EXPECT_LT(text->m_impl->getTextSizePreferred().x, NATURAL); EXPECT_FLOAT_EQ(text->preferredSize({}).value_or(Vector2D{}).x, NATURAL); + const auto PREF = text->preferredSize({}).value_or(Vector2D{}); + g_positioner->position(text, {{}, PREF}, {-1.F, -1.F}); + EXPECT_FLOAT_EQ(text->m_impl->getTextSizePreferred().x, NATURAL); + text.reset(); } @@ -69,16 +62,65 @@ TEST(Element, textEllipsizeStableUnderRoomHint) { const Vector2D tight = {NATURAL * 0.4F, 20.F}; g_positioner->position(text, widebox, tight); - const float CLAMPED = text->preferredSize({}).value_or(Vector2D{}).x; + const float CLAMPED = text->m_impl->getTextSizePreferred().x; EXPECT_LT(CLAMPED, NATURAL); // same room again does not flip the size g_positioner->position(text, widebox, tight); - EXPECT_FLOAT_EQ(text->preferredSize({}).value_or(Vector2D{}).x, CLAMPED); + EXPECT_FLOAT_EQ(text->m_impl->getTextSizePreferred().x, CLAMPED); // room grows back: recover to full g_positioner->position(text, widebox, {NATURAL + 80.F, 20.F}); - EXPECT_FLOAT_EQ(text->preferredSize({}).value_or(Vector2D{}).x, NATURAL); + EXPECT_FLOAT_EQ(text->m_impl->getTextSizePreferred().x, NATURAL); text.reset(); -} \ No newline at end of file +} + +TEST(Element, textMeasurementUsesSuppliedConstraint) { + Tests::Tricks::createBackendSupport(); + + auto text = CTextBuilder::begin()->text("Hello World Foo Bar Baz")->commence(); + const Vector2D natural = text->preferredSize({}).value_or(Vector2D{}); + const Vector2D widthOnlyConstraint{natural.x * 0.4F, -1.F}; + const Vector2D boundedConstraint{widthOnlyConstraint.x, natural.y}; + const Vector2D wrapped = text->preferredSize(widthOnlyConstraint).value_or(Vector2D{}); + const Vector2D bounded = text->preferredSize(boundedConstraint).value_or(Vector2D{}); + + EXPECT_LE(wrapped.x, widthOnlyConstraint.x); + EXPECT_GT(wrapped.y, natural.y); + EXPECT_LE(bounded.x, boundedConstraint.x); + EXPECT_LE(bounded.y, boundedConstraint.y); + + auto unwrapped = CTextBuilder::begin()->text("Hello World Foo Bar Baz")->noEllipsize(true)->commence(); + EXPECT_EQ(unwrapped->preferredSize(widthOnlyConstraint), unwrapped->preferredSize({})); + + text.reset(); + unwrapped.reset(); +} + +TEST(Element, textNoEllipsizeClearsDynamicConstraint) { + Tests::Tricks::createBackendSupport(); + + auto text = CTextBuilder::begin()->text("Hello World Foo Bar Baz")->commence(); + const auto NATURAL = text->preferredSize({}).value_or(Vector2D{}); + g_positioner->position(text, {{}, {NATURAL.x * 0.4F, NATURAL.y}}, {NATURAL.x * 0.4F, NATURAL.y}); + ASSERT_GT(text->m_impl->lastMaxSize.x, 0.F); + + text->rebuild()->noEllipsize(true)->commence(); + g_positioner->position(text, {{}, {NATURAL.x * 0.4F, NATURAL.y}}, {NATURAL.x * 0.4F, NATURAL.y}); + EXPECT_EQ(text->m_impl->lastMaxSize, Vector2D(-1, -1)); + EXPECT_EQ(text->m_impl->getTextSizePreferred(), NATURAL); +} + +TEST(Element, synchronousTextChangesInvalidateTexture) { + Tests::Tricks::createBackendSupport(); + + auto text = CTextBuilder::begin()->text("Before")->async(false)->commence(); + text->m_impl->needsTexRefresh = false; + text->setText("After"); + EXPECT_TRUE(text->m_impl->needsTexRefresh); + + text->m_impl->needsTexRefresh = false; + text->rebuild()->align(HT_FONT_ALIGN_RIGHT)->commence(); + EXPECT_TRUE(text->m_impl->needsTexRefresh); +}