Skip to content

Commit

Permalink
gallery: Use generic list functions
Browse files Browse the repository at this point in the history
Signed-off-by: Artem Senichev <artemsen@gmail.com>
  • Loading branch information
artemsen committed Aug 19, 2024
1 parent 7b86ea4 commit a3b4e81
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 11 deletions.
12 changes: 6 additions & 6 deletions src/gallery.c
Original file line number Diff line number Diff line change
Expand Up @@ -113,13 +113,13 @@ static void reset_loader(void)

for (size_t i = 0; i < max(max_f, max_b); ++i) {
if (i < max_f) {
next_f = image_list_next_file(next_f);
next_f = image_list_nearest(next_f, true, false);
if (!get_thumbnail(next_f)) {
loader_queue_append(next_f);
}
}
if (i < max_b) {
next_b = image_list_prev_file(next_b);
next_b = image_list_nearest(next_b, false, false);
if (!get_thumbnail(next_b)) {
loader_queue_append(next_b);
}
Expand Down Expand Up @@ -254,13 +254,13 @@ static void select_nearest(enum action_type direction)
case action_prev_file:
case action_step_left:
if (index != image_list_first()) {
index = image_list_prev_file(index);
index = image_list_nearest(index, false, false);
}
break;
case action_next_file:
case action_step_right:
if (index != image_list_last()) {
index = image_list_next_file(index);
index = image_list_nearest(index, true, false);
}
break;
case action_step_up:
Expand Down Expand Up @@ -416,8 +416,8 @@ static void draw_thumbnails(struct pixmap* window)
}

// get next thumbnail index
index = image_list_next_file(index);
if (index == IMGLIST_INVALID || index <= ctx.top) {
index = image_list_nearest(index, true, false);
if (index == IMGLIST_INVALID) {
goto done;
}
}
Expand Down
17 changes: 13 additions & 4 deletions src/imagelist.c
Original file line number Diff line number Diff line change
Expand Up @@ -414,14 +414,23 @@ size_t image_list_jump(size_t start, size_t distance, bool forward)

size_t image_list_distance(size_t start, size_t end)
{
size_t index = start;
size_t distance = 0;
size_t index;

if (start == IMGLIST_INVALID || end == IMGLIST_INVALID || start > end) {
return IMGLIST_INVALID;
if (start == IMGLIST_INVALID) {
start = image_list_first();
}
if (end == IMGLIST_INVALID) {
end = image_list_last();
}
if (start <= end) {
index = start;
} else {
index = end;
end = start;
}

while (index != IMGLIST_INVALID && index < end) {
while (index != IMGLIST_INVALID && index != end) {
++distance;
index = image_list_nearest(index, true, false);
}
Expand Down
9 changes: 8 additions & 1 deletion test/imagelist_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -142,12 +142,19 @@ TEST_F(ImageList, Distance)
{
const char* sources[] = { "exec://cmd1", "exec://cmd2", "exec://cmd3" };
image_list_init(sources, 3);
ASSERT_EQ(image_list_distance(IMGLIST_INVALID, IMGLIST_INVALID),
static_cast<size_t>(2));
ASSERT_EQ(image_list_distance(0, IMGLIST_INVALID), static_cast<size_t>(2));
ASSERT_EQ(image_list_distance(IMGLIST_INVALID, 2), static_cast<size_t>(2));
ASSERT_EQ(image_list_distance(0, 0), static_cast<size_t>(0));
ASSERT_EQ(image_list_distance(0, 1), static_cast<size_t>(1));
ASSERT_EQ(image_list_distance(0, 2), static_cast<size_t>(2));
ASSERT_EQ(image_list_distance(2, 1), static_cast<size_t>(1));
ASSERT_EQ(image_list_distance(2, 0), static_cast<size_t>(2));
ASSERT_EQ(image_list_distance(2, 2), static_cast<size_t>(0));
image_list_skip(1);
ASSERT_EQ(image_list_distance(0, 2), static_cast<size_t>(1));
ASSERT_EQ(image_list_distance(2, 0), static_cast<size_t>(IMGLIST_INVALID));
ASSERT_EQ(image_list_distance(2, 0), static_cast<size_t>(1));
}

TEST_F(ImageList, Get)
Expand Down

0 comments on commit a3b4e81

Please sign in to comment.