KMEditImagePropertyViewController.swift 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624
  1. //
  2. // KMEditImagePropertyViewController.swift
  3. // PDF Master
  4. //
  5. // Created by lxy on 2022/12/28.
  6. //
  7. import Cocoa
  8. public enum KMOperationState {
  9. case none
  10. case began
  11. case changed
  12. case ended
  13. }
  14. enum KMEditImagePropertyViewControllerChangeType {
  15. case rotate
  16. case flip
  17. case add
  18. case move
  19. }
  20. protocol KMEditImagePropertyViewControllerDelegate: NSObject {
  21. func editImagePropertyViewControllerDidChanged(controller: KMEditImagePropertyViewController, type: KMEditImagePropertyViewControllerChangeType)
  22. }
  23. class KMEditImagePropertyViewController: NSViewController {
  24. @IBOutlet weak var titleLabel: NSTextField!
  25. @IBOutlet weak var editImageView: NSImageView!
  26. @IBOutlet weak var headerBox: NSBox!
  27. @IBOutlet weak var headerBoxHeight: NSLayoutConstraint!
  28. @IBOutlet weak var headerBoxMaginWidthConstraint: NSLayoutConstraint!
  29. @IBOutlet weak var opacityTitleLabel: NSTextField!
  30. @IBOutlet weak var opacitySlider: NSSlider!
  31. @IBOutlet weak var opacityBox: NSBox!
  32. @IBOutlet weak var opacityBoxBottomMaginConstraint: NSLayoutConstraint!
  33. @IBOutlet weak var opacityBoxTopConstraint: NSLayoutConstraint!
  34. @IBOutlet weak var cropBox: NSBox!
  35. @IBOutlet weak var confirmBox: NSBox!
  36. @IBOutlet weak var cancelBox: NSBox!
  37. @IBOutlet weak var replaceBox: NSBox!
  38. @IBOutlet weak var exportBox: NSBox!
  39. @IBOutlet weak var imageBox: NSBox!
  40. @IBOutlet weak var imageBoxHeight: NSLayoutConstraint!
  41. @IBOutlet weak var opacityBoxHeight: NSLayoutConstraint!
  42. @IBOutlet weak var buttonBoxHeight: NSLayoutConstraint!
  43. @IBOutlet weak var buttonBox: NSBox!
  44. @IBOutlet weak var rotateLeftBox: NSBox!
  45. @IBOutlet weak var rotateRightBox: NSBox!
  46. @IBOutlet weak var flipHorizontalBox: NSBox!
  47. @IBOutlet weak var flipVerticalBox: NSBox!
  48. @IBOutlet weak var alignmentView: KMEditPropertyAlignmentView!
  49. @IBOutlet weak var alignmentViewTopConstraint: NSLayoutConstraint!
  50. var rotateLeftVC: KMDesignPropertySelector?
  51. var rotateRightVC: KMDesignPropertySelector?
  52. var flipHorizontalVC: KMDesignPropertySelector?
  53. var flipVerticalVC: KMDesignPropertySelector?
  54. var opacityVC: KMDesignSelect?
  55. var cropVC: KMDesignButton?
  56. var confirmVC: KMDesignButton?
  57. var cancelVC: KMDesignButton?
  58. var replaceVC: KMDesignButton?
  59. var exportVC: KMDesignButton?
  60. var imagesAreas : [CPDFEditImageArea] = []
  61. var listView : CPDFListView!
  62. var editingAreas: [Any] {
  63. get {
  64. return self.listView.editingAreas() ?? []
  65. }
  66. }
  67. weak var delegate: KMEditImagePropertyViewControllerDelegate?
  68. override func viewDidLoad() {
  69. super.viewDidLoad()
  70. self.setup()
  71. self.initData()
  72. self.updateLanguage()
  73. self.updateButtonState(hidden: true)
  74. self.reloadData()
  75. }
  76. func initData() {
  77. self.updateButtonState(hidden: true)
  78. }
  79. func setup() {
  80. self.titleLabel.font = NSFont.SFProTextSemibold(14.0)
  81. self.titleLabel.textColor = NSColor(hex: "#252629")
  82. self.opacityTitleLabel.font = NSFont.SFProTextSemibold(12.0)
  83. self.opacityTitleLabel.textColor = NSColor(hex: "#616469")
  84. self.imageBox.borderWidth = 1
  85. self.imageBox.borderColor = NSColor(hex: "#DFE1E5")
  86. self.imageBox.cornerRadius = 4
  87. self.imageBox.fillColor = NSColor(hex: "#FFFFFF")
  88. self.alignmentView.didChange = {[unowned self] view, areasArray, boundsArray in
  89. self.changeAreasAlign(areas: areasArray, newBounds: boundsArray)
  90. }
  91. imageBox.backgroundColor(NSColor(hex: "#FFFFFF"))
  92. opacityVC = KMDesignSelect.init(withType: .PopButton)
  93. opacityBox.contentView = opacityVC?.view
  94. opacityBox.fillColor = NSColor.clear
  95. opacityVC?.addItems(withObjectValues: ["25%","50%","75%","100%"])
  96. opacityVC?.selectItem(at: 0)
  97. opacityVC?.delete = self
  98. //alignment
  99. rotateLeftVC = KMDesignPropertySelector.init(withType: .Icon_Btn)
  100. rotateLeftBox.contentView = rotateLeftVC?.view
  101. rotateLeftBox.fillColor = NSColor.clear
  102. rotateLeftVC?.target = self
  103. rotateLeftVC?.action = #selector(leftRotationImageAction)
  104. rotateLeftVC?.image = NSImage(named: "KMImageNameEditPDFRotationLeft")!
  105. rotateLeftVC?._image_disabled = NSImage(named: "KMImageNameEditPDFRotationLeft")!
  106. rotateLeftVC?.updateUI()
  107. rotateRightVC = KMDesignPropertySelector.init(withType: .Icon_Btn)
  108. rotateRightBox.contentView = rotateRightVC?.view
  109. rotateRightBox.fillColor = NSColor.clear
  110. rotateRightVC?.target = self
  111. rotateRightVC?.action = #selector(rightRotationImageAction)
  112. rotateRightVC?.image = NSImage(named: "KMImageNameEditPDFRotationRight")!
  113. rotateRightVC?.updateUI()
  114. flipHorizontalVC = KMDesignPropertySelector.init(withType: .Icon_Btn)
  115. flipHorizontalBox.contentView = flipHorizontalVC?.view
  116. flipHorizontalBox.fillColor = NSColor.clear
  117. flipHorizontalVC?.target = self
  118. flipHorizontalVC?.action = #selector(flipHorizontalImageAction)
  119. flipHorizontalVC?.image = NSImage(named: "KMImageNameEditPDFFlipHorizontal")!
  120. flipHorizontalVC?.updateUI()
  121. flipVerticalVC = KMDesignPropertySelector.init(withType: .Icon_Btn)
  122. flipVerticalBox.contentView = flipVerticalVC?.view
  123. flipVerticalBox.fillColor = NSColor.clear
  124. flipVerticalVC?.target = self
  125. flipVerticalVC?.action = #selector(flipVerticalImageAction)
  126. flipVerticalVC?.image = NSImage(named: "KMImageNameEditPDFFlipVertical")!
  127. flipVerticalVC?.updateUI()
  128. // 设置默认状态下的滑块图像
  129. let customCell = CustomSliderCell()
  130. // 设置滑块图像
  131. customCell.knobImage = NSImage(named: "KMImageNameEditPDFSlider")
  132. // 应用自定义的NSSliderCell到NSSlider
  133. opacitySlider.cell = customCell
  134. // 添加滑动事件处理程序
  135. opacitySlider.target = self
  136. opacitySlider.action = #selector(sliderValueChanged(_:))
  137. opacitySlider.isContinuous = false
  138. cropVC = KMDesignButton.init(withType: .Text)
  139. cropBox.contentView = cropVC?.view
  140. cropBox.fillColor = NSColor.clear
  141. cropVC?.target = self
  142. cropVC?.action = #selector(cutImageAction)
  143. cropVC?.button(type: .Sec, size: .m)
  144. cropVC?.stringValue = NSLocalizedString("Crop", comment: "")
  145. cropVC?.updateUI()
  146. confirmVC = KMDesignButton.init(withType: .Text)
  147. confirmBox.contentView = confirmVC?.view
  148. confirmBox.fillColor = NSColor.clear
  149. confirmVC?.target = self
  150. confirmVC?.action = #selector(confirmVCImageAction)
  151. confirmVC?.button(type: .Cta, size: .m)
  152. confirmVC?.stringValue = NSLocalizedString("Confirm Cut", comment: "")
  153. confirmVC?.updateUI()
  154. confirmVC?.button.keyEquivalent = KMKeyEquivalent.enter
  155. cancelVC = KMDesignButton.init(withType: .Text)
  156. cancelBox.contentView = cancelVC?.view
  157. cancelBox.fillColor = NSColor.clear
  158. cancelVC?.target = self
  159. cancelVC?.action = #selector(cancelCutImageAction)
  160. cancelVC?.button(type: .Sec, size: .m)
  161. cancelVC?.stringValue = NSLocalizedString("Cancel", comment: "")
  162. cancelVC?.updateUI()
  163. replaceVC = KMDesignButton.init(withType: .Text)
  164. replaceBox.contentView = replaceVC?.view
  165. replaceBox.fillColor = NSColor.clear
  166. replaceVC?.target = self
  167. replaceVC?.action = #selector(replaceImageAction)
  168. replaceVC?.button(type: .Sec, size: .m)
  169. replaceVC?.stringValue = NSLocalizedString("Replace", comment: "")
  170. replaceVC?.updateUI()
  171. exportVC = KMDesignButton.init(withType: .Text)
  172. exportBox.contentView = exportVC?.view
  173. exportBox.fillColor = NSColor.clear
  174. exportVC?.target = self
  175. exportVC?.action = #selector(exportImageAction)
  176. exportVC?.button(type: .Sec, size: .m)
  177. exportVC?.stringValue = NSLocalizedString("Export", comment: "")
  178. exportVC?.updateUI()
  179. }
  180. func updateLanguage() {
  181. let areas = self.editingAreas
  182. if imagesAreas.count > 0 && imagesAreas.count != areas.count { //多选图片跟文字
  183. self.titleLabel.stringValue = NSLocalizedString("General Properties", comment: "")
  184. } else {
  185. self.titleLabel.stringValue = NSLocalizedString("Image", comment: "")
  186. }
  187. self.opacityTitleLabel.stringValue = NSLocalizedString("Opacity", comment: "")
  188. self.cropVC?.stringValue = NSLocalizedString("Crop", comment: "")
  189. self.confirmVC?.stringValue = NSLocalizedString("Confirm Crop", comment: "")
  190. self.cancelVC?.stringValue = NSLocalizedString("Cancel", comment: "")
  191. self.replaceVC?.stringValue = NSLocalizedString("Replace", comment: "")
  192. self.exportVC?.stringValue = NSLocalizedString("Export", comment: "")
  193. }
  194. func reloadData() {
  195. imagesAreas = []
  196. if self.editingAreas.count > 0 {
  197. let areas = self.editingAreas
  198. self.alignmentView.editingAreas = areas
  199. for i in 0 ... areas.count-1 {
  200. if areas[i] is CPDFEditImageArea {
  201. imagesAreas.append(areas[i] as! CPDFEditImageArea)
  202. }
  203. }
  204. if imagesAreas.count == 1 && imagesAreas.count == areas.count{ //单个图片
  205. self.listView.selectImageAreas = imagesAreas.first
  206. self.headerBox.isHidden = false
  207. self.headerBoxHeight.constant = 176
  208. self.headerBoxMaginWidthConstraint.constant = 8
  209. self.imageBox.isHidden = false
  210. self.imageBoxHeight.constant = 88
  211. self.opacityBox.isHidden = false
  212. self.opacityBoxHeight.constant = 56
  213. self.opacityBoxBottomMaginConstraint.constant = 16
  214. self.opacityBoxTopConstraint.constant = 16
  215. self.buttonBox.isHidden = false
  216. self.cropBox.isHidden = false
  217. self.cancelBox.isHidden = false
  218. self.replaceBox.isHidden = false
  219. self.buttonBoxHeight.constant = 112
  220. self.editImageView.image = self.listView.selectImageAreas.thumbnailImage
  221. self.alignmentViewTopConstraint.constant = 16
  222. let opacity: CGFloat = self.listView.opacity(for: imagesAreas.first)
  223. self.updateImageAreasOpacity(opacity: opacity, state: .ended, needListView: false)
  224. } else if imagesAreas.count > 1 && imagesAreas.count == areas.count { //多选图片
  225. self.headerBox.isHidden = false
  226. self.headerBoxHeight.constant = 82
  227. self.headerBoxMaginWidthConstraint.constant = 0
  228. self.imageBox.isHidden = true
  229. self.imageBoxHeight.constant = 0
  230. self.opacityBox.isHidden = false
  231. self.opacityBoxHeight.constant = 56
  232. self.opacityBoxBottomMaginConstraint.constant = 0
  233. self.opacityBoxTopConstraint.constant = 0
  234. self.buttonBox.isHidden = false
  235. self.cropBox.isHidden = true
  236. self.cancelBox.isHidden = true
  237. self.replaceBox.isHidden = true
  238. self.buttonBoxHeight.constant = 0
  239. self.alignmentViewTopConstraint.constant = 16
  240. var opacity = self.listView.opacity(for: imagesAreas.first)
  241. for area in imagesAreas {
  242. let newOpacity = self.listView.opacity(for: area)
  243. if (opacity < newOpacity) {
  244. opacity = newOpacity
  245. }
  246. }
  247. self.updateImageAreasOpacity(opacity: opacity, state: .ended, needListView: false)
  248. } else if imagesAreas.count > 0 && imagesAreas.count != areas.count { //多选图片跟文字
  249. self.opacityBoxBottomMaginConstraint.constant = 0
  250. self.headerBoxMaginWidthConstraint.constant = 0
  251. self.alignmentViewTopConstraint.constant = 0
  252. self.opacityBoxTopConstraint.constant = 0
  253. // self.headerBox.isHidden = true
  254. self.headerBoxHeight.constant = 48
  255. self.imageBox.isHidden = true
  256. self.imageBoxHeight.constant = 0
  257. self.opacityBox.isHidden = true
  258. self.opacityBoxHeight.constant = 0
  259. self.buttonBox.isHidden = true
  260. self.buttonBoxHeight.constant = 0
  261. }
  262. self.updateLanguage()
  263. } else {
  264. self.updateButtonState(hidden: true)
  265. self.editImageView.image = NSImage()
  266. }
  267. }
  268. func updateButtonState(hidden: Bool) {
  269. if hidden {
  270. self.cancelBox.isHidden = true
  271. self.confirmBox.isHidden = true
  272. self.cropBox.isHidden = false
  273. self.replaceVC?.enabled = true
  274. self.exportVC?.enabled = true
  275. self.opacitySlider.isEnabled = true
  276. self.opacityVC?.enabled = true
  277. self.rotateLeftVC?.button.isEnabled = true
  278. self.rotateLeftVC?.view.alphaValue = 1.0
  279. self.rotateRightVC?.button.isEnabled = true
  280. self.rotateRightVC?.view.alphaValue = 1.0
  281. self.flipVerticalVC?.button.isEnabled = true
  282. self.flipVerticalVC?.view.alphaValue = 1.0
  283. self.flipHorizontalVC?.button.isEnabled = true
  284. self.flipHorizontalVC?.view.alphaValue = 1.0
  285. } else {
  286. self.cancelBox.isHidden = false
  287. self.confirmBox.isHidden = false
  288. self.cropBox.isHidden = true
  289. self.replaceVC?.enabled = false
  290. self.exportVC?.enabled = false
  291. self.opacitySlider.isEnabled = false
  292. self.opacityVC?.enabled = false
  293. self.rotateLeftVC?.button.isEnabled = false
  294. self.rotateLeftVC?.view.alphaValue = 0.5
  295. self.rotateRightVC?.button.isEnabled = false
  296. self.rotateRightVC?.view.alphaValue = 0.5
  297. self.flipVerticalVC?.button.isEnabled = false
  298. self.flipVerticalVC?.view.alphaValue = 0.5
  299. self.flipHorizontalVC?.button.isEnabled = false
  300. self.flipHorizontalVC?.view.alphaValue = 0.5
  301. }
  302. }
  303. func updateImageAreasOpacity(opacity: CGFloat, state: KMOperationState, needListView: Bool = true) {
  304. if self.editingAreas.count >= 1 {
  305. if needListView {
  306. for area in imagesAreas {
  307. self.listView.setOpacityEditArea(area, opacity: opacity)
  308. }
  309. }
  310. // self.listView.editingAreas()!.count == 1 &&
  311. if (self.listView.editingAreas()?.first is CPDFEditImageArea) {
  312. self.listView.selectImageAreas = self.listView.editingAreas()!.first as? CPDFEditImageArea
  313. self.editImageView.image = self.listView.selectImageAreas.thumbnailImage
  314. self.opacitySlider.objectValue = self.listView.opacity(for: self.listView.selectImageAreas)
  315. self.opacityVC?.stringValue = Int(opacity * 100).description + "%"
  316. self.editImageView.alphaValue = opacity
  317. }
  318. }
  319. }
  320. private func changeAreasAlign(areas:[Any],newBounds:[String]) {
  321. var oldBounds : [String] = []
  322. for i in 0 ... areas.count-1 {
  323. let area : CPDFEditArea = areas[i] as! CPDFEditArea
  324. oldBounds.append(NSStringFromRect(area.bounds))
  325. self.listView.setBoundsEditArea(area, withBounds: NSRectFromString(newBounds[i]))
  326. }
  327. self.listView.setNeedsDisplayForVisiblePages()
  328. }
  329. }
  330. extension KMEditImagePropertyViewController: KMSelectPopButtonDelegate {
  331. func km_comboBoxSelectionDidChange(_ obj: KMDesignSelect) {
  332. if obj == opacityVC {
  333. KMPrint("km_comboBoxSelectionDidChange")
  334. let index = obj.indexOfSelectedItem
  335. var string = obj.items[index]
  336. string = string.replacingOccurrences(of: "%", with: "")
  337. let value = CGFloat(Float(string)! * 0.01)
  338. self.updateImageAreasOpacity(opacity: value, state: .changed)
  339. }
  340. }
  341. func km_controlTextDidEndEditing(_ obj: KMDesignSelect) {
  342. if obj == opacityVC {
  343. KMPrint("km_comboBoxSelectionDidChange")
  344. let index = obj.indexOfSelectedItem
  345. var string = obj.items[index]
  346. string = string.replacingOccurrences(of: "%", with: "")
  347. let value = CGFloat(Float(string)! * 0.01)
  348. self.updateImageAreasOpacity(opacity: value, state: .ended)
  349. }
  350. }
  351. }
  352. //MARK: - Action
  353. extension KMEditImagePropertyViewController {
  354. @objc func sliderValueChanged(_ sender: NSSlider) {
  355. let sliderValue = sender.floatValue
  356. self.updateImageAreasOpacity(opacity:CGFloat(sliderValue), state: .changed)
  357. }
  358. @IBAction func rightRotationImageAction(_ sender: Any) {
  359. if self.listView.editingAreas()?.count ?? 0 < 1 {
  360. return
  361. }
  362. let areas = self.listView.editingAreas()
  363. if areas!.count == 1 && (areas!.first is CPDFEditImageArea) {
  364. let imageArea = areas!.first as! CPDFEditImageArea
  365. self.listView.rotate(with: imageArea, rotate: 90)
  366. self.listView.selectImageAreas = imageArea
  367. self.editImageView.image = self.listView.selectImageAreas.thumbnailImage
  368. } else if areas!.count > 1 {
  369. for imagesArea in imagesAreas {
  370. self.listView.rotate(with: imagesArea, rotate: 90)
  371. }
  372. }
  373. self.delegate?.editImagePropertyViewControllerDidChanged(controller: self, type: .rotate)
  374. }
  375. @IBAction func leftRotationImageAction(_ sender: Any) {
  376. if self.listView.editingAreas()?.count ?? 0 < 1 {
  377. return
  378. }
  379. let areas = self.listView.editingAreas()
  380. if areas!.count == 1 && (areas!.first is CPDFEditImageArea) {
  381. self.listView.rotate(with: self.listView.selectImageAreas, rotate: -90)
  382. if self.listView.editingAreas()!.count == 1 && (self.listView.editingAreas()!.first is CPDFEditImageArea) {
  383. self.listView.selectImageAreas = self.listView.editingAreas()!.first as? CPDFEditImageArea
  384. }
  385. self.editImageView.image = self.listView.selectImageAreas.thumbnailImage
  386. } else {
  387. for imagesArea in imagesAreas {
  388. self.listView.rotate(with: imagesArea, rotate: -90)
  389. }
  390. }
  391. self.delegate?.editImagePropertyViewControllerDidChanged(controller: self, type: .rotate)
  392. }
  393. @IBAction func flipHorizontalImageAction(_ sender: Any) {
  394. if self.listView.editingAreas()?.count ?? 0 < 1 {
  395. return
  396. }
  397. let areas = self.listView.editingAreas()
  398. if areas != nil {
  399. for item in areas! {
  400. if item is CPDFEditImageArea {
  401. self.listView.horizontalMirror(with: item as? CPDFEditImageArea)
  402. if areas?.count == 1 {
  403. self.editImageView.image = (item as AnyObject).thumbnailImage
  404. }
  405. } else {
  406. for imagesArea in imagesAreas {
  407. self.listView.horizontalMirror(with: imagesArea)
  408. }
  409. }
  410. }
  411. }
  412. self.delegate?.editImagePropertyViewControllerDidChanged(controller: self, type: .flip)
  413. }
  414. @IBAction func flipVerticalImageAction(_ sender: Any) {
  415. if self.listView.editingAreas()?.count ?? 0 < 1 {
  416. return
  417. }
  418. let areas = self.listView.editingAreas()
  419. if areas != nil {
  420. for item in areas! {
  421. if item is CPDFEditImageArea {
  422. self.listView.verticalMirror(with: item as? CPDFEditImageArea)
  423. self.editImageView.image = self.listView.selectImageAreas.thumbnailImage
  424. } else {
  425. for imagesArea in imagesAreas {
  426. self.listView.verticalMirror(with: imagesArea)
  427. }
  428. }
  429. }
  430. }
  431. self.delegate?.editImagePropertyViewControllerDidChanged(controller: self, type: .flip)
  432. }
  433. @IBAction func cutImageAction(_ sender: Any) {
  434. if self.listView.selectImageAreas == nil {
  435. return
  436. }
  437. self.listView.isEditImage = true
  438. self.listView.enterCrop(with: self.listView.selectImageAreas)
  439. self.updateButtonState(hidden: false)
  440. }
  441. @IBAction func confirmVCImageAction(_ sender: Any) {
  442. if self.listView.selectImageAreas == nil || self.listView.cropAreas == nil {
  443. return
  444. }
  445. self.listView.cropEditImageArea(self.listView.selectImageAreas, withBounds: self.listView.cropAreas.cropRect)
  446. self.cancelCutImageAction(cancelVC?.button as Any)
  447. }
  448. @IBAction func cancelCutImageAction(_ sender: Any) {
  449. if self.listView.selectImageAreas == nil {
  450. return
  451. }
  452. self.listView.exitCrop(with: self.listView.selectImageAreas)
  453. self.listView.cropAreas = nil
  454. self.listView.isEditImage = false
  455. self.updateButtonState(hidden: true)
  456. }
  457. @IBAction func restoreCutImageAction(_ sender: Any) {
  458. if self.listView.selectImageAreas == nil {
  459. return
  460. }
  461. self.listView.resetCrop(with: self.listView.selectImageAreas)
  462. }
  463. @IBAction func replaceImageAction(_ sender: NSButton) {
  464. if self.listView.selectImageAreas == nil {
  465. return
  466. }
  467. let panel = NSOpenPanel()
  468. panel.allowsMultipleSelection = false
  469. panel.allowedFileTypes = ["png","jpg"]
  470. panel.beginSheetModal(for: NSApp.mainWindow!) { response in
  471. if response == .OK {
  472. let openPath = panel.url?.path
  473. // let s = self.listView.extractImage(with: self.selectImageAreas, toImagePath: openPath!)
  474. let s = self.listView.replace(self.listView.selectImageAreas, imagePath: openPath!)
  475. if s {
  476. }
  477. }
  478. }
  479. }
  480. // @available(macOS 13.0, *)
  481. @IBAction func exportImageAction(_ sender: Any) {
  482. if self.listView.selectImageAreas == nil {
  483. return
  484. }
  485. if imagesAreas.count == 1 {
  486. let panel = NSSavePanel()
  487. panel.nameFieldStringValue = "\(NSLocalizedString("Untitled", comment: "")).jpg"
  488. // let button = NSButton.init(checkboxWithTitle: NSLocalizedString("Open the document after saving", comment: ""), target: nil, action: nil)
  489. // button.state = .on
  490. // panel.accessoryView = button
  491. panel.isExtensionHidden = true
  492. let response = panel.runModal()
  493. if response == .OK {
  494. let url = panel.url
  495. if FileManager.default.fileExists(atPath: url!.path) {
  496. try?FileManager.default.removeItem(atPath: url!.path)
  497. }
  498. let result = self.listView.extractImage(with: self.listView.selectImageAreas, toImagePath: url!.path)
  499. if result {
  500. // if button.state == .on { /// 开启文档
  501. // NSWorkspace.shared.openFile(url!.path)
  502. NSWorkspace.shared.activateFileViewerSelecting([url!])
  503. // } else {
  504. //
  505. // }
  506. }
  507. }
  508. } else if imagesAreas.count > 1 {
  509. let panel = NSOpenPanel()
  510. panel.canChooseFiles = false
  511. panel.canChooseDirectories = true
  512. panel.canCreateDirectories = true
  513. panel.allowsMultipleSelection = false
  514. panel.beginSheetModal(for: NSApp.mainWindow!) { response in
  515. if response == .OK {
  516. let outputURL = panel.url
  517. let s = self.listView.document.documentURL.lastPathComponent
  518. let folderPath = self.listView.document.documentURL.deletingPathExtension().lastPathComponent + "_extract"
  519. var filePath = outputURL?.path.stringByAppendingPathComponent(folderPath)
  520. var i = 1
  521. let testFilePath = filePath
  522. while FileManager.default.fileExists(atPath: filePath!) {
  523. filePath = testFilePath! + "\(i)"
  524. i = i + 1
  525. }
  526. try? FileManager.default.createDirectory(atPath: filePath!, withIntermediateDirectories: false, attributes: nil)
  527. var saveURLs : [URL] = []
  528. for j in 0 ... self.imagesAreas.count-1 {
  529. let documentFileName = self.listView.document.documentURL.deletingPathExtension().lastPathComponent
  530. var outPath = filePath!
  531. outPath = outPath.stringByAppendingPathComponent(documentFileName)
  532. outPath = outPath + "page \(j+1)"
  533. outPath = outPath.stringByAppendingPathExtension("jpg")
  534. let result = self.listView.extractImage(with: self.imagesAreas[j], toImagePath: outPath)
  535. if result {
  536. saveURLs.append(URL(fileURLWithPath: outPath))
  537. }
  538. }
  539. NSWorkspace.shared.activateFileViewerSelecting(saveURLs)
  540. }
  541. }
  542. }
  543. }
  544. }
  545. class CustomSliderCell: NSSliderCell {
  546. var knobImage: NSImage?
  547. override func drawKnob(_ knobRect: NSRect) {
  548. if let image = knobImage {
  549. let imageSize = NSSize(width: knobRect.size.height, height: knobRect.size.height)
  550. let imageRect = NSRect(origin: knobRect.origin, size: imageSize)
  551. image.draw(in: imageRect)
  552. } else {
  553. super.drawKnob(knobRect)
  554. }
  555. }
  556. }