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:
Uli Schlachter 2014-03-29 21:41:47 +01:00
parent 2321b0b6c5
commit 2d8c21af85
1 changed files with 16 additions and 1 deletions

View File

@ -17,14 +17,17 @@ end
-- gears.surface
local surface = { mt = {} }
local surface_cache = setmetatable({}, { __mode = 'v' })
--- Try to convert the argument into an lgi cairo surface.
-- This is usually needed for loading images by file name.
function surface.load(_surface)
function surface.load_uncached(_surface)
-- Nil is not changed
if not _surface then
return nil
end
-- Remove from cache if it was cached
surface_cache[_surface] = nil
-- lgi cairo surfaces don't get changed either
if cairo.Surface:is_type_of(_surface) then
return _surface
@ -37,6 +40,18 @@ function surface.load(_surface)
return cairo.Surface(_surface, true)
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(...)
return surface.load(...)
end