123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136 |
- //
- // 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)
- }
- }
- }
|