awful.widget: add progressbar set_max_value property

The awful.widget.graph allows to change the maximum value a graph can
handle, thus allows users to use widgets and scripts that don't scale
the values down to 0 - 1 range. The progressbars did not allow this
and worked with a hard-coded value of 1.

Signed-off-by: Adrian C. (anrxc) <anrxc@sysphere.org>
Signed-off-by: Julien Danjou <julien@danjou.info>
This commit is contained in:
Adrian C. (anrxc) 2010-02-26 21:25:35 +01:00 committed by Julien Danjou
parent f41d7e270f
commit 21d3d2aae5
1 changed files with 18 additions and 5 deletions

View File

@ -49,9 +49,15 @@ local data = setmetatable({}, { __mode = "k" })
-- @param progressbar The progressbar. -- @param progressbar The progressbar.
-- @param vertical A boolean value. -- @param vertical A boolean value.
--- Set the maximum value the progressbar should handle.
-- @name set_max_value
-- @class function
-- @param progressbar The progressbar.
-- @param value The value.
local properties = { "width", "height", "border_color", local properties = { "width", "height", "border_color",
"gradient_colors", "color", "background_color", "gradient_colors", "color", "background_color",
"vertical", "value" } "vertical", "value", "max_value" }
local function update(pbar) local function update(pbar)
local width = data[pbar].width or 100 local width = data[pbar].width or 100
@ -60,6 +66,12 @@ local function update(pbar)
-- Create new empty image -- Create new empty image
local img = capi.image.argb32(width, height, nil) local img = capi.image.argb32(width, height, nil)
local value = data[pbar].value
local max_value = data[pbar].max_value
if value >= 0 then
value = value / max_value
end
local over_drawn_width = width local over_drawn_width = width
local over_drawn_height = height local over_drawn_height = height
local border_width = 0 local border_width = 0
@ -89,14 +101,14 @@ local function update(pbar)
-- Cover the part that is not set with a rectangle -- Cover the part that is not set with a rectangle
if data[pbar].vertical then if data[pbar].vertical then
local rel_height = math.floor(over_drawn_height * (1 - data[pbar].value)) local rel_height = math.floor(over_drawn_height * (1 - value))
img:draw_rectangle(border_width, img:draw_rectangle(border_width,
border_width, border_width,
over_drawn_width, over_drawn_width,
rel_height, rel_height,
true, data[pbar].background_color or "#000000aa") true, data[pbar].background_color or "#000000aa")
else else
local rel_x = math.floor((over_drawn_width * data[pbar].value) + 0.5) local rel_x = math.floor((over_drawn_width * value) + 0.5)
img:draw_rectangle(border_width + rel_x, img:draw_rectangle(border_width + rel_x,
border_width, border_width,
over_drawn_width - rel_x, over_drawn_width - rel_x,
@ -113,7 +125,8 @@ end
-- @param value The progress bar value between 0 and 1. -- @param value The progress bar value between 0 and 1.
function set_value(pbar, value) function set_value(pbar, value)
local value = value or 0 local value = value or 0
data[pbar].value = math.min(1, math.max(0, value)) local max_value = data[pbar].max_value
data[pbar].value = math.min(max_value, math.max(0, value))
update(pbar) update(pbar)
return pbar return pbar
end end
@ -163,7 +176,7 @@ function new(args)
pbar.widget = capi.widget(args) pbar.widget = capi.widget(args)
pbar.widget.resize = false pbar.widget.resize = false
data[pbar] = { width = width, height = height, value = 0 } data[pbar] = { width = width, height = height, value = 0, max_value = 1 }
-- Set methods -- Set methods
for _, prop in ipairs(properties) do for _, prop in ipairs(properties) do