SKPresentationOptionsSheetController.swift 34 KB

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