Skip to content

Opening Wails on Windows with StartHidden:true steals focus from current window #4882

@MeshyIce

Description

@MeshyIce

Description

When running a new Wails app on Windows with StartHidden: true, the current window becomes unfocused.

To Reproduce

  1. Open any application on your Windows computer
  2. Call wails.Run with the option StartHidden: true (among your other values). Make sure to add a delay before like `time.Sleep(5 * time.Second) to allow you to set up the rest of the bug.
  3. Focus your application (that isn't Wails). Make sure it is focused by trying to enter text, for example.
  4. The Wails window should now open (in the background), and your application should lose focus.

Expected behaviour

When opening with StartHidden: true, the current open application does not lose focus

Screenshots

No response

Attempted Fixes

The root cause is in internal/frontend/desktop/windows/winc/form.go:171 In Center():

w32.SetWindowPos(fm.hwnd, w32.HWND_TOP, int(windowX), int(windowY), int(winWidth), int(winHeight), w32.SWP_NOSIZE)

The SetWindowPos call uses HWND_TOP but does not include the SWP_NOACTIVATE flag. Without this flag, Windows activates the window when positioning it, which steals focus from the foreground application.

Proposed fix:

w32.SetWindowPos(fm.hwnd, w32.HWND_TOP, int(windowX), int(windowY), int(winWidth), int(winHeight), w32.SWP_NOSIZE|w32.SWP_NOACTIVATE)

System Details

Wails Doctor



# Wails
Version | v2.11.0


# System
┌────────────────────────────────────────────────────────────────────────────────────────┐
| OS           | Windows 10 Pro for Workstations                                         |
| Version      | 2009 (Build: 26200)                                                     |
| ID           | 25H2                                                                    |
| Branding     | Windows 11 Pro for Workstations                                         |
| Go Version   | go1.25.5                                                                |
| Platform     | windows                                                                 |
| Architecture | arm64                                                                   |
| CPU          | Apple Silicon                                                           |
| GPU          | Parallels Display Adapter (WDDM) (Parallels) - Driver: 20.18.2612.57293 |
| Memory       | 8GB                                                                     |
└────────────────────────────────────────────────────────────────────────────────────────┘

# Dependencies
┌────────────────────────────────────────────────────────┐
| Dependency | Package Name | Status    | Version        |
| WebView2   | N/A          | Installed | 143.0.3650.139 |
| Nodejs     | N/A          | Available |                |
| npm        | N/A          | Available |                |
| *upx       | N/A          | Available |                |
| *nsis      | N/A          | Available |                |
|                                                        |
└─────────────── * - Optional Dependency ────────────────┘
``

Additional context

No response

Metadata

Metadata

Assignees

No one assigned

    Labels

    BugSomething isn't working

    Type

    No type

    Projects

    Status

    Backlog

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions