2019-06-28 17:50:02 +00:00
|
|
|
local class = require('opus.class')
|
|
|
|
local UI = require('opus.ui')
|
|
|
|
local Util = require('opus.util')
|
2019-02-06 04:03:57 +00:00
|
|
|
|
|
|
|
UI.DropMenu = class(UI.MenuBar)
|
|
|
|
UI.DropMenu.defaults = {
|
|
|
|
UIElement = 'DropMenu',
|
2020-04-22 04:32:12 +00:00
|
|
|
backgroundColor = 'white',
|
2019-02-06 04:03:57 +00:00
|
|
|
buttonClass = 'DropMenuItem',
|
|
|
|
}
|
2019-04-14 21:44:29 +00:00
|
|
|
function UI.DropMenu:layout()
|
|
|
|
UI.MenuBar.layout(self)
|
2019-02-06 04:03:57 +00:00
|
|
|
|
|
|
|
local maxWidth = 1
|
|
|
|
for y,child in ipairs(self.children) do
|
|
|
|
child.x = 1
|
|
|
|
child.y = y
|
|
|
|
if #(child.text or '') > maxWidth then
|
|
|
|
maxWidth = #child.text
|
|
|
|
end
|
|
|
|
end
|
|
|
|
for _,child in ipairs(self.children) do
|
|
|
|
child.width = maxWidth + 2
|
|
|
|
if child.spacer then
|
2019-03-31 19:16:45 +00:00
|
|
|
child.inactive = true
|
2019-02-06 04:03:57 +00:00
|
|
|
child.text = string.rep('-', child.width - 2)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
self.height = #self.children + 1
|
|
|
|
self.width = maxWidth + 2
|
|
|
|
|
2020-03-31 15:57:23 +00:00
|
|
|
if self.x + self.width > self.parent.width then
|
|
|
|
self.x = self.parent.width - self.width + 1
|
2019-04-14 21:44:29 +00:00
|
|
|
end
|
2019-02-06 04:03:57 +00:00
|
|
|
|
2020-03-31 15:57:23 +00:00
|
|
|
self:reposition(self.x, self.y, self.width, self.height)
|
2019-02-06 04:03:57 +00:00
|
|
|
end
|
|
|
|
|
2020-03-31 15:57:23 +00:00
|
|
|
function UI.DropMenu:enable()
|
2020-04-03 03:28:42 +00:00
|
|
|
local menuBar = self.parent:find(self.menuUid)
|
2020-04-09 22:08:54 +00:00
|
|
|
local hasActive
|
|
|
|
|
2020-03-31 15:57:23 +00:00
|
|
|
for _,c in pairs(self.children) do
|
2020-04-03 03:28:42 +00:00
|
|
|
if not c.spacer and menuBar then
|
|
|
|
c.inactive = not menuBar:getActive(c)
|
2020-03-31 15:57:23 +00:00
|
|
|
end
|
2020-04-09 22:08:54 +00:00
|
|
|
if not c.inactive then
|
|
|
|
hasActive = true
|
|
|
|
end
|
2020-03-31 15:57:23 +00:00
|
|
|
end
|
2019-02-06 04:03:57 +00:00
|
|
|
|
2020-04-09 22:08:54 +00:00
|
|
|
-- jump through a lot of hoops if all selections are inactive
|
|
|
|
-- there's gotta be a better way
|
|
|
|
-- lots of exception code just to handle drop menus
|
|
|
|
self.focus = not hasActive and function() end
|
|
|
|
|
2019-02-06 04:03:57 +00:00
|
|
|
UI.Window.enable(self)
|
2020-04-09 22:08:54 +00:00
|
|
|
if self.focus then
|
|
|
|
self:setFocus(self)
|
|
|
|
else
|
|
|
|
self:focusFirst()
|
|
|
|
end
|
2020-03-31 15:57:23 +00:00
|
|
|
self:draw()
|
2019-02-06 04:03:57 +00:00
|
|
|
end
|
|
|
|
|
2020-03-31 15:57:23 +00:00
|
|
|
function UI.DropMenu:disable()
|
|
|
|
UI.Window.disable(self)
|
|
|
|
self:remove()
|
2019-02-06 04:03:57 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
function UI.DropMenu:eventHandler(event)
|
|
|
|
if event.type == 'focus_lost' and self.enabled then
|
2020-04-09 22:08:54 +00:00
|
|
|
if not (Util.contains(self.children, event.focused) or event.focused == self) then
|
2020-03-31 15:57:23 +00:00
|
|
|
self:disable()
|
2019-02-06 04:03:57 +00:00
|
|
|
end
|
|
|
|
elseif event.type == 'mouse_out' and self.enabled then
|
2020-03-31 15:57:23 +00:00
|
|
|
self:disable()
|
|
|
|
self:setFocus(self.parent:find(self.lastFocus))
|
2019-02-06 04:03:57 +00:00
|
|
|
else
|
|
|
|
return UI.MenuBar.eventHandler(self, event)
|
|
|
|
end
|
|
|
|
return true
|
|
|
|
end
|
2019-11-14 04:50:00 +00:00
|
|
|
|
|
|
|
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' },
|
|
|
|
} },
|
2020-03-31 15:57:23 +00:00
|
|
|
{ text = 'Edit', dropdown = {
|
|
|
|
{ text = 'Copy', event = 'run' },
|
|
|
|
{ text = 'Paste s', event = 'shell' },
|
|
|
|
} },
|
|
|
|
{ text = '\187',
|
|
|
|
x = -3,
|
|
|
|
dropdown = {
|
|
|
|
{ text = 'Associations', event = 'associate' },
|
|
|
|
} },
|
2019-11-14 04:50:00 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
end
|