fix Menu widget initialization on inheritance bug in filechooser

pull/214/head
Qingping Hou 11 years ago
parent c66429c903
commit aef2c4123e

@ -1,8 +1,9 @@
require "ui/widget/menu" require "ui/widget/menu"
FileChooser = Menu:new{ FileChooser = Menu:extend{
height = Screen:getHeight(), height = Screen:getHeight(),
width = Screen:getWidth(), width = Screen:getWidth(),
no_title = true,
path = lfs.currentdir(), path = lfs.currentdir(),
parent = nil, parent = nil,
show_hidden = false, show_hidden = false,
@ -10,7 +11,8 @@ FileChooser = Menu:new{
} }
function FileChooser:init() function FileChooser:init()
self:changeToPath(self.path) self:updateItemTableFromPath(self.path)
Menu.init(self) -- call parent's init()
end end
function FileChooser:compressPath(item_path) function FileChooser:compressPath(item_path)
@ -22,11 +24,12 @@ function FileChooser:compressPath(item_path)
return path return path
end end
function FileChooser:changeToPath(path) function FileChooser:updateItemTableFromPath(path)
path = self:compressPath(path) path = self:compressPath(path)
local dirs = {} local dirs = {}
local files = {} local files = {}
self.path = path self.path = path
for f in lfs.dir(self.path) do for f in lfs.dir(self.path) do
if self.show_hidden or not string.match(f, "^%.[^.]") then if self.show_hidden or not string.match(f, "^%.[^.]") then
local filename = self.path.."/"..f local filename = self.path.."/"..f
@ -53,15 +56,16 @@ function FileChooser:changeToPath(path)
for _, file in ipairs(files) do for _, file in ipairs(files) do
table.insert(self.item_table, { text = file, path = self.path.."/"..file }) table.insert(self.item_table, { text = file, path = self.path.."/"..file })
end end
end
Menu.init(self) -- call parent's init() function FileChooser:changeToPath(path)
self:updateItemTableFromPath(path)
self:updateItems(1)
end end
function FileChooser:onMenuSelect(item) function FileChooser:onMenuSelect(item)
if lfs.attributes(item.path, "mode") == "directory" then if lfs.attributes(item.path, "mode") == "directory" then
UIManager:close(self)
self:changeToPath(item.path) self:changeToPath(item.path)
UIManager:show(self)
else else
self:onFileSelect(item.path) self:onFileSelect(item.path)
end end

Loading…
Cancel
Save