Use drawing_cache without disable mechanism

This commit is contained in:
Francesco Abbate 2021-09-05 18:44:55 +02:00
parent 4cb8d4dce2
commit 80e25dbfd3
3 changed files with 42 additions and 27 deletions

View File

@ -56,7 +56,7 @@ function DocView:new(doc)
self.doc = assert(doc)
self.font = "code_font"
self.last_x_offset = {}
self.step_cache = {}
self.drawing_cache = {}
end
@ -98,38 +98,51 @@ function DocView:get_filename()
end
function DocView:get_scrollable_size()
if self.step_cache.enabled and self.step_cache.scrollable_size ~= nil then
return self.step_cache.scrollable_size
end
self.step_cache.scrollable_size = self:get_line_height() * (#self.doc.lines - 1) + self.size.y
return self.step_cache.scrollable_size
function DocView:compute_scrollable_size()
return self:get_line_height() * (#self.doc.lines - 1) + self.size.y
end
function DocView:get_h_scrollable_size()
if self.step_cache.enabled and self.step_cache.h_scrollable_size ~= nil then
return self.step_cache.h_scrollable_size
end
function DocView:get_scrollable_size()
return self.drawing_cache.scrollable_size
end
function DocView:compute_h_scrollable_size()
local xmargin = 3 * self:get_font():get_width(' ') -- from DocView:scroll_to_make_visible
local long_line = next(self.doc.long_lines.line_numbers) or 1
self.step_cache.h_scrollable_size = self:get_col_x_offset(long_line, self.doc.long_lines.length)
+ self:get_gutter_width() + xmargin
return self.step_cache.h_scrollable_size
return self:get_col_x_offset(long_line, self.doc.long_lines.length) + self:get_gutter_width() + xmargin
end
function DocView:get_h_scrollable_size()
return self.drawing_cache.h_scrollable_size
end
function DocView:get_font()
return style[self.font]
end
function DocView:get_line_height()
function DocView:compute_line_height()
return math.floor(self:get_font():get_height() * config.line_height)
end
function DocView:get_line_height()
return self.drawing_cache.line_height
end
function DocView:compute_gutter_width()
return self:get_font():get_width(#self.doc.lines)
end
function DocView:get_gutter_width()
local padding = style.padding.x * 2
return self:get_font():get_width(#self.doc.lines) + padding, padding
return self.drawing_cache.gutter_width + padding, padding
end
@ -285,7 +298,6 @@ end
function DocView:on_mouse_moved(x, y, ...)
self.step_cache.enabled = true
DocView.super.on_mouse_moved(self, x, y, ...)
if self:scrollbar_overlaps_point(x, y) or self.dragging_scrollbar
@ -309,7 +321,6 @@ function DocView:on_mouse_moved(x, y, ...)
self.doc:set_selection(mouse_selection(self.doc, clicks, l1, c1, l2, c2))
end
end
self.step_cache.enabled = false
end
@ -323,14 +334,9 @@ function DocView:on_text_input(text)
self.doc:text_input(text)
end
function DocView:invalidate_step_cache()
self.step_cache.scrollable_size = nil
self.step_cache.h_scrollable_size = nil
end
function DocView:update()
self:invalidate_step_cache()
self.step_cache.enabled = true
self:update_drawing_cache()
-- scroll to make caret visible and reset blink timer if it moved
local line, col = self.doc:get_selection()
if (line ~= self.last_line or col ~= self.last_col) and self.size.x > 0 then
@ -352,7 +358,6 @@ function DocView:update()
end
DocView.super.update(self)
self.step_cache.enabled = false
end
@ -443,7 +448,6 @@ function DocView:draw_overlay()
end
function DocView:draw()
self.step_cache.enabled = true
self:draw_background(style.background)
self:get_font():set_tab_size(config.indent_size)
@ -470,7 +474,15 @@ function DocView:draw()
self:draw_scrollbar()
self:draw_h_scrollbar()
self.step_cache.enabled = false
end
function DocView:update_drawing_cache()
local cache = self.drawing_cache
cache.gutter_width = self:compute_gutter_width()
cache.line_height = self:compute_line_height()
cache.scrollable_size = self:compute_scrollable_size()
cache.h_scrollable_size = self:compute_h_scrollable_size()
end

View File

@ -754,6 +754,7 @@ function RootView:open_doc(doc)
local view = DocView(doc)
node:add_view(view)
self.root_node:update_layout()
view:update_drawing_cache()
view:scroll_to_line(view.doc:get_selection(), true, true)
return view
end

View File

@ -56,6 +56,8 @@ local function set_scale(scale)
renderer.font.set_size(style.code_font, s * style.code_font:get_size())
end
view:update_drawing_cache()
-- restore scroll positions
for view, n in pairs(scrolls) do
view.scroll.y = n * (view:get_scrollable_size() - view.size.y)