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', { style = 'toolbar',
{ id = 'newButton', style = 'toolButton', shortcut = 'z', { id = 'newButton', style = 'toolButton', shortcut = { 'z', 'x' },
icon = 'icon/32px/Blueprint.png', icon = 'icon/32px/Blueprint.png',
status = 'Create a new thing' }, status = 'Create a new thing' },
{ id = 'loadButton', style = 'toolButton', { id = 'loadButton', style = 'toolButton',

View File

@@ -86,17 +86,6 @@ function Attribute.id.set (widget, value)
widget.attributes.id = value widget.attributes.id = value
end 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. Widget value.
@@ -234,18 +223,43 @@ Setting this attribute re-registers the widget with its layout.
--]]-- --]]--
Attribute.shortcut = {} 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) function Attribute.shortcut.set (widget, value)
local layout = widget.layout.master or widget.layout local layout = widget.layout.master or widget.layout
local oldValue = widget.attributes.shortcut local oldValue = widget.attributes.shortcut
if oldValue then if oldValue then
local mainKey, modifierFlags = parseKeyCombo(oldValue) if type(oldValue) == 'table' then
layout.shortcuts[modifierFlags][mainKey] = nil 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 end
if value then if value then
local mainKey, modifierFlags = parseKeyCombo(value) if type(value) == 'table' then
layout.shortcuts[modifierFlags][mainKey] = widget 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 end
widget.attributes.shortcut = value widget.attributes.shortcut = value

View File

@@ -176,6 +176,8 @@ local function initialize (self)
shortcut = ' ' shortcut = ' '
edgeType = 'menu.expander' edgeType = 'menu.expander'
else 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('-', '+') shortcut = shortcut:gsub('%f[%w].', string.upper) -- :gsub('-', '+')
end end
self.height = font:getLineHeight() + pad * 2 self.height = font:getLineHeight() + pad * 2