KMNotesPreferences.swift 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460
  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. let textFontName = KMPreference.shared.markupFontTextString
  193. let textFontSize = KMPreference.shared.markupTextFontSize
  194. let textFont = NSFont(name: textFontName, size: textFontSize.cgFloat)
  195. self.fontWell1.font = textFont
  196. let noteFontName = KMPreference.shared.markupFontNoteString
  197. let noteFontSize = KMPreference.shared.markupNoteFontSize
  198. let noteFont = NSFont(name: noteFontName, size: noteFontSize.cgFloat)
  199. self.fontWell2.font = noteFont
  200. let alignment = KMPreference.shared.markupFontTextAligment
  201. if alignment == .left {
  202. self.alignmentSegmentControl.selectedSegment = 0
  203. } else if alignment == .center {
  204. self.alignmentSegmentControl.selectedSegment = 1
  205. } else if alignment == .right {
  206. self.alignmentSegmentControl.selectedSegment = 2
  207. }
  208. self.textLineWell.displayStyle = .rectangle
  209. self.textLineWell.lineWidth = KMPreference.shared.freeTextNoteLineWidth.cgFloat
  210. self.textLineWell.style = CPDFBorderStyle(rawValue: KMPreference.shared.freeTextNoteLineStyle) ?? .solid
  211. self.textLineWell.dashPattern = KMPreference.shared.freeTextNoteDashPattern as NSArray
  212. self.lineLineWell.displayStyle = .line
  213. self.lineLineWell.lineWidth = KMPreference.shared.lineNoteLineWidth.cgFloat
  214. self.lineLineWell.style = CPDFBorderStyle(rawValue: KMPreference.shared.lineNoteLineStyle) ?? .solid
  215. self.lineLineWell.dashPattern = KMPreference.shared.lineNoteDashPattern as NSArray
  216. self.lineLineWell.startLineStyle = CPDFLineStyle(rawValue: KMPreference.shared.lineNoteStartLineStyle) ?? .none
  217. self.lineLineWell.endLineStyle = CPDFLineStyle(rawValue: KMPreference.shared.lineNoteEndLineStyle) ?? .none
  218. self.freehandLineWell.displayStyle = .simpleLine
  219. self.freehandLineWell.lineWidth = KMPreference.shared.inkNoteLineWidth.cgFloat
  220. self.freehandLineWell.style = CPDFBorderStyle(rawValue: KMPreference.shared.inkNoteLineStyle) ?? .solid
  221. self.freehandLineWell.dashPattern = KMPreference.shared.inkNoteDashPattern as NSArray
  222. self.circleLineWell.displayStyle = .oval
  223. self.circleLineWell.lineWidth = KMPreference.shared.circleNoteLineWidth.cgFloat
  224. self.circleLineWell.style = CPDFBorderStyle(rawValue: KMPreference.shared.circleNoteLineStyle) ?? .solid
  225. self.circleLineWell.dashPattern = KMPreference.shared.circleNoteDashPattern as NSArray
  226. self.rectLineWell.displayStyle = .rectangle
  227. self.rectLineWell.lineWidth = KMPreference.shared.squareNoteLineWidth.cgFloat
  228. self.rectLineWell.style = CPDFBorderStyle(rawValue: KMPreference.shared.squareNoteLineStyle) ?? .solid
  229. self.rectLineWell.dashPattern = KMPreference.shared.squareNoteDashPattern as NSArray
  230. }
  231. private func _initActions() {
  232. self.textColorWell.target = self
  233. self.textColorWell.action = #selector(textColorWellAction)
  234. self.anchoredColorWell.target = self
  235. self.anchoredColorWell.action = #selector(anchoredColorWellAction)
  236. self.lineColorWell.target = self
  237. self.lineColorWell.action = #selector(lineColorWellAction)
  238. self.lineInteriorColorWell.target = self
  239. self.lineInteriorColorWell.action = #selector(lineInteriorColorWellAction)
  240. self.circleColorWell.target = self
  241. self.circleColorWell.action = #selector(circleColorWellAction)
  242. self.circleInteriorColorWell.target = self
  243. self.circleInteriorColorWell.action = #selector(circleInteriorColorWellAction)
  244. self.rectColorWell.target = self
  245. self.rectColorWell.action = #selector(rectColorWellAction)
  246. self.rectInteriorColorWell.target = self
  247. self.rectInteriorColorWell.action = #selector(rectInteriorColorWellAction)
  248. self.highlightColorWell.target = self
  249. self.highlightColorWell.action = #selector(highlightColorWellAction)
  250. self.underlineColorWell.target = self
  251. self.underlineColorWell.action = #selector(underlineColorWellAction)
  252. self.strikeThroughColorWell.target = self
  253. self.strikeThroughColorWell.action = #selector(strikeThroughColorWellAction)
  254. self.freehandColorWell.target = self
  255. self.freehandColorWell.action = #selector(freehandColorWellAction)
  256. self.fontWell1.target = self
  257. self.fontWell1.action = #selector(textFontWellAction)
  258. self.fontWell2.target = self
  259. self.fontWell2.action = #selector(anchoredFontWellAction)
  260. self.alignmentSegmentControl.target = self
  261. self.alignmentSegmentControl.action = #selector(alignmentSegmentAction)
  262. self.textLineWell.target = self
  263. self.textLineWell.action = #selector(textLineWellAction)
  264. self.lineLineWell.target = self
  265. self.lineLineWell.action = #selector(lineLineWellAction)
  266. self.freehandLineWell.target = self
  267. self.freehandLineWell.action = #selector(freehandLineWellAction)
  268. self.circleLineWell.target = self
  269. self.circleLineWell.action = #selector(circleLineWellAction)
  270. self.rectLineWell.target = self
  271. self.rectLineWell.action = #selector(rectLineWellAction)
  272. }
  273. @objc func textColorWellAction(_ sender: NSColorWell) {
  274. KMPreference.shared.markupTextColor = sender.color
  275. }
  276. @objc func anchoredColorWellAction(_ sender: NSColorWell) {
  277. KMPreference.shared.markupNoteColor = sender.color
  278. }
  279. @objc func lineColorWellAction(_ sender: NSColorWell) {
  280. KMPreference.shared.markupLineColor = sender.color
  281. }
  282. @objc func lineInteriorColorWellAction(_ sender: NSColorWell) {
  283. // KMPreference.shared.markupLineColor = sender.color
  284. }
  285. @objc func circleColorWellAction(_ sender: NSColorWell) {
  286. KMPreference.shared.markupCircleBorderColor = sender.color
  287. }
  288. @objc func circleInteriorColorWellAction(_ sender: NSColorWell) {
  289. KMPreference.shared.markupCircleFillColor = sender.color
  290. }
  291. @objc func rectColorWellAction(_ sender: NSColorWell) {
  292. KMPreference.shared.markupRectangleBorderColor = sender.color
  293. }
  294. @objc func rectInteriorColorWellAction(_ sender: NSColorWell) {
  295. KMPreference.shared.markupRectangleFillColor = sender.color
  296. }
  297. @objc func highlightColorWellAction(_ sender: NSColorWell) {
  298. KMPreference.shared.markupHighlightColor = sender.color
  299. }
  300. @objc func underlineColorWellAction(_ sender: NSColorWell) {
  301. KMPreference.shared.markupUnderlineColor = sender.color
  302. }
  303. @objc func strikeThroughColorWellAction(_ sender: NSColorWell) {
  304. KMPreference.shared.markupStrikthroughColor = sender.color
  305. }
  306. @objc func freehandColorWellAction(_ sender: NSColorWell) {
  307. KMPreference.shared.markupPenColor = sender.color
  308. }
  309. // MARK: - Font
  310. @objc func textFontWellAction(_ sender: KMFontWell) {
  311. // KMPrint("\(sender.fontName),\(sender.fontSize)")
  312. KMPreference.shared.markupFontTextString = sender.fontName ?? ""
  313. KMPreference.shared.markupTextFontSize = Float(sender.fontSize)
  314. }
  315. @objc func anchoredFontWellAction(_ sender: KMFontWell) {
  316. KMPreference.shared.markupFontNoteString = sender.fontName ?? ""
  317. KMPreference.shared.markupNoteFontSize = Float(sender.fontSize)
  318. }
  319. @objc func alignmentSegmentAction(_ sender: NSSegmentedControl) {
  320. if sender.selectedSegment == 0 {
  321. KMPreference.shared.markupFontTextAligment = .left
  322. } else if sender.selectedSegment == 1 {
  323. KMPreference.shared.markupFontTextAligment = .center
  324. } else if sender.selectedSegment == 2 {
  325. KMPreference.shared.markupFontTextAligment = .right
  326. }
  327. }
  328. // MARK: - LineWell
  329. @objc func textLineWellAction(_ sender: KMLineWell) {
  330. let action = KMLineInspector.shared.currentLineChangeAction
  331. if action == .lineWidth {
  332. KMPreference.shared.freeTextNoteLineWidth = Float(sender.lineWidth)
  333. } else if action == .style {
  334. KMPreference.shared.freeTextNoteLineStyle = sender.style.rawValue
  335. } else if action == .dashPattern {
  336. KMPreference.shared.freeTextNoteDashPattern = sender.dashPattern as? [CGFloat] ?? []
  337. }
  338. }
  339. @objc func lineLineWellAction(_ sender: KMLineWell) {
  340. let action = KMLineInspector.shared.currentLineChangeAction
  341. if action == .lineWidth {
  342. KMPreference.shared.lineNoteLineWidth = Float(sender.lineWidth)
  343. } else if action == .style {
  344. KMPreference.shared.lineNoteLineStyle = sender.style.rawValue
  345. } else if action == .dashPattern {
  346. KMPreference.shared.lineNoteDashPattern = sender.dashPattern as? [CGFloat] ?? []
  347. } else if action == .startLineStyle {
  348. KMPreference.shared.lineNoteStartLineStyle = sender.startLineStyle.rawValue
  349. } else if action == .endLineStyle {
  350. KMPreference.shared.lineNoteEndLineStyle = sender.endLineStyle.rawValue
  351. }
  352. }
  353. @objc func freehandLineWellAction(_ sender: KMLineWell) {
  354. let action = KMLineInspector.shared.currentLineChangeAction
  355. if action == .lineWidth {
  356. KMPreference.shared.inkNoteLineWidth = Float(sender.lineWidth)
  357. } else if action == .style {
  358. KMPreference.shared.inkNoteLineStyle = sender.style.rawValue
  359. } else if action == .dashPattern {
  360. KMPreference.shared.inkNoteDashPattern = sender.dashPattern as? [CGFloat] ?? []
  361. }
  362. }
  363. @objc func circleLineWellAction(_ sender: KMLineWell) {
  364. // KMPreference.shared.beginGrouping()
  365. // KMPreference.shared.circleNoteLineWidth = Float(sender.lineWidth)
  366. // KMPreference.shared.circleNoteLineStyle = sender.style.rawValue
  367. // KMPreference.shared.circleNoteDashPattern = sender.dashPattern as? [CGFloat] ?? []
  368. // KMPreference.shared.endGrouping()
  369. let action = KMLineInspector.shared.currentLineChangeAction
  370. if action == .lineWidth {
  371. KMPreference.shared.circleNoteLineWidth = Float(sender.lineWidth)
  372. } else if action == .style {
  373. KMPreference.shared.circleNoteLineStyle = sender.style.rawValue
  374. } else if action == .dashPattern {
  375. KMPreference.shared.circleNoteDashPattern = sender.dashPattern as? [CGFloat] ?? []
  376. }
  377. }
  378. @objc func rectLineWellAction(_ sender: KMLineWell) {
  379. let action = KMLineInspector.shared.currentLineChangeAction
  380. if action == .lineWidth {
  381. KMPreference.shared.squareNoteLineWidth = Float(sender.lineWidth)
  382. } else if action == .style {
  383. KMPreference.shared.squareNoteLineStyle = sender.style.rawValue
  384. } else if action == .dashPattern {
  385. KMPreference.shared.squareNoteDashPattern = sender.dashPattern as? [CGFloat] ?? []
  386. }
  387. }
  388. }
  389. extension KMNotesPreferences: KMPreferencePane {
  390. func defaultsDidRevert() {
  391. }
  392. func reloadData() {
  393. self._initValues()
  394. }
  395. }