KMNoteReplyHanddler.swift 13 KB

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