2009-05-04 16:37:21 +02:00
|
|
|
---------------------------------------------------------------------------
|
|
|
|
-- @author Julien Danjou <julien@danjou.info>
|
|
|
|
-- @copyright 2009 Julien Danjou
|
|
|
|
-- @release @AWESOME_VERSION@
|
|
|
|
---------------------------------------------------------------------------
|
|
|
|
|
|
|
|
local setmetatable = setmetatable
|
|
|
|
local ipairs = ipairs
|
|
|
|
local math = math
|
2010-10-06 14:25:59 +02:00
|
|
|
local base = require("wibox.widget.base")
|
2010-10-06 16:27:38 +02:00
|
|
|
local color = require("gears.color")
|
2009-05-04 16:37:21 +02:00
|
|
|
|
|
|
|
--- A progressbar widget.
|
2012-06-14 01:08:27 +02:00
|
|
|
-- awful.widget.progressbar
|
|
|
|
local progressbar = { mt = {} }
|
2009-05-04 16:37:21 +02:00
|
|
|
|
|
|
|
local data = setmetatable({}, { __mode = "k" })
|
|
|
|
|
|
|
|
--- Set the progressbar border color.
|
|
|
|
-- If the value is nil, no border will be drawn.
|
|
|
|
-- @name set_border_color
|
|
|
|
-- @class function
|
|
|
|
-- @param progressbar The progressbar.
|
|
|
|
-- @param color The border color to set.
|
|
|
|
|
|
|
|
--- Set the progressbar foreground color.
|
|
|
|
-- @name set_color
|
|
|
|
-- @class function
|
|
|
|
-- @param progressbar The progressbar.
|
|
|
|
-- @param color The progressbar color.
|
|
|
|
|
|
|
|
--- Set the progressbar background color.
|
|
|
|
-- @name set_background_color
|
|
|
|
-- @class function
|
|
|
|
-- @param progressbar The progressbar.
|
|
|
|
-- @param color The progressbar background color.
|
|
|
|
|
|
|
|
--- Set the progressbar to draw vertically. Default is false.
|
|
|
|
-- @name set_vertical
|
|
|
|
-- @class function
|
|
|
|
-- @param progressbar The progressbar.
|
|
|
|
-- @param vertical A boolean value.
|
|
|
|
|
2010-03-21 18:42:21 +01:00
|
|
|
--- Set the progressbar to draw ticks. Default is false.
|
|
|
|
-- @name set_ticks
|
|
|
|
-- @class function
|
|
|
|
-- @param progressbar The progressbar.
|
|
|
|
-- @param ticks A boolean value.
|
|
|
|
|
|
|
|
--- Set the progressbar ticks gap.
|
|
|
|
-- @name set_ticks_gap
|
|
|
|
-- @class function
|
|
|
|
-- @param progressbar The progressbar.
|
|
|
|
-- @param value The value.
|
|
|
|
|
|
|
|
--- Set the progressbar ticks size.
|
|
|
|
-- @name set_ticks_size
|
|
|
|
-- @class function
|
|
|
|
-- @param progressbar The progressbar.
|
|
|
|
-- @param value The value.
|
|
|
|
|
2010-02-26 21:25:35 +01:00
|
|
|
--- Set the maximum value the progressbar should handle.
|
|
|
|
-- @name set_max_value
|
|
|
|
-- @class function
|
|
|
|
-- @param progressbar The progressbar.
|
|
|
|
-- @param value The value.
|
|
|
|
|
2009-05-04 16:37:21 +02:00
|
|
|
local properties = { "width", "height", "border_color",
|
2010-09-29 19:26:23 +02:00
|
|
|
"color", "background_color",
|
2010-03-21 18:42:21 +01:00
|
|
|
"vertical", "value", "max_value",
|
|
|
|
"ticks", "ticks_gap", "ticks_size" }
|
2009-05-04 16:37:21 +02:00
|
|
|
|
2012-06-14 01:08:27 +02:00
|
|
|
function progressbar.draw(pbar, wibox, cr, width, height)
|
2010-03-21 18:42:21 +01:00
|
|
|
local ticks_gap = data[pbar].ticks_gap or 1
|
|
|
|
local ticks_size = data[pbar].ticks_size or 4
|
2009-05-04 16:37:21 +02:00
|
|
|
|
2010-10-06 14:25:59 +02:00
|
|
|
-- We want one pixel wide lines
|
2010-09-29 19:24:36 +02:00
|
|
|
cr:set_line_width(1)
|
2009-05-04 16:37:21 +02:00
|
|
|
|
2010-02-26 21:25:35 +01:00
|
|
|
local value = data[pbar].value
|
|
|
|
local max_value = data[pbar].max_value
|
|
|
|
if value >= 0 then
|
|
|
|
value = value / max_value
|
|
|
|
end
|
|
|
|
|
2009-05-04 16:37:21 +02:00
|
|
|
local over_drawn_width = width
|
|
|
|
local over_drawn_height = height
|
|
|
|
local border_width = 0
|
|
|
|
if data[pbar].border_color then
|
|
|
|
-- Draw border
|
2010-10-07 13:52:32 +02:00
|
|
|
cr:rectangle(0.5, 0.5, width - 1, height - 1)
|
2010-09-29 19:24:36 +02:00
|
|
|
cr:set_source(color(data[pbar].border_color))
|
|
|
|
cr:stroke()
|
2010-10-06 14:25:59 +02:00
|
|
|
|
2009-05-04 16:37:21 +02:00
|
|
|
over_drawn_width = width - 2 -- remove 2 for borders
|
|
|
|
over_drawn_height = height - 2 -- remove 2 for borders
|
|
|
|
border_width = 1
|
|
|
|
end
|
|
|
|
|
2010-09-29 19:26:23 +02:00
|
|
|
cr:rectangle(border_width, border_width,
|
|
|
|
over_drawn_width, over_drawn_height)
|
|
|
|
cr:set_source(color(data[pbar].color or "#ff0000"))
|
|
|
|
cr:fill()
|
2009-05-04 16:37:21 +02:00
|
|
|
|
|
|
|
-- Cover the part that is not set with a rectangle
|
|
|
|
if data[pbar].vertical then
|
2010-09-29 19:24:36 +02:00
|
|
|
local rel_height = over_drawn_height * (1 - value)
|
|
|
|
cr:rectangle(border_width,
|
|
|
|
border_width,
|
|
|
|
over_drawn_width,
|
|
|
|
rel_height)
|
|
|
|
cr:set_source(color(data[pbar].background_color or "#000000aa"))
|
|
|
|
cr:fill()
|
2010-03-21 18:42:21 +01:00
|
|
|
|
|
|
|
-- Place smaller pieces over the gradient if ticks are enabled
|
|
|
|
if data[pbar].ticks then
|
|
|
|
for i=0, height / (ticks_size+ticks_gap)-border_width do
|
|
|
|
local rel_offset = over_drawn_height / 1 - (ticks_size+ticks_gap) * i
|
|
|
|
|
|
|
|
if rel_offset >= rel_height then
|
2010-09-29 19:24:36 +02:00
|
|
|
cr:rectangle(border_width,
|
|
|
|
rel_offset,
|
|
|
|
over_drawn_width,
|
|
|
|
ticks_gap)
|
2010-03-21 18:42:21 +01:00
|
|
|
end
|
|
|
|
end
|
2010-09-29 19:24:36 +02:00
|
|
|
cr:set_source(color(data[pbar].background_color or "#000000aa"))
|
|
|
|
cr:fill()
|
2010-03-21 18:42:21 +01:00
|
|
|
end
|
2009-05-04 16:37:21 +02:00
|
|
|
else
|
2010-09-29 19:24:36 +02:00
|
|
|
local rel_x = over_drawn_width * value
|
|
|
|
cr:rectangle(border_width + rel_x,
|
|
|
|
border_width,
|
|
|
|
over_drawn_width - rel_x,
|
|
|
|
over_drawn_height)
|
|
|
|
cr:set_source(color(data[pbar].background_color or "#000000aa"))
|
|
|
|
cr:fill()
|
2010-03-21 18:42:21 +01:00
|
|
|
|
|
|
|
if data[pbar].ticks then
|
|
|
|
for i=0, width / (ticks_size+ticks_gap)-border_width do
|
|
|
|
local rel_offset = over_drawn_width / 1 - (ticks_size+ticks_gap) * i
|
|
|
|
|
|
|
|
if rel_offset <= rel_x then
|
2010-09-29 19:24:36 +02:00
|
|
|
cr:rectangle(rel_offset,
|
|
|
|
border_width,
|
|
|
|
ticks_gap,
|
|
|
|
over_drawn_height)
|
2010-03-21 18:42:21 +01:00
|
|
|
end
|
|
|
|
end
|
2010-09-29 19:24:36 +02:00
|
|
|
cr:set_source(color(data[pbar].background_color or "#000000aa"))
|
|
|
|
cr:fill()
|
2010-03-21 18:42:21 +01:00
|
|
|
end
|
2009-05-04 16:37:21 +02:00
|
|
|
end
|
2010-10-06 14:25:59 +02:00
|
|
|
end
|
2009-05-04 16:37:21 +02:00
|
|
|
|
2012-06-14 01:08:27 +02:00
|
|
|
function progressbar.fit(pbar, width, height)
|
2010-10-06 14:25:59 +02:00
|
|
|
return data[pbar].width, data[pbar].height
|
2009-05-04 16:37:21 +02:00
|
|
|
end
|
|
|
|
|
2010-04-27 01:56:43 +02:00
|
|
|
--- Set the progressbar value.
|
2009-05-04 16:37:21 +02:00
|
|
|
-- @param pbar The progress bar.
|
|
|
|
-- @param value The progress bar value between 0 and 1.
|
2012-06-14 01:08:27 +02:00
|
|
|
function progressbar.set_value(pbar, value)
|
2009-05-04 16:37:21 +02:00
|
|
|
local value = value or 0
|
2010-02-26 21:25:35 +01:00
|
|
|
local max_value = data[pbar].max_value
|
|
|
|
data[pbar].value = math.min(max_value, math.max(0, value))
|
2010-10-06 14:25:59 +02:00
|
|
|
pbar:emit_signal("widget::updated")
|
2009-08-08 15:47:00 +02:00
|
|
|
return pbar
|
2009-05-04 16:37:21 +02:00
|
|
|
end
|
|
|
|
|
|
|
|
--- Set the progressbar height.
|
|
|
|
-- @param progressbar The progressbar.
|
|
|
|
-- @param height The height to set.
|
2012-06-14 01:08:27 +02:00
|
|
|
function progressbar.set_height(_progressbar, height)
|
|
|
|
data[_progressbar].height = height
|
|
|
|
_progressbar:emit_signal("widget::updated")
|
|
|
|
return _progressbar
|
2009-05-04 16:37:21 +02:00
|
|
|
end
|
|
|
|
|
|
|
|
--- Set the progressbar width.
|
|
|
|
-- @param progressbar The progressbar.
|
|
|
|
-- @param width The width to set.
|
2012-06-14 01:08:27 +02:00
|
|
|
function progressbar.set_width(_progressbar, width)
|
|
|
|
data[_progressbar].width = width
|
|
|
|
_progressbar:emit_signal("widget::updated")
|
|
|
|
return _progressbar
|
2009-05-04 16:37:21 +02:00
|
|
|
end
|
|
|
|
|
|
|
|
-- Build properties function
|
|
|
|
for _, prop in ipairs(properties) do
|
2012-06-14 01:08:27 +02:00
|
|
|
if not progressbar["set_" .. prop] then
|
|
|
|
progressbar["set_" .. prop] = function(pbar, value)
|
2009-05-04 16:37:21 +02:00
|
|
|
data[pbar][prop] = value
|
2010-10-06 14:25:59 +02:00
|
|
|
pbar:emit_signal("widget::updated")
|
2009-08-08 15:47:00 +02:00
|
|
|
return pbar
|
2009-05-04 16:37:21 +02:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
--- Create a progressbar widget.
|
|
|
|
-- @param args Standard widget() arguments. You should add width and height
|
|
|
|
-- key to set progressbar geometry.
|
|
|
|
-- @return A progressbar widget.
|
2012-06-14 01:08:27 +02:00
|
|
|
function progressbar.new(args)
|
2009-05-04 16:37:21 +02:00
|
|
|
local args = args or {}
|
|
|
|
local width = args.width or 100
|
|
|
|
local height = args.height or 20
|
|
|
|
|
|
|
|
args.type = "imagebox"
|
|
|
|
|
2010-10-06 14:25:59 +02:00
|
|
|
local pbar = base.make_widget()
|
2009-05-04 16:37:21 +02:00
|
|
|
|
2010-02-26 21:25:35 +01:00
|
|
|
data[pbar] = { width = width, height = height, value = 0, max_value = 1 }
|
2009-05-04 16:37:21 +02:00
|
|
|
|
|
|
|
-- Set methods
|
|
|
|
for _, prop in ipairs(properties) do
|
2012-06-14 01:08:27 +02:00
|
|
|
pbar["set_" .. prop] = progressbar["set_" .. prop]
|
2009-05-04 16:37:21 +02:00
|
|
|
end
|
|
|
|
|
2012-06-14 01:08:27 +02:00
|
|
|
pbar.draw = progressbar.draw
|
|
|
|
pbar.fit = progressbar.fit
|
2009-07-19 22:01:15 +02:00
|
|
|
|
2009-05-04 16:37:21 +02:00
|
|
|
return pbar
|
|
|
|
end
|
|
|
|
|
2012-06-14 01:08:27 +02:00
|
|
|
function progressbar.mt:__call(...)
|
|
|
|
return progressbar.new(...)
|
|
|
|
end
|
|
|
|
|
|
|
|
return setmetatable(progressbar, progressbar.mt)
|
2009-05-04 16:37:21 +02:00
|
|
|
|
2011-09-11 16:50:01 +02:00
|
|
|
-- vim: filetype=lua:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:textwidth=80
|