progressbar: Fix a race condition

When created using the declarative syntax, set_value could be
called before set_max_value, this trimmed the value.
This commit is contained in:
Emmanuel Lepage Vallee 2016-07-24 16:25:19 -04:00
parent e5d4c188f1
commit 8ec53c827b
1 changed files with 13 additions and 3 deletions

View File

@ -83,8 +83,10 @@ function progressbar.draw(pbar, _, cr, width, height)
-- We want one pixel wide lines -- We want one pixel wide lines
cr:set_line_width(1) cr:set_line_width(1)
local value = pbar._private.value
local max_value = pbar._private.max_value local max_value = pbar._private.max_value
local value = math.min(max_value, math.max(0, pbar._private.value))
if value >= 0 then if value >= 0 then
value = value / max_value value = value / max_value
end end
@ -142,12 +144,20 @@ end
-- @param value The progress bar value between 0 and 1. -- @param value The progress bar value between 0 and 1.
function progressbar:set_value(value) function progressbar:set_value(value)
value = value or 0 value = value or 0
local max_value = self._private.max_value
self._private.value = math.min(max_value, math.max(0, value)) self._private.value = value
self:emit_signal("widget::redraw_needed") self:emit_signal("widget::redraw_needed")
return self return self
end end
function progressbar:set_max_value(max_value)
self._private.max_value = max_value
self:emit_signal("widget::redraw_needed")
end
--- Set the progressbar height. --- Set the progressbar height.
-- This method is deprecated, it no longer do anything. -- This method is deprecated, it no longer do anything.
-- Use a `wibox.container.constraint` widget or `forced_height`. -- Use a `wibox.container.constraint` widget or `forced_height`.