KMEditImageController.swift 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540
  1. //
  2. // KMEditImageController.swift
  3. // PDF Reader Pro
  4. //
  5. // Created by Niehaoyu on 2024/11/14.
  6. //
  7. import Cocoa
  8. import KMComponentLibrary
  9. class KMEditImageController: NSViewController {
  10. @IBOutlet var scrollView: NSScrollView!
  11. @IBOutlet var contendView: NSView!
  12. @IBOutlet var sizeBGView: NSView!
  13. @IBOutlet var sizeLabel: NSTextField!
  14. @IBOutlet var sizeSyncButton: ComponentButton!
  15. @IBOutlet var sizeWidthInput: ComponentInputNumber!
  16. @IBOutlet var sizeHeightInput: ComponentInputNumber!
  17. @IBOutlet var rotateBGView: NSView!
  18. @IBOutlet var rotateLabel: NSTextField!
  19. @IBOutlet var rotateSelect: ComponentSelect!
  20. @IBOutlet var rotateLeftButton: ComponentButton!
  21. @IBOutlet var rotateRightButton: ComponentButton!
  22. @IBOutlet var flipVerticalButton: ComponentButton!
  23. @IBOutlet var flipHorizontalButton: ComponentButton!
  24. @IBOutlet var opacityBGView: NSView!
  25. @IBOutlet var opacityLabel: NSTextField!
  26. @IBOutlet var opacitySlider: ComponentSlider!
  27. @IBOutlet var opacitySelect: ComponentSelect!
  28. @IBOutlet var cropButton: ComponentButton!
  29. @IBOutlet var replaceButton: ComponentButton!
  30. @IBOutlet var extractButton: ComponentButton!
  31. @IBOutlet var extractBtnTopConst: NSLayoutConstraint!
  32. @IBOutlet var alignmentBGView: NSView!
  33. private var syncChangeBounds: Bool = true //同步修改宽高
  34. private var groupView: ComponentGroup!
  35. private var alignmentController: KMNAlignmentController?
  36. var pdfView: CPDFListView? {
  37. didSet {
  38. reloadData()
  39. }
  40. }
  41. var areas: [CPDFEditImageArea] = []
  42. //MARK: - func
  43. override func viewDidAppear() {
  44. super.viewDidAppear()
  45. opacitySlider.reloadData()
  46. }
  47. override func viewDidLoad() {
  48. super.viewDidLoad()
  49. setupProperty()
  50. }
  51. func setupProperty() {
  52. //Size
  53. sizeLabel.stringValue = KMLocalizedString("Size")
  54. sizeLabel.textColor = ComponentLibrary.shared.getComponentColorFromKey("colorText/2")
  55. sizeLabel.font = ComponentLibrary.shared.getFontFromKey("mac/body-s-medium")
  56. sizeSyncButton.properties = ComponentButtonProperty(type: .text_gray, size: .xxs, onlyIcon: true, icon: NSImage(named: "sync_Change_unlock"), keepPressState: true)
  57. sizeSyncButton.properties.propertyInfo.leftIcon_press = NSImage(named: "sync_Change_lock")
  58. sizeSyncButton.setTarget(self, action: #selector(sizeSyncButtonClicked(_:)))
  59. sizeSyncButton.reloadData()
  60. sizeWidthInput.properties = ComponentInputNumberProperty(alignment: .center,
  61. size: .s,
  62. state: .normal,
  63. showPrefix: true,
  64. leftIcon: NSImage(named: "w_icon"),
  65. showSuffix: false,
  66. minSize: 1,
  67. maxSize: 1000,
  68. text:"100")
  69. sizeWidthInput.delegate = self
  70. sizeHeightInput.properties = ComponentInputNumberProperty(alignment: .center,
  71. size: .s,
  72. state: .normal,
  73. showPrefix: true,
  74. leftIcon: NSImage(named: "h_icon"),
  75. showSuffix: false,
  76. minSize: 1,
  77. maxSize: 1000,
  78. text:"100")
  79. sizeHeightInput.delegate = self
  80. //Rotate
  81. rotateLabel.stringValue = KMLocalizedString("Rotate & Flip")
  82. rotateLabel.textColor = ComponentLibrary.shared.getComponentColorFromKey("colorText/2")
  83. rotateLabel.font = ComponentLibrary.shared.getFontFromKey("mac/body-s-medium")
  84. rotateSelect.properties = ComponentSelectProperties(size: .s,
  85. state: .normal,
  86. creatable: true,
  87. text: "100",
  88. textUnit: "°",
  89. regexString: "0123456789-")
  90. if true {
  91. var opacityItems: [ComponentMenuitemProperty] = []
  92. for string in ["0°", "45°", "-45°", "90°", "-90°"] {
  93. let item = ComponentMenuitemProperty(type: .normal, text: string)
  94. opacityItems.append(item)
  95. }
  96. rotateSelect.updateMenuItemsArr(opacityItems)
  97. }
  98. rotateSelect.delegate = self
  99. rotateLeftButton.properties = ComponentButtonProperty(type: .text_gray, size: .s, onlyIcon: true, icon: NSImage(named: "pageEdit_rotateLeft"), keepPressState: false)
  100. rotateLeftButton.setTarget(self, action: #selector(buttonClicked(_:)))
  101. rotateRightButton.properties = ComponentButtonProperty(type: .text_gray, size: .s, onlyIcon: true, icon: NSImage(named: "pageEdit_rotateRight"), keepPressState: false)
  102. rotateRightButton.setTarget(self, action: #selector(buttonClicked(_:)))
  103. flipVerticalButton.properties = ComponentButtonProperty(type: .text_gray, size: .s, onlyIcon: true, icon: NSImage(named: "flipVertical"), keepPressState: false)
  104. flipVerticalButton.setTarget(self, action: #selector(buttonClicked(_:)))
  105. flipHorizontalButton.properties = ComponentButtonProperty(type: .text_gray, size: .s, onlyIcon: true, icon: NSImage(named: "flipHorizontal"), keepPressState: false)
  106. flipHorizontalButton.setTarget(self, action: #selector(buttonClicked(_:)))
  107. //Opacity
  108. opacityLabel.stringValue = KMLocalizedString("Opacity")
  109. opacityLabel.textColor = ComponentLibrary.shared.getComponentColorFromKey("colorText/2")
  110. opacityLabel.font = ComponentLibrary.shared.getFontFromKey("mac/body-s-medium")
  111. opacitySlider.properties = ComponentSliderProperty(size: .m, percent: 1)
  112. opacitySlider.delegate = self
  113. opacitySelect.properties = ComponentSelectProperties(size: .s,
  114. state: .normal,
  115. creatable: true,
  116. text: "100",
  117. textUnit: "%",
  118. regexString: "0123456789%")
  119. if true {
  120. var opacityItems: [ComponentMenuitemProperty] = []
  121. for string in ["25%", "50%", "75%", "100%"] {
  122. let item = ComponentMenuitemProperty(type: .normal, text: string)
  123. opacityItems.append(item)
  124. }
  125. opacitySelect.updateMenuItemsArr(opacityItems)
  126. }
  127. opacitySelect.delegate = self
  128. cropButton.properties = ComponentButtonProperty(type: .default_tertiary, size: .s, buttonText: KMLocalizedString("Crop"), keepPressState: false)
  129. cropButton.setTarget(self, action: #selector(buttonClicked(_:)))
  130. replaceButton.properties = ComponentButtonProperty(type: .default_tertiary, size: .s, buttonText: KMLocalizedString("Replace"), keepPressState: false)
  131. replaceButton.setTarget(self, action: #selector(buttonClicked(_:)))
  132. extractButton.properties = ComponentButtonProperty(type: .default_tertiary, size: .s, buttonText: KMLocalizedString("Extract"), keepPressState: false)
  133. extractButton.setTarget(self, action: #selector(buttonClicked(_:)))
  134. if alignmentController == nil {
  135. alignmentController = KMNAlignmentController.init()
  136. }
  137. alignmentController?.view.frame = alignmentBGView.bounds
  138. alignmentController?.view.autoresizingMask = [.width, .height]
  139. alignmentController?.delegate = self
  140. alignmentBGView.addSubview(alignmentController!.view)
  141. }
  142. func reloadData() {
  143. areas = pdfView?.km_editingImageAreas() ?? []
  144. if areas.count == 0 {
  145. } else if areas.count == 1 {
  146. cropButton.isHidden = false
  147. replaceButton.isHidden = false
  148. extractBtnTopConst.constant = 112
  149. alignmentBGView.isHidden = true
  150. } else if areas.count > 1 {
  151. cropButton.isHidden = true
  152. replaceButton.isHidden = true
  153. extractBtnTopConst.constant = 16
  154. alignmentBGView.isHidden = false
  155. if areas.count == 2 {
  156. alignmentController?.updateMulti(false)
  157. } else {
  158. alignmentController?.updateMulti(true)
  159. }
  160. }
  161. sizeWidthInput.properties.isDisabled = pdfView?.isEditImage == true
  162. sizeHeightInput.properties.isDisabled = pdfView?.isEditImage == true
  163. sizeSyncButton.properties.isDisabled = pdfView?.isEditImage == true
  164. rotateSelect.properties.isDisabled = pdfView?.isEditImage == true
  165. rotateLeftButton.properties.isDisabled = pdfView?.isEditImage == true
  166. rotateLeftButton.reloadData()
  167. rotateRightButton.properties.isDisabled = pdfView?.isEditImage == true
  168. rotateRightButton.reloadData()
  169. flipVerticalButton.properties.isDisabled = pdfView?.isEditImage == true
  170. flipVerticalButton.reloadData()
  171. flipHorizontalButton.properties.isDisabled = pdfView?.isEditImage == true
  172. flipHorizontalButton.reloadData()
  173. opacitySlider.properties.isDisabled = pdfView?.isEditImage == true
  174. opacitySlider.reloadData()
  175. opacitySelect.properties.isDisabled = pdfView?.isEditImage == true
  176. replaceButton.properties.isDisabled = pdfView?.isEditImage == true
  177. replaceButton.reloadData()
  178. extractButton.properties.isDisabled = pdfView?.isEditImage == true
  179. extractButton.reloadData()
  180. if let area = areas.first {
  181. //Size
  182. if syncChangeBounds {
  183. sizeSyncButton.properties.state = .pressed
  184. } else {
  185. sizeSyncButton.properties.state = .normal
  186. }
  187. sizeSyncButton.reloadData()
  188. if areas.count > 1 {
  189. sizeWidthInput.properties.text = "-"
  190. sizeWidthInput.properties.multiState = true
  191. sizeHeightInput.properties.text = "-"
  192. sizeHeightInput.properties.multiState = true
  193. } else {
  194. let areaFrame = area.bounds
  195. sizeWidthInput.properties.text = String(format: "%.1f", areaFrame.size.width)
  196. sizeWidthInput.properties.multiState = false
  197. sizeHeightInput.properties.text = String(format: "%.1f", areaFrame.size.height)
  198. sizeHeightInput.properties.multiState = false
  199. }
  200. sizeWidthInput.properties.maxSize = Int(area.page.bounds.size.width)
  201. sizeHeightInput.properties.maxSize = Int(area.page.bounds.size.height)
  202. sizeWidthInput.reloadData()
  203. sizeHeightInput.reloadData()
  204. //Rotate
  205. if let rotates = pdfView?.km_editAreasRotates([area]) {
  206. if rotates.count > 0 {
  207. let rotate = rotates.first ?? 1
  208. rotateSelect.properties.text = String(format: "%.0f%@", rotate, "°")
  209. rotateSelect.reloadData()
  210. }
  211. }
  212. //Opacity
  213. if let opacitys = pdfView?.km_editAreasOpacitys([area]) {
  214. if opacitys.count > 0 {
  215. var opacity = opacitys.first ?? 1
  216. opacity = min(1, opacity)
  217. opacity = max(0, opacity)
  218. opacitySelect.properties.text = String(format: "%.0f%@", opacity*100, "%")
  219. opacitySelect.reloadData()
  220. opacitySlider.properties.percent = opacity
  221. opacitySlider.reloadData()
  222. }
  223. }
  224. if pdfView?.isEditImage == true {
  225. cropButton.properties.buttonText = KMLocalizedString("Cancel Crop")
  226. } else {
  227. cropButton.properties.buttonText = KMLocalizedString("Crop")
  228. }
  229. cropButton.reloadData()
  230. }
  231. }
  232. //MARK: - Action
  233. @objc func sizeSyncButtonClicked(_ sender: ComponentButton) {
  234. syncChangeBounds = !syncChangeBounds
  235. if syncChangeBounds {
  236. sizeSyncButton.properties.state = .pressed
  237. } else {
  238. sizeSyncButton.properties.state = .normal
  239. }
  240. sizeSyncButton.reloadData()
  241. }
  242. @objc func buttonClicked(_ sender: ComponentButton) {
  243. if sender == rotateLeftButton {
  244. self.pdfView?.leftRotateAction()
  245. } else if sender == rotateRightButton {
  246. self.pdfView?.rightRotateAction()
  247. } else if sender == flipVerticalButton {
  248. self.pdfView?.reverseYAction()
  249. } else if sender == flipHorizontalButton {
  250. self.pdfView?.reverseXAction()
  251. } else if sender == cropButton {
  252. if pdfView?.isEditImage == true {
  253. pdfView?.cropCancelAction()
  254. } else {
  255. pdfView?.cropAction()
  256. }
  257. } else if sender == replaceButton {
  258. if let areas = self.pdfView?.km_editingImageAreas() {
  259. let panel = NSOpenPanel()
  260. panel.allowsMultipleSelection = false
  261. panel.allowedFileTypes = ["png","jpg"]
  262. panel.beginSheetModal(for: NSApp.mainWindow!) { response in
  263. if response == .OK {
  264. let openPath = panel.url?.path
  265. for area in areas {
  266. self.pdfView?.replace(area, imagePath: openPath!)
  267. }
  268. }
  269. }
  270. }
  271. } else if sender == extractButton {
  272. extractAction()
  273. }
  274. reloadData()
  275. }
  276. func extractAction() {
  277. var menuItemArr: [ComponentMenuitemProperty] = []
  278. let items: [(String, String)] = [("jpg", "export_jpg_Key"),
  279. ("png", "export_png_Key"),
  280. ("pdf", "export_pdf_Key")]
  281. for (i, value) in items {
  282. let properties_Menuitem: ComponentMenuitemProperty = ComponentMenuitemProperty(multipleSelect: false,
  283. itemSelected: false,
  284. keyEquivalent: nil,
  285. text: KMLocalizedString(i),
  286. identifier: value)
  287. menuItemArr.append(properties_Menuitem)
  288. }
  289. if groupView == nil {
  290. groupView = ComponentGroup.createFromNib(in: ComponentLibrary.shared.componentBundle())
  291. }
  292. groupView.groupDelegate = self
  293. groupView?.frame = CGRectMake(0, 0, extractButton.frame.size.width, 36*3+8)
  294. groupView.updateGroupInfo(menuItemArr)
  295. if let point: CGPoint = extractButton.superview?.convert(extractButton.frame.origin, to: self.view.window?.contentView) {
  296. groupView.showWithPoint(CGPoint(x: point.x, y: point.y - CGRectGetHeight(groupView.frame)-4), relativeTo: self.view)
  297. }
  298. }
  299. //MARK: - MouseEvent
  300. override func mouseDown(with event: NSEvent) {
  301. super.mouseDown(with: event)
  302. view.window?.makeFirstResponder(nil)
  303. }
  304. }
  305. //MARK: - ComponentInputNumberDelegate
  306. extension KMEditImageController: ComponentInputNumberDelegate {
  307. func componentInputNumberDidValueChanged(inputNumber: ComponentInputNumber?) {
  308. if areas.count > 1 || areas.count == 0 {
  309. return
  310. }
  311. var textValue = inputNumber?.properties.text?.stringToCGFloat() ?? 0
  312. if inputNumber == sizeWidthInput {
  313. let areas = self.pdfView?.km_editingImageAreas() ?? []
  314. for area in areas {
  315. pdfView?.updateArea(area, newWidth: textValue, syncChangeBounds)
  316. }
  317. } else if inputNumber == sizeHeightInput {
  318. let areas = self.pdfView?.km_editingImageAreas() ?? []
  319. for area in areas {
  320. pdfView?.updateArea(area, newHeight: textValue, syncChangeBounds)
  321. }
  322. }
  323. reloadData()
  324. }
  325. func componentInputNumberDidIncrease(inputNumber: ComponentInputNumber?) {
  326. if areas.count == 1 || areas.count == 0 {
  327. return
  328. }
  329. //只处理多选状态
  330. var textValue = inputNumber?.properties.text?.stringToCGFloat() ?? 0
  331. if inputNumber == sizeWidthInput {
  332. let areas = self.pdfView?.km_editingImageAreas() ?? []
  333. for area in areas {
  334. textValue = area.bounds.size.width + 1
  335. pdfView?.updateArea(area, newWidth: textValue, syncChangeBounds)
  336. }
  337. } else if inputNumber == sizeHeightInput {
  338. let areas = self.pdfView?.km_editingImageAreas() ?? []
  339. for area in areas {
  340. textValue = area.bounds.size.height + 1
  341. pdfView?.updateArea(area, newHeight: textValue, syncChangeBounds)
  342. }
  343. }
  344. reloadData()
  345. }
  346. func componentInputNumberDidDecrease(inputNumber: ComponentInputNumber?) {
  347. if areas.count == 1 || areas.count == 0 {
  348. return
  349. }
  350. //只处理多选状态
  351. var textValue = inputNumber?.properties.text?.stringToCGFloat() ?? 0
  352. if inputNumber == sizeWidthInput {
  353. let areas = self.pdfView?.km_editingImageAreas() ?? []
  354. for area in areas {
  355. textValue = area.bounds.size.width - 1
  356. pdfView?.updateArea(area, newWidth: textValue, syncChangeBounds)
  357. }
  358. } else if inputNumber == sizeHeightInput {
  359. let areas = self.pdfView?.km_editingImageAreas() ?? []
  360. for area in areas {
  361. textValue = area.bounds.size.height - 1
  362. pdfView?.updateArea(area, newHeight: textValue, syncChangeBounds)
  363. }
  364. }
  365. reloadData()
  366. }
  367. }
  368. //MARK: - ComponentSelectDelegate
  369. extension KMEditImageController: ComponentSelectDelegate {
  370. func componentSelectDidSelect(view: ComponentSelect?, menuItemProperty: ComponentMenuitemProperty?) {
  371. if view == opacitySelect {
  372. if let text = opacitySelect.properties.text {
  373. let result = text.stringByDeleteCharString("%")
  374. let opacity = result.stringToCGFloat()/100
  375. pdfView?.setEditingAreasOpacity(opacity)
  376. reloadData()
  377. }
  378. } else if view == rotateSelect {
  379. if let text = rotateSelect.properties.text {
  380. let result = text.stringByDeleteCharString("°")
  381. let value = result.stringToCGFloat()
  382. pdfView?.rotateEditingAreas(value)
  383. reloadData()
  384. }
  385. }
  386. }
  387. func componentSelectTextDidEndEditing(_ view: ComponentSelect) {
  388. if view == opacitySelect {
  389. if let text = opacitySelect.properties.text {
  390. let result = text.stringByDeleteCharString("%")
  391. let opacity = result.stringToCGFloat()/100
  392. pdfView?.setEditingAreasOpacity(opacity)
  393. reloadData()
  394. }
  395. } else if view == rotateSelect {
  396. if let text = rotateSelect.properties.text {
  397. let result = text.stringByDeleteCharString("°")
  398. let value = result.stringToCGFloat()
  399. pdfView?.rotateEditingAreas(value)
  400. reloadData()
  401. }
  402. }
  403. }
  404. }
  405. //MARK: - ComponentCColorDelegate
  406. extension KMEditImageController: ComponentCColorDelegate {
  407. }
  408. //MARK: - ComponentSliderDelegate
  409. extension KMEditImageController: ComponentSliderDelegate {
  410. func componentSliderDidUpdate(_ view: ComponentSlider) {
  411. let percent = view.properties.percent
  412. self.pdfView?.setEditingAreasOpacity(percent)
  413. reloadData()
  414. }
  415. }
  416. //MARK: - ComponentGroupDelegate
  417. extension KMEditImageController: ComponentGroupDelegate {
  418. func componentGroupDidSelect(group: ComponentGroup?, menuItemProperty: ComponentMenuitemProperty?) {
  419. if menuItemProperty?.identifier == "export_jpg_Key" {
  420. pdfView?.exportEditingImageAreasAction(format: "jpg")
  421. } else if menuItemProperty?.identifier == "export_png_Key" {
  422. pdfView?.exportEditingImageAreasAction(format: "png")
  423. } else if menuItemProperty?.identifier == "export_pdf_Key" {
  424. pdfView?.exportEditingImageAreasAction(format: "pdf")
  425. }
  426. }
  427. }
  428. //MARK: - KMNAlignmentControllerDelegate
  429. extension KMEditImageController: KMNAlignmentControllerDelegate {
  430. func alignmentControllerDidClick(_ controller: KMNAlignmentController, _ alignmentType: KMPDFActiveFormsAlignType) {
  431. pdfView?.changeEditingAreas(alignmentType)
  432. }
  433. }