1 ---------------------------------------------------------------------------
2 -- @author Julien Danjou <julien@danjou.info>
3 -- @copyright 2009 Julien Danjou
4 -- @release @AWESOME_VERSION@
5 ---------------------------------------------------------------------------
7 local setmetatable
= setmetatable
10 local capi
= { image
= image
,
12 local layout
= require("awful.widget.layout")
14 --- A progressbar widget.
15 module("awful.widget.progressbar")
17 local data
= setmetatable({}, { __mode
= "k" })
19 --- Set the progressbar border color.
20 -- If the value is nil, no border will be drawn.
21 -- @name set_border_color
23 -- @param progressbar The progressbar.
24 -- @param color The border color to set.
26 --- Set the progressbar foreground color as a gradient.
27 -- @name set_gradient_colors
29 -- @param progressbar The progressbar.
30 -- @param gradient_colors A table with gradients colors. The distance between each color
31 -- can also be specified. Example: { "red", "blue" } or { "red", "green",
32 -- "blue", blue = 10 } to specify blue distance from other colors.
34 --- Set the progressbar foreground color.
37 -- @param progressbar The progressbar.
38 -- @param color The progressbar color.
40 --- Set the progressbar background color.
41 -- @name set_background_color
43 -- @param progressbar The progressbar.
44 -- @param color The progressbar background color.
46 --- Set the progressbar to draw vertically. Default is false.
49 -- @param progressbar The progressbar.
50 -- @param vertical A boolean value.
52 --- Set the progressbar to draw ticks. Default is false.
55 -- @param progressbar The progressbar.
56 -- @param ticks A boolean value.
58 --- Set the progressbar ticks gap.
59 -- @name set_ticks_gap
61 -- @param progressbar The progressbar.
62 -- @param value The value.
64 --- Set the progressbar ticks size.
65 -- @name set_ticks_size
67 -- @param progressbar The progressbar.
68 -- @param value The value.
70 --- Set the maximum value the progressbar should handle.
71 -- @name set_max_value
73 -- @param progressbar The progressbar.
74 -- @param value The value.
76 local properties
= { "width", "height", "border_color",
77 "gradient_colors", "color", "background_color",
78 "vertical", "value", "max_value",
79 "ticks", "ticks_gap", "ticks_size" }
81 local function update(pbar
)
82 local width
= data
[pbar
].width
or 100
83 local height
= data
[pbar
].height
or 20
84 local ticks_gap
= data
[pbar
].ticks_gap
or 1
85 local ticks_size
= data
[pbar
].ticks_size
or 4
87 -- Create new empty image
88 local img
= capi
.image
.argb32(width
, height
, nil)
90 local value
= data
[pbar
].value
91 local max_value
= data
[pbar
].max_value
93 value
= value
/ max_value
96 local over_drawn_width
= width
97 local over_drawn_height
= height
98 local border_width
= 0
99 if data
[pbar
].border_color
then
101 img
:draw_rectangle(0, 0, width
, height
, false, data
[pbar
].border_color
)
102 over_drawn_width
= width
- 2 -- remove 2 for borders
103 over_drawn_height
= height
- 2 -- remove 2 for borders
108 if data
[pbar
].vertical
then
112 -- Draw full gradient
113 if data
[pbar
].gradient_colors
then
114 img
:draw_rectangle_gradient(border_width
, border_width
,
115 over_drawn_width
, over_drawn_height
,
116 data
[pbar
].gradient_colors
, angle
)
118 img
:draw_rectangle(border_width
, border_width
,
119 over_drawn_width
, over_drawn_height
,
120 true, data
[pbar
].color
or "red")
123 -- Cover the part that is not set with a rectangle
124 if data
[pbar
].vertical
then
125 local rel_height
= math
.floor(over_drawn_height
* (1 - value
))
126 img
:draw_rectangle(border_width
,
130 true, data
[pbar
].background_color
or "#000000aa")
132 -- Place smaller pieces over the gradient if ticks are enabled
133 if data
[pbar
].ticks
then
134 for i
=0, height
/ (ticks_size
+ticks_gap
)-border_width
do
135 local rel_offset
= over_drawn_height
/ 1 - (ticks_size
+ticks_gap
) * i
137 if rel_offset
>= rel_height
then
138 img
:draw_rectangle(border_width
,
142 true, data
[pbar
].background_color
or "#000000aa")
147 local rel_x
= math
.ceil(over_drawn_width
* value
)
148 img
:draw_rectangle(border_width
+ rel_x
,
150 over_drawn_width
- rel_x
,
152 true, data
[pbar
].background_color
or "#000000aa")
154 if data
[pbar
].ticks
then
155 for i
=0, width
/ (ticks_size
+ticks_gap
)-border_width
do
156 local rel_offset
= over_drawn_width
/ 1 - (ticks_size
+ticks_gap
) * i
158 if rel_offset
<= rel_x
then
159 img
:draw_rectangle(rel_offset
,
163 true, data
[pbar
].background_color
or "#000000aa")
170 pbar
.widget
.image
= img
173 --- Set the progressbar value.
174 -- @param pbar The progress bar.
175 -- @param value The progress bar value between 0 and 1.
176 function set_value(pbar
, value
)
177 local value
= value
or 0
178 local max_value
= data
[pbar
].max_value
179 data
[pbar
].value
= math
.min(max_value
, math
.max(0, value
))
184 --- Set the progressbar height.
185 -- @param progressbar The progressbar.
186 -- @param height The height to set.
187 function set_height(progressbar
, height
)
188 data
[progressbar
].height
= height
193 --- Set the progressbar width.
194 -- @param progressbar The progressbar.
195 -- @param width The width to set.
196 function set_width(progressbar
, width
)
197 data
[progressbar
].width
= width
202 -- Build properties function
203 for _
, prop
in ipairs(properties
) do
204 if not _M
["set_" .. prop
] then
205 _M
["set_" .. prop
] = function(pbar
, value
)
206 data
[pbar
][prop
] = value
213 --- Create a progressbar widget.
214 -- @param args Standard widget() arguments. You should add width and height
215 -- key to set progressbar geometry.
216 -- @return A progressbar widget.
218 local args
= args
or {}
219 local width
= args
.width
or 100
220 local height
= args
.height
or 20
222 args
.type = "imagebox"
226 pbar
.widget
= capi
.widget(args
)
227 pbar
.widget
.resize
= false
229 data
[pbar
] = { width
= width
, height
= height
, value
= 0, max_value
= 1 }
232 for _
, prop
in ipairs(properties
) do
233 pbar
["set_" .. prop
] = _M
["set_" .. prop
]
236 pbar
.layout
= args
.layout
or layout
.horizontal
.leftright
241 setmetatable(_M
, { __call
= function(_
, ...) return new(...) end })
243 -- vim: filetype=lua:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:textwidth=80