Adding a sibling view to SDL's NSWindow.contentView (SDLView)

Hi, just wondering if anyone has tried to add a “native” NSView as a sibling to the (automatically created) SDLView?

I’m trying to add an NSView backed by an AVPlayerLayer to present a Video behind the SDL view. To that effect I’ve tried the following:

let nsWindow = SDL.window.nsWindow
let sdlContentView = nsWindow.contentView!
nsWindow.contentView = NSView(frame: sdlContentView.frame)

Xcode’s view debugger shows that the new parent and two children are in the view hierarchy in their correct z-order with correct frames. But the video view is displayed on top of the the sdlContentView no matter what I try.

I think this is because the videoPlayerAppKitView is (necessarily) layer-backed. If I set sdlContentView.wantsLayer = true, then my SDL content no longer renders at all (it’s just a black screen, although I can still click on the – now invisible – elements as usual).

Has anybody else tried something like this?

I know it’s been two years since the original post here, but I’ve been experimenting with similar things lately and am trying to nail down the best way to get the best results.

In my case, I need to add a sidebar to the SDL window. What I’ve been doing, basically, is (and pardon my Objective-C; old habits and a cross-platform C project)…

  1. SDL_CreateWindow
  2. Get the NSWindow for the SDL window
  3. Get the [nswindow contentView], which is the SDLView
  4. Create an NSSplitViewController (and the NSSplitView of course)
  5. Load the sidebar nib and create a new NSSplitViewItem using the sidebar’s viewController.
  6. Add the sidebar item to the sidebar using [splitViewController addSidebarItem:sidebarItem]
  7. Create an NSViewController and set its view to the SDLView (since there isn’t one created by libsdl)
  8. Create a new NSSplitViewItem, specifying the SDL view’s controller, using [NSSplitViewItem splitViewItemWithViewController:sdlViewController]
  9. Add the SDL view to the NSSplitView using [splitViewController addSplitViewItem:sdlViewItem]
  10. Replace the content view with the new NSSplitView

This almost works. The problem I run into is that the SDL buffer gets changed to a strange size in the process – one that’s far smaller than I expect, and my rendering gets obliterated because of it.

I’ve been looking at the SDL code to see if I can come up with a way to customize things to let me simply directly add an SDLView to my window, but haven’t gotten far yet. I think it can probably be done but want to try just a little bit longer with the code I have now before going that route because I expect it will be a huge time sink for me. Worthwhile but a time sink.

If anyone else is trying to do things like this, would like to exchange notes!

Hi @sheppy, good to see someone else working on this. I never figured this out because Mac is only our dev (not production) platform and I was too unfamiliar with the legacy of layer-backed and non-layer-backed views on macOS to debug this.

While it’d still be interesting for us to find a solution here, you’ll probably have more luck posting a new thread (I don’t know how visible a thread this old will be to people in the know)

Thanks for the note, @ephemer. I appreciate the heads-up on that. I’ll probably do just that later today or tomorrow after I finish my current round of chasing my tail on this. :slight_smile:

1 Like