CustomStampLeftToolbar.swift 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310
  1. //
  2. // CustomStampLeftToolbar.swift
  3. // PDF Reader Pro
  4. //
  5. // Created by Niehaoyu on 2024/6/19.
  6. //
  7. import Cocoa
  8. class CustomStampLeftToolbar: NSView, NibLoadable {
  9. @IBOutlet var arrowBox: NSBox!
  10. @IBOutlet var arrowBtn: KMButton!
  11. @IBOutlet var textBox: NSBox!
  12. @IBOutlet var textButton: KMButton!
  13. @IBOutlet var dateBox: NSBox!
  14. @IBOutlet var dateButton: KMButton!
  15. @IBOutlet var peopleBox: NSBox!
  16. @IBOutlet var peopleButton: KMButton!
  17. @IBOutlet var imageBox: NSBox!
  18. @IBOutlet var imageButton: KMButton!
  19. @IBOutlet var stampBox: NSBox!
  20. @IBOutlet var stampButton: KMButton!
  21. var originalType: CSToolbarType = .none
  22. var toolbarType: CSToolbarType = .move
  23. var clickHandle: ((_ view: CustomStampLeftToolbar, _ actionType: CSToolbarType, _ string: String)->Void)?
  24. var lastDateItem: NSMenuItem?
  25. var lastIDItem: NSMenuItem?
  26. override func draw(_ dirtyRect: NSRect) {
  27. super.draw(dirtyRect)
  28. // Drawing code here.
  29. }
  30. override func awakeFromNib() {
  31. super.awakeFromNib()
  32. self.configUI()
  33. self.reloadData()
  34. }
  35. func configUI() {
  36. for box in [self.arrowBox, self.textBox, self.dateBox, self.peopleBox, self.imageBox, self.stampBox] {
  37. box?.cornerRadius = 6
  38. box?.borderColor = NSColor.clear
  39. }
  40. let dateNames = ["dd-MMM", "dd/MM/yyyy", "dd.MM.yyyy", "dd-MM-yyyy",
  41. "dd MMMM yyyy", "EEEE,MMMM d,yyyy", "MM/yy", "M/d/yyyy",
  42. "MM/dd/yyyy", "MM-dd-yyyy","MMMM dd,yyyy", "yyyy-MM-dd",
  43. "dd.MM.yyyy hh:mm:ss", "MM-dd-yyyy hh:mm:ss aaa", "MM/dd/yy hh:mm aaa","yyyy.MM.dd hh:mm:ss",
  44. "hh:mm:ss", "hh:mm:ss aaa"]
  45. let dateMenu = NSMenu.init()
  46. for idx in 0...dateNames.count-1 {
  47. let string = dateNames[idx]
  48. let menuItem = NSMenuItem.init(title: string, action: #selector(dateMenuItemClick(_:)), keyEquivalent: "")
  49. menuItem.tag = 1000 + idx
  50. menuItem.target = self
  51. dateMenu.addItem(menuItem)
  52. }
  53. self.dateBox?.menu = dateMenu
  54. self.dateBox.menu?.delegate = self
  55. var idNames = ["Name", "Login Name", "Email Address", "Organization Name"]
  56. let idMenu = NSMenu.init()
  57. for idx in 0...idNames.count-1 {
  58. let string = idNames[idx]
  59. let menuItem = NSMenuItem.init(title: NSLocalizedString(string, comment: ""), action: #selector(idMenuItemClick(_:)), keyEquivalent: "")
  60. menuItem.tag = 1000 + idx
  61. menuItem.target = self
  62. idMenu.addItem(menuItem)
  63. }
  64. self.peopleBox?.menu = idMenu
  65. self.peopleBox.menu?.delegate = self
  66. self.arrowBtn.mouseMoveCallback = {[weak self] mouseEntered in
  67. if mouseEntered {
  68. self?.arrowBox.fillColor = NSColor(red: 73/255, green: 130/255, blue: 230/255, alpha: 0.2)
  69. } else {
  70. if self?.toolbarType == .move {
  71. self?.arrowBox.fillColor = NSColor(red: 73/255, green: 130/255, blue: 230/255, alpha: 0.2)
  72. } else {
  73. self?.arrowBox.fillColor = NSColor.clear
  74. }
  75. }
  76. }
  77. self.textButton.mouseMoveCallback = {[weak self] mouseEntered in
  78. if mouseEntered {
  79. self?.textBox.fillColor = NSColor(red: 73/255, green: 130/255, blue: 230/255, alpha: 0.2)
  80. } else {
  81. if self?.toolbarType == .text {
  82. self?.textBox.fillColor = NSColor(red: 73/255, green: 130/255, blue: 230/255, alpha: 0.2)
  83. } else {
  84. self?.textBox.fillColor = NSColor.clear
  85. }
  86. }
  87. }
  88. self.dateButton.mouseMoveCallback = {[weak self] mouseEntered in
  89. if mouseEntered {
  90. self?.dateBox.fillColor = NSColor(red: 73/255, green: 130/255, blue: 230/255, alpha: 0.2)
  91. } else {
  92. if self?.toolbarType == .dateText {
  93. self?.dateBox.fillColor = NSColor(red: 73/255, green: 130/255, blue: 230/255, alpha: 0.2)
  94. } else {
  95. self?.dateBox.fillColor = NSColor.clear
  96. }
  97. }
  98. }
  99. self.peopleButton.mouseMoveCallback = {[weak self] mouseEntered in
  100. if mouseEntered {
  101. self?.peopleBox.fillColor = NSColor(red: 73/255, green: 130/255, blue: 230/255, alpha: 0.2)
  102. } else {
  103. if self?.toolbarType == .idText {
  104. self?.peopleBox.fillColor = NSColor(red: 73/255, green: 130/255, blue: 230/255, alpha: 0.2)
  105. } else {
  106. self?.peopleBox.fillColor = NSColor.clear
  107. }
  108. }
  109. }
  110. self.imageButton.mouseMoveCallback = {[weak self] mouseEntered in
  111. if mouseEntered {
  112. self?.imageBox.fillColor = NSColor(red: 73/255, green: 130/255, blue: 230/255, alpha: 0.2)
  113. } else {
  114. if self?.toolbarType == .image {
  115. self?.imageBox.fillColor = NSColor(red: 73/255, green: 130/255, blue: 230/255, alpha: 0.2)
  116. } else {
  117. self?.imageBox.fillColor = NSColor.clear
  118. }
  119. }
  120. }
  121. self.stampButton.mouseMoveCallback = {[weak self] mouseEntered in
  122. if mouseEntered {
  123. self?.stampBox.fillColor = NSColor(red: 73/255, green: 130/255, blue: 230/255, alpha: 0.2)
  124. } else {
  125. if self?.toolbarType == .stamp {
  126. self?.stampBox.fillColor = NSColor(red: 73/255, green: 130/255, blue: 230/255, alpha: 0.2)
  127. } else {
  128. self?.stampBox.fillColor = NSColor.clear
  129. }
  130. }
  131. }
  132. }
  133. func reloadData() {
  134. for box in [self.arrowBox, self.textBox, self.dateBox, self.peopleBox, self.imageBox, self.stampBox] {
  135. box?.fillColor = NSColor.clear
  136. }
  137. if self.toolbarType == .move {
  138. self.arrowBox.fillColor = NSColor(red: 73/255, green: 130/255, blue: 230/255, alpha: 0.2)
  139. } else if self.toolbarType == .text {
  140. self.textBox.fillColor = NSColor(red: 73/255, green: 130/255, blue: 230/255, alpha: 0.2)
  141. } else if self.toolbarType == .dateText {
  142. self.dateBox.fillColor = NSColor(red: 73/255, green: 130/255, blue: 230/255, alpha: 0.2)
  143. } else if self.toolbarType == .idText {
  144. self.peopleBox.fillColor = NSColor(red: 73/255, green: 130/255, blue: 230/255, alpha: 0.2)
  145. } else if self.toolbarType == .image {
  146. self.imageBox.fillColor = NSColor(red: 73/255, green: 130/255, blue: 230/255, alpha: 0.2)
  147. } else if self.toolbarType == .stamp {
  148. self.stampBox.fillColor = NSColor(red: 73/255, green: 130/255, blue: 230/255, alpha: 0.2)
  149. }
  150. }
  151. //MARK: IBAction
  152. @IBAction func buttonAction(_ sender: NSButton) {
  153. originalType = self.toolbarType
  154. if sender == self.arrowBtn {
  155. self.toolbarType = .move
  156. } else if sender == self.textButton {
  157. self.toolbarType = .text
  158. } else if sender == self.dateButton {
  159. self.toolbarType = .dateText
  160. if self.lastDateItem == nil {
  161. self.lastDateItem = self.dateBox.menu?.items.first
  162. }
  163. if self.lastDateItem != nil {
  164. self.dateMenuItemClick(self.lastDateItem!)
  165. }
  166. } else if sender == self.peopleButton {
  167. self.toolbarType = .idText
  168. self.reloadData()
  169. self.lastIDItem = nil
  170. self.peopleBox.menu?.popUp(positioning: self.peopleBox.menu?.item(at: 0), at: CGPoint(x: 0, y: 15), in: sender)
  171. return
  172. } else if sender == self.imageButton {
  173. self.toolbarType = .image
  174. } else if sender == self.stampButton {
  175. self.toolbarType = .stamp
  176. }
  177. guard let callBack = self.clickHandle else {
  178. return
  179. }
  180. callBack(self, self.toolbarType, "")
  181. self.reloadData()
  182. }
  183. @objc func idMenuItemClick(_ item: NSMenuItem) {
  184. var string = item.title
  185. if item.tag == 1000 {
  186. string = KMProfileInfo.shared().fullName
  187. } else if item.tag == 1001 {
  188. string = NSFullUserName()
  189. } else if item.tag == 1002 {
  190. string = KMProfileInfo.shared().email
  191. } else if item.tag == 1003 {
  192. string = KMProfileInfo.shared().OrganizeName
  193. }
  194. if string.isEmpty {
  195. string = " "
  196. }
  197. self.lastIDItem = item
  198. self.toolbarType = .idText
  199. self.reloadData()
  200. guard let callBack = self.clickHandle else {
  201. return
  202. }
  203. callBack(self, self.toolbarType, string)
  204. }
  205. @objc func dateMenuItemClick(_ item: NSMenuItem) {
  206. for subItem in self.dateBox.menu?.items ?? [] {
  207. subItem.state = .off
  208. }
  209. item.state = .on
  210. let string = item.title
  211. let date = Date()
  212. let dateFormatter = DateFormatter()
  213. dateFormatter.dateFormat = string
  214. let dateString = dateFormatter.string(from: date)
  215. print(dateString)
  216. self.toolbarType = .dateText
  217. self.reloadData()
  218. guard let callBack = self.clickHandle else {
  219. return
  220. }
  221. callBack(self, self.toolbarType, dateString)
  222. self.lastDateItem = item
  223. }
  224. }
  225. extension CustomStampLeftToolbar: NSMenuDelegate {
  226. func menuWillOpen(_ menu: NSMenu) {
  227. if menu == self.peopleBox.menu {
  228. } else if menu == self.dateBox.menu {
  229. if self.lastDateItem == nil {
  230. self.lastDateItem = self.dateBox.menu?.items.first
  231. }
  232. if self.lastDateItem != nil {
  233. self.dateMenuItemClick(self.lastDateItem!)
  234. }
  235. }
  236. }
  237. func menuDidClose(_ menu: NSMenu) {
  238. if menu == self.peopleBox.menu {
  239. if self.lastIDItem == nil {
  240. self.toolbarType = .move
  241. self.reloadData()
  242. }
  243. }
  244. }
  245. }