Skip to content

Refocus the origin window before pasting a picked clipboard entry - #13103

Open
surim0n wants to merge 3 commits into
omacom:quattrofrom
surim0n:fix/clipboard-paste-target
Open

surim0n wants to merge 3 commits into
omacom:quattrofrom
surim0n:fix/clipboard-paste-target

Conversation

@surim0n

@surim0n surim0n commented Sep 24, 2026

Copy link
Copy Markdown
Contributor

Summary

  • Picking a clipboard entry with the mouse pasted it into the window under the pointer, not the window the manager was opened from — the centred card means the pointer is almost always over a different tile
  • When the exclusive-keyboard layer unmaps, Hyprland restores focus then follow_mouse immediately refocuses the window under the pointer; the synthetic Shift+Insert (0.15s later) went there
  • Clipboard.qml now records the focused toplevel's address in open() (with a lastToplevelAddress fallback for the race where the signal already moved on) and passes it to both paste helpers as --window
  • The helpers wait for the layer to close, then refocus the origin window by address (hl.dsp.focus with a focuswindow fallback), polling until it sticks — the same shape as the emoji picker's mouse-selection fix in Fix Emoji Picker for all apps with both keyboard and mouse #8573
  • Without --window behaviour is unchanged; --copy-only never refocuses

Also guards the pdeathsig watcher assertion behind command -v setpriv so the suite runs off Linux.

Fixes #12987

Test plan

  • bash test/shell.d/clipboard-test.sh — new behavioural assertions: both helpers refocus address:0x… before typing, normalise bare addresses, stop once the origin window is active, and leave focus alone without --window; QML wiring assertions cover open() recording and the --window handoff. 84 assertions pass.
  • Two tiled terminals, focus right, SUPER+CTRL+V, click an entry → paste lands in the right window and focus is unchanged

The clipboard manager is a fullscreen layer holding exclusive keyboard
focus. Clicking an entry moves the pointer over whichever tiled window
sits behind the centred card, and once the layer unmaps follow_mouse
hands focus to it — so the synthetic Shift+Insert landed in the wrong
window and that window took focus.

Record the focused toplevel's address when the manager opens and pass it
to the paste helpers as --window. The helpers wait for the layer to
close, refocus the origin window by address (hl.dsp.focus with a
focuswindow fallback), and confirm it stuck before typing — the same
shape as the emoji picker's mouse-selection fix. Keyboard selection was
never broken because the pointer never moved; behaviour without
--window is unchanged.

Also skip the pdeathsig assertion when setpriv is unavailable so the
suite runs off Linux.

Fixes omacom#12987
@llstrk

llstrk commented Sep 25, 2026

Copy link
Copy Markdown

Automated AI review

Community review: Independent automated community review, unaffiliated with the Omarchy team, intended to help prepare PRs for their review.

Outcome: The diagnosis and design match the Hyprland 0.56.2 source, but the head commit's refocus call uses the legacy dispatcher syntax, which Hyprland rejects under Omarchy's Lua config. On the default setup a mouse-picked entry still pastes into the window under the pointer. This is a source-level finding; it was not reproduced on a live session.

Verified (source):

  • The root cause is as described. While the exclusive layer is mapped, Hyprland refuses window focus. On unmap it restores the last window, then simulates a mouse move, so follow_mouse focuses the tile under the pointer. That matches the two activewindowv2 events 2 ms apart in Clipboard manager pastes into the window under the pointer when an entry is clicked #12987.
  • Recording Hyprland.activeToplevel in open() captures the origin, because no activewindowv2 fires while the layer holds keyboard focus.
  • Quickshell's bare-hex address gets a 0x prefix, which is the form activewindow -j prints and address: selectors match.
  • --copy-only, copySelected, and paste without --window behave as before. Keyboard and mouse picks both go through applySelected, and Clipboard.qml is the only caller of the two helpers.

Refocus dispatch is rejected under the Lua config

