Compare commits

...

4 Commits

Author SHA1 Message Date
Francesco Abbate 80e25dbfd3 Use drawing_cache without disable mechanism 2021-09-05 18:44:55 +02:00
Guldoman 4cb8d4dce2
Enable cache only during `update`, `draw` and `on_mouse_move`
Also moved cache to `DocView`
2021-09-05 03:18:00 +02:00
Guldoman e2c107b6f7
Revert "Merge pull request #481 from Guldoman/h_scroll_size_cache"
This reverts commit a0508103b1, reversing
changes made to f18629ab64.
2021-09-05 02:50:37 +02:00
Guldoman 73affe215f
Update scrollable sizes once per `View:update`
This reduces the number of `get_[h_]scrollable_size` calls by around
three times.
2021-09-04 18:33:24 +02:00
4 changed files with 44 additions and 15 deletions

View File

@ -56,6 +56,7 @@ function DocView:new(doc)
self.doc = assert(doc)
self.font = "code_font"
self.last_x_offset = {}
self.drawing_cache = {}
end
@ -97,37 +98,51 @@ function DocView:get_filename()
end
function DocView:get_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()
local doc_change_id = self.doc:get_change_id()
if self.last_doc_change_id ~= doc_change_id then
self.last_doc_change_id = doc_change_id
local xmargin = 3 * self:get_font():get_width(' ') -- from DocView:scroll_to_make_visible
-- TODO: make Doc calculate the real longest line in pixels, not in characters,
-- as the current implementation only works for monospace fonts
local long_line = next(self.doc.long_lines.line_numbers) or 1
self.h_scrollable_size = self:get_col_x_offset(long_line, self.doc.long_lines.length)
+ self:get_gutter_width() + xmargin
end
return self.h_scrollable_size
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
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
@ -321,6 +336,7 @@ end
function DocView:update()
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
@ -461,4 +477,13 @@ function DocView:draw()
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
return DocView

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

@ -21,6 +21,7 @@ function View:new()
self.scrollable = false
end
function View:move_towards(t, k, dest, rate)
if type(t) ~= "table" then
return self:move_towards(self, t, k, dest, rate)

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)