// // KMWatermarkPositionView.swift // PDF Master // // Created by tangchao on 2022/12/18. // import Cocoa enum KMWatermarkPosition: Int { case leftTop = 0 case top = 1 case rightTop = 2 case leftMiddle = 3 case middle = 4 case rightMiddle = 5 case leftBottom = 6 case bottom = 7 case rightBottom = 8 } typealias KMWatermarkPositionItemClick = (_ position: KMWatermarkPosition)->() class KMWatermarkPositionView: NSView { var hLine1 = NSView() var hLine2 = NSView() var vLine1 = NSView() var vLine2 = NSView() var buttons: Array = [] var position: KMWatermarkPosition = .middle var itemClick: KMWatermarkPositionItemClick! override init(frame frameRect: NSRect) { super.init(frame: frameRect) initSubViews() } required init?(coder: NSCoder) { super.init(coder: coder) initSubViews() } func initSubViews() { wantsLayer = true layer?.borderWidth = 1 layer?.borderColor = NSColor.black.cgColor layer?.cornerRadius = 2 addSubview(hLine1) addSubview(hLine2) addSubview(vLine1) addSubview(vLine2) for line in [hLine1, hLine2, vLine1, vLine2] { line.wantsLayer = true line.layer?.backgroundColor = NSColor.black.cgColor } for button in buttons { button.removeFromSuperview() } buttons.removeAll() for i in 0 ... 8 { let button = NSButton() button.isBordered = false button.wantsLayer = true button.title = "" button.tag = i addSubview(button) buttons.append(button) button.target = self button.action = #selector(buttonAction) } buttonAction(sender: buttons[4]) } override func layout() { super.layout() let width: CGFloat = NSWidth(self.bounds) let height: CGFloat = NSHeight(self.bounds) let itemSize: CGFloat = height / 3 hLine1.frame = NSMakeRect(0, 2*itemSize, width, 1) hLine2.frame = NSMakeRect(0, itemSize, width, 1) vLine1.frame = NSMakeRect(itemSize, 0, 1, height) vLine2.frame = NSMakeRect(2*itemSize, 0, 1, height) var i: Int = 0 for button in buttons { let x: Int = i % 3 let y: Int = i / 3 button.frame = NSMakeRect(CGFloat(x) * itemSize, CGFloat(y) * itemSize, itemSize, itemSize) i += 1 } } @objc func buttonAction(sender: NSButton) { dealButtonSelectedState(selectButton: sender) if (sender.tag == 0) { self.position = .leftBottom } else if (sender.tag == 1) { self.position = .bottom } else if (sender.tag == 2) { self.position = .rightBottom } else if (sender.tag == 3) { self.position = .leftMiddle } else if (sender.tag == 4) { self.position = .middle } else if (sender.tag == 5) { self.position = .rightMiddle } else if (sender.tag == 6) { self.position = .leftTop } else if (sender.tag == 7) { self.position = .top } else if (sender.tag == 8) { self.position = .rightTop } guard let callback = itemClick else { return } callback(self.position) } func dealButtonSelectedState(selectButton: NSButton) { for button in buttons { if (button.isEqual(to: selectButton)) { button.layer?.backgroundColor = NSColor.black.cgColor } else { button.layer?.backgroundColor = NSColor.clear.cgColor } } } func selectPosition(positon: KMWatermarkPosition) { self.position = positon if (positon == .leftTop) { dealButtonSelectedState(selectButton: buttons[0]) } else if (positon == .top) { dealButtonSelectedState(selectButton: buttons[1]) } else if (positon == .rightTop) { dealButtonSelectedState(selectButton: buttons[2]) } else if (positon == .leftMiddle) { dealButtonSelectedState(selectButton: buttons[3]) } else if (positon == .middle) { dealButtonSelectedState(selectButton: buttons[4]) } else if (positon == .rightMiddle) { dealButtonSelectedState(selectButton: buttons[5]) } else if (positon == .leftBottom) { dealButtonSelectedState(selectButton: buttons[6]) } else if (positon == .bottom) { dealButtonSelectedState(selectButton: buttons[7]) } else if (positon == .rightBottom) { dealButtonSelectedState(selectButton: buttons[8]) } } }