2024-01-15 13:30:50 +03:00
|
|
|
Progress = {
|
2024-07-12 13:32:01 +03:00
|
|
|
_area = ui.Rect.default, -- TODO: remove this
|
2024-01-15 13:30:50 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
function Progress:render(area, offset)
|
2024-07-12 13:32:01 +03:00
|
|
|
self._area = ui.Rect {
|
2024-01-15 13:30:50 +03:00
|
|
|
x = math.max(0, area.w - offset - 21),
|
|
|
|
y = area.y,
|
2024-01-24 11:18:40 +03:00
|
|
|
w = ya.clamp(0, area.w - offset - 1, 20),
|
|
|
|
h = math.min(1, area.h),
|
2024-01-15 13:30:50 +03:00
|
|
|
}
|
|
|
|
return self:partial_render()
|
|
|
|
end
|
|
|
|
|
|
|
|
-- Progress bars usually need frequent updates to report the latest task progress.
|
|
|
|
-- We use `partial_render()` to partially render it when there is progress change,
|
|
|
|
-- which has almost no cost compared to a full render by `render()`.
|
|
|
|
function Progress:partial_render()
|
|
|
|
local progress = cx.tasks.progress
|
|
|
|
if progress.total == 0 then
|
2024-10-13 20:54:03 +03:00
|
|
|
return { ui.Text {} }
|
2024-01-15 13:30:50 +03:00
|
|
|
end
|
|
|
|
|
2024-10-14 14:39:59 +03:00
|
|
|
local gauge = ui.Gauge():area(self._area)
|
2024-01-15 13:30:50 +03:00
|
|
|
if progress.fail == 0 then
|
|
|
|
gauge = gauge:gauge_style(THEME.status.progress_normal)
|
|
|
|
else
|
|
|
|
gauge = gauge:gauge_style(THEME.status.progress_error)
|
|
|
|
end
|
|
|
|
|
|
|
|
local percent = 99
|
|
|
|
if progress.found ~= 0 then
|
|
|
|
percent = math.min(99, ya.round(progress.processed * 100 / progress.found))
|
|
|
|
end
|
|
|
|
|
|
|
|
local left = progress.total - progress.succ
|
|
|
|
return {
|
|
|
|
gauge
|
|
|
|
:percent(percent)
|
|
|
|
:label(ui.Span(string.format("%3d%%, %d left", percent, left)):style(THEME.status.progress_label)),
|
|
|
|
}
|
|
|
|
end
|