[input] Invoke onChange() on text change.

This commit is contained in:
Lorenzo Cogotti 2022-10-02 17:54:00 +02:00
parent 4d2083d143
commit b06d56b945
1 changed files with 8 additions and 5 deletions

View File

@ -85,6 +85,8 @@ function Input:textinput(text)
self.text = table.concat {a, text, b} self.text = table.concat {a, text, b}
self.cursor = self.cursor + utf8.len(text) self.cursor = self.cursor + utf8.len(text)
self:onChange(self.text)
end end
end end
@ -100,21 +102,22 @@ function Input:keypressed(key, code, isrepeat)
end end
if self.candidate.length == 0 then if self.candidate.length == 0 then
if key == 'backspace' then if key == 'backspace' and self.cursor > 1 then
local a,b = split(self.text, self.cursor) local a,b = split(self.text, self.cursor)
self.text = table.concat { split(a, utf8.len(a)), b } self.text = table.concat { split(a, utf8.len(a)), b }
self.cursor = math.max(1, self.cursor-1) self.cursor = self.cursor-1
elseif key == 'delete' then self:onChange(self.text)
elseif key == 'delete' and self.cursor ~= utf8.len(self.text)+1 then
local a,b = split(self.text, self.cursor) local a,b = split(self.text, self.cursor)
local _,b = split(b, 2) local _,b = split(b, 2)
self.text = table.concat { a, b } self.text = table.concat { a, b }
self:onChange(self.text)
elseif key == 'left' then elseif key == 'left' then
self.cursor = math.max(0, self.cursor-1) self.cursor = math.max(1, self.cursor-1)
elseif key == 'right' then elseif key == 'right' then
self.cursor = math.min(utf8.len(self.text)+1, self.cursor+1) self.cursor = math.min(utf8.len(self.text)+1, self.cursor+1)
end end