1
0
mirror of https://github.com/kepler155c/opus synced 2024-11-15 05:04:50 +00:00
opus/sys/apis/ui/fileui.lua

143 lines
2.9 KiB
Lua
Raw Normal View History

2017-10-01 00:35:36 +00:00
local UI = require('ui')
local Util = require('util')
2016-12-11 19:24:52 +00:00
2016-12-23 04:22:04 +00:00
return function(args)
2016-12-11 19:24:52 +00:00
local columns = {
2017-10-01 00:35:36 +00:00
{ heading = 'Name', key = 'name' },
2016-12-11 19:24:52 +00:00
}
if UI.term.width > 28 then
table.insert(columns,
2017-10-01 00:35:36 +00:00
{ heading = 'Size', key = 'size', width = 5 }
2016-12-11 19:24:52 +00:00
)
end
2016-12-23 04:22:04 +00:00
args = args or { }
2016-12-27 03:26:43 +00:00
local selectFile = UI.Dialog {
2016-12-23 04:22:04 +00:00
x = args.x or 3,
y = args.y or 2,
z = args.z or 2,
-- rex = args.rex or -3,
-- rey = args.rey or -3,
height = args.height,
width = args.width,
2017-10-06 07:07:24 +00:00
title = 'Select File',
2016-12-23 04:22:04 +00:00
grid = UI.ScrollingGrid {
2016-12-11 19:24:52 +00:00
x = 2,
y = 2,
2017-09-30 02:30:01 +00:00
ex = -2,
ey = -4,
2016-12-11 19:24:52 +00:00
path = '',
sortColumn = 'name',
columns = columns,
2016-12-23 04:22:04 +00:00
},
path = UI.TextEntry {
2016-12-11 19:24:52 +00:00
x = 2,
2017-09-30 02:30:01 +00:00
y = -2,
ex = -11,
2016-12-11 19:24:52 +00:00
limit = 256,
accelerators = {
enter = 'path_enter',
}
2016-12-23 04:22:04 +00:00
},
cancel = UI.Button {
2016-12-11 19:24:52 +00:00
text = 'Cancel',
2017-09-30 02:30:01 +00:00
x = -9,
y = -2,
2016-12-11 19:24:52 +00:00
event = 'cancel',
2016-12-23 04:22:04 +00:00
},
}
2016-12-11 19:24:52 +00:00
function selectFile:enable(path, fn)
self:setPath(path)
self.fn = fn
2017-10-03 04:50:54 +00:00
UI.Dialog.enable(self)
2016-12-11 19:24:52 +00:00
end
function selectFile:setPath(path)
self.grid.dir = path
while not fs.isDir(self.grid.dir) do
self.grid.dir = fs.getDir(self.grid.dir)
end
self.path.value = self.grid.dir
end
function selectFile.grid:draw()
2017-09-27 19:42:40 +00:00
local files = fs.listEx(self.dir)
2016-12-11 19:24:52 +00:00
if #self.dir > 0 then
table.insert(files, {
name = '..',
isDir = true,
})
end
self:setValues(files)
self:setIndex(1)
UI.Grid.draw(self)
end
function selectFile.grid:getDisplayValues(row)
if row.size then
row = Util.shallowCopy(row)
row.size = Util.toBytes(row.size)
end
return row
end
function selectFile.grid:getRowTextColor(file, selected)
if file.isDir then
return colors.cyan
end
if file.isReadOnly then
return colors.pink
end
return colors.white
end
function selectFile.grid:sortCompare(a, b)
if self.sortColumn == 'size' then
return a.size < b.size
end
if a.isDir == b.isDir then
return a.name:lower() < b.name:lower()
end
return a.isDir
end
function selectFile:eventHandler(event)
if event.type == 'grid_select' then
self.grid.dir = fs.combine(self.grid.dir, event.selected.name)
self.path.value = self.grid.dir
if event.selected.isDir then
self.grid:draw()
self.path:draw()
else
UI:setPreviousPage()
self.fn(self.path.value)
end
elseif event.type == 'path_enter' then
if fs.isDir(self.path.value) then
self:setPath(self.path.value)
self.grid:draw()
self.path:draw()
else
UI:setPreviousPage()
self.fn(self.path.value)
end
elseif event.type == 'cancel' then
UI:setPreviousPage()
self.fn()
else
2017-10-03 04:50:54 +00:00
return UI.Dialog.eventHandler(self, event)
2016-12-11 19:24:52 +00:00
end
return true
end
return selectFile
end