Use gears.cache to replace some other caches
Signed-off-by: Uli Schlachter <psychon@znc.in>
This commit is contained in:
parent
42c913332f
commit
a239b2cac7
|
@ -17,7 +17,7 @@ local cairo = require("lgi").cairo
|
||||||
local surface = require("gears.surface")
|
local surface = require("gears.surface")
|
||||||
|
|
||||||
local color = { mt = {} }
|
local color = { mt = {} }
|
||||||
local pattern_cache = setmetatable({}, { __mode = 'v' })
|
local pattern_cache
|
||||||
|
|
||||||
--- Parse a HTML-color.
|
--- Parse a HTML-color.
|
||||||
-- This function can parse colors like `#rrggbb` and `#rrggbbaa`.
|
-- This function can parse colors like `#rrggbb` and `#rrggbbaa`.
|
||||||
|
@ -231,13 +231,7 @@ function color.create_pattern(col)
|
||||||
if cairo.Pattern:is_type_of(col) then
|
if cairo.Pattern:is_type_of(col) then
|
||||||
return col
|
return col
|
||||||
end
|
end
|
||||||
local col = col or "#000000"
|
return pattern_cache:get(col or "#000000")
|
||||||
local result = pattern_cache[col]
|
|
||||||
if not result then
|
|
||||||
result = color.create_pattern_uncached(col)
|
|
||||||
pattern_cache[col] = result
|
|
||||||
end
|
|
||||||
return result
|
|
||||||
end
|
end
|
||||||
|
|
||||||
--- Check if a pattern is opaque.
|
--- Check if a pattern is opaque.
|
||||||
|
@ -296,6 +290,8 @@ function color.mt:__call(...)
|
||||||
return color.create_pattern(...)
|
return color.create_pattern(...)
|
||||||
end
|
end
|
||||||
|
|
||||||
|
pattern_cache = require("gears.cache").new(color.create_pattern_uncached)
|
||||||
|
|
||||||
return setmetatable(color, color.mt)
|
return setmetatable(color, color.mt)
|
||||||
|
|
||||||
-- vim: filetype=lua:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:textwidth=80
|
-- vim: filetype=lua:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:textwidth=80
|
||||||
|
|
|
@ -38,21 +38,7 @@ function base.fit_widget(widget, width, height)
|
||||||
local width = math.max(0, width)
|
local width = math.max(0, width)
|
||||||
local height = math.max(0, height)
|
local height = math.max(0, height)
|
||||||
|
|
||||||
-- Since the geometry cache is a weak table, we have to be careful when
|
return widget._fit_geometry_cache:get(width, height)
|
||||||
-- doing lookups. We can't do "if cache[width] ~= nil then"!
|
|
||||||
local cache = widget._fit_geometry_cache
|
|
||||||
local result = cache[width]
|
|
||||||
if not result then
|
|
||||||
result = {}
|
|
||||||
cache[width] = result
|
|
||||||
end
|
|
||||||
cache, result = result, result[height]
|
|
||||||
if not result then
|
|
||||||
local w, h = widget:fit(width, height)
|
|
||||||
result = { width = w, height = h }
|
|
||||||
cache[height] = result
|
|
||||||
end
|
|
||||||
return result.width, result.height
|
|
||||||
end
|
end
|
||||||
|
|
||||||
--- Draw a widget via a cairo context
|
--- Draw a widget via a cairo context
|
||||||
|
|
|
@ -7,6 +7,7 @@
|
||||||
|
|
||||||
local debug = require("gears.debug")
|
local debug = require("gears.debug")
|
||||||
local object = require("gears.object")
|
local object = require("gears.object")
|
||||||
|
local cache = require("gears.cache")
|
||||||
local setmetatable = setmetatable
|
local setmetatable = setmetatable
|
||||||
local pairs = pairs
|
local pairs = pairs
|
||||||
local type = type
|
local type = type
|
||||||
|
@ -96,9 +97,12 @@ function base.make_widget(proxy)
|
||||||
end
|
end
|
||||||
|
|
||||||
-- Add a geometry for base.fit_widget() that is cleared when necessary
|
-- Add a geometry for base.fit_widget() that is cleared when necessary
|
||||||
ret._fit_geometry_cache = setmetatable({}, { __mode = 'v' })
|
local function cb(...)
|
||||||
|
return ret:fit(...)
|
||||||
|
end
|
||||||
|
ret._fit_geometry_cache = cache.new(cb)
|
||||||
ret:connect_signal("widget::updated", function()
|
ret:connect_signal("widget::updated", function()
|
||||||
ret._fit_geometry_cache = setmetatable({}, { __mode = 'v' })
|
ret._fit_geometry_cache = cache.new(cb)
|
||||||
end)
|
end)
|
||||||
|
|
||||||
return ret
|
return ret
|
||||||
|
|
|
@ -4,6 +4,7 @@
|
||||||
---------------------------------------------------------------------------
|
---------------------------------------------------------------------------
|
||||||
|
|
||||||
local object = require("gears.object")
|
local object = require("gears.object")
|
||||||
|
local cache = require("gears.cache")
|
||||||
local wbase = require("wibox.widget.base")
|
local wbase = require("wibox.widget.base")
|
||||||
local lbase = require("wibox.layout.base")
|
local lbase = require("wibox.layout.base")
|
||||||
local say = require("say")
|
local say = require("say")
|
||||||
|
@ -64,7 +65,7 @@ return {
|
||||||
return width or 10, height or 10
|
return width or 10, height or 10
|
||||||
end
|
end
|
||||||
w.draw = function() end
|
w.draw = function() end
|
||||||
w._fit_geometry_cache = {}
|
w._fit_geometry_cache = cache.new(w.fit)
|
||||||
|
|
||||||
spy.on(w, "fit")
|
spy.on(w, "fit")
|
||||||
stub(w, "draw")
|
stub(w, "draw")
|
||||||
|
|
Loading…
Reference in New Issue