123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394 |
- //
- // KMToolbarPreviousNextItemView.swift
- // PDF Reader Pro
- //
- // Created by tangchao on 2023/12/15.
- //
- import Cocoa
- private func _KMPreviousNextString() -> String {
- return NSLocalizedString("Previous/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.greatestFiniteMagnitude, 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.greatestFiniteMagnitude, 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.greatestFiniteMagnitude, 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)
- }
- }
- }
|