KMNoteReplyHanddler.swift 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441
  1. //
  2. // KMNoteReplyHanddler.swift
  3. // PDF Reader Pro
  4. //
  5. // Created by User-Tangchao on 2024/9/19.
  6. //
  7. import Cocoa
  8. // 注释回复处理类
  9. func KMPDFAnnotationStateGetString(state: CPDFAnnotationState) -> String? {
  10. if state == .none {
  11. return NSLocalizedString("None", comment: "")
  12. } else if state == .unMarked {
  13. return NSLocalizedString("Unmarked", comment: "")
  14. } else if state == .marked {
  15. return NSLocalizedString("Marked", comment: "")
  16. } else if state == .accepted {
  17. return NSLocalizedString("Accepted", comment: "")
  18. } else if state == .rejected {
  19. return NSLocalizedString("Rejected", comment: "")
  20. } else if state == .canceled {
  21. return NSLocalizedString("Cancelled", comment: "")
  22. } else if state == .completed {
  23. return NSLocalizedString("Completed", comment: "")
  24. } else if state == .error {
  25. return NSLocalizedString("Error", comment: "")
  26. }
  27. return nil
  28. }
  29. func KMPDFAnnotationStateGetIcon(state: CPDFAnnotationState) -> String? {
  30. if state == .none {
  31. return "KMImageNameBotaNoteStateNone"
  32. } else if state == .unMarked {
  33. return "KMImageNameBotaNoteMarkup"
  34. } else if state == .marked {
  35. return "KMImageNameBotaNoteMarkSelected"
  36. } else if state == .accepted {
  37. return "KMImageNameBotaNoteStateAccepted"
  38. } else if state == .rejected {
  39. return "KMImageNameBotaNoteStateRejected"
  40. } else if state == .canceled {
  41. return "KMImageNameBotaNoteStateCancelled"
  42. } else if state == .completed {
  43. return "KMImageNameBotaNoteStateCompleted"
  44. } else if state == .error {
  45. }
  46. return nil
  47. }
  48. class KMNoteReplyPopController: KMHomePopViewController {
  49. override func updateUI() {
  50. customBox.fillColor = background
  51. var widthMax: Float = 0;
  52. let subViews: [NSView] = self.customBox.contentView!.subviews
  53. for subView in subViews {
  54. subView.removeFromSuperview()
  55. }
  56. for string in self.dataArr ?? [] {
  57. if !(string as AnyObject).isEqual(to: KMHorizontalLine) {
  58. let width = self.cellContentAdaptiveWidth(string)
  59. if widthMax < width {
  60. widthMax = width
  61. }
  62. }
  63. }
  64. var formTopFloat: Float = 4.0
  65. // for i in (0..<dataArr!.count).reversed() {
  66. for string in dataArr?.reversed() ?? [] {
  67. if (string as AnyObject).isEqual(to: KMHorizontalLine) {
  68. self.createHonrizontalLineWithFrame(CGRect(x: 12.0, y: CGFloat(formTopFloat), width: CGFloat(widthMax)+23, height: 11))
  69. formTopFloat += 11
  70. } else {
  71. popCellViewDownString = string
  72. createPopViewCellLabelWithFrame(formTopFloat, stringValue: string)
  73. formTopFloat += 32;
  74. }
  75. }
  76. customBoxWidthLayoutConstraint.constant = CGFloat(widthMax+47+60)
  77. customBoxHeightLayoutConstraint.constant = CGFloat(formTopFloat+4.0)
  78. }
  79. override func createPopViewCellLabelWithFrame(_ mas_top: Float, stringValue: String) {
  80. var isDisabled = false
  81. if disItems.contains(stringValue) {
  82. isDisabled = true
  83. }
  84. var isSelected = false
  85. if (isDisabled == false && self.selectedItems.contains(stringValue)) {
  86. isSelected = true
  87. }
  88. let box: KMBox = KMBox(frame: NSZeroRect)
  89. box.boxType = .custom
  90. box.borderWidth = 0.0
  91. box.contentViewMargins = NSMakeSize(0, 0)
  92. box.cornerRadius = 4.0
  93. customBox.contentView?.addSubview(box)
  94. box.mas_makeConstraints { (make) in
  95. make?.leading.equalTo()(12.0)
  96. make?.trailing.equalTo()(-12)
  97. make?.height.equalTo()(32.0)
  98. make?.top.equalTo()(customBox.mas_top)?.offset()(CGFloat(mas_top))
  99. }
  100. let iv = NSImageView()
  101. box.addSubview(iv)
  102. iv.mas_makeConstraints { make in
  103. make?.leading.equalTo()(4)
  104. make?.size.equalTo()(NSSize(width: 20, height: 20))
  105. make?.centerY.equalTo()(0.0)
  106. }
  107. if stringValue == NSLocalizedString("Accepted", comment: "") {
  108. iv.image = NSImage(named: "KMImageNameBotaNoteStateAccepted")
  109. } else if stringValue == NSLocalizedString("Rejected", comment: "") {
  110. iv.image = NSImage(named: "KMImageNameBotaNoteStateRejected")
  111. } else if stringValue == NSLocalizedString("Cancelled", comment: "") {
  112. iv.image = NSImage(named: "KMImageNameBotaNoteStateCancelled")
  113. } else if stringValue == NSLocalizedString("Completed", comment: "") {
  114. iv.image = NSImage(named: "KMImageNameBotaNoteStateCompleted")
  115. } else if stringValue == NSLocalizedString("None", comment: "") {
  116. iv.image = NSImage(named: "KMImageNameBotaNoteStateNone")
  117. }
  118. let boxLabel: NSTextField = NSTextField.init()
  119. boxLabel.isEditable = false
  120. boxLabel.isBordered = false
  121. boxLabel.stringValue = stringValue
  122. boxLabel.font = NSFont.systemFont(ofSize: 14.0)
  123. boxLabel.translatesAutoresizingMaskIntoConstraints = false
  124. boxLabel.backgroundColor = NSColor.clear
  125. boxLabel.textColor = textColor//NSColor.km_init(hex: "#252629")
  126. box.contentView?.addSubview(boxLabel)
  127. boxLabel.mas_makeConstraints { (make) in
  128. make?.centerY.equalTo()(0.0)
  129. make?.leading.equalTo()(28.0)
  130. }
  131. let textTypography = KMDesignToken.shared.typography(withToken: "dropdown.m.mac-text.def")
  132. var fontFamily: String = textTypography.fontFamily
  133. let fontWeight: String = textTypography.fontWeight
  134. if fontFamily.contains(" ") {
  135. fontFamily = fontFamily.replacingOccurrences(of: " ", with: "")
  136. }
  137. if fontWeight != "" {
  138. fontFamily = String(format: "%@-%@", fontFamily, fontWeight)
  139. }
  140. boxLabel.font = NSFont(name: fontFamily, size: textTypography.fontSize.stringToCGFloat()) ?? NSFont.systemFont(ofSize: textTypography.fontSize.stringToCGFloat())
  141. let paragraphStyle = NSMutableParagraphStyle()
  142. paragraphStyle.lineSpacing = textTypography.lineHeight.stringToCGFloat()
  143. boxLabel.attributedStringValue = NSAttributedString(string: stringValue, attributes: [NSAttributedString.Key.paragraphStyle: paragraphStyle])
  144. box.moveCallback = {(mouseEntered: Bool, mouseBox: KMBox) -> Void in
  145. if !isDisabled {
  146. if isSelected { // 选中没有 hover 效果
  147. return
  148. }
  149. if mouseEntered {
  150. mouseBox.fillColor = self.enterFillColor
  151. } else {
  152. mouseBox.fillColor = NSColor.clear
  153. }
  154. }
  155. }
  156. box.downCallback = {(downEntered, mouseBox, event) -> Void in
  157. if !isDisabled {
  158. if downEntered {
  159. mouseBox.fillColor = KMDesignToken.shared.fill(withToken: "dropdown.m.bg.sel")
  160. boxLabel.textColor = KMDesignToken.shared.fill(withToken: "dropdown.m.mac-text.sel")
  161. if let callback = self.downCallback {
  162. callback(true, stringValue)
  163. }
  164. } else {
  165. mouseBox.fillColor = KMDesignToken.shared.fill(withToken: "dropdown.m.bg.norm")
  166. boxLabel.textColor = KMDesignToken.shared.fill(withToken: "dropdown.m.mac-text.def")
  167. }
  168. }
  169. }
  170. if isDisabled {
  171. box.fillColor = KMDesignToken.shared.fill(withToken: "dropdown.m.bg.dis")
  172. boxLabel.textColor = KMDesignToken.shared.fill(withToken: "dropdown.m.mac-text.dis")
  173. } else if (isSelected) {
  174. if KMAppearance.isDarkMode() {
  175. box.fillColor = NSColor(hex: "#227AFF").withAlphaComponent(0.3)
  176. } else {
  177. box.fillColor = KMDesignToken.shared.fill(withToken: "dropdown.m.bg.sel")
  178. }
  179. // boxLabel.textColor = KMDesignToken.shared.fill(withToken: "dropdown.m.mac-text.sel")
  180. }
  181. let idx = self.dataArr?.index(of: stringValue) ?? 0
  182. self.viewWillShow?(box, idx)
  183. }
  184. }
  185. class KMNoteReplyHanddler: NSObject {
  186. weak var viewC: KMLeftSideViewController?
  187. private weak var popover_: NSPopover?
  188. func showStatePopView(sender: NSView, anno: CPDFAnnotation?) {
  189. if let _ = self.popover_ {
  190. return
  191. }
  192. //
  193. let datas = [NSLocalizedString("Accepted", comment: ""), NSLocalizedString("Rejected", comment: ""), NSLocalizedString("Cancelled", comment: ""), NSLocalizedString("Completed", comment: ""), NSLocalizedString("None", comment: "")]
  194. let vc = KMNoteReplyPopController(nibName: "KMHomePopViewController", bundle: nil)
  195. _ = vc.initWithPopViewDataArr(datas)
  196. vc.background = KMAppearance.Layout.bgColor()
  197. vc.textColor = KMAppearance.Layout.h0Color()
  198. vc.enterFillColor = KMAppearance.Interactive.s0Color()
  199. let state = self.fetchReviewState(anno) ?? .none
  200. let stateStr = KMPDFAnnotationStateGetString(state: state) ?? NSLocalizedString("None", comment: "")
  201. vc.selectedItems = [stateStr]
  202. vc.downCallback = { [weak self] result, data in
  203. self?.popover_?.close()
  204. if data == NSLocalizedString("Accepted", comment: "") {
  205. self?.updateAnnoState(anno: anno, state: .accepted)
  206. } else if data == NSLocalizedString("Rejected", comment: "") {
  207. self?.updateAnnoState(anno: anno, state: .rejected)
  208. } else if data == NSLocalizedString("Cancelled", comment: "") {
  209. self?.updateAnnoState(anno: anno, state: .canceled)
  210. } else if data == NSLocalizedString("Completed", comment: "") {
  211. self?.updateAnnoState(anno: anno, state: .completed)
  212. } else if data == NSLocalizedString("None", comment: "") {
  213. self?.updateAnnoState(anno: anno, state: .none)
  214. }
  215. FMTrackEventManager.defaultManager.trackEvent(event: "LeftSidebar", withProperties: ["LeftSidebar_Btn": "Btn_LeftSidebar_AnnotationMark"])
  216. self?.viewC?.noteOutlineView.reloadData()
  217. }
  218. let popover = NSPopover()
  219. popover.contentViewController = vc
  220. popover.animates = true
  221. popover.behavior = .semitransient
  222. popover.setValue(true, forKey: "shouldHideAnchor")
  223. popover.delegate = self
  224. popover.show(relativeTo: sender.bounds, of: sender, preferredEdge: .maxY)
  225. self.popover_ = popover
  226. }
  227. func showReplyMorePopView(sender: NSView, replyModel: KMBotaAnnotationReplyModel?) {
  228. if let _ = self.popover_ {
  229. return
  230. }
  231. //
  232. let datas = [NSLocalizedString("Edit", comment: ""), NSLocalizedString("Delete", comment: "")]
  233. let vc = KMHomePopViewController(nibName: "KMHomePopViewController", bundle: nil)
  234. _ = vc.initWithPopViewDataArr(datas)
  235. vc.background = KMAppearance.Layout.bgColor()
  236. vc.textColor = KMAppearance.Layout.h0Color()
  237. vc.enterFillColor = KMAppearance.Interactive.s0Color()
  238. vc.downCallback = { [weak self] result, data in
  239. self?.popover_?.close()
  240. if data == NSLocalizedString("Edit", comment: "") {
  241. self?.editReplyAnnotation(replyModel: replyModel)
  242. DispatchQueue.main.async {
  243. if let row = self?.viewC?.noteOutlineView.row(forItem: replyModel?.annoModel?.footerModel) {
  244. self?.viewC?.noteOutlineView.scrollRowToVisible(row)
  245. }
  246. }
  247. } else if data == NSLocalizedString("Delete", comment: "") {
  248. if let model = replyModel {
  249. self?.removeReplyAnnotation(model.replyAnno)
  250. model.annoModel?.replyAnnos.removeObject(model)
  251. }
  252. }
  253. // self?.viewC?.reloadAnnotation()
  254. self?.viewC?.noteOutlineView.reloadData()
  255. }
  256. let popover = NSPopover()
  257. popover.contentViewController = vc
  258. popover.animates = true
  259. popover.behavior = .semitransient
  260. popover.setValue(true, forKey: "shouldHideAnchor")
  261. popover.delegate = self
  262. popover.show(relativeTo: sender.bounds, of: sender, preferredEdge: .maxY)
  263. self.popover_ = popover
  264. }
  265. func markAnnotation(_ anno: CPDFAnnotation?) {
  266. guard let replyA = self.fetchMarkAnnotation(anno) else {
  267. anno?.createReplyStateAnnotation(.marked)
  268. if let data = self.viewC?.view.window {
  269. KMTools.setDocumentEditedState(window: data)
  270. }
  271. return
  272. }
  273. if replyA.getAnnotState() == .unMarked {
  274. replyA.setAnnotState(.marked)
  275. if let data = self.viewC?.view.window {
  276. KMTools.setDocumentEditedState(window: data)
  277. }
  278. }
  279. }
  280. func unMarkAnnotation(_ anno: CPDFAnnotation?) {
  281. guard let replyA = self.fetchMarkAnnotation(anno) else {
  282. return
  283. }
  284. if replyA.getAnnotState() == .marked {
  285. replyA.setAnnotState(.unMarked)
  286. if let data = self.viewC?.view.window {
  287. KMTools.setDocumentEditedState(window: data)
  288. }
  289. }
  290. }
  291. func updateAnnoState(anno: CPDFAnnotation?, state: CPDFAnnotationState) {
  292. guard let theAnno = self.fetchReviewAnnotation(anno) else {
  293. anno?.createReplyStateAnnotation(state)
  294. if let data = self.viewC?.view.window {
  295. KMTools.setDocumentEditedState(window: data)
  296. }
  297. return
  298. }
  299. theAnno.setAnnotState(state)
  300. if let data = self.viewC?.view.window {
  301. KMTools.setDocumentEditedState(window: data)
  302. }
  303. }
  304. func createReplyAnnotation(_ anno: CPDFAnnotation?, content: String?, userName: String?) -> CPDFAnnotation? {
  305. guard let theAnno = anno else {
  306. return nil
  307. }
  308. let a = theAnno.createReply()
  309. a?.contents = content ?? ""
  310. a?.setUserName(userName ?? "")
  311. a?.bounds = .zero
  312. if let data = self.viewC?.view.window {
  313. KMTools.setDocumentEditedState(window: data)
  314. }
  315. return a
  316. }
  317. func editReplyAnnotation(replyModel: KMBotaAnnotationReplyModel?) {
  318. guard let model = replyModel else {
  319. return
  320. }
  321. model.annoModel?.footerModel?.isExpand = true
  322. model.annoModel?.footerModel?.replyModel = model
  323. }
  324. func removeReplyAnnotation(_ anno: CPDFAnnotation?) {
  325. guard let theAnno = anno else {
  326. return
  327. }
  328. theAnno.page.removeAnnotation(theAnno)
  329. if let data = self.viewC?.view.window {
  330. KMTools.setDocumentEditedState(window: data)
  331. }
  332. }
  333. func fetchReviewState(_ anno: CPDFAnnotation?) -> CPDFAnnotationState? {
  334. guard let theAnno = self.fetchReviewAnnotation(anno) else {
  335. return nil
  336. }
  337. return theAnno.getAnnotState()
  338. }
  339. func fetchAnnoState(_ anno: CPDFAnnotation?) -> CPDFAnnotationState? {
  340. guard let replyA = self.fetchMarkAnnotation(anno) else {
  341. return nil
  342. }
  343. return replyA.getAnnotState()
  344. }
  345. func fetchReplyAnnotations(_ anno: CPDFAnnotation?) -> [CPDFAnnotation]? {
  346. guard let theAnno = anno else {
  347. return nil
  348. }
  349. var annos: [CPDFAnnotation] = []
  350. for a in theAnno.replyAnnotations ?? [] {
  351. if a.replyAnnotationType == .reply {
  352. annos.append(a)
  353. }
  354. }
  355. return annos
  356. }
  357. func fetchReviewAnnotation(_ anno: CPDFAnnotation?) -> CPDFAnnotation? {
  358. guard let theAnno = anno else {
  359. return nil
  360. }
  361. for a in theAnno.replyAnnotations ?? [] {
  362. if a.replyAnnotationType == .review {
  363. return a
  364. }
  365. }
  366. return nil
  367. }
  368. func fetchMarkAnnotation(_ anno: CPDFAnnotation?) -> CPDFAnnotation? {
  369. guard let theAnno = anno else {
  370. return nil
  371. }
  372. for replyA in theAnno.replyAnnotations ?? [] {
  373. if replyA.replyAnnotationType == .mark {
  374. return replyA
  375. }
  376. }
  377. return nil
  378. }
  379. }
  380. extension KMNoteReplyHanddler: NSPopoverDelegate {
  381. func popoverWillClose(_ notification: Notification) {
  382. if let data = self.popover_?.isEqual(to: notification.object), data {
  383. self.popover_ = nil
  384. }
  385. }
  386. }