From 2571e17d1bff358a207d7863218540d1dc03817c Mon Sep 17 00:00:00 2001 From: Guldoman Date: Wed, 29 Nov 2023 16:33:34 +0100 Subject: [PATCH] Fix `core.redraw` when window is not focused (#1601) * Execute at least one step when window has no focus This way if `core.redraw` is set, it's respected. * Fully run threads at least once when window has no focus This allows threads that set `core.redraw` (like `projectsearch`) to continue running even after the window loses focus. "Fully" here means that `run_threads` has gone through *all* the "timed out" coroutines at least once. --- data/core/init.lua | 15 +++++++++++---- 1 file changed, 11 insertions(+), 4 deletions(-) diff --git a/data/core/init.lua b/data/core/init.lua index aee11529..58411e3f 100644 --- a/data/core/init.lua +++ b/data/core/init.lua @@ -1423,11 +1423,11 @@ local run_threads = coroutine.wrap(function() -- stop running threads if we're about to hit the end of frame if system.get_time() - core.frame_start > max_time then - coroutine.yield(0) + coroutine.yield(0, false) end end - coroutine.yield(minimal_time_to_wake) + coroutine.yield(minimal_time_to_wake, true) end end) @@ -1435,10 +1435,15 @@ end) function core.run() local next_step local last_frame_time + local run_threads_full = 0 while true do core.frame_start = system.get_time() - local time_to_wake = run_threads() + local time_to_wake, threads_done = run_threads() + if threads_done then + run_threads_full = run_threads_full + 1 + end local did_redraw = false + local did_step = false local force_draw = core.redraw and last_frame_time and core.frame_start - last_frame_time > (1 / config.fps) if force_draw or not next_step or system.get_time() >= next_step then if core.step() then @@ -1446,11 +1451,12 @@ function core.run() last_frame_time = core.frame_start end next_step = nil + did_step = true end if core.restart_request or core.quit_request then break end if not did_redraw then - if system.window_has_focus() then + if system.window_has_focus() or not did_step or run_threads_full < 2 then local now = system.get_time() if not next_step then -- compute the time until the next blink local t = now - core.blink_start @@ -1467,6 +1473,7 @@ function core.run() next_step = nil -- perform a step when we're not in focus if get we an event end else -- if we redrew, then make sure we only draw at most FPS/sec + run_threads_full = 0 local now = system.get_time() local elapsed = now - core.frame_start local next_frame = math.max(0, 1 / config.fps - elapsed)