KMDesignSelect.swift 19 KB

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