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(calibratedRed: 0.839216, green: 0.866667, blue: 0.898039, alpha: 1)
  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. }
  421. }
  422. @IBAction func changeDurationSlider(_ sender: NSSlider) {
  423. // self.durationTF.stringValue = sender.stringValue
  424. guard let infos = self.selectedInfos(), infos.isEmpty == false else {
  425. NSSound.beep()
  426. return
  427. }
  428. for info in infos {
  429. if info.duration != sender.floatValue { // undo
  430. self.undo_sliderAction(info: info, key: KMDurationName, value: sender.floatValue, slider: nil, textF: self.durationTF)
  431. }
  432. }
  433. // 更新数据
  434. // self._updateInfo(key: KMDurationName, value: sender.floatValue)
  435. }
  436. @objc func undo_sliderAction(info: KMTransitionInfo, key: String, value: Any, slider: NSSlider?, textF: NSTextField?) {
  437. if key == KMDurationName {
  438. let oldValue = info.duration
  439. (self.undoRedoManager.prepare(withInvocationTarget: self) as AnyObject).undo_sliderAction(info: info, key: key, value: oldValue, slider: self.durationSlider, textF: self.durationTF)
  440. let duration = value as? Float ?? 0
  441. slider?.floatValue = duration
  442. textF?.floatValue = duration
  443. }
  444. }
  445. @IBAction func cancelAction(_ sender: Any) {
  446. if let handle = self.closeCallBack {
  447. handle(self)
  448. }
  449. }
  450. @IBAction func okAction(_ sender: Any) {
  451. if let handle = self.closeCallBack {
  452. handle(self)
  453. }
  454. if self.undoRedoManager.canUndo {
  455. self.transitionController?.transitionStyle = transition.transitionStyle
  456. self.transitionController?.duration = CGFloat(transition.duration)
  457. self.transitionController?.shouldRestrict = transition.shouldRestrict
  458. self.transitionController?.pageTransitions = pageTransitions as? [Any]
  459. (self.controller?.undoManager)?.setActionName(NSLocalizedString("Change Transitions", comment: "Undo action name"))
  460. }
  461. // self.controller?.myDocument/*presentationNotesDocument*/ = notesDocument
  462. }
  463. // Noti Actions
  464. @objc private func handleDocumentsDidChangeNotification(notification: Notification?) {
  465. let currentDoc = self.notesDocumentPopUpButton.selectedItem?.representedObject as? NSDocument
  466. while self.notesDocumentPopUpButton.numberOfItems > 1 {
  467. self.notesDocumentPopUpButton.removeItem(at: self.notesDocumentPopUpButton.numberOfItems - 1)
  468. }
  469. // var doc: NSDocument?
  470. let document = self.controller?.myDocument
  471. let documents = NSMutableArray()
  472. let pageCount = self.controller?.document?.pageCount ?? 0
  473. for doc in NSDocumentController.shared.documents {
  474. guard let mainDoc = doc as? KMMainDocument else {
  475. continue
  476. }
  477. if mainDoc.isHome {
  478. continue
  479. }
  480. if mainDoc.isEqual(to: document) {
  481. continue
  482. }
  483. if let pdfDoc = mainDoc.mainViewController?.document {
  484. if pdfDoc.pageCount == pageCount {
  485. documents.add(doc)
  486. }
  487. }
  488. }
  489. let sortDescriptor = NSSortDescriptor(key: "displayName", ascending: true)
  490. documents.sort(using: [sortDescriptor])
  491. for doc in documents {
  492. self.notesDocumentPopUpButton.addItem(withTitle: (doc as AnyObject).displayName)
  493. self.notesDocumentPopUpButton.lastItem?.representedObject = doc
  494. }
  495. let docIndex = self.notesDocumentPopUpButton.indexOfItem(withRepresentedObject: currentDoc)
  496. self.notesDocumentPopUpButton.selectItem(at: docIndex == -1 ? 0 : docIndex)
  497. }
  498. }
  499. // MARK: - NSTextFieldDelegate
  500. extension SKPresentationOptionsSheetController: NSTextFieldDelegate {
  501. func controlTextDidChange(_ obj: Notification) {
  502. if self.durationTF.isEqual(to: obj.object) {
  503. // self.durationSlider.floatValue = self.durationTF.floatValue
  504. // 更新数据
  505. // self._updateInfo(key: KMDurationName, value: self.durationTF.floatValue)
  506. }
  507. }
  508. func controlTextDidEndEditing(_ obj: Notification) {
  509. if self.durationTF.isEqual(to: obj.object) {
  510. if checkString(str: self.durationTF.stringValue) {
  511. self.durationSlider.floatValue = self.durationTF.floatValue
  512. // 更新数据
  513. self._updateInfo(key: KMDurationName, value: self.durationTF.floatValue)
  514. }else{
  515. self.durationTF.stringValue = "1.0"
  516. }
  517. }
  518. }
  519. // func controlTextDidBeginEditing(_ obj: Notification) {
  520. // if self.durationTF.isEqual(to: obj.object) {
  521. //
  522. // }
  523. // }
  524. func checkString(str: String) -> Bool {
  525. let reg = "^[1-9]d*.d*|0.d*[1-9]d*|0?.0+|0$"
  526. let pre = NSPredicate(format: "SELF MATCHES %@", reg)
  527. if pre.evaluate(with: str) {
  528. return true
  529. }
  530. return false
  531. }
  532. }
  533. // MARK: - NSWindowDelegate
  534. extension SKPresentationOptionsSheetController: NSWindowDelegate {
  535. func windowWillReturnUndoManager(_ window: NSWindow) -> UndoManager? {
  536. return self.undoRedoManager
  537. }
  538. }
  539. // MARK: - SKTransitionControllerDelegate
  540. extension SKPresentationOptionsSheetController: SKTransitionControllerDelegate {
  541. func transitionController(_ controller: SKTransitionController!, valueDidChanged info: [AnyHashable : Any]!) {
  542. // 消息转发
  543. self.proxyDelegate_?.transitionController(controller, valueDidChanged: info)
  544. }
  545. }
  546. // MARK: - NSTableViewDelegate, NSTableViewDataSource
  547. extension SKPresentationOptionsSheetController: NSTableViewDelegate, NSTableViewDataSource {
  548. func numberOfRows(in tableView: NSTableView) -> Int {
  549. return self.transitions?.count ?? 0
  550. }
  551. // func tableView(_ tableView: NSTableView, objectValueFor tableColumn: NSTableColumn?, row: Int) -> Any? {
  552. // let identifier = tableColumn?.identifier
  553. // let info = transitions?[row] as? KMTransitionInfo
  554. //
  555. // if let id = identifier?.rawValue, id == PAGE_COLUMNID {
  556. // let cell = tableColumn?.dataCell as? NSTextFieldCell
  557. // cell?.stringValue = info?.label ?? ""
  558. // return info?.label ?? ""
  559. // } else if let id = identifier?.rawValue, id == IMAGE_COLUMNID {
  560. // let cell = tableColumn?.dataCell as? NSImageCell
  561. // let page = self.controller?.listView.document.page(at: UInt(info?.thumbnail?.pageIndex ?? 0))
  562. // let image = page?.thumbnail(of: page?.bounds.size ?? .zero)
  563. //
  564. //// cell?.image = image
  565. // return image
  566. //// if let data = page?.PDFListViewTIFFData(for: page?.bounds ?? .zero) {
  567. //// let image = NSImage(data: data)
  568. //// cell?.image = image
  569. //// return image
  570. //// }
  571. // }
  572. // return nil
  573. // }
  574. //
  575. func tableView(_ tableView: NSTableView, viewFor tableColumn: NSTableColumn?, row: Int) -> NSView? {
  576. let identifier = tableColumn?.identifier
  577. let info = transitions?[row] as? KMTransitionInfo
  578. var cell = tableView.makeView(withIdentifier: NSUserInterfaceItemIdentifier("KMPresentTableViewCell"), owner: self)
  579. if cell == nil {
  580. cell = KMPresentTableViewCell(type: .text)
  581. }
  582. let myCellView: KMPresentTableViewCell = cell! as! KMPresentTableViewCell
  583. // if let id = identifier?.rawValue, id == PAGE_COLUMNID {
  584. // let cellView = KMPresentTableViewCell(type: .text)
  585. myCellView.label?.stringValue = info?.label ?? ""
  586. // return cellView
  587. // } else if let id = identifier?.rawValue, id == IMAGE_COLUMNID {
  588. // let cellView = KMPresentTableViewCell(type: .img)
  589. let page = self.controller?.listView.document.page(at: UInt(info?.thumbnail?.pageIndex ?? 0))
  590. let image = page?.thumbnail(of: page?.bounds.size ?? .zero)
  591. myCellView.iv?.image = image
  592. // return cellView
  593. // }
  594. return myCellView;
  595. }
  596. func tableViewSelectionDidChange(_ notification: Notification) {
  597. if self.tableView.isEqual(to: notification.object) == false {
  598. return
  599. }
  600. let ris = self.tableView.selectedRowIndexes
  601. if ris.isEmpty {
  602. return
  603. }
  604. var info: KMTransitionInfo?
  605. if ris.count == 1 {
  606. info = self.transitions?[ris.first ?? 0] as? KMTransitionInfo
  607. self.pageTransitionBox.title = KMLocalizedString("Page Transition") + (info?.label ?? "")
  608. // self.effectPopUpButton.selectItem(at: Int(info?.transitionStyle.rawValue ?? 0))
  609. // let durationV = info?.duration ?? 0
  610. // self.durationSlider.animator().floatValue = durationV
  611. // self.durationTF.floatValue = durationV
  612. // let shouldRestrict = info?.shouldRestrict ?? false
  613. // if shouldRestrict { // 1
  614. // self.pageBtn.state = .on
  615. // self.screeBtn.state = .off
  616. // } else {
  617. // self.screeBtn.state = .on
  618. // self.pageBtn.state = .off
  619. // }
  620. } else {
  621. self.pageTransitionBox.title = KMLocalizedString("Page Transition")
  622. info = self.transition
  623. }
  624. self.effectPopUpButton.selectItem(at: Int(info?.transitionStyle.rawValue ?? 0))
  625. let durationV = info?.duration ?? 0
  626. self.durationSlider.animator().floatValue = durationV
  627. self.durationTF.floatValue = durationV
  628. let shouldRestrict = info?.shouldRestrict ?? false
  629. if shouldRestrict { // 1
  630. self.pageBtn.state = .on
  631. self.screeBtn.state = .off
  632. } else {
  633. self.screeBtn.state = .on
  634. self.pageBtn.state = .off
  635. }
  636. }
  637. // func tableView(_ tableView: NSTableView, writeRowsWith rowIndexes: IndexSet, to pboard: NSPasteboard) -> Bool {
  638. // if rowIndexes.count == 1 {
  639. // pboard.clearContents()
  640. // let info = KMTransitionInfo.readableTypesForPasteboard(pasteboard: NSPasteboard(name: NSPasteboard.Name.drag))
  641. // let ind: Int = rowIndexes.first ?? 0
  642. // let arr = NSArray(objects: transitions![ind])
  643. // pboard.writeObjects(arr as! [NSPasteboardWriting])
  644. // return true
  645. // } else {
  646. // return false
  647. // }
  648. // }
  649. func tableView(_ tableView: NSTableView, writeRowsWith rowIndexes: IndexSet, to pboard: NSPasteboard) -> Bool {
  650. if rowIndexes.count == 1 {
  651. if let data = transitions?[rowIndexes.first ?? 0] as? NSPasteboardWriting {
  652. pboard.clearContents()
  653. pboard.writeObjects([data])
  654. }
  655. return true
  656. } else {
  657. return false
  658. }
  659. }
  660. func tableView(_ tableView: NSTableView, validateDrop info: NSDraggingInfo, proposedRow row: Int, proposedDropOperation dropOperation: NSTableView.DropOperation) -> NSDragOperation {
  661. // if info.draggingPasteboard.canReadObject(forClasses: [KMTransitionInfo.self], options: [:]) {
  662. // if dropOperation == .above {
  663. // tableView.setDropRow(-1, dropOperation: .on)
  664. // }
  665. // return .every
  666. // }
  667. return NSDragOperation(rawValue: 0)
  668. }
  669. func tableView(_ tableView: NSTableView, acceptDrop info: NSDraggingInfo, row: Int, dropOperation: NSTableView.DropOperation) -> Bool {
  670. // let pboard = info.draggingPasteboard
  671. // if dropOperation == .on {
  672. // guard let infos = pboard.readObjects(forClasses: [KMTransitionInfo.self], options: [:]) as? [KMTransitionInfo], infos.count > 0 else {
  673. // return false
  674. // }
  675. // let propertie: NSDictionary? = infos[0].properties
  676. //
  677. // if row == -1 {
  678. // transitions?.setValue(propertie, forKey: PROPERTIES_KEY)
  679. // } else {
  680. // let transit: KMTransitionInfo = transitions?[row] as! KMTransitionInfo
  681. // transit.properties = propertie
  682. // }
  683. // return true
  684. // }
  685. return false
  686. }
  687. }
  688. extension SKPresentationOptionsSheetController: KMBotaTableViewDelegate {
  689. func tableView(_ aTableView: NSTableView, imageContextForRow rowIndex: Int) -> AnyObject? {
  690. return controller?.document?.page(at: UInt(rowIndex))
  691. }
  692. func tableView(_ aTableView: NSTableView, copyRowsWithIndexes rowIndexes: IndexSet) {
  693. if let data = transitions?[rowIndexes.first ?? 0] as? NSPasteboardWriting {
  694. let pboard = NSPasteboard.general
  695. pboard.clearContents()
  696. pboard.writeObjects([data])
  697. }
  698. }
  699. func tableView(_ aTableView: NSTableView, canCopyRowsWithIndexes rowIndexes: IndexSet) -> Bool {
  700. return false
  701. }
  702. func tableView(_ aTableView: NSTableView, pasteFromPasteboard pboard: NSPasteboard) {
  703. // guard let infos = pboard.readObjects(forClasses: [KMTransitionInfo.self], options: [:]) as? [KMTransitionInfo], infos.count > 0 else { return }
  704. // let arr = transitions?.objects(at: tableView.selectedRowIndexes) as? NSArray
  705. // arr?.setValue(infos[0].properties, forKey: PROPERTIES_KEY)
  706. }
  707. func tableView(_ aTableView: NSTableView, canPasteFromPasteboard pboard: NSPasteboard) -> Bool {
  708. // return (tableView.selectedRow != -1 && pboard.canReadObject(forClasses: [KMTransitionInfo.self], options: [:]))
  709. return false
  710. }
  711. func tableView(_ aTableView: NSTableView, typeSelectHelperSelectionStrings aTypeSelectHelper: SKTypeSelectHelper) -> NSArray {
  712. // return transitions?.value(forKeyPath: "thumbnail.label") as! NSArray
  713. return []
  714. }
  715. }