Skip to content

Auto-submit snippets with Cmd+Enter on macOS in Avalonia app#129

Merged
maraf merged 2 commits into
mainfrom
maraf/avalonia-ctrl-enter-auto-submit
Apr 22, 2026
Merged

Auto-submit snippets with Cmd+Enter on macOS in Avalonia app#129
maraf merged 2 commits into
mainfrom
maraf/avalonia-ctrl-enter-auto-submit

Conversation

@maraf

@maraf maraf commented Apr 22, 2026

Copy link
Copy Markdown
Member

The WPF app has a handy shortcut: hold Ctrl while pressing Enter to apply a snippet and it sends an extra Enter after the paste to auto-submit (e.g. run the pasted command). The Avalonia app was missing this; this PR brings the same behavior over, using Cmd on macOS and Ctrl on Windows.

Approach

Navigator.Send now samples the auto-submit modifier state at entry (before closing the main window and restoring focus), mirroring the WPF flow. If the modifier is held, it waits for the key to be released, pastes, then posts an extra Enter. Modifier detection is platform-specific but stays entirely inside the Avalonia project so the shared ISendTextService signature is unchanged:

  • macOS uses CGEventSourceFlagsState for the state query and CGEventCreateKeyboardEvent + CGEventPost for the Return keystroke, reusing the existing CoreGraphics P/Invoke helper introduced in Faster frontmost app detection on macOS via NSWorkspace + CGEvent #127. No osascript spawn on the hot path.
  • Windows uses GetAsyncKeyState(VK_CONTROL) via a new small WindowsApplication helper.
  • Linux falls back to xdotool key Return for the keystroke and is treated as "modifier not pressed" for state (same conservative behavior as the existing paste path).

Notes for reviewers

  • Behavior parity with WPF: same 5s wait-for-release timeout, same order (wait, paste, enter).
  • The shared ISendTextService/ApplySnippetCommand/tests are untouched - this is Avalonia-only plumbing.
  • All 110 existing tests still pass.

Match the WPF behavior: when a snippet is applied via Enter while the
auto-submit modifier is held (Cmd on macOS, Ctrl on Windows), wait for
the modifier to be released, paste, then send an extra Enter to submit
the pasted content.

- Avalonia Navigator.Send samples the modifier state at entry using
  platform APIs (CGEventSourceFlagsState on macOS, GetAsyncKeyState on
  Windows) and gates the extra Enter on that.
- MacOSApplication.SendEnterShortcut sends the Return key via
  AppleScript; SimulateEnter falls back to 'xdotool key Return' on
  Linux.

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This PR brings WPF’s “auto-submit” behavior to the Avalonia app by detecting an “auto-submit modifier” at send-time, waiting for release, pasting the snippet, and optionally sending an extra Enter—using platform-specific implementations (notably CoreGraphics on macOS).

Changes:

  • Sample auto-submit modifier state at entry to ISendTextService.Send, wait-for-release (5s timeout), then paste and optionally send Enter.
  • Add macOS CoreGraphics-based Enter keystroke simulation and Command-key state query.
  • Add a small Windows helper to query Ctrl key state via GetAsyncKeyState.

Reviewed changes

Copilot reviewed 3 out of 3 changed files in this pull request and generated 2 comments.

File Description
src/Neptuo.Productivity.SnippetManager.Avalonia/WindowsApplication.cs Adds Windows Ctrl key state query helper via GetAsyncKeyState.
src/Neptuo.Productivity.SnippetManager.Avalonia/Navigator.cs Implements modifier sampling, wait-for-release, and conditional Enter simulation after paste.
src/Neptuo.Productivity.SnippetManager.Avalonia/MacOSApplication.cs Adds CGEvent-based Return keystroke posting and Command modifier state query.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment thread src/Neptuo.Productivity.SnippetManager.Avalonia/Navigator.cs
Comment thread src/Neptuo.Productivity.SnippetManager.Avalonia/Navigator.cs Outdated
…Process

- Removed Windows branch from IsAutoSubmitModifierPressed (and the
  WindowsApplication helper). SimulatePaste/SimulateEnter have no
  Windows implementation yet, so detecting Ctrl there just caused a
  5s wait-for-release with no follow-up keystroke.
- Wrapped the Linux xdotool Process.Start calls in 'using' to dispose
  the returned Process handle.

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
@maraf maraf merged commit e8e30be into main Apr 22, 2026
1 check passed
@maraf maraf deleted the maraf/avalonia-ctrl-enter-auto-submit branch April 22, 2026 10:55
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants