KMBookmark.swift 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226
  1. //
  2. // KMBookmark.swift
  3. // PDF Reader Pro
  4. //
  5. // Created by lizhe on 2024/2/5.
  6. //
  7. import Cocoa
  8. enum KMBookmarkType: Int {
  9. case bookmark = 0
  10. case folder
  11. case session
  12. case separator
  13. }
  14. class KMBookmark: NSObject {
  15. var properties: NSDictionary?
  16. var bookmarkType: KMBookmarkType = .bookmark
  17. var label: String = ""
  18. var icon: NSImage = NSImage()
  19. var alternateIcon: NSImage = NSImage()
  20. var fileURL: URL?
  21. var pageIndex: UInt = 0
  22. var pageNumber: NSNumber = 0
  23. var documentSetup: [String: String] = [:]//文档
  24. var parent: KMBookmark?
  25. var children: [KMBookmark] = [] //子
  26. static func bookmark(url: URL, pageIndex: UInt, label: String) -> KMBookmark {
  27. let bookmark = KMBookmark()
  28. bookmark.fileURL = url
  29. bookmark.pageIndex = pageIndex
  30. bookmark.label = label
  31. return bookmark
  32. }
  33. func initWithProperties(properties: NSDictionary) -> KMBookmark {
  34. return KMBookmark()
  35. }
  36. static func bookmark(properties: [String: Any]) -> KMBookmark {
  37. return KMBookmark()
  38. }
  39. // - (id)initWithProperties:(NSDictionary *)dictionary {
  40. // NSString *type = [dictionary objectForKey:TYPE_KEY];
  41. // if ([type isEqualToString:SEPARATOR_STRING]) {
  42. // return (id)[[SKSeparatorBookmark alloc] init];
  43. // } else if ([type isEqualToString:FOLDER_STRING] || [type isEqualToString:SESSION_STRING]) {
  44. // Class bookmarkClass = [type isEqualToString:FOLDER_STRING] ? [SKFolderBookmark class] : [SKSessionBookmark class];
  45. // NSMutableArray *newChildren = [NSMutableArray array];
  46. // SKBookmark *child;
  47. // for (NSDictionary *dict in [dictionary objectForKey:CHILDREN_KEY]) {
  48. // if ((child = [[SKBookmark alloc] initWithProperties:dict])) {
  49. // [newChildren addObject:child];
  50. // [child release];
  51. // } else
  52. // NSLog(@"Failed to read child bookmark: %@", dict);
  53. // }
  54. // return (id)[[bookmarkClass alloc] initFolderWithChildren:newChildren label:[dictionary objectForKey:LABEL_KEY]];
  55. // } else if ([dictionary objectForKey:@"windowFrame"]) {
  56. // return (id)[[SKFileBookmark alloc] initWithSetup:dictionary label:[dictionary objectForKey:LABEL_KEY]];
  57. // } else {
  58. // NSNumber *pageIndex = [dictionary objectForKey:PAGEINDEX_KEY];
  59. // return (id)[[SKFileBookmark alloc] initWithAliasData:[dictionary objectForKey:ALIASDATA_KEY] pageIndex:(pageIndex ? [pageIndex unsignedIntegerValue] : NSNotFound) label:[dictionary objectForKey:LABEL_KEY]];
  60. // }
  61. // }
  62. }
  63. //MARK: Folder
  64. class KMFolderBookmark: KMBookmark {
  65. override var bookmarkType: KMBookmarkType {
  66. get {
  67. return .folder
  68. }
  69. set {
  70. }
  71. }
  72. override func initWithProperties(properties: NSDictionary) -> KMBookmark {
  73. return KMFolderBookmark()
  74. }
  75. override var icon: NSImage {
  76. get {
  77. return NSImage(named: NSImage.folderName)!
  78. }
  79. set {
  80. }
  81. }
  82. override var alternateIcon: NSImage {
  83. get {
  84. return NSImage(named: NSImage.multipleDocumentsName)!
  85. }
  86. set {
  87. }
  88. }
  89. // - (id)initRootWithChildrenProperties:(NSArray *)childrenProperties {
  90. // NSMutableArray *aChildren = [NSMutableArray array];
  91. // SKBookmark *child;
  92. // for (NSDictionary *dict in childrenProperties) {
  93. // if ((child = [[SKBookmark alloc] initWithProperties:dict])) {
  94. // [aChildren addObject:child];
  95. // [child release];
  96. // }
  97. // }
  98. // return (id)[[SKRootBookmark alloc] initFolderWithChildren:aChildren label:NSLocalizedString(@"Bookmarks Menu", @"Menu item title")];
  99. // }
  100. }
  101. class KMRootBookmark: KMFolderBookmark {
  102. static func bookmarkRoot(childrenProperties: NSArray) -> KMRootBookmark {
  103. var bookmark = KMRootBookmark()
  104. var childs: [KMBookmark] = []
  105. for setup in childrenProperties {
  106. let bookmark = KMBookmark()
  107. bookmark.documentSetup = setup as! [String : String]
  108. childs.append(bookmark)
  109. }
  110. bookmark.children = childs
  111. bookmark.label = NSLocalizedString("Bookmarks Menu", comment: "")
  112. return bookmark
  113. }
  114. override var icon: NSImage {
  115. get {
  116. var iconImage = NSImage()
  117. let iconSize = NSSize(width: 16.0, height: 16.0)
  118. let drawingHandler: (NSRect) -> Bool = { rect in
  119. NSColor(calibratedWhite: 0.0, alpha: 0.2).set()
  120. NSBezierPath(rect: NSRect(x: 1.0, y: 1.0, width: 14.0, height: 13.0)).fill()
  121. NSGraphicsContext.saveGraphicsState()
  122. let path = NSBezierPath()
  123. path.move(to: NSPoint(x: 2.0, y: 2.0))
  124. path.line(to: NSPoint(x: 2.0, y: 15.0))
  125. path.line(to: NSPoint(x: 7.0, y: 15.0))
  126. path.line(to: NSPoint(x: 7.0, y: 13.0))
  127. path.line(to: NSPoint(x: 14.0, y: 13.0))
  128. path.line(to: NSPoint(x: 14.0, y: 2.0))
  129. path.close()
  130. NSColor.white.set()
  131. NSShadow.setShadowWith(NSColor(calibratedWhite: 0.0, alpha: 0.33333), blurRadius: 2.0, yOffset: -1.0)
  132. path.fill()
  133. NSGraphicsContext.restoreGraphicsState()
  134. NSColor(calibratedRed: 0.162, green: 0.304, blue: 0.755, alpha: 1.0).set()
  135. NSRect(x: 2.0, y: 13.0, width: 5.0, height: 2.0).fill()
  136. NSColor(calibratedRed: 0.894, green: 0.396, blue: 0.202, alpha: 1.0).set()
  137. NSRect(x: 3.0, y: 4.0, width: 1.0, height: 1.0).fill()
  138. NSRect(x: 3.0, y: 7.0, width: 1.0, height: 1.0).fill()
  139. NSRect(x: 3.0, y: 10.0, width: 1.0, height: 1.0).fill()
  140. NSColor(calibratedWhite: 0.6, alpha: 1.0).set()
  141. NSRect(x: 5.0, y: 4.0, width: 1.0, height: 1.0).fill()
  142. NSRect(x: 5.0, y: 7.0, width: 1.0, height: 1.0).fill()
  143. NSRect(x: 5.0, y: 10.0, width: 1.0, height: 1.0).fill()
  144. for i in 0..<7 {
  145. for j in 0..<3 {
  146. NSColor(calibratedWhite: 0.45 + 0.1 * CGFloat(Float(arc4random()) / Float(UInt32.max)), alpha: 1.0).set()
  147. NSRect(x: 6.0 + CGFloat(i), y: 4.0 + CGFloat(3 * j), width: 1.0, height: 1.0).fill()
  148. }
  149. }
  150. let gradient = NSGradient(starting: NSColor(calibratedWhite: 0.0, alpha: 0.1), ending: NSColor(calibratedWhite: 0.0, alpha: 0.0))
  151. gradient?.draw(in: NSRect(x: 2.0, y: 2.0, width: 12.0, height: 11.0), angle: 90.0)
  152. return true
  153. }
  154. iconImage = NSImage(size: iconSize, flipped: false, drawingHandler: drawingHandler)
  155. return iconImage
  156. }
  157. set {
  158. }
  159. }
  160. }
  161. //MARK: Session
  162. class KMSessionBookmark: KMBookmark {
  163. override var bookmarkType: KMBookmarkType {
  164. get {
  165. return .session
  166. }
  167. set {
  168. }
  169. }
  170. static func bookmarkSession(setups: NSArray, label: String) -> KMSessionBookmark {
  171. var bookmark = KMSessionBookmark()
  172. var childs: [KMBookmark] = []
  173. for setup in setups {
  174. let bookmark = KMBookmark()
  175. bookmark.documentSetup = setup as! [String : String]
  176. childs.append(bookmark)
  177. }
  178. bookmark.children = childs
  179. bookmark.label = label
  180. return bookmark
  181. }
  182. override func initWithProperties(properties: NSDictionary) -> KMBookmark {
  183. return KMSessionBookmark()
  184. }
  185. }
  186. //MARK: Separator
  187. class KMSeparatorBookmark: KMBookmark {
  188. override var bookmarkType: KMBookmarkType {
  189. get {
  190. return .separator
  191. }
  192. set {
  193. }
  194. }
  195. override func initWithProperties(properties: NSDictionary) -> KMBookmark {
  196. return KMSeparatorBookmark()
  197. }
  198. }