KMNotesPreferences.swift 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405
  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. // NSUserDefaultsController *sudc = [NSUserDefaultsController sharedUserDefaultsController];
  66. // SKLineWell *lineWell = [lineWells1 objectAtIndex:0];
  67. // [lineWell bind:SKLineWellLineWidthKey toObject:sudc withKeyPath:VALUES_KEY_PATH(SKStringConstants.freeTextNoteLineWidthKey) options:nil];
  68. // [lineWell bind:SKLineWellStyleKey toObject:sudc withKeyPath:VALUES_KEY_PATH(SKStringConstants.freeTextNoteLineStyleKey) options:nil];
  69. // [lineWell bind:SKLineWellDashPatternKey toObject:sudc withKeyPath:VALUES_KEY_PATH(SKStringConstants.freeTextNoteDashPatternKey) options:nil];
  70. // [lineWell setDisplayStyle:SKLineWellDisplayStyleRectangle];
  71. self.textLineWell.lineWidth = 0
  72. //
  73. // [lineWell bind:SKLineWellLineWidthKey toObject:sudc withKeyPath:VALUES_KEY_PATH(SKStringConstants.circleNoteLineWidthKey) options:nil];
  74. // [lineWell bind:SKLineWellStyleKey toObject:sudc withKeyPath:VALUES_KEY_PATH(SKStringConstants.circleNoteLineStyleKey) options:nil];
  75. // [lineWell bind:SKLineWellDashPatternKey toObject:sudc withKeyPath:VALUES_KEY_PATH(SKStringConstants.circleNoteDashPatternKey) options:nil];
  76. self.circleLineWell.displayStyle = .oval
  77. self.circleLineWell.lineWidth = 2
  78. //
  79. // lineWell = [lineWells2 objectAtIndex:1];
  80. // [lineWell bind:SKLineWellLineWidthKey toObject:sudc withKeyPath:VALUES_KEY_PATH(SKStringConstants.squareNoteLineWidthKey) options:nil];
  81. // [lineWell bind:SKLineWellStyleKey toObject:sudc withKeyPath:VALUES_KEY_PATH(SKStringConstants.squareNoteLineStyleKey) options:nil];
  82. // [lineWell bind:SKLineWellDashPatternKey toObject:sudc withKeyPath:VALUES_KEY_PATH(SKStringConstants.squareNoteDashPatternKey) options:nil];
  83. // [lineWell setDisplayStyle:SKLineWellDisplayStyleRectangle];
  84. self.rectLineWell.displayStyle = .rectangle
  85. self.rectLineWell.lineWidth = 2
  86. //
  87. // lineWell = [lineWells1 objectAtIndex:1];
  88. // [lineWell bind:SKLineWellLineWidthKey toObject:sudc withKeyPath:VALUES_KEY_PATH(SKStringConstants.lineNoteLineWidthKey) options:nil];
  89. // [lineWell bind:SKLineWellStyleKey toObject:sudc withKeyPath:VALUES_KEY_PATH(SKStringConstants.lineNoteLineStyleKey) options:nil];
  90. // [lineWell bind:SKLineWellDashPatternKey toObject:sudc withKeyPath:VALUES_KEY_PATH(SKStringConstants.lineNoteDashPatternKey) options:nil];
  91. // [lineWell bind:SKLineWellStartLineStyleKey toObject:sudc withKeyPath:VALUES_KEY_PATH(SKStringConstants.lineNoteStartLineStyleKey) options:nil];
  92. // [lineWell bind:SKLineWellEndLineStyleKey toObject:sudc withKeyPath:VALUES_KEY_PATH(SKStringConstants.lineNoteEndLineStyleKey) options:nil];
  93. self.lineLineWell.displayStyle = .line
  94. self.lineLineWell.lineWidth = 2
  95. self.lineLineWell.endLineStyle = .openArrow
  96. //
  97. // lineWell = [lineWells1 objectAtIndex:2];
  98. // [lineWell bind:SKLineWellLineWidthKey toObject:sudc withKeyPath:VALUES_KEY_PATH(SKStringConstants.inkNoteLineWidthKey) options:nil];
  99. // [lineWell bind:SKLineWellStyleKey toObject:sudc withKeyPath:VALUES_KEY_PATH(SKStringConstants.inkNoteLineStyleKey) options:nil];
  100. // [lineWell bind:SKLineWellDashPatternKey toObject:sudc withKeyPath:VALUES_KEY_PATH(SKStringConstants.inkNoteDashPatternKey) options:nil];
  101. // [lineWell setDisplayStyle:SKLineWellDisplayStyleSimpleLine];
  102. self.freehandLineWell.displayStyle = .simpleLine
  103. self.freehandLineWell.lineWidth = 2
  104. // let fontWell = self.fontWell1
  105. // NSDictionary *options = [NSDictionary dictionaryWithObjectsAndKeys:NSUnarchiveFromDataTransformerName, NSValueTransformerNameBindingOption, nil];
  106. // [fontWell setHasTextColor:YES];
  107. // [fontWell bind:@"textColor" toObject:sudc withKeyPath:VALUES_KEY_PATH(SKStringConstants.freeTextNoteFontColorKey) options:options];
  108. }
  109. override func viewDidLoad() {
  110. super.viewDidLoad()
  111. self._initStrings()
  112. self._initValues()
  113. self._initActions()
  114. }
  115. private func _initLayoutUI() {
  116. var controls: [NSControl] = []
  117. var dw: CGFloat = 0
  118. var dw1: CGFloat = 0
  119. var dw2: CGFloat = 0
  120. for view in [self.highlightColorWell, self.underlineColorWell, self.strikeThroughColorWell, self.freehandColorWell] {
  121. if let data = view {
  122. controls.append(data)
  123. }
  124. }
  125. var colorLabels3: [NSTextField] = []
  126. for view in [self.highlightLabel, self.underlineLabel, self.strikeThroughLabel, self.freehandLabel] {
  127. if let data = view {
  128. colorLabels3.append(data)
  129. }
  130. }
  131. dw = KMAutoSizeLabelFields(colorLabels3, controls, false)
  132. for view in [self.circleColorWell, self.circleInteriorColorWell, self.rectColorWell, self.rectInteriorColorWell] {
  133. if let data = view {
  134. controls.append(data)
  135. }
  136. }
  137. for view in [self.highlightLabel, self.underlineLabel, self.strikeThroughLabel, self.freehandLabel] {
  138. if let data = view {
  139. controls.append(data)
  140. }
  141. }
  142. var colorLabels2: [NSTextField] = []
  143. for view in [self.circleLabel, self.circleInteriorLabel, self.rectLabel, self.rectInteriorLabel] {
  144. if let data = view {
  145. colorLabels2.append(data)
  146. }
  147. }
  148. dw += KMAutoSizeLabelFields(colorLabels2, controls, false)
  149. for view in [self.textColorWell, self.anchoredColorWell, self.lineColorWell, self.lineInteriorColorWell] {
  150. if let data = view {
  151. controls.append(data)
  152. }
  153. }
  154. for view in [self.circleLabel, self.circleInteriorLabel, self.rectLabel, self.rectInteriorLabel] {
  155. if let data = view {
  156. controls.append(data)
  157. }
  158. }
  159. for view in [self.fontWell1, self.fontWell2] {
  160. if let data = view {
  161. controls.append(data)
  162. }
  163. }
  164. for view in [self.textLineWell, self.lineLineWell, self.freehandLineWell] {
  165. if let data = view {
  166. controls.append(data)
  167. }
  168. }
  169. var labels1: [NSTextField] = []
  170. for view in [self.textLabel, self.anchoredLabel, self.lineLabel, self.lineInteriorLabel, self.textFontLabel, self.anchoredFontLabel, self.lbTextLabel, self.ibLineLabel, self.ibFreehandLabel] {
  171. if let data = view {
  172. labels1.append(data)
  173. }
  174. }
  175. dw1 = KMAutoSizeLabelFields(labels1, controls, false)
  176. dw += dw1
  177. var lineLabels2: [NSTextField] = []
  178. for view in [self.ibCircleLabel, self.ibRectLabel] {
  179. if let data = view {
  180. lineLabels2.append(data)
  181. }
  182. }
  183. var lineWells2: [NSControl] = []
  184. for view in [self.circleLineWell, self.rectLineWell] {
  185. if let data = view {
  186. lineWells2.append(data)
  187. }
  188. }
  189. dw2 = KMAutoSizeLabelFields(lineLabels2, lineWells2, false)
  190. KMShiftAndResizeViews([self.fontWell1, self.fontWell2], dx: 0.0, dw: dw - dw1)
  191. KMShiftAndResizeViews([self.ibCircleLabel, self.ibRectLabel, self.circleLineWell, self.rectLineWell], dx: dw - dw2, dw: 0.0)
  192. KMShiftAndResizeView(self.view, dx: 0.0, dw: dw)
  193. }
  194. private func _initStrings() {
  195. self.noteLabel.stringValue = NSLocalizedString("Note colors:", comment: "")
  196. self.textLabel.stringValue = NSLocalizedString("Text:", comment: "")
  197. self.anchoredLabel.stringValue = NSLocalizedString("Anchored:", comment: "")
  198. self.lineLabel.stringValue = NSLocalizedString("Line:", comment: "")
  199. self.lineInteriorLabel.stringValue = NSLocalizedString("Line interior:", comment: "")
  200. self.circleLabel.stringValue = NSLocalizedString("Circle:", comment: "")
  201. self.circleInteriorLabel.stringValue = NSLocalizedString("Circle interior:", comment: "")
  202. self.rectLabel.stringValue = NSLocalizedString("Rect:", comment: "")
  203. self.rectInteriorLabel.stringValue = NSLocalizedString("Rect interior:", comment: "")
  204. self.highlightLabel.stringValue = NSLocalizedString("Highlight:", comment: "")
  205. self.underlineLabel.stringValue = NSLocalizedString("Underline:", comment: "")
  206. self.strikeThroughLabel.stringValue = NSLocalizedString("Strikethrough:", comment: "")
  207. self.freehandLabel.stringValue = NSLocalizedString("Freehand:", comment: "")
  208. self.fontLabel.stringValue = NSLocalizedString("Note fonts:", comment: "")
  209. self.textFontLabel.stringValue = NSLocalizedString("Text:", comment: "")
  210. self.anchoredFontLabel.stringValue = NSLocalizedString("Anchored:", comment: "")
  211. self.lineAndBorderLabel.stringValue = NSLocalizedString("Note lines and borders:", comment: "")
  212. self.lbTextLabel.stringValue = NSLocalizedString("Text:", comment: "")
  213. self.ibLineLabel.stringValue = NSLocalizedString("Line:", comment: "")
  214. self.ibFreehandLabel.stringValue = NSLocalizedString("Freehand:", comment: "")
  215. self.ibCircleLabel.stringValue = NSLocalizedString("Circle:", comment: "")
  216. self.ibRectLabel.stringValue = NSLocalizedString("Rect:", comment: "")
  217. }
  218. private func _initValues() {
  219. self.textColorWell.color = KMPreference.shared.markupTextColor
  220. self.anchoredColorWell.color = KMPreference.shared.markupNoteColor
  221. self.lineColorWell.color = KMPreference.shared.markupLineColor
  222. // self.lineInteriorColorWell.color = KMPreference.shared.markupLineColor
  223. self.circleColorWell.color = KMPreference.shared.markupCircleBorderColor
  224. self.circleInteriorColorWell.color = KMPreference.shared.markupCircleFillColor
  225. self.rectColorWell.color = KMPreference.shared.markupRectangleBorderColor
  226. self.rectInteriorColorWell.color = KMPreference.shared.markupRectangleFillColor
  227. self.highlightColorWell.color = KMPreference.shared.markupHighlightColor
  228. self.underlineColorWell.color = KMPreference.shared.markupUnderlineColor
  229. self.strikeThroughColorWell.color = KMPreference.shared.markupStrikthroughColor
  230. self.freehandColorWell.color = KMPreference.shared.markupPenColor
  231. // SKFreeTextNoteFontNameKey
  232. // SKFreeTextNoteFontSizeKey
  233. // SKFreeTextNoteAlignmentKey
  234. // SKAnchoredNoteFontNameKey
  235. // SKAnchoredNoteFontSizeKey
  236. let textFontName = KMPreference.shared.markupFontTextString
  237. let textFontSize = KMPreference.shared.markupTextFontSize
  238. let textFont = NSFont(name: textFontName, size: textFontSize.cgFloat)
  239. self.fontWell1.font = textFont
  240. let noteFontName = KMPreference.shared.markupFontNoteString
  241. let noteFontSize = KMPreference.shared.markupNoteFontSize
  242. let noteFont = NSFont(name: noteFontName, size: noteFontSize.cgFloat)
  243. self.fontWell2.font = noteFont
  244. let alignment = KMPreference.shared.markupFontTextAligment
  245. if alignment == .left {
  246. self.alignmentSegmentControl.selectedSegment = 0
  247. } else if alignment == .center {
  248. self.alignmentSegmentControl.selectedSegment = 1
  249. } else if alignment == .right {
  250. self.alignmentSegmentControl.selectedSegment = 2
  251. }
  252. }
  253. private func _initActions() {
  254. self.textColorWell.target = self
  255. self.textColorWell.action = #selector(textColorWellAction)
  256. self.anchoredColorWell.target = self
  257. self.anchoredColorWell.action = #selector(anchoredColorWellAction)
  258. self.lineColorWell.target = self
  259. self.lineColorWell.action = #selector(lineColorWellAction)
  260. self.lineInteriorColorWell.target = self
  261. self.lineInteriorColorWell.action = #selector(lineInteriorColorWellAction)
  262. self.circleColorWell.target = self
  263. self.circleColorWell.action = #selector(circleColorWellAction)
  264. self.circleInteriorColorWell.target = self
  265. self.circleInteriorColorWell.action = #selector(circleInteriorColorWellAction)
  266. self.rectColorWell.target = self
  267. self.rectColorWell.action = #selector(rectColorWellAction)
  268. self.rectInteriorColorWell.target = self
  269. self.rectInteriorColorWell.action = #selector(rectInteriorColorWellAction)
  270. self.highlightColorWell.target = self
  271. self.highlightColorWell.action = #selector(highlightColorWellAction)
  272. self.underlineColorWell.target = self
  273. self.underlineColorWell.action = #selector(underlineColorWellAction)
  274. self.strikeThroughColorWell.target = self
  275. self.strikeThroughColorWell.action = #selector(strikeThroughColorWellAction)
  276. self.freehandColorWell.target = self
  277. self.freehandColorWell.action = #selector(freehandColorWellAction)
  278. self.fontWell1.target = self
  279. self.fontWell1.action = #selector(textFontWellAction)
  280. self.fontWell2.target = self
  281. self.fontWell2.action = #selector(anchoredFontWellAction)
  282. self.alignmentSegmentControl.target = self
  283. self.alignmentSegmentControl.action = #selector(alignmentSegmentAction)
  284. }
  285. @objc func textColorWellAction(_ sender: NSColorWell) {
  286. KMPreference.shared.markupTextColor = sender.color
  287. }
  288. @objc func anchoredColorWellAction(_ sender: NSColorWell) {
  289. KMPreference.shared.markupNoteColor = sender.color
  290. }
  291. @objc func lineColorWellAction(_ sender: NSColorWell) {
  292. KMPreference.shared.markupLineColor = sender.color
  293. }
  294. @objc func lineInteriorColorWellAction(_ sender: NSColorWell) {
  295. // KMPreference.shared.markupLineColor = sender.color
  296. }
  297. @objc func circleColorWellAction(_ sender: NSColorWell) {
  298. KMPreference.shared.markupCircleBorderColor = sender.color
  299. }
  300. @objc func circleInteriorColorWellAction(_ sender: NSColorWell) {
  301. KMPreference.shared.markupCircleFillColor = sender.color
  302. }
  303. @objc func rectColorWellAction(_ sender: NSColorWell) {
  304. KMPreference.shared.markupRectangleBorderColor = sender.color
  305. }
  306. @objc func rectInteriorColorWellAction(_ sender: NSColorWell) {
  307. KMPreference.shared.markupRectangleFillColor = sender.color
  308. }
  309. @objc func highlightColorWellAction(_ sender: NSColorWell) {
  310. KMPreference.shared.markupHighlightColor = sender.color
  311. }
  312. @objc func underlineColorWellAction(_ sender: NSColorWell) {
  313. KMPreference.shared.markupUnderlineColor = sender.color
  314. }
  315. @objc func strikeThroughColorWellAction(_ sender: NSColorWell) {
  316. KMPreference.shared.markupStrikthroughColor = sender.color
  317. }
  318. @objc func freehandColorWellAction(_ sender: NSColorWell) {
  319. KMPreference.shared.markupPenColor = sender.color
  320. }
  321. // MARK: - Font
  322. @objc func textFontWellAction(_ sender: KMFontWell) {
  323. // KMPrint("\(sender.fontName),\(sender.fontSize)")
  324. KMPreference.shared.markupFontTextString = sender.fontName ?? ""
  325. KMPreference.shared.markupTextFontSize = Float(sender.fontSize)
  326. }
  327. @objc func anchoredFontWellAction(_ sender: KMFontWell) {
  328. KMPreference.shared.markupFontNoteString = sender.fontName ?? ""
  329. KMPreference.shared.markupNoteFontSize = Float(sender.fontSize)
  330. }
  331. @objc func alignmentSegmentAction(_ sender: NSSegmentedControl) {
  332. if sender.selectedSegment == 0 {
  333. KMPreference.shared.markupFontTextAligment = .left
  334. } else if sender.selectedSegment == 1 {
  335. KMPreference.shared.markupFontTextAligment = .center
  336. } else if sender.selectedSegment == 2 {
  337. KMPreference.shared.markupFontTextAligment = .right
  338. }
  339. }
  340. }
  341. extension KMNotesPreferences: KMPreferencePane {
  342. func defaultsDidRevert() {
  343. }
  344. }