KMDesignSelect.swift 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559
  1. //
  2. // KMDesignSelect.swift
  3. // PDF Reader Pro
  4. //
  5. // Created by wanjun on 2023/2/22.
  6. //
  7. import Cocoa
  8. @objc protocol KMSelectPopButtonDelegate: NSObjectProtocol {
  9. @objc optional func km_controlTextDidEndEditing(_ obj: KMDesignSelect)
  10. @objc optional func km_controlTextDidChange(_ obj: KMDesignSelect)
  11. @objc optional func km_SelectPopoverWillShow(_ obj: KMDesignSelect)
  12. func km_comboBoxSelectionDidChange(_ obj: KMDesignSelect)
  13. @objc optional func km_cellViewWillShow(_ obj: KMDesignSelect, _ cellView: KMBox, _ index: Int)
  14. }
  15. @objc enum SelectType : Int {
  16. case PopButton = 0
  17. case Combox
  18. }
  19. class KMSelectCell: NSTextFieldCell {
  20. var borderThickness: CGFloat = 1
  21. var offset: CGFloat = 0.0
  22. override func drawingRect(forBounds theRect: NSRect) -> NSRect {
  23. var newRect:NSRect = super.drawingRect(forBounds: theRect)
  24. let textSize:NSSize = self.cellSize(forBounds: theRect)
  25. // let heightDelta:CGFloat = newRect.size.height - textSize.height
  26. // if heightDelta > 0 {
  27. // newRect.size.height = textSize.height
  28. // newRect.origin.y += heightDelta * 0.5
  29. // } else {
  30. // newRect.size.height = textSize.height
  31. // newRect.origin.y += heightDelta
  32. // }
  33. let originY = ((self.controlView?.bounds.size.height)! - textSize.height) / 2
  34. newRect.size.height = textSize.height
  35. newRect.origin.y += originY
  36. newRect.origin.x += offset
  37. newRect.size.width = theRect.width - offset*2
  38. // newRect.fill()
  39. return newRect
  40. }
  41. override func draw(withFrame cellFrame: NSRect, in controlView: NSView) {
  42. // Area that covers the NSTextField itself. That is the total height minus our custom border size.
  43. let interiorFrame = NSRect(x: 0, y: 0, width: cellFrame.width, height: cellFrame.height - borderThickness)
  44. let path = NSBezierPath()
  45. path.lineWidth = borderThickness
  46. // Line width is at the center of the line.
  47. path.move(to: NSPoint(x: 0, y: cellFrame.height))
  48. path.line(to: NSPoint(x: cellFrame.width, y: cellFrame.height))
  49. path.line(to: NSPoint(x: cellFrame.width, y: 0))
  50. path.line(to: NSPoint(x: 0, y: 0))
  51. NSColor.clear.setStroke()
  52. path.stroke()
  53. // Pass in area minus the border thickness in the height
  54. drawInterior(withFrame: interiorFrame, in: controlView)
  55. }
  56. }
  57. @objcMembers class KMDesignSelect: NSViewController {
  58. @IBOutlet weak var mainBox: KMBox!
  59. @IBOutlet weak var selectBox: NSBox!
  60. @IBOutlet weak var textField: NSTextField!
  61. @IBOutlet weak var imageView: NSImageView!
  62. @IBOutlet weak var horizontalPadding_spacing: NSLayoutConstraint!
  63. @IBOutlet weak var itemSpacing_spacing: NSLayoutConstraint!
  64. @IBOutlet weak var imageViewWidth_spacing: NSLayoutConstraint!
  65. @IBOutlet weak var imageViewHeight_spacing: NSLayoutConstraint!
  66. var imageWidth: Float = 12.0 // 图片宽度
  67. var imageHeight: Float = 12.0 // 图片高度
  68. var horizontalPadding: Float = 8.0
  69. var itemSpacing: Float = 8.0
  70. var borderColor: NSColor = .clear// 边框颜色
  71. var borderColor_hov: NSColor = .clear// 边框颜色
  72. var borderColor_focus: NSColor = .clear// 边框颜色
  73. var borderColor_disabled: NSColor = .clear// 边框颜色
  74. var borderColor_errordef: NSColor = .clear// 边框颜色
  75. var borderColor_errorfocus: NSColor = .clear// 边框颜色
  76. var cornerRadius: Float = 0.0// 边框圆角
  77. var cornerRadius_hov: Float = 0.0// 边框圆角
  78. var cornerRadius_focus: Float = 0.0// 边框圆角
  79. var cornerRadius_disabled: Float = 0.0// 边框圆角
  80. var cornerRadius_errordef: Float = 0.0// 边框圆角
  81. var cornerRadius_errorfocus: Float = 0.0// 边框圆角
  82. var borderWidth: Float = 1.0// 边框宽度
  83. var borderWidth_hov: Float = 1.0// 边框宽度
  84. var borderWidth_focus: Float = 1.0// 边框宽度
  85. var borderWidth_disabled: Float = 1.0// 边框宽度
  86. var borderWidth_errordef: Float = 1.0// 边框宽度
  87. var borderWidth_errorfocus: Float = 1.0// 边框宽度
  88. var background: NSColor = .clear// 背景颜色
  89. var background_hov: NSColor = .clear// 背景颜色
  90. var background_focus: NSColor = .clear// 背景颜色
  91. var background_disabled: NSColor = .clear// 背景颜色
  92. var background_errordef: NSColor = .clear// 背景颜色
  93. var background_errorfocus: NSColor = .clear// 背景颜色
  94. var textColor: NSColor = .black // 内容颜色
  95. var textColor_hov: NSColor = .black // 内容颜色
  96. var textColor_focus: NSColor = .black // 内容颜色
  97. var textColor_disabled: NSColor = .black // 内容颜色
  98. var textColor_errordef: NSColor = .black // 内容颜色
  99. var textColor_errorfocus: NSColor = .black // 内容颜色
  100. var lineHeight: CGFloat = 20.0 // 默认 内容行高
  101. var lineHeight_hov: CGFloat = 20.0 // 默认 内容行高
  102. var lineHeight_focus: CGFloat = 20.0 // 默认 内容行高
  103. var lineHeight_disabled: CGFloat = 20.0 // 默认 内容行高
  104. var lineHeight_errordef: CGFloat = 20.0 // 默认 内容行高
  105. var lineHeight_errorfocus: CGFloat = 20.0 // 默认 内容行高
  106. var font: NSFont = NSFont.systemFont(ofSize: 14.0) // 内容字体
  107. var font_hov: NSFont = NSFont.systemFont(ofSize: 14.0) // 内容字体
  108. var font_focus: NSFont = NSFont.systemFont(ofSize: 14.0) // 内容字体
  109. var font_disabled: NSFont = NSFont.systemFont(ofSize: 14.0) // 内容字体
  110. var font_errordef: NSFont = NSFont.systemFont(ofSize: 14.0) // 内容字体
  111. var font_errorfocus: NSFont = NSFont.systemFont(ofSize: 14.0) // 内容字体
  112. var textbg_errorfocus: NSColor = .clear // 正在输入高亮
  113. var _image: NSImage = NSImage(named: "icon_btn_arrow_gray_down_s_norm_false")!
  114. var _stringValue: String = ""// 内容
  115. var _toolTip: String = "" // 提示文字
  116. // button 通用属性
  117. var action: Selector? // 点击事件
  118. var target: AnyObject? // 对象目标
  119. var _enabled: Bool = true // 是否可点击
  120. var _state: KMDesignTokenState = .Norm
  121. var canHover: Bool = true // 是否可悬浮
  122. var _isHidden: Bool = false // 是否隐藏
  123. var _editable: Bool = false // 是否允许编辑
  124. var _alignment: NSTextAlignment = .left //对齐
  125. var placeholderString: String = "" { // 预设文字
  126. didSet {
  127. self.textField.placeholderString = self.placeholderString
  128. }
  129. }
  130. var lineBreakMode: NSLineBreakMode = .byTruncatingTail //文字超出显示
  131. var buttonType: SelectType = .PopButton
  132. var items: [String] = []
  133. open weak var delete: KMSelectPopButtonDelegate?
  134. var _indexOfSelectedItem: Int = 0
  135. var _numberOfItems: Int = 0
  136. var disItems: [String] = []
  137. var isScrollPop = false
  138. var createFilePopover: NSPopover = NSPopover.init()
  139. var popoverBehavior: NSPopover.Behavior = .semitransient
  140. var popViewController: KMHomePopViewController?
  141. var popViewControllerBackground: NSColor?
  142. var popViewControllerTextColor: NSColor?
  143. var popViewControllerEnterFillColor: NSColor?
  144. var showVerticalScroller = false
  145. init(withType type: SelectType) {
  146. super.init(nibName: "KMDesignSelect", bundle: nil)
  147. self.buttonType = type
  148. }
  149. required init?(coder: NSCoder) {
  150. fatalError("init(coder:) has not been implemented")
  151. }
  152. override func viewDidLoad() {
  153. super.viewDidLoad()
  154. // Do view setup here.
  155. textField.isSelectable = false
  156. if (buttonType == .PopButton) {
  157. self.mainBox.contentView = selectBox
  158. textField.delegate = self
  159. self.select(bg: "select.m.bg.norm", text: "select.m.mac-text.act")
  160. self.select(bg: "select.m.bg.hov", text: "select.m.mac-text.act", state: .Hov)
  161. self.select(bg: "select.m.bg.focus", text: "select.m.mac-text.act", state: .Focus)
  162. self.select(bg: "select.m.bg.dis", text: "select.m.mac-text.dis", state: .Disabled)
  163. self.select(bg: "select.m.bg.error-def", text: "select.m.mac-text.act", state: .Error_def)
  164. self.select(bg: "select.m.bg.error-focus", text: "select.m.mac-text.act", textbg: "select.m.bg-text", state: .Error_focus)
  165. } else if (buttonType == .Combox) {
  166. self.mainBox.contentView = selectBox
  167. textField.delegate = self
  168. self.select(bg: "select.m.bg.norm", text: "select.m.mac-text.act")
  169. self.select(bg: "select.m.bg.hov", text: "select.m.mac-text.act", state: .Hov)
  170. self.select(bg: "select.m.bg.focus", text: "select.m.mac-text.act", state: .Focus)
  171. self.select(bg: "select.m.bg.dis", text: "select.m.mac-text.dis", state: .Disabled)
  172. self.select(bg: "select.m.bg.error-def", text: "select.m.mac-text.act", state: .Error_def)
  173. self.select(bg: "select.m.bg.error-focus", text: "select.m.mac-text.act", textbg: "select.m.bg-text", state: .Error_focus)
  174. }
  175. self.mainBox.canHover = true
  176. self.mainBox.canClick = true
  177. self.mainBox.downCallback = { [unowned self](downEntered, mouseBox, event) -> Void in
  178. if self.enabled {
  179. if downEntered {
  180. self.canHover = false
  181. if self.items.count <= 0 {
  182. return
  183. }
  184. self.mainBoxAction(mouseBox)
  185. self.updateUI()
  186. }
  187. }
  188. }
  189. self.mainBox.moveCallback = { [unowned self](mouseEntered: Bool, mouseBox: KMBox) -> Void in
  190. if !self.createFilePopover.isShown && self.canHover && (self.state != .Disabled) {
  191. self.canHover = true
  192. if mouseEntered {
  193. self.state = .Hov
  194. } else {
  195. self.state = .Norm
  196. }
  197. self.updateUI()
  198. }
  199. }
  200. createFilePopover.delegate = self
  201. }
  202. // MARK: Get、Set
  203. var stringValue: String {
  204. get {
  205. return _stringValue
  206. }
  207. set {
  208. _stringValue = newValue
  209. let paragraphStyle = NSMutableParagraphStyle()
  210. if (state == .Norm) {
  211. paragraphStyle.lineSpacing = lineHeight
  212. } else if (state == .Hov) {
  213. paragraphStyle.lineSpacing = lineHeight_hov
  214. } else if (state == .Focus) {
  215. paragraphStyle.lineSpacing = lineHeight_focus
  216. } else if (state == .Disabled) {
  217. paragraphStyle.lineSpacing = lineHeight_disabled
  218. } else if (state == .Error_def) {
  219. paragraphStyle.lineSpacing = lineHeight_errordef
  220. } else if (state == .Error_focus) {
  221. paragraphStyle.lineSpacing = lineHeight_errorfocus
  222. }
  223. paragraphStyle.lineBreakMode = .byTruncatingTail
  224. paragraphStyle.alignment = alignment
  225. textField.attributedStringValue = NSAttributedString(string: _stringValue, attributes: [NSAttributedString.Key.paragraphStyle: paragraphStyle])
  226. }
  227. }
  228. var alert: Bool = false {
  229. didSet {
  230. updateUI()
  231. }
  232. }
  233. var enabled: Bool {
  234. get {
  235. return _enabled
  236. }
  237. set {
  238. _enabled = newValue
  239. if _enabled {
  240. _state = .Norm
  241. } else {
  242. _state = .Disabled
  243. }
  244. updateUI()
  245. }
  246. }
  247. var state: KMDesignTokenState {
  248. get {
  249. return _state
  250. }
  251. set {
  252. _state = newValue
  253. updateUI()
  254. }
  255. }
  256. var isHidden: Bool {
  257. get {
  258. return _isHidden
  259. }
  260. set {
  261. _isHidden = newValue
  262. self.view.isHidden = _isHidden
  263. }
  264. }
  265. var editable: Bool {
  266. get {
  267. return _editable
  268. }
  269. set {
  270. _editable = newValue
  271. textField.isEditable = _editable
  272. }
  273. }
  274. var indexOfSelectedItem: Int {
  275. get {
  276. return _indexOfSelectedItem
  277. }
  278. set {
  279. _indexOfSelectedItem = newValue
  280. }
  281. }
  282. var numberOfItems: Int {
  283. get {
  284. return items.count
  285. }
  286. }
  287. var image: NSImage {
  288. get {
  289. return _image
  290. }
  291. set {
  292. _image = newValue
  293. imageView.image = _image
  294. }
  295. }
  296. var toolTip: String {
  297. get {
  298. return _toolTip
  299. }
  300. set {
  301. _toolTip = newValue
  302. if _toolTip != "" {
  303. mainBox.toolTip = _toolTip
  304. }
  305. }
  306. }
  307. var alignment: NSTextAlignment {
  308. get {
  309. return _alignment
  310. }
  311. set {
  312. _alignment = newValue
  313. textField.alignment = alignment
  314. }
  315. }
  316. // MARK: Private Methods
  317. func updateUI() -> Void {
  318. if (state == .Norm) {
  319. selectBox.fillColor = background
  320. selectBox.borderWidth = CGFloat(borderWidth)
  321. selectBox.cornerRadius = CGFloat(cornerRadius)
  322. selectBox.borderColor = borderColor
  323. textField.textColor = textColor
  324. textField.font = font
  325. imageView.image = NSImage(named: "KMImageNameSelectNormal")
  326. } else if (state == .Hov) {
  327. selectBox.fillColor = background_hov
  328. selectBox.borderWidth = CGFloat(borderWidth_hov)
  329. selectBox.cornerRadius = CGFloat(cornerRadius_hov)
  330. selectBox.borderColor = borderColor_hov
  331. textField.textColor = textColor_hov
  332. textField.font = font_hov
  333. imageView.image = NSImage(named: "KMImageNameSelectHover")
  334. } else if (state == .Focus) {
  335. selectBox.fillColor = background_focus
  336. selectBox.borderWidth = CGFloat(borderWidth_focus)
  337. selectBox.cornerRadius = CGFloat(cornerRadius_focus)
  338. selectBox.borderColor = borderColor_focus
  339. textField.textColor = textColor_focus
  340. textField.font = font_focus
  341. imageView.image = NSImage(named: "KMImageNameSelectFocus")
  342. } else if (state == .Disabled) {
  343. selectBox.fillColor = background_disabled
  344. selectBox.borderWidth = CGFloat(borderWidth_disabled)
  345. selectBox.cornerRadius = CGFloat(cornerRadius_disabled)
  346. selectBox.borderColor = borderColor_disabled
  347. textField.textColor = textColor_disabled
  348. textField.font = font_disabled
  349. imageView.image = NSImage(named: "KMImageNameSelectDisable")
  350. } else if (state == .Error_def) {
  351. selectBox.fillColor = background_errordef
  352. selectBox.borderWidth = CGFloat(borderWidth_errordef)
  353. selectBox.cornerRadius = CGFloat(cornerRadius_errordef)
  354. selectBox.borderColor = borderColor_errordef
  355. textField.textColor = textColor_errordef
  356. textField.font = font_errordef
  357. imageView.image = NSImage(named: "KMImageNameSelectNormal")
  358. } else if (state == .Error_focus) {
  359. selectBox.fillColor = background_errorfocus
  360. selectBox.borderWidth = CGFloat(borderWidth_errorfocus)
  361. selectBox.cornerRadius = CGFloat(cornerRadius_errorfocus)
  362. selectBox.borderColor = borderColor_errorfocus
  363. textField.textColor = textColor_errorfocus
  364. textField.font = font_errorfocus
  365. imageView.image = NSImage(named: "KMImageNameSelectNormal")
  366. }
  367. if alert == true {
  368. selectBox.borderColor = NSColor.km_init(hex: "#F3465B")
  369. } else {
  370. selectBox.borderColor = selectBox.borderColor
  371. }
  372. imageViewWidth_spacing.constant = CGFloat(imageWidth)
  373. imageViewHeight_spacing.constant = CGFloat(imageHeight)
  374. horizontalPadding_spacing.constant = CGFloat(horizontalPadding)
  375. itemSpacing_spacing.constant = CGFloat(itemSpacing)
  376. textField.isEditable = editable
  377. // textField.placeholderString = placeholderString
  378. textField.lineBreakMode = lineBreakMode
  379. popViewController?.updateUI()
  380. }
  381. func removeAllItems() {
  382. items = []
  383. }
  384. func addItems(withObjectValues objects: [String]) {
  385. items = objects
  386. }
  387. func selectItem(at index: Int) {
  388. if items.count > 0 && index < items.count {
  389. stringValue = items[index]
  390. self.indexOfSelectedItem = index
  391. }
  392. }
  393. // MARK: Action
  394. func mainBoxAction(_ sender: Any) -> Void {
  395. self.delete?.km_SelectPopoverWillShow?(self)
  396. if createFilePopover.isShown {
  397. createFilePopover.close()
  398. } else {
  399. var vc: KMHomePopViewController?
  400. if (self.isScrollPop) {
  401. vc = KMScrollPopViewController()
  402. } else {
  403. vc = KMHomePopViewController()
  404. }
  405. if (self.popViewControllerBackground != nil) {
  406. vc?.background = self.popViewControllerBackground!
  407. }
  408. if self.popViewControllerBackground != nil {
  409. vc?.textColor = self.popViewControllerTextColor!
  410. }
  411. if self.popViewControllerBackground != nil {
  412. vc?.enterFillColor = self.popViewControllerEnterFillColor!
  413. }
  414. vc?.showVerticalScroller = self.showVerticalScroller
  415. // KMPrint(self.items)
  416. let _ = vc?.initWithPopViewDataArr(items)
  417. self.popViewController = vc
  418. createFilePopover.contentViewController = vc
  419. if (self.stringValue.isEmpty == false) {
  420. vc?.selectedItems = [self.stringValue]
  421. }
  422. createFilePopover.animates = true
  423. createFilePopover.behavior = self.popoverBehavior
  424. createFilePopover.setValue(true, forKey: "shouldHideAnchor")
  425. createFilePopover.show(relativeTo: CGRect(x: view.bounds.origin.x, y: 10, width: view.bounds.size.width, height: view.bounds.size.height), of: view, preferredEdge: .minY)
  426. var width = mainBox.frame.width
  427. for i in items {
  428. let w = i.getTextRectSize(font: .systemFont(ofSize: 14.0), size: CGSize(width: CGFloat(MAXFLOAT), height: 32.0)).width+12*2
  429. if width < w {
  430. width = w
  431. }
  432. }
  433. vc?.customBoxWidthLayoutConstraint.constant = width
  434. vc?.downCallback = { [weak self] (downEntered: Bool, count: String) -> Void in
  435. guard let blockSelf = self else {
  436. return
  437. }
  438. if downEntered {
  439. blockSelf.stringValue = count
  440. let current = blockSelf.items.firstIndex(of: count) ?? 0
  441. blockSelf.indexOfSelectedItem = current
  442. blockSelf.delete?.km_comboBoxSelectionDidChange(blockSelf)
  443. blockSelf.updateUI()
  444. blockSelf.createFilePopover.close()
  445. }
  446. }
  447. vc?.viewWillShow = { [weak self] cellView, idx in
  448. if let data = self {
  449. self?.delete?.km_cellViewWillShow?(data, cellView, idx)
  450. }
  451. }
  452. }
  453. }
  454. }
  455. extension KMDesignSelect: NSTextFieldDelegate {
  456. func controlTextDidChange(_ obj: Notification) {
  457. let textfield = obj.object as! NSTextField
  458. self.stringValue = textfield.stringValue
  459. self.delete?.km_controlTextDidChange?(self)
  460. }
  461. func controlTextDidEndEditing(_ obj: Notification) {
  462. let textfield = obj.object as! NSTextField
  463. self.stringValue = textfield.stringValue
  464. self.delete?.km_controlTextDidEndEditing?(self)
  465. }
  466. }
  467. extension KMDesignSelect: NSPopoverDelegate {
  468. func popoverWillShow(_ notification: Notification) {
  469. let popover = notification.object as! NSPopover
  470. if (createFilePopover == popover) {
  471. let vc = createFilePopover.contentViewController! as! KMHomePopViewController
  472. vc.disItems = disItems
  473. self.state = .Focus
  474. self.canHover = false
  475. }
  476. }
  477. func popoverWillClose(_ notification: Notification) {
  478. let popover = notification.object as! NSPopover
  479. if (createFilePopover == popover) {
  480. self.state = .Norm
  481. self.canHover = true
  482. }
  483. }
  484. }