wails icon indicating copy to clipboard operation
wails copied to clipboard

Unable to set the initial position of the window

Open jbrodriguez opened this issue 2 years ago • 5 comments

Is your feature request related to a problem? Please describe.

the issue is that when trying to initialize the position of the window, the wails framework centers the window on the screen and only then can we move it based on last position (using preferences)

The issue comes from the fact that can set the position/size of the window, in the onDomReady hook, as suggested here

https://wails.io/docs/reference/runtime/intro/ Whilst the context will be provided to the OnStartup method, there's no guarantee the runtime will work in this method as the window is initialising in a different thread. If you wish to call runtime methods at startup, use OnDomReady.

However, on app start,

	err := wails.Run(&options.App{
		Title: "gotodo",
		Width:            1024,
		Height:           768,
		Assets:           assets,
		BackgroundColour: &options.RGBA{R: 27, G: 38, B: 54, A: 1},
		OnStartup:        app.startup,
		OnDomReady:       app.domReady,
		OnBeforeClose:    app.beforeClose,
		Bind: []interface{}{
			app,
		},
	})

one can't set the initial position of the window

this creates a noticeable jump in the window location, since all the window chrome is built and displayed, the dom is renderer and only then we can move the window

Describe the solution you'd like

add X and Y parameters to Options.App, so that the position is initialized in the wails.Run call

Describe alternatives you've considered

No response

Additional context

No response

jbrodriguez avatar Aug 05 '22 14:08 jbrodriguez

That's a fair request! Just be aware that Linux window managers are very opinionated and this may not work there. Some window managers even ignore centering.

leaanthony avatar Aug 05 '22 21:08 leaanthony

Great suggestion !

I would also like to be able to set the position of the window after the app has started using a runtime method.

pomdtr avatar Jan 01 '23 13:01 pomdtr

We hide the window in the main function:

opts := options.App{
	// other stuff
	OnDomReady:       app.domReady,
	OnShutdown:       app.shutdown,
	StartHidden: true,
}

Read the old window position and set it in domReady. After setting the window position, only then do we show the window.

func (a *App) domReady(ctx context.Context) {
	// pre-display initialization and read config...

	runtime.WindowSetPosition(a.ctx, a.config.Window.X, a.config.Window.Y)
	runtime.WindowShow(ctx)

	// post-display initialization
}

No flashing.

We save the window position only when the app closes properly:

func (a *App) shutdown(ctx context.Context) {
	// save config including window position

	a.config.Window := runtime.WindowGetSize(ctx)

	// save
}```

tjayrush avatar Jan 23 '24 19:01 tjayrush

I'm trying to set the position of the window with runtime.WindowSetPosition() runtime.WindowCenter()` but it does not work. I've tried hidding the window on startup and showing it after setting the position but that does nothing.

Is not posible to do it on Linux?

MrMarble avatar May 09 '24 20:05 MrMarble

Potentially not. The WM gods in Linux land don't believe an app has the right to position itself - it's the job of the window manager ¯\_(ツ)_/¯

https://github.com/gotk3/gotk3/issues/397#issuecomment-522357409

leaanthony avatar May 09 '24 22:05 leaanthony