KMPDFSecToolbarController.swift 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527
  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 == .Measure {
  121. return KMPDFToolbar_measure_Identifier
  122. } else if subToolMode == .Stamp {
  123. return KMPDFToolbar_stamp_Identifier
  124. } else if subToolMode == .Sign {
  125. return KMPDFToolbar_sign_Identifier
  126. } else if subToolMode == .Edit_text {
  127. return KMPDFToolbar_edit_text_Identifier
  128. } else if subToolMode == .Edit_Image {
  129. return KMPDFToolbar_edit_image_Identifier
  130. } else if subToolMode == .Edit_Link {
  131. return KMPDFToolbar_edit_link_Identifier
  132. } else if subToolMode == .Edit_Crop {
  133. return KMPDFToolbar_edit_crop_Identifier
  134. } else if subToolMode == .Fill_tick {
  135. return KMPDFToolbar_fill_tick_Identifier
  136. } else if subToolMode == .fill_fork {
  137. return KMPDFToolbar_fill_fork_Identifier
  138. } else if subToolMode == .fill_rectangle {
  139. return KMPDFToolbar_fill_rectangle_Identifier
  140. } else if subToolMode == .fill_line {
  141. return KMPDFToolbar_fill_line_Identifier
  142. } else if subToolMode == .fill_dot {
  143. return KMPDFToolbar_fill_dot_Identifier
  144. } else if subToolMode == .fill_date {
  145. return KMPDFToolbar_fill_date_Identifier
  146. } else if subToolMode == .fill_sign {
  147. return KMPDFToolbar_fill_sign_Identifier
  148. } else if subToolMode == .Form_text {
  149. return KMPDFToolbar_form_text_Identifier
  150. } else if subToolMode == .Form_checkbox {
  151. return KMPDFToolbar_form_checkbox_Identifier
  152. } else if subToolMode == .Form_radio {
  153. return KMPDFToolbar_form_radio_Identifier
  154. } else if subToolMode == .Form_list {
  155. return KMPDFToolbar_form_list_Identifier
  156. } else if subToolMode == .Form_dropdown {
  157. return KMPDFToolbar_form_dropdown_Identifier
  158. } else if subToolMode == .Form_OK {
  159. return KMPDFToolbar_form_OK_Identifier
  160. } else if subToolMode == .Form_digitalSign {
  161. return KMPDFToolbar_form_digitalSign_Identifier
  162. } else if subToolMode == .Redact {
  163. return KMPDFToolbar_protect_redact_Identifier
  164. } else if subToolMode == .Digital_Sign {
  165. return KMPDFToolbar_protect_digitalSign_Identifier
  166. } else if subToolMode == .Tool_OCR {
  167. return KMPDFToolbar_tools_OCR_Identifier
  168. }
  169. return ""
  170. }
  171. func returnSubToolModeWithID(_ identifier: String) -> KMPDFSubToolMode {
  172. if identifier == KMPDFToolbar_highlight_Identifier {
  173. return .Highlight
  174. } else if identifier == KMPDFToolbar_underline_Identifier {
  175. return .Underline
  176. } else if identifier == KMPDFToolbar_waveline_Identifier {
  177. return .Waveline
  178. } else if identifier == KMPDFToolbar_strikethrough_Identifier {
  179. return .Strikethrough
  180. } else if identifier == KMPDFToolbar_text_Identifier {
  181. return .Text
  182. } else if identifier == KMPDFToolbar_note_Identifier {
  183. return .Note
  184. } else if identifier == KMPDFToolbar_pen_Identifier {
  185. return .Pen
  186. } else if identifier == KMPDFToolbar_eraser_Identifier {
  187. return .Eraser
  188. } else if identifier == KMPDFToolbar_rectangle_Identifier {
  189. return .Rectangle
  190. } else if identifier == KMPDFToolbar_circle_Identifier {
  191. return .Circle
  192. } else if identifier == KMPDFToolbar_arrow_Identifier {
  193. return .Arrow
  194. } else if identifier == KMPDFToolbar_line_Identifier {
  195. return .Line
  196. } else if identifier == KMPDFToolbar_measure_Identifier {
  197. return .Measure
  198. } else if identifier == KMPDFToolbar_stamp_Identifier {
  199. return .Stamp
  200. } else if identifier == KMPDFToolbar_sign_Identifier {
  201. return .Sign
  202. } else if identifier == KMPDFToolbar_edit_text_Identifier {
  203. return .Edit_text
  204. } else if identifier == KMPDFToolbar_edit_image_Identifier {
  205. return .Edit_Image
  206. } else if identifier == KMPDFToolbar_edit_link_Identifier {
  207. return .Edit_Link
  208. } else if identifier == KMPDFToolbar_edit_crop_Identifier {
  209. return .Edit_Crop
  210. } else if identifier == KMPDFToolbar_fill_tick_Identifier {
  211. return .Fill_tick
  212. } else if identifier == KMPDFToolbar_fill_fork_Identifier {
  213. return .fill_fork
  214. } else if identifier == KMPDFToolbar_fill_rectangle_Identifier {
  215. return .fill_rectangle
  216. } else if identifier == KMPDFToolbar_fill_line_Identifier {
  217. return .fill_line
  218. } else if identifier == KMPDFToolbar_fill_dot_Identifier {
  219. return .fill_dot
  220. } else if identifier == KMPDFToolbar_fill_date_Identifier {
  221. return .fill_date
  222. } else if identifier == KMPDFToolbar_fill_sign_Identifier {
  223. return .fill_sign
  224. } else if identifier == KMPDFToolbar_form_text_Identifier {
  225. return .Form_text
  226. } else if identifier == KMPDFToolbar_form_checkbox_Identifier {
  227. return .Form_checkbox
  228. } else if identifier == KMPDFToolbar_form_radio_Identifier {
  229. return .Form_radio
  230. } else if identifier == KMPDFToolbar_form_list_Identifier {
  231. return .Form_list
  232. } else if identifier == KMPDFToolbar_form_dropdown_Identifier {
  233. return .Form_dropdown
  234. } else if identifier == KMPDFToolbar_form_OK_Identifier {
  235. return .Form_OK
  236. } else if identifier == KMPDFToolbar_form_digitalSign_Identifier {
  237. return .Form_digitalSign
  238. } else if identifier == KMPDFToolbar_protect_redact_Identifier {
  239. return .Redact
  240. } else if identifier == KMPDFToolbar_protect_digitalSign_Identifier {
  241. return .Digital_Sign
  242. } else if identifier == KMPDFToolbar_tools_OCR_Identifier {
  243. return .Tool_OCR
  244. }
  245. return .None
  246. }
  247. //MARK: - reload
  248. public func reloadMainPropertys(_ arr: [NSObject]) {
  249. _propertys.removeAll()
  250. for item in arr {
  251. _propertys.append(item)
  252. }
  253. setupMainView()
  254. }
  255. public func reloadRightPropertys(_ arr: [Any]) {
  256. rightPropertys.removeAll()
  257. for item in arr {
  258. rightPropertys.append(item)
  259. }
  260. setupRightView()
  261. }
  262. func setupMainView() {
  263. let subviews = infoContendView.subviews
  264. for view in subviews {
  265. view.removeFromSuperview()
  266. }
  267. var itemXvalue: CGFloat = 0
  268. for property in propertys {
  269. if property is ComponentButtonProperty {
  270. let buttonProperty = (property as! ComponentButtonProperty)
  271. let button = ComponentButton.init()
  272. button.properties = buttonProperty
  273. if buttonProperty.onlyIcon {
  274. button.frame = CGRectMake(itemXvalue, CGRectGetHeight(infoContendView.frame)/2-14, 28, 28)
  275. } else {
  276. button.frame = CGRectMake(itemXvalue, CGRectGetHeight(infoContendView.frame)/2-14, button.properties.propertyInfo.viewWidth, 28)
  277. }
  278. button.setTarget(self, action: #selector(buttonClicked(_:)))
  279. infoContendView.addSubview(button)
  280. itemXvalue += CGRectGetWidth(button.frame)
  281. itemXvalue += 12
  282. lastPropertyWidth = CGRectGetWidth(button.frame)
  283. } else if property is ComponentDividerProperty {
  284. let divider = ComponentDivider.init()
  285. divider.frame = CGRectMake(itemXvalue, CGRectGetHeight(infoContendView.frame)/2-8, 1, 16)
  286. divider.properties = (property as! ComponentDividerProperty)
  287. infoContendView.addSubview(divider)
  288. itemXvalue += 1
  289. itemXvalue += 12
  290. lastPropertyWidth = CGRectGetWidth(divider.frame)
  291. } else if property is ComponentDropdownToolProperty {
  292. let dropdownTool = ComponentDropdownTool.init()
  293. dropdownTool.properties = property as! ComponentDropdownToolProperty
  294. dropdownTool.frame = CGRectMake(itemXvalue, CGRectGetHeight(infoContendView.frame)/2-14, dropdownTool.properties.propertyInfo.viewWidth, 28)
  295. dropdownTool.delegate = self
  296. infoContendView.addSubview(dropdownTool)
  297. itemXvalue += CGRectGetWidth(dropdownTool.frame)
  298. itemXvalue += 12
  299. lastPropertyWidth = CGRectGetWidth(dropdownTool.frame)
  300. } else if property is ComponentSelectProperties {
  301. let selectView = ComponentSelect.init()
  302. selectView.properties = property as! ComponentSelectProperties
  303. selectView.frame = CGRectMake(itemXvalue, CGRectGetHeight(infoContendView.frame)/2-14, 144, 28)
  304. if let items = selectView.properties.menuItemArr {
  305. selectView.updateMenuItemsArr(items)
  306. }
  307. selectView.delegate = self
  308. infoContendView.addSubview(selectView)
  309. itemXvalue += CGRectGetWidth(selectView.frame)
  310. itemXvalue += 12
  311. lastPropertyWidth = CGRectGetWidth(selectView.frame)
  312. }
  313. }
  314. itemXvalue -= 12
  315. propertysWidthValue = itemXvalue
  316. contendViewWidthConst.constant = propertysWidthValue
  317. contendLeftConst.constant = CGRectGetWidth(view.frame)/2 - propertysWidthValue/2
  318. }
  319. func setupRightView() {
  320. let subviews = rightContendView.subviews
  321. for view in subviews {
  322. view.removeFromSuperview()
  323. }
  324. var itemXvalue: CGFloat = 0
  325. for property in rightPropertys {
  326. if property is ComponentButtonProperty {
  327. let buttonProperty = (property as! ComponentButtonProperty)
  328. let button = ComponentButton.init()
  329. button.properties = buttonProperty
  330. if buttonProperty.onlyIcon {
  331. button.frame = CGRectMake(itemXvalue, 0, 28, 28)
  332. } else {
  333. button.frame = CGRectMake(itemXvalue, 0, button.properties.propertyInfo.viewWidth, 28)
  334. }
  335. button.setTarget(self, action: #selector(buttonClicked(_:)))
  336. rightContendView.addSubview(button)
  337. itemXvalue += CGRectGetWidth(button.frame)
  338. itemXvalue += 12
  339. } else if property is ComponentDividerProperty {
  340. let divider = ComponentDivider.init()
  341. divider.frame = CGRectMake(itemXvalue, CGRectGetHeight(infoContendView.frame)/2-8, 1, 16)
  342. divider.properties = (property as! ComponentDividerProperty)
  343. rightContendView.addSubview(divider)
  344. itemXvalue += 1
  345. itemXvalue += 12
  346. }
  347. }
  348. itemXvalue -= 12
  349. rightContendViewWidthConst.constant = itemXvalue
  350. }
  351. func reloadMainview() {
  352. let subviews = infoContendView.subviews
  353. for view in subviews {
  354. if view is ComponentButton {
  355. (view as! ComponentButton).reloadData()
  356. } else if view is ComponentDropdownTool {
  357. (view as! ComponentDropdownTool).reloadData()
  358. } else if view is ComponentSelect {
  359. (view as! ComponentSelect).reloadData()
  360. }
  361. }
  362. }
  363. func reloadRightview() {
  364. let subviews = rightContendView.subviews
  365. for view in subviews {
  366. if view is ComponentButton {
  367. (view as! ComponentButton).reloadData()
  368. }
  369. }
  370. }
  371. func refreshToolbarModeInfo() {
  372. guard let viewManager = self.viewManager else { return }
  373. let subviews = infoContendView.subviews
  374. for view in subviews {
  375. if view is ComponentButton {
  376. let item = (view as! ComponentButton)
  377. let resultID = self.returnIDWithSubToolMode(viewManager.subToolMode)
  378. if item.properties.identifier != resultID {
  379. if item.properties.state == .pressed {
  380. item.properties.state = .normal
  381. item.reloadData()
  382. }
  383. } else {
  384. if item.properties.state != .pressed {
  385. item.properties.state = .pressed
  386. item.reloadData()
  387. }
  388. }
  389. }
  390. }
  391. }
  392. //MARK: -
  393. @objc func buttonClicked(_ sender: ComponentButton) {
  394. let subToolMode = returnSubToolModeWithID(sender.properties.identifier)
  395. if subToolMode != .None {
  396. if viewManager?.subToolMode == subToolMode {
  397. viewManager?.subToolMode = .None
  398. } else {
  399. viewManager?.subToolMode = subToolMode
  400. }
  401. } else {
  402. viewManager?.subToolMode = .None
  403. }
  404. refreshToolbarModeInfo()
  405. delegate?.kmPDFSecToolbarControllerDidItemClicked?(self, sender.properties)
  406. }
  407. //MARK: - Mouse
  408. override func mouseDown(with event: NSEvent) {
  409. super.mouseDown(with: event)
  410. view.window?.makeFirstResponder(self)
  411. }
  412. }
  413. //MARK: - ComponentDropdownToolDelegate
  414. extension KMPDFSecToolbarController: ComponentDropdownToolDelegate {
  415. func componentDropdownToolDidShowPopupView(_ view: ComponentDropdownTool) {
  416. delegate?.kmPDFSecToolbarControllerDidItemClicked?(self, view.properties)
  417. }
  418. func componentDropdownToolDidClicked(_ view: ComponentDropdownTool, menuItem: ComponentMenuitemProperty?) {
  419. if let property = menuItem {
  420. property.itemSelected = false
  421. property.state = .normal
  422. delegate?.kmPDFSecToolbarControllerDidItemClicked?(self, property)
  423. }
  424. }
  425. }
  426. //MARK: - ComponentSelectDelegate
  427. extension KMPDFSecToolbarController: ComponentSelectDelegate {
  428. func componentSelectDidSelect(view: ComponentSelect?, menuItemProperty: ComponentMenuitemProperty?) {
  429. if let property = menuItemProperty {
  430. property.itemSelected = false
  431. property.state = .normal
  432. delegate?.kmPDFSecToolbarControllerDidItemClicked?(self, property)
  433. }
  434. }
  435. func componentSelectTextDidBeginEditing(_ view: ComponentSelect) {
  436. delegate?.kmPDFSecToolbarControllerDidSelectTextDidBeginEditing?(self, view)
  437. }
  438. func componentSelectTextDidChange(_ view: ComponentSelect) {
  439. delegate?.kmPDFSecToolbarControllerDidSelectTextDidChange?(self, view)
  440. }
  441. func componentSelectTextDidEndEditing(_ view: ComponentSelect) {
  442. delegate?.kmPDFSecToolbarControllerDidSelectTextDidEndEditing?(self, view)
  443. }
  444. }