KMNotesPreferences.swift 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462
  1. //
  2. // KMNotesPreferences.swift
  3. // PDF Reader Pro
  4. //
  5. // Created by tangchao on 2023/11/6.
  6. //
  7. import Cocoa
  8. class KMNotesPreferences: NSViewController {
  9. @IBOutlet weak var noteLabel: NSTextField!
  10. @IBOutlet weak var textLabel: NSTextField!
  11. @IBOutlet weak var textColorWell: NSColorWell!
  12. @IBOutlet weak var anchoredLabel: NSTextField!
  13. @IBOutlet weak var anchoredColorWell: NSColorWell!
  14. @IBOutlet weak var lineLabel: NSTextField!
  15. @IBOutlet weak var lineColorWell: NSColorWell!
  16. @IBOutlet weak var lineInteriorLabel: NSTextField!
  17. @IBOutlet weak var lineInteriorColorWell: NSColorWell!
  18. @IBOutlet weak var circleLabel: NSTextField!
  19. @IBOutlet weak var circleColorWell: NSColorWell!
  20. @IBOutlet weak var circleInteriorLabel: NSTextField!
  21. @IBOutlet weak var circleInteriorColorWell: NSColorWell!
  22. @IBOutlet weak var rectLabel: NSTextField!
  23. @IBOutlet weak var rectColorWell: NSColorWell!
  24. @IBOutlet weak var rectInteriorLabel: NSTextField!
  25. @IBOutlet weak var rectInteriorColorWell: NSColorWell!
  26. @IBOutlet weak var highlightLabel: NSTextField!
  27. @IBOutlet weak var highlightColorWell: NSColorWell!
  28. @IBOutlet weak var underlineLabel: NSTextField!
  29. @IBOutlet weak var underlineColorWell: NSColorWell!
  30. @IBOutlet weak var strikeThroughLabel: NSTextField!
  31. @IBOutlet weak var strikeThroughColorWell: NSColorWell!
  32. @IBOutlet weak var freehandLabel: NSTextField!
  33. @IBOutlet weak var freehandColorWell: NSColorWell!
  34. @IBOutlet weak var fontLabel: NSTextField!
  35. @IBOutlet weak var textFontLabel: NSTextField!
  36. @IBOutlet var fontWell1: KMFontWell!
  37. @IBOutlet var fontWell2: KMFontWell!
  38. @IBOutlet weak var anchoredFontLabel: NSTextField!
  39. @IBOutlet weak var alignmentSegmentControl: NSSegmentedControl!
  40. @IBOutlet weak var lineAndBorderLabel: NSTextField!
  41. @IBOutlet weak var lbTextLabel: NSTextField!
  42. @IBOutlet weak var textLineWell: KMLineWell!
  43. @IBOutlet weak var ibLineLabel: NSTextField!
  44. @IBOutlet weak var lineLineWell: KMLineWell!
  45. @IBOutlet weak var ibFreehandLabel: NSTextField!
  46. @IBOutlet weak var freehandLineWell: KMLineWell!
  47. @IBOutlet weak var ibCircleLabel: NSTextField!
  48. @IBOutlet weak var circleLineWell: KMLineWell!
  49. @IBOutlet weak var ibRectLabel: NSTextField!
  50. @IBOutlet weak var rectLineWell: KMLineWell!
  51. override var nibName: NSNib.Name? {
  52. return "NotesPreferences"
  53. }
  54. override var title: String? {
  55. set {
  56. super.title = newValue
  57. }
  58. get {
  59. return KMLocalizedString("Annotate", "Preference pane label")
  60. }
  61. }
  62. override func loadView() {
  63. super.loadView()
  64. self._initLayoutUI()
  65. // let fontWell = self.fontWell1
  66. // NSDictionary *options = [NSDictionary dictionaryWithObjectsAndKeys:NSUnarchiveFromDataTransformerName, NSValueTransformerNameBindingOption, nil];
  67. // [fontWell setHasTextColor:YES];
  68. // [fontWell bind:@"textColor" toObject:sudc withKeyPath:VALUES_KEY_PATH(SKStringConstants.freeTextNoteFontColorKey) options:options];
  69. }
  70. override func viewDidLoad() {
  71. super.viewDidLoad()
  72. self._initStrings()
  73. self._initValues()
  74. self._initActions()
  75. }
  76. private func _initLayoutUI() {
  77. var controls: [NSControl] = []
  78. var dw: CGFloat = 0
  79. var dw1: CGFloat = 0
  80. var dw2: CGFloat = 0
  81. for view in [self.highlightColorWell, self.underlineColorWell, self.strikeThroughColorWell, self.freehandColorWell] {
  82. if let data = view {
  83. controls.append(data)
  84. }
  85. }
  86. var colorLabels3: [NSTextField] = []
  87. for view in [self.highlightLabel, self.underlineLabel, self.strikeThroughLabel, self.freehandLabel] {
  88. if let data = view {
  89. colorLabels3.append(data)
  90. }
  91. }
  92. dw = KMAutoSizeLabelFields(colorLabels3, controls, false)
  93. for view in [self.circleColorWell, self.circleInteriorColorWell, self.rectColorWell, self.rectInteriorColorWell] {
  94. if let data = view {
  95. controls.append(data)
  96. }
  97. }
  98. for view in [self.highlightLabel, self.underlineLabel, self.strikeThroughLabel, self.freehandLabel] {
  99. if let data = view {
  100. controls.append(data)
  101. }
  102. }
  103. var colorLabels2: [NSTextField] = []
  104. for view in [self.circleLabel, self.circleInteriorLabel, self.rectLabel, self.rectInteriorLabel] {
  105. if let data = view {
  106. colorLabels2.append(data)
  107. }
  108. }
  109. dw += KMAutoSizeLabelFields(colorLabels2, controls, false)
  110. for view in [self.textColorWell, self.anchoredColorWell, self.lineColorWell, self.lineInteriorColorWell] {
  111. if let data = view {
  112. controls.append(data)
  113. }
  114. }
  115. for view in [self.circleLabel, self.circleInteriorLabel, self.rectLabel, self.rectInteriorLabel] {
  116. if let data = view {
  117. controls.append(data)
  118. }
  119. }
  120. for view in [self.fontWell1, self.fontWell2] {
  121. if let data = view {
  122. controls.append(data)
  123. }
  124. }
  125. for view in [self.textLineWell, self.lineLineWell, self.freehandLineWell] {
  126. if let data = view {
  127. controls.append(data)
  128. }
  129. }
  130. var labels1: [NSTextField] = []
  131. for view in [self.textLabel, self.anchoredLabel, self.lineLabel, self.lineInteriorLabel, self.textFontLabel, self.anchoredFontLabel, self.lbTextLabel, self.ibLineLabel, self.ibFreehandLabel] {
  132. if let data = view {
  133. labels1.append(data)
  134. }
  135. }
  136. dw1 = KMAutoSizeLabelFields(labels1, controls, false)
  137. dw += dw1
  138. var lineLabels2: [NSTextField] = []
  139. for view in [self.ibCircleLabel, self.ibRectLabel] {
  140. if let data = view {
  141. lineLabels2.append(data)
  142. }
  143. }
  144. var lineWells2: [NSControl] = []
  145. for view in [self.circleLineWell, self.rectLineWell] {
  146. if let data = view {
  147. lineWells2.append(data)
  148. }
  149. }
  150. dw2 = KMAutoSizeLabelFields(lineLabels2, lineWells2, false)
  151. KMShiftAndResizeViews([self.fontWell1, self.fontWell2], dx: 0.0, dw: dw - dw1)
  152. KMShiftAndResizeViews([self.ibCircleLabel, self.ibRectLabel, self.circleLineWell, self.rectLineWell], dx: dw - dw2, dw: 0.0)
  153. KMShiftAndResizeView(self.view, dx: 0.0, dw: dw)
  154. }
  155. private func _initStrings() {
  156. self.noteLabel.stringValue = NSLocalizedString("Note colors:", comment: "")
  157. self.textLabel.stringValue = NSLocalizedString("Text:", comment: "")
  158. self.anchoredLabel.stringValue = NSLocalizedString("Anchored:", comment: "")
  159. self.lineLabel.stringValue = NSLocalizedString("Line:", comment: "")
  160. self.lineInteriorLabel.stringValue = NSLocalizedString("Line interior:", comment: "")
  161. self.circleLabel.stringValue = NSLocalizedString("Circle:", comment: "")
  162. self.circleInteriorLabel.stringValue = NSLocalizedString("Circle interior:", comment: "")
  163. self.rectLabel.stringValue = NSLocalizedString("Rect:", comment: "")
  164. self.rectInteriorLabel.stringValue = NSLocalizedString("Rect interior:", comment: "")
  165. self.highlightLabel.stringValue = NSLocalizedString("Highlight:", comment: "")
  166. self.underlineLabel.stringValue = NSLocalizedString("Underline:", comment: "")
  167. self.strikeThroughLabel.stringValue = NSLocalizedString("Strikethrough:", comment: "")
  168. self.freehandLabel.stringValue = NSLocalizedString("Freehand:", comment: "")
  169. self.fontLabel.stringValue = NSLocalizedString("Note fonts:", comment: "")
  170. self.textFontLabel.stringValue = NSLocalizedString("Text:", comment: "")
  171. self.anchoredFontLabel.stringValue = NSLocalizedString("Anchored:", comment: "")
  172. self.lineAndBorderLabel.stringValue = NSLocalizedString("Note lines and borders:", comment: "")
  173. self.lbTextLabel.stringValue = NSLocalizedString("Text:", comment: "")
  174. self.ibLineLabel.stringValue = NSLocalizedString("Line:", comment: "")
  175. self.ibFreehandLabel.stringValue = NSLocalizedString("Freehand:", comment: "")
  176. self.ibCircleLabel.stringValue = NSLocalizedString("Circle:", comment: "")
  177. self.ibRectLabel.stringValue = NSLocalizedString("Rect:", comment: "")
  178. }
  179. private func _initValues() {
  180. self.textColorWell.color = KMPreference.shared.markupTextColor
  181. self.anchoredColorWell.color = KMPreference.shared.markupNoteColor
  182. self.lineColorWell.color = KMPreference.shared.markupLineColor
  183. // self.lineInteriorColorWell.color = KMPreference.shared.markupLineColor
  184. self.circleColorWell.color = KMPreference.shared.markupCircleBorderColor
  185. self.circleInteriorColorWell.color = KMPreference.shared.markupCircleFillColor
  186. self.rectColorWell.color = KMPreference.shared.markupRectangleBorderColor
  187. self.rectInteriorColorWell.color = KMPreference.shared.markupRectangleFillColor
  188. self.highlightColorWell.color = KMPreference.shared.markupHighlightColor
  189. self.underlineColorWell.color = KMPreference.shared.markupUnderlineColor
  190. self.strikeThroughColorWell.color = KMPreference.shared.markupStrikthroughColor
  191. self.freehandColorWell.color = KMPreference.shared.markupPenColor
  192. // SKFreeTextNoteFontNameKey
  193. // SKFreeTextNoteFontSizeKey
  194. // SKFreeTextNoteAlignmentKey
  195. // SKAnchoredNoteFontNameKey
  196. // SKAnchoredNoteFontSizeKey
  197. let textFontName = KMPreference.shared.markupFontTextString
  198. let textFontSize = KMPreference.shared.markupTextFontSize
  199. let textFont = NSFont(name: textFontName, size: textFontSize.cgFloat)
  200. self.fontWell1.font = textFont
  201. let noteFontName = KMPreference.shared.markupFontNoteString
  202. let noteFontSize = KMPreference.shared.markupNoteFontSize
  203. let noteFont = NSFont(name: noteFontName, size: noteFontSize.cgFloat)
  204. self.fontWell2.font = noteFont
  205. let alignment = KMPreference.shared.markupFontTextAligment
  206. if alignment == .left {
  207. self.alignmentSegmentControl.selectedSegment = 0
  208. } else if alignment == .center {
  209. self.alignmentSegmentControl.selectedSegment = 1
  210. } else if alignment == .right {
  211. self.alignmentSegmentControl.selectedSegment = 2
  212. }
  213. self.textLineWell.displayStyle = .rectangle
  214. self.textLineWell.lineWidth = KMPreference.shared.freeTextNoteLineWidth.cgFloat
  215. self.textLineWell.style = CPDFBorderStyle(rawValue: KMPreference.shared.freeTextNoteLineStyle) ?? .solid
  216. self.textLineWell.dashPattern = KMPreference.shared.freeTextNoteDashPattern as NSArray
  217. self.lineLineWell.displayStyle = .line
  218. self.lineLineWell.lineWidth = KMPreference.shared.lineNoteLineWidth.cgFloat
  219. self.lineLineWell.style = CPDFBorderStyle(rawValue: KMPreference.shared.lineNoteLineStyle) ?? .solid
  220. self.lineLineWell.dashPattern = KMPreference.shared.lineNoteDashPattern as NSArray
  221. self.lineLineWell.startLineStyle = CPDFLineStyle(rawValue: KMPreference.shared.lineNoteStartLineStyle) ?? .none
  222. self.lineLineWell.endLineStyle = CPDFLineStyle(rawValue: KMPreference.shared.lineNoteEndLineStyle) ?? .none
  223. self.freehandLineWell.displayStyle = .simpleLine
  224. self.freehandLineWell.lineWidth = KMPreference.shared.inkNoteLineWidth.cgFloat
  225. self.freehandLineWell.style = CPDFBorderStyle(rawValue: KMPreference.shared.inkNoteLineStyle) ?? .solid
  226. self.freehandLineWell.dashPattern = KMPreference.shared.inkNoteDashPattern as NSArray
  227. self.circleLineWell.displayStyle = .oval
  228. self.circleLineWell.lineWidth = KMPreference.shared.circleNoteLineWidth.cgFloat
  229. self.circleLineWell.style = CPDFBorderStyle(rawValue: KMPreference.shared.circleNoteLineStyle) ?? .solid
  230. self.circleLineWell.dashPattern = KMPreference.shared.circleNoteDashPattern as NSArray
  231. self.rectLineWell.displayStyle = .rectangle
  232. self.rectLineWell.lineWidth = KMPreference.shared.squareNoteLineWidth.cgFloat
  233. self.rectLineWell.style = CPDFBorderStyle(rawValue: KMPreference.shared.squareNoteLineStyle) ?? .solid
  234. self.rectLineWell.dashPattern = KMPreference.shared.squareNoteDashPattern as NSArray
  235. }
  236. private func _initActions() {
  237. self.textColorWell.target = self
  238. self.textColorWell.action = #selector(textColorWellAction)
  239. self.anchoredColorWell.target = self
  240. self.anchoredColorWell.action = #selector(anchoredColorWellAction)
  241. self.lineColorWell.target = self
  242. self.lineColorWell.action = #selector(lineColorWellAction)
  243. self.lineInteriorColorWell.target = self
  244. self.lineInteriorColorWell.action = #selector(lineInteriorColorWellAction)
  245. self.circleColorWell.target = self
  246. self.circleColorWell.action = #selector(circleColorWellAction)
  247. self.circleInteriorColorWell.target = self
  248. self.circleInteriorColorWell.action = #selector(circleInteriorColorWellAction)
  249. self.rectColorWell.target = self
  250. self.rectColorWell.action = #selector(rectColorWellAction)
  251. self.rectInteriorColorWell.target = self
  252. self.rectInteriorColorWell.action = #selector(rectInteriorColorWellAction)
  253. self.highlightColorWell.target = self
  254. self.highlightColorWell.action = #selector(highlightColorWellAction)
  255. self.underlineColorWell.target = self
  256. self.underlineColorWell.action = #selector(underlineColorWellAction)
  257. self.strikeThroughColorWell.target = self
  258. self.strikeThroughColorWell.action = #selector(strikeThroughColorWellAction)
  259. self.freehandColorWell.target = self
  260. self.freehandColorWell.action = #selector(freehandColorWellAction)
  261. self.fontWell1.target = self
  262. self.fontWell1.action = #selector(textFontWellAction)
  263. self.fontWell2.target = self
  264. self.fontWell2.action = #selector(anchoredFontWellAction)
  265. self.alignmentSegmentControl.target = self
  266. self.alignmentSegmentControl.action = #selector(alignmentSegmentAction)
  267. self.textLineWell.target = self
  268. self.textLineWell.action = #selector(textLineWellAction)
  269. self.lineLineWell.target = self
  270. self.lineLineWell.action = #selector(lineLineWellAction)
  271. self.freehandLineWell.target = self
  272. self.freehandLineWell.action = #selector(freehandLineWellAction)
  273. self.circleLineWell.target = self
  274. self.circleLineWell.action = #selector(circleLineWellAction)
  275. self.rectLineWell.target = self
  276. self.rectLineWell.action = #selector(rectLineWellAction)
  277. }
  278. @objc func textColorWellAction(_ sender: NSColorWell) {
  279. KMPreference.shared.markupTextColor = sender.color
  280. }
  281. @objc func anchoredColorWellAction(_ sender: NSColorWell) {
  282. KMPreference.shared.markupNoteColor = sender.color
  283. }
  284. @objc func lineColorWellAction(_ sender: NSColorWell) {
  285. KMPreference.shared.markupLineColor = sender.color
  286. }
  287. @objc func lineInteriorColorWellAction(_ sender: NSColorWell) {
  288. // KMPreference.shared.markupLineColor = sender.color
  289. }
  290. @objc func circleColorWellAction(_ sender: NSColorWell) {
  291. KMPreference.shared.markupCircleBorderColor = sender.color
  292. }
  293. @objc func circleInteriorColorWellAction(_ sender: NSColorWell) {
  294. KMPreference.shared.markupCircleFillColor = sender.color
  295. }
  296. @objc func rectColorWellAction(_ sender: NSColorWell) {
  297. KMPreference.shared.markupRectangleBorderColor = sender.color
  298. }
  299. @objc func rectInteriorColorWellAction(_ sender: NSColorWell) {
  300. KMPreference.shared.markupRectangleFillColor = sender.color
  301. }
  302. @objc func highlightColorWellAction(_ sender: NSColorWell) {
  303. KMPreference.shared.markupHighlightColor = sender.color
  304. }
  305. @objc func underlineColorWellAction(_ sender: NSColorWell) {
  306. KMPreference.shared.markupUnderlineColor = sender.color
  307. }
  308. @objc func strikeThroughColorWellAction(_ sender: NSColorWell) {
  309. KMPreference.shared.markupStrikthroughColor = sender.color
  310. }
  311. @objc func freehandColorWellAction(_ sender: NSColorWell) {
  312. KMPreference.shared.markupPenColor = sender.color
  313. }
  314. // MARK: - Font
  315. @objc func textFontWellAction(_ sender: KMFontWell) {
  316. // KMPrint("\(sender.fontName),\(sender.fontSize)")
  317. KMPreference.shared.markupFontTextString = sender.fontName ?? ""
  318. KMPreference.shared.markupTextFontSize = Float(sender.fontSize)
  319. }
  320. @objc func anchoredFontWellAction(_ sender: KMFontWell) {
  321. KMPreference.shared.markupFontNoteString = sender.fontName ?? ""
  322. KMPreference.shared.markupNoteFontSize = Float(sender.fontSize)
  323. }
  324. @objc func alignmentSegmentAction(_ sender: NSSegmentedControl) {
  325. if sender.selectedSegment == 0 {
  326. KMPreference.shared.markupFontTextAligment = .left
  327. } else if sender.selectedSegment == 1 {
  328. KMPreference.shared.markupFontTextAligment = .center
  329. } else if sender.selectedSegment == 2 {
  330. KMPreference.shared.markupFontTextAligment = .right
  331. }
  332. }
  333. // MARK: - LineWell
  334. @objc func textLineWellAction(_ sender: KMLineWell) {
  335. let action = KMLineInspector.shared.currentLineChangeAction
  336. if action == .lineWidth {
  337. KMPreference.shared.freeTextNoteLineWidth = Float(sender.lineWidth)
  338. } else if action == .style {
  339. KMPreference.shared.freeTextNoteLineStyle = sender.style.rawValue
  340. } else if action == .dashPattern {
  341. KMPreference.shared.freeTextNoteDashPattern = sender.dashPattern as? [CGFloat] ?? []
  342. }
  343. }
  344. @objc func lineLineWellAction(_ sender: KMLineWell) {
  345. let action = KMLineInspector.shared.currentLineChangeAction
  346. if action == .lineWidth {
  347. KMPreference.shared.lineNoteLineWidth = Float(sender.lineWidth)
  348. } else if action == .style {
  349. KMPreference.shared.lineNoteLineStyle = sender.style.rawValue
  350. } else if action == .dashPattern {
  351. KMPreference.shared.lineNoteDashPattern = sender.dashPattern as? [CGFloat] ?? []
  352. } else if action == .startLineStyle {
  353. KMPreference.shared.lineNoteStartLineStyle = sender.startLineStyle.rawValue
  354. } else if action == .endLineStyle {
  355. KMPreference.shared.lineNoteEndLineStyle = sender.endLineStyle.rawValue
  356. }
  357. }
  358. @objc func freehandLineWellAction(_ sender: KMLineWell) {
  359. let action = KMLineInspector.shared.currentLineChangeAction
  360. if action == .lineWidth {
  361. KMPreference.shared.inkNoteLineWidth = Float(sender.lineWidth)
  362. } else if action == .style {
  363. KMPreference.shared.inkNoteLineStyle = sender.style.rawValue
  364. } else if action == .dashPattern {
  365. KMPreference.shared.inkNoteDashPattern = sender.dashPattern as? [CGFloat] ?? []
  366. }
  367. }
  368. @objc func circleLineWellAction(_ sender: KMLineWell) {
  369. // KMPreference.shared.beginGrouping()
  370. // KMPreference.shared.circleNoteLineWidth = Float(sender.lineWidth)
  371. // KMPreference.shared.circleNoteLineStyle = sender.style.rawValue
  372. // KMPreference.shared.circleNoteDashPattern = sender.dashPattern as? [CGFloat] ?? []
  373. // KMPreference.shared.endGrouping()
  374. let action = KMLineInspector.shared.currentLineChangeAction
  375. if action == .lineWidth {
  376. KMPreference.shared.circleNoteLineWidth = Float(sender.lineWidth)
  377. } else if action == .style {
  378. KMPreference.shared.circleNoteLineStyle = sender.style.rawValue
  379. } else if action == .dashPattern {
  380. KMPreference.shared.circleNoteDashPattern = sender.dashPattern as? [CGFloat] ?? []
  381. }
  382. }
  383. @objc func rectLineWellAction(_ sender: KMLineWell) {
  384. let action = KMLineInspector.shared.currentLineChangeAction
  385. if action == .lineWidth {
  386. KMPreference.shared.squareNoteLineWidth = Float(sender.lineWidth)
  387. } else if action == .style {
  388. KMPreference.shared.squareNoteLineStyle = sender.style.rawValue
  389. } else if action == .dashPattern {
  390. KMPreference.shared.squareNoteDashPattern = sender.dashPattern as? [CGFloat] ?? []
  391. }
  392. }
  393. }
  394. extension KMNotesPreferences: KMPreferencePane {
  395. func defaultsDidRevert() {
  396. }
  397. }