Skip to content

Commit

Permalink
centralize script handling logic (#477)
Browse files Browse the repository at this point in the history
currently the logic of when to open/close script is scattered around the
entire code-base which is both ugly and error-prone.

this patch centralizes script handling by remembering the relevant
information on each redraw and then comparing it with the previous
information to figure out whether something changed or not.

this also fixes a bug where scripts weren't being called in thumbnail
mode when mouse was used for selecting a different image.

Closes: https://codeberg.org/nsxiv/nsxiv/issues/475

Reviewed-on: https://codeberg.org/nsxiv/nsxiv/pulls/477
Reviewed-by: eylles <eylles@noreply.codeberg.org>
  • Loading branch information
N-R-K committed Sep 30, 2023
1 parent 80a7131 commit 3659361
Show file tree
Hide file tree
Showing 5 changed files with 28 additions and 23 deletions.
10 changes: 1 addition & 9 deletions commands.c
Original file line number Diff line number Diff line change
Expand Up @@ -83,9 +83,6 @@ bool cg_switch_mode(arg_t _)
load_image(fileidx);
mode = MODE_IMAGE;
}
close_info();
open_info();
title_dirty = true;
return true;
}

Expand Down Expand Up @@ -415,12 +412,7 @@ bool ci_slideshow(arg_t _)

bool ct_move_sel(arg_t dir)
{
bool dirty = tns_move_selection(&tns, dir, prefix);
if (dirty) {
close_info();
open_info();
}
return dirty;
return tns_move_selection(&tns, dir, prefix);
}

bool ct_reload_all(arg_t _)
Expand Down
4 changes: 2 additions & 2 deletions image.c
Original file line number Diff line number Diff line change
Expand Up @@ -717,7 +717,7 @@ static bool img_fit(img_t *img)

if (ABS(img->zoom - z) > 1.0 / MAX(img->w, img->h)) {
img->zoom = z;
img->dirty = title_dirty = true;
img->dirty = true;
return true;
} else {
return false;
Expand Down Expand Up @@ -852,7 +852,7 @@ bool img_zoom_to(img_t *img, float z)
img->y = y - (y - img->y) * z / img->zoom;
img->zoom = z;
img->scalemode = SCALE_ZOOM;
img->dirty = img->checkpan = title_dirty = true;
img->dirty = img->checkpan = true;
return true;
} else {
return false;
Expand Down
34 changes: 25 additions & 9 deletions main.c
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,6 @@ int alternate;
int markcnt;
int markidx;
int prefix;
bool title_dirty;
const XButtonEvent *xbutton_ev;

static void autoreload(void);
Expand Down Expand Up @@ -316,7 +315,7 @@ static void open_title(void)
char *argv[8];
char w[12] = "", h[12] = "", z[12] = "", fidx[12], fcnt[12];

if (wintitle.f.err || !title_dirty)
if (wintitle.f.err)
return;

close_title();
Expand All @@ -331,7 +330,6 @@ static void open_title(void)
fidx, fcnt, w, h, z, NULL);
if ((wintitle.pid = spawn(&wintitle.fd, NULL, argv)) > 0)
fcntl(wintitle.fd, F_SETFL, O_NONBLOCK);
title_dirty = false;
}

void close_info(void)
Expand Down Expand Up @@ -402,10 +400,7 @@ void load_image(int new)
files[new].flags &= ~FF_WARN;
fileidx = current = new;

close_info();
open_info();
arl_add(&arl, files[fileidx].path);
title_dirty = true;

if (img.multi.cnt > 0 && img.multi.animate)
set_timeout(animate, img.multi.frames[img.multi.sel].delay, true);
Expand Down Expand Up @@ -443,9 +438,33 @@ static void update_info(void)
const char *mark;
win_bar_t *l = &win.bar.l, *r = &win.bar.r;

static struct {
const char *filepath;
int fileidx;
float zoom;
appmode_t mode;
} prev;

if (prev.fileidx != fileidx || prev.mode != mode ||
(prev.filepath == NULL || !STREQ(prev.filepath, files[fileidx].path)))
{
close_info();
open_info();
open_title();
} else if (mode == MODE_IMAGE && prev.zoom != img.zoom) {
open_title();
}

/* update bar contents */
if (win.bar.h == 0 || extprefix)
return;

free((char *)prev.filepath);
prev.filepath = estrdup(files[fileidx].path);
prev.fileidx = fileidx;
prev.zoom = img.zoom;
prev.mode = mode;

for (fw = 0, i = filecnt; i > 0; fw++, i /= 10)
;
mark = files[fileidx].flags & FF_MARK ? "* " : "";
Expand Down Expand Up @@ -520,7 +539,6 @@ void redraw(void)
tns_render(&tns);
}
update_info();
open_title();
win_draw(&win);
reset_timeout(redraw);
reset_cursor();
Expand Down Expand Up @@ -669,8 +687,6 @@ static bool run_key_handler(const char *key, unsigned int mask)
if (mode == MODE_IMAGE && changed) {
img_close(&img, true);
load_image(fileidx);
} else {
open_info();
}
free(oldst);
reset_cursor();
Expand Down
1 change: 0 additions & 1 deletion nsxiv.h
Original file line number Diff line number Diff line change
Expand Up @@ -468,6 +468,5 @@ extern int alternate;
extern int markcnt;
extern int markidx;
extern int prefix;
extern bool title_dirty;

#endif /* NSXIV_H */
2 changes: 0 additions & 2 deletions thumbs.c
Original file line number Diff line number Diff line change
Expand Up @@ -447,7 +447,6 @@ void tns_render(tns_t *tns)
}
tns->dirty = false;
tns_highlight(tns, *tns->sel, true);
title_dirty = true;
}

void tns_mark(tns_t *tns, int n, bool mark)
Expand Down Expand Up @@ -516,7 +515,6 @@ bool tns_move_selection(tns_t *tns, direction_t dir, int cnt)
tns_check_view(tns, false);
if (!tns->dirty)
tns_highlight(tns, *tns->sel, true);
title_dirty = true;
}
return *tns->sel != old;
}
Expand Down

0 comments on commit 3659361

Please sign in to comment.