From 6b43233c4a3bf003acbeb728598758c568a760a1 Mon Sep 17 00:00:00 2001 From: chrox Date: Sun, 26 Jun 2016 01:27:23 +0800 Subject: [PATCH] fix #2092 And fix several other bugs introduced in #2028: 1. hint messages are back instead of the "Demo hint" 2. cursor now only presents in the focused inputbox in a multiinput dialog 3. moving cursor now works in multiinput dialog --- frontend/ui/widget/inputtext.lua | 26 ++++++++++++++----------- frontend/ui/widget/scrolltextwidget.lua | 8 ++++++++ frontend/ui/widget/textboxwidget.lua | 12 +++++++++++- 3 files changed, 34 insertions(+), 12 deletions(-) diff --git a/frontend/ui/widget/inputtext.lua b/frontend/ui/widget/inputtext.lua index 95361f6e6..3a702e804 100644 --- a/frontend/ui/widget/inputtext.lua +++ b/frontend/ui/widget/inputtext.lua @@ -50,15 +50,14 @@ if Device.isTouchDevice() then function InputText:onTapTextBox(arg, ges) if self.parent.onSwitchFocus then self.parent:onSwitchFocus(self) - else - local x = ges.pos.x - self.dimen.x - self.bordersize - self.padding - local y = ges.pos.y - self.dimen.y - self.bordersize - self.padding - if x > 0 and y > 0 then - self.charpos = self.text_widget:moveCursor(x, y) - UIManager:setDirty(self.parent, function() - return "ui", self[1].dimen - end) - end + end + local x = ges.pos.x - self.dimen.x - self.bordersize - self.padding + local y = ges.pos.y - self.dimen.y - self.bordersize - self.padding + if x > 0 and y > 0 then + self.charpos = self.text_widget:moveCursor(x, y) + UIManager:setDirty(self.parent, function() + return "ui", self[1].dimen + end) end end else @@ -84,13 +83,15 @@ function InputText:initTextBox(text) if self.text_type == "password" and show_text ~= "" then show_text = self.text:gsub("(.-).", function() return "*" end) show_text = show_text:gsub("(.)$", function() return self.text:sub(-1) end) + elseif show_text == "" then + show_text = self.hint end if self.scroll then self.text_widget = ScrollTextWidget:new{ text = show_text, charlist = self.charlist, charpos = self.charpos, - editable = true, + editable = self.focused, face = self.face, fgcolor = fgcolor, width = self.width, @@ -101,7 +102,7 @@ function InputText:initTextBox(text) text = show_text, charlist = self.charlist, charpos = self.charpos, - editable = true, + editable = self.focused, face = self.face, fgcolor = fgcolor, width = self.width, @@ -136,11 +137,13 @@ end function InputText:unfocus() self.focused = false + self.text_widget:unfocus() self[1].color = Blitbuffer.gray(0.5) end function InputText:focus() self.focused = true + self.text_widget:focus() self[1].color = Blitbuffer.COLOR_BLACK end @@ -185,6 +188,7 @@ function InputText:getText() end function InputText:setText(text) + self.charpos = nil self:initTextBox(text) UIManager:setDirty(self.parent, function() return "partial", self[1].dimen diff --git a/frontend/ui/widget/scrolltextwidget.lua b/frontend/ui/widget/scrolltextwidget.lua index 716b5c34e..5830dec89 100644 --- a/frontend/ui/widget/scrolltextwidget.lua +++ b/frontend/ui/widget/scrolltextwidget.lua @@ -72,6 +72,14 @@ function ScrollTextWidget:init() end end +function ScrollTextWidget:unfocus() + self.text_widget:unfocus() +end + +function ScrollTextWidget:focus() + self.text_widget:focus() +end + function ScrollTextWidget:scrollText(direction) if direction == 0 then return end local low, high diff --git a/frontend/ui/widget/textboxwidget.lua b/frontend/ui/widget/textboxwidget.lua index d28bfba61..18e222f70 100644 --- a/frontend/ui/widget/textboxwidget.lua +++ b/frontend/ui/widget/textboxwidget.lua @@ -61,6 +61,16 @@ function TextBoxWidget:init() self.dimen = Geom:new(self:getSize()) end +function TextBoxWidget:unfocus() + self.editable = false + self:init() +end + +function TextBoxWidget:focus() + self.editable = true + self:init() +end + -- Split `self.text` into `self.charlist` and evaluate the width of each char in it. function TextBoxWidget:_evalCharWidthList() if self.charlist == nil then @@ -77,7 +87,7 @@ end -- Split the text into logical lines to fit into the text box. function TextBoxWidget:_splitCharWidthList() self.vertical_string_list = { - {text = "Demo hint", offset = 1, width = 0} -- hint for empty string + {text = self.text, offset = 1, width = 0} -- hint for empty string } local idx = 1