From 3241326a2f1662fdd312a868ab1eab49ed4c1257 Mon Sep 17 00:00:00 2001 From: "kepler155c@gmail.com" Date: Wed, 13 Nov 2019 21:50:00 -0700 Subject: [PATCH] accelerators for any event + more inspect examples --- sys/apps/inspect.lua | 30 ++++++++++++++----- sys/modules/opus/ui.lua | 16 ++++++---- sys/modules/opus/ui/components/Checkbox.lua | 6 ++++ sys/modules/opus/ui/components/Chooser.lua | 24 ++++++++++++++- sys/modules/opus/ui/components/DropMenu.lua | 13 ++++++++ sys/modules/opus/ui/components/Form.lua | 20 +++++++++++++ sys/modules/opus/ui/components/Grid.lua | 3 ++ sys/modules/opus/ui/components/MenuBar.lua | 9 ++++++ .../opus/ui/components/Notification.lua | 25 ++++++++++++++++ sys/modules/opus/ui/components/Slider.lua | 8 +++++ 10 files changed, 140 insertions(+), 14 deletions(-) diff --git a/sys/apps/inspect.lua b/sys/apps/inspect.lua index ae991d6..48feeb4 100644 --- a/sys/apps/inspect.lua +++ b/sys/apps/inspect.lua @@ -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() diff --git a/sys/modules/opus/ui.lua b/sys/modules/opus/ui.lua index 2ab507d..5686f93 100644 --- a/sys/modules/opus/ui.lua +++ b/sys/modules/opus/ui.lua @@ -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 diff --git a/sys/modules/opus/ui/components/Checkbox.lua b/sys/modules/opus/ui/components/Checkbox.lua index a90e911..54343e5 100644 --- a/sys/modules/opus/ui/components/Checkbox.lua +++ b/sys/modules/opus/ui/components/Checkbox.lua @@ -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 diff --git a/sys/modules/opus/ui/components/Chooser.lua b/sys/modules/opus/ui/components/Chooser.lua index e4d41fa..9698f6f 100644 --- a/sys/modules/opus/ui/components/Chooser.lua +++ b/sys/modules/opus/ui/components/Chooser.lua @@ -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 diff --git a/sys/modules/opus/ui/components/DropMenu.lua b/sys/modules/opus/ui/components/DropMenu.lua index 5cce43a..41bdaf9 100644 --- a/sys/modules/opus/ui/components/DropMenu.lua +++ b/sys/modules/opus/ui/components/DropMenu.lua @@ -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 diff --git a/sys/modules/opus/ui/components/Form.lua b/sys/modules/opus/ui/components/Form.lua index da312d9..69cb7ca 100644 --- a/sys/modules/opus/ui/components/Form.lua +++ b/sys/modules/opus/ui/components/Form.lua @@ -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 diff --git a/sys/modules/opus/ui/components/Grid.lua b/sys/modules/opus/ui/components/Grid.lua index e1edfe8..1d6e480 100644 --- a/sys/modules/opus/ui/components/Grid.lua +++ b/sys/modules/opus/ui/components/Grid.lua @@ -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, diff --git a/sys/modules/opus/ui/components/MenuBar.lua b/sys/modules/opus/ui/components/MenuBar.lua index bccb9be..85f435b 100644 --- a/sys/modules/opus/ui/components/MenuBar.lua +++ b/sys/modules/opus/ui/components/MenuBar.lua @@ -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 diff --git a/sys/modules/opus/ui/components/Notification.lua b/sys/modules/opus/ui/components/Notification.lua index f1b38f6..bf02256 100644 --- a/sys/modules/opus/ui/components/Notification.lua +++ b/sys/modules/opus/ui/components/Notification.lua @@ -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 diff --git a/sys/modules/opus/ui/components/Slider.lua b/sys/modules/opus/ui/components/Slider.lua index 9dee765..09325b4 100644 --- a/sys/modules/opus/ui/components/Slider.lua +++ b/sys/modules/opus/ui/components/Slider.lua @@ -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