Add special 'mac-' and 'win-' modifiers, fixes #29

This commit is contained in:
airstruck
2016-01-28 11:26:04 -05:00
parent f9a8ffa2e2
commit d1501fca41
5 changed files with 72 additions and 30 deletions

View File

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

View File

@@ -13,6 +13,8 @@ to recalculate their size and position.
local ROOT = (...):gsub('[^.]*$', '')
local Shortcut = require(ROOT .. 'shortcut')
local Attribute = {}
local function cascade (widget, attribute)
@@ -223,15 +225,11 @@ 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
local function setShortcut (layout, shortcut, value)
local mainKey, modifierFlags = Shortcut.parseKeyCombo(shortcut)
if mainKey then
layout.shortcuts[modifierFlags][mainKey] = value
end
end
function Attribute.shortcut.set (widget, value)
@@ -241,24 +239,20 @@ function Attribute.shortcut.set (widget, value)
if oldValue then
if type(oldValue) == 'table' then
for _, v in ipairs(oldValue) do
local mainKey, modifierFlags = parseKeyCombo(v)
layout.shortcuts[modifierFlags][mainKey] = nil
setShortcut(layout, v, nil)
end
else
local mainKey, modifierFlags = parseKeyCombo(oldValue)
layout.shortcuts[modifierFlags][mainKey] = nil
setShortcut(layout, oldValue, nil)
end
end
if value then
if type(value) == 'table' then
for _, v in ipairs(value) do
local mainKey, modifierFlags = parseKeyCombo(v)
layout.shortcuts[modifierFlags][mainKey] = widget
setShortcut(layout, v, widget)
end
else
local mainKey, modifierFlags = parseKeyCombo(value)
layout.shortcuts[modifierFlags][mainKey] = widget
setShortcut(layout, value, widget)
end
end

View File

@@ -3,6 +3,7 @@ local ROOT = (...):gsub('[^.]*$', '')
local Backend = require(ROOT .. 'backend')
local Base = require(ROOT .. 'base')
local Event = require(ROOT .. 'event')
local Shortcut = require(ROOT .. 'shortcut')
local Input = Base:extend()
@@ -19,19 +20,11 @@ function Input:handleDisplay (layout)
Event.Display:emit(layout)
end
function Input:getModifierFlags ()
local alt = Backend.isKeyDown('lalt', 'ralt') and 1 or 0
local ctrl = Backend.isKeyDown('lctrl', 'rctrl', 'lgui', 'rgui') and 2 or 0
local shift = Backend.isKeyDown('lshift', 'rshift') and 4 or 0
return alt + ctrl + shift
end
function Input:handleKeyPress (layout, key, x, y)
local widget = layout.focusedWidget or layout.root
local result = widget:bubbleEvent('KeyPress', {
key = key,
modifierFlags = self:getModifierFlags(),
modifierFlags = Shortcut.getModifierFlags(),
x = x, y = y
})
if result ~= nil then return result end
@@ -42,7 +35,7 @@ function Input:handleKeyRelease (layout, key, x, y)
local widget = layout.focusedWidget or layout.root
local result = widget:bubbleEvent('KeyRelease', {
key = key,
modifierFlags = self:getModifierFlags(),
modifierFlags = Shortcut.getModifierFlags(),
x = x, y = y
})
if result ~= nil then return result end

View File

@@ -22,7 +22,6 @@ local Event = require(ROOT .. 'event')
local Widget = require(ROOT .. 'widget')
local Input = require(ROOT .. 'input')
local Style = require(ROOT .. 'style')
local Backend = require(ROOT .. 'backend')
local Layout = Base:extend()
@@ -329,7 +328,7 @@ end
function Layout:addDefaultHandlers ()
self.shortcuts = {}
for i = 0, 8 do
for i = 0, 15 do
self.shortcuts[i] = {}
end

56
luigi/shortcut.lua Normal file
View File

@@ -0,0 +1,56 @@
--[[--
Keyboard shortcut module.
--]]--
local ROOT = (...):gsub('[^.]*$', '')
local Backend = require(ROOT .. 'backend')
local Shortcut = {}
local isMac = Backend.isMac()
local ALT = 1
local CTRL = 2
local SHIFT = 4
local GUI = 8
function Shortcut.parseKeyCombo (value)
-- expand command- and option- aliases
value = value
:gsub('%f[%a]command%-', 'mac-gui-')
:gsub('%f[%a]option%-', 'mac-alt-')
-- exit early if shortcut is for different platform
if isMac and value:match 'win%-' or not isMac and value:match 'mac%-' then
return
end
-- expand c- alias
if isMac then
value = value:gsub('%f[%a]c%-', 'gui-')
else
value = value:gsub('%f[%a]c%-', 'ctrl-')
end
-- extract main key
local mainKey = value:match '[^%-]*%-?$'
-- extract modifiers
local alt = value:match '%f[%a]alt%-' and ALT or 0
local ctrl = value:match '%f[%a]ctrl%-' and CTRL or 0
local shift = value:match '%f[%a]shift%-' and SHIFT or 0
local gui = value:match '%f[%a]gui%-' and GUI or 0
return mainKey, alt + ctrl + shift + gui
end
function Shortcut.getModifierFlags ()
local alt = Backend.isKeyDown('lalt', 'ralt') and ALT or 0
local ctrl = Backend.isKeyDown('lctrl', 'rctrl') and CTRL or 0
local shift = Backend.isKeyDown('lshift', 'rshift') and SHIFT or 0
local gui = Backend.isKeyDown('lgui', 'rgui') and GUI or 0
return alt + ctrl + shift + gui
end
return Shortcut