FileManager: Make reinit slightly less hackish (#7006)

Much like #6958 did for plugins, registering listener twice was also a
big boo-boo.

Revamp the whole thing to make it slightly less hackish.
reviewable/pr7011/r1
NiLuJe 4 years ago committed by GitHub
parent daefdc96e9
commit 642978c97c
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

@ -81,22 +81,34 @@ function FileManager:setRotationMode(init)
end end
end end
function FileManager:init() function FileManager:initGesListener()
if Device:isTouchDevice() then if not Device:isTouchDevice() then
self:registerTouchZones({ return
{ end
id = "filemanager_swipe",
ges = "swipe", self:registerTouchZones({
screen_zone = { {
ratio_x = 0, ratio_y = 0, id = "filemanager_swipe",
ratio_w = Screen:getWidth(), ratio_h = Screen:getHeight(), ges = "swipe",
}, screen_zone = {
handler = function(ges) ratio_x = 0, ratio_y = 0,
self:onSwipeFM(ges) ratio_w = Screen:getWidth(), ratio_h = Screen:getHeight(),
end,
}, },
}) handler = function(ges)
self:onSwipeFM(ges)
end,
},
})
end
function FileManager:onSetDimensions(dimen)
-- update listening according to new screen dimen
if Device:isTouchDevice() then
self:initGesListener()
end end
end
function FileManager:setupLayout()
self.show_parent = self.show_parent or self self.show_parent = self.show_parent or self
local icon_size = Screen:scaleBySize(DGENERIC_ICON_SIZE) local icon_size = Screen:scaleBySize(DGENERIC_ICON_SIZE)
local home_button = IconButton:new{ local home_button = IconButton:new{
@ -457,6 +469,20 @@ function FileManager:init()
ui = self ui = self
} }
if Device:hasKeys() then
self.key_events.Home = { {"Home"}, doc = "go home" }
-- Override the menu.lua way of handling the back key
self.file_chooser.key_events.Back = { {"Back"}, doc = "go back" }
if not Device:hasFewKeys() then
-- Also remove the handler assigned to the "Back" key by menu.lua
self.file_chooser.key_events.Close = nil
end
end
end
function FileManager:init()
self:setupLayout()
local screenshoter = Screenshoter:new{ prefix = 'FileManager' } local screenshoter = Screenshoter:new{ prefix = 'FileManager' }
table.insert(self, screenshoter) -- for regular events table.insert(self, screenshoter) -- for regular events
self.active_widgets = { screenshoter } -- to get events even when hidden self.active_widgets = { screenshoter } -- to get events even when hidden
@ -476,22 +502,19 @@ function FileManager:init()
table.insert(self, DeviceListener:new{ ui = self }) table.insert(self, DeviceListener:new{ ui = self })
-- koreader plugins -- koreader plugins
if not self._plugins_loaded then for _, plugin_module in ipairs(PluginLoader:loadPlugins()) do
for _,plugin_module in ipairs(PluginLoader:loadPlugins()) do if not plugin_module.is_doc_only then
if not plugin_module.is_doc_only then local ok, plugin_or_err = PluginLoader:createPluginInstance(
local ok, plugin_or_err = PluginLoader:createPluginInstance( plugin_module, { ui = self, })
plugin_module, { ui = self, }) -- Keep references to the modules which do not register into menu.
-- Keep references to the modules which do not register into menu. if ok then
if ok then local name = plugin_module.name
local name = plugin_module.name if name then self[name] = plugin_or_err end
if name then self[name] = plugin_or_err end table.insert(self, plugin_or_err)
table.insert(self, plugin_or_err) logger.info("FM loaded plugin", name,
logger.info("FM loaded plugin", name, "at", plugin_module.path)
"at", plugin_module.path)
end
end end
end end
self._plugins_loaded = true
end end
if Device:hasWifiToggle() then if Device:hasWifiToggle() then
@ -499,16 +522,6 @@ function FileManager:init()
table.insert(self, NetworkListener:new{ ui = self }) table.insert(self, NetworkListener:new{ ui = self })
end end
if Device:hasKeys() then
self.key_events.Home = { {"Home"}, doc = "go home" }
-- Override the menu.lua way of handling the back key
self.file_chooser.key_events.Back = { {"Back"}, doc = "go back" }
if not Device:hasFewKeys() then
-- Also remove the handler assigned to the "Back" key by menu.lua
self.file_chooser.key_events.Close = nil
end
end
self:handleEvent(Event:new("SetDimensions", self.dimen)) self:handleEvent(Event:new("SetDimensions", self.dimen))
end end
@ -696,10 +709,12 @@ function FileManager:reinit(path, focused_file)
end end
-- reinit filemanager -- reinit filemanager
self.focused_file = focused_file self.focused_file = focused_file
self:init() self:setupLayout()
self:handleEvent(Event:new("SetDimensions", self.dimen))
self.file_chooser.path_items = path_items_backup self.file_chooser.path_items = path_items_backup
-- self:init() has already done file_chooser:refreshPath(), so this one -- self:init() has already done file_chooser:refreshPath()
-- looks like not necessary (cheap with classic mode, less cheap with -- (by virtue of rebuilding file_chooser), so this one
-- looks unnecessary (cheap with classic mode, less cheap with
-- CoverBrowser plugin's cover image renderings) -- CoverBrowser plugin's cover image renderings)
-- self:onRefresh() -- self:onRefresh()
end end

Loading…
Cancel
Save