From 65acb98396b7eb8e7b22140061c799dbd0de8a3e Mon Sep 17 00:00:00 2001 From: NRK Date: Wed, 5 Jun 2024 19:37:46 +0000 Subject: [PATCH] fix: unresponsive UI when animation is too fast (#489) the previous check_timeouts() logic tried to greedily handle as many timeouts as possible until there are no more active timeouts left. this caused a number of issues such as: https://codeberg.org/nsxiv/nsxiv/issues/439 https://codeberg.org/nsxiv/nsxiv-record/issues/281 the new logic relaxes this and only does a single iteration. any remaining active timeout will be picked up by the next iteration instead. Fixes: https://codeberg.org/nsxiv/nsxiv/issues/439 --- main.c | 24 +++++++++++++++--------- 1 file changed, 15 insertions(+), 9 deletions(-) diff --git a/main.c b/main.c index 0e1e15c..5cd8b09 100644 --- a/main.c +++ b/main.c @@ -249,10 +249,10 @@ void reset_timeout(timeout_f handler) static bool check_timeouts(int *t) { - int i = 0, tdiff, tmin = -1; + int i = 0, tdiff, tmin; struct timeval now; - while (i < (int)ARRLEN(timeouts)) { + for (i = 0; i < (int)ARRLEN(timeouts); ++i) { if (timeouts[i].active) { gettimeofday(&now, 0); tdiff = TV_DIFF(&timeouts[i].when, &now); @@ -260,16 +260,22 @@ static bool check_timeouts(int *t) timeouts[i].active = false; if (timeouts[i].handler != NULL) timeouts[i].handler(); - i = tmin = -1; - } else if (tmin < 0 || tdiff < tmin) { - tmin = tdiff; } } - i++; } - if (tmin > 0 && t != NULL) - *t = tmin; - return tmin > 0; + + tmin = INT_MAX; + gettimeofday(&now, 0); + for (i = 0; i < (int)ARRLEN(timeouts); ++i) { + if (timeouts[i].active) { + tdiff = TV_DIFF(&timeouts[i].when, &now); + tmin = MIN(tmin, tdiff); + } + } + + if (tmin != INT_MAX && t != NULL) + *t = MAX(tmin, 0); + return tmin != INT_MAX; } static void autoreload(void)