// // KMToolbarPreviousNextItemView.swift // PDF Reader Pro // // Created by tangchao on 2023/12/15. // import Cocoa private func _KMPreviousNextString() -> String { return "\(NSLocalizedString("Previous", comment: ""))/\(NSLocalizedString("Next", comment: ""))" } private let _minWidth: CGFloat = 24 * 2 class KMToolbarPreviousNextItemView: NSView { var callback: KMCommonClickBlock? lazy var previousButton: KMCoverButton = { let button = KMCoverButton() button.wantsLayer = true button.isBordered = false button.layer?.cornerRadius = 6 button.image = NSImage(named: "KMImageNameToolbarPagepreviousNor") button.toolTip = NSLocalizedString("Go To Previous Page", comment: "") button.imagePosition = .imageOnly button.target = self button.action = #selector(buttonClicked) button.coverAction = { cbtn, caction in if caction == .enter { cbtn.layer?.backgroundColor = KMToolbarItemView.selectedBackgroundColor.cgColor } else if caction == .exit { cbtn.layer?.backgroundColor = KMToolbarItemView.normalBackgroundColor.cgColor } } return button }() lazy var nextButton: KMCoverButton = { let button = KMCoverButton() button.wantsLayer = true button.isBordered = false button.layer?.cornerRadius = 6 button.image = NSImage(named: "KMImageNameToolbarPagenextNor") button.toolTip = NSLocalizedString("Go To Next Page", comment: "") button.imagePosition = .imageOnly button.target = self button.action = #selector(buttonClicked) button.coverAction = { cbtn, caction in if caction == .enter { cbtn.layer?.backgroundColor = KMToolbarItemView.selectedBackgroundColor.cgColor } else if caction == .exit { cbtn.layer?.backgroundColor = KMToolbarItemView.normalBackgroundColor.cgColor } } return button }() lazy var titleLabel: NSTextField = { let label = NSTextField(labelWithString: _KMPreviousNextString()) label.font = KMToolbarMainItemView.textFont label.textColor = KMToolbarMainItemView.fetchTextNormalColor() return label }() convenience init() { self.init(frame: NSMakeRect(0, 0, Self.itemWidth, 40)) } override init(frame frameRect: NSRect) { super.init(frame: frameRect) self.initSubview() } required init?(coder: NSCoder) { super.init(coder: coder) self.initSubview() } func initSubview() { self.addSubview(self.previousButton) self.addSubview(self.nextButton) self.addSubview(self.titleLabel) self.previousButton.km_add_left_constraint() self.previousButton.km_add_right_constraint(equalTo: self.nextButton, attribute: .left) self.previousButton.km_add_top_constraint() self.previousButton.km_add_height_constraint(constant: 24) self.nextButton.km_add_right_constraint() self.nextButton.km_add_width_constraint(equalTo: self.previousButton, attribute: .width) self.nextButton.km_add_top_constraint() self.nextButton.km_add_height_constraint(constant: 24) self.titleLabel.km_add_bottom_constraint() self.titleLabel.km_add_height_constraint(constant: 14) self.titleLabel.km_add_left_constraint() self.titleLabel.km_add_right_constraint() self.titleLabel.km_add_width_constraint(constant: Self.itemWidth) // self.titleLabel.km_add_top_constraint(equalTo: self.previousButton, attribute: .bottom) } class var itemWidth: CGFloat { get { let string = _KMPreviousNextString() let width = string.boundingRect(with: NSSize(width: CGFLOAT_MAX, height: 20), options: [.usesLineFragmentOrigin, .truncatesLastVisibleLine], attributes: [.font : KMToolbarMainItemView.textFont]).size.width + 5 * 2 if width < _minWidth { return _minWidth } return width } } override func draw(_ dirtyRect: NSRect) { super.draw(dirtyRect) // Drawing code here. } @objc func buttonClicked(_ sender: NSButton) { guard let block = self.callback else { return } if self.previousButton.isEqual(to: sender) { block(1) } else if self.nextButton.isEqual(to: sender) { block(2) } } } private func _KMPreviousBackString() -> String { return "\(NSLocalizedString("Forward", comment: ""))/\(NSLocalizedString("Back", comment: ""))" } //private let _minWidth: CGFloat = 24 * 2 class KMToolbarPreviousBackItemView: NSView { var callback: KMCommonClickBlock? lazy var previousButton: KMCoverButton = { let button = KMCoverButton() button.wantsLayer = true button.isBordered = false button.layer?.cornerRadius = 6 button.image = NSImage(named: "KMImageNameToolbarForwardNor") button.toolTip = NSLocalizedString("Go To Previous Page", comment: "") button.imagePosition = .imageOnly button.target = self button.action = #selector(buttonClicked) button.coverAction = { cbtn, caction in if caction == .enter { cbtn.layer?.backgroundColor = KMToolbarItemView.selectedBackgroundColor.cgColor } else if caction == .exit { cbtn.layer?.backgroundColor = KMToolbarItemView.normalBackgroundColor.cgColor } } return button }() lazy var nextButton: KMCoverButton = { let button = KMCoverButton() button.wantsLayer = true button.isBordered = false button.layer?.cornerRadius = 6 button.image = NSImage(named: "KMImageNameToolbarBackwardNor") button.toolTip = NSLocalizedString("Go To Next Page", comment: "") button.imagePosition = .imageOnly button.target = self button.action = #selector(buttonClicked) button.coverAction = { cbtn, caction in if caction == .enter { cbtn.layer?.backgroundColor = KMToolbarItemView.selectedBackgroundColor.cgColor } else if caction == .exit { cbtn.layer?.backgroundColor = KMToolbarItemView.normalBackgroundColor.cgColor } } return button }() lazy var titleLabel: NSTextField = { let label = NSTextField(labelWithString: _KMPreviousBackString()) label.font = KMToolbarMainItemView.textFont label.textColor = KMToolbarMainItemView.fetchTextNormalColor() return label }() convenience init() { self.init(frame: NSMakeRect(0, 0, Self.itemWidth, 40)) } override init(frame frameRect: NSRect) { super.init(frame: frameRect) self.initSubview() } required init?(coder: NSCoder) { super.init(coder: coder) self.initSubview() } func initSubview() { self.addSubview(self.previousButton) self.addSubview(self.nextButton) self.addSubview(self.titleLabel) self.previousButton.km_add_left_constraint() self.previousButton.km_add_right_constraint(equalTo: self.nextButton, attribute: .left) self.previousButton.km_add_top_constraint() self.previousButton.km_add_height_constraint(constant: 24) self.nextButton.km_add_right_constraint() self.nextButton.km_add_width_constraint(equalTo: self.previousButton, attribute: .width) self.nextButton.km_add_top_constraint() self.nextButton.km_add_height_constraint(constant: 24) self.titleLabel.km_add_bottom_constraint() self.titleLabel.km_add_height_constraint(constant: 14) self.titleLabel.km_add_left_constraint() self.titleLabel.km_add_right_constraint() self.titleLabel.km_add_width_constraint(constant: Self.itemWidth) // self.titleLabel.km_add_top_constraint(equalTo: self.previousButton, attribute: .bottom) } class var itemWidth: CGFloat { get { let string = _KMPreviousBackString() let width = string.boundingRect(with: NSSize(width: CGFLOAT_MAX, height: 20), options: [.usesLineFragmentOrigin, .truncatesLastVisibleLine], attributes: [.font : KMToolbarMainItemView.textFont]).size.width + 5 * 2 if width < _minWidth { return _minWidth } return width } } override func draw(_ dirtyRect: NSRect) { super.draw(dirtyRect) // Drawing code here. } @objc func buttonClicked(_ sender: NSButton) { guard let block = self.callback else { return } if self.previousButton.isEqual(to: sender) { block(1) } else if self.nextButton.isEqual(to: sender) { block(2) } } } private func _KMFirstLastString() -> String { return "\(NSLocalizedString("First", comment: ""))/\(NSLocalizedString("Last", comment: ""))" } //private let _minWidth: CGFloat = 24 * 2 class KMToolbarFirstLastItemView: NSView { var callback: KMCommonClickBlock? lazy var previousButton: KMCoverButton = { let button = KMCoverButton() button.wantsLayer = true button.isBordered = false button.layer?.cornerRadius = 6 button.image = NSImage(named: "KMImageNameToolbarFirstpageNor") button.toolTip = NSLocalizedString("Go To Previous Page", comment: "") button.imagePosition = .imageOnly button.target = self button.action = #selector(buttonClicked) button.coverAction = { cbtn, caction in if caction == .enter { cbtn.layer?.backgroundColor = KMToolbarItemView.selectedBackgroundColor.cgColor } else if caction == .exit { cbtn.layer?.backgroundColor = KMToolbarItemView.normalBackgroundColor.cgColor } } return button }() lazy var nextButton: KMCoverButton = { let button = KMCoverButton() button.wantsLayer = true button.isBordered = false button.layer?.cornerRadius = 6 button.image = NSImage(named: "KMImageNameToolbarLastpageNor") button.toolTip = NSLocalizedString("Go To Next Page", comment: "") button.imagePosition = .imageOnly button.target = self button.action = #selector(buttonClicked) button.coverAction = { cbtn, caction in if caction == .enter { cbtn.layer?.backgroundColor = KMToolbarItemView.selectedBackgroundColor.cgColor } else if caction == .exit { cbtn.layer?.backgroundColor = KMToolbarItemView.normalBackgroundColor.cgColor } } return button }() lazy var titleLabel: NSTextField = { let label = NSTextField(labelWithString: _KMFirstLastString()) label.font = KMToolbarMainItemView.textFont label.textColor = KMToolbarMainItemView.fetchTextNormalColor() return label }() convenience init() { self.init(frame: NSMakeRect(0, 0, Self.itemWidth, 40)) } override init(frame frameRect: NSRect) { super.init(frame: frameRect) self.initSubview() } required init?(coder: NSCoder) { super.init(coder: coder) self.initSubview() } func initSubview() { self.addSubview(self.previousButton) self.addSubview(self.nextButton) self.addSubview(self.titleLabel) self.previousButton.km_add_left_constraint() self.previousButton.km_add_right_constraint(equalTo: self.nextButton, attribute: .left) self.previousButton.km_add_top_constraint() self.previousButton.km_add_height_constraint(constant: 24) self.nextButton.km_add_right_constraint() self.nextButton.km_add_width_constraint(equalTo: self.previousButton, attribute: .width) self.nextButton.km_add_top_constraint() self.nextButton.km_add_height_constraint(constant: 24) self.titleLabel.km_add_bottom_constraint() self.titleLabel.km_add_height_constraint(constant: 14) self.titleLabel.km_add_left_constraint() self.titleLabel.km_add_right_constraint() self.titleLabel.km_add_width_constraint(constant: Self.itemWidth) // self.titleLabel.km_add_top_constraint(equalTo: self.previousButton, attribute: .bottom) } class var itemWidth: CGFloat { get { let string = _KMFirstLastString() let width = string.boundingRect(with: NSSize(width: CGFLOAT_MAX, height: 20), options: [.usesLineFragmentOrigin, .truncatesLastVisibleLine], attributes: [.font : KMToolbarMainItemView.textFont]).size.width + 5 * 2 if width < _minWidth { return _minWidth } return width } } override func draw(_ dirtyRect: NSRect) { super.draw(dirtyRect) // Drawing code here. } @objc func buttonClicked(_ sender: NSButton) { guard let block = self.callback else { return } if self.previousButton.isEqual(to: sender) { block(1) } else if self.nextButton.isEqual(to: sender) { block(2) } } }