Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

On NSWindowController, setting contentViewController updates the window's position, how can I stop this?

I have a NSWindowController with an NSWindow, I am having an issue where, if I set the controller's contentViewController, the frame of the NSWindow updates beyond my expectations

  1. Window is at a origin, say (500,500), and size is (100,100)
  2. I drag it to (300,300)
  3. I press a control which updates contentViewController
  4. The size remains at (100,100), but the origin goes back to (500, 500)

The entire window remains the same size, but it just moves back to the original position

This is the code that updates the frame

self.contentViewController = loadingViewController

I confirmed this by printing out the window's frame in lldb, before and after this line:

▿ Optional<CGRect>
  ▿ some : (-1701.0, 439.0, 1042.0, 778.0)
    ▿ origin : (-1701.0, 439.0)
      - x : -1701.0
      - y : 439.0
    ▿ size : (1042.0, 778.0)
      - width : 1042.0
      - height : 778.0

(lldb) po self.window?.frame
▿ Optional<CGRect>
  ▿ some : (-1680.0, 416.0, 800.0, 778.0)
    ▿ origin : (-1680.0, 416.0)
      - x : -1680.0
      - y : 416.0
    ▿ size : (1042.0, 778.0)
      - width : 1042.0
      - height : 778.0

What is even more mysterious, is that I originally tried 3 separate places to catch the window moving to locate the line of code causing it

I registered as an observer:

NotificationCenter.default.addObserver(self, selector: #selector(windowMoved(notification:)), name: NSWindow.didMoveNotification, object: nil)

as well as setting symbolic breakpoints on:

-[NSWindow _windowMoved:] and -[NSWindow _setFrame:]

All 3 of these methods caught window movement when I manually dragged the window around.

However, none of the 3 caught the window moving when the frame was changed programmatically from that one line of code above ^^

Has anybody seen this before, or have an idea of how this could be?

like image 259
A O Avatar asked Oct 24 '25 13:10

A O


1 Answers

Turned out to be too much effort to isolate this issue into a test project, so I decided to just put a patch in,

I just store the origin before setting the contentViewController, and reset it afterwards. This works for me, though I'm still unsure why this is happening

    let originalFrameOrigin = self.window?.frame.origin ?? CGPoint(x: 100, y: 100)
    self.contentViewController = loadingViewController
    self.window?.setFrameOrigin(originalFrameOrigin)
like image 66
A O Avatar answered Oct 27 '25 03:10

A O



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!