How to draw in background window

Hello! I’m writing a macOS app. I have a custom view in every document window. Suppose I have two documents open at the same time. Call the two corresponding windows A and B.

There are often long background calculations going on in both documents. Suppose A is main. Now, whenever the window behind, B, updates its custom view to reflect the calculations’ progress, B is immediately brought in front of A, but without becoming main! Sometimes both windows will automatically alternate one in front of the other!

How can I draw in the custom view of B, the window behind, without bringing it in front of A?
Answered by ccorbell in 650077022

You might post a little code, and also clarify whether you're using AppKit or SwiftUI.

I'm guessing your background calculation is happening in a background DispatchQueue, and when it completes, you notify the window to redraw its contents using DispatchQueue.main.async to call some UI methods or post a notification?

If so, check this post-calculation UI update code - what does it do? Does it just call the view's setNeedsDisplay? Or does it do some other things like show the window? If you want to keep the window in the background, you should only call the view's setNeedsDisplay - that should make its content redraw but not change anything with regard to the window order or activation status.
Accepted Answer

You might post a little code, and also clarify whether you're using AppKit or SwiftUI.

I'm guessing your background calculation is happening in a background DispatchQueue, and when it completes, you notify the window to redraw its contents using DispatchQueue.main.async to call some UI methods or post a notification?

If so, check this post-calculation UI update code - what does it do? Does it just call the view's setNeedsDisplay? Or does it do some other things like show the window? If you want to keep the window in the background, you should only call the view's setNeedsDisplay - that should make its content redraw but not change anything with regard to the window order or activation status.
Thank you for your reply! I’m using AppKit. This is a big program, written in Objective-C. Yes, indeed the calculations are made in a background operation queue.

The code that updates the custom view in the background window is just a simple setNeedsDisplay: sent to that view, in an operation added to the main operation queue.

However, what you wrote made me think that maybe, surreptitiously, it’s my drawRect: method that, somewhere, somehow, brings the window to the foreground. My drawRect: method is v e r y complex… I’ll examine it thoroughly and will post a sequel to my reply in a couple of days. Thank you very much for your thoughts!
Well, I found pretty quickly an incongruous bringToFront: in my drawRect: code. Am I ashamed! I had put it there a long time ago, without thinking a second, in order to “solve” a problem that appeared when a document was being opened, that necessitated an alert concerning an error in the file. I had completely forgotten this, and my pseudo-solution came back on me with a vengeance. Thanks again!
How to draw in background window
 
 
Q