shortcut attrib: allow multiple shortcuts per widget

This commit is contained in:
airstruck
2016-01-27 16:56:39 -05:00
parent 0c33538de9
commit f9a8ffa2e2
3 changed files with 32 additions and 16 deletions

View File

@@ -28,7 +28,7 @@ return { id = 'mainWindow',
},
},
{ style = 'toolbar',
{ id = 'newButton', style = 'toolButton', shortcut = 'z',
{ id = 'newButton', style = 'toolButton', shortcut = { 'z', 'x' },
icon = 'icon/32px/Blueprint.png',
status = 'Create a new thing' },
{ id = 'loadButton', style = 'toolButton',

View File

@@ -86,17 +86,6 @@ function Attribute.id.set (widget, value)
widget.attributes.id = value
end
-- TODO: formalize this bitfield somewhere
local function parseKeyCombo (value)
local mainKey = (value):match '[^%-]+$'
local alt = (value):match 'alt%-' and 1 or 0
local ctrl = (value):match 'ctrl%-' and 2 or 0
local shift = (value):match 'shift%-' and 4 or 0
local modifierFlags = alt + ctrl + shift
return mainKey, modifierFlags
end
--[[--
Widget value.
@@ -234,18 +223,43 @@ Setting this attribute re-registers the widget with its layout.
--]]--
Attribute.shortcut = {}
-- TODO: formalize this bitfield somewhere
local function parseKeyCombo (value)
local mainKey = (value):match '[^%-]+$'
local alt = (value):match 'alt%-' and 1 or 0
local ctrl = (value):match 'ctrl%-' and 2 or 0
local shift = (value):match 'shift%-' and 4 or 0
local modifierFlags = alt + ctrl + shift
return mainKey, modifierFlags
end
function Attribute.shortcut.set (widget, value)
local layout = widget.layout.master or widget.layout
local oldValue = widget.attributes.shortcut
if oldValue then
local mainKey, modifierFlags = parseKeyCombo(oldValue)
layout.shortcuts[modifierFlags][mainKey] = nil
if type(oldValue) == 'table' then
for _, v in ipairs(oldValue) do
local mainKey, modifierFlags = parseKeyCombo(v)
layout.shortcuts[modifierFlags][mainKey] = nil
end
else
local mainKey, modifierFlags = parseKeyCombo(oldValue)
layout.shortcuts[modifierFlags][mainKey] = nil
end
end
if value then
local mainKey, modifierFlags = parseKeyCombo(value)
layout.shortcuts[modifierFlags][mainKey] = widget
if type(value) == 'table' then
for _, v in ipairs(value) do
local mainKey, modifierFlags = parseKeyCombo(v)
layout.shortcuts[modifierFlags][mainKey] = widget
end
else
local mainKey, modifierFlags = parseKeyCombo(value)
layout.shortcuts[modifierFlags][mainKey] = widget
end
end
widget.attributes.shortcut = value

View File

@@ -176,6 +176,8 @@ local function initialize (self)
shortcut = ' '
edgeType = 'menu.expander'
else
--TODO: only displays first of multiple shortcuts, change this?
if type(shortcut) == 'table' then shortcut = shortcut[1] end
shortcut = shortcut:gsub('%f[%w].', string.upper) -- :gsub('-', '+')
end
self.height = font:getLineHeight() + pad * 2