mirror of
https://github.com/koreader/koreader.git
synced 2025-08-10 00:52:38 +00:00
first filechooser implementation
This commit is contained in:
@@ -409,7 +409,6 @@ Menu = FocusManager:new{
|
||||
height = 500,
|
||||
width = 500,
|
||||
item_table = {},
|
||||
items = 0,
|
||||
item_shortcuts = {
|
||||
"Q", "W", "E", "R", "T", "Y", "U", "I", "O", "P",
|
||||
"A", "S", "D", "F", "G", "H", "J", "K", "L", "Del",
|
||||
@@ -427,10 +426,9 @@ Menu = FocusManager:new{
|
||||
}
|
||||
|
||||
function Menu:init()
|
||||
self.items = #self.item_table
|
||||
self.perpage = math.floor(self.height / self.item_height) - 2
|
||||
self.page = 1
|
||||
self.page_num = math.ceil(self.items / self.perpage)
|
||||
self.page_num = math.ceil(#self.item_table / self.perpage)
|
||||
|
||||
-- set up keyboard events
|
||||
self.key_events.Close = { {"Back"}, doc = "close menu" }
|
||||
@@ -472,16 +470,20 @@ function Menu:init()
|
||||
dimen = {w = G_width, h = G_height},
|
||||
} -- CenterContainer
|
||||
|
||||
self:_updateItems()
|
||||
if #self.item_table > 0 then
|
||||
-- if the table is not yet initialized, this call
|
||||
-- must be done manually:
|
||||
self:updateItems()
|
||||
end
|
||||
end
|
||||
|
||||
function Menu:_updateItems()
|
||||
function Menu:updateItems()
|
||||
self.layout = {}
|
||||
self.item_group:clear()
|
||||
|
||||
for c = 1, self.perpage do
|
||||
local i = (self.page - 1) * self.perpage + c
|
||||
if i <= self.items then
|
||||
if i <= #self.item_table then
|
||||
local item_shortcut = nil
|
||||
local shortcut_style = "square"
|
||||
if self.is_enable_shortcut then
|
||||
@@ -515,6 +517,8 @@ function Menu:_updateItems()
|
||||
self.selected = { x = 1, y = 1 }
|
||||
-- update page information
|
||||
self.page_info.text = "page "..self.page.."/"..self.page_num
|
||||
|
||||
UIManager:setDirty(self)
|
||||
end
|
||||
|
||||
function Menu:onSelectByShortCut(_, keyevent)
|
||||
@@ -535,8 +539,7 @@ end
|
||||
function Menu:onNextPage()
|
||||
if self.page < self.page_num then
|
||||
self.page = self.page + 1
|
||||
self:_updateItems()
|
||||
UIManager:setDirty(self)
|
||||
self:updateItems()
|
||||
end
|
||||
return true
|
||||
end
|
||||
@@ -544,8 +547,7 @@ end
|
||||
function Menu:onPrevPage()
|
||||
if self.page > 1 then
|
||||
self.page = self.page - 1
|
||||
self:_updateItems()
|
||||
UIManager:setDirty(self)
|
||||
self:updateItems()
|
||||
end
|
||||
return true
|
||||
end
|
||||
|
||||
42
frontend/ui/filechooser.lua
Normal file
42
frontend/ui/filechooser.lua
Normal file
@@ -0,0 +1,42 @@
|
||||
require "ui/dialog" -- for Menu
|
||||
|
||||
FileChooser = Menu:new{
|
||||
path = ".",
|
||||
show_hidden = false,
|
||||
filter = function(filename) return true end,
|
||||
}
|
||||
|
||||
function FileChooser:init()
|
||||
self:changeToPath(self.path)
|
||||
end
|
||||
|
||||
function FileChooser:changeToPath(path)
|
||||
local dirs = {}
|
||||
local files = {}
|
||||
for f in lfs.dir(self.path) do
|
||||
if self.show_hidden or not string.match(f, "^%.[^.]") then
|
||||
local filename = self.path.."/"..f
|
||||
local filemode = lfs.attributes(filename, "mode")
|
||||
if filemode == "directory" and f ~= "." and f~=".." then
|
||||
table.insert(dirs, f)
|
||||
elseif filemode == "file" then
|
||||
if self.filter(filename) then
|
||||
table.insert(files, f)
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
table.sort(dirs)
|
||||
if self.path ~= "/" then table.insert(dirs, 1, "..") end
|
||||
table.sort(files)
|
||||
|
||||
self.item_table = {}
|
||||
for _, dir in ipairs(dirs) do
|
||||
table.insert(self.item_table, { text = dir.."/" })
|
||||
end
|
||||
for _, file in ipairs(files) do
|
||||
table.insert(self.item_table, { text = file })
|
||||
end
|
||||
|
||||
Menu.init(self) -- call parent's init()
|
||||
end
|
||||
Reference in New Issue
Block a user