SKPresentationOptionsSheetController.swift 32 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758
  1. //
  2. // SKPresentationOptionsSheetController.swift
  3. // PDF Reader Pro
  4. //
  5. // Created by liujiajie on 2024/1/17.
  6. //
  7. import Cocoa
  8. typealias closePresentationControllerCallBack = (_ vc: SKPresentationOptionsSheetController) -> ()
  9. let RIGHTARROW_CHARACTER: unichar = 0x2192
  10. let PAGE_COLUMNID = "page"
  11. let IMAGE_COLUMNID = "image"
  12. let TRANSITIONSTYLE_KEY = "transitionStyle"
  13. let DURATION_KEY = "duration"
  14. let SHOULDRESTRICT_KEY = "shouldRestrict"
  15. let PROPERTIES_KEY = "properties"
  16. let CONTENTOBJECT_BINDINGNAME = "contentObject"
  17. let MAX_PAGE_COLUMN_WIDTH: CGFloat = 100.0
  18. let TABLE_OFFSET: CGFloat = 8.0
  19. //var SKTransitionPropertiesObservationContext = UnsafeMutableRawPointer(mutating: "SKTransitionPropertiesObservationContext")
  20. var SKPDFViewTransitionsObservationContext: CChar?
  21. class SKPresentationOptionsSheetController: KMBaseWindowController {
  22. @IBOutlet var notesDocumentPopUpButton: NSPopUpButton!
  23. @IBOutlet var tableView: KMBotaTableView!
  24. @IBOutlet var separateCheckButton: NSButton!
  25. @IBOutlet var boxes, transitionLabels, transitionControls, buttons: NSArray!
  26. @IBOutlet var effectPopUpButton: NSPopUpButton!
  27. @IBOutlet var effectLabel: NSTextField!
  28. @IBOutlet var durationLabel: NSTextField!
  29. @IBOutlet var durationSlider: NSSlider!
  30. @IBOutlet var durationTF: NSTextField!
  31. @IBOutlet var extentLabel: NSTextField!
  32. @IBOutlet var screeBtn: NSButtonCell!
  33. @IBOutlet var pageBtn: NSButtonCell!
  34. @IBOutlet var synchoronizBox: NSBox!
  35. @IBOutlet var cancelBtn: NSButton!
  36. @IBOutlet var okBtn: NSButton!
  37. @IBOutlet var pageTransitionBox: NSBox!
  38. var closeCallBack: closePresentationControllerCallBack?
  39. var separate = false
  40. var transition: KMTransitionInfo = KMTransitionInfo()
  41. private var transitions_: NSArray?
  42. var transitions: NSArray? {
  43. get {
  44. return self.transitions_
  45. }
  46. set{
  47. if self.transitions_ != newValue {
  48. // (undoRedoManager?.prepare(withInvocationTarget: self) as AnyObject).transitions = transitions
  49. self.transitions_ = newValue
  50. if let data = self.transitions as? [KMTransitionInfo] {
  51. stopObservingTransitions(infos: data)
  52. startObservingTransitions(data)
  53. }
  54. }
  55. }
  56. }
  57. var currentTransitions: NSArray? {
  58. get{
  59. return self.separate ? self.transitions : [transition as Any]
  60. }
  61. }
  62. var pageTransitions: NSArray? {
  63. get{
  64. if self.separate && self.transitions?.count ?? 0 > 0 {
  65. return transforArr()//[transitions?.value(forKey: PROPERTIES_KEY) as Any]
  66. }
  67. return nil
  68. }
  69. }
  70. var notesDocument: NSDocument? {
  71. get{
  72. _ = self.window
  73. return self.notesDocumentPopUpButton.selectedItem?.representedObject as? NSDocument
  74. }
  75. }
  76. var isScrolling: Bool {
  77. get{
  78. let scroller = self.tableView?.enclosingScrollView?.verticalScroller as? KMScroller
  79. return scroller?.isScrolling ?? false
  80. }
  81. }
  82. var undoRedoManager = UndoManager()
  83. weak var controller: KMMainViewController?
  84. weak var transitionController: SKTransitionController?
  85. private weak var proxyDelegate_: SKTransitionControllerDelegate?
  86. // class func keyPathsForValuesAffectingCurrentTransitions() -> Set<String> {
  87. // return Set<String>(["separate", "transitions", "transition"])
  88. // }
  89. override var windowNibName: NSNib.Name?{
  90. return "TransitionSheet"
  91. }
  92. override init(window: NSWindow?) {
  93. super.init(window: window)
  94. }
  95. required init?(coder: NSCoder) {
  96. super.init(coder: coder)
  97. }
  98. override func windowDidLoad() {
  99. super.windowDidLoad()
  100. let count: Int = SKTransitionController.transitionNames().count
  101. for i in 0..<count {
  102. self.effectPopUpButton.addItem(withTitle: SKTransitionController.localizedName(for: SKAnimationTransitionStyle(rawValue: UInt(i)) ?? .noTransition))
  103. self.effectPopUpButton.lastItem?.tag = i
  104. }
  105. self.notesDocumentPopUpButton.item(at: 0)?.title = NSLocalizedString("None", comment: "")
  106. // creatTransitionController()
  107. let transitionController = self.controller?.listView.transitionController
  108. self.proxyDelegate_ = transitionController?.delegate
  109. transitionController?.delegate = self
  110. self.transitionController = transitionController
  111. self.transition.transitionStyle = transitionController?.transitionStyle ?? .noTransition
  112. self.transition.duration = Float(transitionController?.duration ?? 0)
  113. self.transition.shouldRestrict = transitionController?.shouldRestrict ?? false
  114. self.startObservingTransitions([self.transition])
  115. self.pageTransitionBox.title = KMLocalizedString("Page Transition")
  116. self.effectLabel.stringValue = NSLocalizedString("Effect:", comment: "")
  117. self.durationLabel.stringValue = NSLocalizedString("Duration:", comment: "")
  118. self.extentLabel.stringValue = NSLocalizedString("Extent:", comment: "")
  119. self.screeBtn.title = KMLocalizedString("Screen")
  120. self.pageBtn.title = KMLocalizedString("Page")
  121. separateCheckButton.title = NSLocalizedString("Distinct page transitions", comment: "")
  122. self.separateCheckButton.sizeToFit()
  123. self.synchoronizBox.title = KMLocalizedString("Synchronized Notes Document")
  124. cancelBtn.title = NSLocalizedString("Cancel", comment: "")
  125. okBtn.title = NSLocalizedString("OK", comment: "")
  126. SKAutoSizeButtons(buttons as? [Any], true)
  127. let dw = SKAutoSizeLabelFields(transitionLabels as? [Any], transitionControls as? [Any], false)
  128. if abs(dw) > 0.0 {
  129. SKResizeWindow(self.window, dw)
  130. SKShiftAndResizeViews(self.boxes as? [Any], -dw, dw)
  131. SKShiftAndResizeView(self.separateCheckButton, -dw, 0.0)
  132. }
  133. // collapse the table
  134. self.window?.setFrame(NSInsetRect(window?.frame ?? .zero, 0.5 * (NSWidth(tableView.enclosingScrollView?.frame ?? .zero) + TABLE_OFFSET), 0.0), display: false)
  135. // self.tableView.registerForDraggedTypes(KMTransitionInfo.readableTypesForPasteboard(pasteboard: NSPasteboard(name: NSPasteboard.Name.drag)) as! [NSPasteboard.PasteboardType])
  136. self.tableView.registerForDraggedTypes(KMTransitionInfo.readableTypes(for: NSPasteboard(name: .drag)))
  137. self.tableView.delegate = self
  138. self.tableView.dataSource = self
  139. self.tableView.botaDelegate = self
  140. self.tableView.setTypeSelectHelper(SKTypeSelectHelper(matchOption: .SKFullStringMatch))
  141. self.tableView.hasImageToolTips = true
  142. self.tableView.backgroundColor = NSColor.mainSourceListBackgroundColor()
  143. if (transitionController?.pageTransitions != nil && transitionController?.pageTransitions.count ?? 0 > 0) {
  144. self.undoRedoManager.disableUndoRegistration()
  145. self.separate = true
  146. self.undoRedoManager.enableUndoRegistration()
  147. }
  148. // set the current notes document and observe changes for the popup
  149. self.handleDocumentsDidChangeNotification(notification: nil)
  150. let docIndex = self.notesDocumentPopUpButton.indexOfItem(withRepresentedObject: self.controller?.myDocument/*presentationNotesDocument*/)
  151. self.notesDocumentPopUpButton.selectItem(at: docIndex > 0 ? docIndex : 0)
  152. NotificationCenter.default.addObserver(self, selector: #selector(handleDocumentsDidChangeNotification(notification:)), name: NSNotification.Name("SKDocumentDidShowNotification"), object: nil)
  153. NotificationCenter.default.addObserver(self, selector: #selector(handleDocumentsDidChangeNotification(notification:)), name: NSNotification.Name("SKDocumentControllerDidRemoveDocumentNotification"), object: nil)
  154. }
  155. override func initDefaultValue() {
  156. super.initDefaultValue()
  157. self.effectPopUpButton.target = self
  158. self.effectPopUpButton.action = #selector(effectButtonAction)
  159. self.durationTF.formatter = NumberFormatter()
  160. self.durationTF.delegate = self
  161. self.screeBtn.target = self
  162. self.screeBtn.action = #selector(extentAction)
  163. self.pageBtn.target = self
  164. self.pageBtn.action = #selector(extentAction)
  165. }
  166. func stopObservingTransitions(infos: [KMTransitionInfo]) {
  167. // for info in infos {
  168. // info.removeObserver(self, forKeyPath: TRANSITIONSTYLE_KEY)
  169. // info.removeObserver(self, forKeyPath: DURATION_KEY)
  170. // info.removeObserver(self, forKeyPath: SHOULDRESTRICT_KEY)
  171. // }
  172. }
  173. // func creatTransitionController() {
  174. // transitionController = SKTransitionController(for: controller?.listView)//controller?.mainViewController.listView.transitionController
  175. // let options: NSKeyValueObservingOptions = [.new, .old]
  176. // transitionController?.addObserver(self, forKeyPath: "transitionStyle", options: options, context: &SKPDFViewTransitionsObservationContext)
  177. // transitionController?.addObserver(self, forKeyPath: "duration", options: options, context: &SKPDFViewTransitionsObservationContext)
  178. // transitionController?.addObserver(self, forKeyPath: "shouldRestrict", options: options, context: &SKPDFViewTransitionsObservationContext)
  179. // transitionController?.addObserver(self, forKeyPath: "pageTransitions", options: options, context: &SKPDFViewTransitionsObservationContext)
  180. // }
  181. func startObservingTransitions(_ infos: [KMTransitionInfo]) {
  182. // for info in infos {
  183. // info.addObserver(self, forKeyPath: TRANSITIONSTYLE_KEY, options: [.new, .old], context: &SKTransitionPropertiesObservationContext)
  184. // info.addObserver(self, forKeyPath: DURATION_KEY, options: [.new, .old], context: &SKTransitionPropertiesObservationContext)
  185. // info.addObserver(self, forKeyPath: SHOULDRESTRICT_KEY, options: [.new, .old], context: &SKTransitionPropertiesObservationContext)
  186. // }
  187. }
  188. func setValue(_ value: Any?, forKey key: String, ofTransition info: KMTransitionInfo) {
  189. info.setValue(value, forKey: key)
  190. }
  191. // override class func observeValue(forKeyPath keyPath: String?, of object: Any?, change: [NSKeyValueChangeKey : Any]?, context: UnsafeMutableRawPointer?) {
  192. // if context == &SKTransitionPropertiesObservationContext {
  193. // guard let info = object as? KMTransitionInfo else {
  194. // super.observeValue(forKeyPath: keyPath, of: object, change: change, context: context)
  195. // return
  196. // }
  197. // let newValue = change?[.newKey]
  198. // let oldValue = change?[.oldKey]
  199. //
  200. // let newNonNullValue = newValue as? NSNull == nil ? newValue : nil
  201. // let oldNonNullValue = oldValue as? NSNull == nil ? oldValue : nil
  202. //
  203. //// if (newNonNullValue != nil || oldNonNullValue != nil) && newNonNullValue != oldNonNullValue {
  204. //// (undoManager?.prepare(withInvocationTarget: self) as AnyObject).setValue(oldNonNullValue, forKey: keyPath, ofTransition: info)
  205. //// }
  206. // if (newNonNullValue != nil || oldNonNullValue != nil){
  207. //
  208. // }
  209. // } else {
  210. // super.observeValue(forKeyPath: keyPath, of: object, change: change, context: context)
  211. // }
  212. // }
  213. // MARK: - Private
  214. private func makeTransitions() {
  215. if transitions != nil {
  216. return
  217. }
  218. let tableColumn = self.tableView?.tableColumn(withIdentifier: NSUserInterfaceItemIdentifier(rawValue: PAGE_COLUMNID))
  219. let cell = tableColumn?.dataCell
  220. var labelWidth: CGFloat = 0.0
  221. var array = [Any]()
  222. let dictionary: NSDictionary? = self.transition.properties
  223. let arr: NSArray? = self.transitionController?.pageTransitions as NSArray?
  224. let ptEnum = arr?.objectEnumerator()
  225. var tn: KMThumbnail? = nil
  226. for next in (controller?.leftSideViewController.thumbnails) ?? [] {
  227. if tn != nil {
  228. let info = KMTransitionInfo()
  229. info.thumbnail = tn
  230. info.label = "\(tn?.label ?? "")"+"→"+"\(next.label)"
  231. info.properties = ((ptEnum?.nextObject()) != nil) ? ptEnum?.nextObject() as? NSDictionary : dictionary
  232. array.append(info)
  233. (cell as AnyObject).setStringValue(info.label)
  234. labelWidth = max(labelWidth, ceil(((cell as AnyObject).cellSize?.width) ?? 0))
  235. }
  236. tn = next
  237. }
  238. labelWidth = min(labelWidth, MAX_PAGE_COLUMN_WIDTH)
  239. tableColumn?.minWidth = labelWidth
  240. tableColumn?.maxWidth = labelWidth
  241. tableColumn?.width = labelWidth
  242. var frame: NSRect = self.tableView?.enclosingScrollView?.frame ?? .zero
  243. let wi: CGFloat = 61//tableColumn?.value(forKeyPath: "@sum.width") as! CGFloat
  244. frame.size.width = 19.0 + wi
  245. self.tableView?.enclosingScrollView?.frame = frame
  246. self.transitions = array as NSArray
  247. }
  248. @objc dynamic func changePageTransitionType(sep: Bool) {
  249. // SKImageToolTipWindow.sharedToolTipWindow().orderOut(nil)
  250. self.separate = sep
  251. self.separateCheckButton.state = sep ? .on : .off
  252. let window = self.window
  253. let isVisible = window?.isVisible ?? false
  254. var frame = window?.frame ?? NSRect.zero
  255. let scrollView = tableView?.enclosingScrollView
  256. var extraWidth: CGFloat
  257. let firstResponder = window?.firstResponder
  258. var editor: NSTextView? = nil
  259. if let textView = firstResponder as? NSTextView {
  260. editor = textView
  261. if textView.isFieldEditor {
  262. // firstResponder = textView.delegate
  263. }
  264. }
  265. if let editor = editor, window?.firstResponder != editor {
  266. window?.makeFirstResponder(firstResponder)
  267. }
  268. if sep {
  269. makeTransitions()
  270. extraWidth = (scrollView?.frame.width ?? 0) + TABLE_OFFSET
  271. frame.size.width += extraWidth
  272. frame.origin.x -= floor(0.5 * extraWidth)
  273. window?.setFrame(frame, display: isVisible, animate: isVisible)
  274. scrollView?.isHidden = false
  275. } else {
  276. scrollView?.isHidden = true
  277. extraWidth = (scrollView?.frame.width ?? 0) + TABLE_OFFSET
  278. frame.size.width -= extraWidth
  279. frame.origin.x += floor(0.5 * extraWidth)
  280. window?.setFrame(frame, display: isVisible, animate: isVisible)
  281. }
  282. (self.undoRedoManager.prepare(withInvocationTarget: self) as AnyObject).changePageTransitionType(sep: sep == false)
  283. }
  284. func selectedInfos() -> [KMTransitionInfo]? {
  285. if self.separate == false {
  286. return [self.transition]
  287. } else {
  288. guard let trans = self.transitions else {
  289. return nil
  290. }
  291. let ris = self.tableView.selectedRowIndexes
  292. var infos: [KMTransitionInfo] = []
  293. for ri in ris {
  294. if ri >= trans.count {
  295. continue
  296. }
  297. if let info = trans.object(at: ri) as? KMTransitionInfo {
  298. infos.append(info)
  299. }
  300. }
  301. return infos
  302. }
  303. }
  304. private func _updateInfo(key: String, value: Any) {
  305. if key == KMTransitionStyleName {
  306. let _value = value as? SKAnimationTransitionStyle ?? .noTransition
  307. if self.separate == false {
  308. self.transition.transitionStyle = _value
  309. } else {
  310. guard let trans = self.transitions else {
  311. return
  312. }
  313. let ris = self.tableView.selectedRowIndexes
  314. for ri in ris {
  315. if ri >= trans.count {
  316. continue
  317. }
  318. let info = trans.object(at: ri) as? KMTransitionInfo
  319. info?.transitionStyle = _value
  320. }
  321. }
  322. } else if key == KMDurationName {
  323. let _value = value as? Float ?? 0
  324. if self.separate == false {
  325. self.transition.duration = _value
  326. } else {
  327. guard let trans = self.transitions else {
  328. return
  329. }
  330. let ris = self.tableView.selectedRowIndexes
  331. for ri in ris {
  332. if ri >= trans.count {
  333. continue
  334. }
  335. let info = trans.object(at: ri) as? KMTransitionInfo
  336. info?.duration = _value
  337. }
  338. }
  339. } else if key == KMShouldRestrictName {
  340. let _value = value as? Bool ?? false
  341. if self.separate == false {
  342. self.transition.shouldRestrict = _value
  343. } else {
  344. guard let trans = self.transitions else {
  345. return
  346. }
  347. let ris = self.tableView.selectedRowIndexes
  348. for ri in ris {
  349. if ri >= trans.count {
  350. continue
  351. }
  352. let info = trans.object(at: ri) as? KMTransitionInfo
  353. info?.shouldRestrict = _value
  354. }
  355. }
  356. }
  357. }
  358. // Actions
  359. @objc func effectButtonAction(sender: NSPopUpButton) {
  360. let index = sender.indexOfSelectedItem
  361. let names = SKTransitionController.transitionNames() as? [String]
  362. let name = names?.safe_element(for: index) as? String ?? ""
  363. let type = SKTransitionController.style(forName: name)
  364. guard let infos = self.selectedInfos(), infos.isEmpty == false else {
  365. NSSound.beep()
  366. return
  367. }
  368. for info in infos {
  369. if info.transitionStyle != type { // undo
  370. // let oldValue = info.transitionStyle
  371. self.undo_effectAction(info: info, key: KMTransitionStyleName, value: type, popup: nil)
  372. }
  373. }
  374. // self._updateInfo(key: KMTransitionStyleName, value: type)
  375. }
  376. @objc dynamic func undo_effectAction(info: KMTransitionInfo, key: String, value: Any, popup: NSPopUpButton?) {
  377. if key == KMTransitionStyleName {
  378. let oldValue = info.transitionStyle
  379. (self.undoRedoManager.prepare(withInvocationTarget: self) as AnyObject).undo_effectAction(info: info, key: key, value: oldValue, popup: self.effectPopUpButton)
  380. info.transitionStyle = value as? SKAnimationTransitionStyle ?? .noTransition
  381. popup?.selectItem(at: Int(info.transitionStyle.rawValue))
  382. }
  383. }
  384. @objc func extentAction(sender: NSMatrix) {
  385. // let isScreen = sender.isEqual(to: self.screeBtn)
  386. // self.screeBtn.state = isScreen ? .on : .off
  387. // self.pageBtn.state = isScreen ? .off : .on
  388. // let value = self.pageBtn. == .on
  389. let value = sender.selectedTag() == 1 ? true : false
  390. // (self.undoRedoManager.prepare(withInvocationTarget: self) as AnyObject).extentAction(sender: sender)
  391. self.undoRedoManager.registerUndo(withTarget: self) { target in
  392. if value {
  393. target.screeBtn.performClick(nil)
  394. } else {
  395. target.pageBtn.performClick(nil)
  396. }
  397. }
  398. self._updateInfo(key: KMShouldRestrictName, value: value)
  399. }
  400. func transforArr() -> NSArray {
  401. let arr = NSMutableArray()
  402. for i in 0..<(transitions?.count ?? 0) {
  403. if let item = transitions?[i] as? KMTransitionInfo {
  404. let dic = NSMutableDictionary.init()
  405. dic.setValue(item.duration, forKey: "duration")
  406. dic.setValue(item.shouldRestrict, forKey: "shouldRestrict")
  407. dic.setValue(item.properties?[SKStyleNameKey], forKey: "styleName")
  408. arr.add(dic)
  409. }
  410. }
  411. return arr
  412. }
  413. @IBAction func pageTransitionAction(_ sender: NSButton) {
  414. self.separate = sender.state == .on
  415. self.changePageTransitionType(sep: self.separate)
  416. Task { @MainActor in
  417. self.tableView.reloadData()
  418. }
  419. }
  420. @IBAction func changeDurationSlider(_ sender: NSSlider) {
  421. // self.durationTF.stringValue = sender.stringValue
  422. guard let infos = self.selectedInfos(), infos.isEmpty == false else {
  423. NSSound.beep()
  424. return
  425. }
  426. for info in infos {
  427. if info.duration != sender.floatValue { // undo
  428. self.undo_sliderAction(info: info, key: KMDurationName, value: sender.floatValue, slider: nil, textF: self.durationTF)
  429. }
  430. }
  431. // 更新数据
  432. // self._updateInfo(key: KMDurationName, value: sender.floatValue)
  433. }
  434. @objc func undo_sliderAction(info: KMTransitionInfo, key: String, value: Any, slider: NSSlider?, textF: NSTextField?) {
  435. if key == KMDurationName {
  436. let oldValue = info.duration
  437. (self.undoRedoManager.prepare(withInvocationTarget: self) as AnyObject).undo_sliderAction(info: info, key: key, value: oldValue, slider: self.durationSlider, textF: self.durationTF)
  438. let duration = value as? Float ?? 0
  439. slider?.floatValue = duration
  440. textF?.floatValue = duration
  441. }
  442. }
  443. @IBAction func cancelAction(_ sender: Any) {
  444. if let handle = self.closeCallBack {
  445. handle(self)
  446. }
  447. }
  448. @IBAction func okAction(_ sender: Any) {
  449. if let handle = self.closeCallBack {
  450. handle(self)
  451. }
  452. if self.undoRedoManager.canUndo {
  453. self.transitionController?.transitionStyle = transition.transitionStyle
  454. self.transitionController?.duration = CGFloat(transition.duration)
  455. self.transitionController?.shouldRestrict = transition.shouldRestrict
  456. self.transitionController?.pageTransitions = pageTransitions as? [Any]
  457. (self.controller?.undoManager)?.setActionName(NSLocalizedString("Change Transitions", comment: "Undo action name"))
  458. }
  459. // self.controller?.myDocument/*presentationNotesDocument*/ = notesDocument
  460. }
  461. // Noti Actions
  462. @objc private func handleDocumentsDidChangeNotification(notification: Notification?) {
  463. let currentDoc = self.notesDocumentPopUpButton.selectedItem?.representedObject as? NSDocument
  464. while self.notesDocumentPopUpButton.numberOfItems > 1 {
  465. self.notesDocumentPopUpButton.removeItem(at: self.notesDocumentPopUpButton.numberOfItems - 1)
  466. }
  467. // var doc: NSDocument?
  468. let document = self.controller?.myDocument
  469. let documents = NSMutableArray()
  470. let pageCount = self.controller?.document?.pageCount ?? 0
  471. for doc in NSDocumentController.shared.documents {
  472. guard let mainDoc = doc as? KMMainDocument else {
  473. continue
  474. }
  475. if mainDoc.isHome {
  476. continue
  477. }
  478. if mainDoc.isEqual(to: document) {
  479. continue
  480. }
  481. if let pdfDoc = mainDoc.mainViewController?.document {
  482. if pdfDoc.pageCount == pageCount {
  483. documents.add(doc)
  484. }
  485. }
  486. }
  487. let sortDescriptor = NSSortDescriptor(key: "displayName", ascending: true)
  488. documents.sort(using: [sortDescriptor])
  489. for doc in documents {
  490. self.notesDocumentPopUpButton.addItem(withTitle: (doc as AnyObject).displayName)
  491. self.notesDocumentPopUpButton.lastItem?.representedObject = doc
  492. }
  493. let docIndex = self.notesDocumentPopUpButton.indexOfItem(withRepresentedObject: currentDoc)
  494. self.notesDocumentPopUpButton.selectItem(at: docIndex == -1 ? 0 : docIndex)
  495. }
  496. }
  497. // MARK: - NSTextFieldDelegate
  498. extension SKPresentationOptionsSheetController: NSTextFieldDelegate {
  499. func controlTextDidChange(_ obj: Notification) {
  500. if self.durationTF.isEqual(to: obj.object) {
  501. self.durationSlider.floatValue = self.durationTF.floatValue
  502. // 更新数据
  503. self._updateInfo(key: KMDurationName, value: self.durationTF.floatValue)
  504. }
  505. }
  506. }
  507. // MARK: - NSWindowDelegate
  508. extension SKPresentationOptionsSheetController: NSWindowDelegate {
  509. func windowWillReturnUndoManager(_ window: NSWindow) -> UndoManager? {
  510. return self.undoRedoManager
  511. }
  512. }
  513. // MARK: - SKTransitionControllerDelegate
  514. extension SKPresentationOptionsSheetController: SKTransitionControllerDelegate {
  515. func transitionController(_ controller: SKTransitionController!, valueDidChanged info: [AnyHashable : Any]!) {
  516. // 消息转发
  517. self.proxyDelegate_?.transitionController(controller, valueDidChanged: info)
  518. }
  519. }
  520. // MARK: - NSTableViewDelegate, NSTableViewDataSource
  521. extension SKPresentationOptionsSheetController: NSTableViewDelegate, NSTableViewDataSource {
  522. func numberOfRows(in tableView: NSTableView) -> Int {
  523. return self.transitions?.count ?? 0
  524. }
  525. func tableView(_ tableView: NSTableView, objectValueFor tableColumn: NSTableColumn?, row: Int) -> Any? {
  526. let identifier = tableColumn?.identifier
  527. let info = transitions?[row] as? KMTransitionInfo
  528. if let id = identifier?.rawValue, id == PAGE_COLUMNID {
  529. let cell = tableColumn?.dataCell as? NSTextFieldCell
  530. cell?.stringValue = info?.label ?? ""
  531. return info?.label ?? ""
  532. } else if let id = identifier?.rawValue, id == IMAGE_COLUMNID {
  533. let cell = tableColumn?.dataCell as? NSImageCell
  534. let page = self.controller?.listView.document.page(at: UInt(info?.thumbnail?.pageIndex ?? 0))
  535. let image = page?.thumbnail(of: page?.bounds.size ?? .zero)
  536. cell?.image = image
  537. return image
  538. // if let data = page?.PDFListViewTIFFData(for: page?.bounds ?? .zero) {
  539. // let image = NSImage(data: data)
  540. // cell?.image = image
  541. // return image
  542. // }
  543. }
  544. return nil
  545. }
  546. func tableViewSelectionDidChange(_ notification: Notification) {
  547. if self.tableView.isEqual(to: notification.object) == false {
  548. return
  549. }
  550. let ris = self.tableView.selectedRowIndexes
  551. if ris.isEmpty {
  552. return
  553. }
  554. var info: KMTransitionInfo?
  555. if ris.count == 1 {
  556. info = self.transitions?[ris.first ?? 0] as? KMTransitionInfo
  557. self.pageTransitionBox.title = KMLocalizedString("Page Transition") + (info?.label ?? "")
  558. // self.effectPopUpButton.selectItem(at: Int(info?.transitionStyle.rawValue ?? 0))
  559. // let durationV = info?.duration ?? 0
  560. // self.durationSlider.animator().floatValue = durationV
  561. // self.durationTF.floatValue = durationV
  562. // let shouldRestrict = info?.shouldRestrict ?? false
  563. // if shouldRestrict { // 1
  564. // self.pageBtn.state = .on
  565. // self.screeBtn.state = .off
  566. // } else {
  567. // self.screeBtn.state = .on
  568. // self.pageBtn.state = .off
  569. // }
  570. } else {
  571. self.pageTransitionBox.title = KMLocalizedString("Page Transition")
  572. info = self.transition
  573. }
  574. self.effectPopUpButton.selectItem(at: Int(info?.transitionStyle.rawValue ?? 0))
  575. let durationV = info?.duration ?? 0
  576. self.durationSlider.animator().floatValue = durationV
  577. self.durationTF.floatValue = durationV
  578. let shouldRestrict = info?.shouldRestrict ?? false
  579. if shouldRestrict { // 1
  580. self.pageBtn.state = .on
  581. self.screeBtn.state = .off
  582. } else {
  583. self.screeBtn.state = .on
  584. self.pageBtn.state = .off
  585. }
  586. }
  587. // func tableView(_ tableView: NSTableView, writeRowsWith rowIndexes: IndexSet, to pboard: NSPasteboard) -> Bool {
  588. // if rowIndexes.count == 1 {
  589. // pboard.clearContents()
  590. // let info = KMTransitionInfo.readableTypesForPasteboard(pasteboard: NSPasteboard(name: NSPasteboard.Name.drag))
  591. // let ind: Int = rowIndexes.first ?? 0
  592. // let arr = NSArray(objects: transitions![ind])
  593. // pboard.writeObjects(arr as! [NSPasteboardWriting])
  594. // return true
  595. // } else {
  596. // return false
  597. // }
  598. // }
  599. func tableView(_ tableView: NSTableView, writeRowsWith rowIndexes: IndexSet, to pboard: NSPasteboard) -> Bool {
  600. if rowIndexes.count == 1 {
  601. if let data = transitions?[rowIndexes.first ?? 0] as? NSPasteboardWriting {
  602. pboard.clearContents()
  603. pboard.writeObjects([data])
  604. }
  605. return true
  606. } else {
  607. return false
  608. }
  609. }
  610. func tableView(_ tableView: NSTableView, validateDrop info: NSDraggingInfo, proposedRow row: Int, proposedDropOperation dropOperation: NSTableView.DropOperation) -> NSDragOperation {
  611. // if info.draggingPasteboard.canReadObject(forClasses: [KMTransitionInfo.self], options: [:]) {
  612. // if dropOperation == .above {
  613. // tableView.setDropRow(-1, dropOperation: .on)
  614. // }
  615. // return .every
  616. // }
  617. return NSDragOperation(rawValue: 0)
  618. }
  619. func tableView(_ tableView: NSTableView, acceptDrop info: NSDraggingInfo, row: Int, dropOperation: NSTableView.DropOperation) -> Bool {
  620. // let pboard = info.draggingPasteboard
  621. // if dropOperation == .on {
  622. // guard let infos = pboard.readObjects(forClasses: [KMTransitionInfo.self], options: [:]) as? [KMTransitionInfo], infos.count > 0 else {
  623. // return false
  624. // }
  625. // let propertie: NSDictionary? = infos[0].properties
  626. //
  627. // if row == -1 {
  628. // transitions?.setValue(propertie, forKey: PROPERTIES_KEY)
  629. // } else {
  630. // let transit: KMTransitionInfo = transitions?[row] as! KMTransitionInfo
  631. // transit.properties = propertie
  632. // }
  633. // return true
  634. // }
  635. return false
  636. }
  637. }
  638. extension SKPresentationOptionsSheetController: KMBotaTableViewDelegate {
  639. func tableView(_ aTableView: NSTableView, imageContextForRow rowIndex: Int) -> AnyObject? {
  640. return controller?.document?.page(at: UInt(rowIndex))
  641. }
  642. func tableView(_ aTableView: NSTableView, copyRowsWithIndexes rowIndexes: IndexSet) {
  643. if let data = transitions?[rowIndexes.first ?? 0] as? NSPasteboardWriting {
  644. let pboard = NSPasteboard.general
  645. pboard.clearContents()
  646. pboard.writeObjects([data])
  647. }
  648. }
  649. func tableView(_ aTableView: NSTableView, canCopyRowsWithIndexes rowIndexes: IndexSet) -> Bool {
  650. return false
  651. }
  652. func tableView(_ aTableView: NSTableView, pasteFromPasteboard pboard: NSPasteboard) {
  653. // guard let infos = pboard.readObjects(forClasses: [KMTransitionInfo.self], options: [:]) as? [KMTransitionInfo], infos.count > 0 else { return }
  654. // let arr = transitions?.objects(at: tableView.selectedRowIndexes) as? NSArray
  655. // arr?.setValue(infos[0].properties, forKey: PROPERTIES_KEY)
  656. }
  657. func tableView(_ aTableView: NSTableView, canPasteFromPasteboard pboard: NSPasteboard) -> Bool {
  658. // return (tableView.selectedRow != -1 && pboard.canReadObject(forClasses: [KMTransitionInfo.self], options: [:]))
  659. return false
  660. }
  661. func tableView(_ aTableView: NSTableView, typeSelectHelperSelectionStrings aTypeSelectHelper: SKTypeSelectHelper) -> NSArray {
  662. // return transitions?.value(forKeyPath: "thumbnail.label") as! NSArray
  663. return []
  664. }
  665. }