From 76704b33e723ff3a3b6842c71a3fd64876bf31ce Mon Sep 17 00:00:00 2001 From: Emmanuel Lepage Vallee Date: Tue, 2 Nov 2021 20:15:53 -0700 Subject: [PATCH] progressbar: Make `ticks` and `bar_shape` compatible with each other. It looked horrible/buggy when combined. Now it looks like people would expect it to work. Another problem was the `bar_border_width` look when `ticks` was `true. --- lib/wibox/widget/progressbar.lua | 52 +++++-- .../wibox/widget/progressbar/ticks_size.lua | 48 +++---- .../wibox/widget/progressbar/ticks_size2.lua | 130 ++++++++++++++++++ 3 files changed, 195 insertions(+), 35 deletions(-) create mode 100644 tests/examples/wibox/widget/progressbar/ticks_size2.lua diff --git a/lib/wibox/widget/progressbar.lua b/lib/wibox/widget/progressbar.lua index 3100a0e04..05261466d 100644 --- a/lib/wibox/widget/progressbar.lua +++ b/lib/wibox/widget/progressbar.lua @@ -165,6 +165,10 @@ local progressbar = { mt = {} } -- -- @DOC_wibox_widget_progressbar_ticks_size_EXAMPLE@ -- +-- It is also possible to mix this feature with the `bar_shape` property: +-- +-- @DOC_wibox_widget_progressbar_ticks_size2_EXAMPLE@ +-- -- @property ticks_size -- @tparam[opt=4] number ticks_size -- @propemits true false @@ -428,8 +432,8 @@ function progressbar.draw(pbar, _, cr, width, height) -- Draw the progressbar shape - local bar_shape = pbar._private.bar_shape or - beautiful.progressbar_bar_shape or shape.rectangle + local explicit_bar_shape = pbar._private.bar_shape or beautiful.progressbar_bar_shape + local bar_shape = explicit_bar_shape or shape.rectangle local bar_border_width = pbar._private.bar_border_width or beautiful.progressbar_bar_border_width or pbar._private.border_width or @@ -444,20 +448,46 @@ function progressbar.draw(pbar, _, cr, width, height) over_drawn_height = over_drawn_height - bar_border_width cr:translate(bar_border_width/2, bar_border_width/2) - bar_shape(cr, rel_x, over_drawn_height) + if pbar._private.ticks and explicit_bar_shape then + local tr_off = 0 - cr:set_source(color(pbar._private.color or beautiful.progressbar_fg or "#ff0000")) + -- 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")) - 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() + 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 end - if pbar._private.ticks then + -- 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 for i=0, width / (ticks_size+ticks_gap)-border_width do local rel_offset = over_drawn_width / 1 - (ticks_size+ticks_gap) * i diff --git a/tests/examples/wibox/widget/progressbar/ticks_size.lua b/tests/examples/wibox/widget/progressbar/ticks_size.lua index 60c805902..a290f9bbd 100644 --- a/tests/examples/wibox/widget/progressbar/ticks_size.lua +++ b/tests/examples/wibox/widget/progressbar/ticks_size.lua @@ -1,4 +1,4 @@ ---DOC_GEN_IMAGE --DOC_HIDE_START +--DOC_GEN_IMAGE --DOC_HIDE_START --DOC_NO_USAGE local parent = ... local wibox = require("wibox") @@ -6,31 +6,31 @@ local l = wibox.layout.fixed.horizontal() l.spacing = 5 --DOC_HIDE_END -for _, size in ipairs { 0, 2, 4, 6 } do + for _, size in ipairs { 0, 2, 4, 6 } do - local pb = --DOC_HIDE - wibox.widget { - value = 0.33, - border_width = 2, - ticks = true, - ticks_size = size, - widget = wibox.widget.progressbar, - } + local pb = --DOC_HIDE + wibox.widget { + value = 0.33, + border_width = 2, + ticks = true, + ticks_size = size, + widget = wibox.widget.progressbar, + } - --DOC_HIDE_START - l:add(wibox.widget { - pb, - { - text = size, - align = "center", - widget = wibox.widget.textbox, - }, - forced_height = 30, - forced_width = 75, - layout = wibox.layout.stack - }) - --DOC_HIDE_END -end + --DOC_HIDE_START + l:add(wibox.widget { + pb, + { + text = size, + align = "center", + widget = wibox.widget.textbox, + }, + forced_height = 30, + forced_width = 75, + layout = wibox.layout.stack + }) + --DOC_HIDE_END + end parent:add(l) --DOC_HIDE diff --git a/tests/examples/wibox/widget/progressbar/ticks_size2.lua b/tests/examples/wibox/widget/progressbar/ticks_size2.lua new file mode 100644 index 000000000..a2c7ca999 --- /dev/null +++ b/tests/examples/wibox/widget/progressbar/ticks_size2.lua @@ -0,0 +1,130 @@ +--DOC_GEN_IMAGE --DOC_HIDE_START --DOC_NO_USAGE +local parent = ... +local wibox = require("wibox") +local gears = { shape = require("gears.shape") } +local beautiful = require("beautiful") + +local l1 = wibox.layout.fixed.horizontal() +local l2 = wibox.layout.fixed.horizontal() +local l3 = wibox.layout.fixed.horizontal() +l1.spacing = 5 +l2.spacing = 5 +l3.spacing = 5 + +--DOC_HIDE_END + for _, size in ipairs { 0, 2, 4, 6 } do + + -- Plane shapes. + local pb = --DOC_HIDE + wibox.widget { + value = 1, + border_width = 2, + ticks = true, + ticks_size = size, + ticks_gap = 3, + paddings = 2, + bar_shape = gears.shape.rounded_bar, + widget = wibox.widget.progressbar, + } + + --DOC_NEWLINE + + -- With a border for each shape. + local pb3 = --DOC_HIDE + wibox.widget { + value = 1, + border_width = 2, + ticks = true, + ticks_size = size, + ticks_gap = 3, + paddings = 2, + bar_shape = gears.shape.rounded_bar, + bor_border_width = 2, + bar_border_color = beautiful.border_color, + widget = wibox.widget.progressbar, + } + + --DOC_NEWLINE + + -- With a gradient. + local pb2 = --DOC_HIDE + wibox.widget { + color = { + type = "linear", + from = { 0 , 0 }, + to = { 65, 0 }, + stops = { + { 0 , "#0000ff" }, + { 0.75, "#0000ff" }, + { 1 , "#ff0000" } + } + }, + paddings = 2, + value = 1, + border_width = 2, + ticks = true, + ticks_size = size, + ticks_gap = 3, + bar_shape = gears.shape.rounded_bar, + widget = wibox.widget.progressbar, + } + + --DOC_HIDE_START + l1:add(wibox.widget { + pb, + { + text = size, + align = "center", + widget = wibox.widget.textbox, + }, + forced_height = 30, + forced_width = 75, + layout = wibox.layout.stack + }) + l2:add(wibox.widget { + pb3, + { + text = size, + align = "center", + widget = wibox.widget.textbox, + }, + forced_height = 30, + forced_width = 75, + layout = wibox.layout.stack + }) + + l3:add(wibox.widget { + pb2, + { + { + { + { + text = size, + align = "center", + widget = wibox.widget.textbox, + }, + margins = 4, + widget = wibox.container.margin, + }, + bg = "#ffffff", + shape = gears.shape.circle, + widget = wibox.container.background, + }, + align = "center", + valign = "center", + widget = wibox.container.place, + }, + forced_height = 30, + forced_width = 75, + layout = wibox.layout.stack + }) + + --DOC_HIDE_END + end + +parent:add(l1) --DOC_HIDE +parent:add(l2) --DOC_HIDE +parent:add(l3) --DOC_HIDE +parent.spacing = 10 --DOC_HIDE + +--DOC_HIDE vim: filetype=lua:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:textwidth=80