Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Slider Handle Widget #3699

Open
wants to merge 8 commits into
base: master
Choose a base branch
from

Conversation

thomascft
Copy link

I've added a handle_widget to wibox.widget.slider. I'm not sure how to do the tests, but I'd be willing to try writing them if I was given a direction. I'm not sure on the docs, so if I could get more details on the formatting of those that'd be great!

Slider Widget

@codecov
Copy link

codecov bot commented Sep 20, 2022

Codecov Report

Merging #3699 (f538671) into master (bde785e) will decrease coverage by 0.03%.
The diff coverage is 69.23%.

❗ Current head f538671 differs from pull request most recent head d1ad802. Consider uploading reports for the commit d1ad802 to get more accurate results

@@            Coverage Diff             @@
##           master    #3699      +/-   ##
==========================================
- Coverage   90.92%   90.89%   -0.04%     
==========================================
  Files         896      896              
  Lines       56630    56693      +63     
==========================================
+ Hits        51493    51533      +40     
- Misses       5137     5160      +23     
Flag Coverage Δ
gcov 90.89% <69.23%> (-0.04%) ⬇️
luacov 93.63% <69.23%> (-0.04%) ⬇️

Flags with carried forward coverage won't be shown. Click here to find out more.

Impacted Files Coverage Δ
lib/wibox/widget/slider.lua 82.08% <69.23%> (-7.37%) ⬇️
lib/awful/client.lua 77.77% <0.00%> (-0.23%) ⬇️
tests/examples/screen/template.lua 96.64% <0.00%> (-0.21%) ⬇️
lib/awful/titlebar.lua 88.16% <0.00%> (ø)
tests/test-input-binding.lua 99.50% <0.00%> (+2.00%) ⬆️

Copy link
Member

@Elv13 Elv13 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for this. It looks like an useful feature. I left a few minor comments.

For the tests, if you want, you can create some unit tests in spec/ and run them using make check-unit. You need busted to be installed. We don't have any for the slider right now. See spec/wibox/widget/slider_spec.lua for a minimal example.

But an alternative is to create a couple documentation examples based tests. They are not as fancy, but a lot easier to do and they provide much needed code examples for the users. You can copy paste an existing one and modify it. Pay attention to --DOC_HIDE (_START/_END) and __DOC_NEWLINE. They are preprocessed to help format the examples to remove the boilerplate. We are trying to have at least one of those for every properties.

The automatic bar for the tests right now is 91% overage. This should be rather easy here. If the margin type if gets mutualized in the setter, then a simple test should hit nearly 100% of the code.

Bonus points if you can implement bar_widget for the slider (and maybe progressbar too?). The code would probably be 95% identical and would help consistency across those modules.

lib/wibox/widget/slider.lua Outdated Show resolved Hide resolved
lib/wibox/widget/slider.lua Outdated Show resolved Hide resolved
lib/wibox/widget/slider.lua Show resolved Hide resolved
lib/wibox/widget/slider.lua Outdated Show resolved Hide resolved
@Elv13
Copy link
Member

Elv13 commented Sep 21, 2022

Oh, and also please install luacheck and run it, there's a few linting errors:

Checking lib/wibox/widget/slider.lua              4 warnings

    lib/wibox/widget/slider.lua:517:15: value assigned to variable 'x' is overwritten on line 543 before use
    lib/wibox/widget/slider.lua:517:18: value assigned to variable 'y' is overwritten on line 544 before use
    lib/wibox/widget/slider.lua:531:17: value assigned to variable 'handle_height' is unused
    lib/wibox/widget/slider.lua:536:17: value assigned to variable 'handle_height' is unused

Checking lib/wibox/widget/systray.lua             OK

@thomascft
Copy link
Author

thomascft commented Sep 21, 2022

I'll clean up my code and add the docs. I was considering doing a rewrite of the slider widget at some point rather than trying to edit what exists. I think the current widget is more focused on cario drawings, and a ground up redesign might be better. I'm open to anything though.

@Elv13
Copy link
Member

Elv13 commented Sep 21, 2022

I was considering doing a rewrite of the slider widget at some point rather than trying to edit what exists.

It would be pretty hard to merge (API compatibility and duplication requirements). IMHO extending the current one benefits more users. What kind of feature would such a rewrite improve upon/add? Not saying the current widget is incredible. It has a strange history.

I think the current widget is more focused on cario drawing