Commit a036f1bf replaced hyprctl dispatch "hl.dsp.focus({ window = \"address:$window\" })" || hyprctl dispatch focuswindow ... with only the legacy form (bin/omarchy-clipboard-paste-text:79, bin/omarchy-clipboard-paste-file:61). With a Lua config, hyprctl dispatch wraps its argument as a Lua call:

hyprctl dispatch focuswindow address:0x...
  -> Hyprland v0.56.2, Lua config (src/debug/HyprCtl.cpp:1126-1141)
     eval: return hl.dispatch(focuswindow address:0x...)
     -> Lua syntax error, "error: ..." reply, hyprctl exits 7
  -> helper discards output and status, retries 15 times, 0.04 s apart
  -> wtype Shift+Insert goes to the window under the pointer (same as base, ~0.6 s later)

The legacy dispatcher table is only consulted on the non-Lua branch. This code is the same in 0.55.0, 0.56.0, 0.56.2 and current main. The repository's other focus-by-address calls all try the Lua form first (omarchy-launch-or-focus, omarchy-hyprland-focus-app, omarchy-launch-signal, omarchy-launch-spotify), and commit 804571d8 converted this same legacy call to the Lua form. The PR description still says hl.dsp.focus with a focuswindow fallback.

The new tests pass because the hyprctl stub accepts any string. They assert dispatch focuswindow address:0xdeadbeef, which is the rejected form.

Impact: #12987 remains unfixed on Omarchy's shipped Lua config. Mouse picks with the pointer over another window also gain about 0.6 s of extra delay.

Suggested change: Restore the first commit's Lua-first form (hl.dsp.focus({ window = "address:..." }), then focuswindow), and assert that form in the test, as test/shell.d/hyprland-focus-app-test.sh does. On a Lua session, hyprctl dispatch focuswindow address:0x0 should return an error: reply if the source reading is right. The unchecked two-window manual test would also confirm the fix. A successful focus dispatch also warps the pointer into the origin window, so the pointer will move in that test. Optionally, skip the Shift+Insert when the origin never becomes active. Both helpers currently paste regardless after the 15 attempts.

Empty-workspace origin once the dispatch works

This is latent at the current head, because the rejected dispatch masks it. It appears as soon as the Lua form is restored.

Quickshell 0.3.1 ignores an activewindowv2 event with an empty address (src/wayland/hyprland/ipc/connection.cpp:557-567). After a switch to an empty workspace, Hyprland.activeToplevel therefore still points at the previously focused window, so open() records it. A focus-by-address dispatch to a window on another workspace switches to that workspace and focuses it (src/config/shared/actions/ConfigActions.cpp:457-475).

Picker opened on an empty workspace Result
Base Entry copied; Shift+Insert reaches no window; user stays on the empty workspace
PR with a working dispatch Focus jumps back to the previous window's workspace and the entry is pasted into that window

Impact: An unexpected workspace jump, with clipboard content pasted into a window the user was not looking at. A multi-monitor setup changes the route, not the outcome.

Suggested change: Record the origin only when the active toplevel is on the focused workspace, for example active.workspace && active.workspace.focused in Quickshell 0.3.1, and treat an empty target as "no refocus". Special workspaces need a check before relying on this.

Smaller notes:

  • --window given as the last argument with no value makes shift 2 fail without shifting, so the option loop never ends. This is new in omarchy-clipboard-paste-file. omarchy-clipboard-paste-text already had the same pattern for --history-index.
  • The omarchy-clipboard-paste-file usage message still reads [--copy-only] <mime-type> <path>.
  • Optional test improvement: the new jq stub only extracts .address, but paste-text --history-index also runs jq -e and jq -j on the history file. The new text-paste runs therefore copy empty input and never exercise the history lookup. Real jq parses the stub's JSON, so dropping the stub keeps that path covered.

Review information

Test scope: Source-only review of head a036f1bf against Hyprland v0.56.2, the version Omarchy's package builds and the one reported in #12987, and Quickshell v0.3.1. No tests or helpers were executed and no live Hyprland session was used. The dispatch rejection and the empty-workspace behavior come from source reading. Unmap timing on a real compositor was not measured.

