better colors - resizing

This commit is contained in:
kepler155c@gmail.com 2017-09-30 20:35:36 -04:00
parent 5681b01810
commit c0baa00668
8 changed files with 84 additions and 89 deletions

View File

@ -2,7 +2,7 @@ local json = require('json')
local Util = require('util') local Util = require('util')
local TREE_URL = 'https://api.github.com/repos/%s/%s/git/trees/%s?recursive=1' local TREE_URL = 'https://api.github.com/repos/%s/%s/git/trees/%s?recursive=1'
local FILE_URL = 'https://raw.github.com/%s/%s/%s/%s' local FILE_URL = 'https://raw.githubusercontent.com/%s/%s/%s/%s'
local git = { } local git = { }

View File

@ -608,6 +608,19 @@ function UI.Window:setParent()
self:initChildren() self:initChildren()
end end
function UI.Window:resize()
self.height, self.width = self.oh, self.ow
self.x, self.y = self.ox, self.oy
setSize(self)
if self.children then
for _,child in ipairs(self.children) do
child:resize()
end
end
end
function UI.Window:add(children) function UI.Window:add(children)
UI.setProperties(self, children) UI.setProperties(self, children)
self:initChildren() self:initChildren()
@ -630,7 +643,7 @@ end
function UI.Window:draw() function UI.Window:draw()
self:clear(self.backgroundColor) self:clear(self.backgroundColor)
if self.children then if self.children then
for k,child in pairs(self.children) do for _,child in pairs(self.children) do
if child.enabled then if child.enabled then
child:draw() child:draw()
end end
@ -644,26 +657,10 @@ function UI.Window:sync()
end end
end end
function UI.Window:resize()
self.height = self.oh
self.width = self.ow
self.x = self.ox
self.y = self.oy
setSize(self)
if self.children then
for _,child in ipairs(self.children) do
child:resize()
end
end
end
function UI.Window:enable() function UI.Window:enable()
self.enabled = true self.enabled = true
if self.children then if self.children then
for k,child in pairs(self.children) do for _,child in pairs(self.children) do
child:enable() child:enable()
end end
end end
@ -672,7 +669,7 @@ end
function UI.Window:disable() function UI.Window:disable()
self.enabled = false self.enabled = false
if self.children then if self.children then
for k,child in pairs(self.children) do for _,child in pairs(self.children) do
child:disable() child:disable()
end end
end end
@ -1305,8 +1302,6 @@ function UI.Device:runTransitions(transitions, canvas)
canvas:blitClipped(self.device) -- and blit the remainder canvas:blitClipped(self.device) -- and blit the remainder
canvas:reset() canvas:reset()
local queue = { } -- don't miss events while transition is running
-- especially timers
while true do while true do
for _,k in ipairs(Util.keys(transitions)) do for _,k in ipairs(Util.keys(transitions)) do
local transition = transitions[k] local transition = transitions[k]
@ -1318,20 +1313,7 @@ function UI.Device:runTransitions(transitions, canvas)
break break
end end
os.sleep(0) os.sleep(0)
--[[
local timerId = os.startTimer(0)
while true do
local e = { os.pullEvent() }
if e[1] == 'timer' and e[2] == timerId then
break
end
table.insert(queue, e)
end
--]]
end end
-- for _, e in ipairs(queue) do
-- Event.processEvent(e)
-- end
end end
function UI.Device:sync() function UI.Device:sync()
@ -1359,6 +1341,7 @@ function UI.Device:sync()
end end
--[[-- StringBuffer --]]-- --[[-- StringBuffer --]]--
-- justs optimizes string concatenations
UI.StringBuffer = class() UI.StringBuffer = class()
function UI.StringBuffer:init(bufSize) function UI.StringBuffer:init(bufSize)
self.bufSize = bufSize self.bufSize = bufSize
@ -1384,7 +1367,7 @@ function UI.StringBuffer:clear()
self.buffer = { } self.buffer = { }
end end
-- alternate - better ? -- For manipulating text in a fixed width string
local SB = { } local SB = { }
function SB:new(width) function SB:new(width)
return setmetatable({ return setmetatable({
@ -1476,7 +1459,6 @@ function UI.Page:focusPrevious()
end end
local focused = getPreviousFocus(self.focused) local focused = getPreviousFocus(self.focused)
if focused then if focused then
self:setFocus(focused) self:setFocus(focused)
end end
@ -1639,11 +1621,14 @@ function UI.Grid:adjustWidth()
else else
for k,c in ipairs(t) do for k,c in ipairs(t) do
c.cw = #(t[1].heading or '') c.cw = #(c.heading or '')
w = w - c.cw w = w - c.cw
end end
-- adjust the size to the length of the value -- adjust the size to the length of the value
for key,row in pairs(self.values) do for key,row in pairs(self.values) do
if w <= 0 then
break
end
row = self:getDisplayValues(row, key) row = self:getDisplayValues(row, key)
for _,col in pairs(t) do for _,col in pairs(t) do
local value = row[col.key] local value = row[col.key]
@ -1659,9 +1644,6 @@ function UI.Grid:adjustWidth()
end end
end end
end end
if w <= 0 then
break
end
end end
-- last column does not get padding (right alignment) -- last column does not get padding (right alignment)
@ -2274,22 +2256,22 @@ function UI.MenuBar:init(args)
local buttonProperties = { local buttonProperties = {
x = x, x = x,
width = #button.text + self.spacing, width = #button.text + self.spacing,
backgroundColor = self.backgroundColor, -- backgroundColor = self.backgroundColor,
backgroundFocusColor = colors.gray, -- backgroundFocusColor = colors.gray,
textColor = self.textColor, -- textColor = self.textColor,
centered = false, centered = false,
} }
x = x + buttonProperties.width x = x + buttonProperties.width
UI.setProperties(buttonProperties, button) UI.setProperties(buttonProperties, button)
if button.name then if button.name then
self[button.name] = UI.Button(buttonProperties) self[button.name] = UI.MenuItem(buttonProperties)
else else
table.insert(self.children, UI.Button(buttonProperties)) table.insert(self.children, UI.MenuItem(buttonProperties))
end end
end end
end end
if self.showBackButton then if self.showBackButton then
table.insert(self.children, UI.Button({ table.insert(self.children, UI.MenuItem({
x = UI.term.width - 2, x = UI.term.width - 2,
width = 3, width = 3,
backgroundColor = self.backgroundColor, backgroundColor = self.backgroundColor,
@ -2324,7 +2306,7 @@ end
UI.DropMenu = class(UI.MenuBar) UI.DropMenu = class(UI.MenuBar)
UI.DropMenu.defaults = { UI.DropMenu.defaults = {
UIElement = 'DropMenu', UIElement = 'DropMenu',
backgroundColor = colors.white, backgroundColor = colors.lightGray,
} }
function UI.DropMenu:init(args) function UI.DropMenu:init(args)
local defaults = UI:getDefaults(UI.DropMenu, args) local defaults = UI:getDefaults(UI.DropMenu, args)
@ -2332,7 +2314,6 @@ function UI.DropMenu:init(args)
end end
function UI.DropMenu:setParent() function UI.DropMenu:setParent()
UI.MenuBar.setParent(self) UI.MenuBar.setParent(self)
local maxWidth = 1 local maxWidth = 1
@ -2868,7 +2849,8 @@ UI.Button.defaults = {
text = 'button', text = 'button',
backgroundColor = colors.gray, backgroundColor = colors.gray,
backgroundFocusColor = colors.lightGray, backgroundFocusColor = colors.lightGray,
textFocusColor = colors.black, textFocusColor = colors.white,
textColor = colors.white,
centered = true, centered = true,
height = 1, height = 1,
focusIndicator = '>', focusIndicator = '>',
@ -2924,6 +2906,21 @@ function UI.Button:eventHandler(event)
return false return false
end end
--[[-- MenuItem --]]--
UI.MenuItem = class(UI.Button)
UI.MenuItem.defaults = {
UIElement = 'MenuItem',
textColor = colors.black,
backgroundColor = colors.lightGray,
textFocusColor = colors.white,
backgroundFocusColor = colors.lightGray,
}
function UI.MenuItem:init(args)
local defaults = UI:getDefaults(UI.MenuItem, args)
UI.Button.init(self, defaults)
end
--[[-- TextEntry --]]-- --[[-- TextEntry --]]--
UI.TextEntry = class(UI.Window) UI.TextEntry = class(UI.Window)
UI.TextEntry.defaults = { UI.TextEntry.defaults = {

View File

@ -1,16 +1,15 @@
local Util = require('util') local UI = require('ui')
local UI = require('ui') local Util = require('util')
return function(args) return function(args)
local columns = { local columns = {
{ heading = 'Name', key = 'name', width = UI.term.width - 9 }, { heading = 'Name', key = 'name' },
} }
if UI.term.width > 28 then if UI.term.width > 28 then
columns[1].width = UI.term.width - 16
table.insert(columns, table.insert(columns,
{ heading = 'Size', key = 'size', width = 6 } { heading = 'Size', key = 'size', width = 5 }
) )
end end

View File

@ -7,42 +7,39 @@ multishell.setTitle(multishell.getCurrent(), 'Help')
UI:configure('Help', ...) UI:configure('Help', ...)
local files = { } local files = { }
for _,f in pairs(fs.list('/rom/help')) do for _,f in pairs(help.topics()) do
table.insert(files, { name = f }) table.insert(files, { name = f })
end end
local page = UI.Page({ local page = UI.Page {
labelText = UI.Text({ labelText = UI.Text {
y = 2, x = 3, y = 2,
x = 3,
value = 'Search', value = 'Search',
}), },
filter = UI.TextEntry({ filter = UI.TextEntry {
y = 2, x = 10, y = 2, ex = -3,
x = 10,
width = UI.term.width - 13,
limit = 32, limit = 32,
}), },
grid = UI.ScrollingGrid({ grid = UI.ScrollingGrid {
y = 4, y = 4,
height = UI.term.height - 4,
values = files, values = files,
columns = { columns = {
{ heading = 'Name', key = 'name', width = 12 }, { heading = 'Name', key = 'name' },
}, },
sortColumn = 'name', sortColumn = 'name',
}),
statusBar = UI.StatusBar(),
accelerators = {
q = 'quit',
}, },
}) accelerators = {
q = 'quit',
enter = 'grid_select',
},
}
local function showHelp(name) local function showHelp(name)
UI.term:reset() UI.term:reset()
shell.run('help ' .. name) shell.run('help ' .. name)
print('Press enter to return') print('Press enter to return')
repeat repeat
os.pullEvent('key')
local _, k = os.pullEvent('key_up') local _, k = os.pullEvent('key_up')
until k == keys.enter until k == keys.enter
end end
@ -52,18 +49,13 @@ function page:eventHandler(event)
if event.type == 'quit' then if event.type == 'quit' then
Event.exitPullEvents() Event.exitPullEvents()
elseif event.type == 'key' and event.key == 'enter' then elseif event.type == 'grid_select' then
if self.grid:getSelected() then if self.grid:getSelected() then
showHelp(self.grid:getSelected().name) showHelp(self.grid:getSelected().name)
self:setFocus(self.filter) self:setFocus(self.filter)
self:draw() self:draw()
end end
elseif event.type == 'grid_select' then
showHelp(event.selected.name)
self:setFocus(self.filter)
self:draw()
elseif event.type == 'text_change' then elseif event.type == 'text_change' then
local text = event.text local text = event.text
if #text == 0 then if #text == 0 then

View File

@ -15,7 +15,7 @@ local gridColumns = {
} }
if UI.term.width >= 30 then if UI.term.width >= 30 then
table.insert(gridColumns, { heading = 'Fuel', key = 'fuel' }) table.insert(gridColumns, { heading = 'Fuel', key = 'fuel', width = 5 })
table.insert(gridColumns, { heading = 'Uptime', key = 'uptime' }) table.insert(gridColumns, { heading = 'Uptime', key = 'uptime' })
end end

View File

@ -94,8 +94,8 @@ local systemPage = UI.Page {
}, },
selectable = false, selectable = false,
columns = { columns = {
{ key = 'name', width = 12 }, { key = 'name', width = 12 },
{ key = 'value', width = UI.term.width - 15 }, { key = 'value' },
}, },
}, },
}, },

View File

@ -53,7 +53,7 @@ function shell.run(...)
if path then if path then
tProgramStack[#tProgramStack + 1] = path tProgramStack[#tProgramStack + 1] = path
local oldTitle local oldTitle
if multishell and multishell.getTitle then if multishell and multishell.getTitle then
oldTitle = multishell.getTitle(multishell.getCurrent()) oldTitle = multishell.getTitle(multishell.getCurrent())
multishell.setTitle(multishell.getCurrent(), fs.getName(path)) multishell.setTitle(multishell.getCurrent(), fs.getName(path))
@ -67,11 +67,9 @@ function shell.run(...)
else else
result, err = Util.run(env, path, unpack(args)) result, err = Util.run(env, path, unpack(args))
end end
tProgramStack[#tProgramStack] = nil
if multishell and multishell.getTitle then if multishell and multishell.getTitle then
local title = 'shell'
if #tProgramStack > 0 then
title = fs.getName(tProgramStack[#tProgramStack])
end
multishell.setTitle(multishell.getCurrent(), oldTitle or 'shell') multishell.setTitle(multishell.getCurrent(), oldTitle or 'shell')
end end
@ -257,6 +255,7 @@ if #tArgs > 0 then
if not fn then if not fn then
error(err) error(err)
end end
tProgramStack[#tProgramStack + 1] = path tProgramStack[#tProgramStack + 1] = path
return fn(table.unpack(args)) return fn(table.unpack(args))
end end

View File

@ -231,7 +231,15 @@
icon = "\030f\031f \03131\0308\031f \030f\031d2\ icon = "\030f\031f \03131\0308\031f \030f\031d2\
\030f\031f \031d2\03131\0308\031f \030f\03131\ \030f\031f \031d2\03131\0308\031f \030f\03131\
\030f\03131\0308\031f \030f\03131\031e3", \030f\03131\0308\031f \030f\03131\031e3",
run = "http://pastebin.com/raw/nsKrHTbN", run = "https://pastebin.com/raw/nsKrHTbN",
},
[ "48d6857f6b2869d031f463b13aa34df47e18c548" ] = {
title = "Breakout",
category = "Games",
icon = "\0301\031f \0309 \030c \030b \030e \030c \0306 \
\030 \031f \
\030 \031f \0300 \0310 ",
run = "https://pastebin.com/raw/LTRYaSKt",
}, },
[ "8d59207c8a84153b3e9f035cc3b6ec7a23671323" ] = { [ "8d59207c8a84153b3e9f035cc3b6ec7a23671323" ] = {
title = "Micropaint", title = "Micropaint",