mirror of
https://github.com/koreader/koreader.git
synced 2025-08-10 00:52:38 +00:00
InputDialog: add :addWidget(), use it for checkboxes (#8168)
Also: Text editor now closes its keyboard when calling Find and Go to line (which open their own keyboards) to avoid conflicts between multiple keyboards.
This commit is contained in:
@@ -4,8 +4,6 @@ local CenterContainer = require("ui/widget/container/centercontainer")
|
||||
local DocumentRegistry = require("document/documentregistry")
|
||||
local FileChooser = require("ui/widget/filechooser")
|
||||
local Font = require("ui/font")
|
||||
local HorizontalGroup = require("ui/widget/horizontalgroup")
|
||||
local HorizontalSpan = require("ui/widget/horizontalspan")
|
||||
local InfoMessage = require("ui/widget/infomessage")
|
||||
local InputContainer = require("ui/widget/container/inputcontainer")
|
||||
local InputDialog = require("ui/widget/inputdialog")
|
||||
@@ -13,7 +11,6 @@ local Menu = require("ui/widget/menu")
|
||||
local Size = require("ui/size")
|
||||
local UIManager = require("ui/uimanager")
|
||||
local Utf8Proc = require("ffi/utf8proc")
|
||||
local VerticalGroup = require("ui/widget/verticalgroup")
|
||||
local lfs = require("libs/libkoreader-lfs")
|
||||
local BaseUtil = require("ffi/util")
|
||||
local util = require("util")
|
||||
@@ -157,7 +154,7 @@ function FileSearcher:onShowFileSearch(search_string)
|
||||
},
|
||||
},
|
||||
}
|
||||
-- checkbox
|
||||
|
||||
self.check_button_case = CheckButton:new{
|
||||
text = _("Case sensitive"),
|
||||
checked = self.case_sensitive,
|
||||
@@ -168,19 +165,7 @@ function FileSearcher:onShowFileSearch(search_string)
|
||||
self.case_sensitive = self.check_button_case.checked
|
||||
end,
|
||||
}
|
||||
|
||||
local checkbox_shift = math.floor((self.search_dialog.width - self.search_dialog._input_widget.width) / 2 + 0.5)
|
||||
local check_buttons = HorizontalGroup:new{
|
||||
HorizontalSpan:new{width = checkbox_shift},
|
||||
VerticalGroup:new{
|
||||
align = "left",
|
||||
self.check_button_case,
|
||||
},
|
||||
}
|
||||
|
||||
-- insert check buttons before the regular buttons
|
||||
local nb_elements = #self.search_dialog.dialog_frame[1]
|
||||
table.insert(self.search_dialog.dialog_frame[1], nb_elements-1, check_buttons)
|
||||
self.search_dialog:addWidget(self.check_button_case)
|
||||
|
||||
UIManager:show(self.search_dialog)
|
||||
self.search_dialog:onShowKeyboard()
|
||||
|
||||
@@ -2,14 +2,11 @@ local BD = require("ui/bidi")
|
||||
local ButtonDialog = require("ui/widget/buttondialog")
|
||||
local CheckButton = require("ui/widget/checkbutton")
|
||||
local Device = require("device")
|
||||
local HorizontalGroup = require("ui/widget/horizontalgroup")
|
||||
local HorizontalSpan = require("ui/widget/horizontalspan")
|
||||
local InfoMessage = require("ui/widget/infomessage")
|
||||
local InputContainer = require("ui/widget/container/inputcontainer")
|
||||
local InputDialog = require("ui/widget/inputdialog")
|
||||
local Notification = require("ui/widget/notification")
|
||||
local UIManager = require("ui/uimanager")
|
||||
local VerticalGroup = require("ui/widget/verticalgroup")
|
||||
local logger = require("logger")
|
||||
local _ = require("gettext")
|
||||
local T = require("ffi/util").template
|
||||
@@ -135,7 +132,6 @@ function ReaderSearch:onShowFulltextSearchInput()
|
||||
},
|
||||
}
|
||||
|
||||
-- checkboxes
|
||||
self.check_button_case = CheckButton:new{
|
||||
text = _("Case sensitive"),
|
||||
checked = not self.case_insensitive,
|
||||
@@ -145,6 +141,7 @@ function ReaderSearch:onShowFulltextSearchInput()
|
||||
self.check_button_case:toggleCheck()
|
||||
end,
|
||||
}
|
||||
self.input_dialog:addWidget(self.check_button_case)
|
||||
self.check_button_regex = CheckButton:new{
|
||||
text = _("Regular expression (hold for help)"),
|
||||
checked = self.use_regex,
|
||||
@@ -157,20 +154,7 @@ function ReaderSearch:onShowFulltextSearchInput()
|
||||
UIManager:show(InfoMessage:new{ text = help_text })
|
||||
end,
|
||||
}
|
||||
|
||||
local checkbox_shift = math.floor((self.input_dialog.width - self.input_dialog._input_widget.width) / 2 + 0.5)
|
||||
local check_buttons = HorizontalGroup:new{
|
||||
HorizontalSpan:new{width = checkbox_shift},
|
||||
VerticalGroup:new{
|
||||
align = "left",
|
||||
self.check_button_case,
|
||||
not self.ui.document.info.has_pages and self.check_button_regex or nil,
|
||||
},
|
||||
}
|
||||
|
||||
-- insert check buttons before the regular buttons
|
||||
local nb_elements = #self.input_dialog.dialog_frame[1]
|
||||
table.insert(self.input_dialog.dialog_frame[1], nb_elements-1, check_buttons)
|
||||
self.input_dialog:addWidget(self.check_button_regex)
|
||||
|
||||
UIManager:show(self.input_dialog)
|
||||
self.input_dialog:onShowKeyboard()
|
||||
|
||||
@@ -1,10 +1,7 @@
|
||||
local CheckButton = require("ui/widget/checkbutton")
|
||||
local FFIUtil = require("ffi/util")
|
||||
local HorizontalGroup = require("ui/widget/horizontalgroup")
|
||||
local HorizontalSpan = require("ui/widget/horizontalspan")
|
||||
local Language = require("ui/language")
|
||||
local UIManager = require("ui/uimanager")
|
||||
local VerticalGroup = require("ui/widget/verticalgroup")
|
||||
local VirtualKeyboard = require("ui/widget/virtualkeyboard")
|
||||
local util = require("util")
|
||||
local _ = require("gettext")
|
||||
@@ -62,7 +59,6 @@ local sub_item_table = {
|
||||
},
|
||||
}
|
||||
|
||||
-- checkboxes
|
||||
check_button_bold = CheckButton:new{
|
||||
text = _("in bold"),
|
||||
checked = G_reader_settings:isTrue("keyboard_key_bold"),
|
||||
@@ -72,6 +68,7 @@ local sub_item_table = {
|
||||
check_button_bold:toggleCheck()
|
||||
end,
|
||||
}
|
||||
input_dialog:addWidget(check_button_bold)
|
||||
check_button_border = CheckButton:new{
|
||||
text = _("with border"),
|
||||
checked = G_reader_settings:nilOrTrue("keyboard_key_border"),
|
||||
@@ -81,6 +78,7 @@ local sub_item_table = {
|
||||
check_button_border:toggleCheck()
|
||||
end,
|
||||
}
|
||||
input_dialog:addWidget(check_button_border)
|
||||
check_button_compact = CheckButton:new{
|
||||
text = _("compact"),
|
||||
checked = G_reader_settings:isTrue("keyboard_key_compact"),
|
||||
@@ -90,21 +88,7 @@ local sub_item_table = {
|
||||
check_button_compact:toggleCheck()
|
||||
end,
|
||||
}
|
||||
|
||||
local checkbox_shift = math.floor((input_dialog.width - input_dialog._input_widget.width) / 2 + 0.5)
|
||||
local check_buttons = HorizontalGroup:new{
|
||||
HorizontalSpan:new{width = checkbox_shift},
|
||||
VerticalGroup:new{
|
||||
align = "left",
|
||||
check_button_bold,
|
||||
check_button_border,
|
||||
check_button_compact,
|
||||
},
|
||||
}
|
||||
|
||||
-- insert check buttons before the regular buttons
|
||||
local nb_elements = #input_dialog.dialog_frame[1]
|
||||
table.insert(input_dialog.dialog_frame[1], nb_elements-1, check_buttons)
|
||||
input_dialog:addWidget(check_button_compact)
|
||||
|
||||
UIManager:show(input_dialog)
|
||||
input_dialog:onShowKeyboard()
|
||||
|
||||
@@ -103,8 +103,6 @@ local Font = require("ui/font")
|
||||
local FrameContainer = require("ui/widget/container/framecontainer")
|
||||
local Geom = require("ui/geometry")
|
||||
local GestureRange = require("ui/gesturerange")
|
||||
local HorizontalGroup = require("ui/widget/horizontalgroup")
|
||||
local HorizontalSpan = require("ui/widget/horizontalspan")
|
||||
local InfoMessage = require("ui/widget/infomessage")
|
||||
local InputContainer = require("ui/widget/container/inputcontainer")
|
||||
local InputText = require("ui/widget/inputtext")
|
||||
@@ -420,6 +418,25 @@ function InputDialog:init()
|
||||
end
|
||||
end
|
||||
|
||||
-- Combine all
|
||||
self.vgroup = VerticalGroup:new{
|
||||
align = "left",
|
||||
self.title_widget,
|
||||
self.title_bar,
|
||||
self.description_widget,
|
||||
vspan_before_input_text,
|
||||
CenterContainer:new{
|
||||
dimen = Geom:new{
|
||||
w = self.width,
|
||||
h = self._input_widget:getSize().h,
|
||||
},
|
||||
self._input_widget,
|
||||
},
|
||||
-- added widgets may be inserted here
|
||||
vspan_after_input_text,
|
||||
buttons_container,
|
||||
}
|
||||
|
||||
-- Final widget
|
||||
self.dialog_frame = FrameContainer:new{
|
||||
radius = self.fullscreen and 0 or Size.radius.window,
|
||||
@@ -427,22 +444,7 @@ function InputDialog:init()
|
||||
margin = 0,
|
||||
bordersize = self.border_size,
|
||||
background = Blitbuffer.COLOR_WHITE,
|
||||
VerticalGroup:new{
|
||||
align = "left",
|
||||
self.title_widget,
|
||||
self.title_bar,
|
||||
self.description_widget,
|
||||
vspan_before_input_text,
|
||||
CenterContainer:new{
|
||||
dimen = Geom:new{
|
||||
w = self.width,
|
||||
h = self._input_widget:getSize().h,
|
||||
},
|
||||
self._input_widget,
|
||||
},
|
||||
vspan_after_input_text,
|
||||
buttons_container,
|
||||
}
|
||||
self.vgroup,
|
||||
}
|
||||
local frame = self.dialog_frame
|
||||
if self.is_movable then
|
||||
@@ -470,6 +472,29 @@ function InputDialog:init()
|
||||
},
|
||||
}
|
||||
end
|
||||
if self._added_widgets then
|
||||
for _, widget in ipairs(self._added_widgets) do
|
||||
self:addWidget(widget, true)
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
function InputDialog:addWidget(widget, re_init)
|
||||
if not re_init then -- backup widget for re-init
|
||||
widget = CenterContainer:new{
|
||||
dimen = Geom:new{
|
||||
w = self.width,
|
||||
h = widget:getSize().h,
|
||||
},
|
||||
widget,
|
||||
}
|
||||
if not self._added_widgets then
|
||||
self._added_widgets = {}
|
||||
end
|
||||
table.insert(self._added_widgets, widget)
|
||||
end
|
||||
-- insert widget before the bottom buttons and their previous vspan
|
||||
table.insert(self.vgroup, #self.vgroup-1, widget)
|
||||
end
|
||||
|
||||
function InputDialog:onTap()
|
||||
@@ -528,10 +553,28 @@ function InputDialog:onShowKeyboard(ignore_first_hold_release)
|
||||
end
|
||||
end
|
||||
|
||||
function InputDialog:toggleKeyboard(force_hide)
|
||||
if force_hide and self.keyboard_hidden then return end
|
||||
self.keyboard_hidden = not self.keyboard_hidden
|
||||
self.input = self:getInputText() -- re-init with up-to-date text
|
||||
self:onClose() -- will close keyboard and save view position
|
||||
self:free()
|
||||
self:init()
|
||||
if not self.keyboard_hidden then
|
||||
self:onShowKeyboard()
|
||||
end
|
||||
end
|
||||
|
||||
function InputDialog:onKeyboardHeightChanged()
|
||||
self.input = self:getInputText() -- re-init with up-to-date text
|
||||
self:onClose() -- will close keyboard and save view position
|
||||
self._input_widget:onCloseWidget() -- proper cleanup of InputText and its keyboard
|
||||
if self._added_widgets then
|
||||
-- prevent these externally added widgets from being freed as :init() will re-add them
|
||||
for i = 1, #self._added_widgets do
|
||||
table.remove(self.vgroup, #self.vgroup-2)
|
||||
end
|
||||
end
|
||||
self:free()
|
||||
-- Restore original text_height (or reset it if none to force recomputing it)
|
||||
self.text_height = self.orig_text_height or nil
|
||||
@@ -738,14 +781,7 @@ function InputDialog:_addScrollButtons(nav_bar)
|
||||
text = self.keyboard_hidden and "↑⌨" or "↓⌨",
|
||||
id = "keyboard",
|
||||
callback = function()
|
||||
self.keyboard_hidden = not self.keyboard_hidden
|
||||
self.input = self:getInputText() -- re-init with up-to-date text
|
||||
self:onClose() -- will close keyboard and save view position
|
||||
self:free()
|
||||
self:init()
|
||||
if not self.keyboard_hidden then
|
||||
self:onShowKeyboard()
|
||||
end
|
||||
self:toggleKeyboard()
|
||||
end,
|
||||
})
|
||||
end
|
||||
@@ -754,11 +790,12 @@ function InputDialog:_addScrollButtons(nav_bar)
|
||||
table.insert(row, {
|
||||
text = _("Find"),
|
||||
callback = function()
|
||||
local keyboard_hidden_state = not self.keyboard_hidden
|
||||
self:toggleKeyboard(true) -- hide text editor keyboard
|
||||
local input_dialog
|
||||
input_dialog = InputDialog:new{
|
||||
title = _("Enter text to search for"),
|
||||
stop_events_propagation = true, -- avoid interactions with upper InputDialog
|
||||
deny_keyboard_hiding = true,
|
||||
input = self.search_value,
|
||||
buttons = {
|
||||
{
|
||||
@@ -766,6 +803,8 @@ function InputDialog:_addScrollButtons(nav_bar)
|
||||
text = _("Cancel"),
|
||||
callback = function()
|
||||
UIManager:close(input_dialog)
|
||||
self.keyboard_hidden = keyboard_hidden_state
|
||||
self:toggleKeyboard()
|
||||
end,
|
||||
},
|
||||
{
|
||||
@@ -774,6 +813,8 @@ function InputDialog:_addScrollButtons(nav_bar)
|
||||
self.search_value = input_dialog:getInputText()
|
||||
if self.search_value ~= "" then
|
||||
UIManager:close(input_dialog)
|
||||
self.keyboard_hidden = keyboard_hidden_state
|
||||
self:toggleKeyboard()
|
||||
local msg
|
||||
local char_pos = self._input_widget:searchString(self.search_value, self.case_sensitive, 1)
|
||||
if char_pos > 0 then
|
||||
@@ -795,6 +836,8 @@ function InputDialog:_addScrollButtons(nav_bar)
|
||||
self.search_value = input_dialog:getInputText()
|
||||
if self.search_value ~= "" then
|
||||
UIManager:close(input_dialog)
|
||||
self.keyboard_hidden = keyboard_hidden_state
|
||||
self:toggleKeyboard()
|
||||
local msg
|
||||
local char_pos = self._input_widget:searchString(self.search_value, self.case_sensitive)
|
||||
if char_pos > 0 then
|
||||
@@ -813,7 +856,6 @@ function InputDialog:_addScrollButtons(nav_bar)
|
||||
},
|
||||
}
|
||||
|
||||
-- checkbox
|
||||
self.check_button_case = CheckButton:new{
|
||||
text = _("Case sensitive"),
|
||||
checked = self.case_sensitive,
|
||||
@@ -824,19 +866,7 @@ function InputDialog:_addScrollButtons(nav_bar)
|
||||
self.case_sensitive = self.check_button_case.checked
|
||||
end,
|
||||
}
|
||||
|
||||
local checkbox_shift = math.floor((input_dialog.width - input_dialog._input_widget.width) / 2 + 0.5)
|
||||
local check_buttons = HorizontalGroup:new{
|
||||
HorizontalSpan:new{width = checkbox_shift},
|
||||
VerticalGroup:new{
|
||||
align = "left",
|
||||
self.check_button_case,
|
||||
},
|
||||
}
|
||||
|
||||
-- insert check buttons before the regular buttons
|
||||
local nb_elements = #input_dialog.dialog_frame[1]
|
||||
table.insert(input_dialog.dialog_frame[1], nb_elements-1, check_buttons)
|
||||
input_dialog:addWidget(self.check_button_case)
|
||||
|
||||
UIManager:show(input_dialog)
|
||||
input_dialog:onShowKeyboard()
|
||||
@@ -846,6 +876,8 @@ function InputDialog:_addScrollButtons(nav_bar)
|
||||
table.insert(row, {
|
||||
text = _("Go"),
|
||||
callback = function()
|
||||
local keyboard_hidden_state = not self.keyboard_hidden
|
||||
self:toggleKeyboard(true) -- hide text editor keyboard
|
||||
local cur_line_num, last_line_num = self._input_widget:getLineNums()
|
||||
local input_dialog
|
||||
input_dialog = InputDialog:new{
|
||||
@@ -854,13 +886,14 @@ function InputDialog:_addScrollButtons(nav_bar)
|
||||
input_hint = T(_("%1 (1 - %2)"), cur_line_num, last_line_num),
|
||||
input_type = "number",
|
||||
stop_events_propagation = true, -- avoid interactions with upper InputDialog
|
||||
deny_keyboard_hiding = true,
|
||||
buttons = {
|
||||
{
|
||||
{
|
||||
text = _("Cancel"),
|
||||
callback = function()
|
||||
UIManager:close(input_dialog)
|
||||
self.keyboard_hidden = keyboard_hidden_state
|
||||
self:toggleKeyboard()
|
||||
end,
|
||||
},
|
||||
{
|
||||
@@ -870,6 +903,8 @@ function InputDialog:_addScrollButtons(nav_bar)
|
||||
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.keyboard_hidden = keyboard_hidden_state
|
||||
self:toggleKeyboard()
|
||||
self._input_widget:moveCursorToCharPos(self._input_widget:getLineCharPos(new_line_num))
|
||||
end
|
||||
end,
|
||||
|
||||
Reference in New Issue
Block a user