marketplace/utility

Window Essentials

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

@rebindv0.1.0free

More install options
$rebind install @rebind/window-essentials

Gallery

Window Essentials preview

Readme

Move and size windows without hunting for their edges.

Drag

  • Alt + left drag moves the window under the cursor.
  • Alt + right drag resizes it from the bottom-right corner.

A maximized window is left where it is. Turn the drag behaviour off in the Drag tab if you only want the keys.

Keys

Hold the modifier (default Ctrl+Alt) and press:

KeyAction
LeftWindow fills the left half of the work area
RightWindow fills the right half
UpMaximize
DownRestore
PPin the window above others, press again to unpin

The work area is the monitor under the cursor, minus the taskbar. Pins are released when the script stops. Restart the script after changing a key.

Platform

Windows. macOS window operations are not handle-targeted yet, so this package does not run there.

Limits

  • Left and right clicks are captured system wide while the script runs. Clicks without Alt are passed straight through, but they do pass through the Rebind Engine first, and some games treat the result as synthetic input.
  • Some fullscreen and DirectX apps ignore pinning and repaint over pinned windows.

Reviews

No reviews yet. Write a review in the app.

Source

Version 0.1.0 (current)

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)
  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
        ok, err = pcall(
          Window.Move,
          handle,
          nil,
          nil,
          math.max(120, start.width + dx),
          math.max(80, start.height + dy)
        )
      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