From 19cef97bcd0906ad29c3e28ac11ff3f785c994cc 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 | 24 ++++++++++++++++++------ 1 file changed, 18 insertions(+), 6 deletions(-) diff --git a/data/core/init.lua b/data/core/init.lua index 3f0cc096..6aead929 100644 --- a/data/core/init.lua +++ b/data/core/init.lua @@ -1378,29 +1378,40 @@ 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) 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 - if not next_step or system.get_time() >= next_step then - did_redraw = core.step() + 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 + did_redraw = true + 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 @@ -1417,6 +1428,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)