KMMainViewController+UI.swift 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477
  1. //
  2. // KMMainViewController+UI.swift
  3. // PDF Master
  4. //
  5. // Created by wanjun on 2022/12/15.
  6. //
  7. import Foundation
  8. extension KMMainViewController {
  9. func showChildToolbar(showToolbar:Bool) {
  10. if showToolbar == true && self.isReadMode == false {
  11. self.view.addSubview(self.childToolbarController.view)
  12. } else {
  13. self.childToolbarController.view.removeFromSuperview()
  14. }
  15. self.updateTopView()
  16. }
  17. private func updateTopView () {
  18. let frame = self.view.frame
  19. if self.childToolbarController.view.superview != nil {
  20. let toolbarFrame = self.childToolbarController.view.frame;
  21. self.childToolbarController.view.frame = CGRect(x: 0, y: (frame.size.height ?? 0)-toolbarFrame.size.height, width: frame.size.width ?? 0, height: toolbarFrame.size.height)
  22. self.childToolbarController.view.autoresizingMask = [.width,.minYMargin]
  23. self.toplayoutConstraint.constant = toolbarFrame.size.height
  24. } else {
  25. self.toplayoutConstraint.constant = 0
  26. }
  27. }
  28. //通知
  29. func preferenceDidChangeNotification(notification:Notification) {
  30. let info : [AnyHashable : Any] = notification.userInfo ?? [:]
  31. if info.keys.contains(KMPreference.viewZoomScaleTypeKey) {
  32. self.selectZoom(KMPreferenceManager.shared.viewZoomScaleType)
  33. }
  34. if notification.name.rawValue == "pdfViewDocumentDidLoaded" || info.keys.contains(KMPreference.viewPageDisplayTypeKey) {
  35. self.selectDisplay(display: KMPreferenceManager.shared.viewPageDisplayType)
  36. }
  37. if info.keys.contains(KMPreference.displayBackgroundNormalColorKey) || notification.name.rawValue == "pdfViewDocumentDidLoaded" {
  38. self.listView.backgroundColor = KMPreferenceManager.shared.displayBackgroundNormalColor
  39. self.listView.layoutDocumentView()
  40. }
  41. self.updatePageIndicatoreType()
  42. if (info.keys.contains(KMPreference.generalAuthorNameKey)) { // 作者名称
  43. CPDFKitConfig.sharedInstance().setAnnotationAuthor((info[KMPreference.generalAuthorNameKey] as! String))
  44. }
  45. if (info.keys.contains(KMPreference.highlightLinksKey)) {
  46. let hlLink = info[KMPreference.highlightLinksKey] as? Bool
  47. CPDFKitConfig.sharedInstance().setEnableLinkFieldHighlight(hlLink == nil ? false : hlLink!)
  48. self.listView.setNeedsDisplayForVisiblePages()
  49. }
  50. if (info.keys.contains(KMPreference.autoSaveKey) || info.keys.contains(KMPreference.autoSavePerNumberMinuteKey)) {
  51. self.autoSaveTimeStartOrStopIfNeed()
  52. }
  53. }
  54. //刷新页面显示器
  55. func updatePageIndicatoreType () {
  56. if self.isReadMode {
  57. self.pageNumberDisplayView.dismiss()
  58. } else {
  59. if self.listView.document != nil {
  60. self.pageNumberDisplayView.totalPagesCount = Int(self.listView.document.pageCount)
  61. }
  62. self.pageNumberDisplayView.currentPageIndex = self.listView.currentPageIndex
  63. self.pageNumberDisplayView.displayType = KMPreferenceManager.shared.pageIndicatorType
  64. self.readContentView.addSubview(self.tipCurrentPageBox, positioned: .above, relativeTo: self.readContentView)
  65. }
  66. }
  67. }
  68. extension KMMainViewController: KMToolbarControllerDelegate {
  69. func toolbarController(_ viewController: KMToolbarController, heightOffsetChange heightOffset: Float) {
  70. if self.isReadMode {
  71. self.heightOffset.constant = CGFloat(0)
  72. } else {
  73. self.heightOffset.constant = CGFloat(heightOffset)
  74. }
  75. self.refreshListViewToolMode()
  76. self.listView.annotationType = .unkown
  77. // self.rightSideViewController.view.isHidden = true
  78. self.rightSideViewController.isHidden = true
  79. if self.toolbarController.toolbarType != .LeftPanel {
  80. self.closeRightPane()
  81. }
  82. }
  83. func changeAnnotationModeAction(item: KMToolbarClickButton){
  84. var currentType = self.listView.annotationType
  85. if (currentType == .eraser) {
  86. currentType = .ink
  87. }
  88. var isSelected = (currentType == CAnnotationType(rawValue: item.tag) ?? CAnnotationType.unkown) && ((self.listView.toolMode == .noteToolMode) || (self.listView.toolMode == .selfSignMode))
  89. var editSelectd = false
  90. if self.listView.annotationType == CAnnotationType(rawValue: item.tag) ?? CAnnotationType.unkown && self.listView.toolMode == .editPDFToolMode {
  91. editSelectd = true
  92. }
  93. //多边形工具栏点击特殊处理
  94. if (self.listView.annotationType == .square ||
  95. self.listView.annotationType == .circle ||
  96. self.listView.annotationType == .arrow ||
  97. self.listView.annotationType == .line) {
  98. if CAnnotationType(rawValue: item.tag) ?? CAnnotationType.unkown == .square ||
  99. CAnnotationType(rawValue: item.tag) ?? CAnnotationType.unkown == .circle ||
  100. CAnnotationType(rawValue: item.tag) ?? CAnnotationType.unkown == .arrow ||
  101. CAnnotationType(rawValue: item.tag) ?? CAnnotationType.unkown == .line {
  102. isSelected = true
  103. }
  104. } else if (item.tag > 3 && item.tag < 8) {
  105. let shape = UserDefaults.standard.object(forKey: KMToolBarToolPDFShapeStyle)
  106. item.tag = shape as? Int ?? item.tag
  107. }
  108. if isSelected {
  109. self.listView.toolMode = .textToolMode
  110. self.listView.annotationType = .unkown
  111. self.closeRightPane()
  112. } else {
  113. if self.listView.currentSelection != nil {
  114. let annotation = self.listView.addAnnotation(with: CAnnotationType(rawValue: item.tag) ?? CAnnotationType.unkown, selection: self.listView.currentSelection, page: self.listView.currentSelection.page, bounds: self.listView.currentSelection.bounds)
  115. if((annotation != nil) &&
  116. !self.listView.activeAnnotations.contains(annotation ?? self.listView.activeAnnotation)) {
  117. var newAnnonations : [CPDFAnnotation] = []
  118. newAnnonations.append(annotation!)
  119. self.listView.updateActiveAnnotations(newAnnonations)
  120. self.listView.setNeedsDisplayAnnotationViewFor(annotation?.page)
  121. }
  122. }
  123. self.refreshListViewToolMode()
  124. let type = CAnnotationType(rawValue: item.tag) ?? CAnnotationType.unkown
  125. var annotations: [CPDFAnnotation] = []
  126. if (type == .link) {
  127. for annotation in self.listView.activeAnnotations {
  128. if ((annotation is CPDFLinkAnnotation) == false) {
  129. continue
  130. }
  131. annotations.append(annotation as! CPDFAnnotation)
  132. }
  133. }
  134. self.listView.annotationType = type
  135. if (type == .link && annotations.count > 0) {
  136. self.listView.updateActiveAnnotations(annotations)
  137. }
  138. }
  139. if self.listView.toolMode != .editPDFToolMode && self.listView.annotationType != .unkown {
  140. // self.rightSideViewController.view.isHidden = false
  141. self.rightSideViewController.isHidden = false
  142. self.rightSideViewController.subViewType = .AnnotationProperts
  143. self.openRightPane()
  144. } else if self.listView.toolMode == .editPDFToolMode {
  145. if editSelectd {
  146. // self.rightSideViewController.view.isHidden = true
  147. self.rightSideViewController.isHidden = true
  148. self.listView.setShouAddEdit([])
  149. self.listView.annotationType = .unkown
  150. self.closeRightPane()
  151. } else if self.listView.annotationType == .addText {
  152. // self.rightSideViewController.view.isHidden = false
  153. self.rightSideViewController.isHidden = false
  154. self.openRightPane()
  155. self.rightSideViewController.subViewType = .EditPDFAddText
  156. self.listView.setShouAddEdit(.text)
  157. } else if self.listView.annotationType == .addImage {
  158. self.rightSideViewController.subViewType = .EditPDFAddImage
  159. self.listView.setShouAddEdit(.image)
  160. }
  161. } else {
  162. // self.rightSideViewController.view.isHidden = true
  163. self.rightSideViewController.isHidden = true
  164. self.closeRightPane()
  165. }
  166. }
  167. func refreshListViewToolMode () {
  168. if self.toolbarController.toolbarType == .Annatiton {
  169. self.listView.toolMode = .noteToolMode
  170. } else if self.toolbarController.toolbarType == .Move {
  171. self.listView.toolMode = .moveToolMode
  172. } else if self.toolbarController.toolbarType == .Magnify {
  173. self.listView.toolMode = .magnifyToolMode
  174. } else if self.toolbarController.toolbarType == .Select {
  175. self.listView.toolMode = .selectToolMode
  176. } else if self.toolbarController.toolbarType == .SelectZoom {
  177. self.listView.toolMode = .selectZoomToolMode
  178. } else if self.toolbarController.toolbarType == .Page {
  179. } else if self.toolbarController.toolbarType == .Form {
  180. self.listView.toolMode = .formToolMode
  181. self.rightSideViewController.subViewType = .AnnotationProperts
  182. } else if self.toolbarController.toolbarType == .FillSign {
  183. self.listView.toolMode = .selfSignMode
  184. self.rightSideViewController.subViewType = .AnnotationProperts
  185. } else if self.toolbarController.toolbarType == .editPDF {
  186. self.listView.toolMode = .editPDFToolMode
  187. self.listView.editingConfig().isShowEditingAreaHover = true
  188. self.listView.editingConfig().editingBorderWidth = 1
  189. self.listView.editingConfig().editingCenterRadius = 5
  190. self.listView.editingConfig().editAreaMargin = 2
  191. self.listView.editingConfig().editingHoverBorderColor = NSColor.init(red: 0, green: 136.0/255.0, blue: 1.0, alpha: 0.5)
  192. self.listView.editingConfig().editingBorderColor = NSColor.init(red: 0, green: 0, blue: 0, alpha: 0.4)
  193. self.listView.editingConfig().addEditAreaColor = NSColor.init(red: 23.0/255.0, green: 112.0/255.0, blue: 244.0/255.0, alpha: 0.5)
  194. self.listView.editingConfig().editAreaMoveFillColor = NSColor.init(red: 23.0/255.0, green: 112.0/255.0, blue: 244.0/255.0, alpha: 0.1)
  195. }else if self.toolbarController.toolbarType == .LeftPanel {
  196. self.toggleLeftPane()
  197. self.leftSideViewController.showPanelView(show: true)
  198. }else {
  199. // if self.leftSideViewController.isShowPanel {
  200. // self.leftSideViewController.showPanelView(show: false)
  201. // }
  202. self.listView.toolMode = .textToolMode
  203. }
  204. }
  205. func showPDFLayoutModeAction(show: Bool) {
  206. self.listView.annotationType = .unkown
  207. self.showOrHideNotes()
  208. }
  209. func toolbarViewController(_ viewController: KMToolbarViewController, rightPanel toolbarItem: KMToolBoxItem) {
  210. self.toggleRightPane()
  211. }
  212. func toolbarViewController(_ viewController: KMToolbarViewController, leftPanel toolbarItem: KMToolBoxItem) {
  213. self.toggleLeftPane()
  214. self.leftSideViewController.showPanelView(show: toolbarItem.isSelected)
  215. }
  216. }
  217. extension KMMainViewController : NSSplitViewDelegate {
  218. func splitView(_ splitView: NSSplitView, canCollapseSubview subview: NSView) -> Bool {
  219. if splitView == self.mianSplitView {
  220. // return (subview == self.listView) == false
  221. }
  222. return false
  223. }
  224. func splitView(_ splitView: NSSplitView, shouldCollapseSubview subview: NSView, forDoubleClickOnDividerAt dividerIndex: Int) -> Bool {
  225. return false
  226. }
  227. func splitView(_ splitView: NSSplitView, shouldHideDividerAt dividerIndex: Int) -> Bool {
  228. return splitView == self.mianSplitView
  229. }
  230. func splitView(_ splitView: NSSplitView, constrainMaxCoordinate proposedMaximumPosition: CGFloat, ofSubviewAt dividerIndex: Int) -> CGFloat {
  231. if splitView == mianSplitView {
  232. let splitViewWidth = splitView.frame.width
  233. let dividerIndexX0 = panelWidth + functionWidth
  234. if dividerIndex == 0 {
  235. if leftPanelOpen {
  236. return CGFloat(dividerIndexX0)
  237. } else {
  238. return functionWidth
  239. }
  240. } else if dividerIndex == 1 {
  241. return splitViewWidth
  242. }
  243. }
  244. return proposedMaximumPosition
  245. }
  246. func splitView(_ splitView: NSSplitView, constrainMinCoordinate proposedMinimumPosition: CGFloat, ofSubviewAt dividerIndex: Int) -> CGFloat {
  247. if splitView == mianSplitView {
  248. let splitViewWidth = splitView.frame.width
  249. let dividerIndexX1 = splitViewWidth - defaultRightWidth
  250. if dividerIndex == 0 {
  251. return functionWidth
  252. } else if dividerIndex == 1 {
  253. return dividerIndexX1
  254. }
  255. }
  256. return proposedMinimumPosition
  257. }
  258. func splitView(_ splitView: NSSplitView, resizeSubviewsWithOldSize oldSize: NSSize) {
  259. if splitView.isEqual(to: mianSplitView) {
  260. let leftView = splitView.subviews[0]
  261. let centerView = splitView.subviews[1]
  262. let rightView = splitView.subviews[2]
  263. let leftCollapsed = splitView.isSubviewCollapsed(leftView)
  264. let rightCollapsed = splitView.isSubviewCollapsed(rightView)
  265. var leftSize = leftView.frame.size
  266. var mainSize = centerView.frame.size
  267. var rightSize = rightView.frame.size
  268. var contentWidth = splitView.frame.width
  269. if leftCollapsed {
  270. leftSize.width = 0.0
  271. } else {
  272. contentWidth = contentWidth - splitView.dividerThickness
  273. }
  274. if rightCollapsed {
  275. rightSize.width = 0.0
  276. } else {
  277. contentWidth = contentWidth - splitView.dividerThickness
  278. }
  279. if contentWidth < leftSize.width + rightSize.width {
  280. var oldContentWidth = oldSize.width
  281. if leftCollapsed == false {
  282. oldContentWidth = oldContentWidth - splitView.dividerThickness
  283. }
  284. if rightCollapsed == false {
  285. oldContentWidth = oldContentWidth - splitView.dividerThickness
  286. }
  287. let resizeFactor = contentWidth / oldContentWidth
  288. leftSize.width = floor(resizeFactor*leftSize.width)
  289. rightSize.width = floor(resizeFactor*rightSize.width)
  290. }
  291. mainSize.width = contentWidth - leftSize.width - rightSize.width
  292. leftSize.height = splitView.frame.height
  293. rightSize.height = splitView.frame.height
  294. mainSize.height = splitView.frame.height
  295. if leftCollapsed == false {
  296. leftView.setFrameSize(leftSize)
  297. }
  298. if rightCollapsed == false {
  299. rightView.setFrameSize(rightSize)
  300. }
  301. centerView.setFrameSize(mainSize)
  302. }
  303. splitView.adjustSubviews()
  304. }
  305. func splitViewWillResizeSubviews(_ notification: Notification) {
  306. let defaultView = KMSplitView()
  307. let sender : NSSplitView = notification.object as? NSSplitView ?? defaultView
  308. if (sender == self.mianSplitView || sender == defaultView) && self.view.window?.frameAutosaveName != nil{
  309. let leftWidth = self.mianSplitView.isSubviewCollapsed(self.leftView) ? 0.0 : leftSideViewController.view.frame.width
  310. if leftPanelOpen {
  311. // if leftWidth < 100 && lastLeftPanWidth > functionWidth{
  312. // self.closeLeftPane()
  313. // }
  314. }
  315. }
  316. }
  317. func splitViewDidResizeSubviews(_ notification: Notification) {
  318. self.locationPageView.frame = CGRect(x: 0, y: self.listView.frame.maxY-32, width: self.listView.frame.width, height: 32)
  319. let defaultView = KMSplitView()
  320. let sender : NSSplitView = notification.object as? NSSplitView ?? defaultView
  321. if (sender == self.mianSplitView || sender == defaultView) && self.view.window?.frameAutosaveName != nil{
  322. let leftWidth = self.mianSplitView.isSubviewCollapsed(self.leftView) ? 0.0 : leftSideViewController.view.frame.width
  323. let rightWidth = self.mianSplitView.isSubviewCollapsed(self.rightView) ? 0.0 : rightSideViewController.view.frame.width
  324. UserDefaults.standard.set(leftWidth, forKey: CPDFOfficeLeftSidePaneWidthKey)
  325. UserDefaults.standard.set(rightWidth, forKey: CPDFOfficeRightSidePaneWidthKey)
  326. }
  327. }
  328. func splitView(_ splitView: NSSplitView, constrainSplitPosition proposedPosition: CGFloat, ofSubviewAt dividerIndex: Int) -> CGFloat {
  329. if dividerIndex == 0 {
  330. if leftPanelOpen {
  331. //当左边栏开启式,拖拽时不改变宽度,当宽度达到原宽度的一半时关闭窗口
  332. //如果 isShowPanel 为 true 那么将不会收缩
  333. if (proposedPosition < functionWidth + panelWidth * 0.5) && !self.leftSideViewController.isShowPanel {
  334. self.closeLeftPane()
  335. self.leftSideViewController.refreshMethodType(methodType: .None)
  336. return functionWidth
  337. } else {
  338. return panelWidth + functionWidth
  339. }
  340. } else {
  341. return functionWidth
  342. }
  343. }
  344. return proposedPosition
  345. }
  346. }
  347. extension KMMainViewController: KMLeftSideViewControllerDelegate {
  348. func controlStateChange(_ obj: KMLeftSideViewController, show: Bool) {
  349. if show {
  350. self.openLeftPane()
  351. } else {
  352. self.closeLeftPane()
  353. }
  354. }
  355. func enterEditMode(_ obj: KMLeftSideViewController, _ pages: [Int]) {
  356. let item : KMToolBoxItem = (self.toolbarController.mainToolBarView?.toolbarItemFindItemIdentifiers(value: KMDocumentPageToolbarItemIdentifier))!
  357. self.toolbarController.mainToolBarView?.delegate?.toolbarViewController?(self.toolbarController.mainToolBarView!, clickMode: .Page, toolbar: item, pages)
  358. }
  359. func searchAction(searchString: String,isCase:Bool) {
  360. self.search(searchString: searchString,isCase: isCase)
  361. }
  362. func controller(_ controller: KMLeftSideViewController, dispayDidChange dispay: KMPDFDisplayType) {
  363. self.selectDisplay(display: dispay, viewSettingIsReload: false)
  364. }
  365. func controller(controller: KMLeftSideViewController, itemClick item: Any?, itemKey: KMItemKey, params: Any?) {
  366. if (itemKey == .print) { // 打印
  367. var pageRange: KMPrintPageRange = KMPrintPageRange(type: .allPage, selectPages: [], reversePrintOrder: false)
  368. if (params != nil) {
  369. pageRange.type = .custom
  370. pageRange.selectPages = params as! [Int]
  371. }
  372. self.showPrintWindow(pageRange: pageRange)
  373. } else if (itemKey == .cut || itemKey == .paste || itemKey == .delete || itemKey == .leftRotate || itemKey == .rightRotate) {
  374. self._isPDFDocumentEdited = true
  375. }
  376. }
  377. func controller(controller: KMLeftSideViewController, bookMarkDidChange bookMarks: [KMBookMarkItem]) {
  378. self.needSave = true
  379. self._isPDFDocumentEdited = true
  380. }
  381. }
  382. //MARK: ReadModel
  383. extension KMMainViewController {
  384. @objc func openReadModel() {
  385. //设置阅读模式
  386. self.isReadMode = true
  387. self.readModelView.isHidden = false
  388. //顶部菜单栏状态清空
  389. self.toolbarController.toolbarType = .None
  390. self.toolbarController.cancelSelected(KMLeftControlToolbarItemIdentifier)
  391. self.toolbarController.cancelSelected(KMRightControlToolbarItemIdentifier)
  392. self.toolbarController.lastItemBox.isSelected = false
  393. //左侧菜单栏情况
  394. self.leftPanelOpen = false
  395. self.lastLeftPanWidth = 0
  396. self.leftSideViewController.refreshMethodType(methodType: .None)
  397. //阅读界面初始化
  398. self.readModelView.isHidden = false
  399. self.readModelView.currentPageIndex = self.listView.currentPageIndex
  400. self.readModelView.totalPagesCount = Int(self.listView.document.pageCount)
  401. self.readModelView.beginTimer()
  402. // self.listView.autoScales = true
  403. // self.readModelView.scaleType = .autoSize
  404. self.readModelView.zoomButton.stringValue = (NSString(format: "%.0f", self.listView.scaleFactor * 100) as String) + "%"
  405. //关闭左侧右侧菜单栏
  406. self.closeLeftPane()
  407. self.closeRightPane()
  408. self.leftSideViewController.showPanelView(show: false)
  409. self.leftView.isHidden = true
  410. //弹出提示框
  411. let alertView = CustomAlertView(message: NSLocalizedString("Read mode on, you can press ESC to exit.", comment: ""), from: self.view, with: .blue, backgroundColor: NSColor(hex: "#36383B"))
  412. }
  413. @objc func closeReadModel() {
  414. if self.isReadMode {
  415. self.isShowBOTA = false
  416. self.readModelView.isHidden = true
  417. self.isReadMode = false
  418. self.readModelView.isHidden = true
  419. self.toolbarController.toolbarType = self.toolbarController.toolbarType
  420. } else {
  421. self.toolbarController.toolbarType = .None
  422. }
  423. let alertView = CustomAlertView(message: NSLocalizedString("Read Mode Off", comment: ""), from: self.view, with: .blue, backgroundColor: NSColor(hex: "#36383B"))
  424. }
  425. @objc func highlightLinks() {
  426. let highlightLinks = KMPreferenceManager.shared.highlightLinks
  427. KMPreferenceManager.shared.highlightLinks = !highlightLinks
  428. }
  429. }