123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164 |
- //
- // KMMergeCollectionPageViewItem.swift
- // PDF Master
- //
- // Created by tangchao on 2022/11/25.
- //
- import Cocoa
- import PDFKit
- typealias KMMergeCollectionPageViewItemFoldClick = (_ viewItem: AnyObject)->()
- typealias KMMergeCollectionPageViewItemSelectedClick = (_ viewItem: AnyObject)->()
- class KMMergeCollectionPageViewItem: NSCollectionViewItem {
- lazy var iconImageView = NSImageView()
- lazy var backgroundView = NSView()
- lazy var pageView = KMPDFThumbnialPageView()
-
- lazy var nameLabel = NSTextField(labelWithString: "")
-
- var flodClick: KMMergeCollectionPageViewItemFoldClick!
- var selectedClick: KMMergeCollectionPageViewItemSelectedClick!
-
- private var flodButton = NSButton()
- private var selectCheckBox = NSButton(checkboxWithTitle: "", target: nil, action: nil)
-
- private var myModel: KMMergePageModel!
- var model: KMMergePageModel {
- get {
- myModel
- }
- set {
- myModel = newValue
- pageView.page = newValue.page;
- if newValue.pageID == 0 {
- flodButton.isHidden = false
- } else {
- flodButton.isHidden = true
- }
-
-
- if model.pageID == 0 {
- nameLabel.stringValue = fileModel.documentURL.lastPathComponent.components(separatedBy: ".").first!
- } else {
- nameLabel.stringValue = String(model.pageID+1)
- }
-
- if model.selected {
- selectCheckBox.state = NSControl.StateValue.on
- } else {
- selectCheckBox.state = NSControl.StateValue.off
- }
- }
- }
-
- private var myFileModel: KMMergeFileModel!
- var fileModel: KMMergeFileModel {
- get {
- myFileModel
- }
- set {
- myFileModel = newValue
- }
- }
-
- override func viewDidLoad() {
- super.viewDidLoad()
-
- view.addSubview(backgroundView)
- view.addSubview(pageView)
- view.addSubview(flodButton)
- view.addSubview(selectCheckBox)
- view.addSubview(nameLabel)
-
- nameLabel.alignment = .center
- nameLabel.lineBreakMode = .byTruncatingTail
-
- flodButton.wantsLayer = true
- flodButton.isBordered = false;
- flodButton.title = NSLocalizedString("折叠", comment: "")
- flodButton.layer?.backgroundColor = NSColor.white.cgColor;
- flodButton.target = self
- flodButton.action = #selector(flodButtonAction)
-
- // let clickGestureRecongnizer = NSClickGestureRecognizer()
- // clickGestureRecongnizer.numberOfClicksRequired = 2
- // view.addGestureRecognizer(clickGestureRecongnizer)
- // clickGestureRecongnizer.target = self
- // clickGestureRecongnizer.action = #selector(clickGestureRecongnizerAction)
- }
-
- override func viewDidLayout() {
- super.viewDidLayout()
-
- let width = NSWidth(view.bounds)
- let height = NSHeight(view.bounds)
-
- iconImageView.frame = view.bounds
- backgroundView.frame = view.bounds
-
- let selectCheckBoxSize: CGFloat = 18
- selectCheckBox.frame = NSMakeRect(width-selectCheckBoxSize-5, height-selectCheckBoxSize-5, selectCheckBoxSize, selectCheckBoxSize)
-
- let flodSize: CGFloat = 28
- flodButton.frame = NSMakeRect(width-flodSize-10, height-flodSize-30, flodSize, flodSize)
-
- nameLabel.frame = NSMakeRect(0, 0, width, 20)
-
- let border: CGFloat = 10
- var bounds: NSRect = NSZeroRect
- if (myModel != nil) {
- bounds = myModel.page.bounds
- }
-
- if (bounds.size.width == 0) {
- return
- }
-
- if (bounds.size.height == 0) {
- return
- }
-
- let pageSelectionSize = CGSize(width: self.view.frame.size.width-30, height: self.view.frame.size.height-40)
- var size = NSMakeSize(pageSelectionSize.width - 2 * border, pageSelectionSize.height - 2 * border)
- let minScale = min(size.width/bounds.size.width, size.height/bounds.size.height)
- size.width = bounds.size.width * minScale
- size.height = bounds.size.height * minScale
-
- let pageViewX = (pageSelectionSize.width-size.width) * 0.5 + 15
- let pageViewY = (pageSelectionSize.height-size.height) * 0.5 + 10
- pageView.frame = NSMakeRect(pageViewX, pageViewY, size.width, size.height)
- }
-
- override var isSelected: Bool {
- get {
- super.isSelected
- }
- set {
- super.isSelected = newValue
-
- backgroundView.wantsLayer = true
- if newValue {
- backgroundView.layer?.backgroundColor = NSColor.blue.cgColor
- } else {
- backgroundView.layer?.backgroundColor = NSColor.clear.cgColor
- }
- }
- }
-
- override func mouseUp(with event: NSEvent) {
- guard let callback = selectedClick else {
- return
- }
- callback(self)
- }
-
- @objc func flodButtonAction() {
- guard let callback = flodClick else {
- return
- }
- callback(self)
- }
- }
|