Resolve Edit Deck

@xzenile / resolve-edit-deckv1.0.1Public

Split with a thumb button, nudge clips with the wheel, hold to unsnap, and an edit palette for DaVinci Resolve's Edit page.

Files

Version 1.0.1 (current)

vid-resolve.luau11.0 KB

--[[
  rebind: min_sdk=3.15.0
  rebind: name=Video · Resolve Edit Deck
  rebind: version=1.0.1
  rebind: author=PLACEHOLDER
  rebind: description=Split with a thumb button, nudge clips with the wheel, hold to unsnap, and an edit palette for DaVinci Resolve's Edit page.
  rebind: process=Resolve
  rebind: permissions=none
  rebind: instance=single
]]

-- Resolve Edit Deck (active while DaVinci Resolve is the focused app; made for the Edit page)
--   Back (Mouse4)                       -> split clips at the playhead (Command or Ctrl + \)
--   Hold Forward (Mouse5) + wheel       -> nudge the selection one frame (hold Shift too for 5)
--   Hold Caps Lock                      -> snapping flips (N) while held and flips back on release
--   Palette key (Ctrl+Shift+Space)      -> ripple delete, trim to playhead, transition, markers and more
--   Ctrl+Alt+0 -> Bind Board: change these keys from an overlay, no restart.
--     The wheel-hold button is set in the Options tab.
-- Every key comes from Blackmagic's DaVinci Resolve 20 Reference Manual, "Keyboard Shortcuts in
-- This Chapter" tables and steps (Edit chapters 35-38). The manual lists Mac keys and says Command
-- is Ctrl and Option is Alt on Windows and Linux. No timeline scrubbing.

local cfg = rb.ui.schema({
  split_key = rb.ui.keybind("Mouse4", { label = "Split at playhead", group = "Hotkeys" }),
  nudge_hold = rb.ui.keybind("Mouse5", { label = "Hold to nudge with the wheel",
    tooltip = "Takes effect after you restart the script.", group = "Hotkeys" }),
  unsnap_key = rb.ui.keybind("CapsLock", { label = "Hold to flip snapping", group = "Hotkeys" }),
  palette_key = rb.ui.keybind("LCtrl+LShift+Space", { label = "Edit palette", group = "Hotkeys" }),
  board = rb.ui.keybind("LCtrl+LAlt+0", { label = "Edit hotkeys (overlay)", group = "Hotkeys" }),
  wheel_layer = rb.ui.toggle(true, { label = "Wheel nudges",
    tooltip = "Takes effect after you restart the script.", group = "Behavior" }),
  flip_wheel = rb.ui.toggle(false, { label = "Wheel up nudges left",
    tooltip = "Takes effect after you restart the script.", group = "Behavior" }),
})

local MAC = rb.info.platform == "macos"
local MOD = MAC and "LWin" or "LCtrl"   -- Command on a Mac, Ctrl on Windows
local ALT = "LAlt"                      -- Option on a Mac, Alt on Windows

local function send(k) rb.hid.combo(k) end -- one call for keys and chords; rb.hid.press is coroutine-only (docs: rb.hid)

local function nudge(right)
  local fast = rb.input.is_down("LShift")
  rb.hid.combo((fast and "LShift+" or "") .. (right and "Period" or "Comma"))
end
local RIGHT = not cfg.flip_wheel
local function held() return rb.input.is_down(cfg.nudge_hold) end

if cfg.wheel_layer then
  for _, pre in { "", "LShift+" } do
    pcall(rb.bind, pre .. "WheelUp",   function() nudge(RIGHT) end,     { when = held })
    pcall(rb.bind, pre .. "WheelDown", function() nudge(not RIGHT) end, { when = held })
  end
end

local ITEMS = {
  { "Ripple delete (close the gap)", "Delete" },
  { "Lift (leave a gap)", "Backspace" },
  { "Trim start to playhead", "LShift+LeftBrace" },
  { "Trim end to playhead", "LShift+RightBrace" },
  { "Add transition", MOD .. "+T" },
  { "Add marker", "M" },
  { "Enable or disable clip", "D" },
  { "Match frame", "F" },
  { "Mark the clip at the playhead", "X" },
  { "Clear In and Out", ALT .. "+X" },
  { "Linked selection on or off", MOD .. "+LShift+L" },
  { "Selection tool", "A" },
  { "Trim tool", "T" },
  { "Razor (blade) tool", "B" },
}

