2016-05-23 07:22:35 +02:00
|
|
|
---------------------------------------------------------------------------
|
|
|
|
--- A progressbar widget.
|
|
|
|
--
|
2021-11-03 03:29:31 +01:00
|
|
|
-- ![Components](../images/progressbar.svg)
|
|
|
|
--
|
|
|
|
-- Common usage examples
|
|
|
|
-- =====================
|
|
|
|
--
|
2016-07-25 04:06:11 +02:00
|
|
|
-- To add text on top of the progressbar, a `wibox.layout.stack` can be used:
|
|
|
|
--
|
|
|
|
--@DOC_wibox_widget_progressbar_text_EXAMPLE@
|
|
|
|
--
|
|
|
|
-- To display the progressbar vertically, use a `wibox.container.rotate` widget:
|
|
|
|
--
|
|
|
|
--@DOC_wibox_widget_progressbar_vertical_EXAMPLE@
|
|
|
|
--
|
2016-07-24 01:21:15 +02:00
|
|
|
-- By default, this widget will take all the available size. To prevent this,
|
|
|
|
-- a `wibox.container.constraint` widget or the `forced_width`/`forced_height`
|
|
|
|
-- properties have to be used.
|
|
|
|
--
|
2021-11-03 03:29:31 +01:00
|
|
|
-- To have a gradient between 2 colors when the bar reaches a threshold, use
|
|
|
|
-- the `gears.color` gradients:
|
|
|
|
--
|
|
|
|
--@DOC_wibox_widget_progressbar_grad1_EXAMPLE@
|
|
|
|
--
|
|
|
|
-- The same goes for multiple solid colors:
|
|
|
|
--
|
|
|
|
--@DOC_wibox_widget_progressbar_grad2_EXAMPLE@
|
|
|
|
--
|
2016-05-25 23:22:44 +02:00
|
|
|
--@DOC_wibox_widget_defaults_progressbar_EXAMPLE@
|
2016-07-25 04:06:11 +02:00
|
|
|
--
|
2016-05-23 07:22:35 +02:00
|
|
|
-- @author Julien Danjou <julien@danjou.info>
|
|
|
|
-- @copyright 2009 Julien Danjou
|
2019-06-06 08:15:53 +02:00
|
|
|
-- @widgetmod wibox.widget.progressbar
|
2021-03-27 20:41:28 +01:00
|
|
|
-- @supermodule wibox.widget.base
|
2016-05-23 07:22:35 +02:00
|
|
|
---------------------------------------------------------------------------
|
|
|
|
|
|
|
|
local setmetatable = setmetatable
|
|
|
|
local ipairs = ipairs
|
|
|
|
local math = math
|
2017-03-15 06:08:40 +01:00
|
|
|
local gdebug = require("gears.debug")
|
2016-05-23 07:22:35 +02:00
|
|
|
local base = require("wibox.widget.base")
|
|
|
|
local color = require("gears.color")
|
2016-05-27 07:26:46 +02:00
|
|
|
local beautiful = require("beautiful")
|
2016-07-25 00:05:00 +02:00
|
|
|
local shape = require("gears.shape")
|
2017-03-08 21:18:33 +01:00
|
|
|
local gtable = require("gears.table")
|
2016-05-23 07:22:35 +02:00
|
|
|
|
|
|
|
local progressbar = { mt = {} }
|
|
|
|
|
2016-07-24 01:02:00 +02:00
|
|
|
--- The progressbar border color.
|
2019-11-29 06:33:44 +01:00
|
|
|
--
|
2016-05-23 07:22:35 +02:00
|
|
|
-- If the value is nil, no border will be drawn.
|
|
|
|
--
|
2021-11-03 03:29:31 +01:00
|
|
|
-- @DOC_wibox_widget_progressbar_border_color_EXAMPLE@
|
|
|
|
--
|
2016-07-24 01:02:00 +02:00
|
|
|
-- @property border_color
|
2021-12-21 06:54:15 +01:00
|
|
|
-- @tparam gears.color border_color The border color to set.
|
2019-11-29 06:33:44 +01:00
|
|
|
-- @propemits true false
|
|
|
|
-- @propbeautiful
|
2016-11-20 10:01:39 +01:00
|
|
|
-- @see gears.color
|
2016-05-23 07:22:35 +02:00
|
|
|
|
2016-07-25 00:05:00 +02:00
|
|
|
--- The progressbar border width.
|
2019-11-29 06:33:44 +01:00
|
|
|
--
|
2021-11-03 03:29:31 +01:00
|
|
|
-- @DOC_wibox_widget_progressbar_border_width_EXAMPLE@
|
|
|
|
--
|
2016-07-25 00:05:00 +02:00
|
|
|
-- @property border_width
|
2019-11-29 06:33:44 +01:00
|
|
|
-- @tparam number border_width
|
|
|
|
-- @propemits true false
|
|
|
|
-- @propbeautiful
|
2016-07-25 00:05:00 +02:00
|
|
|
|
|
|
|
--- The progressbar inner border color.
|
2019-11-29 06:33:44 +01:00
|
|
|
--
|
2016-07-25 00:05:00 +02:00
|
|
|
-- If the value is nil, no border will be drawn.
|
|
|
|
--
|
2021-11-03 03:29:31 +01:00
|
|
|
-- @DOC_wibox_widget_progressbar_bar_border_color_EXAMPLE@
|
|
|
|
--
|
2016-07-25 00:05:00 +02:00
|
|
|
-- @property bar_border_color
|
2021-12-21 06:54:15 +01:00
|
|
|
-- @tparam gears.color bar_border_color The border color to set.
|
2019-11-29 06:33:44 +01:00
|
|
|
-- @propemits true false
|
|
|
|
-- @propbeautiful
|
2016-11-20 10:01:39 +01:00
|
|
|
-- @see gears.color
|
2016-07-25 00:05:00 +02:00
|
|
|
|
|
|
|
--- The progressbar inner border width.
|
2019-11-29 06:33:44 +01:00
|
|
|
--
|
2021-11-03 03:29:31 +01:00
|
|
|
-- @DOC_wibox_widget_progressbar_bar_border_width_EXAMPLE@
|
|
|
|
--
|
2016-07-25 00:05:00 +02:00
|
|
|
-- @property bar_border_width
|
2019-11-29 06:33:44 +01:00
|
|
|
-- @tparam number bar_border_width
|
|
|
|
-- @propbeautiful
|
|
|
|
-- @propemits true false
|
2016-07-25 00:05:00 +02:00
|
|
|
|
2016-07-24 01:02:00 +02:00
|
|
|
--- The progressbar foreground color.
|
2016-05-23 07:22:35 +02:00
|
|
|
--
|
2021-11-03 03:29:31 +01:00
|
|
|
-- @DOC_wibox_widget_progressbar_color_EXAMPLE@
|
|
|
|
--
|
2016-07-24 01:02:00 +02:00
|
|
|
-- @property color
|
2021-11-03 03:29:31 +01:00
|
|
|
-- @tparam gears.color color The progressbar color.
|
2019-11-29 06:33:44 +01:00
|
|
|
-- @propemits true false
|
|
|
|
-- @usebeautiful beautiful.progressbar_fg
|
2016-11-20 10:01:39 +01:00
|
|
|
-- @see gears.color
|
2016-05-23 07:22:35 +02:00
|
|
|
|
2016-07-24 01:02:00 +02:00
|
|
|
--- The progressbar background color.
|
2016-05-23 07:22:35 +02:00
|
|
|
--
|
2021-11-03 03:29:31 +01:00
|
|
|
-- @DOC_wibox_widget_progressbar_background_color_EXAMPLE@
|
|
|
|
--
|
2016-07-24 01:02:00 +02:00
|
|
|
-- @property background_color
|
2021-12-21 06:54:15 +01:00
|
|
|
-- @tparam gears.color background_color The progressbar background color.
|
2019-11-29 06:33:44 +01:00
|
|
|
-- @propemits true false
|
|
|
|
-- @usebeautiful beautiful.progressbar_bg
|
2016-11-20 10:01:39 +01:00
|
|
|
-- @see gears.color
|
2016-05-23 07:22:35 +02:00
|
|
|
|
2016-07-25 00:05:00 +02:00
|
|
|
--- The progressbar inner shape.
|
2016-07-25 04:35:58 +02:00
|
|
|
--
|
|
|
|
--@DOC_wibox_widget_progressbar_bar_shape_EXAMPLE@
|
|
|
|
--
|
2016-07-25 00:05:00 +02:00
|
|
|
-- @property bar_shape
|
2021-12-21 06:54:15 +01:00
|
|
|
-- @tparam[opt=gears.shape.rectangle] gears.shape bar_shape
|
2019-11-29 06:33:44 +01:00
|
|
|
-- @propemits true false
|
|
|
|
-- @propbeautiful
|
2016-07-25 00:05:00 +02:00
|
|
|
-- @see gears.shape
|
|
|
|
|
|
|
|
--- The progressbar shape.
|
2016-07-25 04:35:58 +02:00
|
|
|
--
|
|
|
|
--@DOC_wibox_widget_progressbar_shape_EXAMPLE@
|
|
|
|
--
|
2016-07-25 00:05:00 +02:00
|
|
|
-- @property shape
|
|
|
|
-- @tparam[opt=gears.shape.rectangle] gears.shape shape
|
2019-11-29 06:33:44 +01:00
|
|
|
-- @propemits true false
|
|
|
|
-- @propbeautiful
|
2016-07-25 00:05:00 +02:00
|
|
|
-- @see gears.shape
|
|
|
|
|
2016-07-24 01:21:15 +02:00
|
|
|
--- Set the progressbar to draw vertically.
|
2019-11-29 06:33:44 +01:00
|
|
|
--
|
2016-07-24 01:21:15 +02:00
|
|
|
-- This doesn't do anything anymore, use a `wibox.container.rotate` widget.
|
2019-11-29 06:33:44 +01:00
|
|
|
--
|
2016-07-24 00:32:37 +02:00
|
|
|
-- @deprecated set_vertical
|
2016-07-24 01:02:00 +02:00
|
|
|
-- @tparam boolean vertical
|
2019-11-29 06:33:44 +01:00
|
|
|
-- @deprecatedin 4.0
|
2016-05-23 07:22:35 +02:00
|
|
|
|
2016-07-25 00:05:00 +02:00
|
|
|
--- Force the inner part (the bar) to fit in the background shape.
|
|
|
|
--
|
2016-07-25 04:18:12 +02:00
|
|
|
--@DOC_wibox_widget_progressbar_clip_EXAMPLE@
|
|
|
|
--
|
2016-07-25 00:05:00 +02:00
|
|
|
-- @property clip
|
|
|
|
-- @tparam[opt=true] boolean clip
|
2019-11-29 06:33:44 +01:00
|
|
|
-- @propemits true false
|
2016-07-25 00:05:00 +02:00
|
|
|
|
2021-11-03 03:29:31 +01:00
|
|
|
--- The progressbar to draw ticks.
|
|
|
|
--
|
|
|
|
-- @DOC_wibox_widget_progressbar_ticks_EXAMPLE@
|
2016-05-23 07:22:35 +02:00
|
|
|
--
|
2016-07-24 01:02:00 +02:00
|
|
|
-- @property ticks
|
2021-11-03 03:29:31 +01:00
|
|
|
-- @tparam[opt=false] boolean ticks
|
2019-11-29 06:33:44 +01:00
|
|
|
-- @propemits true false
|
2016-05-23 07:22:35 +02:00
|
|
|
|
2016-07-24 01:02:00 +02:00
|
|
|
--- The progressbar ticks gap.
|
2016-05-23 07:22:35 +02:00
|
|
|
--
|
2021-11-03 03:29:31 +01:00
|
|
|
-- @DOC_wibox_widget_progressbar_ticks_gap_EXAMPLE@
|
|
|
|
--
|
2016-07-24 01:02:00 +02:00
|
|
|
-- @property ticks_gap
|
2021-11-03 03:29:31 +01:00
|
|
|
-- @tparam[opt=1] number ticks_gap
|
2019-11-29 06:33:44 +01:00
|
|
|
-- @propemits true false
|
2016-05-23 07:22:35 +02:00
|
|
|
|
2016-07-24 01:02:00 +02:00
|
|
|
--- The progressbar ticks size.
|
2016-05-23 07:22:35 +02:00
|
|
|
--
|
2021-11-03 03:29:31 +01:00
|
|
|
-- @DOC_wibox_widget_progressbar_ticks_size_EXAMPLE@
|
|
|
|
--
|
2021-11-03 04:15:53 +01:00
|
|
|
-- It is also possible to mix this feature with the `bar_shape` property:
|
|
|
|
--
|
|
|
|
-- @DOC_wibox_widget_progressbar_ticks_size2_EXAMPLE@
|
|
|
|
--
|
2016-07-24 01:02:00 +02:00
|
|
|
-- @property ticks_size
|
2021-11-03 03:29:31 +01:00
|
|
|
-- @tparam[opt=4] number ticks_size
|
2019-11-29 06:33:44 +01:00
|
|
|
-- @propemits true false
|
2016-05-23 07:22:35 +02:00
|
|
|
|
2016-07-24 01:02:00 +02:00
|
|
|
--- The maximum value the progressbar should handle.
|
2016-05-23 07:22:35 +02:00
|
|
|
--
|
2021-11-03 03:29:31 +01:00
|
|
|
-- By default, the value is 1. So the content of `value` is
|
|
|
|
-- a percentage.
|
|
|
|
--
|
|
|
|
-- @DOC_wibox_widget_progressbar_max_value_EXAMPLE@
|
|
|
|
--
|
2016-07-24 01:02:00 +02:00
|
|
|
-- @property max_value
|
2021-11-03 03:29:31 +01:00
|
|
|
-- @tparam[opt=1] number max_value
|
2019-11-29 06:33:44 +01:00
|
|
|
-- @propemits true false
|
2021-11-03 03:29:31 +01:00
|
|
|
-- @see value
|
2016-05-23 07:22:35 +02:00
|
|
|
|
2016-05-27 07:26:46 +02:00
|
|
|
--- The progressbar background color.
|
2019-11-29 06:33:44 +01:00
|
|
|
--
|
2016-07-24 00:26:27 +02:00
|
|
|
-- @beautiful beautiful.progressbar_bg
|
2019-06-08 06:15:59 +02:00
|
|
|
-- @param color
|
2016-05-27 07:26:46 +02:00
|
|
|
|
|
|
|
--- The progressbar foreground color.
|
2019-11-29 06:33:44 +01:00
|
|
|
--
|
2016-07-24 00:26:27 +02:00
|
|
|
-- @beautiful beautiful.progressbar_fg
|
2019-06-08 06:15:59 +02:00
|
|
|
-- @param color
|
2016-05-27 07:26:46 +02:00
|
|
|
|
2016-07-25 00:05:00 +02:00
|
|
|
--- The progressbar shape.
|
2019-11-29 06:33:44 +01:00
|
|
|
--
|
2016-07-25 00:05:00 +02:00
|
|
|
-- @beautiful beautiful.progressbar_shape
|
2019-06-08 06:15:59 +02:00
|
|
|
-- @tparam gears.shape shape
|
2016-07-25 00:05:00 +02:00
|
|
|
-- @see gears.shape
|
|
|
|
|
2016-05-27 07:26:46 +02:00
|
|
|
--- The progressbar border color.
|
2019-11-29 06:33:44 +01:00
|
|
|
--
|
2016-07-24 00:26:27 +02:00
|
|
|
-- @beautiful beautiful.progressbar_border_color
|
2019-06-08 06:15:59 +02:00
|
|
|
-- @param color
|
2016-05-27 07:26:46 +02:00
|
|
|
|
2016-07-25 00:05:00 +02:00
|
|
|
--- The progressbar outer border width.
|
2019-11-29 06:33:44 +01:00
|
|
|
--
|
2016-07-25 00:05:00 +02:00
|
|
|
-- @beautiful beautiful.progressbar_border_width
|
2019-06-08 06:15:59 +02:00
|
|
|
-- @param number
|
2016-07-25 00:05:00 +02:00
|
|
|
|
|
|
|
--- The progressbar inner shape.
|
2019-11-29 06:33:44 +01:00
|
|
|
--
|
2016-07-25 00:05:00 +02:00
|
|
|
-- @beautiful beautiful.progressbar_bar_shape
|
2019-06-08 06:15:59 +02:00
|
|
|
-- @tparam gears.shape shape
|
2016-07-25 00:05:00 +02:00
|
|
|
-- @see gears.shape
|
|
|
|
|
|
|
|
--- The progressbar bar border width.
|
2019-11-29 06:33:44 +01:00
|
|
|
--
|
2016-07-25 00:05:00 +02:00
|
|
|
-- @beautiful beautiful.progressbar_bar_border_width
|
2019-06-08 06:15:59 +02:00
|
|
|
-- @param number
|
2016-07-25 00:05:00 +02:00
|
|
|
|
|
|
|
--- The progressbar bar border color.
|
2019-11-29 06:33:44 +01:00
|
|
|
--
|
2016-07-25 00:05:00 +02:00
|
|
|
-- @beautiful beautiful.progressbar_bar_border_color
|
2019-06-08 06:15:59 +02:00
|
|
|
-- @param color
|
2016-07-25 00:05:00 +02:00
|
|
|
|
2016-07-25 02:11:40 +02:00
|
|
|
--- The progressbar margins.
|
2019-11-29 06:33:44 +01:00
|
|
|
--
|
2021-11-03 03:29:31 +01:00
|
|
|
-- The margins are around the progressbar. If you want to add space between the
|
|
|
|
-- bar and the border, use `paddings`.
|
|
|
|
--
|
|
|
|
-- @DOC_wibox_widget_progressbar_margins2_EXAMPLE@
|
|
|
|
--
|
2016-07-25 02:11:40 +02:00
|
|
|
-- Note that if the `clip` is disabled, this allows the background to be smaller
|
|
|
|
-- than the bar.
|
2016-07-25 04:18:12 +02:00
|
|
|
--
|
2021-11-03 03:29:31 +01:00
|
|
|
-- It is also possible to specify a single number instead of a border for each
|
|
|
|
-- direction;
|
|
|
|
--
|
|
|
|
-- @DOC_wibox_widget_progressbar_margins1_EXAMPLE@
|
2016-07-25 04:18:12 +02:00
|
|
|
--
|
2019-11-29 06:33:44 +01:00
|
|
|
-- @property margins
|
2021-12-21 06:54:15 +01:00
|
|
|
-- @tparam[opt=0] table|number|nil margins A table for each side or a number
|
2016-07-25 02:11:40 +02:00
|
|
|
-- @tparam[opt=0] number margins.top
|
|
|
|
-- @tparam[opt=0] number margins.bottom
|
|
|
|
-- @tparam[opt=0] number margins.left
|
|
|
|
-- @tparam[opt=0] number margins.right
|
2019-11-29 06:33:44 +01:00
|
|
|
-- @propemits false false
|
|
|
|
-- @propbeautiful
|
2016-07-25 02:11:40 +02:00
|
|
|
-- @see clip
|
2022-02-03 13:25:05 +01:00
|
|
|
-- @see paddings
|
2021-11-03 03:29:31 +01:00
|
|
|
-- @see wibox.container.margin
|
2016-07-25 02:11:40 +02:00
|
|
|
|
|
|
|
--- The progressbar padding.
|
2019-11-29 06:33:44 +01:00
|
|
|
--
|
2021-11-03 03:29:31 +01:00
|
|
|
-- This is the space between the inner bar and the progressbar outer border.
|
|
|
|
--
|
2016-07-25 02:11:40 +02:00
|
|
|
-- Note that if the `clip` is disabled, this allows the bar to be taller
|
|
|
|
-- than the background.
|
2016-07-25 04:18:12 +02:00
|
|
|
--
|
2021-11-03 03:29:31 +01:00
|
|
|
-- @DOC_wibox_widget_progressbar_paddings2_EXAMPLE@
|
|
|
|
--
|
|
|
|
-- The paddings can also be a single numeric value:
|
|
|
|
--
|
|
|
|
-- @DOC_wibox_widget_progressbar_paddings1_EXAMPLE@
|
2016-07-25 04:18:12 +02:00
|
|
|
--
|
2019-11-29 06:33:44 +01:00
|
|
|
-- @property paddings
|
2021-12-21 06:54:15 +01:00
|
|
|
-- @tparam[opt=0] (table|number|nil) paddings A table for each side or a number
|
2022-02-03 13:25:05 +01:00
|
|
|
-- @tparam[opt=0] number paddings.top
|
|
|
|
-- @tparam[opt=0] number paddings.bottom
|
|
|
|
-- @tparam[opt=0] number paddings.left
|
|
|
|
-- @tparam[opt=0] number paddings.right
|
2019-11-29 06:33:44 +01:00
|
|
|
-- @propemits false false
|
|
|
|
-- @propbeautiful
|
2016-07-25 02:11:40 +02:00
|
|
|
-- @see clip
|
2021-11-03 03:29:31 +01:00
|
|
|
-- @see margins
|
2016-07-25 02:11:40 +02:00
|
|
|
|
|
|
|
--- The progressbar margins.
|
2019-11-29 06:33:44 +01:00
|
|
|
--
|
2016-07-25 02:11:40 +02:00
|
|
|
-- Note that if the `clip` is disabled, this allows the background to be smaller
|
|
|
|
-- than the bar.
|
2022-02-03 13:25:05 +01:00
|
|
|
-- @beautiful beautiful.progressbar_margins
|
2016-07-25 02:11:40 +02:00
|
|
|
-- @tparam[opt=0] (table|number|nil) margins A table for each side or a number
|
|
|
|
-- @tparam[opt=0] number margins.top
|
|
|
|
-- @tparam[opt=0] number margins.bottom
|
|
|
|
-- @tparam[opt=0] number margins.left
|
|
|
|
-- @tparam[opt=0] number margins.right
|
|
|
|
-- @see clip
|
2022-02-03 13:25:05 +01:00
|
|
|
-- @see beautiful.progressbar_paddings
|
|
|
|
-- @see wibox.container.margin
|
2016-07-25 02:11:40 +02:00
|
|
|
|
|
|
|
--- The progressbar padding.
|
2019-11-29 06:33:44 +01:00
|
|
|
--
|
2016-07-25 02:11:40 +02:00
|
|
|
-- Note that if the `clip` is disabled, this allows the bar to be taller
|
|
|
|
-- than the background.
|
|
|
|
-- @beautiful beautiful.progressbar_paddings
|
2022-02-03 13:25:05 +01:00
|
|
|
-- @tparam[opt=0] (table|number|nil) padding A table for each side or a number
|
|
|
|
-- @tparam[opt=0] number paddings.top
|
|
|
|
-- @tparam[opt=0] number paddings.bottom
|
|
|
|
-- @tparam[opt=0] number paddings.left
|
|
|
|
-- @tparam[opt=0] number paddings.right
|
2016-07-25 02:11:40 +02:00
|
|
|
-- @see clip
|
2022-02-03 13:25:05 +01:00
|
|
|
-- @see beautiful.progressbar_margins
|
|
|
|
|
2016-07-25 02:11:40 +02:00
|
|
|
|
2016-07-25 00:05:00 +02:00
|
|
|
local properties = { "border_color", "color" , "background_color",
|
|
|
|
"value" , "max_value" , "ticks",
|
|
|
|
"ticks_gap" , "ticks_size", "border_width",
|
|
|
|
"shape" , "bar_shape" , "bar_border_width",
|
2016-07-25 02:11:40 +02:00
|
|
|
"clip" , "margins" , "bar_border_color",
|
|
|
|
"paddings",
|
2016-07-25 00:05:00 +02:00
|
|
|
}
|
2016-05-23 07:22:35 +02:00
|
|
|
|
|
|
|
function progressbar.draw(pbar, _, cr, width, height)
|
2016-07-24 00:39:29 +02:00
|
|
|
local ticks_gap = pbar._private.ticks_gap or 1
|
|
|
|
local ticks_size = pbar._private.ticks_size or 4
|
2016-05-23 07:22:35 +02:00
|
|
|
|
|
|
|
-- We want one pixel wide lines
|
|
|
|
cr:set_line_width(1)
|
|
|
|
|
2016-07-24 00:39:29 +02:00
|
|
|
local max_value = pbar._private.max_value
|
2016-07-24 22:25:19 +02:00
|
|
|
|
|
|
|
local value = math.min(max_value, math.max(0, pbar._private.value))
|
|
|
|
|
2016-05-23 07:22:35 +02:00
|
|
|
if value >= 0 then
|
|
|
|
value = value / max_value
|
|
|
|
end
|
2016-07-25 00:05:00 +02:00
|
|
|
local border_width = pbar._private.border_width
|
|
|
|
or beautiful.progressbar_border_width or 0
|
|
|
|
|
|
|
|
local bcol = pbar._private.border_color or beautiful.progressbar_border_color
|
|
|
|
|
|
|
|
border_width = bcol and border_width or 0
|
|
|
|
|
|
|
|
local bg = pbar._private.background_color or
|
|
|
|
beautiful.progressbar_bg or "#ff0000aa"
|
|
|
|
|
|
|
|
local bg_width, bg_height = width, height
|
|
|
|
|
2016-07-25 02:11:40 +02:00
|
|
|
local clip = pbar._private.clip ~= false and beautiful.progressbar_clip ~= false
|
|
|
|
|
|
|
|
-- Apply the margins
|
|
|
|
local margin = pbar._private.margins or beautiful.progressbar_margins
|
|
|
|
|
|
|
|
if margin then
|
|
|
|
if type(margin) == "number" then
|
|
|
|
cr:translate(margin, margin)
|
|
|
|
bg_width, bg_height = bg_width - 2*margin, bg_height - 2*margin
|
|
|
|
else
|
|
|
|
cr:translate(margin.left or 0, margin.top or 0)
|
|
|
|
bg_height = bg_height -
|
|
|
|
(margin.top or 0) - (margin.bottom or 0)
|
|
|
|
bg_width = bg_width -
|
|
|
|
(margin.left or 0) - (margin.right or 0)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2016-07-25 00:05:00 +02:00
|
|
|
-- Draw the background shape
|
|
|
|
if border_width > 0 then
|
|
|
|
-- Cairo draw half of the border outside of the path area
|
|
|
|
cr:translate(border_width/2, border_width/2)
|
|
|
|
bg_width, bg_height = bg_width - border_width, bg_height - border_width
|
|
|
|
cr:set_line_width(border_width)
|
|
|
|
end
|
|
|
|
|
|
|
|
local background_shape = pbar._private.shape or
|
|
|
|
beautiful.progressbar_shape or shape.rectangle
|
|
|
|
|
|
|
|
background_shape(cr, bg_width, bg_height)
|
|
|
|
|
|
|
|
cr:set_source(color(bg))
|
|
|
|
|
2016-07-25 02:11:40 +02:00
|
|
|
local over_drawn_width = bg_width + border_width
|
|
|
|
local over_drawn_height = bg_height + border_width
|
|
|
|
|
2016-07-25 00:05:00 +02:00
|
|
|
if border_width > 0 then
|
|
|
|
cr:fill_preserve()
|
|
|
|
|
|
|
|
-- Draw the border
|
|
|
|
cr:set_source(color(bcol))
|
|
|
|
|
2016-05-23 07:22:35 +02:00
|
|
|
cr:stroke()
|
|
|
|
|
2016-07-25 02:11:40 +02:00
|
|
|
over_drawn_width = over_drawn_width - 2*border_width
|
|
|
|
over_drawn_height = over_drawn_height - 2*border_width
|
2016-07-25 00:05:00 +02:00
|
|
|
else
|
|
|
|
cr:fill()
|
2016-05-23 07:22:35 +02:00
|
|
|
end
|
|
|
|
|
2016-07-25 00:05:00 +02:00
|
|
|
-- Undo the translation
|
2016-07-25 02:11:40 +02:00
|
|
|
cr:translate(-border_width/2, -border_width/2)
|
2016-07-25 00:05:00 +02:00
|
|
|
|
|
|
|
-- Make sure the bar stay in the shape
|
2016-07-25 02:11:40 +02:00
|
|
|
if clip then
|
|
|
|
background_shape(cr, bg_width, bg_height)
|
2016-07-25 00:05:00 +02:00
|
|
|
cr:clip()
|
2016-07-25 02:11:40 +02:00
|
|
|
cr:translate(border_width, border_width)
|
|
|
|
else
|
|
|
|
-- Assume the background size is irrelevant to the bar itself
|
|
|
|
if type(margin) == "number" then
|
|
|
|
cr:translate(-margin, -margin)
|
|
|
|
else
|
|
|
|
cr:translate(-(margin.left or 0), -(margin.top or 0))
|
|
|
|
end
|
|
|
|
|
|
|
|
over_drawn_height = height
|
|
|
|
over_drawn_width = width
|
|
|
|
end
|
|
|
|
|
|
|
|
-- Apply the padding
|
|
|
|
local padding = pbar._private.paddings or beautiful.progressbar_paddings
|
|
|
|
|
|
|
|
if padding then
|
|
|
|
if type(padding) == "number" then
|
|
|
|
cr:translate(padding, padding)
|
|
|
|
over_drawn_height = over_drawn_height - 2*padding
|
|
|
|
over_drawn_width = over_drawn_width - 2*padding
|
|
|
|
else
|
|
|
|
cr:translate(padding.left or 0, padding.top or 0)
|
|
|
|
|
|
|
|
over_drawn_height = over_drawn_height -
|
|
|
|
(padding.top or 0) - (padding.bottom or 0)
|
|
|
|
over_drawn_width = over_drawn_width -
|
|
|
|
(padding.left or 0) - (padding.right or 0)
|
|
|
|
end
|
2016-07-25 00:05:00 +02:00
|
|
|
end
|
|
|
|
|
2016-07-25 02:11:40 +02:00
|
|
|
over_drawn_width = math.max(over_drawn_width , 0)
|
|
|
|
over_drawn_height = math.max(over_drawn_height, 0)
|
|
|
|
|
|
|
|
local rel_x = over_drawn_width * value
|
|
|
|
|
|
|
|
|
2016-07-25 00:05:00 +02:00
|
|
|
-- Draw the progressbar shape
|
|
|
|
|
2021-11-03 04:15:53 +01:00
|
|
|
local explicit_bar_shape = pbar._private.bar_shape or beautiful.progressbar_bar_shape
|
|
|
|
local bar_shape = explicit_bar_shape or shape.rectangle
|
2016-07-25 00:05:00 +02:00
|
|
|
|
|
|
|
local bar_border_width = pbar._private.bar_border_width or
|
|
|
|
beautiful.progressbar_bar_border_width or pbar._private.border_width or
|
|
|
|
beautiful.progressbar_border_width or 0
|
|
|
|
|
|
|
|
local bar_border_color = pbar._private.bar_border_color or
|
|
|
|
beautiful.progressbar_bar_border_color
|
|
|
|
|
|
|
|
bar_border_width = bar_border_color and bar_border_width or 0
|
|
|
|
|
|
|
|
over_drawn_width = over_drawn_width - bar_border_width
|
|
|
|
over_drawn_height = over_drawn_height - bar_border_width
|
|
|
|
cr:translate(bar_border_width/2, bar_border_width/2)
|
|
|
|
|
2021-11-03 04:15:53 +01:00
|
|
|
if pbar._private.ticks and explicit_bar_shape then
|
|
|
|
local tr_off = 0
|
2016-07-25 00:05:00 +02:00
|
|
|
|
2021-11-03 04:15:53 +01:00
|
|
|
-- Make all the shape and fill later in case the `color` is a gradient.
|
|
|
|
for _=0, width / (ticks_size+ticks_gap)-border_width do
|
|
|
|
bar_shape(cr, ticks_size - (bar_border_width/2), over_drawn_height)
|
|
|
|
cr:translate(ticks_size+ticks_gap, 0)
|
|
|
|
tr_off = tr_off + ticks_size+ticks_gap
|
|
|
|
end
|
|
|
|
|
|
|
|
-- Re-align the (potential) color gradients to 0,0.
|
|
|
|
cr:translate(-tr_off, 0)
|
|
|
|
|
|
|
|
if bar_border_width > 0 then
|
|
|
|
cr:set_source(color(bar_border_color))
|
|
|
|
cr:set_line_width(bar_border_width)
|
|
|
|
cr:stroke_preserve()
|
|
|
|
end
|
|
|
|
|
|
|
|
cr:set_source(color(pbar._private.color or beautiful.progressbar_fg or "#ff0000"))
|
2016-07-25 00:05:00 +02:00
|
|
|
|
|
|
|
cr:fill()
|
2021-11-03 04:15:53 +01:00
|
|
|
else
|
|
|
|
bar_shape(cr, rel_x, over_drawn_height)
|
|
|
|
|
|
|
|
cr:set_source(color(pbar._private.color or beautiful.progressbar_fg or "#ff0000"))
|
|
|
|
|
|
|
|
if bar_border_width > 0 then
|
|
|
|
cr:fill_preserve()
|
|
|
|
cr:set_source(color(bar_border_color))
|
|
|
|
cr:set_line_width(bar_border_width)
|
|
|
|
cr:stroke()
|
|
|
|
else
|
|
|
|
cr:fill()
|
|
|
|
end
|
2016-07-25 00:05:00 +02:00
|
|
|
end
|
2016-07-24 01:21:15 +02:00
|
|
|
|
2021-11-03 04:15:53 +01:00
|
|
|
-- Legacy "ticks" bars. It looks horrible, but to avoid breaking the
|
|
|
|
-- behavior, so be it.
|
|
|
|
if pbar._private.ticks and not explicit_bar_shape then
|
2016-07-24 01:21:15 +02:00
|
|
|
for i=0, width / (ticks_size+ticks_gap)-border_width do
|
|
|
|
local rel_offset = over_drawn_width / 1 - (ticks_size+ticks_gap) * i
|
2016-05-23 07:22:35 +02:00
|
|
|
|
2016-07-24 01:21:15 +02:00
|
|
|
if rel_offset <= rel_x then
|
|
|
|
cr:rectangle(rel_offset,
|
|
|
|
border_width,
|
|
|
|
ticks_gap,
|
|
|
|
over_drawn_height)
|
2016-05-23 07:22:35 +02:00
|
|
|
end
|
|
|
|
end
|
2016-07-24 00:39:29 +02:00
|
|
|
cr:set_source(color(pbar._private.background_color or "#000000aa"))
|
2016-05-23 07:22:35 +02:00
|
|
|
cr:fill()
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2016-07-24 01:21:15 +02:00
|
|
|
function progressbar:fit(_, width, height)
|
|
|
|
return width, height
|
2016-05-23 07:22:35 +02:00
|
|
|
end
|
|
|
|
|
|
|
|
--- Set the progressbar value.
|
2019-11-29 06:33:44 +01:00
|
|
|
--
|
2021-11-03 03:29:31 +01:00
|
|
|
-- By default, unless `max_value` is set, it is number between
|
|
|
|
-- zero and one.
|
|
|
|
--
|
|
|
|
-- @DOC_wibox_widget_progressbar_value_EXAMPLE@
|
|
|
|
--
|
2019-06-06 22:32:53 +02:00
|
|
|
-- @property value
|
2022-07-10 09:29:33 +02:00
|
|
|
-- @tparam[opt=0] number value The progress bar value.
|
2019-11-29 06:33:44 +01:00
|
|
|
-- @propemits true false
|
2021-11-03 03:29:31 +01:00
|
|
|
-- @see max_value
|
2019-06-06 22:32:53 +02:00
|
|
|
|
2016-05-23 07:22:35 +02:00
|
|
|
function progressbar:set_value(value)
|
|
|
|
value = value or 0
|
2016-07-24 22:25:19 +02:00
|
|
|
|
|
|
|
self._private.value = value
|
|
|
|
|
2016-05-23 07:22:35 +02:00
|
|
|
self:emit_signal("widget::redraw_needed")
|
|
|
|
return self
|
|
|
|
end
|
|
|
|
|
2016-07-24 22:25:19 +02:00
|
|
|
function progressbar:set_max_value(max_value)
|
|
|
|
|
|
|
|
self._private.max_value = max_value
|
|
|
|
|
|
|
|
self:emit_signal("widget::redraw_needed")
|
|
|
|
end
|
|
|
|
|
2016-05-23 07:22:35 +02:00
|
|
|
--- Set the progressbar height.
|
2019-11-29 06:33:44 +01:00
|
|
|
--
|
2016-10-05 00:24:43 +02:00
|
|
|
-- This method is deprecated. Use a `wibox.container.constraint` widget or
|
|
|
|
-- `forced_height`.
|
2019-11-29 06:33:44 +01:00
|
|
|
--
|
|
|
|
-- @tparam number height The height to set.
|
2016-07-24 00:32:37 +02:00
|
|
|
-- @deprecated set_height
|
2019-11-29 06:33:44 +01:00
|
|
|
-- @renamedin 4.0
|
2016-10-05 00:24:43 +02:00
|
|
|
function progressbar:set_height(height)
|
2017-03-15 06:08:40 +01:00
|
|
|
gdebug.deprecate("Use a `wibox.container.constraint` widget or `forced_height`", {deprecated_in=4})
|
2016-10-05 00:24:43 +02:00
|
|
|
self:set_forced_height(height)
|
2016-05-23 07:22:35 +02:00
|
|
|
end
|
|
|
|
|
|
|
|
--- Set the progressbar width.
|
2019-11-29 06:33:44 +01:00
|
|
|
--
|
2016-10-05 00:24:43 +02:00
|
|
|
-- This method is deprecated. Use a `wibox.container.constraint` widget or
|
|
|
|
-- `forced_width`.
|
2019-11-29 06:33:44 +01:00
|
|
|
--
|
|
|
|
-- @tparam number width The width to set.
|
2016-07-24 00:32:37 +02:00
|
|
|
-- @deprecated set_width
|
2019-11-29 06:33:44 +01:00
|
|
|
-- @renamedin 4.0
|
2016-10-05 00:24:43 +02:00
|
|
|
function progressbar:set_width(width)
|
2017-03-15 06:08:40 +01:00
|
|
|
gdebug.deprecate("Use a `wibox.container.constraint` widget or `forced_width`", {deprecated_in=4})
|
2016-10-05 00:24:43 +02:00
|
|
|
self:set_forced_width(width)
|
2016-05-23 07:22:35 +02:00
|
|
|
end
|
|
|
|
|
|
|
|
-- Build properties function
|
|
|
|
for _, prop in ipairs(properties) do
|
|
|
|
if not progressbar["set_" .. prop] then
|
|
|
|
progressbar["set_" .. prop] = function(pbar, value)
|
2016-07-24 00:39:29 +02:00
|
|
|
pbar._private[prop] = value
|
2016-05-23 07:22:35 +02:00
|
|
|
pbar:emit_signal("widget::redraw_needed")
|
2019-11-29 06:33:44 +01:00
|
|
|
pbar:emit_signal("property::"..prop, value)
|
2016-05-23 07:22:35 +02:00
|
|
|
return pbar
|
|
|
|
end
|
|
|
|
end
|
2019-11-29 06:33:44 +01:00
|
|
|
if not progressbar["get_"..prop] then
|
2019-12-01 09:35:26 +01:00
|
|
|
progressbar["get_" .. prop] = function(pbar)
|
2019-11-29 06:33:44 +01:00
|
|
|
return pbar._private[prop]
|
|
|
|
end
|
|
|
|
end
|
2016-05-23 07:22:35 +02:00
|
|
|
end
|
|
|
|
|
2016-07-24 00:32:37 +02:00
|
|
|
function progressbar:set_vertical(value) --luacheck: no unused_args
|
2017-03-15 06:08:40 +01:00
|
|
|
gdebug.deprecate("Use a `wibox.container.rotate` widget", {deprecated_in=4})
|
2016-07-24 00:32:37 +02:00
|
|
|
end
|
|
|
|
|
|
|
|
|
2016-05-23 07:22:35 +02:00
|
|
|
--- Create a progressbar widget.
|
2019-11-29 06:33:44 +01:00
|
|
|
--
|
|
|
|
-- @tparam table args Standard widget() arguments. You should add width and
|
|
|
|
-- height constructor parameters to set progressbar geometry.
|
2021-12-21 06:54:15 +01:00
|
|
|
-- @tparam[opt] number args.width The width.
|
|
|
|
-- @tparam[opt] number args.height The height.
|
|
|
|
-- @tparam[opt] gears.color args.border_color The progressbar border color.
|
|
|
|
-- @tparam[opt] number args.border_width The progressbar border width.
|
|
|
|
-- @tparam[opt] gears.color args.bar_border_color The progressbar inner border color.
|
|
|
|
-- @tparam[opt] number args.bar_border_width The progressbar inner border width.
|
|
|
|
-- @tparam[opt] gears.color args.color The progressbar foreground color.
|
|
|
|
-- @tparam[opt] gears.color args.background_color The progressbar background color.
|
|
|
|
-- @tparam[opt] gears.shape args.bar_shape The progressbar inner shape.
|
|
|
|
-- @tparam[opt] gears.shape args.shape The progressbar shape.
|
|
|
|
-- @tparam[opt] boolean args.clip Force the inner part (the bar) to fit in the background shape.
|
|
|
|
-- @tparam[opt] boolean args.ticks The progressbar to draw ticks.
|
|
|
|
-- @tparam[opt] number args.ticks_gap The progressbar ticks gap.
|
|
|
|
-- @tparam[opt] number args.ticks_size The progressbar ticks size.
|
|
|
|
-- @tparam[opt] number args.max_value The maximum value the progressbar should handle.
|
|
|
|
-- @tparam[opt] table|number args.margins The progressbar margins.
|
|
|
|
-- @tparam[opt] table|number args.paddings The progressbar padding.
|
|
|
|
-- @tparam[opt] number args.value Set the progressbar value.
|
2019-11-29 06:33:44 +01:00
|
|
|
-- @treturn wibox.widget.progressbar A progressbar widget.
|
2019-06-07 20:59:34 +02:00
|
|
|
-- @constructorfct wibox.widget.progressbar
|
2016-05-23 07:22:35 +02:00
|
|
|
function progressbar.new(args)
|
|
|
|
args = args or {}
|
|
|
|
|
2016-07-24 00:39:29 +02:00
|
|
|
local pbar = base.make_widget(nil, nil, {
|
|
|
|
enable_properties = true,
|
|
|
|
})
|
2016-05-23 07:22:35 +02:00
|
|
|
|
2016-07-24 00:39:29 +02:00
|
|
|
pbar._private.width = args.width or 100
|
|
|
|
pbar._private.height = args.height or 20
|
|
|
|
pbar._private.value = 0
|
|
|
|
pbar._private.max_value = 1
|
2016-05-23 07:22:35 +02:00
|
|
|
|
2017-03-08 21:18:33 +01:00
|
|
|
gtable.crush(pbar, progressbar, true)
|
2016-05-23 07:22:35 +02:00
|
|
|
|
|
|
|
return pbar
|
|
|
|
end
|
|
|
|
|
|
|
|
function progressbar.mt:__call(...)
|
|
|
|
return progressbar.new(...)
|
|
|
|
end
|
|
|
|
|
|
|
|
return setmetatable(progressbar, progressbar.mt)
|
|
|
|
|
|
|
|
-- vim: filetype=lua:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:textwidth=80
|