mirror of
https://github.com/kepler155c/opus
synced 2025-05-05 17:04:13 +00:00
accelerators for any event + more inspect examples
This commit is contained in:
parent
db48031c7c
commit
3241326a2f
@ -1,4 +1,5 @@
|
|||||||
local UI = require('opus.ui')
|
local UI = require('opus.ui')
|
||||||
|
local Util = require('opus.util')
|
||||||
|
|
||||||
local colors = _G.colors
|
local colors = _G.colors
|
||||||
local multishell = _ENV.multishell
|
local multishell = _ENV.multishell
|
||||||
@ -6,6 +7,7 @@ local multishell = _ENV.multishell
|
|||||||
local name = ({ ... })[1] or error('Syntax: inspect COMPONENT')
|
local name = ({ ... })[1] or error('Syntax: inspect COMPONENT')
|
||||||
local events = { }
|
local events = { }
|
||||||
local page
|
local page
|
||||||
|
local lastEvent
|
||||||
|
|
||||||
local function isRelevant(el)
|
local function isRelevant(el)
|
||||||
return page.testContainer == el or el.parent and isRelevant(el.parent)
|
return page.testContainer == el or el.parent and isRelevant(el.parent)
|
||||||
@ -13,7 +15,8 @@ end
|
|||||||
|
|
||||||
local emitter = UI.Window.emit
|
local emitter = UI.Window.emit
|
||||||
function UI.Window:emit(event)
|
function UI.Window:emit(event)
|
||||||
if not event._recorded and isRelevant(self) then
|
if event ~= lastEvent and isRelevant(self) then
|
||||||
|
lastEvent = event
|
||||||
local t = { }
|
local t = { }
|
||||||
for k,v in pairs(event) do
|
for k,v in pairs(event) do
|
||||||
if k ~= 'type' and k ~= 'recorded' then
|
if k ~= 'type' and k ~= 'recorded' then
|
||||||
@ -21,7 +24,7 @@ function UI.Window:emit(event)
|
|||||||
end
|
end
|
||||||
end
|
end
|
||||||
table.insert(events, 1, { type = event.type, value = table.concat(t, ' '), raw = event })
|
table.insert(events, 1, { type = event.type, value = table.concat(t, ' '), raw = event })
|
||||||
while #events > 10 do
|
while #events > 20 do
|
||||||
table.remove(events)
|
table.remove(events)
|
||||||
end
|
end
|
||||||
page.tabs.events.grid:update()
|
page.tabs.events.grid:update()
|
||||||
@ -29,7 +32,6 @@ function UI.Window:emit(event)
|
|||||||
page.tabs.events.grid:draw()
|
page.tabs.events.grid:draw()
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
event._recorded = true
|
|
||||||
return emitter(self, event)
|
return emitter(self, event)
|
||||||
end
|
end
|
||||||
|
|
||||||
@ -72,7 +74,14 @@ page = UI.Page {
|
|||||||
},
|
},
|
||||||
events = UI.Tab {
|
events = UI.Tab {
|
||||||
tabTitle = 'Events',
|
tabTitle = 'Events',
|
||||||
|
UI.MenuBar {
|
||||||
|
y = -1,
|
||||||
|
buttons = {
|
||||||
|
{ text = 'Clear', event = 'event_clear' },
|
||||||
|
}
|
||||||
|
},
|
||||||
grid = UI.ScrollingGrid {
|
grid = UI.ScrollingGrid {
|
||||||
|
ey = -2,
|
||||||
headerBackgroundColor = colors.red,
|
headerBackgroundColor = colors.red,
|
||||||
values = events,
|
values = events,
|
||||||
autospace = true,
|
autospace = true,
|
||||||
@ -80,6 +89,9 @@ page = UI.Page {
|
|||||||
{ heading = 'type', key = 'type' },
|
{ heading = 'type', key = 'type' },
|
||||||
{ heading = 'value', key = 'value', }
|
{ 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:update()
|
||||||
self.tabs.methodsTab.grid:draw()
|
self.tabs.methodsTab.grid:draw()
|
||||||
|
|
||||||
elseif event.type == 'grid_select' and event.element == self.tabs.events.grid then
|
elseif event.type == 'event_clear' then
|
||||||
event.selected.raw._recorded = nil
|
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({
|
multishell.openTab({
|
||||||
path = 'sys/apps/Lua.lua',
|
path = 'sys/apps/Lua.lua',
|
||||||
args = { event.selected.raw },
|
args = { event.selected.raw },
|
||||||
focused = true,
|
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.entry.value = event.selected.value
|
||||||
self.editor:show()
|
self.editor:show()
|
||||||
|
|
||||||
|
@ -912,14 +912,18 @@ end
|
|||||||
function UI.Window:emit(event)
|
function UI.Window:emit(event)
|
||||||
local parent = self
|
local parent = self
|
||||||
while parent do
|
while parent do
|
||||||
if parent.accelerators and event.key then -- not ideal
|
if parent.accelerators then
|
||||||
-- could be [event.key or event.type] to support accelerators
|
-- events types can be made unique via accelerators
|
||||||
-- for non-input type events
|
local acc = parent.accelerators[event.key or event.type]
|
||||||
local acc = parent.accelerators[event.key]
|
if acc and acc ~= event.type then -- don't get stuck in a loop
|
||||||
if acc and parent:emit({ type = acc, element = parent }) then
|
local event2 = Util.shallowCopy(event)
|
||||||
|
event2.type = acc
|
||||||
|
event2.key = nil
|
||||||
|
if parent:emit(event2) then
|
||||||
return true
|
return true
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
end
|
||||||
if parent.eventHandler then
|
if parent.eventHandler then
|
||||||
if parent:eventHandler(event) then
|
if parent:eventHandler(event) then
|
||||||
return true
|
return true
|
||||||
|
@ -61,3 +61,9 @@ function UI.Checkbox:eventHandler(event)
|
|||||||
return true
|
return true
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
function UI.Checkbox.example()
|
||||||
|
return UI.Checkbox {
|
||||||
|
x = 2, y = 2,
|
||||||
|
}
|
||||||
|
end
|
||||||
|
@ -53,7 +53,7 @@ function UI.Chooser:eventHandler(event)
|
|||||||
if event.key == 'right' or event.key == 'space' then
|
if event.key == 'right' or event.key == 'space' then
|
||||||
local _,k = Util.find(self.choices, 'value', self.value)
|
local _,k = Util.find(self.choices, 'value', self.value)
|
||||||
local choice
|
local choice
|
||||||
if not k then k = 1 end
|
if not k then k = 0 end
|
||||||
if k and k < #self.choices then
|
if k and k < #self.choices then
|
||||||
choice = self.choices[k+1]
|
choice = self.choices[k+1]
|
||||||
else
|
else
|
||||||
@ -86,3 +86,25 @@ function UI.Chooser:eventHandler(event)
|
|||||||
end
|
end
|
||||||
end
|
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
|
||||||
|
@ -73,3 +73,16 @@ function UI.DropMenu:eventHandler(event)
|
|||||||
end
|
end
|
||||||
return true
|
return true
|
||||||
end
|
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
|
||||||
|
@ -134,3 +134,23 @@ function UI.Form:eventHandler(event)
|
|||||||
end
|
end
|
||||||
return true
|
return true
|
||||||
end
|
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
|
||||||
|
@ -512,6 +512,9 @@ function UI.Grid.example()
|
|||||||
{ heading = 'key', key = 'key' },
|
{ heading = 'key', key = 'key' },
|
||||||
{ heading = 'value', key = 'value' },
|
{ heading = 'value', key = 'value' },
|
||||||
},
|
},
|
||||||
|
accelerators = {
|
||||||
|
grid_select = 'custom_select',
|
||||||
|
}
|
||||||
},
|
},
|
||||||
noheader = UI.Grid {
|
noheader = UI.Grid {
|
||||||
ex = '48%', y = 6, ey = -2,
|
ex = '48%', y = 6, ey = -2,
|
||||||
|
@ -88,3 +88,12 @@ function UI.MenuBar:eventHandler(event)
|
|||||||
return true
|
return true
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
function UI.MenuBar.example()
|
||||||
|
return UI.MenuBar {
|
||||||
|
buttons = {
|
||||||
|
{ text = 'Choice1', event = 'event1' },
|
||||||
|
{ text = 'Choice2', event = 'event2' },
|
||||||
|
}
|
||||||
|
}
|
||||||
|
end
|
||||||
|
@ -90,3 +90,28 @@ function UI.Notification:eventHandler(event)
|
|||||||
end
|
end
|
||||||
end
|
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
|
||||||
|
@ -75,3 +75,11 @@ function UI.Slider:eventHandler(event)
|
|||||||
self:draw()
|
self:draw()
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
function UI.Slider.example()
|
||||||
|
return UI.Slider {
|
||||||
|
y = 2, x = 2, ex = -2,
|
||||||
|
min = 0, max = 1,
|
||||||
|
help = 'Volume setting',
|
||||||
|
}
|
||||||
|
end
|
||||||
|
Loading…
x
Reference in New Issue
Block a user