NSWindow live-resizing issue - keeping window centered horizontally

I am trying to create a window that remains horizontally centered during window resizing. I've tried to subclass NSWindow and override the setframe method, as well as use the window delegate methods "windowDidResize" and "windowWillStartLiveResize" to adjust the x origin. Regardless of which method I use, the horizontal resizing stops way before the dragging event is finished. Not sure why this is happening, if anyone could provide help?


Window subclass method:

import Cocoa
class CWindow: NSWindow {
    override func setFrame(_ frameRect: NSRect, display flag: Bool) {
        var upFrame = frameRect
        upFrame = inLiveResize ? frame(for: frameRect.size) : upFrame
        super.setFrame(upFrame, display: flag)
    }

    func frame(for size: NSSize) -> NSRect {
        var newFrame: NSRect = frame
        newFrame.origin.x += (newFrame.size.width - size.width) / 2
        newFrame.origin.y += newFrame.size.height - size.height
        newFrame.size = size
        print(newFrame)
        return newFrame
    }



Windows Delegate Method:

  func windowDidResize(_ notification: Notification) {
        if (window?.inLiveResize)!  {
        winFrame.origin.x -= ((window?.frame.size.width)! - winFrame.size.width)/2
        winFrame.size.width += ((window?.frame.size.width)! - winFrame.size.width)
            winFrame.origin.y = (window?.frame.origin.y)!
            winFrame.size.height = (window?.frame.height)!
        window?.setFrame(winFrame, display: true)
        }
    }

    func windowWillStartLiveResize(_ notification: Notification) {
        winFrame = (window?.frame)!
    }



The window in question is just a standard window, no additional views or objects, no constraints, and no mimimum/maximum content size constraints.