KMNDisplayViewController.swift 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639
  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. }
  21. class KMNDisplayViewController: NSViewController {
  22. @IBOutlet var titleLabel: NSTextField!
  23. @IBOutlet var displayModeView: NSView!
  24. @IBOutlet var displayModeLabel: NSTextField!
  25. @IBOutlet var singlePageSelector: ComponentCSelector!
  26. @IBOutlet var twoPageSelector: ComponentCSelector!
  27. @IBOutlet var bookSelector: ComponentCSelector!
  28. @IBOutlet var readModeSelector: ComponentCSelector!
  29. @IBOutlet var fullScreenSelector: ComponentCSelector!
  30. @IBOutlet var slideShowSelector: ComponentCSelector!
  31. @IBOutlet var continueScrollCheckBox: ComponentCheckBox!
  32. @IBOutlet var pageBreaksCheckBox: ComponentCheckBox!
  33. //Split
  34. @IBOutlet var splitViewBGView: NSView!
  35. @IBOutlet var splitViewLabel: NSTextField!
  36. @IBOutlet var noSplitSelector: ComponentCSelector!
  37. @IBOutlet var verticalSelector: ComponentCSelector!
  38. @IBOutlet var horizontalSelector: ComponentCSelector!
  39. @IBOutlet var split_selFileButton: ComponentButton!
  40. @IBOutlet var split_fileInfoView: NSView!
  41. @IBOutlet var split_fileLabel: NSTextField!
  42. @IBOutlet var split_syncScrollCheckbox: ComponentCheckBox!
  43. @IBOutlet var split_showToolbarCheckbox: ComponentCheckBox!
  44. @IBOutlet var splitViewBGHeightConst: NSLayoutConstraint!
  45. @IBOutlet var split_SyncScrollTopConst: NSLayoutConstraint!
  46. //Themes
  47. @IBOutlet var themesBGView: NSView!
  48. @IBOutlet var themesLabel: NSTextField!
  49. @IBOutlet var themesContendView: NSView!
  50. private var themesArray: [NSColor] = []
  51. private var themesGroupView: ComponentGroup!
  52. private var themeEditItem: ComponentCColorItem?
  53. var pdfView: CPDFListView?
  54. var viewManager: KMPDFViewManager?
  55. weak open var delegate: KMNDisplayViewControllerDelegate?
  56. deinit {
  57. print("\n\(self.className) deinit.\n")
  58. }
  59. override func viewDidLoad() {
  60. super.viewDidLoad()
  61. // Do view setup here.
  62. setupPropertys()
  63. reloadData()
  64. }
  65. func setupPropertys() {
  66. view.wantsLayer = true
  67. view.layer?.backgroundColor = ComponentLibrary.shared.getComponentColorFromKey("colorBg/layout-middle").cgColor
  68. titleLabel.textColor = ComponentLibrary.shared.getComponentColorFromKey("colorText/2")
  69. titleLabel.font = ComponentLibrary.shared.getFontFromKey("mac/body-m-bold")
  70. titleLabel.stringValue = KMLocalizedString("View")
  71. displayModeLabel.stringValue = KMLocalizedString("Display Mode")
  72. displayModeLabel.textColor = ComponentLibrary.shared.getComponentColorFromKey("colorText/2")
  73. displayModeLabel.font = ComponentLibrary.shared.getFontFromKey("mac/body-s-medium")
  74. singlePageSelector.properties = ComponentCSelectorProperty.init(size: .m, state: .normal, text: KMLocalizedString("Single Page"), iconImage: NSImage(named: "display_singlePage"))
  75. twoPageSelector.properties = ComponentCSelectorProperty.init(size: .m, state: .normal, text: KMLocalizedString("Two Page"), iconImage: NSImage(named: "display_twoPage"))
  76. bookSelector.properties = ComponentCSelectorProperty.init(size: .m, state: .normal, text: KMLocalizedString("Book"), iconImage: NSImage(named: "display_book"))
  77. readModeSelector.properties = ComponentCSelectorProperty.init(size: .m, state: .normal, text: KMLocalizedString("Read Mode"), iconImage: NSImage(named: "display_readMode"))
  78. fullScreenSelector.properties = ComponentCSelectorProperty.init(size: .m, state: .normal, text: KMLocalizedString("Full Screen"), iconImage: NSImage(named: "display_fullscreen"))
  79. slideShowSelector.properties = ComponentCSelectorProperty.init(size: .m, state: .normal, text: KMLocalizedString("Slide Show"), iconImage: NSImage(named: "display_slideShow"))
  80. singlePageSelector.setTarget(self, action: #selector(selectorClicked(_:)))
  81. twoPageSelector.setTarget(self, action: #selector(selectorClicked(_:)))
  82. bookSelector.setTarget(self, action: #selector(selectorClicked(_:)))
  83. readModeSelector.setTarget(self, action: #selector(selectorClicked(_:)))
  84. fullScreenSelector.setTarget(self, action: #selector(selectorClicked(_:)))
  85. slideShowSelector.setTarget(self, action: #selector(selectorClicked(_:)))
  86. continueScrollCheckBox.properties = ComponentCheckBoxProperty(size: .s,
  87. state: .normal,
  88. text: KMLocalizedString("Continuous Scroll"),
  89. checkboxType: .normal)
  90. pageBreaksCheckBox.properties = ComponentCheckBoxProperty(size: .s,
  91. state: .normal,
  92. text: KMLocalizedString("Page Breaks"),
  93. checkboxType: .normal)
  94. continueScrollCheckBox.setTarget(self, action: #selector(checkBoxClicked(_:)))
  95. pageBreaksCheckBox.setTarget(self, action: #selector(checkBoxClicked(_:)))
  96. //Split
  97. splitViewLabel.stringValue = KMLocalizedString("Split View")
  98. splitViewLabel.textColor = ComponentLibrary.shared.getComponentColorFromKey("colorText/2")
  99. splitViewLabel.font = ComponentLibrary.shared.getFontFromKey("mac/body-s-medium")
  100. noSplitSelector.properties = ComponentCSelectorProperty.init(size: .m, state: .normal, text: KMLocalizedString("No Split"), iconImage: NSImage(named: "display_noSplit"))
  101. verticalSelector.properties = ComponentCSelectorProperty.init(size: .m, state: .normal, text: KMLocalizedString("Vertical"), iconImage: NSImage(named: "display_horizontalSplit"))
  102. horizontalSelector.properties = ComponentCSelectorProperty.init(size: .m, state: .normal, text: KMLocalizedString("Horizontal"), iconImage: NSImage(named: "display_verticalSplit"))
  103. noSplitSelector.setTarget(self, action: #selector(selectorClicked(_:)))
  104. verticalSelector.setTarget(self, action: #selector(selectorClicked(_:)))
  105. horizontalSelector.setTarget(self, action: #selector(selectorClicked(_:)))
  106. split_selFileButton.properties = ComponentButtonProperty(type: .default_tertiary, size: .s, buttonText: KMLocalizedString("Select File"), keepPressState: false)
  107. split_selFileButton.setTarget(self, action: #selector(splitSelFileButtonClicked(_:)))
  108. split_fileInfoView.wantsLayer = true
  109. split_fileInfoView.layer?.borderWidth = 1
  110. if let value = ComponentLibrary.shared.getComponentValueFromKey("radius/xs") {
  111. let currentValue = value as! CGFloat
  112. split_fileInfoView.layer?.cornerRadius = currentValue
  113. }
  114. split_fileInfoView.layer?.borderColor = ComponentLibrary.shared.getComponentColorFromKey("colorBorder/4").cgColor
  115. split_fileInfoView.layer?.backgroundColor = ComponentLibrary.shared.getComponentColorFromKey("colorFill/4").cgColor
  116. split_syncScrollCheckbox.properties = ComponentCheckBoxProperty(size: .s,
  117. state: .normal,
  118. text: KMLocalizedString("Sync Scroll and Zoom"),
  119. checkboxType: .normal)
  120. split_showToolbarCheckbox.properties = ComponentCheckBoxProperty(size: .s,
  121. state: .normal,
  122. text: KMLocalizedString("Show Bottom Toolbar"),
  123. checkboxType: .normal)
  124. split_syncScrollCheckbox.setTarget(self, action: #selector(checkBoxClicked(_:)))
  125. split_showToolbarCheckbox.setTarget(self, action: #selector(checkBoxClicked(_:)))
  126. //Themes
  127. themesLabel.stringValue = KMLocalizedString("Themes")
  128. themesLabel.textColor = ComponentLibrary.shared.getComponentColorFromKey("colorText/2")
  129. themesLabel.font = ComponentLibrary.shared.getFontFromKey("mac/body-s-medium")
  130. }
  131. func reloadData() {
  132. let mode = pdfView?.displayMode() ?? .singlePage
  133. let book = pdfView?.displaysAsBook ?? false
  134. singlePageSelector.properties.state = .normal
  135. twoPageSelector.properties.state = .normal
  136. bookSelector.properties.state = .normal
  137. readModeSelector.properties.state = .normal
  138. fullScreenSelector.properties.state = .normal
  139. slideShowSelector.properties.state = .normal
  140. bookSelector.properties.state = .normal
  141. if mode == .singlePage {
  142. singlePageSelector.properties.state = .pressed
  143. continueScrollCheckBox.properties.checkboxType = .normal
  144. } else if mode == .singlePageContinuous {
  145. singlePageSelector.properties.state = .pressed
  146. continueScrollCheckBox.properties.checkboxType = .selected
  147. } else if mode == .twoUp {
  148. bookSelector.properties.state = book ? .pressed : .normal
  149. twoPageSelector.properties.state = book ? .normal : .pressed
  150. continueScrollCheckBox.properties.checkboxType = .normal
  151. } else if mode == .twoUpContinuous {
  152. bookSelector.properties.state = book ? .pressed : .normal
  153. twoPageSelector.properties.state = book ? .normal : .pressed
  154. continueScrollCheckBox.properties.checkboxType = .selected
  155. }
  156. let pageBreaks = pdfView?.displaysPageBreaks ?? false
  157. pageBreaksCheckBox.properties.checkboxType = pageBreaks ? .selected : .normal
  158. singlePageSelector.reloadData()
  159. twoPageSelector.reloadData()
  160. bookSelector.reloadData()
  161. readModeSelector.reloadData()
  162. fullScreenSelector.reloadData()
  163. slideShowSelector.reloadData()
  164. continueScrollCheckBox.reloadData()
  165. pageBreaksCheckBox.reloadData()
  166. //SplitView
  167. noSplitSelector.properties.state = .normal
  168. verticalSelector.properties.state = .normal
  169. horizontalSelector.properties.state = .normal
  170. if pdfView?.viewSplitMode == .disable {
  171. noSplitSelector.properties.state = .pressed
  172. } else if pdfView?.viewSplitMode == .horizontal {
  173. horizontalSelector.properties.state = .pressed
  174. } else if pdfView?.viewSplitMode == .vertical {
  175. verticalSelector.properties.state = .pressed
  176. }
  177. split_syncScrollCheckbox.properties.checkboxType = viewManager?.splitSyncScroll == true ? .selected : .normal
  178. split_showToolbarCheckbox.properties.checkboxType = viewManager?.splitShowBottomBar == true ? .selected : .normal
  179. if let fileUrl = viewManager?.splitPDFFileURL {
  180. split_fileLabel.stringValue = fileUrl.lastPathComponent
  181. }
  182. noSplitSelector.reloadData()
  183. horizontalSelector.reloadData()
  184. verticalSelector.reloadData()
  185. split_syncScrollCheckbox.reloadData()
  186. split_showToolbarCheckbox.reloadData()
  187. refreshSplitView()
  188. //Themes
  189. var data: Data? = nil
  190. if let themesData = UserDefaults.standard.object(forKey: "kKMPDFViewModeThemesArray") as? Data {
  191. data = themesData
  192. }
  193. let appArray = NSKeyedUnarchiver.unarchiveObject(with: data ?? Data()) as? [Any] ?? []
  194. let mutableArray = appArray
  195. if mutableArray.count > 0 {
  196. themesArray = mutableArray as! [NSColor]
  197. } else {
  198. let normalColor = NSColor.white
  199. let softColor = NSColor(deviceRed: 238.0/255.0, green: 232.0/255.0, blue: 216.0/255.0, alpha: 1.0)
  200. let nightColor = NSColor.black
  201. let greenColor = NSColor(deviceRed: 153.0/255.0, green: 207.0/255.0, blue: 161.0/255.0, alpha: 1.0)
  202. themesArray = [normalColor, softColor, nightColor, greenColor]
  203. }
  204. setUpThemesItems()
  205. }
  206. //MARK: - Split
  207. func refreshSplitView() {
  208. if pdfView?.viewSplitMode == .disable {
  209. splitViewBGHeightConst.constant = 90
  210. split_selFileButton.isHidden = true
  211. split_fileInfoView.isHidden = true
  212. split_syncScrollCheckbox.isHidden = true
  213. split_showToolbarCheckbox.isHidden = true
  214. } else {
  215. if (viewManager?.splitPDFFileURL) != nil {
  216. splitViewBGHeightConst.constant = 246
  217. split_SyncScrollTopConst.constant = 52
  218. split_selFileButton.isHidden = false
  219. split_fileInfoView.isHidden = false
  220. split_syncScrollCheckbox.isHidden = false
  221. split_showToolbarCheckbox.isHidden = false
  222. } else {
  223. splitViewBGHeightConst.constant = 202
  224. split_SyncScrollTopConst.constant = 8
  225. split_selFileButton.isHidden = false
  226. split_fileInfoView.isHidden = true
  227. split_syncScrollCheckbox.isHidden = false
  228. split_showToolbarCheckbox.isHidden = false
  229. }
  230. }
  231. }
  232. //MARK: - Themes
  233. func setUpThemesItems() {
  234. let subviews = themesContendView.subviews
  235. for view in subviews {
  236. if view is ComponentCColorItem {
  237. view.removeFromSuperview()
  238. } else if view is ComponentButton {
  239. view.removeFromSuperview()
  240. }
  241. }
  242. //MAX = 10
  243. var xValue: CGFloat = 0
  244. var yValue = CGRectGetHeight(themesContendView.frame) - 32
  245. for index in 0...themesArray.count-1 {
  246. let color = themesArray[index]
  247. if index%5 == 0 {
  248. xValue = 0
  249. }
  250. if index/5 == 1 {
  251. yValue = 0
  252. }
  253. let colorItem = ComponentCColorItem.init()
  254. colorItem.delegate = self
  255. colorItem.frame = CGRectMake(xValue, yValue, 32, 32)
  256. colorItem.properties = ComponentCColorProperty(colorType: .color, state: .normal, isCustom: false, color: color)
  257. xValue += 32
  258. xValue += 18
  259. if index == 0 {
  260. colorItem.properties?.identifier = "normal"
  261. } else if index == 1 {
  262. colorItem.properties?.identifier = "soft"
  263. } else if index == 2 {
  264. colorItem.properties?.identifier = "night"
  265. } else if index == 3 {
  266. colorItem.properties?.identifier = "green"
  267. } else {
  268. colorItem.properties?.identifier = "custom"
  269. }
  270. if pdfView?.displayMode == .normal {
  271. if index == 0 {
  272. colorItem.properties?.state = .pressed
  273. }
  274. } else if pdfView?.displayMode == .soft {
  275. if index == 1 {
  276. colorItem.properties?.state = .pressed
  277. }
  278. } else if pdfView?.displayMode == .night {
  279. if index == 2 {
  280. colorItem.properties?.state = .pressed
  281. }
  282. } else if pdfView?.displayMode == .green {
  283. if index == 3 {
  284. colorItem.properties?.state = .pressed
  285. }
  286. } else if pdfView?.displayMode == .custom {
  287. if color == pdfView?.displayModeCustomColor {
  288. colorItem.properties?.state = .pressed
  289. }
  290. }
  291. colorItem.reloadData()
  292. themesContendView.addSubview(colorItem)
  293. }
  294. if themesArray.count < 10 {
  295. if themesArray.count%5 == 0 {
  296. xValue = 0
  297. }
  298. if themesArray.count/5 == 1 {
  299. yValue = 0
  300. }
  301. let addButton = ComponentButton.init()
  302. addButton.frame = CGRectMake(xValue, yValue, 32, 32)
  303. addButton.properties = ComponentButtonProperty(type: .text_gray, size: .s, onlyIcon: true, icon: NSImage(named: "toolbar_plus"), keepPressState: false)
  304. addButton.setTarget(self, action: #selector(themeAddButtonClicked(_:)))
  305. themesContendView.addSubview(addButton)
  306. }
  307. }
  308. func refreshColorItems() {
  309. }
  310. func saveThemeColors() {
  311. let data = NSKeyedArchiver.archivedData(withRootObject: themesArray)
  312. UserDefaults.standard.set(data, forKey: "kKMPDFViewModeThemesArray")
  313. UserDefaults.standard.synchronize()
  314. }
  315. //MARK: - Action
  316. @objc func selectorClicked(_ sender: ComponentCSelector) {
  317. if sender == singlePageSelector {
  318. singlePageSelector.properties.state = .pressed
  319. twoPageSelector.properties.state = .normal
  320. bookSelector.properties.state = .normal
  321. updatePDFViewDisplayInfo()
  322. } else if sender == twoPageSelector {
  323. singlePageSelector.properties.state = .normal
  324. twoPageSelector.properties.state = .pressed
  325. bookSelector.properties.state = .normal
  326. updatePDFViewDisplayInfo()
  327. } else if sender == bookSelector {
  328. singlePageSelector.properties.state = .normal
  329. twoPageSelector.properties.state = .normal
  330. bookSelector.properties.state = .pressed
  331. updatePDFViewDisplayInfo()
  332. } else if sender == readModeSelector {
  333. if let manager = viewManager {
  334. manager.isPDFReadMode = !manager.isPDFReadMode
  335. }
  336. readModeSelector.properties.state = .normal
  337. delegate?.displayViewControllerDidReadModeUpdated?(self)
  338. } else if sender == fullScreenSelector {
  339. fullScreenSelector.properties.state = .normal
  340. view.window?.toggleFullScreen(nil)
  341. } else if sender == slideShowSelector {
  342. slideShowSelector.properties.state = .normal
  343. } else if sender == noSplitSelector {
  344. pdfView?.viewSplitMode = .disable
  345. refreshSplitView()
  346. updatePDFSplitViewInfo()
  347. } else if sender == verticalSelector {
  348. pdfView?.viewSplitMode = .vertical
  349. refreshSplitView()
  350. updatePDFSplitViewInfo()
  351. } else if sender == horizontalSelector {
  352. pdfView?.viewSplitMode = .horizontal
  353. refreshSplitView()
  354. updatePDFSplitViewInfo()
  355. }
  356. reloadData()
  357. }
  358. @objc func checkBoxClicked(_ sender: ComponentCheckBox) {
  359. if sender == continueScrollCheckBox {
  360. updatePDFViewDisplayInfo()
  361. } else if sender == pageBreaksCheckBox {
  362. pdfView?.displaysPageBreaks = pageBreaksCheckBox.properties.checkboxType == .selected ? true : false
  363. updatePDFViewDisplayInfo()
  364. } else if sender == split_syncScrollCheckbox {
  365. viewManager?.splitSyncScroll = split_syncScrollCheckbox.properties.checkboxType == .selected ? true : false
  366. } else if sender == split_showToolbarCheckbox {
  367. viewManager?.splitShowBottomBar = split_showToolbarCheckbox.properties.checkboxType == .selected ? true : false
  368. }
  369. reloadData()
  370. }
  371. @objc func themeAddButtonClicked(_ sender: ComponentButton) {
  372. let color = KMAppearance.Layout.l_1Color()
  373. themesArray.append(color)
  374. pdfView?.setPageBackgroundColorWith(color, viewMode: .other)
  375. setUpThemesItems()
  376. saveThemeColors()
  377. }
  378. @objc func splitSelFileButtonClicked(_ sender: ComponentButton) {
  379. let openPanel = NSOpenPanel()
  380. openPanel.allowedFileTypes = ["pdf","PDF"]
  381. openPanel.allowsMultipleSelection = false
  382. openPanel.beginSheetModal(for: self.view.window!) { [weak self] result in
  383. if (result == .OK) {
  384. guard let weakSelf = self else { return }
  385. let fileURL = openPanel.url
  386. let pdfDoc = CPDFDocument(url: fileURL)
  387. if let data = pdfDoc?.isLocked, data {
  388. DispatchQueue.main.asyncAfter(deadline: .now()+0.5) {
  389. KMNBaseWindowController.checkPassword(url: fileURL ?? NSURL.fileURL(withPath: ""), type: .owner) { result, pwd in
  390. if (pwd.isEmpty == false) {
  391. }
  392. }
  393. }
  394. } else {
  395. weakSelf.viewManager?.splitPDFFileURL = fileURL
  396. weakSelf.reloadData()
  397. }
  398. weakSelf.delegate?.displayViewControllerDidSplitFileChanged?(weakSelf)
  399. }
  400. }
  401. }
  402. //MARK: - Update
  403. func updatePDFViewDisplayInfo() {
  404. var pdfViewMode: CPDFDisplayViewMode = .singlePage
  405. if singlePageSelector.properties.state == .pressed {
  406. if continueScrollCheckBox.properties.checkboxType == .selected {
  407. pdfViewMode = .singlePageContinuous
  408. }
  409. pdfView?.setDisplay(pdfViewMode)
  410. } else if twoPageSelector.properties.state == .pressed {
  411. pdfViewMode = .twoUp
  412. if continueScrollCheckBox.properties.checkboxType == .selected {
  413. pdfViewMode = .twoUpContinuous
  414. }
  415. pdfView?.setDisplay(pdfViewMode)
  416. } else if bookSelector.properties.state == .pressed {
  417. //书本模式
  418. pdfView?.displaysAsBook = true
  419. pdfView?.displayTwoUp = true
  420. if continueScrollCheckBox.properties.checkboxType == .selected {
  421. pdfView?.displayDirection = .vertical
  422. } else {
  423. pdfView?.displayDirection = .horizontal
  424. }
  425. }
  426. delegate?.displayViewControllerDidDisplayModeChanged?(self)
  427. }
  428. func updatePDFSplitViewInfo() {
  429. delegate?.displayViewControllerDidSplitModeChanged?(self)
  430. }
  431. //MARK: - PageTurner Menu
  432. func showCustomThemePopViewWithPoint(_ point: CGPoint) {
  433. if (themesGroupView?.superview) != nil {
  434. return
  435. }
  436. let deleteItem: ComponentMenuitemProperty = ComponentMenuitemProperty(type: .normal, text: KMLocalizedString("Delete"))
  437. if themesGroupView == nil {
  438. themesGroupView = ComponentGroup.createFromNib(in: ComponentLibrary.shared.componentBundle())
  439. }
  440. themesGroupView.clickedAutoHide = true
  441. themesGroupView?.frame = CGRectMake(0, 0, 160, 44)
  442. themesGroupView?.reloadData()
  443. themesGroupView.groupDelegate = self
  444. themesGroupView.showWithPoint(point, relativeTo: themesContendView)
  445. themesGroupView?.updateGroupInfo([deleteItem])
  446. }
  447. func hideGroupView() {
  448. themesGroupView?.removeGroupView()
  449. }
  450. }
  451. //MARK: - ComponentCColorDelegate
  452. extension KMNDisplayViewController: ComponentCColorDelegate {
  453. func componentCColorDidChooseColor(_ view: NSView, _ color: NSColor?) {
  454. let subviews = themesContendView.subviews
  455. let index = subviews.firstIndex(of: view) ?? 0
  456. var model: KMPDFViewMode = .normal
  457. if index <= 3 {
  458. model = KMPDFViewMode(rawValue: UInt(index)) ?? .normal
  459. } else {
  460. model = .other
  461. }
  462. pdfView?.setPageBackgroundColorWith(color, viewMode: model)
  463. for subview in subviews {
  464. if subview is ComponentCColorItem {
  465. if subview != view {
  466. (subview as! ComponentCColorItem).properties?.state = .normal
  467. (subview as! ComponentCColorItem).reloadData()
  468. }
  469. }
  470. }
  471. }
  472. func componentCColorDidRightMouseUp(_ view: ComponentCColorItem, with event: NSEvent) {
  473. if view.properties?.identifier == "custom" {
  474. var point = self.view.convert(event.locationInWindow, from: nil)
  475. point.y -= 20
  476. themeEditItem = view
  477. showCustomThemePopViewWithPoint(point)
  478. }
  479. }
  480. }
  481. //MARK: - ComponentGroupDelegate
  482. extension KMNDisplayViewController: ComponentGroupDelegate {
  483. func componentGroupDidSelect(group: ComponentGroup?, menuItemProperty: ComponentMenuitemProperty?) {
  484. if let item = themeEditItem, let color = item.properties?.color {
  485. if let index = themesArray.firstIndex(of: color) {
  486. themesArray.remove(at: index)
  487. }
  488. }
  489. setUpThemesItems()
  490. saveThemeColors()
  491. }
  492. }