Hi all, I am trying to show an OverlayMBS panel with a NSVisualEffectView material on macOS. I want the overlay to have the “vibrancy” effect, using a material from the NSVisualVisuallEffect Material enum. Has anyone tried that, is that even possible?
This is in the Constructor of my OverlayMBS subclass.
Const kNSVisualEffectViewMaterialMenu = 5
Dim contentViewObj As NSViewMBS
Dim nsVisualEffectViewObj As NSVisualEffectViewMBS
contentViewObj = Self.NSView
If contentViewObj <> Nil Then
nsVisualEffectViewObj = New NSVisualEffectViewMBS(0, 0, Self.Width, Self.Height)
If nsVisualEffectViewObj <> Nil Then
nsVisualEffectViewObj.state = NSVisualEffectViewMBS.NSVisualEffectStateActive
nsVisualEffectViewObj.autoresizesSubviews = True
nsVisualEffectViewObj.material = kNSVisualEffectViewMaterialMenu
nsVisualEffectViewObj.autoresizingMask = NSViewMBS.NSViewWidthSizable + NSViewMBS.NSViewHeightSizable
contentViewObj.superview.addSubview(nsVisualEffectViewObj, -1, Nil)
End If
End If
1 Like
Thank you Kevin, that worked perfectly. In my test I noticed that the Mask picture must be nil.
Starting from there, I also managed to add a mask with rounded corners and a border adapting the declares from the topic about round windows.
After setting the NSVisualEffectView, I added this code to the Constructor
Declare Function NSClassFromString Lib "AppKit" (clsName As CFStringRef) As Ptr
Declare Function contentView Lib "AppKit" Selector "contentView" (windowRef As Integer) As Ptr
Declare Function CGPathCreateWithRoundedRect Lib "CoreGraphics" (rect As CGRect, cornerWidth as Double, cornerHeight as Double, transform As Ptr) As Ptr
Declare Sub setWantsLayer Lib "AppKit" Selector "setWantsLayer:" (view As Ptr, flag As Boolean)
Declare Function layer Lib "AppKit" Selector "layer" (view As Ptr) As Ptr
Declare Sub setMask Lib "QuartzCore" Selector "setMask:" (layerRef As Ptr, maskLayer As Ptr)
// CAShapeLayer specific declares
Declare Function alloc Lib "QuartzCore" Selector "alloc" (classRef As Ptr) As Ptr
Declare Function init Lib "QuartzCore" Selector "init" (ref As Ptr) As Ptr
Declare Sub setPath Lib "QuartzCore" Selector "setPath:" (layerRef As Ptr, path As Ptr)
Declare Sub setBounds Lib "QuartzCore" Selector "setBounds:" (layerRef As Ptr, rect As CGRect)
Declare Sub setFrame Lib "QuartzCore" Selector "setFrame:" (layerRef As Ptr, rect As CGRect)
// Additional needed declares
Declare Function retain Lib "Foundation" Selector "retain" (obj As Ptr) As Ptr
// Window transparency declares
Declare Sub setOpaque Lib "AppKit" Selector "setOpaque:" (windowRef As Integer, flag As Boolean)
Declare Sub setBackgroundColor Lib "AppKit" Selector "setBackgroundColor:" (windowRef As Integer, color As Ptr)
Declare Function clearColor Lib "AppKit" Selector "clearColor" (classRef As Ptr) As Ptr
// Get window reference
Var winRef As Integer = Self.WindowHandle
// Get content view and enable layer
Var viewRef As Ptr = contentView(winRef)
setWantsLayer(viewRef, True)
Var mainLayer As Ptr = layer(viewRef)
// Create rectangle for the bounds
Var r As CGRect
r.x = 0
r.y = 0
r.width = Self.Width
r.height = Self.Height
// Create circular path
var rrectPath As Ptr = CGPathCreateWithRoundedRect(r, 16, 16, nil)
// Create a CAShapeLayer
Var shapeLayerClass As Ptr = NSClassFromString("CAShapeLayer")
Var maskLayer As Ptr = alloc(shapeLayerClass)
maskLayer = init(maskLayer)
maskLayer = retain(maskLayer) // Ensure it's retained
// Set the bounds and frame of the mask layer
setBounds(maskLayer, r)
setFrame(maskLayer, r)
// Set the path
setPath(maskLayer, rrectPath)
// Apply it as a mask to the main layer
setMask(mainLayer, maskLayer)
2 Likes
HI.
I’m glad it worked for you.
It is worth noting that some, if not all of the declares you have used to give the window rounded corners can be replaced with MBS plugin functions.