accelerators for any event + more inspect examples

This commit is contained in:
kepler155c@gmail.com 2019-11-13 21:50:00 -07:00
parent db48031c7c
commit 3241326a2f
10 changed files with 140 additions and 14 deletions

View File

@ -1,4 +1,5 @@
local UI = require('opus.ui')
local UI = require('opus.ui')
local Util = require('opus.util')
local colors = _G.colors
local multishell = _ENV.multishell
@ -6,6 +7,7 @@ local multishell = _ENV.multishell
local name = ({ ... })[1] or error('Syntax: inspect COMPONENT')
local events = { }
local page
local lastEvent
local function isRelevant(el)
return page.testContainer == el or el.parent and isRelevant(el.parent)
@ -13,7 +15,8 @@ end
local emitter = UI.Window.emit
function UI.Window:emit(event)
if not event._recorded and isRelevant(self) then
if event ~= lastEvent and isRelevant(self) then
lastEvent = event
local t = { }
for k,v in pairs(event) do
if k ~= 'type' and k ~= 'recorded' then
@ -21,7 +24,7 @@ function UI.Window:emit(event)
end
end
table.insert(events, 1, { type = event.type, value = table.concat(t, ' '), raw = event })
while #events > 10 do
while #events > 20 do
table.remove(events)
end
page.tabs.events.grid:update()
@ -29,7 +32,6 @@ function UI.Window:emit(event)
page.tabs.events.grid:draw()
end
end
event._recorded = true
return emitter(self, event)
end
@ -72,7 +74,14 @@ page = UI.Page {
},
events = UI.Tab {
tabTitle = 'Events',
UI.MenuBar {
y = -1,
buttons = {
{ text = 'Clear', event = 'event_clear' },
}
},
grid = UI.ScrollingGrid {
ey = -2,
headerBackgroundColor = colors.red,
values = events,
autospace = true,
@ -80,6 +89,9 @@ page = UI.Page {
{ heading = 'type', key = 'type' },
{ heading = 'value', key = 'value', }
},
accelerators = {
grid_select = 'event_inspect',
},
}
}
},
@ -122,15 +134,19 @@ page = UI.Page {
self.tabs.methodsTab.grid:update()
self.tabs.methodsTab.grid:draw()
elseif event.type == 'grid_select' and event.element == self.tabs.events.grid then
event.selected.raw._recorded = nil
elseif event.type == 'event_clear' then
Util.clear(self.tabs.events.grid.values)
self.tabs.events.grid:update()
self.tabs.events.grid:draw()
elseif event.type == 'event_inspect' then
multishell.openTab({
path = 'sys/apps/Lua.lua',
args = { event.selected.raw },
focused = true,
})
elseif event.type == 'grid_select' and event.element == self.tabs.properties.grid then
elseif event.type == 'edit_property' then
self.editor.entry.value = event.selected.value
self.editor:show()

View File

@ -912,12 +912,16 @@ end
function UI.Window:emit(event)
local parent = self
while parent do
if parent.accelerators and event.key then -- not ideal
-- could be [event.key or event.type] to support accelerators
-- for non-input type events
local acc = parent.accelerators[event.key]
if acc and parent:emit({ type = acc, element = parent }) then
return true
if parent.accelerators then
-- events types can be made unique via accelerators
local acc = parent.accelerators[event.key or event.type]
if acc and acc ~= event.type then -- don't get stuck in a loop
local event2 = Util.shallowCopy(event)
event2.type = acc
event2.key = nil
if parent:emit(event2) then
return true
end
end
end
if parent.eventHandler then

View File

@ -61,3 +61,9 @@ function UI.Checkbox:eventHandler(event)
return true
end
end
function UI.Checkbox.example()
return UI.Checkbox {
x = 2, y = 2,
}
end

View File

@ -53,7 +53,7 @@ function UI.Chooser:eventHandler(event)
if event.key == 'right' or event.key == 'space' then
local _,k = Util.find(self.choices, 'value', self.value)
local choice
if not k then k = 1 end
if not k then k = 0 end
if k and k < #self.choices then
choice = self.choices[k+1]
else
@ -86,3 +86,25 @@ function UI.Chooser:eventHandler(event)
end
end
end
function UI.Chooser.example()
return UI.Window {
a = UI.Chooser {
x = 2, y = 2,
choices = {
{ name = 'choice1', value = 'value1' },
{ name = 'choice2', value = 'value2' },
{ name = 'choice3', value = 'value3' },
},
value = 'value2',
},
b = UI.Chooser {
x = 2, y = 4,
choices = {
{ name = 'choice1', value = 'value1' },
{ name = 'choice2', value = 'value2' },
{ name = 'choice3', value = 'value3' },
},
}
}
end

View File

@ -73,3 +73,16 @@ function UI.DropMenu:eventHandler(event)
end
return true
end
function UI.DropMenu.example()
return UI.MenuBar {
buttons = {
{ text = 'File', dropdown = {
{ text = 'Run', event = 'run' },
{ text = 'Shell s', event = 'shell' },
{ spacer = true },
{ text = 'Quit ^q', event = 'quit' },
} },
}
}
end

View File

@ -134,3 +134,23 @@ function UI.Form:eventHandler(event)
end
return true
end
function UI.Form.example()
return UI.Form {
x = 2, ex = -2, y = 2,
ptype = UI.Chooser {
formLabel = 'Type', formKey = 'type',
width = 10,
choices = {
{ name = 'Modem', value = 'wireless_modem' },
{ name = 'Drive', value = 'disk_drive' },
},
},
drive_id = UI.TextEntry {
formLabel = 'Drive', formKey = 'drive_id',
required = true,
width = 5,
transform = 'number',
},
}
end

View File

@ -512,6 +512,9 @@ function UI.Grid.example()
{ heading = 'key', key = 'key' },
{ heading = 'value', key = 'value' },
},
accelerators = {
grid_select = 'custom_select',
}
},
noheader = UI.Grid {
ex = '48%', y = 6, ey = -2,

View File

@ -88,3 +88,12 @@ function UI.MenuBar:eventHandler(event)
return true
end
end
function UI.MenuBar.example()
return UI.MenuBar {
buttons = {
{ text = 'Choice1', event = 'event1' },
{ text = 'Choice2', event = 'event2' },
}
}
end

View File

@ -90,3 +90,28 @@ function UI.Notification:eventHandler(event)
end
end
end
function UI.Notification.example()
return UI.Window {
notify = UI.Notification {
anchor = 'top',
},
button1 = UI.Button {
x = 2, y = 3,
text = 'success',
event = 'test_success',
},
button2 = UI.Button {
x = 2, y = 5,
text = 'error',
event = 'test_error',
},
eventHandler = function (self, event)
if event.type == 'test_success' then
self.notify:success('Example text')
elseif event.type == 'test_error' then
self.notify:error('Example text')
end
end,
}
end

View File

@ -75,3 +75,11 @@ function UI.Slider:eventHandler(event)
self:draw()
end
end
function UI.Slider.example()
return UI.Slider {
y = 2, x = 2, ex = -2,
min = 0, max = 1,
help = 'Volume setting',
}
end