Add vertical edge gesture (#5156)

* Add vertical edge gesture
* Add left/right edge swipe gesture
* Move kobolight plugin to ReaderFrontlight
* Remove kobolight plugin
pull/5165/head
Robert 5 years ago committed by NiLuJe
parent 91b2faae6a
commit c6c8c5a037

@ -10,6 +10,7 @@ local _ = require("gettext")
local ReaderFrontLight = InputContainer:new{
steps = {0,1,1,1,1,2,2,2,3,4,5,6,7,8,9,10},
steps_fl = { 0.1, 0.1, 0.2, 0.4, 0.7, 1.1, 1.6, 2.2, 2.9, 3.7, 4.6, 5.6, 6.7, 7.9, 9.2, 10.6, },
gestureScale = Screen:getWidth() * FRONTLIGHT_SENSITIVITY_DECREASE,
}
@ -64,22 +65,153 @@ function ReaderFrontLight:onAdjust(arg, ges)
return true
end
-- direction +1 - increase frontlight
-- direction -1 - decrease frontlight
function ReaderFrontLight:onChangeFlIntensity(ges, direction)
local powerd = Device:getPowerDevice()
local gestureScale
if ges.direction == "south" or ges.direction == "north" then
gestureScale = Screen:getHeight() * 7/8 * 0.8
elseif ges.direction == "west" or ges.direction == "east" then
gestureScale = Screen:getWidth() * 7/8 * 0.8
else
local width = Screen:getWidth()
local height = Screen:getHeight()
-- diagonal
gestureScale = math.sqrt(width * width + height * height) * 7/8 * 0.8
end
if powerd.fl_intensity == nil then return false end
local steps_tbl = {}
local scale = (powerd.fl_max - powerd.fl_min) / 2 / 10.6
for i = 1, #self.steps_fl, 1
do
steps_tbl[i] = math.ceil(self.steps_fl[i] * scale)
end
if ges.distance == nil then
ges.distance = 1
end
local step = math.ceil(#steps_tbl * ges.distance / gestureScale)
local delta_int = steps_tbl[step] or steps_tbl[#steps_tbl]
if direction ~= -1 and direction ~= 1 then
-- set default value (increase frontlight)
direction = 1
end
local new_intensity = powerd.fl_intensity + direction * delta_int
if new_intensity == nil then return true end
-- when new_intensity <=0, toggle light off
if new_intensity <= 0 then
powerd:turnOffFrontlight()
else
powerd:setIntensity(new_intensity)
end
self:onShowIntensity()
if self.view and self.view.footer_visible and self.view.footer.settings.frontlight then
self.view.footer:updateFooter()
end
return true
end
-- direction +1 - increase frontlight warmth
-- direction -1 - decrease frontlight warmth
function ReaderFrontLight:onChangeFlWarmth(ges, direction)
local powerd = Device:getPowerDevice()
if powerd.fl_warmth == nil then return false end
if powerd.auto_warmth then
UIManager:show(Notification:new{
text = _("Warmth is handled automatically."),
timeout = 1.0,
})
return true
end
local gestureScale
if ges.direction == "south" or ges.direction == "north" then
gestureScale = Screen:getHeight() * 7/8 * 0.8
elseif ges.direction == "west" or ges.direction == "east" then
gestureScale = Screen:getWidth() * 7/8 * 0.8
else
local width = Screen:getWidth()
local height = Screen:getHeight()
-- diagonal
gestureScale = math.sqrt(width * width + height * height) * 7/8 * 0.8
end
local steps_tbl = {}
local scale = (powerd.fl_max - powerd.fl_min) / 2 / 10.6
for i = 1, #self.steps_fl, 1
do
steps_tbl[i] = math.ceil(self.steps_fl[i] * scale)
end
if ges.distance == nil then
ges.distance = 1
end
local step = math.ceil(#steps_tbl * ges.distance / gestureScale)
local delta_int = steps_tbl[step] or steps_tbl[#steps_tbl]
local warmth
if direction ~= -1 and direction ~= 1 then
-- set default value (increase frontlight)
direction = 1
end
warmth = powerd.fl_warmth + direction * delta_int
if warmth > 100 then
warmth = 100
elseif warmth < 0 then
warmth = 0
end
powerd:setWarmth(warmth)
self:onShowWarmth()
return true
end
function ReaderFrontLight:onShowOnOff()
local powerd = Device:getPowerDevice()
local new_text
if powerd.is_fl_on then
new_text = _("Frontlight enabled.")
else
new_text = _("Frontlight disabled.")
end
UIManager:show(Notification:new{
text = new_text,
timeout = 1.0,
})
return true
end
function ReaderFrontLight:onShowIntensity()
if not Device:hasFrontlight() then return true end
local powerd = Device:getPowerDevice()
local new_text
if powerd:isFrontlightOff() then
new_text = _("Frontlight is off.")
new_text = _("Frontlight disabled.")
else
new_text = T(_("Frontlight intensity is set to %1."), powerd:frontlightIntensity())
end
UIManager:show(Notification:new{
text = new_text,
timeout = 2,
timeout = 1,
})
return true
end
function ReaderFrontLight:onShowWarmth(value)
local powerd = Device:getPowerDevice()
if powerd.fl_warmth ~= nil then
UIManager:show(Notification:new{
text = T(_("Warmth set to %1."), powerd.fl_warmth),
timeout = 1.0,
})
end
return true
end
function ReaderFrontLight:onSwipe(arg, ges)
if ges.direction == "north" or ges.direction == "south" then
logger.dbg("onSwipe activated")

@ -61,6 +61,10 @@ local action_strings = {
show_config_menu = _("Show bottom menu"),
show_frontlight_dialog = _("Show frontlight dialog"),
toggle_frontlight = _("Toggle frontlight"),
increase_frontlight = _("Increase frontlight brightness"),
decrease_frontlight = _("Decrease frontlight brightness"),
increase_frontlight_warmth = _("Increase frontlight warmth"),
decrease_frontlight_warmth = _("Decrease frontlight warmth"),
toggle_gsensor = _("Toggle accelerometer"),
toggle_rotation = _("Toggle rotation"),
@ -157,6 +161,10 @@ function ReaderGesture:init()
hold_top_right_corner = "ignore",
hold_bottom_left_corner = "ignore",
hold_bottom_right_corner = "ignore",
one_finger_swipe_left_edge_down = Device:hasFrontlight() and "decrease_frontlight" or "ignore",
one_finger_swipe_left_edge_up = Device:hasFrontlight() and "increase_frontlight" or "ignore",
one_finger_swipe_right_edge_down = Device:hasNaturalLight() and "decrease_frontlight_warmth" or "ignore",
one_finger_swipe_right_edge_up = Device:hasNaturalLight() and "increase_frontlight_warmth" or "ignore",
two_finger_tap_top_left_corner = "ignore",
two_finger_tap_top_right_corner = "ignore",
two_finger_tap_bottom_left_corner = "ignore",
@ -357,6 +365,28 @@ function ReaderGesture:addToMainMenu(menu_items)
text = _("Short diagonal swipe"),
sub_item_table = self:buildMenu("short_diagonal_swipe", self.default_gesture["short_diagonal_swipe"]),
},
{
text = _("One-finger swipe"),
sub_item_table = {
{
text_func = function() return actionTextFunc("one_finger_swipe_left_edge_down", _("Left edge down")) end,
sub_item_table = self:buildMenu("one_finger_swipe_left_edge_down", self.default_gesture["one_finger_swipe_left_edge_down"]),
},
{
text_func = function() return actionTextFunc("one_finger_swipe_left_edge_up", _("Left edge up")) end,
sub_item_table = self:buildMenu("one_finger_swipe_left_edge_up", self.default_gesture["one_finger_swipe_left_edge_up"]),
},
{
text_func = function() return actionTextFunc("one_finger_swipe_right_edge_down", _("Right edge down")) end,
sub_item_table = self:buildMenu("one_finger_swipe_right_edge_down", self.default_gesture["one_finger_swipe_right_edge_down"]),
},
{
text_func = function() return actionTextFunc("one_finger_swipe_right_edge_up", _("Right edge up")) end,
sub_item_table = self:buildMenu("one_finger_swipe_right_edge_up", self.default_gesture["one_finger_swipe_right_edge_up"]),
},
},
},
},
}
menu_items.gesture_intervals = {
@ -610,7 +640,12 @@ function ReaderGesture:buildMenu(ges, default)
{"show_menu", true},
{"show_config_menu", not self.is_docless, true},
{"show_frontlight_dialog", Device:hasFrontlight()},
{"toggle_frontlight", Device:hasFrontlight(), true},
{"toggle_frontlight", Device:hasFrontlight()},
{"increase_frontlight", Device:hasFrontlight()},
{"decrease_frontlight", Device:hasFrontlight(), true},
{"increase_frontlight_warmth", Device:hasNaturalLight()},
{"decrease_frontlight_warmth", Device:hasNaturalLight(), true},
{"toggle_gsensor", Device:canToggleGSensor()},
{"toggle_rotation", not self.is_docless, true},
@ -792,6 +827,15 @@ function ReaderGesture:setupGesture(ges, action)
ratio_w = 1, ratio_h = 1,
}
local zone_left_edge = {
ratio_x = 0, ratio_y = 1/8,
ratio_w = 1/8, ratio_h = 7/8,
}
local zone_right_edge = {
ratio_x = 7/8, ratio_y = 1/8,
ratio_w = 1/8, ratio_h = 7/8,
}
-- legacy global variable DTAP_ZONE_FLIPPING may still be defined in default.persistent.lua
local dtap_zone_top_left = DTAP_ZONE_FLIPPING and DTAP_ZONE_FLIPPING or DTAP_ZONE_TOP_LEFT
local zone_top_left_corner = {
@ -823,6 +867,9 @@ function ReaderGesture:setupGesture(ges, action)
local overrides_tap_corner
local overrides_hold_corner
local overrides_vertical_edge
local overrides_pan, overrides_pan_release
local overrides_swipe_pan, overrides_swipe_pan_release
if self.is_docless then
overrides_tap_corner = {
"filemanager_tap",
@ -838,6 +885,17 @@ function ReaderGesture:setupGesture(ges, action)
overrides_hold_corner = {
"readerfooter_hold",
}
overrides_vertical_edge = {
"paging_swipe",
"rolling_swipe",
}
overrides_pan = {
"paging_swipe",
"rolling_swipe",
}
overrides_pan_release = {
"paging_pan_release",
}
end
if ges == "multiswipe" then
@ -881,6 +939,34 @@ function ReaderGesture:setupGesture(ges, action)
ges_type = "hold"
zone = zone_bottom_left_corner
overrides = overrides_hold_corner
elseif ges == "one_finger_swipe_left_edge_down" then
ges_type = "swipe"
zone = zone_left_edge
direction = {south = true}
overrides = overrides_vertical_edge
overrides_swipe_pan = overrides_pan
overrides_swipe_pan_release = overrides_pan_release
elseif ges == "one_finger_swipe_left_edge_up" then
ges_type = "swipe"
zone = zone_left_edge
direction = {north = true}
overrides = overrides_vertical_edge
overrides_swipe_pan = overrides_pan
overrides_swipe_pan_release = overrides_pan_release
elseif ges == "one_finger_swipe_right_edge_down" then
ges_type = "swipe"
zone = zone_right_edge
direction = {south = true}
overrides = overrides_vertical_edge
overrides_swipe_pan = overrides_pan
overrides_swipe_pan_release = overrides_pan_release
elseif ges == "one_finger_swipe_right_edge_up" then
ges_type = "swipe"
zone = zone_right_edge
direction = {north = true}
overrides = overrides_vertical_edge
overrides_swipe_pan = overrides_pan
overrides_swipe_pan_release = overrides_pan_release
elseif ges == "two_finger_tap_top_left_corner" then
ges_type = "two_finger_tap"
zone = zone_top_left_corner
@ -939,6 +1025,13 @@ function ReaderGesture:setupGesture(ges, action)
else return
end
self:registerGesture(ges, action, ges_type, zone, overrides, direction, distance)
-- make dummy zone to disable panning and panning_release when gesture is swipe
if ges_type == "swipe" and ges ~= "short_diagonal_swipe" then
local pan_gesture = ges.."_pan"
local pan_release_gesture = ges.."_pan_release"
self:registerGesture(pan_gesture, "", "pan", zone, overrides_swipe_pan, direction, distance)
self:registerGesture(pan_release_gesture, "", "pan_release", zone, overrides_swipe_pan_release, direction, distance)
end
end
function ReaderGesture:registerGesture(ges, action, ges_type, zone, overrides, direction, distance)
@ -1081,6 +1174,34 @@ function ReaderGesture:gestureAction(action, ges)
else
self.ui:handleEvent(Event:new("ShowFlDialog"))
end
elseif action == "increase_frontlight" then
if self.ges_mode == "gesture_fm" then
local ReaderFrontLight = require("apps/reader/modules/readerfrontlight")
ReaderFrontLight:onChangeFlIntensity(ges, 1)
else
self.ui:handleEvent(Event:new("ChangeFlIntensity", ges, 1))
end
elseif action == "decrease_frontlight" then
if self.ges_mode == "gesture_fm" then
local ReaderFrontLight = require("apps/reader/modules/readerfrontlight")
ReaderFrontLight:onChangeFlIntensity(ges, -1)
else
self.ui:handleEvent(Event:new("ChangeFlIntensity", ges, -1))
end
elseif action == "increase_frontlight_warmth" then
if self.ges_mode == "gesture_fm" then
local ReaderFrontLight = require("apps/reader/modules/readerfrontlight")
ReaderFrontLight:onChangeFlWarmth(ges, 1)
else
self.ui:handleEvent(Event:new("ChangeFlWarmth", ges, 1))
end
elseif action == "decrease_frontlight_warmth" then
if self.ges_mode == "gesture_fm" then
local ReaderFrontLight = require("apps/reader/modules/readerfrontlight")
ReaderFrontLight:onChangeFlWarmth(ges, -1)
else
self.ui:handleEvent(Event:new("ChangeFlWarmth", ges, -1))
end
elseif action == "toggle_bookmark" then
self.ui:handleEvent(Event:new("ToggleBookmark"))
elseif action == "toggle_frontlight" then

@ -3,7 +3,8 @@ local logger = require("logger")
local DEFAULT_PLUGIN_PATH = "plugins"
local OBSOLETE_PLUGINS = {
storagestat = true
storagestat = true,
kobolight = true,
}
local function sandboxPluginEventHandlers(plugin)

@ -77,7 +77,6 @@ local order = {
},
taps_and_gestures = {
"gesture_manager",
"frontlight_gesture_controller",
"gesture_intervals",
"----------------------------",
"menu_activate",

@ -96,7 +96,6 @@ local order = {
},
taps_and_gestures = {
"gesture_manager",
"frontlight_gesture_controller",
"gesture_intervals",
"----------------------------",
"follow_links",

@ -1,6 +0,0 @@
local _ = require("gettext")
return {
name = 'kobolight',
fullname = _("Frontlight gesture controller"),
description = _([[Controls the frontlight with gestures on the left border of screen.]]),
}

Binary file not shown.

Before

Width:  |  Height:  |  Size: 32 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 33 KiB

@ -1,271 +0,0 @@
local Device = require("device")
local with_frontlight = (Device:isCervantes() or Device:isKindle() or Device:isKobo()) and Device:hasFrontlight()
local with_natural_light = Device:hasNaturalLight()
if not (with_frontlight or Device:isSDL()) then
return { disabled = true, }
end
local ConfirmBox = require("ui/widget/confirmbox")
local ImageWidget = require("ui/widget/imagewidget")
local InfoMessage = require("ui/widget/infomessage")
local Notification = require("ui/widget/notification")
local Screen = require("device").screen
local UIManager = require("ui/uimanager")
local WidgetContainer = require("ui/widget/container/widgetcontainer")
local T = require("ffi/util").template
local _ = require("gettext")
local swipe_touch_zone_ratio = { x = 0, y = 1/8, w = 1/10, h = 7/8, }
local swipe_touch_zone_ratio_warmth = { x = 7/8, y = 1/8, w = 1/8, h = 7/8, }
local KoboLight = WidgetContainer:new{
name = 'kobolight',
gestureScale = nil, -- initialized in self:resetLayout()
}
function KoboLight:init()
local powerd = Device:getPowerDevice()
local scale = (powerd.fl_max - powerd.fl_min) / 2 / 10.6
self.steps = { 0.1, 0.1, 0.2, 0.4, 0.7, 1.1, 1.6, 2.2, 2.9, 3.7, 4.6, 5.6, 6.7, 7.9, 9.2, 10.6, }
for i = 1, #self.steps, 1
do
self.steps[i] = math.ceil(self.steps[i] * scale)
end
self.ui.menu:registerToMainMenu(self)
end
function KoboLight:onReaderReady()
self:setupTouchZones()
self:resetLayout()
end
function KoboLight:disabled()
return G_reader_settings:isTrue("disable_kobolight")
end
function KoboLight:setupTouchZones()
if not Device:isTouchDevice() then return end
if self:disabled() then return end
local swipe_zone = {
ratio_x = swipe_touch_zone_ratio.x, ratio_y = swipe_touch_zone_ratio.y,
ratio_w = swipe_touch_zone_ratio.w, ratio_h = swipe_touch_zone_ratio.h,
}
local swipe_zone_warmth = {
ratio_x = swipe_touch_zone_ratio_warmth.x,
ratio_y = swipe_touch_zone_ratio_warmth.y,
ratio_w = swipe_touch_zone_ratio_warmth.w,
ratio_h = swipe_touch_zone_ratio_warmth.h,
}
self.ui:registerTouchZones({
{
id = "plugin_kobolight_swipe",
ges = "swipe",
screen_zone = swipe_zone,
handler = function(ges) return self:onSwipe(nil, ges) end,
overrides = {
"paging_swipe",
"rolling_swipe",
},
},
{
-- dummy zone to disable reader panning
id = "plugin_kobolight_pan",
ges = "pan",
screen_zone = swipe_zone,
handler = function(ges) return true end,
overrides = {
"paging_pan",
"rolling_pan",
},
},
{
-- dummy zone to disable reader panning
id = "plugin_kobolight_pan_release",
ges = "pan_release",
screen_zone = swipe_zone,
handler = function(ges) return true end,
overrides = {
"paging_pan_release",
},
},
})
if with_natural_light then
self.ui:registerTouchZones({
{
id = "plugin_kobolight_swipe_warmth",
ges = "swipe",
screen_zone = swipe_zone_warmth,
handler = function(ges) return self:onSwipeWarmth(nil, ges) end,
overrides = {
"paging_swipe",
"rolling_swipe",
},
},
{
-- dummy zone to disable reader panning
id = "plugin_kobolight_pan_warmth",
ges = "pan",
screen_zone = swipe_zone_warmth,
handler = function(ges) return true end,
overrides = {
"paging_pan",
"rolling_pan",
},
},
{
-- dummy zone to disable reader panning
id = "plugin_kobolight_pan_release_warmth",
ges = "pan_release",
screen_zone = swipe_zone_warmth,
handler = function(ges) return true end,
overrides = {
"paging_pan_release",
},
},
})
end
end
function KoboLight:resetLayout()
local new_screen_height = Screen:getHeight()
self.gestureScale = new_screen_height * swipe_touch_zone_ratio.h * 0.8
end
function KoboLight:onShowIntensity()
local powerd = Device:getPowerDevice()
if powerd.fl_intensity ~= nil then
UIManager:show(Notification:new{
text = T(_("Frontlight intensity is set to %1."), powerd.fl_intensity),
timeout = 1.0,
})
end
return true
end
function KoboLight:onShowWarmth(value)
local powerd = Device:getPowerDevice()
if powerd.fl_warmth ~= nil then
UIManager:show(Notification:new{
text = T(_("Warmth is set to %1."), powerd.fl_warmth),
timeout = 1.0,
})
end
return true
end
function KoboLight:onShowOnOff()
local powerd = Device:getPowerDevice()
local new_text
if powerd.is_fl_on then
new_text = _("Frontlight is on.")
else
new_text = _("Frontlight is off.")
end
UIManager:show(Notification:new{
text = new_text,
timeout = 1.0,
})
return true
end
function KoboLight:onSwipe(_, ges)
local powerd = Device:getPowerDevice()
if powerd.fl_intensity == nil then return false end
local step = math.ceil(#self.steps * ges.distance / self.gestureScale)
local delta_int = self.steps[step] or self.steps[#self.steps]
local new_intensity
if ges.direction == "north" then
new_intensity = powerd.fl_intensity + delta_int
elseif ges.direction == "south" then
new_intensity = powerd.fl_intensity - delta_int
else
return false -- don't consume swipe event if it's not matched
end
-- when new_intensity <= 0, toggle light off
if new_intensity <= 0 then
if powerd.is_fl_on then
powerd:toggleFrontlight()
end
self:onShowOnOff()
else -- general case
powerd:setIntensity(new_intensity)
self:onShowIntensity()
end
return true
end
function KoboLight:onSwipeWarmth(ignored, ges)
local powerd = Device:getPowerDevice()
if powerd.fl_warmth == nil then return false end
if powerd.auto_warmth then
UIManager:show(Notification:new{
text = _("Warmth is handled automatically."),
timeout = 1.0,
})
return true
end
local step = math.ceil(#self.steps * ges.distance / self.gestureScale)
local delta_int = self.steps[step] or self.steps[#self.steps]
local warmth
if ges.direction == "north" then
warmth = math.min(powerd.fl_warmth + delta_int, 100)
elseif ges.direction == "south" then
warmth = math.max(powerd.fl_warmth - delta_int, 0)
else
return false -- don't consume swipe event if it's not matched
end
powerd:setWarmth(warmth)
self:onShowWarmth()
return true
end
function KoboLight:addToMainMenu(menu_items)
menu_items.frontlight_gesture_controller = {
text = _("Frontlight gesture controller"),
keep_menu_open = true,
callback = function()
local image_name
local nl_text = ""
if with_natural_light then
image_name = "/demo_ka1.png"
nl_text = _("\n- Change frontlight warmth by swiping up or down on the right of the screen.")
else
image_name = "/demo.png"
end
local image = ImageWidget:new{
file = self.path .. image_name,
height = Screen:getHeight(),
width = Screen:getWidth(),
scale_factor = 0,
}
UIManager:show(image)
UIManager:show(ConfirmBox:new{
text = _("Frontlight gesture controller can:\n- Turn on or off frontlight by tapping bottom left of the screen.\n- Change frontlight intensity by swiping up or down on the left of the screen.") .. nl_text .. "\n\n" ..
(self:disabled() and _("Do you want to enable the frontlight gesture controller?") or _("Do you want to disable the frontlight gesture controller?")),
ok_text = self:disabled() and _("Enable") or _("Disable"),
ok_callback = function()
UIManager:close(image, "full")
UIManager:show(InfoMessage:new{
text = T(_("You have %1 the frontlight gesture controller. It will take effect on next restart."),
self:disabled() and _("enabled") or _("disabled"))
})
G_reader_settings:flipTrue("disable_kobolight")
end,
cancel_text = _("Close"),
cancel_callback = function()
UIManager:close(image, "full")
end,
})
UIManager:setDirty("all", "full")
end,
}
end
return KoboLight
Loading…
Cancel
Save