-- ===== Bind Board v1.3 (shared; paste above your binds) =====
-- Usage: BB.bind("snooze", "Snooze", "LCtrl+LAlt+B", fn)  and  BB.board("board", "LCtrl+LAlt+K")
local BB = { rows = {}, order = {}, busy = false }
local MODS = { "LCtrl", "RCtrl", "LShift", "RShift", "LAlt", "RAlt", "LWin", "RWin" }
local IS_MOD = {}
for _, m in MODS do
  IS_MOD[m] = true
end
local REFUSE = { Escape = "Esc always cancels", Mouse1 = "a plain left click can't be a hotkey" }

local function cur(id)
  return rb.ui.get(id) or BB.rows[id].default
end

local function shown(k) -- hide a pinned device id
  return (string.match(k, "^[^@]+"))
end

local function chord(key)
  local parts = {}
  for _, m in MODS do
    if m ~= key and rb.input.is_down(m) then
      table.insert(parts, m)
    end
  end
  table.insert(parts, key)
  return table.concat(parts, "+")
end

local function apply(id, key)
  local r = BB.rows[id]
  local ok, h = pcall(rb.bind, key, r.fn, r.opts)
  if not ok then
    return false
  end
  if r.handle then
    r.handle:stop()
  end
  r.handle = h
  rb.ui.set(id, key)
  return true
end

local function guarded(opts) -- pause this script's Bind Board binds while capture listens
  local g = {}
  for k, v in opts or {} do
    g[k] = v
  end
  local orig = g.when
  g.when = function(d)
    return not BB.busy and (orig == nil or orig(d))
  end
  return g
end

function BB.bind(id, label, default, fn, opts)
  opts = guarded(opts)
  local r = { label = label, default = default, fn = fn, opts = opts }
  BB.rows[id] = r
  table.insert(BB.order, id)
  local key = cur(id)
  local ok, h = pcall(rb.bind, key, fn, opts)
  if not ok and key ~= default then -- a bad saved key falls back to the default
    ok, h = pcall(rb.bind, default, fn, opts)
    if ok then
      rb.ui.set(id, default)
      rb.overlay.toast(label .. " couldn't use " .. shown(key) .. ", so it's back to " .. default .. ".")
    end
  end
  if ok then
    r.handle = h
  else
    rb.overlay.toast(label .. " has no hotkey. Set one in Edit hotkeys or the Options tab.")
  end
end

local function restart_menu(text)
  rb.overlay.menu({
    { label = "Restart script now", action = function() rb.script.reload() end },
    { label = "Later" },
  }, { title = text, subtitle = "Restart to apply" })
end

local function saved(r, key)
  rb.overlay.toast(r.label .. " is now " .. shown(key) .. ".", { title = "Hotkey saved" })
end

local function swap(id, key, clash, mine)
  local r, r2 = BB.rows[id], BB.rows[clash]
  if r2.handle then
    r2.handle:stop()
  end
  r2.handle = nil
  rb.ui.set(clash, mine) -- saved first, so a restart never doubles a key
  if not apply(id, key) then -- put the other action back on its key
    rb.ui.set(clash, key)
    local ok, h = pcall(rb.bind, key, r2.fn, r2.opts)
    if ok then
      r2.handle = h
    end
    rb.overlay.toast("Couldn't use " .. shown(key) .. ". Nothing changed.")
    return
  end
  if apply(clash, mine) then
    saved(r, key)
    return
  end
  restart_menu("Swapped. " .. r2.label .. " is saved as " .. shown(mine))
end

local function set_key(id, key)
  local r, clash = BB.rows[id], nil
  for _, o in BB.order do
    if o ~= id and cur(o) == key then
      clash = o
    end
  end
  if not clash then
    if apply(id, key) then
      saved(r, key)
      return
    end
    rb.ui.set(id, key)
    restart_menu("Saved " .. shown(key) .. " for " .. r.label)
    return
  end
  local mine = cur(id)
  rb.overlay.menu({
    { label = "Cancel" },
    { label = "Swap the two keys", hint = BB.rows[clash].label .. " → " .. shown(mine),
      action = function() swap(id, key, clash, mine) end },
  }, { title = shown(key) .. " is already " .. BB.rows[clash].label, subtitle = "Nothing changes unless you pick Swap" })
