KMHomePopViewController.swift 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398
  1. //
  2. // KMHomePopViewController.swift
  3. // PDF Reader Pro
  4. //
  5. // Created by wanjun on 2022/10/17.
  6. //
  7. import Cocoa
  8. public enum DataNavigationViewButtonActionType : Int, CaseIterable {
  9. case Batch = 0 // 批量处理
  10. case ConvertPDF // 转换PDF
  11. case ImageToPDF // 图片转PDF
  12. case MergePDF // 合并
  13. case Compression // 压缩
  14. case Security // 安全
  15. case PDFToPPT // PDF转PPT
  16. case PDFToExcel // PDF转Excel
  17. case PDFToWord // PDF转Word
  18. case PDFToImage // PDF转图片
  19. case Watermark // 水印
  20. case Background // 背景
  21. case HeaderAndFooter// 页眉页脚
  22. case BatesCode // 贝茨码
  23. case Print // 打印
  24. case PrintMultipage // 多页
  25. case PrintPoster // 海报
  26. case PrintBooklet // 小册子
  27. case BatchRemove // 批量移除
  28. case Insert // 插入
  29. case BreakUp // 拆分
  30. case Extract // 提取
  31. case MarkCipher // 标记密文
  32. case PageEdit // 页面编辑
  33. case OCR // 批量OCR
  34. case AutomaticFormRecognition //表单自动识别
  35. case FileCompare // 文件对比
  36. case ComparativeTable // 比较表
  37. case equity // 权益弹窗
  38. case AITools //AI Tools
  39. }
  40. typealias popCellViewDownCallback = (_ downEntered: Bool, _ count: String) -> Void
  41. typealias popCellViewWillShow = (_ cellView: KMBox, _ index: Int) -> Void
  42. @objcMembers class KMHomePopViewController: NSViewController {
  43. @IBOutlet weak var customBox: NSBox!
  44. @IBOutlet weak var customBoxWidthLayoutConstraint: NSLayoutConstraint!
  45. @IBOutlet weak var customBoxHeightLayoutConstraint: NSLayoutConstraint!
  46. var downCallback: popCellViewDownCallback?
  47. var viewWillShow: popCellViewWillShow?
  48. var popCellViewDownString: String?
  49. var popCellCount: Int?
  50. var dataArr: [String]?
  51. var KMHorizontalLine: String = "KMHorizontalLine"
  52. var enterFillColor : NSColor = .black
  53. var textColor : NSColor = .black // 背景颜色
  54. var background : NSColor = .white // 背景颜色
  55. var background_hov : NSColor = .clear // 背景颜色
  56. var background_sel : NSColor = .clear // 背景颜色
  57. var background_disabled : NSColor = .clear // 背景颜色
  58. var cornerRadius : Float = 0.0 // 边框圆角
  59. var cornerRadius_hov : Float = 0.0 // 边框圆角
  60. var cornerRadius_sel : Float = 0.0 // 边框圆角
  61. var cornerRadius_disabled : Float = 0.0 // 边框圆角
  62. var lineHeight : CGFloat = 20.0 // 默认 内容行高
  63. var lineHeight_hov : CGFloat = 20.0 // 默认 内容行高
  64. var lineHeight_sel : CGFloat = 20.0 // 默认 内容行高
  65. var lineHeight_disabled : CGFloat = 20.0 // 默认 内容行高
  66. var font : NSFont = NSFont.systemFont(ofSize: 14.0) // 内容字体
  67. var font_hov : NSFont = NSFont.systemFont(ofSize: 14.0) // 内容字体
  68. var font_sel : NSFont = NSFont.systemFont(ofSize: 14.0) // 内容字体
  69. var font_disabled : NSFont = NSFont.systemFont(ofSize: 14.0) // 内容字体
  70. var _state: KMDesignTokenState = .Norm
  71. var enabled: Bool = true // 是否可点击
  72. var canHover: Bool = true // 是否可悬浮
  73. var disItems: [String] = []
  74. var selectedItems: [String] = []
  75. var showVerticalScroller: Bool = false
  76. func initWithPopViewDataArr(_ popViewDataArr: [String]) -> Self {
  77. // self.dataArr = popViewDataArr.reverseObjectEnumerator().allObjects as NSArray
  78. self.dataArr = popViewDataArr.reversed()
  79. return self
  80. }
  81. override func viewDidLoad() {
  82. super.viewDidLoad()
  83. customBox.fillColor = background//NSColor.km_init(hex: "#FFFFFF")
  84. }
  85. override func viewDidAppear() {
  86. super.viewDidAppear()
  87. self.updateUI()
  88. }
  89. // MARK: Private
  90. func updateUI() {
  91. customBox.fillColor = background
  92. var widthMax: Float = 0;
  93. let subViews: [NSView] = self.customBox.contentView!.subviews
  94. for subView in subViews {
  95. subView.removeFromSuperview()
  96. }
  97. for string in self.dataArr ?? [] {
  98. if !(string as AnyObject).isEqual(to: KMHorizontalLine) {
  99. let width = self.cellContentAdaptiveWidth(string)
  100. if widthMax < width {
  101. widthMax = width
  102. }
  103. }
  104. }
  105. var formTopFloat: Float = 4.0
  106. // for i in (0..<dataArr!.count).reversed() {
  107. for string in dataArr?.reversed() ?? [] {
  108. if (string as AnyObject).isEqual(to: KMHorizontalLine) {
  109. self.createHonrizontalLineWithFrame(CGRect(x: 12.0, y: CGFloat(formTopFloat), width: CGFloat(widthMax)+23, height: 11))
  110. formTopFloat += 11
  111. } else {
  112. popCellViewDownString = string
  113. // self.createPopViewCellLabelWithFrame(CGRect(x: 4.0, y: CGFloat(formTopFloat), width: CGFloat(widthMax)+47, height: 32), stringValue: string)
  114. createPopViewCellLabelWithFrame(formTopFloat, stringValue: string)
  115. formTopFloat += 32;
  116. }
  117. }
  118. customBoxWidthLayoutConstraint.constant = CGFloat(widthMax+47)
  119. customBoxHeightLayoutConstraint.constant = CGFloat(formTopFloat+4.0)
  120. }
  121. func createHonrizontalLineWithFrame(_ frame: CGRect) {
  122. let box: NSBox = NSBox.init(frame: frame)
  123. box.boxType = .custom
  124. box.borderWidth = 0.0
  125. box.contentViewMargins = NSMakeSize(0, 0)
  126. customBox.contentView?.addSubview(box)
  127. box.mas_makeConstraints { (make) in
  128. make?.leading.equalTo()(frame.origin.x)
  129. make?.trailing.equalTo()(-frame.origin.x)
  130. make?.height.equalTo()(frame.size.height)
  131. make?.top.equalTo()(customBox.mas_top)?.offset()(CGFloat(frame.origin.y))
  132. }
  133. let lineBox = NSBox.init(frame: CGRect(x: 0, y: 6, width: frame.width, height: 1))
  134. lineBox.boxType = .separator
  135. box.addSubview(lineBox)
  136. }
  137. func createPopViewCellLabelWithFrame(_ mas_top: Float, stringValue: String) {
  138. var isDisabled = false
  139. if disItems.contains(stringValue) {
  140. isDisabled = true
  141. }
  142. var isSelected = false
  143. if (isDisabled == false && self.selectedItems.contains(stringValue)) {
  144. isSelected = true
  145. }
  146. let box: KMBox = KMBox(frame: NSZeroRect)
  147. box.boxType = .custom
  148. box.borderWidth = 0.0
  149. box.contentViewMargins = NSMakeSize(0, 0)
  150. box.cornerRadius = 4.0
  151. customBox.contentView?.addSubview(box)
  152. box.mas_makeConstraints { (make) in
  153. make?.leading.equalTo()(4.0)
  154. make?.trailing.equalTo()(-4.0)
  155. make?.height.equalTo()(32.0)
  156. make?.top.equalTo()(customBox.mas_top)?.offset()(CGFloat(mas_top))
  157. }
  158. let boxLabel: NSTextField = NSTextField.init()
  159. boxLabel.isEditable = false
  160. boxLabel.isBordered = false
  161. boxLabel.stringValue = stringValue
  162. boxLabel.font = NSFont.systemFont(ofSize: 14.0)
  163. boxLabel.translatesAutoresizingMaskIntoConstraints = false
  164. boxLabel.backgroundColor = NSColor.clear
  165. boxLabel.textColor = textColor//NSColor.km_init(hex: "#252629")
  166. box.contentView?.addSubview(boxLabel)
  167. boxLabel.mas_makeConstraints { (make) in
  168. make?.centerY.equalTo()(0.0)
  169. make?.leading.equalTo()(8.0)
  170. }
  171. let idx = self.dataArr?.index(of: stringValue) ?? 0
  172. self.viewWillShow?(box, idx)
  173. }
  174. func cellContentAdaptiveWidth(_ content: String) -> Float {
  175. if content.isEmpty {
  176. return 0
  177. }
  178. let attributes = [NSAttributedString.Key.font : NSFont.systemFont(ofSize: 14.0)]
  179. let rect : CGRect = content.boundingRect(with: CGSize(width: 0, height: 18),options: .usesLineFragmentOrigin, attributes: attributes,context:nil)
  180. return Float(rect.size.width)
  181. }
  182. func changePopViewCellData(_ count: Int, content: String) {
  183. let boxArray: Array<NSView> = customBox.contentView!.subviews
  184. for subView in boxArray {
  185. subView.removeFromSuperview()
  186. }
  187. var dataMutableArr: [String] = Array((dataArr?.reversed())!)
  188. dataMutableArr[count] = content
  189. dataArr = Array(dataMutableArr.reversed())
  190. self.updateUI()
  191. }
  192. // MARK: - Init Views
  193. fileprivate func initBoxLabel() -> NSTextField {
  194. let label = NSTextField.init()
  195. label.isEditable = false
  196. label.isBordered = false
  197. label.font = NSFont.systemFont(ofSize: 14.0)
  198. label.translatesAutoresizingMaskIntoConstraints = false
  199. label.backgroundColor = NSColor.clear
  200. label.textColor = NSColor.km_init(hex: "#252629")
  201. return label
  202. }
  203. }
  204. class KMScrollPopViewController: KMHomePopViewController {
  205. private var scrollView = NSScrollView()
  206. private var contentView = NSView()
  207. private var currentItemPosition: NSPoint = .zero
  208. convenience init() {
  209. self.init(nibName: "KMHomePopViewController", bundle: nil)
  210. }
  211. override func viewDidLoad() {
  212. super.viewDidLoad()
  213. self.view.addSubview(self.scrollView)
  214. self.scrollView.documentView = self.contentView
  215. self.scrollView.documentView?.backgroundColor(self.background)
  216. self.scrollView.hasVerticalScroller = self.showVerticalScroller
  217. self.scrollView.borderType = .noBorder
  218. self.scrollView.drawsBackground = false
  219. }
  220. override func viewDidAppear() {
  221. super.viewDidAppear()
  222. DispatchQueue.main.async {
  223. let contentView = self.scrollView.contentView
  224. let pageH = NSHeight(self.scrollView.bounds)
  225. var numberPages: Int = 0
  226. var currentPage: Int = 0
  227. var scrollY: CGFloat = 0
  228. if (pageH > 0) {
  229. numberPages = Int(NSHeight(self.contentView.bounds) / pageH) + 1
  230. currentPage = Int((self.currentItemPosition.y + 32 + 4 * 2) / pageH)
  231. if (currentPage == (numberPages - 1)) {
  232. scrollY = 0
  233. } else {
  234. scrollY = NSHeight(self.contentView.bounds) - pageH * CGFloat(currentPage+1)
  235. }
  236. }
  237. contentView.scroll(to: NSPoint(x: 0, y: scrollY))
  238. }
  239. }
  240. override func viewDidLayout() {
  241. super.viewDidLayout()
  242. self.scrollView.frame = NSMakeRect(0, 0, NSWidth(self.view.frame), NSHeight(self.view.window!.frame))
  243. self.contentView.frame = NSMakeRect(0, 0, NSWidth(self.customBox.frame), NSHeight(self.customBox.frame)+30)
  244. }
  245. override func updateUI() {
  246. super.updateUI()
  247. customBox.fillColor = background
  248. var widthMax: Float = 0
  249. for subView in self.contentView.subviews {
  250. subView.removeFromSuperview()
  251. }
  252. for string in self.dataArr ?? [] {
  253. if ((string as AnyObject).isEqual(to: KMHorizontalLine)) {
  254. continue
  255. }
  256. let width = self.cellContentAdaptiveWidth(string)
  257. if (widthMax < width) {
  258. widthMax = width
  259. }
  260. }
  261. var formTopFloat: Float = 4.0
  262. for string in dataArr?.reversed() ?? [] {
  263. if (string as AnyObject).isEqual(to: KMHorizontalLine) {
  264. self.createHonrizontalLineWithFrame(CGRect(x: 12.0, y: CGFloat(formTopFloat), width: CGFloat(widthMax)+23, height: 11))
  265. formTopFloat += 11
  266. } else {
  267. self.popCellViewDownString = string
  268. self.createPopViewCellLabelWithFrame(formTopFloat, stringValue: string)
  269. formTopFloat += 32
  270. }
  271. }
  272. self.customBoxWidthLayoutConstraint.constant = CGFloat(widthMax+47)
  273. self.customBoxHeightLayoutConstraint.constant = CGFloat(formTopFloat+4.0)
  274. }
  275. override func createHonrizontalLineWithFrame(_ frame: CGRect) {
  276. let box: NSBox = NSBox.init(frame: frame)
  277. box.boxType = .custom
  278. box.borderWidth = 0.0
  279. box.contentViewMargins = NSMakeSize(0, 0)
  280. self.contentView.addSubview(box)
  281. let lineBox = NSBox.init(frame: CGRect(x: 0, y: 6, width: frame.width, height: 1))
  282. lineBox.boxType = .separator
  283. box.addSubview(lineBox)
  284. }
  285. override func createPopViewCellLabelWithFrame(_ mas_top: Float, stringValue: String) {
  286. var isDisabled = false
  287. if (self.disItems.contains(stringValue)) {
  288. isDisabled = true
  289. }
  290. var isSelected = false
  291. if (isDisabled == false && self.selectedItems.contains(stringValue)) {
  292. isSelected = true
  293. }
  294. if (isSelected && self.selectedItems.first == stringValue) {
  295. self.currentItemPosition = NSPoint(x: 0, y: Int(mas_top))
  296. }
  297. let box: KMBox = KMBox(frame: NSZeroRect)
  298. box.boxType = .custom
  299. box.borderWidth = 0.0
  300. box.contentViewMargins = NSMakeSize(0, 0)
  301. box.cornerRadius = 4.0
  302. self.contentView.addSubview(box)
  303. box.mas_makeConstraints { (make) in
  304. make?.leading.equalTo()(4.0)
  305. make?.trailing.equalTo()(-4.0)
  306. make?.height.equalTo()(32.0)
  307. make?.top.equalTo()(self.customBox.mas_top)?.offset()(CGFloat(mas_top))
  308. }
  309. let boxLabel = self.initBoxLabel()
  310. boxLabel.stringValue = stringValue
  311. box.contentView?.addSubview(boxLabel)
  312. boxLabel.mas_makeConstraints { (make) in
  313. make?.centerY.equalTo()(0.0)
  314. make?.leading.equalTo()(8.0)
  315. make?.trailing.mas_equalTo()(0)
  316. }
  317. box.moveCallback = { mouseEntered, mouseBox in
  318. if (isDisabled) {
  319. return
  320. }
  321. if (isSelected) { // 选中没有 hover 效果
  322. return
  323. }
  324. if mouseEntered {
  325. mouseBox.fillColor = self.enterFillColor
  326. } else {
  327. mouseBox.fillColor = NSColor.clear
  328. }
  329. }
  330. box.downCallback = { [unowned self] downEntered, mouseBox, _ in
  331. if (isDisabled) {
  332. return
  333. }
  334. }
  335. let idx = self.dataArr?.index(of: stringValue) ?? 0
  336. self.viewWillShow?(box, idx)
  337. }
  338. }