slight refactoring of filechooser so it remembers state

pull/2/merge
HW 13 years ago
parent d89fd83235
commit c47512a01d

@ -3,15 +3,32 @@ require "keys"
require "graphics" require "graphics"
FileChooser = { FileChooser = {
-- Class vars:
-- font for displaying file/dir names
face = freetype.newBuiltinFace("sans", 25),
fhash = "s25",
-- font for paging display
sface = freetype.newBuiltinFace("sans", 16),
sfhash = "s16",
-- spacing between lines
spacing = 40,
-- state buffer
dirs = nil, dirs = nil,
files = nil files = nil,
items = 0,
path = "",
page = 1,
current = 1,
oldcurrent = 0,
} }
function FileChooser:readdir(path) function FileChooser:readdir()
self.dirs = {} self.dirs = {}
self.files = {} self.files = {}
for f in lfs.dir(path) do for f in lfs.dir(self.path) do
if lfs.attributes(path.."/"..f, "mode") == "directory" and f ~= "." and not string.match(f, "^%.[^.]") then if lfs.attributes(self.path.."/"..f, "mode") == "directory" and f ~= "." and not string.match(f, "^%.[^.]") then
table.insert(self.dirs, f) table.insert(self.dirs, f)
elseif string.match(f, ".+%.[pP][dD][fF]$") then elseif string.match(f, ".+%.[pP][dD][fF]$") then
table.insert(self.files, f) table.insert(self.files, f)
@ -21,120 +38,116 @@ function FileChooser:readdir(path)
table.sort(self.files) table.sort(self.files)
end end
function FileChooser:choose(startpath, ypos, height) function FileChooser:setPath(newPath)
local face = freetype.newBuiltinFace("sans", 25) self.path = newPath
local fhash = "s25" self:readdir()
local sface = freetype.newBuiltinFace("sans", 16) self.items = #self.dirs + #self.files
local sfhash = "s16" if self.items == 0 then
local path = startpath return nil
local spacing = 40 end
local perpage = math.floor(height / spacing) - 1 self.page = 1
local pathdirty = true self.current = 1
local pagedirty = false return true
local framebufferdirty = false end
local markerdirty = true
local oldcurrent = 0 function FileChooser:choose(ypos, height)
local page local perpage = math.floor(height / self.spacing) - 1
local current local pagedirty = true
local items local markerdirty = false
while true do while true do
if pathdirty then
print("showing file chooser in <"..path..">")
self:readdir(path)
items = #self.dirs + #self.files
if items == 0 then
return nil
end
page = 1
current = 1
pathdirty = false
pagedirty = true
end
if pagedirty then if pagedirty then
fb.bb:paintRect(0, ypos, fb.bb:getWidth(), height, 0) fb.bb:paintRect(0, ypos, fb.bb:getWidth(), height, 0)
local c local c
for c = 1, perpage do for c = 1, perpage do
local i = (page - 1) * perpage + c local i = (self.page - 1) * perpage + c
if i <= #self.dirs then if i <= #self.dirs then
renderUtf8Text(fb.bb, 39, ypos + spacing*c, face, fhash, "/", true) -- resembles display in midnight commander: adds "/" prefix for directories
renderUtf8Text(fb.bb, 50, ypos + spacing*c, face, fhash, self.dirs[i], true) renderUtf8Text(fb.bb, 39, ypos + self.spacing*c, self.face, self.fhash, "/", true)
elseif i <= items then renderUtf8Text(fb.bb, 50, ypos + self.spacing*c, self.face, self.fhash, self.dirs[i], true)
renderUtf8Text(fb.bb, 50, ypos + spacing*c, face, fhash, self.files[i-#self.dirs], true) elseif i <= self.items then
renderUtf8Text(fb.bb, 50, ypos + self.spacing*c, self.face, self.fhash, self.files[i-#self.dirs], true)
end end
end end
renderUtf8Text(fb.bb, 39, ypos + spacing * perpage + 32, sface, sfhash, renderUtf8Text(fb.bb, 39, ypos + self.spacing * perpage + 32, self.sface, self.sfhash,
"Page "..page.." of "..(math.floor(items / perpage)+1), true) "Page "..self.page.." of "..(math.floor(self.items / perpage)+1), true)
framebufferdirty = true
markerdirty = true markerdirty = true
pagedirty = false
end end
if markerdirty then if markerdirty then
if oldcurrent > 0 then if not pagedirty then
fb.bb:paintRect(30, ypos + spacing*oldcurrent + 10, fb.bb:getWidth() - 60, 3, 0) if self.oldcurrent > 0 then
fb:refresh(1, ypos + spacing*oldcurrent + 10, fb.bb:getWidth() - 60, 3) fb.bb:paintRect(30, ypos + self.spacing*self.oldcurrent + 10, fb.bb:getWidth() - 60, 3, 0)
fb:refresh(1, 30, ypos + self.spacing*self.oldcurrent + 10, fb.bb:getWidth() - 60, 3)
end
end
fb.bb:paintRect(30, ypos + self.spacing*self.current + 10, fb.bb:getWidth() - 60, 3, 15)
if not pagedirty then
fb:refresh(1, 30, ypos + self.spacing*self.current + 10, fb.bb:getWidth() - 60, 3)
end end
fb.bb:paintRect(30, ypos + spacing*current + 10, fb.bb:getWidth() - 60, 3, 15) self.oldcurrent = self.current
fb:refresh(1, ypos + spacing*current + 10, fb.bb:getWidth() - 60, 3)
oldcurrent = current
markerdirty = false markerdirty = false
end end
if pagedirty then
fb:refresh(0, 0, ypos, fb.bb:getWidth(), height)
pagedirty = false
end
local ev = input.waitForEvent() local ev = input.waitForEvent()
if ev.type == EV_KEY and ev.value == EVENT_VALUE_KEY_PRESS then if ev.type == EV_KEY and ev.value == EVENT_VALUE_KEY_PRESS then
if ev.code == KEY_FW_UP then if ev.code == KEY_FW_UP then
if current == 1 then if self.current == 1 then
if page > 1 then if self.page > 1 then
current = perpage self.current = self.perpage
page = page - 1 self.page = self.page - 1
pagedirty = true pagedirty = true
end end
else else
current = current - 1 self.current = self.current - 1
markerdirty = true markerdirty = true
end end
elseif ev.code == KEY_FW_DOWN then elseif ev.code == KEY_FW_DOWN then
if current == perpage then if self.current == perpage then
if page < (items / perpage) then if self.page < (self.items / perpage) then
current = 1 self.current = 1
page = page + 1 self.page = page + 1
pagedirty = true pagedirty = true
end end
else else
if page ~= math.floor(items / perpage) + 1 if self.page ~= math.floor(self.items / perpage) + 1
or current + (page-1)*perpage < items then or self.current + (self.page-1)*perpage < self.items then
current = current + 1 self.current = self.current + 1
markerdirty = true markerdirty = true
end end
end end
elseif ev.code == KEY_PGFWD then elseif ev.code == KEY_PGFWD then
if page < (items / perpage) then if self.page < (self.items / perpage) then
if current + page*perpage > items then if self.current + self.page*perpage > self.items then
current = items - page*perpage self.current = self.items - self.page*perpage
end end
page = page + 1 self.page = self.page + 1
pagedirty = true pagedirty = true
else else
current = items - (page-1)*perpage self.current = self.items - (self.page-1)*perpage
markerdirty = true markerdirty = true
end end
elseif ev.code == KEY_PGBCK then elseif ev.code == KEY_PGBCK then
if page > 1 then if self.page > 1 then
page = page - 1 self.page = self.page - 1
pagedirty = true pagedirty = true
else else
current = 1 self.current = 1
markerdirty = true markerdirty = true
end end
elseif ev.code == KEY_ENTER or ev.code == KEY_FWPRESS then elseif ev.code == KEY_ENTER or ev.code == KEY_FWPRESS then
local newdir = self.dirs[perpage*(page-1)+current] local newdir = self.dirs[perpage*(self.page-1)+self.current]
if newdir == ".." then if newdir == ".." then
path = string.gsub(path, "(.*)/[^/]+/?$", "%1") local path = string.gsub(self.path, "(.*)/[^/]+/?$", "%1")
pathdirty = true self:setPath(path)
elseif newdir then elseif newdir then
path = path.."/"..newdir local path = self.path.."/"..newdir
pathdirty = true self:setPath(path)
else else
return path.."/"..self.files[perpage*(page-1)+current - #self.dirs] return self.path.."/"..self.files[perpage*(self.page-1)+self.current - #self.dirs]
end end
pagedirty = true
elseif ev.code == KEY_BACK then elseif ev.code == KEY_BACK then
return nil return nil
end end

@ -74,8 +74,9 @@ width, height = fb:getSize()
if lfs.attributes(ARGV[optind], "mode") == "directory" then if lfs.attributes(ARGV[optind], "mode") == "directory" then
local running = true local running = true
FileChooser:setPath(ARGV[optind])
while running do while running do
local pdffile = FileChooser:choose(ARGV[optind],0,height) local pdffile = FileChooser:choose(0,height)
if pdffile ~= nil then if pdffile ~= nil then
PDFReader:open(pdffile,"") -- TODO: query for password PDFReader:open(pdffile,"") -- TODO: query for password
PDFReader:goto(tonumber(PDFReader.settings:readsetting("last_page") or 1)) PDFReader:goto(tonumber(PDFReader.settings:readsetting("last_page") or 1))

Loading…
Cancel
Save