KMCompareFilesConfig.swift 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241
  1. //
  2. // KMCompareFilesConfig.swift
  3. // PDF Reader Pro
  4. //
  5. // Created by lizhe on 2023/11/16.
  6. //
  7. import Cocoa
  8. let PDFCompareISCompareTextFilesKey = "PDFCompareISCompareTextFiles1"
  9. let PDFCompareISCompareImageFilesKey = "PDFCompareISCompareImageFiles1"
  10. let PDFCompareInsertColorFilesKey = "PDFCompareInsertColorFiles"
  11. let PDFCompareInsertOpacityFilesKey = "PDFCompareInsertOpacityFiles"
  12. let PDFCompareDeleteColorFilesKey = "PDFCompareDeleteColorFiles"
  13. let PDFCompareDeleteOpacityFilesKey = "PDFCompareDeleteOpacityFiles"
  14. let PDFCompareReplaceColorFilesKey = "PDFCompareReplaceColorFiles"
  15. let PDFCompareReplaceOpacityFilesKey = "PDFCompareReplaceOpacityFiles"
  16. let PDFCompareOldStrokeColorFilesKey = "PDFCompareOldStrokeColorFiles"
  17. let PDFCompareNewStrokeColorFilesKey = "PDFCompareNewStrokeColorFiles"
  18. let PDFCompareNewStrokeOpacityFilesKey = "PDFCompareNewStrokeOpacityFiles"
  19. let PDFCompareOldStrokeOpacityFilesKey = "PDFCompareOldStrokeOpacityFiles"
  20. let PDFCompareNewFillOpacityFilesKey = "PDFCompareNewFillOpacityFiles"
  21. let PDFCompareOldFillOpacityFilesKey = "PDFCompareOldFillOpacityFiles"
  22. let PDFCompareIsNOFillFilesKey = "PDFCompareIsNOFillFiles"
  23. let PDFComparePDFBlendModeFilesKey = "PDFComparePDFBlendModeFiles"
  24. class KMCompareFilesConfig {
  25. static let defaultConfig: KMCompareFilesConfig = KMCompareFilesConfig()
  26. var fileNewAttribute: KMFileAttribute = KMFileAttribute()
  27. var fileOldAttribute: KMFileAttribute = KMFileAttribute()
  28. func setColor(_ color: NSColor?, forKey key: String) {
  29. let data = color != nil ? NSArchiver.archivedData(withRootObject: color!) : NSArchiver.archivedData(withRootObject: NSColor.red)
  30. UserDefaults.standard.set(data, forKey: key)
  31. }
  32. func colorForKey(_ key: String) -> NSColor? {
  33. var color: NSColor?
  34. if let data = UserDefaults.standard.data(forKey: key) {
  35. color = NSUnarchiver.unarchiveObject(with: data) as? NSColor
  36. } else if let array = UserDefaults.standard.array(forKey: key), array.count > 0 {
  37. var red, green, blue, alpha: CGFloat
  38. red = CGFloat(array[0] as! Double)
  39. green = CGFloat(array[0] as! Double)
  40. blue = CGFloat(array[0] as! Double)
  41. if array.count > 2 {
  42. green = CGFloat(array[1] as! Double)
  43. blue = CGFloat(array[2] as! Double)
  44. }
  45. if array.count == 2 {
  46. alpha = CGFloat(array[1] as! Double)
  47. } else if array.count > 3 {
  48. alpha = CGFloat(array[3] as! Double)
  49. } else {
  50. alpha = 1.0
  51. }
  52. color = NSColor(calibratedRed: red, green: green, blue: blue, alpha: alpha)
  53. }
  54. return color
  55. }
  56. func setIsCompareText(_ isCompareText: Bool) {
  57. UserDefaults.standard.set(isCompareText, forKey: PDFCompareISCompareTextFilesKey)
  58. }
  59. func isCompareText() -> Bool {
  60. if UserDefaults.standard.object(forKey: PDFCompareISCompareTextFilesKey) != nil {
  61. return UserDefaults.standard.bool(forKey: PDFCompareISCompareTextFilesKey)
  62. } else {
  63. return true
  64. }
  65. }
  66. func setIsCompareImage(_ isCompareImage: Bool) {
  67. UserDefaults.standard.set(isCompareImage, forKey: PDFCompareISCompareImageFilesKey)
  68. }
  69. func isCompareImage() -> Bool {
  70. if UserDefaults.standard.object(forKey: PDFCompareISCompareImageFilesKey) != nil {
  71. return UserDefaults.standard.bool(forKey: PDFCompareISCompareImageFilesKey)
  72. } else {
  73. return true
  74. }
  75. }
  76. func setInsertColor(_ insertColor: NSColor?) {
  77. setColor(insertColor, forKey: PDFCompareInsertColorFilesKey)
  78. }
  79. func insertColor() -> NSColor {
  80. guard let color = colorForKey(PDFCompareInsertColorFilesKey) else {
  81. return NSColor.green
  82. }
  83. return color
  84. }
  85. func setInsertOpacity(_ insertOpacity: CGFloat) {
  86. UserDefaults.standard.set(insertOpacity, forKey: PDFCompareInsertOpacityFilesKey)
  87. }
  88. func insertOpacity() -> CGFloat {
  89. if let value = UserDefaults.standard.value(forKey: PDFCompareInsertOpacityFilesKey) as? CGFloat {
  90. return value
  91. }
  92. return 0.2
  93. }
  94. func setDeleteColor(_ deleteColor: NSColor?) {
  95. setColor(deleteColor, forKey: PDFCompareDeleteColorFilesKey)
  96. }
  97. func deleteColor() -> NSColor {
  98. guard let color = colorForKey(PDFCompareDeleteColorFilesKey) else {
  99. return NSColor.red
  100. }
  101. return color
  102. }
  103. func setDeleteOpacity(_ deleteOpacity: CGFloat) {
  104. UserDefaults.standard.set(deleteOpacity, forKey: PDFCompareDeleteOpacityFilesKey)
  105. }
  106. func deleteOpacity() -> CGFloat {
  107. if let value = UserDefaults.standard.value(forKey: PDFCompareDeleteOpacityFilesKey) as? CGFloat {
  108. return value
  109. }
  110. return 0.2
  111. }
  112. func setReplaceColor(_ replaceColor: NSColor?) {
  113. setColor(replaceColor, forKey: PDFCompareReplaceColorFilesKey)
  114. }
  115. func replaceColor() -> NSColor {
  116. guard let color = colorForKey(PDFCompareReplaceColorFilesKey) else {
  117. return NSColor.blue
  118. }
  119. return color
  120. }
  121. func setReplaceOpacity(_ replaceOpacity: CGFloat) {
  122. UserDefaults.standard.set(replaceOpacity, forKey: PDFCompareReplaceOpacityFilesKey)
  123. }
  124. func replaceOpacity() -> CGFloat {
  125. if let value = UserDefaults.standard.value(forKey: PDFCompareReplaceOpacityFilesKey) as? CGFloat {
  126. return value
  127. }
  128. return 0.2
  129. }
  130. func setOldStrokeColor(_ oldStrokeColor: NSColor?) {
  131. setColor(oldStrokeColor, forKey: PDFCompareOldStrokeColorFilesKey)
  132. }
  133. func oldStrokeColor() -> NSColor {
  134. guard let color = colorForKey(PDFCompareOldStrokeColorFilesKey) else {
  135. return NSColor(calibratedRed: 245.0/255.0, green: 40.0/255.0, blue: 27.0/255.0, alpha: 1.0)
  136. }
  137. return color
  138. }
  139. func setNewStrokeColor(_ newStrokeColor: NSColor?) {
  140. setColor(newStrokeColor, forKey: PDFCompareNewStrokeColorFilesKey)
  141. }
  142. func newStrokeColor() -> NSColor {
  143. guard let color = colorForKey(PDFCompareNewStrokeColorFilesKey) else {
  144. return NSColor(calibratedRed: 49.0/255.0, green: 193.0/255.0, blue: 255.0/255.0, alpha: 1.0)
  145. }
  146. return color
  147. }
  148. func setNewStrokeOpacity(_ newStrokeOpacity: CGFloat) {
  149. UserDefaults.standard.set(newStrokeOpacity, forKey: PDFCompareNewStrokeOpacityFilesKey)
  150. }
  151. func newStrokeOpacity() -> CGFloat {
  152. if let value = UserDefaults.standard.value(forKey: PDFCompareNewStrokeOpacityFilesKey) as? CGFloat {
  153. return value
  154. }
  155. return 1.0
  156. }
  157. func setOldStrokeOpacity(_ oldStrokeOpacity: CGFloat) {
  158. UserDefaults.standard.set(oldStrokeOpacity, forKey: PDFCompareOldStrokeOpacityFilesKey)
  159. }
  160. func oldStrokeOpacity() -> CGFloat {
  161. if let value = UserDefaults.standard.value(forKey: PDFCompareOldStrokeOpacityFilesKey) as? CGFloat {
  162. return value
  163. }
  164. return 1.0
  165. }
  166. func setNewFillOpacity(_ newFillOpacity: CGFloat) {
  167. UserDefaults.standard.set(newFillOpacity, forKey: PDFCompareNewFillOpacityFilesKey)
  168. }
  169. func newFillOpacity() -> CGFloat {
  170. if let value = UserDefaults.standard.value(forKey: PDFCompareNewFillOpacityFilesKey) as? CGFloat {
  171. return value
  172. }
  173. return 1.0
  174. }
  175. func setOldFillOpacity(_ oldFillOpacity: CGFloat) {
  176. UserDefaults.standard.set(oldFillOpacity, forKey: PDFCompareOldFillOpacityFilesKey)
  177. }
  178. func oldFillOpacity() -> CGFloat {
  179. if let value = UserDefaults.standard.value(forKey: PDFCompareOldFillOpacityFilesKey) as? CGFloat {
  180. return value
  181. }
  182. return 1.0
  183. }
  184. func setIsNOFill(_ isNOFill: Bool) {
  185. UserDefaults.standard.set(isNOFill, forKey: PDFCompareIsNOFillFilesKey)
  186. }
  187. func isNOFill() -> Bool {
  188. return UserDefaults.standard.bool(forKey: PDFCompareIsNOFillFilesKey)
  189. }
  190. func setBlendMod(_ blendMod: CPDFBlendMode) {
  191. UserDefaults.standard.set(blendMod.rawValue, forKey: PDFComparePDFBlendModeFilesKey)
  192. }
  193. func blendMod() -> CPDFBlendMode {
  194. if let value = UserDefaults.standard.value(forKey: PDFComparePDFBlendModeFilesKey) as? Int,
  195. let blendMode = CPDFBlendMode(rawValue: value) {
  196. return blendMode
  197. }
  198. return CPDFBlendMode.darken
  199. }
  200. }