I think it's useful to provide both shape/cairo and sub-widgets in the same module. Most users are satisfied with shapes+colors, but might eventually want to upgrade to a declarative custom sub-widgets. Doing so without having to replace the code or adding a 3rd party widget library makes it a lot more straightforward. Having less overlapping modules also reduce the mental load when learning/experimenting. It doesn't make the internal implementation pretty, but I am usually fine with internal complexity if it makes the user experience better.

@thomascft
Copy link
Author

thomascft commented Sep 21, 2022

Understandable. For the bar widget, how exactly would you like that to be implemented functionality-wise? Would you like it to span the container, or only fill the active portion?

@thomascft
Copy link
Author

It would be pretty hard to merge (API compatibility and duplication requirements). IMHO extending the current one benefits more users. What kind of feature would such a rewrite improve upon/add? Not saying the current widget is incredible. It has a strange history.

I just find the code to be a bit messy, and a refactor would make it easier to understand/maintain. I figure a refactor is vastly different from a complete rewrite, I just think the chance to clean it up a bit at some point might be valuable.

@Elv13
Copy link
Member

Elv13 commented Sep 21, 2022

I just find the code to be a bit messy, and a refactor would make it easier to understand/maintain. I figure a refactor is vastly different from a complete rewrite, I just think the chance to clean it up a bit at some point might be valuable.

Refactors are fine. Just make sure the public API and behavior don't have unexpected change. Depending on what kind of scope you want in the PR, maybe you want the initial version merged first? That still requires the doc and tests/example/wibox/widget/slider file.

Understandable. For the bar widget, how exactly would you like that to be implemented functionality-wise? Would you like it to span the container, or only fill the active portion?

Behave the same as the bar_shape, but with a widget instead of a shape?

@thomascft
Copy link
Author

thomascft commented Sep 21, 2022

I'll work on the bar widget and the docs/tests. The refactor would definitely be a different pr.

@thomascft
Copy link
Author

I'm currently working on the margin setters. How would I call them on the widget init? I set them up to format the self._private.*_margins if a value isn't passed. I'd like to format so I ca always assume it's a table in the draw + layout functions.

@Elv13
Copy link
Member

Elv13 commented Sep 26, 2022

How would I call them on the widget init?

Move gtable.crush(ret._private, args or {}) 2 lines down and remove the ._private. This will force all properties to be created using the setters.

This is already used in various other widgets where setters have some business logic.

@thomascft
Copy link
Author

So how does the setter get called?

@Elv13
Copy link
Member

Elv13 commented Sep 26, 2022

So how does the setter get called?

AwesomeWM objects (both gears.object and capi.object) have meta code to make sure obj:set_foo("bar"), obj.foo = "bar" and obj["foo"] = "bar" are equivalent. gears.table.crush is a for loop that sets all values from one table to the other table. So it will indirectly call the setters automatically.

@thomascft
Copy link
Author

Ok, I added the bar_widget and tried to make the margin setter functions. Still a bit confused on documentation. I added what I understood, but I don't think I covered it all. Are the code examples supposed to be in the widget file?

lib/wibox/widget/slider.lua Outdated Show resolved Hide resolved
lib/wibox/widget/slider.lua Outdated Show resolved Hide resolved
lib/wibox/widget/slider.lua Outdated Show resolved Hide resolved
Comment on lines 498 to 500
(margins.left or 0) - (margins.right or 0)
handle_height = handle_height -
(margins.top or 0) - (margins.bottom or 0)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If we are going to use X times the margins.direction or 0 statement, we should definitely have local variables to handle these operations once for all.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not sure if I fully understand. What scope should the vars be in?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ok, just pushed some changes to the margin stuff. If you could take a look and see if it's what you were looking for.

lib/wibox/widget/slider.lua Outdated Show resolved Hide resolved
@Aire-One Aire-One marked this pull request as draft September 29, 2022 12:00
@Aire-One
Copy link
Member

Since the PR is still a work in progress, I'm converting it to a draft.

@Elv13
Copy link
Member

Elv13 commented Oct 1, 2022

Are the code examples supposed to be in the widget file?

--@DOC_wibox_widget_slider_handle_widget_EXAMPLE@ looks for a file called tests/examples/wibox/widget/slider/handle_widget.lua (it just converts the underscore to slashes). You can copy paste another file from that directory and modify the content. The build system will take care of prettyfying the code, generate the image and insert it in the doc. You need to have ldoc installed for this to work. It will also print some warnings if it doesn't like how the doc is written. If it doesn't then you are likely doing it right (or I need to add more warnings to my linting bot).

@thomascft
Copy link
Author

Will these also add object properties?

