Skip to content

Instantly share code, notes, and snippets.

@ebai101
Last active July 5, 2024 21:00
Show Gist options
  • Save ebai101/cc5ff3ef39e00ff34e1bbb02b531c65d to your computer and use it in GitHub Desktop.
Save ebai101/cc5ff3ef39e00ff34e1bbb02b531c65d to your computer and use it in GitHub Desktop.
reason pinch zoom
--[[
Pinch zoom script for Reason
This file should be put in the ~/.hammerspoon directory
or just click Open Config in the Hammerspoon menu and paste all of this in
To use, just pinch with the trackpad while in the sequencer.
Hold shift to zoom in on the playhead instead of the cursor
Hold command to zoom vertically
]]
pinchZoom = {}
pinchZoom.appName = 'Reason' -- works for Reason 12, might be different for other versions
pinchZoom.threshold = 0.10 -- adjust to vary sensitivity - lower is faster
pinchZoom.zoomSum = 0
pinchZoom.lastZoomTime = 0
pinchZoom.eventtap = hs.eventtap.new({ hs.eventtap.event.types.gesture }, function(event)
local gestureType = event:getType(true)
if gestureType ~= hs.eventtap.event.types.magnify then return end
local zoomTime = hs.timer.absoluteTime()
if zoomTime - pinchZoom.lastZoomTime > 1000000000 then pinchZoom.zoomSum = 0 end
pinchZoom.lastZoomTime = zoomTime
-- the zoom key commands are much less sensitive than the scroll wheel
-- so we lower the threshold when using them
local threshold = pinchZoom.threshold
if event:getFlags()['shift'] then threshold = threshold * 0.5 end
local zoomLevel = event:getTouchDetails().magnification
pinchZoom.zoomSum = pinchZoom.zoomSum + zoomLevel
local offsets = {}
if pinchZoom.zoomSum >= threshold then
offsets = { 1, 0 }
pinchZoom.zoomSum = 0
elseif pinchZoom.zoomSum <= -threshold then
offsets = { -1, 0 }
pinchZoom.zoomSum = 0
else
return
end
if event:getFlags()['shift'] or event:getFlags()['cmd'] then
local flags = { ['cmd'] = true, ['shift'] = true }
if event:getFlags()['cmd'] then flags['alt'] = true end
local key = offsets[1] == 1 and '=' or '-'
hs.eventtap.event.newKeyEvent(key, true):setFlags(flags):post()
hs.eventtap.event.newKeyEvent(key, false):setFlags(flags):post()
else
hs.eventtap.event.newScrollEvent(offsets, { 'cmd', 'shift' }, 'pixel'):post()
end
end)
pinchZoom.watcher = hs.application.watcher.new(function(appName, eventType)
if appName == pinchZoom.appName then
if eventType == hs.application.watcher.activated then
pinchZoom.eventtap:start()
elseif eventType == hs.application.watcher.deactivated then
pinchZoom.eventtap:stop()
end
end
end)
pinchZoom.watcher:start()
hs.alert('reloaded Hammerspoon')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment