Skip to content
This repository has been archived by the owner on Jan 6, 2022. It is now read-only.

Start the app in the center of the screen #44

Open
ZolAnder85 opened this issue Sep 24, 2021 · 1 comment
Open

Start the app in the center of the screen #44

ZolAnder85 opened this issue Sep 24, 2021 · 1 comment

Comments

@ZolAnder85
Copy link

Is your feature request related to a problem? Please describe.
The window always opens in the lower left corner of the screen.

Describe the solution you'd like
Opening the app should place its window in the center of the screen, or (as many built-in apple apps do) a little higher than the center.

Describe alternatives you've considered
It might also be good to save the window position. De default position should still be improved though.

@ZolAnder85
Copy link
Author

ZolAnder85 commented Sep 24, 2021

Few years ago I have written a "nibless" cocoa app for an OpenGL based multiplatform game engine.
This is how I set up the window:

init(app: NSApplication) {
	let screen = NSScreen.main()!
	let rect = NSMakeRect(
		screen.visibleFrame.origin.x + screen.visibleFrame.width / 2 - CGFloat(preferredWidth) / screen.backingScaleFactor / 2,
		screen.visibleFrame.origin.y + screen.visibleFrame.height / 2 - CGFloat(preferredHeight) / screen.backingScaleFactor / 2,
		CGFloat(preferredWidth) / screen.backingScaleFactor,
		CGFloat(preferredHeight) / screen.backingScaleFactor
	)
	window = NSWindow(
		contentRect: rect,
		styleMask: [ NSTitledWindowMask, NSClosableWindowMask, NSResizableWindowMask ],
		backing: .buffered,
		defer: false
	)
	window.title = Bundle.main.infoDictionary!["CFBundleName"] as! String
	view = OpenGLView(frame: rect)
}

Not sure if this speeds things up for you, or where you would put this in your code. Maybe some params in the storyboard does the trick. Also, you do not need the screen.backingScaleFactor, which I used because preferred size of the game is in physical pixels.

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant