KMNoteReplyHanddler.swift 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299
  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: .minY)
  178. self.popover_ = popover
  179. }
  180. func markAnnotation(_ anno: CPDFAnnotation?) {
  181. guard let replyA = self.fetchMarkAnnotation(anno) else {
  182. anno?.createReplyStateAnnotation(.marked)
  183. return
  184. }
  185. if replyA.getAnnotState() == .unMarked {
  186. replyA.setAnnotState(.marked)
  187. }
  188. }
  189. func unMarkAnnotation(_ anno: CPDFAnnotation?) {
  190. guard let replyA = self.fetchMarkAnnotation(anno) else {
  191. return
  192. }
  193. if replyA.getAnnotState() == .marked {
  194. replyA.setAnnotState(.unMarked)
  195. }
  196. }
  197. func updateAnnoState(anno: CPDFAnnotation?, state: CPDFAnnotationState) {
  198. guard let theAnno = self.fetchReviewAnnotation(anno) else {
  199. anno?.createReplyStateAnnotation(state)
  200. return
  201. }
  202. theAnno.setAnnotState(state)
  203. }
  204. func createReplyAnnotation(_ anno: CPDFAnnotation?, content: String?, userName: String?) -> CPDFAnnotation? {
  205. guard let theAnno = anno else {
  206. return nil
  207. }
  208. let a = theAnno.createReply()
  209. a?.contents = content ?? ""
  210. a?.setUserName(userName ?? "")
  211. return a
  212. }
  213. func fetchReviewState(_ anno: CPDFAnnotation?) -> CPDFAnnotationState? {
  214. guard let theAnno = self.fetchReviewAnnotation(anno) else {
  215. return nil
  216. }
  217. return theAnno.getAnnotState()
  218. }
  219. func fetchAnnoState(_ anno: CPDFAnnotation?) -> CPDFAnnotationState? {
  220. guard let replyA = self.fetchMarkAnnotation(anno) else {
  221. return nil
  222. }
  223. return replyA.getAnnotState()
  224. }
  225. func fetchReplyAnnotations(_ anno: CPDFAnnotation?) -> [CPDFAnnotation]? {
  226. guard let theAnno = anno else {
  227. return nil
  228. }
  229. var annos: [CPDFAnnotation] = []
  230. for a in theAnno.replyAnnotations ?? [] {
  231. if a.replyAnnotationType == .reply {
  232. annos.append(a)
  233. }
  234. }
  235. return annos
  236. }
  237. func fetchReviewAnnotation(_ anno: CPDFAnnotation?) -> CPDFAnnotation? {
  238. guard let theAnno = anno else {
  239. return nil
  240. }
  241. for a in theAnno.replyAnnotations ?? [] {
  242. if a.replyAnnotationType == .review {
  243. return a
  244. }
  245. }
  246. return nil
  247. }
  248. func fetchMarkAnnotation(_ anno: CPDFAnnotation?) -> CPDFAnnotation? {
  249. guard let theAnno = anno else {
  250. return nil
  251. }
  252. for replyA in theAnno.replyAnnotations ?? [] {
  253. if replyA.replyAnnotationType == .mark {
  254. return replyA
  255. }
  256. }
  257. return nil
  258. }
  259. }
  260. extension KMNoteReplyHanddler: NSPopoverDelegate {
  261. func popoverWillClose(_ notification: Notification) {
  262. if let data = self.popover_?.isEqual(to: notification.object), data {
  263. self.popover_ = nil
  264. }
  265. }
  266. }