InputDialog: add 'Go to line' button (#7673)

pull/7700/head
hius07 3 years ago committed by GitHub
parent e6027313e9
commit 834feef8cd
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

@ -266,6 +266,9 @@ function InputContainer:onGesture(ev)
end
end
end
if self.stop_events_propagation then
return true
end
end
function InputContainer:onInput(input, ignore_first_hold_release)

@ -115,6 +115,7 @@ local UIManager = require("ui/uimanager")
local VerticalGroup = require("ui/widget/verticalgroup")
local VerticalSpan = require("ui/widget/verticalspan")
local Screen = Device.screen
local T = require("ffi/util").template
local _ = require("gettext")
local InputDialog = InputContainer:new{
@ -723,6 +724,46 @@ function InputDialog:_addScrollButtons(nav_bar)
end,
})
end
-- Add a button to go to the line by its number in the file
if self.fullscreen then
table.insert(row, {
text = _("Go"),
callback = function()
local cur_line_num, last_line_num = self._input_widget:getLineNums()
local input_dialog
input_dialog = InputDialog:new{
title = _("Enter line number"),
-- @translators %1 is the current line number, %2 is the last line number
input_hint = T(_("%1 (1 - %2)"), cur_line_num, last_line_num),
input_type = "number",
stop_events_propagation = true, -- avoid interactions with upper InputDialog
buttons = {
{
{
text = _("Cancel"),
callback = function()
UIManager:close(input_dialog)
end,
},
{
text = _("Go to line"),
is_enter_default = true,
callback = function()
local new_line_num = tonumber(input_dialog:getInputText())
if new_line_num and new_line_num >= 1 and new_line_num <= last_line_num then
UIManager:close(input_dialog)
self._input_widget:moveCursorToCharPos(self._input_widget:getLineCharPos(new_line_num))
end
end,
},
},
},
}
UIManager:show(input_dialog)
input_dialog:onShowKeyboard()
end,
})
end
table.insert(row, {
text = "",
id = "top",

@ -501,6 +501,38 @@ function InputText:getKeyboardDimen()
return self.keyboard.dimen
end
-- calculate current and last (original) line numbers
function InputText:getLineNums()
local cur_line_num, last_line_num = 1, 1
for i = 1, #self.charlist do
if self.text_widget.charlist[i] == "\n" then
if i < self.charpos then
cur_line_num = cur_line_num + 1
end
last_line_num = last_line_num + 1
end
end
return cur_line_num, last_line_num
end
-- calculate charpos for the beginning of (original) line
function InputText:getLineCharPos(line_num)
local char_pos = 1
if line_num > 1 then
local j = 1
for i = 1, #self.charlist do
if self.charlist[i] == "\n" then
j = j + 1
if j == line_num then
char_pos = i + 1
break
end
end
end
end
return char_pos
end
function InputText:addChars(chars)
if not chars then
-- VirtualKeyboard:addChar(key) gave us 'nil' once (?!)
@ -578,6 +610,11 @@ function InputText:goToEnd()
self.text_widget:moveCursorToCharPos(0)
end
function InputText:moveCursorToCharPos(char_pos)
self.text_widget:moveCursorToCharPos(char_pos)
self.charpos, self.top_line_num = self.text_widget:getCharPos()
end
function InputText:upLine()
self.text_widget:moveCursorUp()
self.charpos, self.top_line_num = self.text_widget:getCharPos()

Loading…
Cancel
Save