Window Essentials

@rebindv0.1.4

Alt-drag any window to move it, Alt-right-drag to resize, plus pin and half-screen slot keys.

Files

main.luau

-- Window Essentials: Alt+drag moves, Alt+right-drag resizes, keys pin and
-- slot the focused window.

local MOD_NAMES = { "Ctrl+Alt", "Ctrl+Shift", "Alt+Shift", "Ctrl+Alt+Shift" }
local MODS = {
  ["Ctrl+Alt"] = { ctrl = true, alt = true },
  ["Ctrl+Shift"] = { ctrl = true, shift = true },
  ["Alt+Shift"] = { alt = true, shift = true },
  ["Ctrl+Alt+Shift"] = { ctrl = true, alt = true, shift = true },
}

local cfg = UI.Schema({
  drag = UI.Toggle(
    true,
    { label = "Alt+drag moves, Alt+right-drag resizes", tab = "Drag" }
  ),
  modifier = UI.Select(
    "Ctrl+Alt",
    MOD_NAMES,
    { label = "Modifier held with every slot key", tab = "Keys" }
  ),
  leftKey = UI.Keybind("Left", { label = "Left half", tab = "Keys" }),
  rightKey = UI.Keybind("Right", { label = "Right half", tab = "Keys" }),
  fullKey = UI.Keybind("Up", { label = "Maximize", tab = "Keys" }),
  restoreKey = UI.Keybind("Down", { label = "Restore", tab = "Keys" }),
  pinKey = UI.Keybind("P", { label = "Pin or unpin on top", tab = "Keys" }),
})

local pinned = {}
local dragging = false

local function altOnly()
  local m = Input.GetModifiers()
  return m.alt and not (m.ctrl or m.shift or m.win)
end

local function chordHeld()
  local want = MODS[cfg.modifier] or {}
  local have = Input.GetModifiers()
  for _, m in { "ctrl", "alt", "shift", "win" } do
    if (want[m] == true) ~= have[m] then
      return false
    end
  end
  return true
end

local function warn(message)
  Log.Warn(message)
  UI.Notify(message, "warning")
end

-- Follow the cursor at ~120 Hz until the button is released. The window under
-- the cursor at press time is the target; a maximized window is left alone.
local function drag(resize)
  local mx, my = System.Mouse()
  local handle = Window.FromPoint(mx, my)
  if handle == nil then
    return
  end
  if Window.GetState(handle) == "maximized" then
    return
  end
  local start = Window.GetPos(handle)
  local left = mx < start.x + start.width / 2
  local top = my < start.y + start.height / 2
  dragging = true
  Run(function()
    while dragging do
      local x, y = System.Mouse()
      local dx, dy = x - mx, y - my
      local ok, err
      if resize then
        local width = math.max(120, start.width + (if left then -dx else dx))
        local height = math.max(80, start.height + (if top then -dy else dy))
        local wx = if left then start.x + start.width - width else start.x
        local wy = if top then start.y + start.height - height else start.y
        ok, err = pcall(Window.Move, handle, wx, wy, width, height)
      else
        ok, err = pcall(Window.Move, handle, start.x + dx, start.y + dy)
      end
      if not ok then
        warn(`window could not be moved: {err}`)
        break
      end
      Sleep(8)
    end
    dragging = false
  end)
end

-- Alt went down and up with the click swallowed in between, which Windows
-- reads as a menu-bar activation request. A Ctrl tap while Alt is still held
-- cancels that without reaching the app as a shortcut.
local function endDrag()
  dragging = false
  HID.Combo("LCtrl")
end

for button, resize in { Mouse1 = false, Mouse2 = true } do
  Bind(button, {
    when = function()
      return cfg.drag and altOnly()
    end,
    action = function()
      drag(resize)
      return nil
    end,
    release = endDrag,
  })
end

local function foreground()
  local handle = Window.GetForeground()
  if handle == nil then
    warn("no focused window")
  end
  return handle
end

local function slot(side)
  local handle = foreground()
  if handle == nil then
    return
  end
  local ok, err = pcall(function()
    local area = Screen.WorkArea(System.Mouse())
    if Window.GetState(handle) == "maximized" then
      Window.Restore(handle)
    end
    local half = area.width // 2
    local x = if side == "left" then area.x else area.x + half
    Window.Move(handle, x, area.y, half, area.height)
  end)
  if not ok then
    warn(`window could not be placed: {err}`)
  end
end

local function pin()
  local handle = foreground()
  if handle == nil then
    return
  end
  local on = not pinned[handle]
  local ok, err = pcall(Window.SetAlwaysOnTop, handle, on)
  if not ok then
    warn(`pin failed: {err}`)
    return
  end
  pinned[handle] = if on then true else nil
  UI.Notify(if on then "Pinned on top" else "Unpinned", "info")
end

local KEYS = {
  {
    cfg.leftKey,
    function()
      slot("left")
    end,
  },
  {
    cfg.rightKey,
    function()
      slot("right")
    end,
  },
  {
    cfg.fullKey,
    function()
      local handle = foreground()
      if handle then
        pcall(Window.Maximize, handle)
      end
    end,
  },
  {
    cfg.restoreKey,
    function()
      local handle = foreground()
      if handle then
        pcall(Window.Restore, handle)
      end
    end,
  },
  { cfg.pinKey, pin },
}
for _, entry in KEYS do
  Bind(entry[1], {
    when = chordHeld,
    action = function()
      entry[2]()
      return nil
    end,
  })
end

function OnStart()
  Log.Info(
    `Alt+drag moves. {cfg.modifier} + Left/Right/Up/Down/P slot, maximize, restore, pin.`
  )
end

function OnStop()
  dragging = false
  for handle in pinned do
    pcall(Window.SetAlwaysOnTop, handle, false)
  end
  pinned = {}
end