@Elv13
Copy link
Member

Elv13 commented Oct 1, 2022

Will these also add object properties?

They don't add anything, they are just code examples used to generate images and (optionally) code for the doc. This allows us to unit test our examples and have global templates to maintain the images. If you clean your build directory, copy paste some existing files from the same directory and rebuild awesome, it should just work. Then you can modify the code to better suit the property you are demonstrating.

@thomascft
Copy link
Author

I'm having and unreasonably difficult time getting handle_widget and bar_widget to show up in the docs.

-- The slider handle widget.
--
--@DOC_wibox_widget_slider_handle_widget_EXAMPLE@
--
-- @property handle_widget
-- @tparam widget|nil handle_widget
-- @propemits true false
-- @propbeautiful
-- @see wibox.widget

The file I'm opening to check is build/doc/widgets/wibox.widget.slider.html
What am I doing wrong?

@thomascft
Copy link
Author

I'm not 100% sure on the method documentation for set_handle_margins or set_bar_margins, I can only get the table parameter to render in the docs. If y'all could take a look at that, I think it's mostly ready.

@thomascft thomascft marked this pull request as ready for review October 3, 2022 03:28
@thomascft
Copy link
Author

Hold up, I forgot testing.

@Elv13
Copy link
Member

Elv13 commented Oct 3, 2022

The image is ./raw_images/AUTOGEN_wibox_widget_slider_handle_widget.svg. It was getting generated, but empty because nothing called the gen() function. I copy pasted the code back and it does things:

diff --git a/tests/examples/wibox/widget/slider/bar_widget.lua b/tests/examples/wibox/widget/slider/bar_widget.lua
index 96415c17f..d950b4e8b 100644
--- a/tests/examples/wibox/widget/slider/bar_widget.lua
+++ b/tests/examples/wibox/widget/slider/bar_widget.lua
@@ -20,4 +20,19 @@ local function gen(val)
     }
 end
 
+local l = wibox.layout {
+    gen(0), gen(3), gen(6), gen({
+                                top    = 12,
+                                bottom = 12,
+                                left   = 12,
+                                right  = 12,
+                            }),
+    forced_height = 30,
+    forced_width  = 400,
+    spacing       = 5,
+    layout        = wibox.layout.flex.horizontal
+}
+
+parent:add(l)
+
 --DOC_HIDE vim: filetype=lua:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:textwidth=80
diff --git a/tests/examples/wibox/widget/slider/handle_widget.lua b/tests/examples/wibox/widget/slider/handle_widget.lua
index 29928c1b7..05d0c7567 100644
--- a/tests/examples/wibox/widget/slider/handle_widget.lua
+++ b/tests/examples/wibox/widget/slider/handle_widget.lua
@@ -20,4 +20,19 @@ local function gen(val)
     }
 end
 
+local l = wibox.layout {
+    gen(0), gen(3), gen(6), gen({
+                                top    = 12,
+                                bottom = 12,
+                                left   = 12,
+                                right  = 12,
+                            }),
+    forced_height = 30,
+    forced_width  = 400,
+    spacing       = 5,
+    layout        = wibox.layout.flex.horizontal
+}
+
+parent:add(l)
+
 --DOC_HIDE vim: filetype=lua:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:textwidth=80

However, "does things" here means this:

2022-10-02 22:25:00 E: awesome: Error during a protected call: /home/lepagee/dev/awesome/lib/wibox/widget/slider.lua:643: attempt to perform arithmetic on a nil value (field 'left')
stack traceback:
/home/lepagee/dev/awesome/lib/wibox/widget/slider.lua:643: in function 'wibox.widget.slider.layout'
[C]: in function 'xpcall'
/home/lepagee/dev/awesome/lib/gears/protected_call.lua:41: in function </home/lepagee/dev/awesome/lib/gears/protected_call.lua:40>
(...tail calls...)
/home/lepagee/dev/awesome/lib/gears/cache.lua:42: in function 'gears.cache.get'
(...tail calls...)
/home/lepagee/dev/awesome/lib/wibox/hierarchy.lua:133: in upvalue 'hierarchy_update'
/home/lepagee/dev/awesome/lib/wibox/hierarchy.lua:141: in upvalue 'hierarchy_update'
/home/lepagee/dev/awesome/lib/wibox/hierarchy.lua:141: in upvalue 'hierarchy_update'
/home/lepagee/dev/awesome/lib/wibox/hierarchy.lua:230: in function 'wibox.hierarchy.update'
/home/lepagee/dev/awesome/lib/wibox/hierarchy.lua:215: in function 'wibox.hierarchy.new'
/home/lepagee/dev/awesome/lib/gears/surface.lua:229: in upvalue 'run_in_hierarchy'
/home/lepagee/dev/awesome/lib/gears/surface.lua:257: in function 'gears.surface.widget_to_svg'
/home/lepagee/dev/awesome/tests/examples/wibox/template.lua:62: in main chunk
[C]: in ?

