One-Button Resize

@xzenile / one-button-resizev1.1.0Public

Hold one thumb button in Photoshop to get the on-canvas brush resize gesture: drag left/right for size, up/down for hardness.

Files

Version 1.1.0 (current)

gfx-one-button-resize.luau10.9 KB

--[[
  rebind: min_sdk=3.11.1
  rebind: name=GFX · One-Button Resize
  rebind: version=1.1.0
  rebind: author=PLACEHOLDER
  rebind: description=Hold one thumb button in Photoshop to get the on-canvas brush resize gesture: drag left/right for size, up/down for hardness. Photoshop keys are from Adobe's pages via search, not a live read, so check before use.
  rebind: process=Photoshop
  rebind: permissions=none
  rebind: instance=single
]]

-- One-Button Resize v1.1.0 (only active while Photoshop is focused)
--   Hold the resize button (default: Back / Mouse4) and move the mouse:
--     left / right = brush size, up / down = hardness. Let go to finish.
--   While it is held the script holds Photoshop's documented gesture for you:
--     Windows: Alt + right mouse button      macOS: Control + Option + (left) mouse button
--   Ctrl+Alt+V opens Edit hotkeys (Bind Board) over Photoshop (only while Photoshop is in front).
--   Source (Adobe, "Visually resize or change hardness of painting cursors"):
--     https://helpx.adobe.com/photoshop/using/using-tools.html
--     "press Alt+right-click (Windows) or control+option (macOS), and drag left or right. To change hardness, drag up or down."
--     NOTE: from Adobe's pages via search, not a live read, so check before use.
--     The preview needs Photoshop's graphics processor (OpenGL) setting on.
-- Key names follow https://docs.rebind.gg/reference/sdk/keys.md: LAlt = Alt / Option, LCtrl = Control,
--   Mouse1 = left button, Mouse2 = right button.
-- Hotkeys: "Edit hotkeys" goes through the shared Bind Board block below. The resize button (a hold
--   with a release) stays plain rb.bind, outside Bind Board.
-- APIs: rb.bind (release), rb.hid.down/up, rb.info.platform, rb.overlay.status/toast/menu (3.11.1), rb.ui.schema,
--   rb.timer, rb.on("blur"), rb.script.reload. min_sdk 3.11.1.
-- Platforms: Windows and macOS. If the script stops while the button is held, Rebind releases the held keys.

local DEFAULTS = { show_status = true }
local RESIZE_DEFAULT = "Mouse4"
local BOARD_DEFAULT = "LCtrl+LAlt+V"

local cfg = rb.ui.schema({
  resize_key = rb.ui.keybind(RESIZE_DEFAULT, {
    label = "Resize button",
    tooltip = "Hold it and drag to resize the brush. Rebind restarts the script to apply a change made here. Photoshop keys are from Adobe's pages via search, not a live read, so check before use.",
    group = "Hotkeys",
  }),
  board = rb.ui.keybind(BOARD_DEFAULT, {
    label = "Edit hotkeys",
    tooltip = "Opens the Bind Board over Photoshop: pick a hotkey, then press the new key.",
    group = "Hotkeys",
  }),
  show_status = rb.ui.toggle(DEFAULTS.show_status, {
    label = "Show a pill while resizing",
    tooltip = "A small 'Resizing' pill at the top while the button is held.",
    group = "Display",
  }),
  reset = rb.ui.toggle(false, {
    label = "Reset to defaults",
    tooltip = "Resets the Display setting. Hotkeys reset from Edit hotkeys or this tab.",
    group = "Reset",
  }),
})

local MAC = rb.info.platform == "macos"
-- Adobe: Windows Alt + right-click drag; macOS Control + Option + drag.
local GESTURE = MAC and "LCtrl+LAlt+Mouse1" or "LAlt+Mouse2"
local LABEL = MAC and "Control + Option + drag" or "Alt + right-drag"

local held = false
local pill = nil

local function startResize()
  if held then
    return
  end
  held = true
  rb.hid.down(GESTURE)
  if cfg.show_status then
    pill = rb.overlay.status({ { "Resizing ", "accent", true }, { "← → size · ↑ ↓ hardness", "dim" } }, { anchor = "top" })
  end
end

local function stopResize()
  if not held then
    return
  end
  held = false
  rb.hid.up(GESTURE) -- a chord releases right to left: the button first, then the modifiers
  if pill then
    pill.remove()
    pill = nil
  end
end

-- ===== 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 =====

-- Bind Board: the Edit hotkeys key.
BB.bind("board", "Edit hotkeys", BOARD_DEFAULT, BB.open, {
  when = function()
    local ok, w = pcall(rb.system.window)
    return ok and w ~= nil and string.find(string.lower(w.process or ""), "photoshop", 1, true) ~= nil
  end,
})

-- Plain rb.bind (outside Bind Board): the resize button, a hold with a release.
local okBind, why = pcall(rb.bind, cfg.resize_key, startResize, { release = stopResize })
if not okBind then
  rb.log.warn("resize key not bound: " .. tostring(why))
  rb.overlay.toast("Could not bind " .. tostring(cfg.resize_key) .. ". Pick another key in Options.", { title = "One-Button Resize" })
end

rb.on("blur", stopResize) -- Photoshop lost focus mid-drag: let go cleanly

rb.timer.every(1000, function()
  if cfg.reset then
    for k, v in DEFAULTS do
      rb.ui.set(k, v)
    end
    cfg.reset = false
    rb.ui.notify("Display setting reset to its default.", "success")
  end
end)

rb.log.info("One-Button Resize ready: hold " .. tostring(cfg.resize_key) .. " for " .. LABEL)

3 files, 13.9 KB.