From ebf997dd9900c7286d4e39fb80a3ed54549448e5 Mon Sep 17 00:00:00 2001 From: poire-z Date: Sat, 9 Jan 2021 21:59:31 +0100 Subject: [PATCH] DictQuickLookup: allow continuous reading with keys We could scroll a definition with keys. Now, when reaching top or bottom, switch to prev/next result. --- frontend/ui/widget/dictquicklookup.lua | 29 +++++++++++++++--------- frontend/ui/widget/scrollhtmlwidget.lua | 30 ++++++++++++------------- frontend/ui/widget/scrolltextwidget.lua | 30 ++++++++++++------------- 3 files changed, 48 insertions(+), 41 deletions(-) diff --git a/frontend/ui/widget/dictquicklookup.lua b/frontend/ui/widget/dictquicklookup.lua index d7d077a08..e9b31b3a4 100644 --- a/frontend/ui/widget/dictquicklookup.lua +++ b/frontend/ui/widget/dictquicklookup.lua @@ -28,6 +28,7 @@ local logger = require("logger") local util = require("util") local _ = require("gettext") local C_ = _.pgettext +local Input = Device.input local Screen = Device.screen local T = require("ffi/util").template @@ -70,6 +71,8 @@ function DictQuickLookup:init() self.image_alt_face = Font:getFace("cfont", font_size_alt) if Device:hasKeys() then self.key_events = { + ReadPrevResult = {{Input.group.PgBack}, doc = "read prev result"}, + ReadNextResult = {{Input.group.PgFwd}, doc = "read next result"}, Close = { {"Back"}, doc = "close quick lookup" } } end @@ -948,9 +951,19 @@ function DictQuickLookup:changeToDefaultDict() end ]]-- -function DictQuickLookup:onAnyKeyPressed() - -- triggered by our defined key events - UIManager:close(self) +function DictQuickLookup:onReadNextResult() + self:changeToNextDict() + return true +end + +function DictQuickLookup:onReadPrevResult() + local prev_index = self.dict_index + self:changeToPrevDict() + if self.dict_index ~= prev_index then + -- Jump directly to bottom of previous dict definition + -- to keep "continuous reading with tap" consistent + self.definition_widget[1]:scrollToRatio(1) -- 1 = 100% = bottom + end return true end @@ -971,15 +984,9 @@ function DictQuickLookup:onTap(arg, ges_ev) -- will pop it up for us here when it can't scroll anymore). -- This allow for continuous reading of results' definitions with tap. if BD.flipIfMirroredUILayout(ges_ev.pos.x < Screen:getWidth()/2) then - local prev_index = self.dict_index - self:changeToPrevDict() - if self.dict_index ~= prev_index then - -- Jump directly to bottom of previous dict definition - -- to keep "continuous reading with tap" consistent - self.definition_widget[1]:scrollToRatio(1) -- 1 = 100% = bottom - end + self:onReadPrevResult() else - self:changeToNextDict() + self:onReadNextResult() end end return true diff --git a/frontend/ui/widget/scrollhtmlwidget.lua b/frontend/ui/widget/scrollhtmlwidget.lua index 1e25999fe..a54df146d 100644 --- a/frontend/ui/widget/scrollhtmlwidget.lua +++ b/frontend/ui/widget/scrollhtmlwidget.lua @@ -150,28 +150,28 @@ end function ScrollHtmlWidget:onTapScrollText(arg, ges) if BD.flipIfMirroredUILayout(ges.pos.x < Screen:getWidth()/2) then - if self.htmlbox_widget.page_number > 1 then - self:scrollText(-1) - return true - end + return self:onScrollUp() else - if self.htmlbox_widget.page_number < self.htmlbox_widget.page_count then - self:scrollText(1) - return true - end + return self:onScrollDown() + end +end + +function ScrollHtmlWidget:onScrollUp() + if self.htmlbox_widget.page_number > 1 then + self:scrollText(-1) + return true end -- if we couldn't scroll (because we're already at top or bottom), -- let it propagate up (e.g. for quickdictlookup to go to next/prev result) end function ScrollHtmlWidget:onScrollDown() - self:scrollText(1) - return true -end - -function ScrollHtmlWidget:onScrollUp() - self:scrollText(-1) - return true + if self.htmlbox_widget.page_number < self.htmlbox_widget.page_count then + self:scrollText(1) + return true + end + -- if we couldn't scroll (because we're already at top or bottom), + -- let it propagate up (e.g. for quickdictlookup to go to next/prev result) end return ScrollHtmlWidget diff --git a/frontend/ui/widget/scrolltextwidget.lua b/frontend/ui/widget/scrolltextwidget.lua index d1056f03b..7b9307df0 100644 --- a/frontend/ui/widget/scrolltextwidget.lua +++ b/frontend/ui/widget/scrolltextwidget.lua @@ -252,28 +252,28 @@ function ScrollTextWidget:onTapScrollText(arg, ges) end -- same tests as done in TextBoxWidget:scrollUp/Down if BD.flipIfMirroredUILayout(ges.pos.x < Screen:getWidth()/2) then - if self.text_widget.virtual_line_num > 1 then - self:scrollText(-1) - return true - end + return self:onScrollUp() else - if self.text_widget.virtual_line_num + self.text_widget:getVisLineCount() <= #self.text_widget.vertical_string_list then - self:scrollText(1) - return true - end + return self:onScrollDown() + end +end + +function ScrollTextWidget:onScrollUp() + if self.text_widget.virtual_line_num > 1 then + self:scrollText(-1) + return true end -- if we couldn't scroll (because we're already at top or bottom), -- let it propagate up (e.g. for quickdictlookup to go to next/prev result) end function ScrollTextWidget:onScrollDown() - self:scrollText(1) - return true -end - -function ScrollTextWidget:onScrollUp() - self:scrollText(-1) - return true + if self.text_widget.virtual_line_num + self.text_widget:getVisLineCount() <= #self.text_widget.vertical_string_list then + self:scrollText(1) + return true + end + -- if we couldn't scroll (because we're already at top or bottom), + -- let it propagate up (e.g. for quickdictlookup to go to next/prev result) end function ScrollTextWidget:onPanText(arg, ges)