Files
omarchycn/bin/omarchy-capture-webcam-resize
T
ada53b090e Anchor the webcam overlay to the recorded region, not the monitor (#6384)
* Anchor the webcam overlay to the recorded region, not the monitor

Recording a single window on an ultrawide put the camera outside the frame.
The overlay is placed twice, and neither placement knows what is being
captured: the window rules in default/hypr/apps/webcam-overlay.lua position it
from monitor_w/monitor_h, and omarchy-capture-webcam-resize recomputes the same
corner from hyprctl monitors. Both resolve to the monitor's bottom-right, so a
window anywhere but the far right of the display records without the camera in
it, and dragging it into frame by hand is undone by the next resize.

The geometry is already known. select_capture_target resolves the picked window
to a region rectangle before start_webcam_overlay runs, and a bare click in
smart mode snaps that rectangle to the window's own bounds. gpu-screen-recorder
takes the region in the compositor's logical coordinate space, which is the
same space window moves take, so the value needs no conversion to be reused as
an anchor.

Publish it for the resize helper, which becomes the single place that positions
the overlay. It anchors to the region when one is recorded and falls back to
the monitor otherwise, so full-monitor captures, the portal backend, and manual
resizes outside a recording all keep their current behaviour. A malformed or
empty file falls back the same way.

Presets scale from the anchor's height rather than the monitor's, so the camera
keeps its proportion of the frame instead of covering a small capture outright.
A tall, narrow region cannot fit a preset derived from its height, so widths are
capped to the space available and heights follow at the same 8:9 aspect.

Placement happens inside start_webcam_overlay rather than after it returns.
Correcting the position once that function had returned left the move adjacent
to gpu-screen-recorder starting, and the camera was recorded sliding the last
stretch into its corner over the opening frames. The overlay is waited for
explicitly instead, positioned, and only then does capture start.

Waiting for the map is what the blind second was partly guessing at, so the
remainder is trimmed to hold the delay before capture where it was. Starting
later is not free: it eats the opening words of whatever is being narrated.

* Keep the webcam size ladder usable in a narrow region

Capping each preset's width to the region separately collapsed small,
medium and large onto the same width, so Super + Alt + [ and ] had
nothing to step between, and integer division left small taller than
medium. Cap the height the presets scale from instead, which shrinks
the ladder as a whole and keeps the three sizes ordered and distinct.

Cover the anchoring in the test suite: a region the camera follows, the
fallback for an unreadable one, and the narrow-region ladder. Point
XDG_RUNTIME_DIR at the test's own directory while doing so — the resize
helper now reads a region file from there, and the existing geometry
assertions would pick up a real one from a live recording.

Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>

---------

Co-authored-by: David Heinemeier Hansson <david@hey.com>
Co-authored-by: Claude Opus 5 (1M context) <noreply@anthropic.com>
2026-07-26 15:27:06 -07:00

150 lines
4.8 KiB
Bash
Executable File

#!/bin/bash
# omarchy:summary=Resize the active webcam recording overlay
# omarchy:group=capture
# omarchy:args=<smaller|larger|reset|small|medium|large>
# omarchy:examples=omarchy capture webcam resize smaller | omarchy-capture-webcam-resize reset
set -euo pipefail
readonly MARGIN=40
readonly REGION_FILE="${XDG_RUNTIME_DIR:-/tmp}/omarchy-screenrecord-region"
usage() {
echo "Usage: omarchy-capture-webcam-resize <smaller|larger|reset|small|medium|large>" >&2
exit 1
}
hypr_dispatch() {
local lua="$1"
shift
hyprctl dispatch "$lua" >/dev/null 2>&1 || hyprctl dispatch "$@" >/dev/null
}
action=${1:-}
case $action in
smaller | larger | reset | small | medium | large) ;;
*) usage ;;
esac
if ! client=$(hyprctl clients -j 2>/dev/null | jq -cer 'first(.[] | select(.title == "WebcamOverlay")) // empty' 2>/dev/null); then
exit 0
fi
read -r address current_width current_height monitor_id < <(
jq -r '[.address, .size[0], .size[1], .monitor] | @tsv' <<<"$client"
)
[[ -n $address && $current_width =~ ^[0-9]+$ && $current_height =~ ^[0-9]+$ && $monitor_id =~ ^[0-9]+$ ]] || exit 0
((current_width > 0 && current_height > 0)) || exit 0
if ! monitor=$(hyprctl monitors -j 2>/dev/null | jq -cer --argjson id "$monitor_id" 'first(.[] | select(.id == $id)) // empty' 2>/dev/null); then
exit 0
fi
read -r monitor_x monitor_y monitor_width monitor_height < <(
jq -r '
. as $monitor |
(($monitor.transform // 0) % 2 == 1) as $rotated |
[
.x,
.y,
(((if $rotated then .height else .width end) / .scale) | floor),
(((if $rotated then .width else .height end) / .scale) | floor)
] | @tsv
' <<<"$monitor"
)
[[ $monitor_x =~ ^-?[0-9]+$ && $monitor_y =~ ^-?[0-9]+$ && $monitor_width =~ ^[0-9]+$ && $monitor_height =~ ^[0-9]+$ ]] || exit 0
# Anchor to the recorded region when there is one, so a window picked on a wide
# display keeps the camera in its own corner. Full-monitor captures, the portal
# backend, and resizes outside a recording publish none and fall back here.
anchor_x=$monitor_x
anchor_y=$monitor_y
anchor_width=$monitor_width
anchor_height=$monitor_height
if [[ -f $REGION_FILE ]] && region=$(<"$REGION_FILE"); then
if [[ $region =~ ^([0-9]+)x([0-9]+)\+(-?[0-9]+)\+(-?[0-9]+)$ ]]; then
anchor_width=${BASH_REMATCH[1]}
anchor_height=${BASH_REMATCH[2]}
anchor_x=${BASH_REMATCH[3]}
anchor_y=${BASH_REMATCH[4]}
fi
fi
# A tall, narrow region can't fit presets scaled from its own height, so cap the
# height they scale from to what the width allows — the large preset is the
# widest at 3/10 of it. Scaling the ladder as a whole leaves small, medium and
# large distinct sizes for smaller and larger to step between.
scale_height=$anchor_height
available_width=$((anchor_width - 2 * MARGIN))
((available_width > 0 && scale_height * 3 / 10 > available_width)) &&
scale_height=$((available_width * 10 / 3))
# Scale the 8:9 portrait presets from that height so they occupy the same
# proportion of a 1080p, HiDPI, ultrawide, or 6K recording.
small_height=$(((scale_height * 9 + 25) / 50))
small_width=$(((small_height * 8 + 4) / 9))
medium_height=$(((scale_height + 2) / 4))
medium_width=$(((medium_height * 8 + 4) / 9))
large_height=$(((scale_height * 27 + 40) / 80))
large_width=$(((large_height * 8 + 4) / 9))
target_width=$current_width
target_height=$current_height
case $action in
small)
target_width=$small_width
target_height=$small_height
;;
medium | reset)
target_width=$medium_width
target_height=$medium_height
;;
large)
target_width=$large_width
target_height=$large_height
;;
smaller)
if ((large_width < current_width)); then
target_width=$large_width
target_height=$large_height
elif ((medium_width < current_width)); then
target_width=$medium_width
target_height=$medium_height
elif ((small_width < current_width)); then
target_width=$small_width
target_height=$small_height
fi
;;
larger)
if ((small_width > current_width)); then
target_width=$small_width
target_height=$small_height
elif ((medium_width > current_width)); then
target_width=$medium_width
target_height=$medium_height
elif ((large_width > current_width)); then
target_width=$large_width
target_height=$large_height
fi
;;
esac
target_x=$((anchor_x + anchor_width - target_width - MARGIN))
target_y=$((anchor_y + anchor_height - target_height - MARGIN))
((target_x < anchor_x + MARGIN)) && target_x=$((anchor_x + MARGIN))
((target_y < anchor_y + MARGIN)) && target_y=$((anchor_y + MARGIN))
window="address:$address"
hypr_dispatch \
"hl.dsp.window.resize({ window = \"$window\", x = $target_width, y = $target_height })" \
resizewindowpixel "exact $target_width $target_height,$window"
hypr_dispatch \
"hl.dsp.window.move({ window = \"$window\", x = $target_x, y = $target_y })" \
movewindowpixel "exact $target_x $target_y,$window"