KMPDFSecToolbarController.swift 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517
  1. //
  2. // KMPDFSecToolbarController.swift
  3. // PDF Reader Pro
  4. //
  5. // Created by Niehaoyu on 2024/10/8.
  6. //
  7. import Cocoa
  8. import KMComponentLibrary
  9. @objc protocol KMPDFSecToolbarControllerDelegate: AnyObject {
  10. @objc optional func kmPDFSecToolbarControllerDidItemClicked(_ controller: KMPDFSecToolbarController, _ property: Any)
  11. @objc optional func kmPDFSecToolbarControllerDidSelectTextDidBeginEditing(_ controller: KMPDFSecToolbarController, _ view: ComponentSelect)
  12. @objc optional func kmPDFSecToolbarControllerDidSelectTextDidChange(_ controller: KMPDFSecToolbarController, _ view: ComponentSelect)
  13. @objc optional func kmPDFSecToolbarControllerDidSelectTextDidEndEditing(_ controller: KMPDFSecToolbarController, _ view: ComponentSelect)
  14. }
  15. class KMPDFSecToolbarController: NSViewController {
  16. @IBOutlet var contendBox: NSBox!
  17. @IBOutlet var infoContendView: NSView!
  18. @IBOutlet var rightContendView: NSView!
  19. @IBOutlet var moreToolsBGView: NSView!
  20. @IBOutlet var moreToolsView: ComponentDropdownTool!
  21. @IBOutlet var contendLeftConst: NSLayoutConstraint!
  22. @IBOutlet var contendViewWidthConst: NSLayoutConstraint!
  23. @IBOutlet var rightContendViewWidthConst: NSLayoutConstraint!
  24. private var _propertys: [NSObject] = []
  25. var propertys: [NSObject] {
  26. get {
  27. return _propertys
  28. }
  29. }
  30. private var propertysWidthValue: CGFloat = 0
  31. private var lastPropertyWidth: CGFloat = 0
  32. private var hidePropertys: [Any] = []
  33. private var rightPropertys: [Any] = []
  34. var viewManager: KMPDFViewManager?
  35. weak open var delegate: KMPDFSecToolbarControllerDelegate?
  36. override func viewWillLayout() {
  37. super.viewWillLayout()
  38. let contendXValue = CGRectGetWidth(view.frame)/2 - propertysWidthValue/2
  39. contendLeftConst.constant = max(0, contendXValue)
  40. if contendXValue <= 0 {
  41. contendViewWidthConst.constant = CGRectGetWidth(view.frame)
  42. } else {
  43. contendViewWidthConst.constant = propertysWidthValue
  44. }
  45. hidePropertys.removeAll()
  46. moreToolsBGView.isHidden = true
  47. let subviews = infoContendView.subviews
  48. for view in subviews {
  49. if CGRectGetMidX(view.frame) > CGRectGetWidth(self.view.frame) - lastPropertyWidth {
  50. moreToolsBGView.isHidden = false
  51. view.isHidden = true
  52. if view is ComponentButton {
  53. hidePropertys.append((view as! ComponentButton).properties)
  54. } else if view is ComponentDropdownTool {
  55. hidePropertys.append((view as! ComponentDropdownTool).properties)
  56. }
  57. } else {
  58. view.isHidden = false
  59. }
  60. }
  61. NSObject.cancelPreviousPerformRequests(withTarget: self, selector: #selector(setUpHidePropertys), object: nil)
  62. self.perform(#selector(setUpHidePropertys), with: nil, afterDelay: 0.35)
  63. }
  64. override func viewDidLoad() {
  65. super.viewDidLoad()
  66. // Do view setup here.
  67. setUpProperty()
  68. }
  69. //MARK: - Setup
  70. func setUpProperty() {
  71. contendBox.fillColor = ComponentLibrary.shared.getComponentColorFromKey("colorBg/layout-middle")
  72. moreToolsBGView.wantsLayer = true
  73. moreToolsBGView.layer?.backgroundColor = contendBox.fillColor.cgColor
  74. moreToolsBGView.isHidden = true
  75. moreToolsView.properties = ComponentDropdownToolProperty(state: .normal, leftIcon: NSImage(named: "toolbar_doubleArrow_right"))
  76. moreToolsView.delegate = self
  77. }
  78. @objc func setUpHidePropertys() {
  79. var menuItemArr: [ComponentMenuitemProperty] = []
  80. for item in hidePropertys {
  81. if item is ComponentButtonProperty {
  82. let menuItem = ComponentMenuitemProperty(type: .normal, text: (item as! ComponentButtonProperty).buttonText ?? "", identifier: (item as! ComponentButtonProperty).identifier)
  83. menuItemArr.append(menuItem)
  84. } else if item is ComponentDropdownToolProperty {
  85. let menuItem = ComponentMenuitemProperty(type: .normal, text: (item as! ComponentDropdownToolProperty).text ?? "testtest", identifier: "")
  86. if let subItems = (item as! ComponentDropdownToolProperty).menuItemArr {
  87. menuItem.subPropertys = subItems
  88. }
  89. menuItemArr.append(menuItem)
  90. }
  91. }
  92. moreToolsView.properties.menuItemArr = menuItemArr
  93. }
  94. //MARK: - Getter
  95. func returnIDWithSubToolMode(_ subToolMode: KMPDFSubToolMode) -> String {
  96. if subToolMode == .Highlight {
  97. return KMPDFToolbar_highlight_Identifier
  98. } else if subToolMode == .Underline {
  99. return KMPDFToolbar_underline_Identifier
  100. } else if subToolMode == .Waveline {
  101. return KMPDFToolbar_waveline_Identifier
  102. } else if subToolMode == .Strikethrough {
  103. return KMPDFToolbar_strikethrough_Identifier
  104. } else if subToolMode == .Text {
  105. return KMPDFToolbar_text_Identifier
  106. } else if subToolMode == .Note {
  107. return KMPDFToolbar_note_Identifier
  108. } else if subToolMode == .Pen {
  109. return KMPDFToolbar_pen_Identifier
  110. } else if subToolMode == .Eraser {
  111. return KMPDFToolbar_eraser_Identifier
  112. } else if subToolMode == .Rectangle {
  113. return KMPDFToolbar_rectangle_Identifier
  114. } else if subToolMode == .Circle {
  115. return KMPDFToolbar_circle_Identifier
  116. } else if subToolMode == .Arrow {
  117. return KMPDFToolbar_arrow_Identifier
  118. } else if subToolMode == .Line {
  119. return KMPDFToolbar_line_Identifier
  120. } else if subToolMode == .Stamp {
  121. return KMPDFToolbar_stamp_Identifier
  122. } else if subToolMode == .Sign {
  123. return KMPDFToolbar_sign_Identifier
  124. } else if subToolMode == .Edit_text {
  125. return KMPDFToolbar_edit_text_Identifier
  126. } else if subToolMode == .Edit_Image {
  127. return KMPDFToolbar_edit_image_Identifier
  128. } else if subToolMode == .Edit_Link {
  129. return KMPDFToolbar_edit_link_Identifier
  130. } else if subToolMode == .Edit_Crop {
  131. return KMPDFToolbar_edit_crop_Identifier
  132. } else if subToolMode == .Fill_tick {
  133. return KMPDFToolbar_fill_tick_Identifier
  134. } else if subToolMode == .fill_fork {
  135. return KMPDFToolbar_fill_fork_Identifier
  136. } else if subToolMode == .fill_rectangle {
  137. return KMPDFToolbar_fill_rectangle_Identifier
  138. } else if subToolMode == .fill_line {
  139. return KMPDFToolbar_fill_line_Identifier
  140. } else if subToolMode == .fill_dot {
  141. return KMPDFToolbar_fill_dot_Identifier
  142. } else if subToolMode == .fill_date {
  143. return KMPDFToolbar_fill_date_Identifier
  144. } else if subToolMode == .fill_sign {
  145. return KMPDFToolbar_fill_sign_Identifier
  146. } else if subToolMode == .Form_text {
  147. return KMPDFToolbar_form_text_Identifier
  148. } else if subToolMode == .Form_checkbox {
  149. return KMPDFToolbar_form_checkbox_Identifier
  150. } else if subToolMode == .Form_radio {
  151. return KMPDFToolbar_form_radio_Identifier
  152. } else if subToolMode == .Form_list {
  153. return KMPDFToolbar_form_list_Identifier
  154. } else if subToolMode == .Form_dropdown {
  155. return KMPDFToolbar_form_dropdown_Identifier
  156. } else if subToolMode == .Form_OK {
  157. return KMPDFToolbar_form_OK_Identifier
  158. } else if subToolMode == .Form_digitalSign {
  159. return KMPDFToolbar_form_digitalSign_Identifier
  160. }
  161. return ""
  162. }
  163. func returnSubToolModeWithID(_ identifier: String) -> KMPDFSubToolMode {
  164. if identifier == KMPDFToolbar_highlight_Identifier {
  165. return .Highlight
  166. } else if identifier == KMPDFToolbar_underline_Identifier {
  167. return .Underline
  168. } else if identifier == KMPDFToolbar_waveline_Identifier {
  169. return .Waveline
  170. } else if identifier == KMPDFToolbar_strikethrough_Identifier {
  171. return .Strikethrough
  172. } else if identifier == KMPDFToolbar_text_Identifier {
  173. return .Text
  174. } else if identifier == KMPDFToolbar_note_Identifier {
  175. return .Note
  176. } else if identifier == KMPDFToolbar_pen_Identifier {
  177. return .Pen
  178. } else if identifier == KMPDFToolbar_eraser_Identifier {
  179. return .Eraser
  180. } else if identifier == KMPDFToolbar_rectangle_Identifier {
  181. return .Rectangle
  182. } else if identifier == KMPDFToolbar_circle_Identifier {
  183. return .Circle
  184. } else if identifier == KMPDFToolbar_arrow_Identifier {
  185. return .Arrow
  186. } else if identifier == KMPDFToolbar_line_Identifier {
  187. return .Line
  188. } else if identifier == KMPDFToolbar_measure_Identifier {
  189. return .Measure
  190. } else if identifier == KMPDFToolbar_stamp_Identifier {
  191. return .Stamp
  192. } else if identifier == KMPDFToolbar_sign_Identifier {
  193. return .Sign
  194. } else if identifier == KMPDFToolbar_edit_text_Identifier {
  195. return .Edit_text
  196. } else if identifier == KMPDFToolbar_edit_image_Identifier {
  197. return .Edit_Image
  198. } else if identifier == KMPDFToolbar_edit_link_Identifier {
  199. return .Edit_Link
  200. } else if identifier == KMPDFToolbar_edit_crop_Identifier {
  201. return .Edit_Crop
  202. } else if identifier == KMPDFToolbar_fill_tick_Identifier {
  203. return .Fill_tick
  204. } else if identifier == KMPDFToolbar_fill_fork_Identifier {
  205. return .fill_fork
  206. } else if identifier == KMPDFToolbar_fill_rectangle_Identifier {
  207. return .fill_rectangle
  208. } else if identifier == KMPDFToolbar_fill_line_Identifier {
  209. return .fill_line
  210. } else if identifier == KMPDFToolbar_fill_dot_Identifier {
  211. return .fill_dot
  212. } else if identifier == KMPDFToolbar_fill_date_Identifier {
  213. return .fill_date
  214. } else if identifier == KMPDFToolbar_fill_sign_Identifier {
  215. return .fill_sign
  216. } else if identifier == KMPDFToolbar_form_text_Identifier {
  217. return .Form_text
  218. } else if identifier == KMPDFToolbar_form_checkbox_Identifier {
  219. return .Form_checkbox
  220. } else if identifier == KMPDFToolbar_form_radio_Identifier {
  221. return .Form_radio
  222. } else if identifier == KMPDFToolbar_form_list_Identifier {
  223. return .Form_list
  224. } else if identifier == KMPDFToolbar_form_dropdown_Identifier {
  225. return .Form_dropdown
  226. } else if identifier == KMPDFToolbar_form_OK_Identifier {
  227. return .Form_OK
  228. } else if identifier == KMPDFToolbar_form_digitalSign_Identifier {
  229. return .Form_digitalSign
  230. }
  231. return .None
  232. }
  233. //MARK: - reload
  234. public func reloadMainPropertys(_ arr: [NSObject]) {
  235. _propertys.removeAll()
  236. for item in arr {
  237. _propertys.append(item)
  238. }
  239. setupMainView()
  240. }
  241. public func reloadRightPropertys(_ arr: [Any]) {
  242. rightPropertys.removeAll()
  243. for item in arr {
  244. rightPropertys.append(item)
  245. }
  246. setupRightView()
  247. }
  248. func setupMainView() {
  249. let subviews = infoContendView.subviews
  250. for view in subviews {
  251. view.removeFromSuperview()
  252. }
  253. var itemXvalue: CGFloat = 0
  254. for property in propertys {
  255. if property is ComponentButtonProperty {
  256. let buttonProperty = (property as! ComponentButtonProperty)
  257. let button = ComponentButton.init()
  258. button.properties = buttonProperty
  259. if buttonProperty.onlyIcon {
  260. button.frame = CGRectMake(itemXvalue, CGRectGetHeight(infoContendView.frame)/2-14, 28, 28)
  261. } else {
  262. button.frame = CGRectMake(itemXvalue, CGRectGetHeight(infoContendView.frame)/2-14, button.properties.propertyInfo.viewWidth, 28)
  263. }
  264. button.setTarget(self, action: #selector(buttonClicked(_:)))
  265. infoContendView.addSubview(button)
  266. itemXvalue += CGRectGetWidth(button.frame)
  267. itemXvalue += 12
  268. lastPropertyWidth = CGRectGetWidth(button.frame)
  269. } else if property is ComponentDividerProperty {
  270. let divider = ComponentDivider.init()
  271. divider.frame = CGRectMake(itemXvalue, CGRectGetHeight(infoContendView.frame)/2-8, 1, 16)
  272. divider.properties = (property as! ComponentDividerProperty)
  273. infoContendView.addSubview(divider)
  274. itemXvalue += 1
  275. itemXvalue += 12
  276. lastPropertyWidth = CGRectGetWidth(divider.frame)
  277. } else if property is ComponentDropdownToolProperty {
  278. let dropdownTool = ComponentDropdownTool.init()
  279. dropdownTool.properties = property as! ComponentDropdownToolProperty
  280. dropdownTool.frame = CGRectMake(itemXvalue, CGRectGetHeight(infoContendView.frame)/2-14, dropdownTool.properties.propertyInfo.viewWidth, 28)
  281. dropdownTool.delegate = self
  282. infoContendView.addSubview(dropdownTool)
  283. itemXvalue += CGRectGetWidth(dropdownTool.frame)
  284. itemXvalue += 12
  285. lastPropertyWidth = CGRectGetWidth(dropdownTool.frame)
  286. } else if property is ComponentSelectProperties {
  287. let selectView = ComponentSelect.init()
  288. selectView.properties = property as! ComponentSelectProperties
  289. selectView.frame = CGRectMake(itemXvalue, CGRectGetHeight(infoContendView.frame)/2-14, 144, 28)
  290. if let items = selectView.properties.menuItemArr {
  291. selectView.updateMenuItemsArr(items)
  292. }
  293. selectView.delegate = self
  294. infoContendView.addSubview(selectView)
  295. itemXvalue += CGRectGetWidth(selectView.frame)
  296. itemXvalue += 12
  297. lastPropertyWidth = CGRectGetWidth(selectView.frame)
  298. }
  299. }
  300. itemXvalue -= 12
  301. propertysWidthValue = itemXvalue
  302. contendViewWidthConst.constant = propertysWidthValue
  303. contendLeftConst.constant = CGRectGetWidth(view.frame)/2 - propertysWidthValue/2
  304. }
  305. func setupRightView() {
  306. let subviews = rightContendView.subviews
  307. for view in subviews {
  308. view.removeFromSuperview()
  309. }
  310. var itemXvalue: CGFloat = 0
  311. for property in rightPropertys {
  312. if property is ComponentButtonProperty {
  313. let buttonProperty = (property as! ComponentButtonProperty)
  314. let button = ComponentButton.init()
  315. button.properties = buttonProperty
  316. if buttonProperty.onlyIcon {
  317. button.frame = CGRectMake(itemXvalue, 0, 28, 28)
  318. } else {
  319. button.frame = CGRectMake(itemXvalue, 0, button.properties.propertyInfo.viewWidth, 28)
  320. }
  321. button.setTarget(self, action: #selector(buttonClicked(_:)))
  322. rightContendView.addSubview(button)
  323. itemXvalue += CGRectGetWidth(button.frame)
  324. itemXvalue += 12
  325. } else if property is ComponentDividerProperty {
  326. let divider = ComponentDivider.init()
  327. divider.frame = CGRectMake(itemXvalue, CGRectGetHeight(infoContendView.frame)/2-8, 1, 16)
  328. divider.properties = (property as! ComponentDividerProperty)
  329. rightContendView.addSubview(divider)
  330. itemXvalue += 1
  331. itemXvalue += 12
  332. }
  333. }
  334. itemXvalue -= 12
  335. rightContendViewWidthConst.constant = itemXvalue
  336. }
  337. func reloadMainview() {
  338. let subviews = infoContendView.subviews
  339. for view in subviews {
  340. if view is ComponentButton {
  341. (view as! ComponentButton).reloadData()
  342. } else if view is ComponentDropdownTool {
  343. (view as! ComponentDropdownTool).reloadData()
  344. } else if view is ComponentSelect {
  345. (view as! ComponentSelect).reloadData()
  346. }
  347. }
  348. }
  349. func reloadRightview() {
  350. let subviews = rightContendView.subviews
  351. for view in subviews {
  352. if view is ComponentButton {
  353. (view as! ComponentButton).reloadData()
  354. }
  355. }
  356. }
  357. func refreshToolbarModeInfo() {
  358. guard let viewManager = self.viewManager else { return }
  359. let subviews = infoContendView.subviews
  360. for view in subviews {
  361. if view is ComponentButton {
  362. let item = (view as! ComponentButton)
  363. let subToolMode = viewManager.subToolMode
  364. let resultID = self.returnIDWithSubToolMode(viewManager.subToolMode)
  365. if item.properties.identifier != resultID {
  366. if item.properties.state == .pressed {
  367. item.properties.state = .normal
  368. item.reloadData()
  369. }
  370. } else {
  371. if item.properties.state != .pressed {
  372. item.properties.state = .pressed
  373. item.reloadData()
  374. }
  375. }
  376. }
  377. }
  378. }
  379. //MARK: -
  380. @objc func buttonClicked(_ sender: ComponentButton) {
  381. let subToolMode = returnSubToolModeWithID(sender.properties.identifier)
  382. if subToolMode != .None {
  383. if viewManager?.subToolMode == subToolMode {
  384. viewManager?.subToolMode = .None
  385. } else {
  386. viewManager?.subToolMode = subToolMode
  387. }
  388. }
  389. refreshToolbarModeInfo()
  390. delegate?.kmPDFSecToolbarControllerDidItemClicked?(self, sender.properties)
  391. }
  392. //MARK: - Mouse
  393. override func mouseDown(with event: NSEvent) {
  394. super.mouseDown(with: event)
  395. view.window?.makeFirstResponder(self)
  396. }
  397. }
  398. //MARK: - ComponentDropdownToolDelegate
  399. extension KMPDFSecToolbarController: ComponentDropdownToolDelegate {
  400. func componentDropdownToolDidShowPopupView(_ view: ComponentDropdownTool) {
  401. if viewManager?.subToolMode != .None {
  402. viewManager?.subToolMode = .None
  403. }
  404. refreshToolbarModeInfo()
  405. delegate?.kmPDFSecToolbarControllerDidItemClicked?(self, view.properties)
  406. }
  407. func componentDropdownToolDidClicked(_ view: ComponentDropdownTool, menuItem: ComponentMenuitemProperty?) {
  408. if let property = menuItem {
  409. property.itemSelected = false
  410. property.state = .normal
  411. delegate?.kmPDFSecToolbarControllerDidItemClicked?(self, property)
  412. }
  413. }
  414. }
  415. //MARK: - ComponentSelectDelegate
  416. extension KMPDFSecToolbarController: ComponentSelectDelegate {
  417. func componentSelectDidSelect(view: ComponentSelect?, menuItemProperty: ComponentMenuitemProperty?) {
  418. if let property = menuItemProperty {
  419. property.itemSelected = false
  420. property.state = .normal
  421. delegate?.kmPDFSecToolbarControllerDidItemClicked?(self, property)
  422. }
  423. }
  424. func componentSelectTextDidBeginEditing(_ view: ComponentSelect) {
  425. delegate?.kmPDFSecToolbarControllerDidSelectTextDidBeginEditing?(self, view)
  426. }
  427. func componentSelectTextDidChange(_ view: ComponentSelect) {
  428. delegate?.kmPDFSecToolbarControllerDidSelectTextDidChange?(self, view)
  429. }
  430. func componentSelectTextDidEndEditing(_ view: ComponentSelect) {
  431. delegate?.kmPDFSecToolbarControllerDidSelectTextDidEndEditing?(self, view)
  432. }
  433. }