Display appropriate strings for shortcuts

This commit is contained in:
airstruck
2016-01-28 13:47:11 -05:00
parent d1501fca41
commit b0614e67f1
3 changed files with 46 additions and 13 deletions

View File

@@ -15,18 +15,28 @@ local CTRL = 2
local SHIFT = 4
local GUI = 8
function Shortcut.parseKeyCombo (value)
-- expand command- and option- aliases
value = value
function Shortcut.appliesToPlatform (value)
if isMac and value:match '%f[%a]win%-'
or not isMac and value:match '%f[%a]mac%-' then
return false
end
return true
end
function Shortcut.expandAliases (value)
return value
:gsub('%f[%a]command%-', 'mac-gui-')
:gsub('%f[%a]option%-', 'mac-alt-')
end
function Shortcut.parseKeyCombo (value)
-- expand command- and option- aliases
value = Shortcut.expandAliases(value)
-- exit early if shortcut is for different platform
if isMac and value:match 'win%-' or not isMac and value:match 'mac%-' then
return
end
if not Shortcut.appliesToPlatform(value) then return end
-- expand c- alias
-- expand c- special modifier
if isMac then
value = value:gsub('%f[%a]c%-', 'gui-')
else
@@ -53,4 +63,28 @@ function Shortcut.getModifierFlags ()
return alt + ctrl + shift + gui
end
function Shortcut.stringify (shortcut)
if type(shortcut) ~= 'table' then
shortcut = { shortcut }
end
for _, value in ipairs(shortcut) do
value = Shortcut.expandAliases(value)
if Shortcut.appliesToPlatform(value) then
if isMac then
value = value
:gsub('%f[%a]c%-', 'command-')
:gsub('%f[%a]gui%-', 'command-')
:gsub('%f[%a]alt%-', 'option-')
else
value = value
:gsub('%f[%a]c%-', 'ctrl-')
:gsub('%f[%a]gui%-', 'windows-')
end
value = value:gsub('%f[%a]win%-', ''):gsub('%f[%a]mac%-', '')
value = value:gsub('%f[%w].', string.upper)
return value
end
end
end
return Shortcut

View File

@@ -10,6 +10,7 @@ not be explicitly created.
local ROOT = (...):gsub('[^.]*.[^.]*.[^.]*$', '')
local Backend = require(ROOT .. 'backend')
local Shortcut = require(ROOT .. 'shortcut')
local Layout, Event
@@ -176,9 +177,7 @@ 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('-', '+')
shortcut = Shortcut.stringify(shortcut)
end
self.height = font:getLineHeight() + pad * 2
self.flow = 'x'