diff --git a/frontend/apps/reader/modules/readerstyletweak.lua b/frontend/apps/reader/modules/readerstyletweak.lua index f7b0e28bb..8fed3af8a 100644 --- a/frontend/apps/reader/modules/readerstyletweak.lua +++ b/frontend/apps/reader/modules/readerstyletweak.lua @@ -13,7 +13,7 @@ local FrameContainer = require("ui/widget/container/framecontainer") local Geom = require("ui/geometry") local GestureRange = require("ui/gesturerange") local InfoMessage = require("ui/widget/infomessage") -local InputContainer = require("ui/widget/container/inputcontainer") +local FocusManager = require("ui/widget/focusmanager") local MovableContainer = require("ui/widget/container/movablecontainer") local Notification = require("ui/widget/notification") local Size = require("ui/size") @@ -31,7 +31,7 @@ local Screen = Device.screen local T = require("ffi/util").template -- Simple widget for showing tweak info -local TweakInfoWidget = InputContainer:extend{ +local TweakInfoWidget = FocusManager:extend{ tweak = nil, is_global_default = nil, toggle_global_default_callback = function() end, @@ -95,6 +95,9 @@ function TweakInfoWidget:init() self.css_frame = FrameContainer:new{ bordersize = Size.border.thin, padding = Size.padding.large, + focusable = true, + focus_border_size = Size.border.thick, + focus_inner_border = true, TextBoxWidget:new{ text = self.css_text, face = Font:getFace("infont", 16), @@ -158,6 +161,22 @@ function TweakInfoWidget:init() show_parent = self, } + -- Note: for devices with DPad but no clipboard, button_table is a FocusManager + -- so the widget should work correctly nonetheless. + if Device:hasDPad() and Device:hasClipboard() then + self.css_frame.onPress = function() + local item = self:getFocusItem() + if item == self.css_frame then + self:copyTextToClipboard(self.css_text) + return true + end + end + self.layout = {} + table.insert(self.layout, 1, { self.css_frame }) + self:mergeLayoutInVertical(button_table) + self:refocusWidget() + end + self.movable = MovableContainer:new{ FrameContainer:new{ background = Blitbuffer.COLOR_WHITE, @@ -206,11 +225,7 @@ function TweakInfoWidget:onTap(arg, ges) if ges.pos:intersectWith(self.css_frame.dimen) and Device:hasClipboard() then -- Tap inside CSS text copies it into clipboard (so it -- can be pasted into the book-specific tweak editor) - -- (Add \n on both sides for easier pasting) - Device.input.setClipboardText("\n"..self.css_text.."\n") - UIManager:show(Notification:new{ - text = _("CSS text copied to clipboard"), - }) + self:copyTextToClipboard(self.css_text) return true elseif ges.pos:notIntersectWith(self.movable.dimen) then -- Tap outside closes widget @@ -220,6 +235,15 @@ function TweakInfoWidget:onTap(arg, ges) return false end +function TweakInfoWidget:copyTextToClipboard(text) + -- (Add \n on both sides for easier pasting) + Device.input.setClipboardText("\n"..text.."\n") + UIManager:show(Notification:new{ + text = _("CSS text copied to clipboard"), + }) + return true +end + function TweakInfoWidget:onSelect() if self.selected.x == 1 then self:toggle_global_default_callback() diff --git a/frontend/ui/widget/inputtext.lua b/frontend/ui/widget/inputtext.lua index 8173e7ffc..430296d22 100644 --- a/frontend/ui/widget/inputtext.lua +++ b/frontend/ui/widget/inputtext.lua @@ -173,117 +173,8 @@ local function initTouchEvents() end function InputText:onHoldTextBox(arg, ges) - if self.parent.onSwitchFocus then - self.parent:onSwitchFocus(self) - end - -- clipboard dialog - self._hold_handled = nil - if Device:hasClipboard() then - if self.do_select then -- select mode on - if self.selection_start_pos then -- select end - local selection_end_pos = self.charpos - 1 - if self.selection_start_pos > selection_end_pos then - self.selection_start_pos, selection_end_pos = selection_end_pos + 1, self.selection_start_pos - 1 - end - local txt = table.concat(self.charlist, "", self.selection_start_pos, selection_end_pos) - Device.input.setClipboardText(txt) - UIManager:show(Notification:new{ - text = _("Selection copied to clipboard."), - }) - self.selection_start_pos = nil - self.do_select = false - self:initTextBox() - else -- select start - self.selection_start_pos = self.charpos - UIManager:show(Notification:new{ - text = _("Set cursor to end of selection, then long-press in text box."), - }) - end - self._hold_handled = true - return true - end - local clipboard_value = Device.input.getClipboardText() - local is_clipboard_empty = clipboard_value == "" - local clipboard_dialog - clipboard_dialog = require("ui/widget/textviewer"):new{ - title = _("Clipboard"), - show_menu = false, - text = is_clipboard_empty and _("(empty)") or clipboard_value, - fgcolor = is_clipboard_empty and Blitbuffer.COLOR_DARK_GRAY or Blitbuffer.COLOR_BLACK, - width = math.floor(math.min(Screen:getWidth(), Screen:getHeight()) * 0.8), - height = math.floor(math.max(Screen:getWidth(), Screen:getHeight()) * 0.4), - justified = false, - modal = true, - stop_events_propagation = true, - buttons_table = { - { - { - text = _("Copy all"), - callback = function() - UIManager:close(clipboard_dialog) - Device.input.setClipboardText(table.concat(self.charlist)) - UIManager:show(Notification:new{ - text = _("All text copied to clipboard."), - }) - end, - }, - { - text = _("Copy line"), - callback = function() - UIManager:close(clipboard_dialog) - local txt = table.concat(self.charlist, "", self:getStringPos()) - Device.input.setClipboardText(txt) - UIManager:show(Notification:new{ - text = _("Line copied to clipboard."), - }) - end, - }, - { - text = _("Copy word"), - callback = function() - UIManager:close(clipboard_dialog) - local txt = table.concat(self.charlist, "", self:getStringPos(true)) - Device.input.setClipboardText(txt) - UIManager:show(Notification:new{ - text = _("Word copied to clipboard."), - }) - end, - }, - }, - { - { - text = _("Delete all"), - enabled = #self.charlist > 0, - callback = function() - UIManager:close(clipboard_dialog) - self:delAll() - end, - }, - { - text = _("Select"), - callback = function() - UIManager:close(clipboard_dialog) - UIManager:show(Notification:new{ - text = _("Set cursor to start of selection, then long-press in text box."), - }) - self.do_select = true - self:initTextBox() - end, - }, - { - text = _("Paste"), - enabled = not is_clipboard_empty, - callback = function() - UIManager:close(clipboard_dialog) - self:addChars(clipboard_value) - end, - }, - }, - }, - } - UIManager:show(clipboard_dialog) - end - self._hold_handled = true + -- Logic moved below as it is also used when not isTouchDevice + self:holdTextBox(arg, ges) return true end @@ -352,6 +243,121 @@ end InputText.initInputEvents() +function InputText:holdTextBox(arg, ges) + if self.parent.onSwitchFocus then + self.parent:onSwitchFocus(self) + end + -- clipboard dialog + self._hold_handled = nil + if Device:hasClipboard() then + if self.do_select then -- select mode on + if self.selection_start_pos then -- select end + local selection_end_pos = self.charpos - 1 + if self.selection_start_pos > selection_end_pos then + self.selection_start_pos, selection_end_pos = selection_end_pos + 1, self.selection_start_pos - 1 + end + local txt = table.concat(self.charlist, "", self.selection_start_pos, selection_end_pos) + Device.input.setClipboardText(txt) + UIManager:show(Notification:new{ + text = _("Selection copied to clipboard."), + }) + self.selection_start_pos = nil + self.do_select = false + self:initTextBox() + else -- select start + self.selection_start_pos = self.charpos + UIManager:show(Notification:new{ + text = _("Set cursor to end of selection, then long-press in text box."), + }) + end + self._hold_handled = true + return true + end + local clipboard_value = Device.input.getClipboardText() + local is_clipboard_empty = clipboard_value == "" + local clipboard_dialog + clipboard_dialog = require("ui/widget/textviewer"):new{ + title = _("Clipboard"), + show_menu = false, + text = is_clipboard_empty and _("(empty)") or clipboard_value, + fgcolor = is_clipboard_empty and Blitbuffer.COLOR_DARK_GRAY or Blitbuffer.COLOR_BLACK, + width = math.floor(math.min(Screen:getWidth(), Screen:getHeight()) * 0.8), + height = math.floor(math.max(Screen:getWidth(), Screen:getHeight()) * 0.4), + justified = false, + modal = true, + stop_events_propagation = true, + buttons_table = { + { + { + text = _("Copy all"), + callback = function() + UIManager:close(clipboard_dialog) + Device.input.setClipboardText(table.concat(self.charlist)) + UIManager:show(Notification:new{ + text = _("All text copied to clipboard."), + }) + end, + }, + { + text = _("Copy line"), + callback = function() + UIManager:close(clipboard_dialog) + local txt = table.concat(self.charlist, "", self:getStringPos()) + Device.input.setClipboardText(txt) + UIManager:show(Notification:new{ + text = _("Line copied to clipboard."), + }) + end, + }, + { + text = _("Copy word"), + callback = function() + UIManager:close(clipboard_dialog) + local txt = table.concat(self.charlist, "", self:getStringPos(true)) + Device.input.setClipboardText(txt) + UIManager:show(Notification:new{ + text = _("Word copied to clipboard."), + }) + end, + }, + }, + { + { + text = _("Delete all"), + enabled = #self.charlist > 0, + callback = function() + UIManager:close(clipboard_dialog) + self:delAll() + end, + }, + { + text = _("Select"), + callback = function() + UIManager:close(clipboard_dialog) + UIManager:show(Notification:new{ + text = _("Set cursor to start of selection, then long-press in text box."), + }) + self.do_select = true + self:initTextBox() + end, + }, + { + text = _("Paste"), + enabled = not is_clipboard_empty, + callback = function() + UIManager:close(clipboard_dialog) + self:addChars(clipboard_value) + end, + }, + }, + }, + } + UIManager:show(clipboard_dialog) + end + self._hold_handled = true + return true +end + function InputText:checkTextEditability() -- The split of the 'text' string to a table of utf8 chars may not be -- reversible to the same string, if 'text' comes from a binary file @@ -715,6 +721,8 @@ function InputText:onKeyPress(key) self:upLine() elseif key["Down"] then self:downLine() + elseif key["Press"] then + self:holdTextBox() elseif key["Home"] then if self.keyboard:isVisible() then self:onCloseKeyboard()