Improved file selector grid alignment

This commit is contained in:
Yousef Khadadeh 2026-04-21 18:38:24 +03:00
parent 3b9b9c0cf9
commit c75c668bf7
7 changed files with 45 additions and 14 deletions

View file

@ -19,15 +19,20 @@ void GridScrollArea::layout(const Clay_ElementId& id, const Options& o) {
rowWidth = std::fabs(contParams.containerDimensions.x());
if(extraCallback) extraCallback(contParams);
};
size_t entriesPerRow = static_cast<size_t>(rowWidth / options.entryMaximumWidth) + 1;
size_t entriesPerRow = std::max<size_t>(rowWidth / options.entryWidth, 1);
size_t rowCount = (options.entryCount + entriesPerRow - 1) / entriesPerRow;
float entryWidth = rowWidth == 0.0f ? 100.0f : (rowWidth / 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 * 2));
opts.elementContent = [&] (size_t rowIndex) {
gui.new_id("grid row", [&] {
CLAY_AUTO_ID({
.layout = {
.sizing = {.width = CLAY_SIZING_GROW(0), .height = CLAY_SIZING_FIXED(options.entryHeight)},
.sizing = {.width = CLAY_SIZING_GROW(0), .height = CLAY_SIZING_GROW(0)},
.padding = {.left = xPadding, .right = xPadding},
.childGap = xPadding,
.childAlignment = { .x = options.childAlignmentX}
}
}) {
@ -36,7 +41,9 @@ void GridScrollArea::layout(const Clay_ElementId& id, const Options& o) {
for(size_t i = startIndex; i < endIndex; i++) {
gui.new_id(static_cast<int64_t>(i - startIndex), [&] {
CLAY_AUTO_ID({
.layout = {.sizing = {.width = CLAY_SIZING_FIXED(entryWidth), .height = CLAY_SIZING_FIXED(options.entryHeight)}}
.layout = {
.sizing = {.width = CLAY_SIZING_FIXED(options.entryWidth), .height = CLAY_SIZING_FIXED(options.entryHeight)},
}
}) {
options.elementContent(i);
}
@ -46,7 +53,9 @@ void GridScrollArea::layout(const Clay_ElementId& id, const Options& o) {
});
};
CLAY(id, {
.layout = {.sizing = {.width = CLAY_SIZING_GROW(0), .height = CLAY_SIZING_GROW(0)}}
.layout = {
.sizing = {.width = CLAY_SIZING_GROW(0), .height = CLAY_SIZING_GROW(0)}
}
}) {
scroll_area_many_entries(gui, "scroll many area", opts);
}