mirror of
https://github.com/airstruck/luigi.git
synced 2025-12-19 02:16:43 +00:00
add keyboard focus
This commit is contained in:
3
luigi/widget/button.lua
Normal file
3
luigi/widget/button.lua
Normal file
@@ -0,0 +1,3 @@
|
||||
return function (self)
|
||||
|
||||
end
|
||||
@@ -1,6 +1,11 @@
|
||||
return function (self)
|
||||
|
||||
self.value = 0.5
|
||||
local function clamp (value)
|
||||
return value < 0 and 0 or value > 1 and 1 or value
|
||||
end
|
||||
|
||||
self:setValue(clamp(self.value or 0.5))
|
||||
self.step = self.step or 0.01
|
||||
self.flow = 'x' -- TODO: support vertical slider
|
||||
|
||||
local spacer = self:addChild()
|
||||
@@ -13,22 +18,32 @@ return function (self)
|
||||
}
|
||||
|
||||
local function unpress ()
|
||||
thumb.pressed = false
|
||||
thumb.pressed = false -- don't make the thumb appear pushed in
|
||||
return false -- don't press thumb on focused keyboard activation
|
||||
end
|
||||
|
||||
thumb:onPressStart(unpress)
|
||||
thumb:onPressEnter(unpress)
|
||||
|
||||
thumb:onKeyPress(function (event)
|
||||
local key = event.key
|
||||
if key == 'left' or key == 'down' then
|
||||
self:setValue(clamp(self.value - self.step))
|
||||
self:reshape()
|
||||
elseif event.key == 'right' or key == 'up' then
|
||||
self:setValue(clamp(self.value + self.step))
|
||||
self:reshape()
|
||||
end
|
||||
end)
|
||||
|
||||
local function press (event)
|
||||
local x1, y1, x2, y2 = self:getRectangle(true, true)
|
||||
self.value = (event.x - x1) / (x2 - x1)
|
||||
if self.value < 0 then self.value = 0 end
|
||||
if self.value > 1 then self.value = 1 end
|
||||
self:setValue(clamp((event.x - x1) / (x2 - x1)))
|
||||
self:reshape()
|
||||
self.layout:tryFocus(thumb)
|
||||
end
|
||||
|
||||
self:onPressStart(press)
|
||||
|
||||
self:onPressDrag(press)
|
||||
|
||||
self:onEnter(function (event)
|
||||
|
||||
@@ -14,6 +14,7 @@ return function (self)
|
||||
type = 'text',
|
||||
align = 'middle center',
|
||||
margin = 0,
|
||||
canFocus = false,
|
||||
}
|
||||
|
||||
local increment = self:addChild {
|
||||
@@ -31,7 +32,7 @@ return function (self)
|
||||
local function updateValue ()
|
||||
if not self.options then return end
|
||||
local option = self.options[self.index]
|
||||
self.value = option.value
|
||||
self:setValue(option.value)
|
||||
view.text = option.text
|
||||
end
|
||||
|
||||
|
||||
Reference in New Issue
Block a user