32 lines
1.1 KiB
Swift
32 lines
1.1 KiB
Swift
import AppKit
|
||
|
||
/// 半透明悬浮窗口
|
||
final class FloatingWindow: NSWindow {
|
||
override var canBecomeKey: Bool { true }
|
||
override var canBecomeMain: Bool { false }
|
||
|
||
override init(
|
||
contentRect: NSRect = NSRect(x: 0, y: 0, width: 250, height: 300),
|
||
styleMask style: NSWindow.StyleMask = [.borderless, .resizable],
|
||
backing backingStoreType: NSWindow.BackingStoreType = .buffered,
|
||
defer flag: Bool = false
|
||
) {
|
||
super.init(contentRect: contentRect, styleMask: style, backing: backingStoreType, defer: flag)
|
||
|
||
self.level = .floating
|
||
self.collectionBehavior = [.canJoinAllSpaces, .stationary, .fullScreenAuxiliary]
|
||
|
||
self.isOpaque = false
|
||
self.backgroundColor = .clear
|
||
self.hasShadow = true
|
||
self.isMovableByWindowBackground = true
|
||
self.minSize = NSSize(width: 240, height: 200)
|
||
|
||
// 替换默认 contentView 为透明视图,防止系统默认灰色背景露出
|
||
let view = NSView()
|
||
view.wantsLayer = true
|
||
view.layer?.backgroundColor = NSColor.clear.cgColor
|
||
self.contentView = view
|
||
}
|
||
}
|