// // KMWatermarkAdjectiveTopBarView.swift // PDF Master // // Created by tangchao on 2022/12/15. // import Cocoa typealias KMWatermarkAdjectiveTopBarViewItemClick = (_ secton: Int,_ item: Int)->() typealias KMWatermarkAdjectiveTopBarViewApplyClick = ()->() typealias KMWatermarkAdjectiveTopBarViewCancelClick = ()->() class KMWatermarkAdjectiveTopBarView: NSView { private lazy var itemContainsView: NSView = { let itemContainsView = NSView() return itemContainsView }() private lazy var applyButton: NSButton = { let button = NSButton() button.isBordered = false button.title = NSLocalizedString("Apply", comment: "") button.wantsLayer = true button.layer?.cornerRadius = 4 button.layer?.backgroundColor = NSColor.black.cgColor button.attributedTitle = NSAttributedString(string: button.title, attributes: [NSAttributedString.Key.foregroundColor : NSColor.white]) button.target = self button.action = #selector(applyButonClick) return button }() private lazy var cancelButton: NSButton = { let button = NSButton() button.isBordered = false button.title = NSLocalizedString("Exit", comment: "") button.wantsLayer = true button.layer?.cornerRadius = 4 button.layer?.borderWidth = 1 button.layer?.borderColor = NSColor.black.cgColor button.target = self button.action = #selector(cancelButtonClick) return button }() var itemClick: KMWatermarkAdjectiveTopBarViewItemClick! var applyClick: KMWatermarkAdjectiveTopBarViewApplyClick! var cancelClick: KMWatermarkAdjectiveTopBarViewCancelClick! private var itemViews: Array = [] override init(frame frameRect: NSRect) { super.init(frame: frameRect) self.addSubview(self.itemContainsView) self.addSubview(self.applyButton) self.addSubview(self.cancelButton) } required init?(coder: NSCoder) { super.init(coder: coder) } override func layout() { super.layout() let width: CGFloat = NSWidth(self.bounds) let height: CGFloat = NSHeight(self.bounds) let itemContainsViewX: CGFloat = 120 self.itemContainsView.frame = NSMakeRect(itemContainsViewX, 0, width-itemContainsViewX*2, height) let itemLineH: CGFloat = 10 let itemH: CGFloat = 32 let itemContainsH = NSHeight(self.itemContainsView.frame) let itemConainsW = 84 let itemX: CGFloat = NSWidth(self.itemContainsView.frame)*0.5-100 var preItemView: NSView! for itemView in self.itemViews { if (itemView.isKind(of: KMWatermarkAdjectiveTopBarItemLineView.self)) { itemView.frame = NSMakeRect(preItemView.frame.maxX+10, (itemContainsH-itemLineH)*0.5, 1, itemLineH) } else { if (itemView.isEqual(to: self.itemViews.first)) { itemView.frame = NSMakeRect(itemX, (itemContainsH-itemH)*0.5, 84, itemH) } else { if (preItemView.isKind(of: KMWatermarkAdjectiveTopBarItemLineView.self)) { itemView.frame = NSMakeRect(preItemView.frame.maxX+10, (itemContainsH-itemH)*0.5, 60, itemH) } else { itemView.frame = NSMakeRect(preItemView.frame.maxX+10, (itemContainsH-itemH)*0.5, 60, itemH) } } } preItemView = itemView } let buttonSize: CGSize = CGSize(width: 44, height: 24) let buttonY = (height-buttonSize.height) * 0.5 let cancelButtonX = width-12-buttonSize.width self.cancelButton.frame = NSMakeRect(cancelButtonX, buttonY, buttonSize.width, buttonSize.height) let applyButtonX = self.cancelButton.frame.minX - 10 - buttonSize.width self.applyButton.frame = NSMakeRect(applyButtonX, buttonY, buttonSize.width, buttonSize.height) } func initItemData(itemArrays: Array>) { for subview in self.itemContainsView.subviews { subview.removeFromSuperview() } self.itemViews.removeAll() var count: Int = 0 var index: Int = 0 for items in itemArrays { for model in items { let view = KMWatermarkAdjectiveTopBarItemView() self.itemContainsView.addSubview(view) self.itemViews.append(view) view.button.title = model.itemTitle view.button.tag = index view.button.target = self view.button.action = #selector(itemButtonClick) index += 1 } if (count != itemArrays.count-1) { let line = KMWatermarkAdjectiveTopBarItemLineView() self.itemContainsView.addSubview(line) self.itemViews.append(line) line.wantsLayer = true line.layer?.backgroundColor = NSColor.lightGray.cgColor } count += 1 } self.layoutSubtreeIfNeeded() } @objc func applyButonClick() { guard let callback = applyClick else { return } callback() } @objc func cancelButtonClick() { guard let callback = cancelClick else { return } callback() } @objc func itemButtonClick(sender: NSButton) { guard let callback = itemClick else { return } let index = sender.tag callback(0, index) } func selectTopItem(index: Int) { var count: Int = 0 for item in self.itemViews { if (!(item.isKind(of: KMWatermarkAdjectiveTopBarItemView.self))) { continue } let itemView: KMWatermarkAdjectiveTopBarItemView = item as! KMWatermarkAdjectiveTopBarItemView if (index == count) { itemView.isSelected(selected: true) } else { itemView.isSelected(selected: false) } count += 1 } } func unselectTopItem(index: Int) { var count: Int = 0 for item in self.itemViews { if (!(item.isKind(of: KMWatermarkAdjectiveTopBarItemView.self))) { continue } let itemView: KMWatermarkAdjectiveTopBarItemView = item as! KMWatermarkAdjectiveTopBarItemView if (index == count) { itemView.isSelected(selected: false) } else { // itemView.isSelected(selected: false) } count += 1 } } func isCanApply(can: Bool) { self.applyButton.isEnabled = can if (can) { self.applyButton.layer?.backgroundColor = NSColor.black.cgColor self.applyButton.attributedTitle = NSAttributedString(string: self.applyButton.title, attributes: [NSAttributedString.Key.foregroundColor : NSColor.white]) } else { self.applyButton.layer?.backgroundColor = NSColor.lightGray.cgColor self.applyButton.attributedTitle = NSAttributedString(string: self.applyButton.title, attributes: [NSAttributedString.Key.foregroundColor : NSColor.white]) } } }