This is what tests are for ;)

@@ -307,7 +323,47 @@ for prop in pairs(properties) do
end
end

--- Set the handle widget
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Those @method sections are not required. The @property is enough. Otherwise, it will document how to set the widget twice. We only document some legacy setters which had more than 1 parameters. Everything else is documented using the property mechanism.

@thomascft
Copy link
Author

The image is ./raw_images/AUTOGEN_wibox_widget_slider_handle_widget.svg. It was getting generated, but empty because nothing called the gen() function. I copy pasted the code back and it does things:

This is what tests are for ;)

I think the margins aren't being created by their setter functions. in the setter function I set the fallback of 0.

@Elv13
Copy link
Member

Elv13 commented Oct 3, 2022

I think the margins aren't being created by their setter functions. in the setter function I set the fallback of 0.

Yeah, it will need an for _, prop in ipairs {"handle_margins", "bar_margins"} do if not args[prop] then ret[prop] = 0 end end. I see you mentioned this earlier, I apologize for not connecting the dots.

@thomascft
Copy link
Author

that'll set it to 0, but it needs to build the table aswell.

@thomascft
Copy link
Author

Where should I put that?

I think the margins aren't being created by their setter functions. in the setter function I set the fallback of 0.

Yeah, it will need an for _, prop in ipairs {"handle_margins", "bar_margins"} do if not args[prop] then ret[prop] = 0 end end. I see you mentioned this earlier, I apologize for not connecting the dots.

The args is always nil

@Elv13
Copy link
Member

Elv13 commented Oct 5, 2022

The args is always nil

I fixed that in the diff below by adding args = args or {}

diff --git a/lib/wibox/widget/slider.lua b/lib/wibox/widget/slider.lua
index 344f91e20..7becf2e9e 100644
--- a/lib/wibox/widget/slider.lua
+++ b/lib/wibox/widget/slider.lua
@@ -40,7 +40,7 @@ local slider = {mt={}}
 --@DOC_wibox_widget_slider_handle_widget_EXAMPLE@
 --
 -- @property handle_widget
--- @tparam[opt=nil] widget|nil handle_widget The handle widget 
+-- @tparam[opt=nil] widget|nil handle_widget The handle widget
 
 --- The slider handle color.
 --
@@ -323,14 +323,6 @@ for prop in pairs(properties) do
     end
 end
 
---- Set the handle widget
---
--- @method set_handle_widget
--- @tparam[opt=nil] widget|nil bar_widget Set the handle widget
--- @noreturn
-
-
-
 function slider:set_handle_widget(value)
     local changed = self._private.handle_widget ~= value
         self._private.handle_widget = value
@@ -340,14 +332,6 @@ function slider:set_handle_widget(value)
     end
 end
 
---- Set the bar widget
---
--- @method set_bar_widget
--- @tparam[opt=nil] widget|nil bar_widget Set the widget that spans the active bar segment
--- @noreturn
-
-
-
 function slider:set_bar_widget(value)
     local changed = self._private.bar_widget ~= value
         self._private.bar_widget = value
@@ -357,13 +341,6 @@ function slider:set_bar_widget(value)
     end
 end
 
--- Add some validation to set_value
-
---- Set the slider's value
--- @tparam number number Set value to number
--- @method set_value
--- @noreturn
-
 function slider:set_value(value)
     value = math.min(value, self:get_maximum())
     value = math.max(value, self:get_minimum())
@@ -388,22 +365,9 @@ local function get_extremums(self)
     return min, max, interval
 end
 
--- TODO add single number param to margin setters
-
---- Set the slider's margins
---
--- @tparam[opt={}] table|number|nil handle_margins
--- @tparam[opt=0] number handle_margins.left
--- @tparam[opt=0] number handle_margins.right
--- @tparam[opt=0] number handle_margins.top
--- @tparam[opt=0] number handle_margins.bottom
--- @propemits true false
--- @propbeautiful
--- @method set_handle_margins
--- @noreturn
-
 function slider:set_handle_margins(value)