AI process: Opus 5.5 Medium coordination and synthesis, independent Opus 5.5 Xhigh and GPT 6 Sol Xhigh technical assessments, with targeted cross-checking of single-sourced claims, and an Opus 5.5 Medium editorial check.

Opt out: To stop receiving these reviews, reply to this comment saying so.

@surim0n

surim0n commented Sep 25, 2026

Copy link
Copy Markdown
Contributor Author

You are right, thanks. Dropping the Lua form in a036f1b was a mistake on my part. Fixed in c07a61c:

  • Both helpers now dispatch hl.dsp.focus({ window = "address:..." }) first and fall back to focuswindow, matching omarchy-launch-or-focus and omarchy-hyprland-focus-app. The test asserts the Lua form on the success path and, with the stub rejecting the Lua call, asserts the legacy fallback is taken.
  • Empty-workspace case: open() now records the origin only when activeToplevel.workspace.focused is true, and the stale lastToplevelAddress fallback is gone, so a pick from an empty workspace pastes nowhere rather than jumping workspaces.
  • --window with no value no longer spins the option loop; usage message for paste-file updated; the jq stub is removed so --history-index exercises the real history lookup.

@llstrk

llstrk commented Sep 27, 2026

Copy link
Copy Markdown

Automated AI review

Community review: Independent automated community review, unaffiliated with the Omarchy team, intended to help prepare PRs for their review.

Follow-up at c07a61c4 to the earlier review of a036f1bf, after the author's reply.

Outcome: On Omarchy's shipped Lua config, four earlier points are resolved and the empty-workspace point is resolved for regular workspaces. One narrower variant remains through an empty special workspace (below), traced in source only. No other regressions against base were found in the changed code.

Earlier point Status at c07a61c4
Refocus dispatch rejected under the Lua config Resolved
Empty-workspace origin jumps workspaces Resolved for regular workspaces; special-workspace variant below
Valueless --window spins the option loop Resolved in both helpers
paste-file usage message Resolved
jq stub hid the history lookup Resolved

Verified:

  • Both helpers now send hl.dsp.focus({ window = "address:..." }) first, which Hyprland 0.56.2 with a Lua config evaluates through hl.dispatch to a focus-by-selector action and answers ok. In a hyprctl stand-in modelled on that source, the head makes one Lua call and the Shift+Insert reaches the origin window. a036f1bf under the same model makes 15 focuswindow calls and pastes into the other window.
  • open() records an origin only when active.workspace.focused is true. In Quickshell 0.3.1 that means the workspace is the focused monitor's active workspace, so after a switch to an empty regular workspace nothing is recorded and the paste goes nowhere, as in base. When the gate fails, the helpers skip the refocus rather than jump.
  • paste-file --window and paste-text --window with no value exit 1. The paste-file usage text and args metadata list --window.
  • With the jq stub removed, the --history-index 0 runs copy the real history entry. The head suite passes (91 ok, previously 84).
  • The optional "skip Shift+Insert when the origin never becomes active" was not adopted. Behavior there is unchanged from before, not a regression.

Empty special workspace still lets a stale origin through

Quickshell 0.3.1 does not track special workspaces: it has no activespecial handling, and Hyprland emits activespecial/activespecialv2, not workspacev2, when one opens. The focused workspace stays the regular one underneath. The sequence below is traced from Hyprland v0.56.2 and Quickshell v0.3.1 source and was not run in a live session.

Workspace 1: window W focused, pointer over the workspace area
Open an empty scratchpad       -> Hyprland clears focus (empty activewindowv2, ignored by Quickshell)
                                  activeToplevel = W, W.workspace (1) still "focused"
SUPER+CTRL+V, mouse-pick entry -> origin = W
Helper: hl.dsp.focus(W)        -> focusing a non-special window closes the scratchpad
Shift+Insert                   -> pasted into W

