Expand width of grid elements to fit longer names

This commit is contained in:
Yousef Khadadeh 2026-04-28 20:55:30 +03:00
parent 4c30939882
commit cd45d1e8cd
3 changed files with 18 additions and 4 deletions

View file

@ -24,9 +24,21 @@ void GridScrollArea::layout(const Clay_ElementId& id, const Options& o) {
size_t entriesPerRow = std::max<size_t>(rowWidth / options.entryWidth, 1);
size_t rowCount = (options.entryCount + entriesPerRow - 1) / entriesPerRow;
opts.entryCount = rowCount;
uint16_t xPadding = 0;
if(rowCount > 1 || (rowCount == 1 && ((options.entryCount + entriesPerRow) / entriesPerRow) > 1)) // More than 1 row or 1st row is full
xPadding = static_cast<float>(std::max<float>(rowWidth - (entriesPerRow * options.entryWidth), 0) / (entriesPerRow + 1));
uint16_t xChildGap = 0;
float entryWidth = options.entryWidth;
if(rowCount > 1 || (rowCount == 1 && ((options.entryCount + entriesPerRow) / entriesPerRow) > 1)) { // More than 1 row or 1st row is full
float padding = std::max<float>(rowWidth - (entriesPerRow * options.entryWidth), 0) / (entriesPerRow + 1);
xChildGap = xPadding = padding;
if(o.entryWidthIsMinimum) {
xPadding = padding / 2;
entryWidth = padding + options.entryWidth;
xChildGap = 0;
}
else
xPadding = xChildGap = padding;
}
opts.elementContent = [&] (size_t rowIndex) {
gui.new_id("grid row", [&] {
@ -34,7 +46,7 @@ void GridScrollArea::layout(const Clay_ElementId& id, const Options& o) {
.layout = {
.sizing = {.width = CLAY_SIZING_GROW(0), .height = CLAY_SIZING_GROW(0)},
.padding = {.left = xPadding, .right = xPadding},
.childGap = xPadding,
.childGap = xChildGap,
.childAlignment = { .x = options.childAlignmentX}
}
}) {
@ -44,7 +56,7 @@ void GridScrollArea::layout(const Clay_ElementId& id, const Options& o) {
gui.new_id(static_cast<int64_t>(i - startIndex), [&] {
CLAY_AUTO_ID({
.layout = {
.sizing = {.width = CLAY_SIZING_FIXED(options.entryWidth), .height = CLAY_SIZING_FIXED(options.entryHeight)},
.sizing = {.width = CLAY_SIZING_FIXED(entryWidth), .height = CLAY_SIZING_FIXED(options.entryHeight)},
}
}) {
options.elementContent(i);

View file

@ -14,6 +14,7 @@ class GridScrollArea : public Element {
Clay_LayoutAlignmentX childAlignmentX = CLAY_ALIGN_X_LEFT;
float entryHeight;
size_t entryCount;
bool entryWidthIsMinimum = false;
std::function<void(size_t elementIndex)> elementContent;
std::function<void(const ScrollArea::InnerContentParameters&)> innerContentExtraCallback;
};

View file

@ -692,6 +692,7 @@ void FileSelectScreen::file_view() {
.childAlignmentX = CLAY_ALIGN_X_LEFT,
.entryHeight = entrySize.y(),
.entryCount = fileList.size(),
.entryWidthIsMinimum = true,
.elementContent = [&](size_t i) {
fileButton(i, false, iconSize);
}