-    local value = value or 0
+    value = value or 0
+
     if type(value) == "number" then
         value = {
             top    = value,
@@ -419,19 +383,9 @@ function slider:set_handle_margins(value)
     self:emit_signal("widget::layout_changed")
 end
 
---- Set the bar's margins
---
--- @tparam[opt={}] table|number|nil bar_margins
--- @tparam[opt=0] number bar_margins.left
--- @tparam[opt=0] number bar_margins.right
--- @tparam[opt=0] number bar_margins.top
--- @tparam[opt=0] number bar_margins.bottom
--- @propemits true false
--- @propbeautiful
--- @method set_bar_margins
--- @noreturn
-
 function slider:set_bar_margins(value)
+    value = value or 0
+
     if type(value) == "number" then
         value = {
             top    = value,
@@ -625,13 +579,15 @@ function slider:layout(context, width, height)
 
     local value = self._private.value or self._private.min or 0
     local min, _, interval = get_extremums(self)
-    local bar_height, bar_width = self._private.bar_height or height, width
+    local bar_height = self._private.bar_height or height
     local handle_height, handle_width = height, self._private.handle_width
         or beautiful.slider_handle_width
         or width
 
     if bar_widget then
-        bar_width = (((value - min) / interval) * width + handle_width / 2 - ((value - min) / interval ) * handle_width)
+        local bar_width = (
+            ((value - min) / interval) * width + handle_width / 2
+                - ((value - min) / interval ) * handle_width)
         local w, h = base.fit_widget(self, context, bar_widget, bar_width, bar_height)
         local margins = self._private.bar_margins
             or beautiful.slider_bar_margins
@@ -639,9 +595,7 @@ function slider:layout(context, width, height)
         local x_offset, y_offset = 0, 0
 
         if margins then
-                x_offset, y_offset = margins.left, margins.top
-                bar_width       = bar_width -
-                    (margins.left) - (margins.right)
+            x_offset, y_offset = margins.left, margins.top
         end
         local x, y = 0 + x_offset, 0 + y_offset
         table.insert(result, base.place_widget_at(bar_widget, x, y, w, h))
@@ -725,6 +679,8 @@ end
 -- @tparam[opt] number args.minimum The slider minimum value.
 -- @tparam[opt] number args.maximum The slider maximum value.
 local function new(args)
+    args = args or {}
+
     local ret = base.make_widget(nil, nil, {
         enable_properties = true,
     })
@@ -735,6 +691,13 @@ local function new(args)
 
     ret:connect_signal("button::press", mouse_press)
 
+    -- Initialize the tables.
+    for _, prop in ipairs {"handle_margins", "bar_margins"} do
+        if not args[prop] then
+           ret[prop] = 0
+        end
+    end
+
     return ret
 end
 

With that, the handle_widget image renders.

I also, there's a little helpful command called luacheck which you can run before sending code to GitHub, it will catch a few warnings which would have failed the CI. There was just a couple, I fixed them in the diff above. Do you want me to push this patch to the PR?

@thomascft
Copy link
Author

Sure

@thomascft
Copy link
Author

The args is always nil

I fixed that in the diff below by adding args = args or {}

diff --git a/lib/wibox/widget/slider.lua b/lib/wibox/widget/slider.lua
index 344f91e20..7becf2e9e 100644
--- a/lib/wibox/widget/slider.lua
+++ b/lib/wibox/widget/slider.lua
@@ -40,7 +40,7 @@ local slider = {mt={}}
 --@DOC_wibox_widget_slider_handle_widget_EXAMPLE@
 --
 -- @property handle_widget
--- @tparam[opt=nil] widget|nil handle_widget The handle widget 
+-- @tparam[opt=nil] widget|nil handle_widget The handle widget
 
 --- The slider handle color.
 --
@@ -323,14 +323,6 @@ for prop in pairs(properties) do
     end
 end
 
---- Set the handle widget
---
--- @method set_handle_widget
--- @tparam[opt=nil] widget|nil bar_widget Set the handle widget
--- @noreturn
-
-
-
 function slider:set_handle_widget(value)
     local changed = self._private.handle_widget ~= value
         self._private.handle_widget = value
@@ -340,14 +332,6 @@ function slider:set_handle_widget(value)
     end
 end
 
---- Set the bar widget
---
--- @method set_bar_widget
--- @tparam[opt=nil] widget|nil bar_widget Set the widget that spans the active bar segment
--- @noreturn
-
-
-
 function slider:set_bar_widget(value)
     local changed = self._private.bar_widget ~= value
         self._private.bar_widget = value
@@ -357,13 +341,6 @@ function slider:set_bar_widget(value)
     end
 end
 
--- Add some validation to set_value
-
---- Set the slider's value
--- @tparam number number Set value to number
--- @method set_value
--- @noreturn
-
 function slider:set_value(value)
     value = math.min(value, self:get_maximum())
     value = math.max(value, self:get_minimum())
@@ -388,22 +365,9 @@ local function get_extremums(self)
     return min, max, interval
 end
 
--- TODO add single number param to margin setters
-
---- Set the slider's margins
---
--- @tparam[opt={}] table|number|nil handle_margins
--- @tparam[opt=0] number handle_margins.left
--- @tparam[opt=0] number handle_margins.right
--- @tparam[opt=0] number handle_margins.top
--- @tparam[opt=0] number handle_margins.bottom
--- @propemits true false
--- @propbeautiful
--- @method set_handle_margins
--- @noreturn
-
 function slider:set_handle_margins(value)
-    local value = value or 0
+    value = value or 0
+
     if type(value) == "number" then
         value = {
             top    = value,
@@ -419,19 +383,9 @@ function slider:set_handle_margins(value)
     self:emit_signal("widget::layout_changed")
 end
 
---- Set the bar's margins
---
--- @tparam[opt={}] table|number|nil bar_margins
--- @tparam[opt=0] number bar_margins.left
--- @tparam[opt=0] number bar_margins.right
--- @tparam[opt=0] number bar_margins.top
--- @tparam[opt=0] number bar_margins.bottom
--- @propemits true false
--- @propbeautiful
--- @method set_bar_margins
--- @noreturn
-
 function slider:set_bar_margins(value)
+    value = value or 0
+
     if type(value) == "number" then
         value = {
             top    = value,
@@ -625,13 +579,15 @@ function slider:layout(context, width, height)
 
     local value = self._private.value or self._private.min or 0
     local min, _, interval = get_extremums(self)
-    local bar_height, bar_width = self._private.bar_height or height, width
+    local bar_height = self._private.bar_height or height
     local handle_height, handle_width = height, self._private.handle_width
         or beautiful.slider_handle_width
         or width
 
     if bar_widget then
-        bar_width = (((value - min) / interval) * width + handle_width / 2 - ((value - min) / interval ) * handle_width)
+        local bar_width = (
+            ((value - min) / interval) * width + handle_width / 2
+                - ((value - min) / interval ) * handle_width)
         local w, h = base.fit_widget(self, context, bar_widget, bar_width, bar_height)
         local margins = self._private.bar_margins
             or beautiful.slider_bar_margins
@@ -639,9 +595,7 @@ function slider:layout(context, width, height)
         local x_offset, y_offset = 0, 0
 
         if margins then
-                x_offset, y_offset = margins.left, margins.top
-                bar_width       = bar_width -
-                    (margins.left) - (margins.right)
+            x_offset, y_offset = margins.left, margins.top
         end
         local x, y = 0 + x_offset, 0 + y_offset
         table.insert(result, base.place_widget_at(bar_widget, x, y, w, h))
@@ -725,6 +679,8 @@ end
 -- @tparam[opt] number args.minimum The slider minimum value.
 -- @tparam[opt] number args.maximum The slider maximum value.
 local function new(args)
+    args = args or {}
+
     local ret = base.make_widget(nil, nil, {
         enable_properties = true,
     })
@@ -735,6 +691,13 @@ local function new(args)
 
     ret:connect_signal("button::press", mouse_press)
 
+    -- Initialize the tables.
+    for _, prop in ipairs {"handle_margins", "bar_margins"} do
+        if not args[prop] then
+           ret[prop] = 0
+        end
+    end
+
     return ret
 end
 

With that, the handle_widget image renders.

I also, there's a little helpful command called luacheck which you can run before sending code to GitHub, it will catch a few warnings which would have failed the CI. There was just a couple, I fixed them in the diff above. Do you want me to push this patch to the PR?

Is there anything else you'd like me to get working before the merge?

@Elv13
Copy link
Member

Elv13 commented Oct 21, 2022

No, I need to circle back to this. But we have another PR in progress for a generc template system. It would be nice to have this merged first so we can use it in the slider. This would allow to have the bar widget to display the slider value inline (optionally) and that kind of stuff.

@thomascft
Copy link
Author

thomascft commented Oct 21, 2022

Good to know, I'll watch for that

@Elv13 Elv13 added this to the v4.4 milestone Dec 31, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants