KMTransitionInfo.swift 2.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. //
  2. // KMTransitionInfo.swift
  3. // PDF Reader Pro
  4. //
  5. // Created by liujiajie on 2024/1/17.
  6. //
  7. import Foundation
  8. let SKPasteboardTypeTransition = "net.sourceforge.skim-app.pasteboard.transition"
  9. class KMTransitionInfo: NSObject, NSPasteboardReading, NSPasteboardWriting {
  10. var transitionStyle: SKAnimationTransitionStyle = .noTransition
  11. var duration: Float = 0.0
  12. var shouldRestrict = false {
  13. didSet {
  14. // KMPrint("")
  15. }
  16. }
  17. var thumbnail: KMThumbnail?
  18. var label: String = ""
  19. var title: String {
  20. get {
  21. return NSLocalizedString("Page Transition", comment: "Box title")
  22. }
  23. }
  24. var properties: NSDictionary?{
  25. get{
  26. return [SKStyleNameKey: SKTransitionController.name(for: transitionStyle) ?? "", SKDurationKey: NSNumber(value: duration), SKShouldRestrictKey: NSNumber(value: shouldRestrict)] as [String : Any] as NSDictionary
  27. }
  28. set{
  29. if let value = newValue?[SKStyleNameKey] {
  30. transitionStyle = SKTransitionController.style(forName: value as? String)
  31. }
  32. if let value = newValue?[SKDurationKey] {
  33. duration = value as! Float
  34. }
  35. if let value = newValue?[SKShouldRestrictKey] {
  36. shouldRestrict = value as! Bool
  37. }
  38. }
  39. }
  40. override init() {
  41. super.init()
  42. self.transitionStyle = .noTransition
  43. self.duration = 1.0
  44. self.shouldRestrict = false
  45. self.thumbnail = nil
  46. self.label = ""
  47. }
  48. override var description: String {
  49. return "<\(type(of: self)) \(Unmanaged.passUnretained(self).toOpaque())> \(properties)"
  50. }
  51. static func readingOptions(forType type: NSPasteboard.PasteboardType, pasteboard: NSPasteboard) -> NSPasteboard.ReadingOptions {
  52. if type.rawValue == SKPasteboardTypeTransition {
  53. return .asPropertyList
  54. }
  55. return .asData
  56. }
  57. static func readableTypes(for pasteboard: NSPasteboard) -> [NSPasteboard.PasteboardType] {
  58. let type = NSPasteboard.PasteboardType(SKPasteboardTypeTransition)
  59. return [type]
  60. }
  61. required convenience init?(pasteboardPropertyList propertyList: Any, ofType type: NSPasteboard.PasteboardType) {
  62. self.init()
  63. if type.rawValue == SKPasteboardTypeTransition {
  64. self.properties = propertyList as? NSDictionary ?? [:]
  65. }
  66. }
  67. func writableTypes(for pasteboard: NSPasteboard) -> [NSPasteboard.PasteboardType] {
  68. let type = NSPasteboard.PasteboardType(SKPasteboardTypeTransition)
  69. return [type]
  70. }
  71. func pasteboardPropertyList(forType type: NSPasteboard.PasteboardType) -> Any? {
  72. if type.rawValue == SKPasteboardTypeTransition {
  73. return properties
  74. }
  75. return nil
  76. }
  77. }