KMSecondaryPDFView.swift 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642
  1. //
  2. // KMSecondaryPDFView.swift
  3. // PDF Reader Pro
  4. //
  5. // Created by tangchao on 2024/1/17.
  6. //
  7. import Cocoa
  8. class KMSecondaryPDFView: CPDFListView {
  9. var scalePopUpButton: NSPopUpButton?
  10. var pagePopUpButton: NSPopUpButton?
  11. var toolModeButton: NSButton?
  12. private var _controlView: NSView?
  13. var controlView: NSView? {
  14. get {
  15. return self._controlView
  16. }
  17. }
  18. private var _synchronizedPDFView: CPDFView?
  19. var synchronizedPDFView: CPDFView? {
  20. get {
  21. return self._synchronizedPDFView
  22. }
  23. set {
  24. if self._synchronizedPDFView != newValue {
  25. if self.synchronizeZoom {
  26. self._stopObservingSynchronizedPDFView()
  27. }
  28. self._synchronizedPDFView = newValue
  29. if self.synchronizeZoom {
  30. self._startObservingSynchronizedPDFView()
  31. }
  32. }
  33. }
  34. }
  35. var synchronizeZoom = false
  36. var switching = false
  37. private var _selectsText = false
  38. var selectsText: Bool {
  39. get {
  40. return self._selectsText
  41. }
  42. set {
  43. if self._selectsText != newValue {
  44. self._selectsText = newValue
  45. if newValue == false {
  46. self.currentSelection = CPDFSelection(document: self.document)
  47. }
  48. self.toolModeButton?.state = self.selectsText ? .on : .off
  49. KMDataManager.ud_set(newValue, forKey: SKLastSecondarySelectsTextKey)
  50. }
  51. if self.selectsText {
  52. self.toolMode = .textToolMode
  53. } else {
  54. self.toolMode = .moveToolMode
  55. }
  56. }
  57. }
  58. var startScale: CGFloat = 0.0
  59. 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%"]
  60. 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]
  61. private var SKMinDefaultScaleMenuFactor = 0.1
  62. private var SKDefaultScaleMenuFactorsCount = 24
  63. private var CONTROL_FONT_SIZE = 10.0
  64. private var CONTROL_HEIGHT = 15.0
  65. private var CONTROL_WIDTH_OFFSET = 20.0
  66. override func draw(_ dirtyRect: NSRect) {
  67. super.draw(dirtyRect)
  68. // Drawing code here.
  69. }
  70. deinit {
  71. KMPrint("KMSecondaryPDFView deinit.")
  72. NotificationCenter.default.removeObserver(self)
  73. }
  74. override init(frame frameRect: NSRect) {
  75. super.init(frame: frameRect)
  76. self.commonInitialization()
  77. self._initControlView()
  78. }
  79. required init?(coder: NSCoder) {
  80. super.init(coder: coder)
  81. self.commonInitialization()
  82. self._initControlView()
  83. }
  84. func commonInitialization() {
  85. self.scalePopUpButton = nil
  86. self.pagePopUpButton = nil
  87. self.synchronizedPDFView = nil
  88. self.synchronizeZoom = false
  89. self.selectsText = KMDataManager.ud_bool(forKey: SKLastSecondarySelectsTextKey)
  90. NotificationCenter.default.addObserver(self, selector: #selector(handlePageChangedNotification), name: NSNotification.Name.CPDFViewPageChanged, object: self)
  91. NotificationCenter.default.addObserver(self, selector: #selector(themeChanged), name: NSApplication.interfaceThemeChangedNotification, object: nil)
  92. }
  93. private func _initControlView() {
  94. if (self.scalePopUpButton == nil) {
  95. // create it
  96. self.scalePopUpButton = NSPopUpButton(frame: NSMakeRect(0.0, 0.0, 1.0, 1.0), pullsDown: false)
  97. self.scalePopUpButton?.cell?.controlSize = .small
  98. self.scalePopUpButton?.isBordered = false
  99. self.scalePopUpButton?.isEnabled = true
  100. self.scalePopUpButton?.refusesFirstResponder = true
  101. (self.scalePopUpButton?.cell as? NSPopUpButtonCell)?.usesItemFromMenu = true
  102. // set a suitable font, the control size is 0, 1 or 2
  103. self.scalePopUpButton?.font = .toolTipsFont(ofSize: CONTROL_FONT_SIZE)
  104. var cnt = 0
  105. let numberOfDefaultItems = SKDefaultScaleMenuFactorsCount
  106. var curItem: NSMenuItem?
  107. var label: String?
  108. var width: CGFloat = 0
  109. var maxWidth: CGFloat = 0
  110. var size = NSMakeSize(1000.0, 1000.0)
  111. let attrs = [NSAttributedString.Key.font : self.scalePopUpButton?.font ?? .toolTipsFont(ofSize: CONTROL_FONT_SIZE)]
  112. var maxIndex = 0
  113. for i in 0 ..< numberOfDefaultItems {
  114. cnt = i
  115. label = Bundle.main.localizedString(forKey: SKDefaultScaleMenuLabels[i], value: "", table: "")
  116. width = NSWidth(label!.boundingRect(with: size, options: NSString.DrawingOptions(rawValue: 0), attributes: attrs))
  117. if (width > maxWidth) {
  118. maxWidth = width
  119. maxIndex = cnt
  120. }
  121. self.scalePopUpButton?.addItem(withTitle: label ?? "")
  122. curItem = self.scalePopUpButton?.item(at: cnt)
  123. curItem?.representedObject = SKDefaultScaleMenuFactors[cnt] > 0.0 ? NSNumber(value: SKDefaultScaleMenuFactors[cnt]) : nil
  124. }
  125. if self.synchronizeZoom {
  126. self.setSynchronizeZoom(true, adjustPopup: true)
  127. } else if self.autoScales {
  128. self.setScaleFactor(0.0, adjustPopup: true)
  129. } else {
  130. self.setScaleFactor(self.scaleFactor, adjustPopup: true)
  131. }
  132. self.sizePopUpToItemAtIndex(self.scalePopUpButton, anIndex: UInt(maxIndex))
  133. self.scalePopUpButton?.refusesFirstResponder = true
  134. self.scalePopUpButton?.target = self
  135. self.scalePopUpButton?.action = #selector(scalePopUpAction)
  136. }
  137. if (self.pagePopUpButton == nil) {
  138. self.pagePopUpButton = NSPopUpButton(frame: NSMakeRect(0.0, 0.0, 1.0, 1.0), pullsDown: false)
  139. self.pagePopUpButton?.cell?.controlSize = .small
  140. self.pagePopUpButton?.isBordered = false
  141. self.pagePopUpButton?.isEnabled = true
  142. self.pagePopUpButton?.refusesFirstResponder = true
  143. (self.pagePopUpButton?.cell as? NSPopUpButtonCell)?.usesItemFromMenu = true
  144. self.pagePopUpButton?.font = .toolTipsFont(ofSize: CONTROL_FONT_SIZE)
  145. self.reloadPagePopUpButton()
  146. self.pagePopUpButton?.refusesFirstResponder = true
  147. self.pagePopUpButton?.target = self
  148. self.pagePopUpButton?.action = #selector(pagePopUpAction)
  149. }
  150. if (self.toolModeButton == nil) {
  151. self.toolModeButton = NSButton(frame: NSMakeRect(0.0, 0.0, 17.0, 15.0))
  152. self.toolModeButton?.setButtonType(.onOff)
  153. self.toolModeButton?.bezelStyle = .texturedSquare
  154. self.toolModeButton?.image = NSImage(named: "TextToolAdorn")
  155. self.toolModeButton?.state = self.selectsText ? .on : .off
  156. self.toolModeButton?.refusesFirstResponder = true
  157. self.toolModeButton?.target = self
  158. self.toolModeButton?.action = #selector(toolModeButtonAction)
  159. }
  160. if (self._controlView == nil) {
  161. var toolRect = self.toolModeButton?.frame ?? .zero
  162. var pageRect = self.pagePopUpButton?.frame ?? .zero
  163. var scaleRect = self.scalePopUpButton?.frame ?? .zero
  164. var tmpRect: NSRect = .zero
  165. self._controlView = NSView(frame: NSMakeRect(0.0, 0.0, NSWidth(toolRect) + NSWidth(pageRect) + NSWidth(scaleRect), CONTROL_HEIGHT))
  166. self.toolModeButton?.autoresizingMask = [.maxXMargin, .maxYMargin]
  167. self.pagePopUpButton?.autoresizingMask = [.maxXMargin, .maxYMargin]
  168. self.scalePopUpButton?.autoresizingMask = [.minXMargin, .maxYMargin]
  169. NSDivideRect(self.controlView?.bounds ?? .zero, &toolRect, &tmpRect, NSWidth(toolRect), .minX)
  170. NSDivideRect(tmpRect, &pageRect, &scaleRect, NSWidth(pageRect), .minX)
  171. self.toolModeButton?.frame = toolRect
  172. self.pagePopUpButton?.frame = pageRect
  173. self.scalePopUpButton?.frame = scaleRect
  174. self.controlView?.addSubview(self.toolModeButton!)
  175. self.controlView?.addSubview(self.pagePopUpButton!)
  176. self.controlView?.addSubview(self.scalePopUpButton!)
  177. self.controlView?.wantsLayer = true
  178. }
  179. }
  180. func setSynchronizeZoom(_ newSync: Bool, adjustPopup flag: Bool = true) {
  181. if self.synchronizeZoom != newSync {
  182. var savedSwitching = self.switching
  183. self.switching = true
  184. self.synchronizeZoom = newSync
  185. if (newSync) {
  186. if self.autoScales {
  187. super.autoScales = false
  188. }
  189. super.scaleFactor = self.synchronizedPDFView != nil ? self.synchronizedPDFView!.scaleFactor : 1.0
  190. self._startObservingSynchronizedPDFView()
  191. if (flag) {
  192. self.scalePopUpButton?.selectItem(at: 0)
  193. }
  194. } else {
  195. self._stopObservingSynchronizedPDFView()
  196. self.setScaleFactor(self.scaleFactor, adjustPopup: flag)
  197. }
  198. self.switching = savedSwitching
  199. }
  200. }
  201. @objc func themeChanged(_ notification: Notification) {
  202. DispatchQueue.main.asyncAfter(deadline: .now() + 0.3) {
  203. self.updateViewColor()
  204. }
  205. }
  206. override var document: CPDFDocument! {
  207. get {
  208. return super.document
  209. }
  210. set {
  211. if super.document != nil {
  212. NotificationCenter.default.removeObserver(self, name: NSNotification.Name.CPDFDocumentDidUnlock, object: self.document)
  213. }
  214. super.document = newValue
  215. self.reloadPagePopUpButton()
  216. if newValue != nil && newValue.isLocked {
  217. NotificationCenter.default.addObserver(self, selector: #selector(handleDocumentDidUnlockNotification), name: NSNotification.Name.CPDFDocumentDidUnlock, object: newValue)
  218. }
  219. }
  220. }
  221. func updateViewColor() {
  222. self.controlView?.wantsLayer = true
  223. if KMAppearance.isDarkMode() {
  224. self.controlView?.layer?.backgroundColor = NSColor(red: 37/255.0, green: 37/255.0, blue: 38/255.0, alpha: 1).cgColor
  225. } else {
  226. self.controlView?.layer?.backgroundColor = NSColor(red: 250/255.0, green: 250/255.0, blue: 250/255.0, alpha: 1).cgColor
  227. }
  228. self.controlView?.layer?.backgroundColor = KMAppearance.Layout.l0Color().cgColor
  229. }
  230. // MARK: - Popup buttons
  231. func reloadPagePopUpButton() {
  232. let labels = self.document?.pageLabels() ?? []
  233. var count = self.pagePopUpButton?.numberOfItems ?? 0
  234. var size = NSMakeSize(1000, 1000)
  235. var attrs = [NSAttributedString.Key.font : self.pagePopUpButton?.font ?? NSFont.toolTipsFont(ofSize: CONTROL_FONT_SIZE)]
  236. var maxWidth = 0.0
  237. var maxIndex = 0
  238. while count > 0 {
  239. count -= 1
  240. self.pagePopUpButton?.removeItem(at: count)
  241. }
  242. if labels.count > 0 {
  243. for (i, label) in labels.enumerated() {
  244. var width = NSWidth(label.boundingRect(with: size, options: NSString.DrawingOptions(rawValue: 0), attributes: attrs))
  245. if (width > maxWidth) {
  246. maxWidth = width;
  247. maxIndex = i
  248. }
  249. self.pagePopUpButton?.addItem(withTitle: label)
  250. }
  251. self.sizePopUpToItemAtIndex(self.pagePopUpButton, anIndex: UInt(maxIndex))
  252. self.pagePopUpButton?.selectItem(at: self.currentPageIndex)
  253. if (self.controlView != nil) {
  254. let w = NSWidth(self.toolModeButton?.frame ?? .zero) + NSWidth(self.pagePopUpButton?.frame ?? .zero) + NSWidth(self.scalePopUpButton?.frame ?? .zero)
  255. let h = NSHeight(self.controlView?.frame ?? .zero)
  256. self.controlView?.setFrameSize(NSMakeSize(w, h))
  257. }
  258. }
  259. }
  260. @objc func scalePopUpAction(_ sender: NSPopUpButton) {
  261. let selectedFactorObject = sender.selectedItem?.representedObject
  262. if (selectedFactorObject != nil) {
  263. self.setScaleFactor(selectedFactorObject as! CGFloat, adjustPopup: false)
  264. } else if (sender.indexOfSelectedItem == 0) {
  265. self.setSynchronizeZoom(true, adjustPopup: false)
  266. } else {
  267. self.setAutoScales(true, adjustPopup: false)
  268. }
  269. }
  270. @objc func pagePopUpAction(_ sender: NSPopUpButton) {
  271. self.go(toPageIndex: sender.indexOfSelectedItem, animated: false)
  272. }
  273. @objc func toolModeButtonAction(_ sender: NSButton) {
  274. self.selectsText = sender.state == .on
  275. }
  276. override var canZoomIn: Bool {
  277. if super.canZoomIn == false {
  278. return false
  279. }
  280. let numberOfDefaultItems = SKDefaultScaleMenuFactorsCount
  281. let i = self.lowerIndex(for: self.scaleFactor)
  282. return i < numberOfDefaultItems - 1
  283. }
  284. override func zoomIn(_ sender: Any!) {
  285. let numberOfDefaultItems = SKDefaultScaleMenuFactorsCount
  286. var i = self.lowerIndex(for: self.scaleFactor)
  287. if i < numberOfDefaultItems-1 {
  288. i += 1
  289. }
  290. self.setScaleFactor(SKDefaultScaleMenuFactors[Int(i)], adjustPopup: true)
  291. }
  292. override var canZoomOut: Bool {
  293. if super.canZoomOut == false {
  294. return false
  295. }
  296. let i = self.upperIndex(for: self.scaleFactor)
  297. return i > 2
  298. }
  299. override func zoomOut(_ sender: Any!) {
  300. var i = self.upperIndex(for: self.scaleFactor)
  301. if i > 2 {
  302. i -= 1
  303. }
  304. self.setScaleFactor(SKDefaultScaleMenuFactors[Int(i)], adjustPopup: true)
  305. }
  306. override func performKeyEquivalent(with event: NSEvent) -> Bool {
  307. return false
  308. }
  309. @IBAction func toggleDisplayAsBookFromMenu(_ sender: AnyObject?) {
  310. self.displaysAsBook = !self.displaysAsBook
  311. if self.displaysAsBook {
  312. self.displayTwoUp = true
  313. self.displayDirection = .horizontal
  314. }
  315. self.layoutDocumentView()
  316. }
  317. @IBAction func toggleDisplayPageBreaksFromMenu(_ sender: AnyObject?) {
  318. self.displaysPageBreaks = !self.displaysPageBreaks
  319. self.layoutDocumentView()
  320. }
  321. @objc func doActualSize(_ sender: AnyObject?) {
  322. self.scaleFactor = 1.0
  323. }
  324. @objc func doPhysicalSize(_ sender: AnyObject?) {
  325. }
  326. @objc func changeToolMode(_ sender: NSMenuItem?) {
  327. self.selectsText = ((sender?.tag ?? 0) == 1)
  328. }
  329. override func menu(for event: NSEvent) -> NSMenu? {
  330. var selectionActions = ["copy:", "_searchInSpotlight:", "_searchInGoogle:", "_searchInDictionary:", "_revealSelection:"]
  331. var menu = super.menu(for: event)
  332. var item: NSMenuItem?
  333. if self.selectsText == false {
  334. self.currentSelection = CPDFSelection(document: self.document)
  335. while let cnt = menu?.numberOfItems, cnt > 0 {
  336. item = menu?.item(at: 0)
  337. if item!.isSeparatorItem || selectionActions.contains(NSStringFromSelector(item!.action!)) {
  338. menu?.removeItem(at: 0)
  339. } else {
  340. break
  341. }
  342. }
  343. }
  344. if let idx = menu?.indexOfItem(withTarget: nil, andAction: NSSelectorFromString("menuItemClick_Paste:")) { // 移除 粘贴菜单
  345. let num = menu?.numberOfItems ?? 0
  346. if idx >= 0 && idx < num {
  347. menu?.removeItem(at: idx)
  348. }
  349. }
  350. var i = menu?.indexOfItem(withTarget: nil, andAction: NSSelectorFromString("menuItemClick_TwoPagesContinuous:"))
  351. if (i == -1) {
  352. i = menu?.indexOfItem(withTarget: self, andAction: NSSelectorFromString("_toggleContinuous:"))
  353. }
  354. if var data = i, data != -1 {
  355. data += 1
  356. menu?.insertItem(.separator(), at: data)
  357. let displayMode = self.fetchDisplayViewMode()
  358. if (displayMode == .twoUp || displayMode == .twoUpContinuous) {
  359. data += 1
  360. item = menu?.insertItem(withTitle: NSLocalizedString("Book Mode", comment: "Menu item title"), action: #selector(toggleDisplayAsBookFromMenu), target: self, at: data)
  361. }
  362. data += 1
  363. item = menu?.insertItem(withTitle: KMLocalizedString("Page Breaks"), action: #selector(toggleDisplayPageBreaksFromMenu), target: self, at: data)
  364. }
  365. i = menu?.indexOfItem(withTarget: self, andAction: NSSelectorFromString("zoomToPhysicalSize:"))
  366. if let data = i, data != -1 {
  367. menu?.item(at: data)?.action = #selector(doActualSize)
  368. item = menu?.insertItem(withTitle: KMLocalizedString("Physical Size"), action: #selector(doPhysicalSize), target: self, at: data + 1)
  369. item?.keyEquivalentModifierMask = [.option]
  370. item?.isAlternate = true
  371. }
  372. i = menu?.indexOfItem(withTarget: nil, andAction: NSSelectorFromString("NextPageAction:"))
  373. if i == NSNotFound || i == 0 {
  374. i = menu?.numberOfItems
  375. } else if let data = i {
  376. i = data - 1
  377. }
  378. if let data = i {
  379. _ = menu?.insertItem(withTitle: KMLocalizedString("Scroll Tool"), action: #selector(changeToolMode), target: self, at: data)
  380. _ = menu?.insertItem(withTitle: KMLocalizedString("Text Tool"), action: #selector(changeToolMode), target: self, tag: 1 ,at: data)
  381. menu?.insertItem(.separator(), at: data)
  382. }
  383. return menu
  384. }
  385. override func magnify(with event: NSEvent) {
  386. if KMDataManager.ud_bool(forKey: SKDisablePinchZoomKey) == false && event.responds(to: NSSelectorFromString("magnification")) {
  387. if event.responds(to: NSSelectorFromString("phase")) && event.phase == .began {
  388. self.startScale = self.scaleFactor
  389. }
  390. let magnifyFactor = (1.0 + fmax(-0.5, fmin(1.0 , event.magnification)))
  391. super.scaleFactor = magnifyFactor * self.scaleFactor
  392. if event.responds(to: NSSelectorFromString("phase")) && event.phase == .ended || event.phase == .cancelled && (fabs(self.startScale-self.scaleFactor) > 0.001) {
  393. self.setScaleFactor(fmax(self.scaleFactor, SKMinDefaultScaleMenuFactor), adjustPopup: true)
  394. }
  395. }
  396. }
  397. // MARK: - mark Notification handling
  398. private func _startObservingSynchronizedPDFView() {
  399. if self.synchronizedPDFView != nil {
  400. NotificationCenter.default.addObserver(self, selector: #selector(handleSynchronizedScaleChangedNotification), name: NSNotification.Name.CPDFViewScaleChanged, object: self.synchronizedPDFView)
  401. }
  402. }
  403. private func _stopObservingSynchronizedPDFView() {
  404. if self.synchronizedPDFView != nil {
  405. NotificationCenter.default.removeObserver(self, name: NSNotification.Name.CPDFViewScaleChanged, object: self.synchronizedPDFView)
  406. }
  407. }
  408. @objc func handlePageChangedNotification(_ notification: Notification) {
  409. }
  410. @objc func handleSynchronizedScaleChangedNotification(_ notification: Notification) {
  411. if self.document != nil && !self.document.isLocked {
  412. if self.synchronizeZoom {
  413. self.scaleFactor = self.synchronizedPDFView?.scaleFactor ?? 0.0
  414. }
  415. }
  416. }
  417. @objc func handleDocumentDidUnlockNotification(_ notification: Notification) {
  418. if self.document != nil && !self.document.isLocked {
  419. self.reloadPagePopUpButton()
  420. }
  421. }
  422. @objc func handlePDFViewScaleChangedNotification(_ notification: Notification) {
  423. if self.document != nil && !self.document.isLocked {
  424. if self.autoScales == false && self.synchronizeZoom == false {
  425. self.setScaleFactor(fmax(self.scaleFactor, SKMinDefaultScaleMenuFactor), adjustPopup: true)
  426. }
  427. }
  428. }
  429. func sizePopUpToItemAtIndex(_ popUpButton: NSPopUpButton?, anIndex: UInt) {
  430. let i = popUpButton?.indexOfSelectedItem
  431. popUpButton?.selectItem(at: Int(anIndex))
  432. popUpButton?.sizeToFit()
  433. popUpButton?.setFrameSize(NSMakeSize(NSWidth(popUpButton?.frame ?? .zero) - CONTROL_WIDTH_OFFSET, CONTROL_HEIGHT))
  434. popUpButton?.selectItem(at: i ?? Int(anIndex))
  435. }
  436. override var scaleFactor: CGFloat {
  437. get {
  438. return super.scaleFactor
  439. }
  440. set {
  441. let savedSwitching = self.switching
  442. self.switching = true
  443. if self.synchronizeZoom || savedSwitching {
  444. super.scaleFactor = newValue
  445. } else {
  446. self.setScaleFactor(newValue, adjustPopup: true)
  447. }
  448. self.switching = savedSwitching
  449. }
  450. }
  451. func setScaleFactor(_ newScaleFactor: CGFloat, adjustPopup flag: Bool) {
  452. let savedSwitching = self.switching
  453. self.switching = true
  454. if self.synchronizeZoom {
  455. self.setSynchronizeZoom(false, adjustPopup: false)
  456. }
  457. var _newScaleFactor = newScaleFactor
  458. if (flag) {
  459. let i = self.index(for: newScaleFactor)
  460. self.scalePopUpButton?.selectItem(at: Int(i))
  461. _newScaleFactor = SKDefaultScaleMenuFactors[Int(i)];
  462. }
  463. if self.autoScales {
  464. self.setAutoScales(false, adjustPopup: false)
  465. }
  466. super.scaleFactor = _newScaleFactor
  467. self.switching = savedSwitching
  468. }
  469. override var autoScales: Bool {
  470. get {
  471. return super .autoScales
  472. }
  473. set {
  474. let savedSwitching = self.switching
  475. self.switching = true
  476. if (savedSwitching) {
  477. super.autoScales = newValue
  478. } else {
  479. self.setAutoScales(newValue, adjustPopup: true)
  480. }
  481. self.switching = savedSwitching
  482. }
  483. }
  484. func setAutoScales(_ newAuto: Bool, adjustPopup flag: Bool) {
  485. let savedSwitching = self.switching
  486. self.switching = true
  487. if self.synchronizeZoom {
  488. self.setSynchronizeZoom(false, adjustPopup: false)
  489. }
  490. super.autoScales = newAuto
  491. if (newAuto && flag) {
  492. self.scalePopUpButton?.selectItem(at: 1)
  493. }
  494. self.switching = savedSwitching
  495. }
  496. func lowerIndex(for scaleFactor: CGFloat) -> UInt {
  497. let count = SKDefaultScaleMenuFactorsCount
  498. for i in 1 ..< count-1 {
  499. let index = count-i
  500. if (scaleFactor * 1.01 > SKDefaultScaleMenuFactors[index]) {
  501. return UInt(index)
  502. }
  503. }
  504. return 2
  505. }
  506. func upperIndex(for scaleFactor: CGFloat) -> UInt {
  507. let count = SKDefaultScaleMenuFactorsCount
  508. for i in 2 ..< count {
  509. if (scaleFactor * 0.99 < SKDefaultScaleMenuFactors[i]) {
  510. return UInt(i)
  511. }
  512. }
  513. return UInt(count - 1)
  514. }
  515. func index(for scaleFactor: CGFloat) -> UInt {
  516. let lower = self.lowerIndex(for: scaleFactor)
  517. let upper = self.upperIndex(for: scaleFactor)
  518. if (upper > lower && scaleFactor < 0.5 * (SKDefaultScaleMenuFactors[Int(lower)] + SKDefaultScaleMenuFactors[Int(upper)])) {
  519. return lower
  520. }
  521. return upper
  522. }
  523. override func validate(_ menuItem: NSMenuItem) -> Bool {
  524. let action = menuItem.action
  525. if action == #selector(toggleDisplayAsBookFromMenu) {
  526. menuItem.state = self.displaysAsBook ? .on : .off
  527. return true
  528. } else if action == #selector(toggleDisplayPageBreaksFromMenu) {
  529. menuItem.state = self.displaysPageBreaks ? .on : .off
  530. return true
  531. } else if action == #selector(doActualSize) {
  532. menuItem.state = abs(self.scaleFactor-1.0) < 0.1 ? .on : .off
  533. return true
  534. } else if action == #selector(doPhysicalSize) {
  535. } else if action == #selector(changeToolMode) {
  536. if self.selectsText {
  537. menuItem.state = menuItem.tag == 1 ? .on : .off
  538. } else {
  539. menuItem.state = menuItem.tag == 0 ? .on : .off
  540. }
  541. return true
  542. } else if let supCls = self.superclass as? NSObject.Type {
  543. return super.validate(menuItem)
  544. }
  545. return true
  546. }
  547. }