123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642 |
- //
- // KMSecondaryPDFView.swift
- // PDF Reader Pro
- //
- // Created by tangchao on 2024/1/17.
- //
- import Cocoa
- class KMSecondaryPDFView: CPDFListView {
-
- var scalePopUpButton: NSPopUpButton?
- var pagePopUpButton: NSPopUpButton?
- var toolModeButton: NSButton?
-
- private var _controlView: NSView?
- var controlView: NSView? {
- get {
- return self._controlView
- }
- }
-
- private var _synchronizedPDFView: CPDFView?
- var synchronizedPDFView: CPDFView? {
- get {
- return self._synchronizedPDFView
- }
- set {
- if self._synchronizedPDFView != newValue {
- if self.synchronizeZoom {
- self._stopObservingSynchronizedPDFView()
- }
- self._synchronizedPDFView = newValue
- if self.synchronizeZoom {
- self._startObservingSynchronizedPDFView()
- }
- }
- }
- }
-
- var synchronizeZoom = false
- var switching = false
- private var _selectsText = false
- var selectsText: Bool {
- get {
- return self._selectsText
- }
- set {
- if self._selectsText != newValue {
- self._selectsText = newValue
- if newValue == false {
- self.currentSelection = CPDFSelection(document: self.document)
- }
- self.toolModeButton?.state = self.selectsText ? .on : .off
- KMDataManager.ud_set(newValue, forKey: SKLastSecondarySelectsTextKey)
- }
-
- if self.selectsText {
- self.toolMode = .textToolMode
- } else {
- self.toolMode = .moveToolMode
- }
- }
- }
-
- var startScale: CGFloat = 0.0
-
-
- private var SKDefaultScaleMenuLabels = ["=", "Auto", "10%", "20%", "25%", "35%", "50%", "60%", "71%", "85%", "100%", "120%", "141%", "170%", "200%", "300%", "400%", "600%", "800%", "1000%", "1200%", "1400%", "1700%", "2000%"]
- private var SKDefaultScaleMenuFactors = [0.0, 0.0, 0.1, 0.2, 0.25, 0.35, 0.5, 0.6, 0.71, 0.85, 1.0, 1.2, 1.41, 1.7, 2.0, 3.0, 4.0, 6.0, 8.0, 10.0, 12.0, 14.0, 17.0, 20.0]
-
- private var SKMinDefaultScaleMenuFactor = 0.1
- private var SKDefaultScaleMenuFactorsCount = 24
-
- private var CONTROL_FONT_SIZE = 10.0
- private var CONTROL_HEIGHT = 15.0
- private var CONTROL_WIDTH_OFFSET = 20.0
-
- override func draw(_ dirtyRect: NSRect) {
- super.draw(dirtyRect)
-
- // Drawing code here.
- }
-
- deinit {
- KMPrint("KMSecondaryPDFView deinit.")
-
- NotificationCenter.default.removeObserver(self)
- }
-
- override init(frame frameRect: NSRect) {
- super.init(frame: frameRect)
-
- self.commonInitialization()
- self._initControlView()
- }
-
- required init?(coder: NSCoder) {
- super.init(coder: coder)
-
- self.commonInitialization()
- self._initControlView()
- }
-
- func commonInitialization() {
- self.scalePopUpButton = nil
- self.pagePopUpButton = nil
- self.synchronizedPDFView = nil
- self.synchronizeZoom = false
- self.selectsText = KMDataManager.ud_bool(forKey: SKLastSecondarySelectsTextKey)
-
- NotificationCenter.default.addObserver(self, selector: #selector(handlePageChangedNotification), name: NSNotification.Name.CPDFViewPageChanged, object: self)
- NotificationCenter.default.addObserver(self, selector: #selector(themeChanged), name: NSApplication.interfaceThemeChangedNotification, object: nil)
- }
-
- private func _initControlView() {
- if (self.scalePopUpButton == nil) {
- // create it
- self.scalePopUpButton = NSPopUpButton(frame: NSMakeRect(0.0, 0.0, 1.0, 1.0), pullsDown: false)
- self.scalePopUpButton?.cell?.controlSize = .small
- self.scalePopUpButton?.isBordered = false
- self.scalePopUpButton?.isEnabled = true
- self.scalePopUpButton?.refusesFirstResponder = true
- (self.scalePopUpButton?.cell as? NSPopUpButtonCell)?.usesItemFromMenu = true
-
- // set a suitable font, the control size is 0, 1 or 2
- self.scalePopUpButton?.font = .toolTipsFont(ofSize: CONTROL_FONT_SIZE)
-
- var cnt = 0
- let numberOfDefaultItems = SKDefaultScaleMenuFactorsCount
- var curItem: NSMenuItem?
- var label: String?
- var width: CGFloat = 0
- var maxWidth: CGFloat = 0
- var size = NSMakeSize(1000.0, 1000.0)
- let attrs = [NSAttributedString.Key.font : self.scalePopUpButton?.font ?? .toolTipsFont(ofSize: CONTROL_FONT_SIZE)]
- var maxIndex = 0
-
- for i in 0 ..< numberOfDefaultItems {
- cnt = i
- label = Bundle.main.localizedString(forKey: SKDefaultScaleMenuLabels[i], value: "", table: "")
- width = NSWidth(label!.boundingRect(with: size, options: NSString.DrawingOptions(rawValue: 0), attributes: attrs))
- if (width > maxWidth) {
- maxWidth = width
- maxIndex = cnt
- }
- self.scalePopUpButton?.addItem(withTitle: label ?? "")
- curItem = self.scalePopUpButton?.item(at: cnt)
- curItem?.representedObject = SKDefaultScaleMenuFactors[cnt] > 0.0 ? NSNumber(value: SKDefaultScaleMenuFactors[cnt]) : nil
- }
-
- if self.synchronizeZoom {
- self.setSynchronizeZoom(true, adjustPopup: true)
- } else if self.autoScales {
- self.setScaleFactor(0.0, adjustPopup: true)
- } else {
- self.setScaleFactor(self.scaleFactor, adjustPopup: true)
- }
-
- self.sizePopUpToItemAtIndex(self.scalePopUpButton, anIndex: UInt(maxIndex))
-
- self.scalePopUpButton?.refusesFirstResponder = true
-
- self.scalePopUpButton?.target = self
- self.scalePopUpButton?.action = #selector(scalePopUpAction)
- }
-
- if (self.pagePopUpButton == nil) {
- self.pagePopUpButton = NSPopUpButton(frame: NSMakeRect(0.0, 0.0, 1.0, 1.0), pullsDown: false)
- self.pagePopUpButton?.cell?.controlSize = .small
- self.pagePopUpButton?.isBordered = false
- self.pagePopUpButton?.isEnabled = true
- self.pagePopUpButton?.refusesFirstResponder = true
- (self.pagePopUpButton?.cell as? NSPopUpButtonCell)?.usesItemFromMenu = true
-
- self.pagePopUpButton?.font = .toolTipsFont(ofSize: CONTROL_FONT_SIZE)
-
- self.reloadPagePopUpButton()
-
- self.pagePopUpButton?.refusesFirstResponder = true
-
- self.pagePopUpButton?.target = self
- self.pagePopUpButton?.action = #selector(pagePopUpAction)
- }
-
- if (self.toolModeButton == nil) {
- self.toolModeButton = NSButton(frame: NSMakeRect(0.0, 0.0, 17.0, 15.0))
-
- self.toolModeButton?.setButtonType(.onOff)
- self.toolModeButton?.bezelStyle = .texturedSquare
- self.toolModeButton?.image = NSImage(named: "TextToolAdorn")
-
- self.toolModeButton?.state = self.selectsText ? .on : .off
-
- self.toolModeButton?.refusesFirstResponder = true
-
- self.toolModeButton?.target = self
- self.toolModeButton?.action = #selector(toolModeButtonAction)
- }
-
- if (self._controlView == nil) {
- var toolRect = self.toolModeButton?.frame ?? .zero
- var pageRect = self.pagePopUpButton?.frame ?? .zero
- var scaleRect = self.scalePopUpButton?.frame ?? .zero
- var tmpRect: NSRect = .zero
-
- self._controlView = NSView(frame: NSMakeRect(0.0, 0.0, NSWidth(toolRect) + NSWidth(pageRect) + NSWidth(scaleRect), CONTROL_HEIGHT))
-
- self.toolModeButton?.autoresizingMask = [.maxXMargin, .maxYMargin]
- self.pagePopUpButton?.autoresizingMask = [.maxXMargin, .maxYMargin]
- self.scalePopUpButton?.autoresizingMask = [.minXMargin, .maxYMargin]
- NSDivideRect(self.controlView?.bounds ?? .zero, &toolRect, &tmpRect, NSWidth(toolRect), .minX)
- NSDivideRect(tmpRect, &pageRect, &scaleRect, NSWidth(pageRect), .minX)
- self.toolModeButton?.frame = toolRect
- self.pagePopUpButton?.frame = pageRect
- self.scalePopUpButton?.frame = scaleRect
- self.controlView?.addSubview(self.toolModeButton!)
- self.controlView?.addSubview(self.pagePopUpButton!)
- self.controlView?.addSubview(self.scalePopUpButton!)
- self.controlView?.wantsLayer = true
- }
- }
-
- func setSynchronizeZoom(_ newSync: Bool, adjustPopup flag: Bool = true) {
- if self.synchronizeZoom != newSync {
- var savedSwitching = self.switching
- self.switching = true
- self.synchronizeZoom = newSync
- if (newSync) {
- if self.autoScales {
- super.autoScales = false
- }
- super.scaleFactor = self.synchronizedPDFView != nil ? self.synchronizedPDFView!.scaleFactor : 1.0
- self._startObservingSynchronizedPDFView()
- if (flag) {
- self.scalePopUpButton?.selectItem(at: 0)
- }
- } else {
- self._stopObservingSynchronizedPDFView()
- self.setScaleFactor(self.scaleFactor, adjustPopup: flag)
- }
- self.switching = savedSwitching
- }
- }
-
- @objc func themeChanged(_ notification: Notification) {
- DispatchQueue.main.asyncAfter(deadline: .now() + 0.3) {
- self.updateViewColor()
- }
- }
-
- override var document: CPDFDocument! {
- get {
- return super.document
- }
- set {
- if super.document != nil {
- NotificationCenter.default.removeObserver(self, name: NSNotification.Name.CPDFDocumentDidUnlock, object: self.document)
- }
- super.document = newValue
- self.reloadPagePopUpButton()
-
- if newValue != nil && newValue.isLocked {
- NotificationCenter.default.addObserver(self, selector: #selector(handleDocumentDidUnlockNotification), name: NSNotification.Name.CPDFDocumentDidUnlock, object: newValue)
- }
- }
- }
-
- func updateViewColor() {
- self.controlView?.wantsLayer = true
- if KMAppearance.isDarkMode() {
- self.controlView?.layer?.backgroundColor = NSColor(red: 37/255.0, green: 37/255.0, blue: 38/255.0, alpha: 1).cgColor
- } else {
- self.controlView?.layer?.backgroundColor = NSColor(red: 250/255.0, green: 250/255.0, blue: 250/255.0, alpha: 1).cgColor
- }
- self.controlView?.layer?.backgroundColor = KMAppearance.Layout.l0Color().cgColor
- }
-
- // MARK: - Popup buttons
-
- func reloadPagePopUpButton() {
- let labels = self.document?.pageLabels() ?? []
- var count = self.pagePopUpButton?.numberOfItems ?? 0
- var size = NSMakeSize(1000, 1000)
- var attrs = [NSAttributedString.Key.font : self.pagePopUpButton?.font ?? NSFont.toolTipsFont(ofSize: CONTROL_FONT_SIZE)]
- var maxWidth = 0.0
- var maxIndex = 0
-
- while count > 0 {
- count -= 1
- self.pagePopUpButton?.removeItem(at: count)
- }
-
- if labels.count > 0 {
- for (i, label) in labels.enumerated() {
- var width = NSWidth(label.boundingRect(with: size, options: NSString.DrawingOptions(rawValue: 0), attributes: attrs))
- if (width > maxWidth) {
- maxWidth = width;
- maxIndex = i
- }
- self.pagePopUpButton?.addItem(withTitle: label)
- }
-
- self.sizePopUpToItemAtIndex(self.pagePopUpButton, anIndex: UInt(maxIndex))
- self.pagePopUpButton?.selectItem(at: self.currentPageIndex)
- if (self.controlView != nil) {
- let w = NSWidth(self.toolModeButton?.frame ?? .zero) + NSWidth(self.pagePopUpButton?.frame ?? .zero) + NSWidth(self.scalePopUpButton?.frame ?? .zero)
- let h = NSHeight(self.controlView?.frame ?? .zero)
- self.controlView?.setFrameSize(NSMakeSize(w, h))
- }
- }
- }
-
- @objc func scalePopUpAction(_ sender: NSPopUpButton) {
- let selectedFactorObject = sender.selectedItem?.representedObject
- if (selectedFactorObject != nil) {
- self.setScaleFactor(selectedFactorObject as! CGFloat, adjustPopup: false)
- } else if (sender.indexOfSelectedItem == 0) {
- self.setSynchronizeZoom(true, adjustPopup: false)
- } else {
- self.setAutoScales(true, adjustPopup: false)
- }
- }
-
- @objc func pagePopUpAction(_ sender: NSPopUpButton) {
- self.go(toPageIndex: sender.indexOfSelectedItem, animated: false)
- }
-
- @objc func toolModeButtonAction(_ sender: NSButton) {
- self.selectsText = sender.state == .on
- }
-
- override var canZoomIn: Bool {
- if super.canZoomIn == false {
- return false
- }
-
- let numberOfDefaultItems = SKDefaultScaleMenuFactorsCount
- let i = self.lowerIndex(for: self.scaleFactor)
- return i < numberOfDefaultItems - 1
- }
-
- override func zoomIn(_ sender: Any!) {
- let numberOfDefaultItems = SKDefaultScaleMenuFactorsCount
- var i = self.lowerIndex(for: self.scaleFactor)
- if i < numberOfDefaultItems-1 {
- i += 1
- }
- self.setScaleFactor(SKDefaultScaleMenuFactors[Int(i)], adjustPopup: true)
- }
-
- override var canZoomOut: Bool {
- if super.canZoomOut == false {
- return false
- }
- let i = self.upperIndex(for: self.scaleFactor)
- return i > 2
- }
-
- override func zoomOut(_ sender: Any!) {
- var i = self.upperIndex(for: self.scaleFactor)
- if i > 2 {
- i -= 1
- }
- self.setScaleFactor(SKDefaultScaleMenuFactors[Int(i)], adjustPopup: true)
- }
-
- override func performKeyEquivalent(with event: NSEvent) -> Bool {
- return false
- }
-
- @IBAction func toggleDisplayAsBookFromMenu(_ sender: AnyObject?) {
- self.displaysAsBook = !self.displaysAsBook
- if self.displaysAsBook {
- self.displayTwoUp = true
- self.displayDirection = .horizontal
- }
- self.layoutDocumentView()
- }
-
- @IBAction func toggleDisplayPageBreaksFromMenu(_ sender: AnyObject?) {
- self.displaysPageBreaks = !self.displaysPageBreaks
- self.layoutDocumentView()
- }
-
- @objc func doActualSize(_ sender: AnyObject?) {
- self.scaleFactor = 1.0
- }
-
- @objc func doPhysicalSize(_ sender: AnyObject?) {
-
- }
-
- @objc func changeToolMode(_ sender: NSMenuItem?) {
- self.selectsText = ((sender?.tag ?? 0) == 1)
- }
-
- override func menu(for event: NSEvent) -> NSMenu? {
- var selectionActions = ["copy:", "_searchInSpotlight:", "_searchInGoogle:", "_searchInDictionary:", "_revealSelection:"]
- var menu = super.menu(for: event)
- var item: NSMenuItem?
- if self.selectsText == false {
- self.currentSelection = CPDFSelection(document: self.document)
- while let cnt = menu?.numberOfItems, cnt > 0 {
- item = menu?.item(at: 0)
- if item!.isSeparatorItem || selectionActions.contains(NSStringFromSelector(item!.action!)) {
- menu?.removeItem(at: 0)
- } else {
- break
- }
- }
- }
-
- if let idx = menu?.indexOfItem(withTarget: nil, andAction: NSSelectorFromString("menuItemClick_Paste:")) { // 移除 粘贴菜单
- let num = menu?.numberOfItems ?? 0
- if idx >= 0 && idx < num {
- menu?.removeItem(at: idx)
- }
- }
-
- var i = menu?.indexOfItem(withTarget: nil, andAction: NSSelectorFromString("menuItemClick_TwoPagesContinuous:"))
- if (i == -1) {
- i = menu?.indexOfItem(withTarget: self, andAction: NSSelectorFromString("_toggleContinuous:"))
- }
- if var data = i, data != -1 {
- data += 1
- menu?.insertItem(.separator(), at: data)
- let displayMode = self.fetchDisplayViewMode()
- if (displayMode == .twoUp || displayMode == .twoUpContinuous) {
- data += 1
- item = menu?.insertItem(withTitle: NSLocalizedString("Book Mode", comment: "Menu item title"), action: #selector(toggleDisplayAsBookFromMenu), target: self, at: data)
- }
- data += 1
- item = menu?.insertItem(withTitle: KMLocalizedString("Page Breaks"), action: #selector(toggleDisplayPageBreaksFromMenu), target: self, at: data)
- }
-
- i = menu?.indexOfItem(withTarget: self, andAction: NSSelectorFromString("zoomToPhysicalSize:"))
- if let data = i, data != -1 {
- menu?.item(at: data)?.action = #selector(doActualSize)
- item = menu?.insertItem(withTitle: KMLocalizedString("Physical Size"), action: #selector(doPhysicalSize), target: self, at: data + 1)
- item?.keyEquivalentModifierMask = [.option]
- item?.isAlternate = true
- }
-
- i = menu?.indexOfItem(withTarget: nil, andAction: NSSelectorFromString("NextPageAction:"))
- if i == NSNotFound || i == 0 {
- i = menu?.numberOfItems
- } else if let data = i {
- i = data - 1
- }
-
- if let data = i {
- _ = menu?.insertItem(withTitle: KMLocalizedString("Scroll Tool"), action: #selector(changeToolMode), target: self, at: data)
- _ = menu?.insertItem(withTitle: KMLocalizedString("Text Tool"), action: #selector(changeToolMode), target: self, tag: 1 ,at: data)
- menu?.insertItem(.separator(), at: data)
- }
-
- return menu
- }
-
- override func magnify(with event: NSEvent) {
- if KMDataManager.ud_bool(forKey: SKDisablePinchZoomKey) == false && event.responds(to: NSSelectorFromString("magnification")) {
- if event.responds(to: NSSelectorFromString("phase")) && event.phase == .began {
- self.startScale = self.scaleFactor
- }
-
- let magnifyFactor = (1.0 + fmax(-0.5, fmin(1.0 , event.magnification)))
- super.scaleFactor = magnifyFactor * self.scaleFactor
- if event.responds(to: NSSelectorFromString("phase")) && event.phase == .ended || event.phase == .cancelled && (fabs(self.startScale-self.scaleFactor) > 0.001) {
- self.setScaleFactor(fmax(self.scaleFactor, SKMinDefaultScaleMenuFactor), adjustPopup: true)
- }
- }
- }
-
- // MARK: - mark Notification handling
-
- private func _startObservingSynchronizedPDFView() {
- if self.synchronizedPDFView != nil {
- NotificationCenter.default.addObserver(self, selector: #selector(handleSynchronizedScaleChangedNotification), name: NSNotification.Name.CPDFViewScaleChanged, object: self.synchronizedPDFView)
- }
- }
-
- private func _stopObservingSynchronizedPDFView() {
- if self.synchronizedPDFView != nil {
- NotificationCenter.default.removeObserver(self, name: NSNotification.Name.CPDFViewScaleChanged, object: self.synchronizedPDFView)
- }
- }
-
- @objc func handlePageChangedNotification(_ notification: Notification) {
-
- }
-
- @objc func handleSynchronizedScaleChangedNotification(_ notification: Notification) {
- if self.document != nil && !self.document.isLocked {
- if self.synchronizeZoom {
- self.scaleFactor = self.synchronizedPDFView?.scaleFactor ?? 0.0
- }
- }
- }
-
- @objc func handleDocumentDidUnlockNotification(_ notification: Notification) {
- if self.document != nil && !self.document.isLocked {
- self.reloadPagePopUpButton()
- }
- }
-
- @objc func handlePDFViewScaleChangedNotification(_ notification: Notification) {
- if self.document != nil && !self.document.isLocked {
- if self.autoScales == false && self.synchronizeZoom == false {
- self.setScaleFactor(fmax(self.scaleFactor, SKMinDefaultScaleMenuFactor), adjustPopup: true)
- }
- }
- }
-
- func sizePopUpToItemAtIndex(_ popUpButton: NSPopUpButton?, anIndex: UInt) {
- let i = popUpButton?.indexOfSelectedItem
- popUpButton?.selectItem(at: Int(anIndex))
- popUpButton?.sizeToFit()
- popUpButton?.setFrameSize(NSMakeSize(NSWidth(popUpButton?.frame ?? .zero) - CONTROL_WIDTH_OFFSET, CONTROL_HEIGHT))
- popUpButton?.selectItem(at: i ?? Int(anIndex))
- }
-
- override var scaleFactor: CGFloat {
- get {
- return super.scaleFactor
- }
- set {
- let savedSwitching = self.switching
- self.switching = true
- if self.synchronizeZoom || savedSwitching {
- super.scaleFactor = newValue
- } else {
- self.setScaleFactor(newValue, adjustPopup: true)
- }
- self.switching = savedSwitching
- }
- }
-
- func setScaleFactor(_ newScaleFactor: CGFloat, adjustPopup flag: Bool) {
- let savedSwitching = self.switching
- self.switching = true
- if self.synchronizeZoom {
- self.setSynchronizeZoom(false, adjustPopup: false)
- }
- var _newScaleFactor = newScaleFactor
- if (flag) {
- let i = self.index(for: newScaleFactor)
- self.scalePopUpButton?.selectItem(at: Int(i))
- _newScaleFactor = SKDefaultScaleMenuFactors[Int(i)];
- }
- if self.autoScales {
- self.setAutoScales(false, adjustPopup: false)
- }
- super.scaleFactor = _newScaleFactor
- self.switching = savedSwitching
- }
-
- override var autoScales: Bool {
- get {
- return super .autoScales
- }
- set {
- let savedSwitching = self.switching
- self.switching = true
- if (savedSwitching) {
- super.autoScales = newValue
- } else {
- self.setAutoScales(newValue, adjustPopup: true)
- }
- self.switching = savedSwitching
- }
- }
-
- func setAutoScales(_ newAuto: Bool, adjustPopup flag: Bool) {
- let savedSwitching = self.switching
- self.switching = true
- if self.synchronizeZoom {
- self.setSynchronizeZoom(false, adjustPopup: false)
- }
- super.autoScales = newAuto
- if (newAuto && flag) {
- self.scalePopUpButton?.selectItem(at: 1)
- }
- self.switching = savedSwitching
- }
-
-
- func lowerIndex(for scaleFactor: CGFloat) -> UInt {
- let count = SKDefaultScaleMenuFactorsCount
- for i in 1 ..< count-1 {
- let index = count-i
- if (scaleFactor * 1.01 > SKDefaultScaleMenuFactors[index]) {
- return UInt(index)
- }
- }
- return 2
- }
-
- func upperIndex(for scaleFactor: CGFloat) -> UInt {
- let count = SKDefaultScaleMenuFactorsCount
- for i in 2 ..< count {
- if (scaleFactor * 0.99 < SKDefaultScaleMenuFactors[i]) {
- return UInt(i)
- }
- }
- return UInt(count - 1)
- }
-
- func index(for scaleFactor: CGFloat) -> UInt {
- let lower = self.lowerIndex(for: scaleFactor)
- let upper = self.upperIndex(for: scaleFactor)
- if (upper > lower && scaleFactor < 0.5 * (SKDefaultScaleMenuFactors[Int(lower)] + SKDefaultScaleMenuFactors[Int(upper)])) {
- return lower
- }
- return upper
- }
-
- override func validate(_ menuItem: NSMenuItem) -> Bool {
- let action = menuItem.action
- if action == #selector(toggleDisplayAsBookFromMenu) {
- menuItem.state = self.displaysAsBook ? .on : .off
- return true
- } else if action == #selector(toggleDisplayPageBreaksFromMenu) {
- menuItem.state = self.displaysPageBreaks ? .on : .off
- return true
- } else if action == #selector(doActualSize) {
- menuItem.state = abs(self.scaleFactor-1.0) < 0.1 ? .on : .off
- return true
- } else if action == #selector(doPhysicalSize) {
- } else if action == #selector(changeToolMode) {
- if self.selectsText {
- menuItem.state = menuItem.tag == 1 ? .on : .off
- } else {
- menuItem.state = menuItem.tag == 0 ? .on : .off
- }
- return true
- } else if let supCls = self.superclass as? NSObject.Type {
- return super.validate(menuItem)
- }
- return true
- }
- }
|