// // KMWatermarkAligementView.swift // PDF Master // // Created by tangchao on 2023/1/17. // import Cocoa typealias KMWatermarkAligementViewItemClick = (_ index: Int)->() class KMWatermarkAligementView: NSView { var leftButton = NSButton() var centerButton = NSButton() var rightButton = NSButton() var vLine = NSView() var vLine2 = NSView() var itemClick: KMWatermarkAligementViewItemClick! private var myIsEnabled: Bool = true var isEnabled: Bool { get { return myIsEnabled } set { myIsEnabled = newValue for button in [leftButton, centerButton, rightButton] { button.isEnabled = newValue } } } 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(leftButton) addSubview(centerButton) addSubview(rightButton) addSubview(vLine) addSubview(vLine2) for button in [leftButton, centerButton, rightButton] { button.isBordered = false button.wantsLayer = true button.target = self button.action = #selector(buttonAction) } leftButton.title = "" centerButton.title = "" rightButton.title = "" for line in [vLine, vLine2] { line.wantsLayer = true line.layer?.backgroundColor = NSColor.black.cgColor } buttonAction(sender: leftButton) } override func layout() { super.layout() let width: CGFloat = NSWidth(self.bounds) let height: CGFloat = NSHeight(self.bounds) let itemWidth: CGFloat = width / 3 leftButton.frame = NSMakeRect(0, 0, itemWidth, height) centerButton.frame = NSMakeRect(itemWidth, 0, itemWidth, height) rightButton.frame = NSMakeRect(2*itemWidth, 0, itemWidth, height) vLine.frame = NSMakeRect(itemWidth, 0, 1, height) vLine2.frame = NSMakeRect(2*itemWidth, 0, 1, height) } @objc func buttonAction(sender: NSButton) { dealButtonSelectedState(selectButton: sender) guard let callback = itemClick else { return } var index: Int = 0 if (sender.isEqual(to: leftButton)) { index = 0 } else if (sender.isEqual(to: centerButton)) { index = 1 } else if (sender.isEqual(to: rightButton)) { index = 2 } callback(index) } func dealButtonSelectedState(selectButton: NSButton) { for button in [leftButton, centerButton, rightButton] { if (button.isEqual(to: selectButton)) { button.wantsLayer = true button.layer?.cornerRadius = 4 button.layer?.borderWidth = 1 button.layer?.borderColor = NSColor.km_init(hex: "#CED0D4").cgColor button.layer?.backgroundColor = NSColor.km_init(hex: "#CED0D499").cgColor } else { button.wantsLayer = true button.layer?.backgroundColor = NSColor.clear.cgColor button.layer?.borderColor = NSColor.clear.cgColor } } if (selectButton.isEqual(to: self.leftButton)) { self.leftButton.image = NSImage(named: "KMImageNameEditPDFAlignLeftSelect") self.leftButton.wantsLayer = true self.leftButton.layer?.backgroundColor = NSColor.km_init(hex: "#CED0D499").cgColor self.centerButton.image = NSImage(named: "KMImageNameEditPDFAlignCenter") self.rightButton.image = NSImage(named: "KMImageNameEditPDFAlignRight") } else if (selectButton.isEqual(to: self.centerButton)) { self.centerButton.image = NSImage(named: "KMImageNameEditPDFAlignCenterSelect") self.leftButton.image = NSImage(named: "KMImageNameEditPDFAlignLeft") self.rightButton.image = NSImage(named: "KMImageNameEditPDFAlignRight") } else if (selectButton.isEqual(to: self.rightButton)) { self.rightButton.image = NSImage(named: "KMImageNameEditPDFAlignRightSelect") self.centerButton.image = NSImage(named: "KMImageNameEditPDFAlignCenter") self.leftButton.image = NSImage(named: "KMImageNameEditPDFAlignLeft") } } func selectIndex(index: Int) { if (index == 0) { dealButtonSelectedState(selectButton: leftButton) } else if (index == 1) { dealButtonSelectedState(selectButton: centerButton) } else if (index == 2) { dealButtonSelectedState(selectButton: rightButton) } } }