Home > Software design >  How to get default DarkMode "tinted" colors of NSWindow titlebar and content view backgrou
How to get default DarkMode "tinted" colors of NSWindow titlebar and content view backgrou

Time:10-27

Setup:

Mac OS Monterey 12.3.1 (21E258)

Steps

  1. Activate Dark Mode.
  2. Launch Xcode.
  3. File > New > Project > macOS > App, click "Next".
  4. Product Name: ColorTest, Interface: Xib, Language: Objective-C
  5. Location: Dektop, click "Create"
  6. Product > Run

Result

See ColorTest app window launch. The window titlebar color and window content view background color are both slightly tinted to match the currently selected Desktop background. These are solid colors. They are not semi-transparent "material" colors.

enter image description here

These same tinted colors can be seen by launching the "General" System Preference pane.

enter image description here enter image description here

Problem

I want to get these two colors (solid yet tinted titlebar color and solid but tinted window content background color) programmatically so I can paint them at runtime inside of -[NSView drawRect:]. I expect something like this:

NSColor *color = [NSColor windowBackgroundColor];
[color setFill];
NSRectFill(self.bounds);

I thought -[NSColor windowBackgroundColor] might return one or the other of these colors, but it does not. I have checked many other such standard color methods of NSColor, but none of them match these two colors.

It seems like it should be extremely simple to find these two colors at runtime. But I'm stumped. Can't find them. Where are they?

CodePudding user response:

You need NSVisualEffectView with .behindWindow blendingMode with .contentBackground and .titlebar materials. And then using those colors in drawRect will result in what you see.

CodePudding user response:

Create a NSVisualEffectView with blending mode set to .withinWindow.

    let effectView = NSVisualEffectView(frame: NSRect(x: 0, y: 0, width: 100, height: 100))
    effectView.blendingMode = .withinWindow
    effectView.material = .titlebar // or .windowBackground
    view.addSubview(effectView)
  • Related