// // CustomStampTopToolbar.swift // PDF Reader Pro // // Created by Niehaoyu on 2024/6/28. // import Cocoa class CustomStampTopToolbar: NSView, NibLoadable { @IBOutlet var contendView: NSView! @IBOutlet var titleLabel: NSTextField! @IBOutlet var fitWidthBtn: KMButton! @IBOutlet var increaseBtn: KMButton! @IBOutlet var reduceBtn: KMButton! @IBOutlet var zoomView: NSView! @IBOutlet var zoomBox: NSBox! @IBOutlet var zoomButton: NSPopUpButton! @IBOutlet weak var zoomTextField: NSTextField! @IBOutlet var colorBGView: NSView! @IBOutlet var seplineView1: NSView! @IBOutlet var seplineView2: NSView! var pdfView: CPDFListView! var colorChooseView: KMColorChooseItem! override func awakeFromNib() { super.awakeFromNib() self.configUI() self.reloadData() self.updateViewColor() } func configUI() { self.titleLabel.stringValue = NSLocalizedString("Canvas", comment: "") self.titleLabel.font = NSFont.SFProTextRegularFont(13) self.zoomView.wantsLayer = true self.zoomView.layer?.cornerRadius = 1 self.zoomView.layer?.masksToBounds = true self.zoomBox.borderWidth = 1 self.zoomTextField.delegate = self for item in self.zoomButton.itemArray { let string = item.title item.title = NSLocalizedString(string, comment: "") } self.colorChooseView = KMColorChooseItem.createFromNib() self.colorChooseView.frame = self.colorBGView.bounds self.colorChooseView.autoresizingMask = [.width, .height] self.colorChooseView.customColor = NSColor.white self.colorChooseView.clickHandle = {[weak self] view, color in self?.pdfView.setPageBackgroundColorWith(color, viewMode: .other) } self.colorBGView.addSubview(self.colorChooseView) self.seplineView1.wantsLayer = true self.seplineView2.wantsLayer = true } func reloadData() { guard let pdflistView = self.pdfView else { return } if pdflistView.displayModeCustomColor != nil { self.colorChooseView.customColor = pdflistView.displayModeCustomColor } else { if (pdflistView.viewMode == .normal) { self.colorChooseView.customColor = NSColor(red: 255/255, green: 255/255, blue: 255/255, alpha: 1) } else if (pdflistView.viewMode == .soft) { self.colorChooseView.customColor = NSColor(red: 245/255, green: 237/255, blue: 209/255, alpha: 1) } else if (pdflistView.viewMode == .night) { self.colorChooseView.customColor = NSColor(red: 0, green: 0, blue: 0, alpha: 1) } else if (pdflistView.viewMode == .green) { self.colorChooseView.customColor = NSColor(red: 205/255, green: 233/255, blue: 207/255, alpha: 1) } else { self.colorChooseView.customColor = pdflistView.displayModeCustomColor } } } func updateViewColor() { if KMAppearance.isDarkMode() { self.titleLabel.textColor = KMAppearance.Layout.h1Color() self.seplineView1.layer?.backgroundColor = NSColor.white.withAlphaComponent(0.1).cgColor self.seplineView2.layer?.backgroundColor = NSColor.white.withAlphaComponent(0.1).cgColor self.zoomBox.borderColor = KMAppearance.Interactive.s0Color() self.zoomBox.fillColor = KMAppearance.Layout.l1Color() } else { self.titleLabel.textColor = NSColor.black.withAlphaComponent(0.85) self.seplineView1.layer?.backgroundColor = NSColor.black.withAlphaComponent(0.1).cgColor self.seplineView2.layer?.backgroundColor = NSColor.black.withAlphaComponent(0.1).cgColor self.zoomBox.borderColor = KMAppearance.Interactive.s0Color() self.zoomBox.fillColor = KMAppearance.Layout.l1Color() } } func pdfViewScaleChanged() { guard let pdflistView = self.pdfView else { return } self.zoomTextField.stringValue = String(format: "%.2f%@", pdflistView.scaleFactor * 100, "%") let selectedTag: Int = self.zoomButton.selectedTag() switch selectedTag { case 2: if self.pdfView.scaleFactor != 1.0 { self.zoomButton.selectedItem?.state = .off } else { self.zoomButton.selectedItem?.state = .on } break case 1: let pageHeight = self.pdfView.currentPage()!.size.height let pdfviewHeight = self.pdfView.bounds.size.height if self.pdfView.scaleFactor != pdfviewHeight/pageHeight { self.zoomButton.selectedItem?.state = .off } else { self.zoomButton.selectedItem?.state = .on } break case 0: if self.pdfView.autoScales == false { self.zoomButton.selectedItem?.state = .off } else { self.zoomButton.selectedItem?.state = .on } break case 3: if self.pdfView.scaleFactor != 0.1 { self.zoomButton.selectedItem?.state = .off } else { self.zoomButton.selectedItem?.state = .on } break case 4: if self.pdfView.scaleFactor != 0.25 { self.zoomButton.selectedItem?.state = .off } else { self.zoomButton.selectedItem?.state = .on } break case 5: if self.pdfView.scaleFactor != 0.5 { self.zoomButton.selectedItem?.state = .off } else { self.zoomButton.selectedItem?.state = .on } break case 6: if self.pdfView.scaleFactor != 0.75 { self.zoomButton.selectedItem?.state = .off } else { self.zoomButton.selectedItem?.state = .on } break case 7: if self.pdfView.scaleFactor != 1.0 { self.zoomButton.selectedItem?.state = .off } else { self.zoomButton.selectedItem?.state = .on } break case 8: if self.pdfView.scaleFactor != 1.5 { self.zoomButton.selectedItem?.state = .off } else { self.zoomButton.selectedItem?.state = .on } break case 9: if self.pdfView.scaleFactor != 2.0 { self.zoomButton.selectedItem?.state = .off } else { self.zoomButton.selectedItem?.state = .on } break case 10: if self.pdfView.scaleFactor != 4.0 { self.zoomButton.selectedItem?.state = .off } else { self.zoomButton.selectedItem?.state = .on } break case 11: if self.pdfView.scaleFactor != 8.0 { self.zoomButton.selectedItem?.state = .off } else { self.zoomButton.selectedItem?.state = .on } break case 12: if self.pdfView.scaleFactor != 16.0 { self.zoomButton.selectedItem?.state = .off } else { self.zoomButton.selectedItem?.state = .on } break case 13: if self.pdfView.scaleFactor != 32.0 { self.zoomButton.selectedItem?.state = .off } else { self.zoomButton.selectedItem?.state = .on } break case 14: if self.pdfView.scaleFactor != 64.0 { self.zoomButton.selectedItem?.state = .off } else { self.zoomButton.selectedItem?.state = .on } break case 15: if self.pdfView.scaleFactor != 100.0 { self.zoomButton.selectedItem?.state = .off } else { self.zoomButton.selectedItem?.state = .on } break default: break } } //MARK: IBAction @IBAction func fitPageWidthAction(_ sender: Any) { self.pdfView.autoScales = true guard let activeAnnot = self.pdfView.activeAnnotation else { return } self.pdfView.setNeedsDisplay(self.pdfView.activeAnnotation) } @IBAction func increaseAction(_ sender: Any) { let zoomSting = self.zoomTextField.stringValue ?? "10" var scale = CGFloat((NSString(string: zoomSting)).floatValue / 100.0) switch scale { case 0...0.25: scale += 0.25 case 0.25...3: scale += 0.25 case 3.1...10: scale += 0.4 case 10.1...100: scale += 1 default: scale += 1 } self.pdfView.scaleFactor = scale self.reloadData() } @IBAction func reduceAction(_ sender: KMButton) { let zoomSting = self.zoomTextField.stringValue ?? "10" var scale = CGFloat((NSString(string: zoomSting)).floatValue / 100.0) switch scale { case 0...0.25: scale = 0 case 0.25...3: scale -= 0.25 case 3.1...10: scale -= 0.4 case 10.1...100: scale -= 1 default: scale -= 1 } self.pdfView.scaleFactor = scale self.reloadData() } @IBAction func zoomModelSelectButtonAction(_ sender: NSPopUpButton) { // self.delegate?.toolbarViewController?(self, zoomModel: sender.selectedTag()) for item in sender.itemArray { item.state = .off } sender.selectedItem?.state = .on let selectedTag: Int = sender.selectedTag() switch selectedTag { case 2: if self.pdfView.scaleFactor != 1.0 { self.pdfView.scaleFactor = 1.0 self.pdfView.autoScales = false } break case 1: let pageHeight = self.pdfView.currentPage()!.size.height let pdfviewHeight = self.pdfView.bounds.size.height self.pdfView.scaleFactor = pdfviewHeight/pageHeight self.pdfView.autoScales = false break case 0: self.pdfView.autoScales = true break case 3: self.pdfView.scaleFactor = 0.1 break case 4: self.pdfView.scaleFactor = 0.25 break case 5: self.pdfView.scaleFactor = 0.5 break case 6: self.pdfView.scaleFactor = 0.75 break case 7: self.pdfView.scaleFactor = 1.0 break case 8: self.pdfView.scaleFactor = 1.5 break case 9: self.pdfView.scaleFactor = 2.0 break case 10: self.pdfView.scaleFactor = 4.0 break case 11: self.pdfView.scaleFactor = 8.0 break case 12: self.pdfView.scaleFactor = 16.0 break case 13: self.pdfView.scaleFactor = 32.0 break case 14: self.pdfView.scaleFactor = 64.0 break case 15: self.pdfView.scaleFactor = 100.0 break default: break } self.reloadData() } override func mouseDown(with event: NSEvent) { super.mouseDown(with: event) self.window?.makeFirstResponder(nil) } } extension CustomStampTopToolbar: NSTextFieldDelegate { func controlTextDidEndEditing(_ obj: Notification) { let textField = obj.object as? NSTextField if textField == self.zoomTextField { guard let zoomString = textField?.stringValue else { return } var scale = CGFloat(textField!.floatValue / 100.0) if scale > 100 { scale = 100 } if scale < 0.1 { scale = 0.1 } if (scale <= 0.101) { self.pdfView.scaleFactor = self.pdfView.minScaleFactor() } else { self.pdfView.scaleFactor = scale } } } }