123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206 |
- //
- // KMWatermarkAdjectiveTopBarView.swift
- // PDF Reader Pro
- //
- // 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<NSView> = []
-
- 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<Array<KMWatermarkAdjectiveTopBarItemModel>>) {
- 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])
- }
- }
-
- }
|