In base the paste reaches no window and the scratchpad stays open. The same blind spot means a window on a special workspace never gets an origin. With two windows on the scratchpad and the click over the other one, the #12987 misroute remains there. That part is unchanged from base.

Impact: Narrow. It needs an empty special workspace (for example special:scratchpad with no default agent configured). The result is the same class as the earlier empty-workspace finding: the scratchpad closes and the entry is pasted into the window underneath.

Suggested change: Base the origin on Hyprland's own focus events instead of Quickshell's workspace model. For example, track activewindowv2 through Hyprland.rawEvent and clear the origin on an empty payload, or also check activespecialv2. Either would cover empty regular and special workspaces. Neither option was tested.

Smaller notes:

  • Fallback on a hyprlang config: the || hyprctl dispatch focuswindow branch cannot run there. Hyprland replies Invalid dispatcher to the Lua form, and hyprctl exits non-zero on a reply only when it starts with error:. The new fallback test passes because its stub exits 1 on the Lua call, which real hyprctl does not do for the Invalid dispatcher reply. There is no effect on the shipped Lua config, and the repository's other Lua-first focus calls share the pattern. Comparing the reply text to ok would make the fallback reachable (untested).
  • Optional test improvement: two mutations still pass the suite (91 ok): reverting paste-text's --window to shift 2, and removing the paste-file fallback. Adding the valueless --window and HYPRCTL_LUA_FAIL=1 cases for the other helper catches both (checked: each mutation then fails).
  • Related PR: fix(clipboard): paste into the window that opened the manager (#12987) #12991 is an open PR for the same issue Clipboard manager pastes into the window under the pointer when an entry is clicked #12987. It changes the same four files plus ClipboardHistory.js, using a trailing positional address rather than --window and a new originAddress helper. The two sets of source-shape test assertions conflict, so only one approach can land as is.

Review information

Test scope: Follow-up of head c07a61c4 against Hyprland v0.56.2 and Quickshell v0.3.1 source. The PR's clipboard-test.sh, both helpers under a source-derived hyprctl model (Lua and hyprlang behavior), valueless-option runs and mutation checks were run in an isolated sandbox. No live Hyprland or Quickshell session was used. The QML gate and the special-workspace sequence come from source reading only.

AI process: Opus 5.5 Medium coordination and synthesis, Opus 5.5 Xhigh technical review and final fact check, GPT 6 Sol Xhigh search for related issues, Opus 5.5 Medium editorial check.

Opt out: To stop receiving these reviews, reply to this comment saying so.

@omarchybot

Copy link
Copy Markdown
Collaborator

Thanks for this, and for turning the earlier review around so quickly. This fix competes with #12991, which was opened a day earlier for the same issue (#12987) and takes the same approach: it records the origin window in open(), accepts it only on the focused workspace, and refocuses it by address with the Lua dispatch first. When two fixes are this close, the older one is preferred, so whoever got to the fix first keeps the credit. #12991 has already been run on a worker and seen to fix the bug.

One concrete difference also favours #12991. Here, if the origin never becomes active again (it was closed, or the refocus does not take), both omarchy-clipboard-paste-text and omarchy-clipboard-paste-file still send Shift+Insert after the 15 retries, so the entry lands in whichever window has focus — the same wrong-window paste this sets out to prevent. #12991 checks focus after the retries and leaves the entry copied without typing it, and its tests cover that for both helpers. The tests here expect typing even though the stub never activates the target, so they would not catch it.

What this adds over #12991 is the named --window <address> option in place of a trailing positional address. That is a clearer interface, but it fixes nothing #12991 gets wrong, so it is not being carried across. Note too that --window --copy-only would take --copy-only as the address.

Checked by reading both diffs against quattro, by Claude Opus 5.5 and, independently, Codex Medium, which reached the same choice and found the failed-refocus paste. This branch was not run on a worker, because #12991 is the fix being taken forward. Both pull requests are with the maintainer, who decides what lands.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bug Something isn't working

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Clipboard manager pastes into the window under the pointer when an entry is clicked

3 participants