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: true,
  207. text: KMLocalizedString("Automatically expand the properties panel"),
  208. checkboxType: SettingsManager.sharedInstance.showQuickActionBar ? .selected : .normal)
  209. autoExpandPropertyPanelCheckbox.setTarget(self, action: #selector(propertyPanelAction(_:)))
  210. autoExpandPropertyPanelCheckbox.toolTip = KMLocalizedString("If you don't want the automatic expansion mode to interfere with your immersive reading experience, you can turn this option off. You can still click the \"Properties\" icon to expand the panel when you need it (⌥⌘0).")
  211. propertyPanelWidthConst.constant = autoExpandPropertyPanelCheckbox.properties.propertyInfo.viewWidth
  212. showQuickCheckbox.properties = ComponentCheckBoxProperty(size: .s,
  213. state: .normal,
  214. isDisabled: false,
  215. showhelp: false,
  216. text: KMLocalizedString("Always show quick action bar"),
  217. checkboxType: SettingsManager.sharedInstance.showQuickActionBar ? .selected : .normal)
  218. showQuickCheckbox.setTarget(self, action: #selector(propertyPanelAction(_:)))
  219. showQuickCheckboxWidthConst.constant = showQuickCheckbox.properties.propertyInfo.viewWidth
  220. }
  221. func setUpHighlight() {
  222. //Highlight
  223. highlightLinkCheckbox.properties = ComponentCheckBoxProperty(size: .s,
  224. state: .normal,
  225. isDisabled: false,
  226. showhelp: false,
  227. text: KMLocalizedString("Highlight Link"),
  228. checkboxType: .normal)
  229. highlightLinkBoxWidthConst.constant = highlightLinkCheckbox.properties.propertyInfo.viewWidth
  230. highlightFormCheckbox.properties = ComponentCheckBoxProperty(size: .s,
  231. state: .normal,
  232. isDisabled: false,
  233. showhelp: false,
  234. text: KMLocalizedString("Highlight Form Field"),
  235. checkboxType: .normal)
  236. highglightFormboxWidthConst.constant = highlightFormCheckbox.properties.propertyInfo.viewWidth
  237. highlightLinkCheckbox.setTarget(self, action: #selector(highlightAction(_:)))
  238. highlightFormCheckbox.setTarget(self, action: #selector(highlightAction(_:)))
  239. }
  240. func setUpAutoScroll() {
  241. autoTimeSlider.properties = ComponentSliderProperty(size: .m, percent: 1)
  242. autoTimeSlider.delegate = self
  243. autoTimeSelect.properties = ComponentSelectProperties(size: .s,
  244. state: .normal,
  245. text: KMLocalizedString("5"))
  246. if true {
  247. var menuItemArr: [ComponentMenuitemProperty] = []
  248. for string in ["5", "10", "20", "40", "50", "100"] {
  249. var itemProperty: ComponentMenuitemProperty = ComponentMenuitemProperty(multipleSelect: false,
  250. itemSelected: false,
  251. isDisabled: false,
  252. keyEquivalent: nil,
  253. text: KMLocalizedString(string))
  254. if string == " " {
  255. itemProperty = ComponentMenuitemProperty.divider()
  256. }
  257. menuItemArr.append(itemProperty)
  258. }
  259. autoTimeSelect.updateMenuItemsArr(menuItemArr)
  260. autoTimeSelect.delegate = self
  261. }
  262. jumpSpaceSlider.properties = ComponentSliderProperty(size: .m, percent: 1)
  263. jumpSpaceSlider.delegate = self
  264. jumpSpaceSelect.properties = ComponentSelectProperties(size: .s,
  265. state: .normal,
  266. text: KMLocalizedString("20"))
  267. if true {
  268. var menuItemArr: [ComponentMenuitemProperty] = []
  269. for string in ["10", "50", "100", "200", "500", "1000"] {
  270. var itemProperty: ComponentMenuitemProperty = ComponentMenuitemProperty(multipleSelect: false,
  271. itemSelected: false,
  272. isDisabled: false,
  273. keyEquivalent: nil,
  274. text: KMLocalizedString(string))
  275. if string == " " {
  276. itemProperty = ComponentMenuitemProperty.divider()
  277. }
  278. menuItemArr.append(itemProperty)
  279. }
  280. jumpSpaceSelect.updateMenuItemsArr(menuItemArr)
  281. jumpSpaceSelect.delegate = self
  282. }
  283. }
  284. func reloadData() {
  285. //Default Layout and Zoom
  286. layoutSelectView.selectItemAtIndex(SettingsManager.sharedInstance.layoutType.rawValue)
  287. zoomSelectView.selectItemAtIndex(SettingsManager.sharedInstance.zoomType.rawValue)
  288. //Left Side Panel
  289. defaultOpenRadio.properties.checkboxType = .normal
  290. remeberLastRadio.properties.checkboxType = .normal
  291. hideLeftSideRadio.properties.checkboxType = .normal
  292. prioritizeOutlineRadio.properties.checkboxType = .normal
  293. defaultOpenSelectView.properties.isDisabled = true
  294. if SettingsManager.sharedInstance.leftPanelType == .defaultOpen {
  295. defaultOpenSelectView.properties.isDisabled = false
  296. defaultOpenRadio.properties.checkboxType = .selected
  297. } else if SettingsManager.sharedInstance.leftPanelType == .sameAsLastOpen {
  298. remeberLastRadio.properties.checkboxType = .selected
  299. } else if SettingsManager.sharedInstance.leftPanelType == .hideLeftSide {
  300. hideLeftSideRadio.properties.checkboxType = .selected
  301. } else if SettingsManager.sharedInstance.leftPanelType == .prioritizeOutline {
  302. prioritizeOutlineRadio.properties.checkboxType = .selected
  303. }
  304. defaultOpenRadio.reloadData()
  305. remeberLastRadio.reloadData()
  306. hideLeftSideRadio.reloadData()
  307. prioritizeOutlineRadio.reloadData()
  308. defaultOpenSelectView.selectItemAtIndex(SettingsManager.sharedInstance.defaultOpen.rawValue)
  309. defaultOpenSelectView.reloadData()
  310. //Property Panel
  311. autoExpandPropertyPanelCheckbox.properties.checkboxType = SettingsManager.sharedInstance.autoExpandPropertyPanel ? .selected : .normal
  312. autoExpandPropertyPanelCheckbox.reloadData()
  313. showQuickCheckbox.properties.checkboxType = SettingsManager.sharedInstance.showQuickActionBar ? .selected : .normal
  314. showQuickCheckbox.reloadData()
  315. //Highlight
  316. highlightLinkCheckbox.properties.checkboxType = CPDFKitConfig.sharedInstance().enableLinkFieldHighlight() ? .selected : .normal
  317. highlightLinkCheckbox.reloadData()
  318. highlightFormCheckbox.properties.checkboxType = CPDFKitConfig.sharedInstance().enableFormFieldHighlight() ? .selected : .normal
  319. highlightFormCheckbox.reloadData()
  320. //Auto Scroll
  321. let autoScrollTime: CGFloat = SettingsManager.sharedInstance.autoScrollTimeInterval
  322. autoTimeSlider.properties.percent = (autoScrollTime - 1) / (100 - 1)
  323. autoTimeSlider.reloadData()
  324. autoTimeSelect.properties.text = String(format: "%.0f", autoScrollTime, "%")
  325. autoTimeSelect.reloadData()
  326. let autoScrollJump: CGFloat = SettingsManager.sharedInstance.autoScrollJumpSpace
  327. jumpSpaceSlider.properties.percent = (autoScrollJump - 10) / (1000 - 10)
  328. jumpSpaceSlider.reloadData()
  329. jumpSpaceSelect.properties.text = String(format: "%.0f", autoScrollJump, "%")
  330. jumpSpaceSelect.reloadData()
  331. }
  332. //MARK: - Action
  333. @objc func leftSidePanelAction(_ sender: NSView) {
  334. if sender == defaultOpenRadio {
  335. SettingsManager.sharedInstance.leftPanelType = .defaultOpen
  336. } else if sender == remeberLastRadio {
  337. SettingsManager.sharedInstance.leftPanelType = .sameAsLastOpen
  338. } else if sender == hideLeftSideRadio {
  339. SettingsManager.sharedInstance.leftPanelType = .hideLeftSide
  340. } else if sender == prioritizeOutlineRadio {
  341. SettingsManager.sharedInstance.leftPanelType = .prioritizeOutline
  342. }
  343. self.reloadData()
  344. }
  345. @objc func propertyPanelAction(_ sender: NSView) {
  346. if sender == showQuickCheckbox {
  347. SettingsManager.sharedInstance.showQuickActionBar = showQuickCheckbox.properties.checkboxType == .selected ? true : false
  348. } else if sender == autoExpandPropertyPanelCheckbox {
  349. SettingsManager.sharedInstance.autoExpandPropertyPanel = autoExpandPropertyPanelCheckbox.properties.checkboxType == .selected ? true : false
  350. }
  351. self.reloadData()
  352. }
  353. @objc func highlightAction(_ sender: NSView) {
  354. if sender == highlightLinkCheckbox {
  355. let value = highlightLinkCheckbox.properties.checkboxType == .selected ? true : false
  356. CPDFAnnotation.updateLinkFieldHighlight(nil, linkFieldHighlight: value)
  357. NotificationCenter.default.post(name: kPDFViewHighlightLinkUpdateNotiName, object: nil)
  358. } else if sender == highlightFormCheckbox {
  359. let value = highlightFormCheckbox.properties.checkboxType == .selected ? true : false
  360. CPDFAnnotation.updateHighlightFormFiled(nil, highlightFormFiled: value)
  361. NotificationCenter.default.post(name: kPDFViewHighlightFormFiledUpdateNotiName, object: nil)
  362. }
  363. self.reloadData()
  364. }
  365. }
  366. //MARK: - ComponentSelectDelegate
  367. extension SettingsDisplayView: ComponentSelectDelegate {
  368. func componentSelectDidSelect(view: ComponentSelect?, menuItemProperty: ComponentMenuitemProperty?) {
  369. if view == layoutSelectView {
  370. if layoutSelectView.indexOfSelect() >= 0 {
  371. SettingsManager.sharedInstance.layoutType = pageLayoutType(rawValue: layoutSelectView.indexOfSelect())!
  372. }
  373. } else if view == zoomSelectView {
  374. if zoomSelectView.indexOfSelect() >= 0 {
  375. SettingsManager.sharedInstance.zoomType = zoomInfoType(rawValue: zoomSelectView.indexOfSelect())!
  376. }
  377. } else if view == defaultOpenSelectView {
  378. if defaultOpenSelectView.indexOfSelect() >= 0 {
  379. SettingsManager.sharedInstance.defaultOpen = defaultOpenType(rawValue: defaultOpenSelectView.indexOfSelect())!
  380. }
  381. }
  382. SettingsManager.sharedInstance.saveData()
  383. self.reloadData()
  384. }
  385. }
  386. //MARK: - ComponentSliderDelegate
  387. extension SettingsDisplayView: ComponentSliderDelegate {
  388. func componentSliderDidUpdate(_ view: ComponentSlider) {
  389. let value = view.properties.percent
  390. if view == autoTimeSlider {
  391. SettingsManager.sharedInstance.autoScrollTimeInterval = 1 + (100 - 1)*value
  392. } else if view == jumpSpaceSlider {
  393. SettingsManager.sharedInstance.autoScrollJumpSpace = 10 + (1000 - 10)*value
  394. }
  395. reloadData()
  396. }
  397. }