end

local function capture(id)
  local r = BB.rows[id]
  local status = rb.overlay.status("Press the new key for " .. r.label .. "  ·  Esc cancels")
  local hDown, hScroll, timer
  local function done()
    BB.busy = false
    status.remove()
    if hDown then
      hDown:stop()
    end
    if hScroll then
      hScroll:stop()
    end
    if timer then
      timer:stop()
    end
  end
  hDown = rb.on("down", function(key)
    if not BB.busy then
      return
    end
    if IS_MOD[key] then -- let modifiers through; wait for the real key
      return
    end
    done()
    if key == "Escape" then
      rb.overlay.toast("Nothing changed.")
      return false
    end
    if REFUSE[key] and chord(key) == key then
      rb.overlay.toast("Can't use " .. key .. ": " .. REFUSE[key] .. ".")
      return false
    end
    set_key(id, chord(key))
    return false
  end)
  hScroll = rb.on("scroll", function(delta)
    if not BB.busy then
      return
    end
    done()
    set_key(id, chord(delta > 0 and "WheelUp" or "WheelDown"))
    return false
  end)
  timer = rb.timer.after(10000, function()
    if not BB.busy then
      return
    end
    done()
    rb.overlay.toast("No key pressed. Nothing changed.")
  end)
end

local function reset_all()
  local failed = {}
  for _, id in BB.order do
    if not apply(id, BB.rows[id].default) then
      table.insert(failed, BB.rows[id].label)
    end
  end
  if #failed == 0 then
    rb.overlay.toast("Every hotkey is back to its default.")
  else
    rb.overlay.toast("Couldn't reset: " .. table.concat(failed, ", ") .. ".")
  end
end

function BB.open()
  if BB.busy then
    return
  end
  local items = {}
  for _, id in BB.order do
    table.insert(items, {
      label = BB.rows[id].label,
      hint = shown(cur(id)),
      action = function()
        BB.busy = true
        capture(id)
      end,
    })
  end
  table.insert(items, { label = "Reset all to defaults", action = reset_all })
  table.insert(items, { label = "Done" })
  local m, why = rb.overlay.menu(items, { title = "Edit hotkeys", subtitle = "Pick one, then press the new key" })
  if not m then
    rb.overlay.toast("Can't open the editor: " .. why .. ".")
  end
end

function BB.board(id, default)
  BB.bind(id, "Edit hotkeys", default, BB.open)
end
-- ===== end Bind Board =====

-- Option+Space and Shift+Space are Resolve's Play Around In and Out, so the palette defaults to
-- Ctrl+Shift+Space.
BB.bind("split_key", "Split at playhead", "Mouse4", function() rb.hid.combo(MOD .. "+Backslash") end)
local unsnapped = false   -- ignores key auto-repeat, so N is pressed once each way
BB.bind("unsnap_key", "Hold to flip snapping", "CapsLock", function()
  if unsnapped then return end
  unsnapped = true; rb.hid.combo("N")
end, { release = function()
  if not unsnapped then return end
  unsnapped = false; rb.hid.combo("N")
end })
BB.bind("palette_key", "Edit palette", "LCtrl+LShift+Space", function()
  local items = {}
  for _, it in ITEMS do
    local k = it[2]
    table.insert(items, { label = it[1], hint = k, action = function() send(k) end })
  end
  local m, why = rb.overlay.menu(items, { title = "Resolve edit palette", subtitle = "Acts on the selection or at the playhead. Esc closes." })
  if not m then rb.log.warn("palette not shown: " .. tostring(why)) end
end)
-- The editor key only works while this script's app is in front.
local BOARD_APPS = { "resolve" }
local function app_in_front()
  local p = string.lower(rb.system.window().process or "")
  for _, a in BOARD_APPS do
    if string.find(p, a, 1, true) then
      return true
    end
  end
  return false
end
BB.bind("board", "Edit hotkeys", "LCtrl+LAlt+0", BB.open, { when = app_in_front })

3 files, 13.6 KB.