3
votes

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?

1
How can we reproduce the issue in a test project? - Willeke
oo that's a good point. I'm about out of ideas, especially because I can't catch the window's frame changing instruction, I can only see it visually change. I'll try to pull it out, isolate it into a test project and if I can figure out the issue I'll report back. Otherwise I'll just upload the test project for you guys to check out - A O

1 Answers

1
votes

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)