SettingsDisplayView.swift 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508
  1. //
  2. // SettingsDisplayView.swift
  3. // PDF Reader Pro
  4. //
  5. // Created by Niehaoyu on 2024/9/26.
  6. //
  7. import Cocoa
  8. import KMComponentLibrary
  9. class SettingsDisplayView: BaseXibView {
  10. @IBOutlet weak var contendBox: NSBox!
  11. @IBOutlet weak var layoutZoomView: NSView!
  12. @IBOutlet weak var layoutZoomLabel: NSTextField!
  13. @IBOutlet weak var layoutLabel: NSTextField!
  14. @IBOutlet weak var layoutSelectView: ComponentSelect!
  15. @IBOutlet weak var zoomLabel: NSTextField!
  16. @IBOutlet weak var zoomSelectView: ComponentSelect!
  17. @IBOutlet weak var leftSideView: NSView!
  18. @IBOutlet weak var leftSideLabel: NSTextField!
  19. @IBOutlet weak var defaultOpenRadio: ComponentRadio!
  20. @IBOutlet weak var defaultOpenSelectView: ComponentSelect!
  21. @IBOutlet weak var remeberLastRadio: ComponentRadio!
  22. @IBOutlet weak var hideLeftSideRadio: ComponentRadio!
  23. @IBOutlet weak var prioritizeOutlineRadio: ComponentRadio!
  24. @IBOutlet weak var defaultOpenRadioWidthConst: NSLayoutConstraint!
  25. @IBOutlet weak var remeberLastRadioWidthConst: NSLayoutConstraint!
  26. @IBOutlet weak var hideLeftSideRadioWidthConst: NSLayoutConstraint!
  27. @IBOutlet weak var prioritizeOutlineCheckboxWidthConst: NSLayoutConstraint!
  28. @IBOutlet weak var propertyPanelView: NSView!
  29. @IBOutlet weak var propertyPanelLabel: NSTextField!
  30. @IBOutlet var autoExpandPropertyPanelCheckbox: ComponentCheckBox!
  31. @IBOutlet weak var showQuickCheckbox: ComponentCheckBox!
  32. @IBOutlet weak var showQuickCheckboxWidthConst: NSLayoutConstraint!
  33. @IBOutlet var propertyPanelWidthConst: NSLayoutConstraint!
  34. @IBOutlet weak var highlightView: NSView!
  35. @IBOutlet weak var highlightLabel: NSTextField!
  36. @IBOutlet weak var highlightLinkCheckbox: ComponentCheckBox!
  37. @IBOutlet weak var highlightFormCheckbox: ComponentCheckBox!
  38. @IBOutlet weak var highlightLinkBoxWidthConst: NSLayoutConstraint!
  39. @IBOutlet weak var highglightFormboxWidthConst: NSLayoutConstraint!
  40. @IBOutlet var autoScrollView: NSView!
  41. @IBOutlet var autoScrolllabel: NSTextField!
  42. @IBOutlet var timeLabel: NSTextField!
  43. @IBOutlet var autoTimeSlider: ComponentSlider!
  44. @IBOutlet var autoTimeSelect: ComponentSelect!
  45. @IBOutlet var timeValueLabel: NSTextField!
  46. @IBOutlet var jumpSpaceLabel: NSTextField!
  47. @IBOutlet var jumpSpaceSlider: ComponentSlider!
  48. @IBOutlet var jumpSpaceSelect: ComponentSelect!
  49. @IBOutlet var jumpSpaceValueLabel: NSTextField!
  50. deinit {
  51. NotificationCenter.default.removeObserver(self)
  52. }
  53. override func draw(_ dirtyRect: NSRect) {
  54. super.draw(dirtyRect)
  55. // Drawing code here.
  56. self.setTitleUI()
  57. self.setUpLayoutAndZoom()
  58. self.setUpLeftSidePanel()
  59. self.setUpPropertyPanel()
  60. self.setUpHighlight()
  61. setUpAutoScroll()
  62. self.reloadData()
  63. }
  64. public required init?(coder decoder: NSCoder) {
  65. super.init(coder: decoder)
  66. }
  67. override init(frame frameRect: NSRect) {
  68. super.init(frame: frameRect)
  69. }
  70. public override func awakeFromNib() {
  71. super.awakeFromNib()
  72. }
  73. func setTitleUI () {
  74. //获取颜色
  75. let titleLabelColor: NSColor = ComponentLibrary.shared.getComponentColorFromKey("colorText/2")
  76. let subtitleLabelColor: NSColor = ComponentLibrary.shared.getComponentColorFromKey("comp-field/colorText-filled-nor")
  77. //获取字体
  78. let titleLabelFont: NSFont = ComponentLibrary.shared.getFontFromKey("mac/body-s-medium")
  79. let subtitleLabelFont: NSFont = ComponentLibrary.shared.getFontFromKey("mac/body-s-regular")
  80. //Layout and Zoom
  81. layoutZoomLabel.stringValue = KMLocalizedString("Default Layout and Zoom")
  82. layoutZoomLabel.textColor = titleLabelColor
  83. layoutZoomLabel.font = titleLabelFont
  84. layoutLabel.stringValue = KMLocalizedString("Page Layout:")
  85. layoutLabel.textColor = subtitleLabelColor
  86. layoutLabel.font = subtitleLabelFont
  87. zoomLabel.stringValue = KMLocalizedString("Zoom:")
  88. zoomLabel.textColor = subtitleLabelColor
  89. zoomLabel.font = subtitleLabelFont
  90. leftSideLabel.stringValue = KMLocalizedString("Left Side Panel")
  91. leftSideLabel.textColor = titleLabelColor
  92. leftSideLabel.font = titleLabelFont
  93. propertyPanelLabel.stringValue = KMLocalizedString("Property Panel")
  94. propertyPanelLabel.textColor = titleLabelColor
  95. propertyPanelLabel.font = titleLabelFont
  96. highlightLabel.stringValue = KMLocalizedString("Highlight")
  97. highlightLabel.textColor = titleLabelColor
  98. highlightLabel.font = titleLabelFont
  99. autoScrolllabel.stringValue = KMLocalizedString("Auto Scroll Options")
  100. autoScrolllabel.textColor = titleLabelColor
  101. autoScrolllabel.font = titleLabelFont
  102. timeLabel.stringValue = KMLocalizedString("Time Interval:")
  103. timeLabel.textColor = subtitleLabelColor
  104. timeLabel.font = subtitleLabelFont
  105. timeValueLabel.stringValue = KMLocalizedString("sec (1~100)")
  106. timeValueLabel.textColor = subtitleLabelColor
  107. timeValueLabel.font = subtitleLabelFont
  108. jumpSpaceLabel.stringValue = KMLocalizedString("Jump Space:")
  109. jumpSpaceLabel.textColor = subtitleLabelColor
  110. jumpSpaceLabel.font = subtitleLabelFont
  111. jumpSpaceValueLabel.stringValue = KMLocalizedString("px (10~1000)")
  112. jumpSpaceValueLabel.textColor = subtitleLabelColor
  113. jumpSpaceValueLabel.font = subtitleLabelFont
  114. }
  115. func setUpLayoutAndZoom() {
  116. layoutSelectView.properties = ComponentSelectProperties(size: .s,
  117. state: .normal,
  118. text: KMLocalizedString("Single Page Continuous"))
  119. if true {
  120. var menuItemArr: [ComponentMenuitemProperty] = []
  121. for string in ["Single Page", "Single Page Continuous", "Two Page", "Two Page Continuous",
  122. "Book Mode", "Book Mode Continuous"] {
  123. var itemProperty: ComponentMenuitemProperty = ComponentMenuitemProperty(multipleSelect: false,
  124. itemSelected: false,
  125. isDisabled: false,
  126. keyEquivalent: nil,
  127. text: KMLocalizedString(string))
  128. if string == " " {
  129. itemProperty = ComponentMenuitemProperty.divider()
  130. }
  131. menuItemArr.append(itemProperty)
  132. }
  133. layoutSelectView.updateMenuItemsArr(menuItemArr)
  134. layoutSelectView.delegate = self
  135. }
  136. zoomSelectView.properties = ComponentSelectProperties(size: .s,
  137. state: .normal,
  138. text: KMLocalizedString("Adaptation Width"))
  139. if true {
  140. zoomSelectView.updateMenuItemsArr(KMPDFToolbarConfig.scaleZoomItems())
  141. zoomSelectView.delegate = self
  142. }
  143. }
  144. func setUpLeftSidePanel() {
  145. //Left Side Panel
  146. defaultOpenRadio.properties = ComponentCheckBoxProperty(size: .s,
  147. state: .normal,
  148. isDisabled: false,
  149. showhelp: false,
  150. text: KMLocalizedString("Default Open"),
  151. checkboxType: .normal)
  152. defaultOpenRadioWidthConst.constant = defaultOpenRadio.properties.propertyInfo.viewWidth
  153. defaultOpenSelectView.properties = ComponentSelectProperties(size: .s,
  154. state: .normal,
  155. isDisabled: false,
  156. isError: false,
  157. leftIcon: false,
  158. placeholder: nil,
  159. errorText: nil,
  160. creatable: false,
  161. text: KMLocalizedString("Thumbnail"))
  162. if true {
  163. var menuItemArr: [ComponentMenuitemProperty] = []
  164. let ThumbnailProperty: ComponentMenuitemProperty = ComponentMenuitemProperty(multipleSelect: false, itemSelected: false, isDisabled: false, keyEquivalent: nil, text: "Thumbnail")
  165. menuItemArr.append(ThumbnailProperty)
  166. let OutlineProperty: ComponentMenuitemProperty = ComponentMenuitemProperty(multipleSelect: false, itemSelected: false, isDisabled: false, keyEquivalent: nil, text: "Outline")
  167. menuItemArr.append(OutlineProperty)
  168. let BookmarkProperty: ComponentMenuitemProperty = ComponentMenuitemProperty(multipleSelect: false, itemSelected: false, isDisabled: false, keyEquivalent: nil, text: "Bookmark")
  169. menuItemArr.append(BookmarkProperty)
  170. let AnnotationProperty: ComponentMenuitemProperty = ComponentMenuitemProperty(multipleSelect: false, itemSelected: false, isDisabled: false, keyEquivalent: nil, text: "Annotation")
  171. menuItemArr.append(AnnotationProperty)
  172. defaultOpenSelectView.updateMenuItemsArr(menuItemArr)
  173. defaultOpenSelectView.delegate = self
  174. }
  175. remeberLastRadio.properties = ComponentCheckBoxProperty(size: .s,
  176. state: .normal,
  177. isDisabled: false,
  178. showhelp: false,
  179. text: KMLocalizedString("Same as the last open"),
  180. checkboxType: .normal)
  181. remeberLastRadioWidthConst.constant = remeberLastRadio.properties.propertyInfo.viewWidth
  182. hideLeftSideRadio.properties = ComponentCheckBoxProperty(size: .s,
  183. state: .normal,
  184. isDisabled: false,
  185. showhelp: false,
  186. text: KMLocalizedString("Hide left side panel"),
  187. checkboxType: .normal)
  188. hideLeftSideRadioWidthConst.constant = hideLeftSideRadio.properties.propertyInfo.viewWidth
  189. prioritizeOutlineRadio.properties = ComponentCheckBoxProperty(size: .s,
  190. state: .normal,
  191. isDisabled: false,
  192. showhelp: false,
  193. text: KMLocalizedString("Prioritize open the outline list when outlines are available"),
  194. checkboxType: .normal)
  195. prioritizeOutlineCheckboxWidthConst.constant = prioritizeOutlineRadio.properties.propertyInfo.viewWidth
  196. defaultOpenRadio.setTarget(self, action: #selector(leftSidePanelAction(_:)))
  197. remeberLastRadio.setTarget(self, action: #selector(leftSidePanelAction(_:)))
  198. hideLeftSideRadio.setTarget(self, action: #selector(leftSidePanelAction(_:)))
  199. prioritizeOutlineRadio.setTarget(self, action: #selector(leftSidePanelAction(_:)))
  200. }
  201. func setUpPropertyPanel() {
  202. //Property Panel
  203. autoExpandPropertyPanelCheckbox.properties = ComponentCheckBoxProperty(size: .s,
  204. state: .normal,
  205. isDisabled: false,
  206. showhelp: false,
  207. text: KMLocalizedString("Automatically expand the properties panel"),
  208. checkboxType: SettingsManager.sharedInstance.showQuickActionBar ? .selected : .normal)
  209. autoExpandPropertyPanelCheckbox.setTarget(self, action: #selector(propertyPanelAction(_:)))
  210. propertyPanelWidthConst.constant = autoExpandPropertyPanelCheckbox.properties.propertyInfo.viewWidth
  211. showQuickCheckbox.properties = ComponentCheckBoxProperty(size: .s,
  212. state: .normal,
  213. isDisabled: false,
  214. showhelp: false,
  215. text: KMLocalizedString("Always show quick action bar"),
  216. checkboxType: SettingsManager.sharedInstance.showQuickActionBar ? .selected : .normal)
  217. showQuickCheckbox.setTarget(self, action: #selector(propertyPanelAction(_:)))
  218. showQuickCheckboxWidthConst.constant = showQuickCheckbox.properties.propertyInfo.viewWidth
  219. }
  220. func setUpHighlight() {
  221. //Highlight
  222. highlightLinkCheckbox.properties = ComponentCheckBoxProperty(size: .s,
  223. state: .normal,
  224. isDisabled: false,
  225. showhelp: false,
  226. text: KMLocalizedString("Highlight Link"),
  227. checkboxType: .normal)
  228. highlightLinkBoxWidthConst.constant = highlightLinkCheckbox.properties.propertyInfo.viewWidth
  229. highlightFormCheckbox.properties = ComponentCheckBoxProperty(size: .s,
  230. state: .normal,
  231. isDisabled: false,
  232. showhelp: false,
  233. text: KMLocalizedString("Highlight Form Field"),
  234. checkboxType: .normal)
  235. highglightFormboxWidthConst.constant = highlightFormCheckbox.properties.propertyInfo.viewWidth
  236. highlightLinkCheckbox.setTarget(self, action: #selector(highlightAction(_:)))
  237. highlightFormCheckbox.setTarget(self, action: #selector(highlightAction(_:)))
  238. }
  239. func setUpAutoScroll() {
  240. autoTimeSlider.properties = ComponentSliderProperty(size: .m, percent: 1)
  241. autoTimeSlider.delegate = self
  242. autoTimeSelect.properties = ComponentSelectProperties(size: .s,
  243. state: .normal,
  244. text: KMLocalizedString("5"))
  245. if true {
  246. var menuItemArr: [ComponentMenuitemProperty] = []
  247. for string in ["5", "10", "20", "40", "50", "100"] {
  248. var itemProperty: ComponentMenuitemProperty = ComponentMenuitemProperty(multipleSelect: false,
  249. itemSelected: false,
  250. isDisabled: false,
  251. keyEquivalent: nil,
  252. text: KMLocalizedString(string))
  253. if string == " " {
  254. itemProperty = ComponentMenuitemProperty.divider()
  255. }
  256. menuItemArr.append(itemProperty)
  257. }
  258. autoTimeSelect.updateMenuItemsArr(menuItemArr)
  259. autoTimeSelect.delegate = self
  260. }
  261. jumpSpaceSlider.properties = ComponentSliderProperty(size: .m, percent: 1)
  262. jumpSpaceSlider.delegate = self
  263. jumpSpaceSelect.properties = ComponentSelectProperties(size: .s,
  264. state: .normal,
  265. text: KMLocalizedString("20"))
  266. if true {
  267. var menuItemArr: [ComponentMenuitemProperty] = []
  268. for string in ["10", "50", "100", "200", "500", "1000"] {
  269. var itemProperty: ComponentMenuitemProperty = ComponentMenuitemProperty(multipleSelect: false,
  270. itemSelected: false,
  271. isDisabled: false,
  272. keyEquivalent: nil,
  273. text: KMLocalizedString(string))
  274. if string == " " {
  275. itemProperty = ComponentMenuitemProperty.divider()
  276. }
  277. menuItemArr.append(itemProperty)
  278. }
  279. jumpSpaceSelect.updateMenuItemsArr(menuItemArr)
  280. jumpSpaceSelect.delegate = self
  281. }
  282. }
  283. func reloadData() {
  284. //Default Layout and Zoom
  285. layoutSelectView.selectItemAtIndex(SettingsManager.sharedInstance.layoutType.rawValue)
  286. zoomSelectView.selectItemAtIndex(SettingsManager.sharedInstance.zoomType.rawValue)
  287. //Left Side Panel
  288. defaultOpenRadio.properties.checkboxType = .normal
  289. remeberLastRadio.properties.checkboxType = .normal
  290. hideLeftSideRadio.properties.checkboxType = .normal
  291. prioritizeOutlineRadio.properties.checkboxType = .normal
  292. defaultOpenSelectView.properties.isDisabled = true
  293. if SettingsManager.sharedInstance.leftPanelType == .defaultOpen {
  294. defaultOpenSelectView.properties.isDisabled = false
  295. defaultOpenRadio.properties.checkboxType = .selected
  296. } else if SettingsManager.sharedInstance.leftPanelType == .sameAsLastOpen {
  297. remeberLastRadio.properties.checkboxType = .selected
  298. } else if SettingsManager.sharedInstance.leftPanelType == .hideLeftSide {
  299. hideLeftSideRadio.properties.checkboxType = .selected
  300. } else if SettingsManager.sharedInstance.leftPanelType == .prioritizeOutline {
  301. prioritizeOutlineRadio.properties.checkboxType = .selected
  302. }
  303. defaultOpenRadio.reloadData()
  304. remeberLastRadio.reloadData()
  305. hideLeftSideRadio.reloadData()
  306. prioritizeOutlineRadio.reloadData()
  307. defaultOpenSelectView.selectItemAtIndex(SettingsManager.sharedInstance.defaultOpen.rawValue)
  308. defaultOpenSelectView.reloadData()
  309. //Property Panel
  310. autoExpandPropertyPanelCheckbox.properties.checkboxType = SettingsManager.sharedInstance.autoExpandPropertyPanel ? .selected : .normal
  311. autoExpandPropertyPanelCheckbox.reloadData()
  312. showQuickCheckbox.properties.checkboxType = SettingsManager.sharedInstance.showQuickActionBar ? .selected : .normal
  313. showQuickCheckbox.reloadData()
  314. //Highlight
  315. highlightLinkCheckbox.properties.checkboxType = CPDFKitConfig.sharedInstance().enableLinkFieldHighlight() ? .selected : .normal
  316. highlightLinkCheckbox.reloadData()
  317. highlightFormCheckbox.properties.checkboxType = CPDFKitConfig.sharedInstance().enableFormFieldHighlight() ? .selected : .normal
  318. highlightFormCheckbox.reloadData()
  319. //Auto Scroll
  320. let autoScrollTime: CGFloat = SettingsManager.sharedInstance.autoScrollTimeInterval
  321. autoTimeSlider.properties.percent = (autoScrollTime - 1) / (100 - 1)
  322. autoTimeSlider.reloadData()
  323. autoTimeSelect.properties.text = String(format: "%.0f", autoScrollTime, "%")
  324. autoTimeSelect.reloadData()
  325. let autoScrollJump: CGFloat = SettingsManager.sharedInstance.autoScrollJumpSpace
  326. jumpSpaceSlider.properties.percent = (autoScrollJump - 10) / (1000 - 10)
  327. jumpSpaceSlider.reloadData()
  328. jumpSpaceSelect.properties.text = String(format: "%.0f", autoScrollJump, "%")
  329. jumpSpaceSelect.reloadData()
  330. }
  331. //MARK: - Action
  332. @objc func leftSidePanelAction(_ sender: NSView) {
  333. if sender == defaultOpenRadio {
  334. SettingsManager.sharedInstance.leftPanelType = .defaultOpen
  335. } else if sender == remeberLastRadio {
  336. SettingsManager.sharedInstance.leftPanelType = .sameAsLastOpen
  337. } else if sender == hideLeftSideRadio {
  338. SettingsManager.sharedInstance.leftPanelType = .hideLeftSide
  339. } else if sender == prioritizeOutlineRadio {
  340. SettingsManager.sharedInstance.leftPanelType = .prioritizeOutline
  341. }
  342. self.reloadData()
  343. }
  344. @objc func propertyPanelAction(_ sender: NSView) {
  345. if sender == showQuickCheckbox {
  346. SettingsManager.sharedInstance.showQuickActionBar = showQuickCheckbox.properties.checkboxType == .selected ? true : false
  347. } else if sender == autoExpandPropertyPanelCheckbox {
  348. SettingsManager.sharedInstance.autoExpandPropertyPanel = autoExpandPropertyPanelCheckbox.properties.checkboxType == .selected ? true : false
  349. }
  350. self.reloadData()
  351. }
  352. @objc func highlightAction(_ sender: NSView) {
  353. if sender == highlightLinkCheckbox {
  354. let value = highlightLinkCheckbox.properties.checkboxType == .selected ? true : false
  355. CPDFAnnotation.updateLinkFieldHighlight(nil, linkFieldHighlight: value)
  356. NotificationCenter.default.post(name: kPDFViewHighlightLinkUpdateNotiName, object: nil)
  357. } else if sender == highlightFormCheckbox {
  358. let value = highlightFormCheckbox.properties.checkboxType == .selected ? true : false
  359. CPDFAnnotation.updateHighlightFormFiled(nil, highlightFormFiled: value)
  360. NotificationCenter.default.post(name: kPDFViewHighlightFormFiledUpdateNotiName, object: nil)
  361. }
  362. self.reloadData()
  363. }
  364. }
  365. //MARK: - ComponentSelectDelegate
  366. extension SettingsDisplayView: ComponentSelectDelegate {
  367. func componentSelectDidSelect(view: ComponentSelect?, menuItemProperty: ComponentMenuitemProperty?) {
  368. if view == layoutSelectView {
  369. if layoutSelectView.indexOfSelect() >= 0 {
  370. SettingsManager.sharedInstance.layoutType = pageLayoutType(rawValue: layoutSelectView.indexOfSelect())!
  371. }
  372. } else if view == zoomSelectView {
  373. if zoomSelectView.indexOfSelect() >= 0 {
  374. SettingsManager.sharedInstance.zoomType = zoomInfoType(rawValue: zoomSelectView.indexOfSelect())!
  375. }
  376. } else if view == defaultOpenSelectView {
  377. if defaultOpenSelectView.indexOfSelect() >= 0 {
  378. SettingsManager.sharedInstance.defaultOpen = defaultOpenType(rawValue: defaultOpenSelectView.indexOfSelect())!
  379. }
  380. }
  381. SettingsManager.sharedInstance.saveData()
  382. self.reloadData()
  383. }
  384. }
  385. //MARK: - ComponentSliderDelegate
  386. extension SettingsDisplayView: ComponentSliderDelegate {
  387. func componentSliderDidUpdate(_ view: ComponentSlider) {
  388. let value = view.properties.percent
  389. if view == autoTimeSlider {
  390. SettingsManager.sharedInstance.autoScrollTimeInterval = 1 + (100 - 1)*value
  391. } else if view == jumpSpaceSlider {
  392. SettingsManager.sharedInstance.autoScrollJumpSpace = 10 + (1000 - 10)*value
  393. }
  394. reloadData()
  395. }
  396. }