partial fix of scroll_view

This commit is contained in:
2026-02-11 15:16:52 +05:00
parent ee6375f553
commit 5ef7f91764
4 changed files with 66 additions and 59 deletions

View File

@@ -350,7 +350,8 @@ TimPanelItem* tim_panel(TimPanel* self, bool is_selected, i32 x, i32 y, i32 w, i
void TimPanel_selectNext(TimPanel* self); void TimPanel_selectNext(TimPanel* self);
void TimPanel_selectPrev(TimPanel* self); void TimPanel_selectPrev(TimPanel* self);
/// /// Scrollable scope.
/// WARNING: draw it before anything else or it will overlap
void tim_scroll_view(TimScrollView* self, i32 x, i32 y, i32 w, i32 h, TimStyle style); void tim_scroll_view(TimScrollView* self, i32 x, i32 y, i32 w, i32 h, TimStyle style);
#pragma endregion #pragma endregion

View File

@@ -10,7 +10,7 @@ void TimPanel_selectPrev(TimPanel* l){
l->cur--; l->cur--;
} }
TimPanelItem* tim_panel(TimPanel* self, bool is_panel_selected, i32 x, i32 y, i32 w, i32 h){ TimPanelItem* tim_panel(TimPanel* self, bool is_panel_selected, i32 x1, i32 y1, i32 w1, i32 h1){
// select item with keyboard // select item with keyboard
if(tim_is_key_press(self->is_horizontal ? TimKey_Left : TimKey_Up)) if(tim_is_key_press(self->is_horizontal ? TimKey_Left : TimKey_Up))
{ {
@@ -25,38 +25,44 @@ TimPanelItem* tim_panel(TimPanel* self, bool is_panel_selected, i32 x, i32 y, i3
if(self->cur < self->len) if(self->cur < self->len)
tim->focus = self->items[self->cur].focus_target; tim->focus = self->items[self->cur].focus_target;
tim_scope(x, y, w, h)
{
TimRect content_scope = tim->scopes[tim->scope];
// TODO: draw current item and as much previous items as possible in scope // TODO: draw current item and as much previous items as possible in scope
TimRect item_place = { 0 }; TimRect absolute = tim_scope_rect_to_absolute(x1, y1, w1, h1);
TimRect scope = tim->scopes[tim->scope];
TimRect item_place = {
.x = absolute.x - scope.x,
.y = absolute.y - scope.y,
.w = 0,
.h = 0
};
for(i32 i = 0; i < self->len; i++){ for(i32 i = 0; i < self->len; i++){
TimPanelItem* item = &self->items[i]; TimPanelItem* item = &self->items[i];
// calculate item width
item_place.w = item->w; item_place.w = item->w;
if(item_place.w == A){ if(item_place.w == A){
if(self->is_horizontal){ if(self->is_horizontal){
item_place.w = content_scope.w / self->len ; item_place.w = absolute.w / self->len ;
// add remaining width to the last item // add remaining width to the last item
if(i == self->len - 1) if(i == self->len - 1)
item_place.w += content_scope.w % self->len; item_place.w += absolute.w % self->len;
else item_place.w -= self->spacing; else item_place.w -= self->spacing;
} }
else { else {
item_place.w = content_scope.w; item_place.w = absolute.w;
} }
} }
// calculate item height
item_place.h = item->h; item_place.h = item->h;
if(item_place.h == A){ if(item_place.h == A){
if(self->is_horizontal){ if(self->is_horizontal){
item_place.h = content_scope.h; item_place.h = absolute.h;
} }
else { else {
item_place.h = content_scope.h / self->len - self->spacing; item_place.h = absolute.h / self->len - self->spacing;
// add remaining height to the last item // add remaining height to the last item
if(i == self->len - 1) if(i == self->len - 1)
item_place.h += content_scope.h % self->len; item_place.h += absolute.h % self->len;
else item_place.h -= self->spacing; else item_place.h -= self->spacing;
} }
} }
@@ -80,7 +86,6 @@ TimPanelItem* tim_panel(TimPanel* self, bool is_panel_selected, i32 x, i32 y, i3
item_place.y += item_place.h + self->spacing; item_place.y += item_place.h + self->spacing;
} }
} }
}
return &self->items[self->cur]; return &self->items[self->cur];
} }

View File

@@ -17,17 +17,18 @@ TimRect tim_scope_rect_to_absolute(i32 x, i32 y, i32 w, i32 h) {
if (x == A) { // if (x == A) { //
x = p.x + (p.w - w) / 2; // center x on parent x = p.x + (p.w - w) / 2; // center x on parent
} else { // } else { //
if (x < 0) { // // if (x < 0) { //
x += p.w - w + 1; // anchor x to right // x += p.w - w + 1; // anchor x to right
} // // } //
if(x >= 0)
x += p.x; // anchor x to left x += p.x; // anchor x to left
} // } //
if (y == A) { // if (y == A) { //
y = p.y + (p.h - h) / 2; // center y on parent y = p.y + (p.h - h) / 2; // center y on parent
} else { // } else { //
if (y < 0) { // // if (y < 0) { //
y += p.h - h + 1; // anchor y to bottom // y += p.h - h + 1; // anchor y to bottom
} // // } //
y += p.y; // anchor y to top y += p.y; // anchor y to top
} }

View File

@@ -75,7 +75,7 @@ void tim_scroll_view(TimScrollView* self, i32 x, i32 y, i32 w, i32 h, TimStyle s
tim->scopes[tim->scope] = content_scope; tim->scopes[tim->scope] = content_scope;
// draw content // draw content
TimRect content_place = { .x = 0, .y = -self->offset - 1, .w = content_scope.w, .h = content_scope.h }; TimRect content_place = { .x = 0, .y = -self->offset, .w = content_scope.w, .h = content_scope.h };
self->draw(self->data, content_place); self->draw(self->data, content_place);
} }
} }