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.
This commit is contained in:
Guldoman 2023-11-29 16:33:34 +01:00 committed by George Sokianos
parent 4e2f70e5ee
commit 2571e17d1b
1 changed files with 11 additions and 4 deletions

View File

@ -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)