KMNDisplayViewController.swift 30 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746
  1. //
  2. // KMNDisplayViewController.swift
  3. // PDF Reader Pro
  4. //
  5. // Created by Niehaoyu on 2024/10/27.
  6. //
  7. import Cocoa
  8. import KMComponentLibrary
  9. @objc protocol KMNDisplayViewControllerDelegate: AnyObject {
  10. //Display Mode
  11. @objc optional func displayViewControllerDidDisplayModeChanged(_ controller: KMNDisplayViewController)
  12. //阅读模式
  13. @objc optional func displayViewControllerDidReadModeUpdated(_ controller: KMNDisplayViewController)
  14. //PPT
  15. @objc optional func displayViewControllerDidGotoSlideShow(_ controller: KMNDisplayViewController)
  16. //SplitView
  17. @objc optional func displayViewControllerDidSplitModeChanged(_ controller: KMNDisplayViewController)
  18. //SplitView
  19. @objc optional func displayViewControllerDidSplitFileChanged(_ controller: KMNDisplayViewController)
  20. //SplitView-showToolbar
  21. @objc optional func displayViewControllerDidToolbarStateChanged(_ controller: KMNDisplayViewController)
  22. }
  23. class KMNDisplayViewController: NSViewController {
  24. @IBOutlet var scrollView: NSScrollView!
  25. @IBOutlet var contendBox: NSBox!
  26. @IBOutlet var titleLabel: NSTextField!
  27. @IBOutlet var displayModeView: NSView!
  28. @IBOutlet var displayModeLabel: NSTextField!
  29. @IBOutlet var singlePageSelector: ComponentCSelector!
  30. @IBOutlet var twoPageSelector: ComponentCSelector!
  31. @IBOutlet var bookSelector: ComponentCSelector!
  32. @IBOutlet var readModeSelector: ComponentCSelector!
  33. @IBOutlet var fullScreenSelector: ComponentCSelector!
  34. @IBOutlet var slideShowSelector: ComponentCSelector!
  35. @IBOutlet var continueScrollCheckBox: ComponentCheckBox!
  36. @IBOutlet var pageBreaksCheckBox: ComponentCheckBox!
  37. //Split
  38. @IBOutlet var splitViewBGView: NSView!
  39. @IBOutlet var splitViewLabel: NSTextField!
  40. @IBOutlet var noSplitSelector: ComponentCSelector!
  41. @IBOutlet var verticalSelector: ComponentCSelector!
  42. @IBOutlet var horizontalSelector: ComponentCSelector!
  43. @IBOutlet var split_selFileButton: ComponentButton!
  44. @IBOutlet var split_fileInfoView: NSView!
  45. @IBOutlet var split_fileLabel: NSTextField!
  46. @IBOutlet var split_syncScrollCheckbox: ComponentCheckBox!
  47. @IBOutlet var split_showToolbarCheckbox: ComponentCheckBox!
  48. @IBOutlet var splitViewBGHeightConst: NSLayoutConstraint!
  49. @IBOutlet var split_SyncScrollTopConst: NSLayoutConstraint!
  50. //Themes
  51. @IBOutlet var themesBGView: NSView!
  52. @IBOutlet var themesLabel: NSTextField!
  53. @IBOutlet var themesContendView: NSView!
  54. @IBOutlet var scrollViewHeightConst: NSLayoutConstraint!
  55. @IBOutlet var boxHeightConst: NSLayoutConstraint!
  56. private var themesArray: [NSColor] = []
  57. private var themeEditItem: ComponentCColorItem?
  58. var isCustomSelected = false //防止多个相同的自定义颜色item,出现同时选中的情况,
  59. var pdfView: CPDFListView?
  60. var viewManager: KMPDFViewManager?
  61. weak open var delegate: KMNDisplayViewControllerDelegate?
  62. deinit {
  63. print("\n\(self.className) deinit.\n")
  64. }
  65. override func viewWillLayout() {
  66. super.viewWillLayout()
  67. if pdfView?.viewSplitMode == .disable {
  68. //502
  69. scrollViewHeightConst.constant = min(boxHeightConst.constant+5, self.view.frame.size.height)
  70. } else {
  71. if let _ = viewManager?.splitPDFFileURL {
  72. //614
  73. scrollViewHeightConst.constant = min(boxHeightConst.constant+5, self.view.frame.size.height)
  74. } else {
  75. scrollViewHeightConst.constant = min(boxHeightConst.constant+5, self.view.frame.size.height)
  76. }
  77. }
  78. if view.window?.styleMask.contains(.fullScreen) == true {
  79. fullScreenSelector.properties.state = .pressed
  80. } else {
  81. fullScreenSelector.properties.state = .normal
  82. }
  83. fullScreenSelector.reloadData()
  84. }
  85. override func viewDidLoad() {
  86. super.viewDidLoad()
  87. // Do view setup here.
  88. scrollView.documentView = contendBox
  89. scrollView.backgroundColor = NSColor.clear
  90. setupPropertys()
  91. reloadData()
  92. }
  93. func setupPropertys() {
  94. view.wantsLayer = true
  95. view.layer?.backgroundColor = ComponentLibrary.shared.getComponentColorFromKey("colorBg/layout-middle").cgColor
  96. titleLabel.textColor = ComponentLibrary.shared.getComponentColorFromKey("colorText/2")
  97. titleLabel.font = ComponentLibrary.shared.getFontFromKey("mac/body-m-bold")
  98. titleLabel.stringValue = KMLocalizedString("View")
  99. displayModeLabel.stringValue = KMLocalizedString("Display Mode")
  100. displayModeLabel.textColor = ComponentLibrary.shared.getComponentColorFromKey("colorText/2")
  101. displayModeLabel.font = ComponentLibrary.shared.getFontFromKey("mac/body-s-medium")
  102. singlePageSelector.properties = ComponentCSelectorProperty.init(size: .m, state: .normal, text: KMLocalizedString("Single Page"), iconImage: NSImage(named: "display_singlePage"))
  103. twoPageSelector.properties = ComponentCSelectorProperty.init(size: .m, state: .normal, text: KMLocalizedString("Two Page"), iconImage: NSImage(named: "display_twoPage"))
  104. bookSelector.properties = ComponentCSelectorProperty.init(size: .m, state: .normal, text: KMLocalizedString("Book"), iconImage: NSImage(named: "display_book"))
  105. readModeSelector.properties = ComponentCSelectorProperty.init(size: .m, state: .normal, text: KMLocalizedString("Read Mode"), iconImage: NSImage(named: "display_readMode"))
  106. fullScreenSelector.properties = ComponentCSelectorProperty.init(size: .m, state: .normal, text: KMLocalizedString("Full Screen"), iconImage: NSImage(named: "display_fullscreen"))
  107. slideShowSelector.properties = ComponentCSelectorProperty.init(size: .m, state: .normal, text: KMLocalizedString("Slide Show"), iconImage: NSImage(named: "display_slideShow"))
  108. singlePageSelector.setTarget(self, action: #selector(selectorClicked(_:)))
  109. twoPageSelector.setTarget(self, action: #selector(selectorClicked(_:)))
  110. bookSelector.setTarget(self, action: #selector(selectorClicked(_:)))
  111. readModeSelector.setTarget(self, action: #selector(selectorClicked(_:)))
  112. fullScreenSelector.setTarget(self, action: #selector(selectorClicked(_:)))
  113. slideShowSelector.setTarget(self, action: #selector(selectorClicked(_:)))
  114. continueScrollCheckBox.properties = ComponentCheckBoxProperty(size: .s,
  115. state: .normal,
  116. text: KMLocalizedString("Continuous Scroll"),
  117. checkboxType: .normal)
  118. pageBreaksCheckBox.properties = ComponentCheckBoxProperty(size: .s,
  119. state: .normal,
  120. text: KMLocalizedString("Page Breaks"),
  121. checkboxType: .normal)
  122. continueScrollCheckBox.setTarget(self, action: #selector(checkBoxClicked(_:)))
  123. pageBreaksCheckBox.setTarget(self, action: #selector(checkBoxClicked(_:)))
  124. //Split
  125. splitViewLabel.stringValue = KMLocalizedString("Split View")
  126. splitViewLabel.textColor = ComponentLibrary.shared.getComponentColorFromKey("colorText/2")
  127. splitViewLabel.font = ComponentLibrary.shared.getFontFromKey("mac/body-s-medium")
  128. noSplitSelector.properties = ComponentCSelectorProperty.init(size: .m, state: .normal, text: KMLocalizedString("No Split"), iconImage: NSImage(named: "display_noSplit"))
  129. verticalSelector.properties = ComponentCSelectorProperty.init(size: .m, state: .normal, text: KMLocalizedString("Vertical"), iconImage: NSImage(named: "display_verticalSplit"))
  130. horizontalSelector.properties = ComponentCSelectorProperty.init(size: .m, state: .normal, text: KMLocalizedString("Horizontal"), iconImage: NSImage(named: "display_horizontalSplit"))
  131. noSplitSelector.setTarget(self, action: #selector(selectorClicked(_:)))
  132. verticalSelector.setTarget(self, action: #selector(selectorClicked(_:)))
  133. horizontalSelector.setTarget(self, action: #selector(selectorClicked(_:)))
  134. split_selFileButton.properties = ComponentButtonProperty(type: .default_tertiary, size: .s, buttonText: KMLocalizedString("Select File"), keepPressState: false)
  135. split_selFileButton.setTarget(self, action: #selector(splitSelFileButtonClicked(_:)))
  136. split_fileInfoView.wantsLayer = true
  137. split_fileInfoView.layer?.borderWidth = 1
  138. if let value = ComponentLibrary.shared.getComponentValueFromKey("radius/xs") {
  139. let currentValue = value as! CGFloat
  140. split_fileInfoView.layer?.cornerRadius = currentValue
  141. }
  142. split_fileInfoView.layer?.borderColor = ComponentLibrary.shared.getComponentColorFromKey("colorBorder/4").cgColor
  143. split_fileInfoView.layer?.backgroundColor = ComponentLibrary.shared.getComponentColorFromKey("colorFill/4").cgColor
  144. split_syncScrollCheckbox.properties = ComponentCheckBoxProperty(size: .s,
  145. state: .normal,
  146. text: KMLocalizedString("Sync Scroll and Zoom"),
  147. checkboxType: .normal)
  148. split_showToolbarCheckbox.properties = ComponentCheckBoxProperty(size: .s,
  149. state: .normal,
  150. text: KMLocalizedString("Show Bottom Toolbar"),
  151. checkboxType: .normal)
  152. split_syncScrollCheckbox.setTarget(self, action: #selector(checkBoxClicked(_:)))
  153. split_showToolbarCheckbox.setTarget(self, action: #selector(checkBoxClicked(_:)))
  154. //Themes
  155. themesLabel.stringValue = KMLocalizedString("Themes")
  156. themesLabel.textColor = ComponentLibrary.shared.getComponentColorFromKey("colorText/2")
  157. themesLabel.font = ComponentLibrary.shared.getFontFromKey("mac/body-s-medium")
  158. }
  159. func reloadData() {
  160. let mode = pdfView?.displayMode() ?? .singlePage
  161. let book = pdfView?.displaysAsBook ?? false
  162. singlePageSelector.properties.state = .normal
  163. twoPageSelector.properties.state = .normal
  164. bookSelector.properties.state = .normal
  165. readModeSelector.properties.state = .normal
  166. fullScreenSelector.properties.state = .normal
  167. slideShowSelector.properties.state = .normal
  168. bookSelector.properties.state = .normal
  169. if mode == .singlePage {
  170. singlePageSelector.properties.state = .pressed
  171. continueScrollCheckBox.properties.checkboxType = .normal
  172. } else if mode == .singlePageContinuous {
  173. singlePageSelector.properties.state = .pressed
  174. continueScrollCheckBox.properties.checkboxType = .selected
  175. } else if mode == .twoUp {
  176. bookSelector.properties.state = book ? .pressed : .normal
  177. twoPageSelector.properties.state = book ? .normal : .pressed
  178. continueScrollCheckBox.properties.checkboxType = .normal
  179. } else if mode == .twoUpContinuous {
  180. bookSelector.properties.state = book ? .pressed : .normal
  181. twoPageSelector.properties.state = book ? .normal : .pressed
  182. continueScrollCheckBox.properties.checkboxType = .selected
  183. }
  184. let pageBreaks = pdfView?.displaysPageBreaks ?? false
  185. pageBreaksCheckBox.properties.checkboxType = pageBreaks ? .selected : .normal
  186. singlePageSelector.reloadData()
  187. twoPageSelector.reloadData()
  188. bookSelector.reloadData()
  189. readModeSelector.reloadData()
  190. fullScreenSelector.reloadData()
  191. slideShowSelector.reloadData()
  192. continueScrollCheckBox.reloadData()
  193. pageBreaksCheckBox.reloadData()
  194. //SplitView
  195. noSplitSelector.properties.state = .normal
  196. verticalSelector.properties.state = .normal
  197. horizontalSelector.properties.state = .normal
  198. if pdfView?.viewSplitMode == .disable {
  199. noSplitSelector.properties.state = .pressed
  200. } else if pdfView?.viewSplitMode == .horizontal {
  201. horizontalSelector.properties.state = .pressed
  202. } else if pdfView?.viewSplitMode == .vertical {
  203. verticalSelector.properties.state = .pressed
  204. }
  205. split_syncScrollCheckbox.properties.checkboxType = viewManager?.splitSyncScroll == true ? .selected : .normal
  206. split_showToolbarCheckbox.properties.checkboxType = viewManager?.splitShowBottomBar == true ? .selected : .normal
  207. if let fileUrl = viewManager?.splitPDFFileURL {
  208. split_fileLabel.stringValue = fileUrl.lastPathComponent
  209. }
  210. noSplitSelector.reloadData()
  211. horizontalSelector.reloadData()
  212. verticalSelector.reloadData()
  213. split_syncScrollCheckbox.reloadData()
  214. split_showToolbarCheckbox.reloadData()
  215. refreshSplitView()
  216. //Themes
  217. var data: Data? = nil
  218. if let themesData = UserDefaults.standard.object(forKey: "kmPDFViewModeThemesArray") as? Data {
  219. data = themesData
  220. }
  221. let appArray = NSKeyedUnarchiver.unarchiveObject(with: data ?? Data()) as? [Any] ?? []
  222. let mutableArray = appArray
  223. if mutableArray.count > 0 {
  224. themesArray = mutableArray as! [NSColor]
  225. } else {
  226. let normalColor = NSColor.white
  227. let softColor = NSColor(deviceRed: 245/255.0, green: 237/255.0, blue: 203/255.0, alpha: 1.0)
  228. let nightColor = NSColor.black
  229. let greenColor = NSColor(deviceRed: 201/255.0, green: 228/255.0, blue: 198/255.0, alpha: 1.0)
  230. themesArray = [normalColor, softColor, greenColor, nightColor]
  231. }
  232. setUpThemesItems()
  233. }
  234. //MARK: - Split
  235. func refreshSplitView() {
  236. if pdfView?.viewSplitMode == .disable {
  237. splitViewBGHeightConst.constant = 90
  238. split_selFileButton.isHidden = true
  239. split_fileInfoView.isHidden = true
  240. split_syncScrollCheckbox.isHidden = true
  241. split_showToolbarCheckbox.isHidden = true
  242. } else {
  243. if (viewManager?.splitPDFFileURL) != nil {
  244. splitViewBGHeightConst.constant = 246
  245. split_SyncScrollTopConst.constant = 52
  246. split_selFileButton.isHidden = false
  247. split_fileInfoView.isHidden = false
  248. split_syncScrollCheckbox.isHidden = false
  249. split_showToolbarCheckbox.isHidden = false
  250. } else {
  251. splitViewBGHeightConst.constant = 202
  252. split_SyncScrollTopConst.constant = 8
  253. split_selFileButton.isHidden = false
  254. split_fileInfoView.isHidden = true
  255. split_syncScrollCheckbox.isHidden = false
  256. split_showToolbarCheckbox.isHidden = false
  257. }
  258. }
  259. if pdfView?.viewSplitMode == .disable {
  260. boxHeightConst.constant = 502
  261. scrollViewHeightConst.constant = min(boxHeightConst.constant, self.view.frame.size.height)
  262. } else {
  263. if let _ = viewManager?.splitPDFFileURL {
  264. boxHeightConst.constant = 658 + 12
  265. scrollViewHeightConst.constant = min(boxHeightConst.constant, self.view.frame.size.height)
  266. } else {
  267. boxHeightConst.constant = 614 + 12
  268. scrollViewHeightConst.constant = min(boxHeightConst.constant, self.view.frame.size.height)
  269. }
  270. }
  271. }
  272. //MARK: - Themes
  273. func setUpThemesItems() {
  274. let subviews = themesContendView.subviews
  275. for view in subviews {
  276. if view is ComponentCColorItem {
  277. view.removeFromSuperview()
  278. } else if view is ComponentButton {
  279. view.removeFromSuperview()
  280. }
  281. }
  282. //MAX = 10
  283. var xValue: CGFloat = 0
  284. var yValue = CGRectGetHeight(themesContendView.frame) - 32
  285. for index in 0...themesArray.count-1 {
  286. let color = themesArray[index]
  287. if index%5 == 0 {
  288. xValue = 0
  289. }
  290. if index/5 == 1 {
  291. yValue = 0
  292. }
  293. let colorItem = ComponentCColorItem.init()
  294. colorItem.delegate = self
  295. colorItem.frame = CGRectMake(xValue, yValue, 32, 32)
  296. colorItem.properties = ComponentCColorProperty(colorType: .color, state: .normal, isCustom: false, color: color)
  297. xValue += 32
  298. xValue += 18
  299. if index == 0 {
  300. colorItem.properties?.identifier = "normal"
  301. } else if index == 1 {
  302. colorItem.properties?.identifier = "soft"
  303. } else if index == 2 {
  304. colorItem.properties?.identifier = "night"
  305. } else if index == 3 {
  306. colorItem.properties?.identifier = "green"
  307. } else {
  308. colorItem.properties?.identifier = "custom"
  309. }
  310. if pdfView?.displayMode == .normal && index == 0 {
  311. colorItem.properties?.state = .pressed
  312. } else if pdfView?.displayMode == .soft && index == 1 {
  313. colorItem.properties?.state = .pressed
  314. } else if pdfView?.displayMode == .night && index == 3 {
  315. colorItem.properties?.state = .pressed
  316. } else if pdfView?.displayMode == .green && index == 2 {
  317. colorItem.properties?.state = .pressed
  318. } else if pdfView?.displayMode == .custom && color == pdfView?.displayModeCustomColor && isCustomSelected == false {
  319. colorItem.properties?.state = .pressed
  320. isCustomSelected = true
  321. } else {
  322. colorItem.properties?.state = .normal
  323. }
  324. colorItem.reloadData()
  325. themesContendView.addSubview(colorItem)
  326. }
  327. if themesArray.count < 10 {
  328. if themesArray.count%5 == 0 {
  329. xValue = 0
  330. }
  331. if themesArray.count/5 == 1 {
  332. yValue = 0
  333. }
  334. let addButton = ComponentButton.init()
  335. addButton.frame = CGRectMake(xValue, yValue, 32, 32)
  336. addButton.properties = ComponentButtonProperty(type: .text_gray, size: .s, onlyIcon: true, icon: NSImage(named: "toolbar_plus"), keepPressState: false)
  337. addButton.setTarget(self, action: #selector(themeAddButtonClicked(_:)))
  338. themesContendView.addSubview(addButton)
  339. }
  340. }
  341. func refreshColorItems() {
  342. let subviews = themesContendView.subviews
  343. for view in subviews {
  344. if let item = view as? ComponentCColorItem {
  345. if item.properties?.identifier == "normal" && pdfView?.displayMode == .normal {
  346. item.properties?.state = .pressed
  347. } else if item.properties?.identifier == "soft" && pdfView?.displayMode == .soft {
  348. item.properties?.state = .pressed
  349. } else if item.properties?.identifier == "night" && pdfView?.displayMode == .night {
  350. item.properties?.state = .pressed
  351. } else if item.properties?.identifier == "green" && pdfView?.displayMode == .green {
  352. item.properties?.state = .pressed
  353. } else if pdfView?.displayMode == .custom && item.properties?.color == pdfView?.displayModeCustomColor && isCustomSelected == false {
  354. item.properties?.state = .pressed
  355. isCustomSelected = true
  356. } else {
  357. item.properties?.state = .normal
  358. }
  359. item.reloadData()
  360. }
  361. }
  362. }
  363. @objc func saveThemeColors() {
  364. themesArray.removeAll()
  365. let subviews = themesContendView.subviews
  366. for view in subviews {
  367. if view is ComponentCColorItem {
  368. if let color = (view as! ComponentCColorItem).properties?.color {
  369. themesArray.append(color)
  370. }
  371. }
  372. }
  373. refreshColorItems()
  374. let data = NSKeyedArchiver.archivedData(withRootObject: themesArray)
  375. UserDefaults.standard.set(data, forKey: "kmPDFViewModeThemesArray")
  376. UserDefaults.standard.synchronize()
  377. }
  378. //MARK: - Action
  379. @objc func selectorClicked(_ sender: ComponentCSelector) {
  380. if sender == singlePageSelector {
  381. singlePageSelector.properties.state = .pressed
  382. twoPageSelector.properties.state = .normal
  383. bookSelector.properties.state = .normal
  384. updatePDFViewDisplayInfo()
  385. } else if sender == twoPageSelector {
  386. singlePageSelector.properties.state = .normal
  387. twoPageSelector.properties.state = .pressed
  388. bookSelector.properties.state = .normal
  389. updatePDFViewDisplayInfo()
  390. } else if sender == bookSelector {
  391. singlePageSelector.properties.state = .normal
  392. twoPageSelector.properties.state = .normal
  393. bookSelector.properties.state = .pressed
  394. updatePDFViewDisplayInfo()
  395. } else if sender == readModeSelector {
  396. if let manager = viewManager {
  397. manager.isPDFReadMode = !manager.isPDFReadMode
  398. }
  399. readModeSelector.properties.state = .normal
  400. delegate?.displayViewControllerDidReadModeUpdated?(self)
  401. } else if sender == fullScreenSelector {
  402. view.window?.toggleFullScreen(nil)
  403. if view.window?.styleMask.contains(.fullScreen) == true {
  404. fullScreenSelector.properties.state = .pressed
  405. } else {
  406. fullScreenSelector.properties.state = .normal
  407. }
  408. fullScreenSelector.reloadData()
  409. } else if sender == slideShowSelector {
  410. slideShowSelector.properties.state = .normal
  411. delegate?.displayViewControllerDidGotoSlideShow?(self)
  412. } else if sender == noSplitSelector {
  413. pdfView?.viewSplitMode = .disable
  414. refreshSplitView()
  415. updatePDFSplitViewInfo()
  416. } else if sender == verticalSelector {
  417. pdfView?.viewSplitMode = .vertical
  418. refreshSplitView()
  419. updatePDFSplitViewInfo()
  420. } else if sender == horizontalSelector {
  421. pdfView?.viewSplitMode = .horizontal
  422. refreshSplitView()
  423. updatePDFSplitViewInfo()
  424. }
  425. reloadData()
  426. }
  427. @objc func checkBoxClicked(_ sender: ComponentCheckBox) {
  428. if sender == continueScrollCheckBox {
  429. updatePDFViewDisplayInfo()
  430. } else if sender == pageBreaksCheckBox {
  431. pdfView?.displaysPageBreaks = pageBreaksCheckBox.properties.checkboxType == .selected ? true : false
  432. updatePDFViewDisplayInfo()
  433. } else if sender == split_syncScrollCheckbox {
  434. viewManager?.splitSyncScroll = split_syncScrollCheckbox.properties.checkboxType == .selected ? true : false
  435. } else if sender == split_showToolbarCheckbox {
  436. viewManager?.splitShowBottomBar = split_showToolbarCheckbox.properties.checkboxType == .selected ? true : false
  437. delegate?.displayViewControllerDidToolbarStateChanged?(self)
  438. }
  439. reloadData()
  440. }
  441. @objc func themeAddButtonClicked(_ sender: ComponentButton) {
  442. let color = KMAppearance.Layout.l_1Color()
  443. themesArray.append(color)
  444. pdfView?.setPageBackgroundColorWith(color, viewMode: .other)
  445. setUpThemesItems()
  446. let subviews = themesContendView.subviews
  447. for view in subviews {
  448. if view is ComponentCColorItem {
  449. themeEditItem = (view as! ComponentCColorItem)
  450. }
  451. }
  452. showColorPanel()
  453. saveThemeColors()
  454. }
  455. @objc private func showColorPanel() {
  456. let panel = NSColorPanel.shared
  457. panel.setTarget(self)
  458. panel.setAction(#selector(colorPanelAction))
  459. panel.orderFront(nil)
  460. }
  461. @objc private func colorPanelAction(sender: NSColorPanel) {
  462. if let item = themeEditItem {
  463. item.properties?.color = sender.color
  464. item.reloadData()
  465. pdfView?.setPageBackgroundColorWith(sender.color, viewMode: .other)
  466. NSObject.cancelPreviousPerformRequests(withTarget: self, selector: #selector(saveThemeColors), object: nil)
  467. self.perform(#selector(saveThemeColors), with: nil, afterDelay: 0.35)
  468. }
  469. }
  470. @objc func splitSelFileButtonClicked(_ sender: ComponentButton) {
  471. let openPanel = NSOpenPanel()
  472. openPanel.allowedFileTypes = ["pdf","PDF"]
  473. openPanel.allowsMultipleSelection = false
  474. openPanel.beginSheetModal(for: self.view.window!) { [weak self] result in
  475. if (result == .OK) {
  476. guard let weakSelf = self else { return }
  477. let fileURL = openPanel.url
  478. let pdfDoc = CPDFDocument(url: fileURL)
  479. if let data = pdfDoc?.isLocked, data {
  480. DispatchQueue.main.asyncAfter(deadline: .now()+0.5) {
  481. NSWindowController.checkPassword(url: fileURL ?? NSURL.fileURL(withPath: ""), type: .owner) { result, pwd in
  482. if (pwd.isEmpty == false) {
  483. }
  484. }
  485. }
  486. } else {
  487. weakSelf.viewManager?.splitPDFFileURL = fileURL
  488. weakSelf.reloadData()
  489. }
  490. weakSelf.delegate?.displayViewControllerDidSplitFileChanged?(weakSelf)
  491. }
  492. }
  493. }
  494. //MARK: - Update
  495. func updatePDFViewDisplayInfo() {
  496. var pdfViewMode: CPDFDisplayViewMode = .singlePage
  497. if singlePageSelector.properties.state == .pressed {
  498. if continueScrollCheckBox.properties.checkboxType == .selected {
  499. pdfViewMode = .singlePageContinuous
  500. }
  501. pdfView?.setDisplay(pdfViewMode)
  502. } else if twoPageSelector.properties.state == .pressed {
  503. pdfViewMode = .twoUp
  504. if continueScrollCheckBox.properties.checkboxType == .selected {
  505. pdfViewMode = .twoUpContinuous
  506. }
  507. pdfView?.setDisplay(pdfViewMode)
  508. } else if bookSelector.properties.state == .pressed {
  509. //书本模式
  510. pdfView?.displaysAsBook = true
  511. pdfView?.displayTwoUp = true
  512. if continueScrollCheckBox.properties.checkboxType == .selected {
  513. pdfView?.displayDirection = .vertical
  514. } else {
  515. pdfView?.displayDirection = .horizontal
  516. }
  517. }
  518. delegate?.displayViewControllerDidDisplayModeChanged?(self)
  519. }
  520. func updatePDFSplitViewInfo() {
  521. delegate?.displayViewControllerDidSplitModeChanged?(self)
  522. }
  523. //MARK: - Mouse
  524. }
  525. //MARK: - ComponentCColorDelegate
  526. extension KMNDisplayViewController: ComponentCColorDelegate {
  527. func componentCColorDidChooseColor(_ view: NSView, _ color: NSColor?) {
  528. if view is ComponentCColorItem {
  529. if (view as! ComponentCColorItem) != themeEditItem {
  530. themeEditItem = nil
  531. if NSColorPanel.shared.isVisible {
  532. NSColorPanel.shared.setTarget(nil)
  533. NSColorPanel.shared.setAction(nil)
  534. NSColorPanel.shared.close()
  535. }
  536. }
  537. }
  538. let subviews = themesContendView.subviews
  539. let index = subviews.firstIndex(of: view) ?? 0
  540. var model: KMPDFViewMode = .normal
  541. if index <= 3 {
  542. if index == 0 {
  543. model = .normal
  544. } else if index == 1 {
  545. model = .soft
  546. } else if index == 2 {
  547. model = .green
  548. } else if index == 3 {
  549. model = .night
  550. }
  551. } else {
  552. model = .other
  553. }
  554. pdfView?.setPageBackgroundColorWith(color, viewMode: model)
  555. for subview in subviews {
  556. if subview is ComponentCColorItem {
  557. if subview != view {
  558. (subview as! ComponentCColorItem).properties?.state = .normal
  559. (subview as! ComponentCColorItem).reloadData()
  560. }
  561. }
  562. }
  563. }
  564. func componentCColorDidRightMouseUpWithStrings(_ view: NSView) -> [String] {
  565. let subviews = themesContendView.subviews
  566. let index = subviews.firstIndex(of: view) ?? 0
  567. if index <= 3 {
  568. } else {
  569. if view is ComponentCColorItem {
  570. if (view as! ComponentCColorItem) != themeEditItem {
  571. themeEditItem = (view as! ComponentCColorItem)
  572. }
  573. }
  574. return [KMLocalizedString("Delete")]
  575. }
  576. return []
  577. }
  578. func componentCColorDidRightMenuItemClicked(_ view: NSView, menuItemProperty: ComponentMenuitemProperty?) {
  579. if let item = themeEditItem, let color = item.properties?.color {
  580. if let index = themesArray.firstIndex(of: color) {
  581. themesArray.remove(at: index)
  582. }
  583. }
  584. self.pdfView?.setPageBackgroundColorWith(NSColor.white, viewMode: .normal)
  585. self.setUpThemesItems()
  586. self.saveThemeColors()
  587. }
  588. }