compress duplicated logic into a function (#384)

Reviewed-on: https://codeberg.org/nsxiv/nsxiv/pulls/384
Reviewed-by: TAAPArthur <taaparthur@noreply.codeberg.org>
This commit is contained in:
NRK 2022-11-05 00:01:56 +01:00
parent 045abbd117
commit 3804b50656

View file

@ -31,6 +31,20 @@ extern img_t img;
extern tns_t tns; extern tns_t tns;
extern win_t win; extern win_t win;
static bool navigate_to(arg_t n)
{
if (n >= 0 && n < filecnt && n != fileidx) {
if (mode == MODE_IMAGE) {
load_image(n);
} else if (mode == MODE_THUMB) {
fileidx = n;
tns.dirty = true;
}
return true;
}
return false;
}
bool cg_quit(arg_t status) bool cg_quit(arg_t status)
{ {
unsigned int i; unsigned int i;
@ -126,32 +140,13 @@ bool cg_remove_image(arg_t _)
bool cg_first(arg_t _) bool cg_first(arg_t _)
{ {
if (mode == MODE_IMAGE && fileidx != 0) { return navigate_to(0);
load_image(0);
return true;
} else if (mode == MODE_THUMB && fileidx != 0) {
fileidx = 0;
tns.dirty = true;
return true;
} else {
return false;
}
} }
bool cg_n_or_last(arg_t _) bool cg_n_or_last(arg_t _)
{ {
int n = prefix != 0 && prefix - 1 < filecnt ? prefix - 1 : filecnt - 1; int n = prefix != 0 && prefix - 1 < filecnt ? prefix - 1 : filecnt - 1;
return navigate_to(n);
if (mode == MODE_IMAGE && fileidx != n) {
load_image(n);
return true;
} else if (mode == MODE_THUMB && fileidx != n) {
fileidx = n;
tns.dirty = true;
return true;
} else {
return false;
}
} }
bool cg_scroll_screen(arg_t dir) bool cg_scroll_screen(arg_t dir)
@ -224,17 +219,7 @@ bool cg_navigate_marked(arg_t n)
new = i; new = i;
} }
} }
if (new != fileidx) { return navigate_to(new);
if (mode == MODE_IMAGE) {
load_image(new);
} else {
fileidx = new;
tns.dirty = true;
}
return true;
} else {
return false;
}
} }
bool cg_change_gamma(arg_t d) bool cg_change_gamma(arg_t d)