Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 20 additions & 10 deletions src/element/LinearLayout.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,8 @@ namespace Hyprtoolkit::LinearLayout {

std::vector<size_t> sizes;
sizes.resize(C.size());
std::vector<double> preferredSizes;
preferredSizes.resize(C.size());

size_t i = 0;
for (i = 0; i < C.size(); ++i) {
Expand All @@ -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.
Expand All @@ -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());
Expand Down Expand Up @@ -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 {
Expand All @@ -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;
}

Expand Down Expand Up @@ -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);
Expand Down
32 changes: 22 additions & 10 deletions src/element/columnLayout/ColumnLayout.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,19 @@

#include "../Element.hpp"
#include "../LinearLayout.hpp"
#include "../text/Text.hpp"

using namespace Hyprtoolkit;

static Vector2D childSizeForConstraint(const SP<IElement>& child, const Vector2D& constraint, const Vector2D& textConstraint) {
const auto CHILD_CONSTRAINT = dynamicPointerCast<CTextElement>(child) ? textConstraint : constraint;

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think having a specific work around for one type of element is a good idea. Putting text inside a rectangle for a background or a border would break this.

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> CColumnLayoutElement::create(const SColumnLayoutData& data) {
auto p = SP<CColumnLayoutElement>(new CColumnLayoutElement(data));
p->impl->self = p;
Expand Down Expand Up @@ -51,11 +61,8 @@ Hyprutils::Math::Vector2D CColumnLayoutElement::size() {
}

Hyprutils::Math::Vector2D CColumnLayoutElement::childSize(Hyprutils::Memory::CSharedPointer<IElement> 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<Hyprutils::Math::Vector2D> CColumnLayoutElement::preferredSize(const Hyprutils::Math::Vector2D& parent) {
Expand All @@ -64,10 +71,12 @@ std::optional<Hyprutils::Math::Vector2D> 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);

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This should respect calc if it is defined, not just pass in parent.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Passing the parent means that percent children will grow to the full size of the parent, not the container (hence the fix in the spinbox). I think this behavior is helpful and should be kept.

+-------------------------+
| button |  two  | button |
|        | lines |        |
+-------------------------+

The center element is multi line text or some larger height element. And there are two buttons beside it (like a spinbox). There is no way for the side buttons to grow to be the same height as the center element without being able to set their height percent.

max.x = std::max(CHILD_SIZE.x, max.x);
max.y += CHILD_SIZE.y + m_impl->data.gap;
}

if (!impl->children.empty())
Expand All @@ -88,9 +97,12 @@ std::optional<Hyprutils::Math::Vector2D> CColumnLayoutElement::preferredSize(con
}

std::optional<Hyprutils::Math::Vector2D> 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;
Expand Down
32 changes: 22 additions & 10 deletions src/element/rowLayout/RowLayout.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,19 @@

#include "../Element.hpp"
#include "../LinearLayout.hpp"
#include "../text/Text.hpp"

using namespace Hyprtoolkit;

static Vector2D childSizeForConstraint(const SP<IElement>& child, const Vector2D& constraint, const Vector2D& textConstraint) {
const auto CHILD_CONSTRAINT = dynamicPointerCast<CTextElement>(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> CRowLayoutElement::create(const SRowLayoutData& data) {
auto p = SP<CRowLayoutElement>(new CRowLayoutElement(data));
p->impl->self = p;
Expand Down Expand Up @@ -39,11 +49,8 @@ void CRowLayoutElement::reposition(const Hyprutils::Math::CBox& sbox, const Hypr
}

Hyprutils::Math::Vector2D CRowLayoutElement::childSize(Hyprutils::Memory::CSharedPointer<IElement> 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() {
Expand All @@ -56,10 +63,12 @@ std::optional<Hyprutils::Math::Vector2D> 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())
Expand All @@ -80,9 +89,12 @@ std::optional<Hyprutils::Math::Vector2D> CRowLayoutElement::preferredSize(const
}

std::optional<Hyprutils::Math::Vector2D> 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;
Expand Down
20 changes: 12 additions & 8 deletions src/element/spinbox/SpinboxSpinner.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,15 +20,17 @@ SP<CSpinboxSpinner> CSpinboxSpinner::create(SP<CSpinboxElement> 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<CSpinboxElement> 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);
Expand All @@ -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; })
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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() {
Expand All @@ -127,11 +131,11 @@ std::optional<Vector2D> CSpinboxSpinner::preferredSize(const Hyprutils::Math::Ve
}

std::optional<Vector2D> 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<Vector2D> CSpinboxSpinner::maximumSize(const Hyprutils::Math::Vector2D& parent) {
return m_layout->preferredSize(parent);
return preferredSize(parent);
}

bool CSpinboxSpinner::acceptsMouseInput() {
Expand Down
Loading
Loading