简体   繁体   中英

How to avoid running window after its size scaled to the minimal size?

The main window has a minimal width, height and an aspect ratio via NSSize However when the window scale to the minimal size, it would start to running downwards on the screen if the resizing event keep triggering from the top or right window corner.

Is there any window property have been missed here?

let contentView = ContentView()
    .frame(minWidth: 120, maxWidth: .infinity, minHeight: 50, maxHeight: .infinity)
    .edgesIgnoringSafeArea(.all)

/// Create the window and set the content view.
window = NSWindow(
    contentRect: NSRect(x: 0, y: 0, width: 120, height: 50),
    styleMask: [.titled, .closable, .miniaturizable, .resizable, .fullSizeContentView],
    backing: .buffered, defer: false, onKeyDown: handleKeyDownEvent)

window.contentView = NSHostingView(rootView: contentView)
window.contentView?.window?.aspectRatio = NSSize(
    width: 120,
    height: 50
)

Tried prevent the window from resizing via the window delegate method, windowWillResize(_:to:) source but the window still running around when it get squeezed.

Also tried to use windowWillMove(_:) source to print the window position and with an intension to stop it from moving. However, the position changed by squeezing the window wont be captured in the debug console.

在此处输入图片说明

Remove

window.contentView?.window?.aspectRatio = NSSize(
    width: 120,
    height: 50
)

A better approach to make window resize in proportion is to do it inside windowWillResize(_:to:) an NSWindowDelegate method. Don't forget to set the window.delegate if you set up all user interface programmatically.

extension AppDelegate: NSWindowDelegate {
    func windowWillResize(_ sender: NSWindow, to frameSize: NSSize) -> NSSize {
        return CGSize(width: frameSize.height * SOME_RATIO, height: frameSize.height)
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM