123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161 |
- //
- // KMLinkPageView.swift
- // PDF Reader Pro
- //
- // Created by Niehaoyu on 2024/10/18.
- //
- import Cocoa
- import KMComponentLibrary
- @objc public protocol KMLinkPageViewDelegate: AnyObject {
-
- @objc optional func kmLinkPageViewDidGoToPage(_ view: KMLinkPageView, _ pageIndex: Int)
-
- @objc optional func kmLinkPageViewDidChangeDestination(_ view: KMLinkPageView, _ pageIndex: Int)
-
- }
-
- public class KMLinkPageView: BaseXibView {
- @IBOutlet var infoContendView: NSView!
- @IBOutlet var imageBGView: NSView!
- @IBOutlet var pageImage: NSImageView!
-
- @IBOutlet var paginationView: ComponentPagination!
-
- @IBOutlet var goButton: ComponentButton!
-
- var pdfView: CPDFView? = nil
-
- var annotation: CPDFLinkAnnotation? = nil
-
- var startPageIndex: Int = 0
- var choosedIndex: Int = 0
- var isGo: Bool = false
- weak open var delegate: KMLinkPageViewDelegate?
- //MARK: - func
- public override func draw(_ dirtyRect: NSRect) {
- super.draw(dirtyRect)
-
- // Drawing code here.
- }
-
- public required init?(coder decoder: NSCoder) {
- super.init(coder: decoder)
-
- }
-
- override init(frame frameRect: NSRect) {
- super.init(frame: frameRect)
- }
-
- public override func awakeFromNib() {
- super.awakeFromNib()
-
- self.setUpUI()
- }
-
-
-
- func setUpUI() {
- infoContendView.wantsLayer = true
- if let value = ComponentLibrary.shared.getComponentValueFromKey("radius/m") {
- let currentValue = value as! CGFloat
- infoContendView.layer?.cornerRadius = currentValue
- }
- infoContendView.layer?.borderWidth = 1
- infoContendView.layer?.borderColor = ComponentLibrary.shared.getComponentColorFromKey("colorBorder/4").cgColor
- infoContendView.layer?.backgroundColor = ComponentLibrary.shared.getComponentColorFromKey("colorFill/4").cgColor
- infoContendView.layer?.masksToBounds = true
-
- imageBGView.wantsLayer = true
- if let value = ComponentLibrary.shared.getComponentValueFromKey("comp-field/radius") {
- let currentValue = value as! CGFloat
- imageBGView.layer?.cornerRadius = currentValue
- }
- imageBGView.layer?.borderWidth = 1
- imageBGView.layer?.borderColor = ComponentLibrary.shared.getComponentColorFromKey("colorBorder/4").cgColor
- imageBGView.layer?.backgroundColor = ComponentLibrary.shared.getComponentColorFromKey("colorFill/4").cgColor
- imageBGView.layer?.masksToBounds = true
-
- paginationView.properties = ComponentPaginationProperty(doubleArrow_show: false, currentIndex: 0, totalCount: 1)
- paginationView.delegate = self
-
- goButton.properties = ComponentButtonProperty(type: .default_tertiary,
- size: .s,
- buttonText: KMLocalizedString("Go"))
- goButton.setTarget(self, action: #selector(buttonClicked(_:)))
- }
-
-
- func reloadData() {
- guard let pdfView = self.pdfView else {
- return
- }
-
- paginationView.properties.currentIndex = pdfView.currentPageIndex + 1
- if let destination = annotation?.destination() {
- if let page = destination.page() {
- let pageIndex = page.document?.index(for: page) ?? 0
- paginationView.properties.currentIndex = Int((pageIndex + 1))
- }
- }
-
- paginationView.properties.totalCount = Int(pdfView.document.pageCount)
- paginationView.reloadData()
-
- isGo = false
- goButton.properties.buttonText = KMLocalizedString("Go")
-
- choosedIndex = paginationView.properties.currentIndex
- startPageIndex = paginationView.properties.currentIndex
-
- }
-
- func refreshGoButton() {
- if isGo {
- goButton.properties.buttonText = KMLocalizedString("Back")
- } else {
- goButton.properties.buttonText = KMLocalizedString("Go")
- }
- goButton.reloadData()
-
- }
-
- @objc func buttonClicked(_ sender: NSView) {
- if isGo == false {
- delegate?.kmLinkPageViewDidChangeDestination?(self, choosedIndex)
- isGo = true
- } else {
- delegate?.kmLinkPageViewDidGoToPage?(self, startPageIndex)
- isGo = false
- }
- refreshGoButton()
-
- }
-
-
- //MARK: - MouseEvent
- public override func mouseDown(with event: NSEvent) {
- super.mouseDown(with: event)
-
- window?.makeFirstResponder(nil)
- }
-
- }
- //MARK: - ComponentPaginationDelegate
- extension KMLinkPageView: ComponentPaginationDelegate {
- public func componentPaginationDidValueChanged(pagination: ComponentPagination) {
- if choosedIndex != pagination.properties.currentIndex {
- choosedIndex = pagination.properties.currentIndex
-
- isGo = false
-
- refreshGoButton()
- }
- }
- }
|