More aggressive wait_event timeout when window has focus

Adopt a time based logic for cursor blinking and wait for event

when idle with a timeout equal to blink remaining time.
This commit is contained in:
Francesco Abbate 2021-04-17 14:57:04 -07:00
parent 394f98041d
commit 032018ec48
2 changed files with 20 additions and 11 deletions

View File

@ -55,7 +55,6 @@ function DocView:new(doc)
self.doc = assert(doc)
self.font = "code_font"
self.last_x_offset = {}
self.blink_timer = 0
end
@ -244,7 +243,7 @@ function DocView:on_mouse_pressed(button, x, y, clicks)
self.doc:set_selection(mouse_selection(self.doc, clicks, line, col, line, col))
self.mouse_selecting = { line, col, clicks = clicks }
end
self.blink_timer = 0
core.blink_reset()
end
@ -284,18 +283,18 @@ function DocView:update()
if core.active_view == self then
self:scroll_to_make_visible(line, col)
end
self.blink_timer = 0
core.blink_reset()
self.last_line, self.last_col = line, col
end
-- update blink timer
if self == core.active_view and not self.mouse_selecting then
local n = config.blink_period / 2
local prev = self.blink_timer
self.blink_timer = (self.blink_timer + 1 / config.fps) % config.blink_period
if (self.blink_timer > n) ~= (prev > n) then
local T, t0 = config.blink_period, core.blink_start
local ta, tb = core.blink_timer, system.get_time()
if ((tb - t0) % T < T / 2) ~= ((ta - t0) % T < T / 2) then
core.redraw = true
end
core.blink_timer = tb
end
DocView.super.update(self)
@ -348,8 +347,9 @@ function DocView:draw_line_body(idx, x, y)
self:draw_line_text(idx, x, y)
-- draw caret if it overlaps this line
local T = config.blink_period
if line == idx and core.active_view == self
and self.blink_timer < config.blink_period / 2
and (core.blink_timer - core.blink_start) % T < T / 2
and system.window_has_focus() then
local lh = self:get_line_height()
local x1 = x + self:get_col_x_offset(line, col)

View File

@ -403,6 +403,8 @@ function core.init()
core.log_items = {}
core.docs = {}
core.threads = setmetatable({}, { __mode = "k" })
core.blink_start = system.get_time()
core.blink_timer = core.blink_start
local project_dir_abs = system.absolute_path(project_dir)
local set_project_ok = project_dir_abs and core.set_project_dir(project_dir_abs)
@ -865,7 +867,6 @@ end)
function core.run()
local idle_iterations = 0
local frame_duration = 1 / config.fps
while true do
core.frame_start = system.get_time()
local did_redraw = core.step()
@ -878,7 +879,10 @@ function core.run()
if idle_iterations > 1 then
if system.window_has_focus() then
-- keep running even with no events to make the cursor blinks
system.wait_event(frame_duration)
local t = system.get_time() - core.blink_start
local h = config.blink_period / 2
local dt = math.ceil(t / h) * h - t
system.wait_event(dt + 1 / config.fps)
else
system.wait_event()
end
@ -886,12 +890,17 @@ function core.run()
else
idle_iterations = 0
local elapsed = system.get_time() - core.frame_start
system.sleep(math.max(0, frame_duration - elapsed))
system.sleep(math.max(0, 1 / config.fps - elapsed))
end
end
end
function core.blink_reset()
core.blink_start = system.get_time()
end
function core.on_error(err)
-- write error to file
local fp = io.open(USERDIR .. "/error.txt", "wb")