gears.surface: Cache files from disk
Instead of loading files from disk every time we need them, add a cache to gears.surface as a weak table that maps strings to cairo surfaces. If this cache should be avoided, there is a new gears.surface.load_uncached() function which works just like gears.surface.load() worked before. Signed-off-by: Uli Schlachter <psychon@znc.in>
This commit is contained in:
parent
2321b0b6c5
commit
2d8c21af85
|
@ -17,14 +17,17 @@ end
|
||||||
|
|
||||||
-- gears.surface
|
-- gears.surface
|
||||||
local surface = { mt = {} }
|
local surface = { mt = {} }
|
||||||
|
local surface_cache = setmetatable({}, { __mode = 'v' })
|
||||||
|
|
||||||
--- Try to convert the argument into an lgi cairo surface.
|
--- Try to convert the argument into an lgi cairo surface.
|
||||||
-- This is usually needed for loading images by file name.
|
-- This is usually needed for loading images by file name.
|
||||||
function surface.load(_surface)
|
function surface.load_uncached(_surface)
|
||||||
-- Nil is not changed
|
-- Nil is not changed
|
||||||
if not _surface then
|
if not _surface then
|
||||||
return nil
|
return nil
|
||||||
end
|
end
|
||||||
|
-- Remove from cache if it was cached
|
||||||
|
surface_cache[_surface] = nil
|
||||||
-- lgi cairo surfaces don't get changed either
|
-- lgi cairo surfaces don't get changed either
|
||||||
if cairo.Surface:is_type_of(_surface) then
|
if cairo.Surface:is_type_of(_surface) then
|
||||||
return _surface
|
return _surface
|
||||||
|
@ -37,6 +40,18 @@ function surface.load(_surface)
|
||||||
return cairo.Surface(_surface, true)
|
return cairo.Surface(_surface, true)
|
||||||
end
|
end
|
||||||
|
|
||||||
|
function surface.load(_surface)
|
||||||
|
if type(_surface) == "string" then
|
||||||
|
local cache = surface_cache[_surface]
|
||||||
|
if not cache then
|
||||||
|
cache = surface.load_uncached(_surface)
|
||||||
|
surface_cache[_surface] = cache
|
||||||
|
end
|
||||||
|
return cache
|
||||||
|
end
|
||||||
|
return surface.load_uncached(_surface)
|
||||||
|
end
|
||||||
|
|
||||||
function surface.mt:__call(...)
|
function surface.mt:__call(...)
|
||||||
return surface.load(...)
|
return surface.load(...)
|
||||||
end
|
end
|
||||||
|
|
Loading…
Reference in New Issue