Last active
April 27, 2022 02:37
-
-
Save adamnejm/35993d4cfa4950070b59768e7098e644 to your computer and use it in GitHub Desktop.
AwesomeWM - slider.set_value_direct
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
-- Solution #1: Add `set_value_direct` only for the specified slider instance | |
local slider = wibox.widget.slider() -- ...or any other valid way to create the slider object | |
function slider:set_value_direct(value) | |
value = math.min(math.max(value, self:get_minimum()), self:get_maximum()) | |
if value ~= self._private.value then | |
self._private.value = value | |
self:emit_signal("property::value", value, true) -- Pass an additional `true` value to the callback | |
self:emit_signal("widget::redraw_needed") | |
end | |
end | |
-- Solution #2: Add `set_value_direct` to all sliders (created before and after the following code has ran) | |
local slider_mt = wibox.widget.slider.mt | |
function slider_mt:set_value_direct(value) | |
value = math.min(math.max(value, self:get_minimum()), self:get_maximum()) | |
if value ~= self._private.value then | |
self._private.value = value | |
self:emit_signal("property::value", value, true) -- Pass an additional `true` value to the callback | |
self:emit_signal("widget::redraw_needed") | |
end | |
end | |
------------------------------------------- | |
-- Usage: | |
local my_slider = wibox.widget.slider() -- Any slider instance | |
my_slider:connect_signal("property::value", function(value, direct) | |
if direct then | |
-- Value update was caused by calling `set_value_direct` aka "from code" | |
-- ... | |
else | |
-- Value update was caused by user input | |
-- ... | |
end | |
end) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment