123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174 |
- //
- // KMMergeCollectionViewItem.swift
- // PDF Master
- //
- // Created by tangchao on 2022/11/23.
- //
- import Cocoa
- import PDFKit
- typealias KMMergeCollectionViewItemDoubleClick = (_ viewItem: AnyObject)->()
- typealias KMMergeCollectionViewItemPageRangeClick = (_ viewItem: AnyObject)->()
- class KMMergeCollectionViewItem: NSCollectionViewItem {
- lazy var iconImageView = NSImageView()
- lazy var backgroundView = NSView()
- lazy var selectedView = NSView()
- lazy var pageView = KMPDFThumbnialPageView()
-
- private var expandButton = NSButton()
- private var pageRangeButton = NSButton()
-
- lazy var nameLabel = NSTextField(labelWithString: "")
-
- var doubleClick: KMMergeCollectionViewItemDoubleClick!
- var pageRangeClick: KMMergeCollectionViewItemPageRangeClick!
-
- private var myModel: KMMergeFileModel!
- var model: KMMergeFileModel {
- get {
- myModel
- }
- set {
- myModel = newValue
-
- pageView.page = newValue.page;
- nameLabel.stringValue = model.documentURL.lastPathComponent.components(separatedBy: ".").first!
- if newValue.pageRange == .custom {
- pageRangeButton.title = model.pageRangeString
- } else {
- pageRangeButton.title = (newValue.pageRange).rawValue
- }
- }
- }
-
- override func viewDidLoad() {
- super.viewDidLoad()
-
- view.addSubview(backgroundView)
- view.addSubview(selectedView)
- view.addSubview(pageView)
- view.addSubview(expandButton)
- view.addSubview(pageRangeButton)
- view.addSubview(nameLabel)
-
- selectedView.wantsLayer = true
- selectedView.layer?.backgroundColor = NSColor.white.cgColor
- selectedView.layer?.borderWidth = 1
- selectedView.layer?.borderColor = NSColor.white.cgColor
- selectedView.layer?.cornerRadius = 8
- nameLabel.alignment = .center
- nameLabel.lineBreakMode = .byTruncatingTail
-
- expandButton.wantsLayer = true
- expandButton.isBordered = false
- expandButton.title = NSLocalizedString("Expand", comment: "")
- expandButton.layer?.backgroundColor = NSColor.white.cgColor;
- expandButton.target = self
- expandButton.action = #selector(expandButtonAction)
-
- pageRangeButton.wantsLayer = true
- pageRangeButton.isBordered = false
- pageRangeButton.layer?.backgroundColor = NSColor.lightGray.cgColor
- pageRangeButton.target = self
- pageRangeButton.action = #selector(pageRangeButtonAction)
-
- 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 flodSize: CGFloat = 28
- expandButton.frame = NSMakeRect(width-flodSize-10, height-flodSize-10, flodSize, flodSize)
-
- pageRangeButton.frame = NSMakeRect(15, 10+20+10, width-30, 28)
-
- selectedView.frame = NSMakeRect(15, 20, width-30, height-20)
-
- 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
- }
-
- var pageSelectionSize = CGSize(width: self.view.frame.size.width-30, height: self.view.frame.size.height-40)
- // pageSelectionSize.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 + 20
- 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
- selectedView.layer?.borderColor = NSColor.blue.cgColor
- } else {
- // backgroundView.layer?.backgroundColor = NSColor.clear.cgColor
- selectedView.layer?.borderColor = NSColor.white.cgColor
- }
- }
- }
-
- override func mouseDown(with event: NSEvent) {
- if event.clickCount == 2 {
- guard let callback = doubleClick else {
- return
- }
- callback(self)
- } else {
- super.mouseDown(with: event)
- }
- }
-
- @objc func clickGestureRecongnizerAction() {
-
- }
-
- @objc func expandButtonAction() {
- guard let callback = doubleClick else {
- return
- }
- callback(self)
- }
-
- @objc func pageRangeButtonAction() {
- guard let callback = pageRangeClick else {
- return
- }
- callback(self)
- }
- }
|