KMEditImagePropertyViewController.swift 60 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301
  1. //
  2. // KMEditImagePropertyViewController.swift
  3. // PDF Reader Pro
  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 backgroundView: NSView!
  25. @IBOutlet weak var titleLabel: NSTextField!
  26. @IBOutlet weak var headerBox: NSBox!
  27. @IBOutlet weak var headerBoxHeight: NSLayoutConstraint!
  28. @IBOutlet weak var whTitleLabel: NSTextField!
  29. @IBOutlet weak var wTextFieldItemView: KMCustomTextField!
  30. @IBOutlet weak var whBox: NSBox!
  31. @IBOutlet weak var hTextFieldItemView: KMCustomTextField!
  32. var whVC: KMDesignButton?
  33. private var whState_: KMDesignTokenState = .None
  34. @IBOutlet weak var rotateTitleLabel: NSTextField!
  35. @IBOutlet weak var rotateTextFieldItemView: KMDesignTextField!
  36. @IBOutlet weak var leftRotateBox: NSBox!
  37. @IBOutlet weak var rightRotateBox: NSBox!
  38. var leftRotateVC: KMDesignButton?
  39. var rightRotateVC: KMDesignButton?
  40. @IBOutlet weak var opacityTitleLabel: NSTextField!
  41. @IBOutlet weak var opacitySlider: NSSlider!
  42. @IBOutlet weak var opacityBox: NSBox!
  43. @IBOutlet weak var opacityBoxBottomMaginConstraint: NSLayoutConstraint!
  44. @IBOutlet weak var opacityBoxTopConstraint: NSLayoutConstraint!
  45. @IBOutlet weak var turnOverBox: NSBox!
  46. @IBOutlet weak var turnOverTitleLabel: NSTextField!
  47. @IBOutlet weak var reverseXBox: NSView!
  48. @IBOutlet weak var reverseYBox: NSView!
  49. var reverseXVC: KMDesignButton?
  50. var reverseYVC: KMDesignButton?
  51. @IBOutlet weak var cropBox: NSBox!
  52. @IBOutlet weak var confirmBox: NSBox!
  53. @IBOutlet weak var cancelBox: NSBox!
  54. @IBOutlet weak var replaceBox: NSBox!
  55. @IBOutlet weak var exportBox: NSBox!
  56. @IBOutlet weak var opacityBoxHeight: NSLayoutConstraint!
  57. @IBOutlet weak var buttonBoxHeight: NSLayoutConstraint!
  58. @IBOutlet weak var buttonBox: NSBox!
  59. @IBOutlet weak var alignmentView: KMEditPropertyAlignmentView!
  60. @IBOutlet weak var alignmentViewTopConstraint: NSLayoutConstraint!
  61. //NO Image
  62. @IBOutlet weak var noImageView: NSImageView!
  63. @IBOutlet weak var noTitleLabel: NSTextField!
  64. @IBOutlet weak var noSubtitleLabel: NSTextField!
  65. @IBOutlet weak var noBackgroundView: NSView!
  66. var rotateLeftVC: KMDesignPropertySelector?
  67. var rotateRightVC: KMDesignPropertySelector?
  68. var flipHorizontalVC: KMDesignPropertySelector?
  69. var flipVerticalVC: KMDesignPropertySelector?
  70. var opacityVC: KMDesignSelect?
  71. @IBOutlet weak var toolsTitleLabel: NSTextField!
  72. var cropVC: KMDesignButton?
  73. var confirmVC: KMDesignButton?
  74. var cancelVC: KMDesignButton?
  75. var replaceVC: KMDesignButton?
  76. var exportVC: KMDesignButton?
  77. var imagesAreas : [CPDFEditImageArea] = []
  78. var listView : CPDFListView!
  79. var editingAreas: [Any] {
  80. get {
  81. return self.listView.editingAreas() ?? []
  82. }
  83. }
  84. weak var delegate: KMEditImagePropertyViewControllerDelegate?
  85. weak var handdler: KMEditPDfHanddler?
  86. @objc func changeEffectiveAppearance() {
  87. print("changeEffectiveAppearance")
  88. let isDarkModel = NSView.isDarkModel()
  89. if isDarkModel {
  90. self.view.appearance = NSAppearance(named: .darkAqua)
  91. } else {
  92. self.view.appearance = NSAppearance(named: .aqua)
  93. }
  94. self.updateUI()
  95. }
  96. // func updateUI() {
  97. // let isDarkModel = NSView.isDarkModel()
  98. // if isDarkModel {
  99. // self.backgroundView.backgroundColor(NSColor.km_init(hex: "#252526"))
  100. // self.noBackgroundView.backgroundColor(NSColor.km_init(hex: "#252526"))
  101. // } else {
  102. // self.backgroundView.backgroundColor(NSColor.km_init(hex: "#FAFAFA"))
  103. // self.noBackgroundView.backgroundColor(NSColor.km_init(hex: "#F7F8FA"))
  104. // }
  105. // }
  106. static let keepProportionsKey = "KMEditPDFKeepProportionsKey"
  107. deinit {
  108. KMPrint("KMEditImagePropertyViewController deinit.")
  109. self.removeNotification()
  110. }
  111. override func viewDidLoad() {
  112. super.viewDidLoad()
  113. self.setup()
  114. self.initData()
  115. self.updateLanguage()
  116. self.updateButtonState(hidden: true)
  117. self.reloadData()
  118. self.addNotification()
  119. self.updateUI()
  120. }
  121. func initData() {
  122. self.updateButtonState(hidden: true)
  123. }
  124. func setup() {
  125. self.titleLabel.font = NSFont.SFProTextSemiboldFont(14.0)
  126. self.titleLabel.textColor = KMAppearance.Layout.h0Color()
  127. self.opacityTitleLabel.font = NSFont.SFProTextRegularFont(12.0)
  128. // self.opacityTitleLabel.textColor = KMAppearance.Layout.h0Color()
  129. self.whTitleLabel.font = NSFont.SFProTextRegularFont(12)
  130. self.whTitleLabel.stringValue = NSLocalizedString("Size", comment: "")
  131. self.wTextFieldItemView.backgroundView.wantsLayer = true
  132. self.wTextFieldItemView.backgroundView.layer?.backgroundColor = .white
  133. self.wTextFieldItemView.backgroundView.layer?.borderWidth = 1
  134. self.wTextFieldItemView.backgroundView.layer?.borderColor = NSColor.km_init(hex: "#DFE1E5").cgColor
  135. self.wTextFieldItemView.backgroundView.layer?.cornerRadius = 4
  136. self.wTextFieldItemView.delegate = self
  137. self.wTextFieldItemView.offset = 8
  138. self.wTextFieldItemView.rightViewMode = .always
  139. let wRightView = NSView()
  140. wRightView.frame = NSMakeRect(0, 0, 24, 32)
  141. let wIv = NSImageView()
  142. wIv.frame = NSMakeRect(0, 8, 16, 16)
  143. wIv.image = NSImage(named: "KMImageNameEditPDFWidthIcon")
  144. wRightView.addSubview(wIv)
  145. self.wTextFieldItemView.rightView = wRightView
  146. var formatter = NumberFormatter()
  147. formatter.numberStyle = .decimal
  148. // formatter.maximum = NSNumber(value: 999)
  149. // formatter.minimum = NSNumber(value: 1)
  150. self.wTextFieldItemView.textFiled.formatter = formatter
  151. self.hTextFieldItemView.backgroundView.wantsLayer = true
  152. self.hTextFieldItemView.backgroundView.layer?.backgroundColor = .white
  153. self.hTextFieldItemView.backgroundView.layer?.borderWidth = 1
  154. self.hTextFieldItemView.backgroundView.layer?.borderColor = NSColor.km_init(hex: "#DFE1E5").cgColor
  155. self.hTextFieldItemView.backgroundView.layer?.cornerRadius = 4
  156. self.hTextFieldItemView.delegate = self
  157. self.hTextFieldItemView.textFiled.formatter = formatter
  158. let hRightView = NSView()
  159. hRightView.frame = NSMakeRect(0, 0, 24, 32)
  160. let hIv = NSImageView()
  161. hIv.frame = NSMakeRect(0, 8, 16, 16)
  162. hIv.image = NSImage(named: "KMImageNameEditPDFHeightIcon")
  163. hRightView.addSubview(hIv)
  164. self.hTextFieldItemView.rightView = hRightView
  165. self.hTextFieldItemView.rightViewMode = .always
  166. self.alignmentView.didChange = {[unowned self] view, areasArray, boundsArray in
  167. self._trackEvent()
  168. let textAreas = self.handdler?.editingTextAreas ?? []
  169. let imageAreas = self.handdler?.editingImageAreas ?? []
  170. if textAreas.count > 0 && imageAreas.count > 0 {
  171. self._trackAlignEvent()
  172. }
  173. self.changeAreasAlign(areas: areasArray, newBounds: boundsArray)
  174. }
  175. self.whVC = KMDesignButton(withType: .Image)
  176. self.whBox.contentView = self.whVC?.view
  177. self.whBox.borderWidth = 0
  178. self.whVC?.target = self
  179. self.whVC?.action = #selector(whAction)
  180. self.whVC?.image = NSImage(named: "KMImageNameEditPDFWhExchange")!
  181. self.whVC?.image_act = NSImage(named: "KMImageNameEditPDFWhExchangeSelected")!
  182. self.whVC?.pagination()
  183. whVC?.cornerRadius = 4
  184. whVC?.cornerRadius_hov = 4
  185. whVC?.cornerRadius_disabled = 4
  186. self.whVC?.toolTip = NSLocalizedString("Keep Proportions", comment: "")
  187. // self.whVC?.state = self._getKeepProportionsButtonState() ? .Act : .Norm
  188. let whState: KMDesignTokenState = self._getKeepProportionsButtonState() ? .Act : .Norm
  189. self.whVC?.state = .Norm
  190. self._setWHButtonState(whState)
  191. self.rotateTitleLabel.stringValue = NSLocalizedString("Rotate", comment: "")
  192. self.rotateTitleLabel.font = NSFont.SFProTextRegularFont(12)
  193. self.rotateTextFieldItemView.delegate = self
  194. self.rotateTextFieldItemView.enterKeyEndEdit = true
  195. self.rotateTextFieldItemView.useToken = false
  196. self.leftRotateVC = KMDesignButton(withType: .Image)
  197. self.leftRotateBox.contentView = self.leftRotateVC?.view
  198. self.leftRotateBox.borderWidth = 0
  199. self.leftRotateVC?.target = self
  200. self.leftRotateVC?.action = #selector(leftRotationImageAction)
  201. self.leftRotateVC?.image = NSImage(named: "KMImageNameEditPDFRotationLeft")!
  202. self.leftRotateVC?.pagination()
  203. self.rightRotateVC = KMDesignButton(withType: .Image)
  204. self.rightRotateBox.contentView = self.rightRotateVC?.view
  205. self.rightRotateBox.borderWidth = 0
  206. self.rightRotateVC?.target = self
  207. self.rightRotateVC?.action = #selector(rightRotationImageAction)
  208. self.rightRotateVC?.image = NSImage(named: "KMImageNameEditPDFRotationRight")!
  209. self.rightRotateVC?.pagination()
  210. opacityVC = KMDesignSelect.init(withType: .PopButton)
  211. opacityBox.contentView = opacityVC?.view
  212. opacityBox.fillColor = KMAppearance.Layout.bgDrakColor()
  213. opacityVC?.addItems(withObjectValues: ["25%","50%","75%","100%"])
  214. opacityVC?.selectItem(at: 0)
  215. opacityVC?.delete = self
  216. opacityVC?.borderColor = KMAppearance.Interactive.s0Color()
  217. opacityVC?.borderColor_disabled = KMAppearance.Interactive.s0Color()
  218. opacityVC?.background = KMAppearance.Layout.l1Color()
  219. opacityVC?.background_hov = KMAppearance.Layout.l1Color()
  220. opacityVC?.background_focus = KMAppearance.Layout.l1Color()
  221. opacityVC?.background_disabled = KMAppearance.Layout.l1Color()
  222. opacityVC?.textColor = KMAppearance.Layout.h1Color()
  223. opacityVC?.textColor_hov = KMAppearance.Layout.h1Color()
  224. opacityVC?.textColor_focus = KMAppearance.Layout.h1Color()
  225. opacityVC?.popViewControllerBackground = KMAppearance.Layout.bgColor()
  226. opacityVC?.popViewControllerTextColor = KMAppearance.Layout.h0Color()
  227. opacityVC?.popViewControllerEnterFillColor = KMAppearance.Interactive.s0Color()
  228. opacityVC?.updateUI()
  229. let customCell = CustomSliderCell()
  230. customCell.knobImage = NSImage(named: "KMImageNameEditPDFSlider")
  231. opacitySlider.cell = customCell
  232. opacitySlider.target = self
  233. opacitySlider.action = #selector(sliderValueChanged(_:))
  234. opacitySlider.isContinuous = false
  235. self.turnOverTitleLabel.stringValue = NSLocalizedString("Flip", comment: "")
  236. self.reverseXVC = KMDesignButton(withType: .Image)
  237. self.reverseXBox.addSubview(self.reverseXVC!.view)
  238. self.reverseXVC?.view.frame = self.reverseXBox.bounds
  239. self.reverseXVC?.view.autoresizingMask = [.width, .height]
  240. self.reverseXVC?.target = self
  241. self.reverseXVC?.action = #selector(flipHorizontalImageAction)
  242. self.reverseXVC?.image = NSImage(named: "KMImageNameEditPDFReverseX")!
  243. self.reverseXVC?.pagination()
  244. self.reverseYVC = KMDesignButton(withType: .Image)
  245. self.reverseYBox.addSubview(self.reverseYVC!.view)
  246. self.reverseYVC?.view.frame = self.reverseXBox.bounds
  247. self.reverseYVC?.view.autoresizingMask = [.width, .height]
  248. self.reverseYVC?.target = self
  249. self.reverseYVC?.action = #selector(flipVerticalImageAction)
  250. self.reverseYVC?.image = NSImage(named: "KMImageNameEditPDFReverseY")!
  251. self.reverseYVC?.pagination()
  252. self.toolsTitleLabel.stringValue = NSLocalizedString("Tools", comment: "")
  253. self.toolsTitleLabel.font = NSFont.SFProTextRegularFont(12)
  254. cropVC = KMDesignButton.init(withType: .Text)
  255. cropBox.contentView = cropVC?.view
  256. cropBox.fillColor = NSColor.clear
  257. cropVC?.target = self
  258. cropVC?.action = #selector(cutImageAction)
  259. cropVC?.button(type: .Sec, size: .m)
  260. cropVC?.stringValue = NSLocalizedString("Crop", comment: "")
  261. // cropVC?.borderColor = KMAppearance.Interactive.s0Color()
  262. // cropVC?.background = KMAppearance.Layout.l1Color()
  263. // cropVC?.background_hov = KMAppearance.Layout.l1Color()
  264. // cropVC?.background_focus = KMAppearance.Layout.l1Color()
  265. // cropVC?.textColor = KMAppearance.Layout.h1Color()
  266. // cropVC?.textColor_hov = KMAppearance.Layout.h1Color()
  267. // cropVC?.textColor_focus = KMAppearance.Layout.h1Color()
  268. // cropVC?.updateUI()
  269. cropVC?.borderColor = KMAppearance.Interactive.s0Color()
  270. cropVC?.borderColor_hov = KMAppearance.Interactive.s0Color()
  271. cropVC?.borderColor_disabled = KMAppearance.Interactive.s0Color()
  272. cropVC?.background = KMAppearance.Layout.l1Color()
  273. cropVC?.background_hov = KMAppearance.Layout.l1Color()
  274. cropVC?.background_focus = KMAppearance.Layout.l1Color()
  275. cropVC?.background_disabled = KMAppearance.Layout.l1Color()
  276. cropVC?.textColor = KMAppearance.Layout.h1Color()
  277. cropVC?.textColor_hov = KMAppearance.Layout.h1Color()
  278. cropVC?.textColor_focus = KMAppearance.Layout.h1Color()
  279. cropVC?.updateUI()
  280. confirmVC = KMDesignButton.init(withType: .Text)
  281. confirmBox.contentView = confirmVC?.view
  282. confirmBox.fillColor = NSColor.clear
  283. confirmVC?.target = self
  284. confirmVC?.action = #selector(confirmVCImageAction)
  285. confirmVC?.button(type: .Cta, size: .m)
  286. confirmVC?.stringValue = NSLocalizedString("Confirm", comment: "")
  287. confirmVC?.updateUI()
  288. cancelVC = KMDesignButton.init(withType: .Text)
  289. cancelBox.contentView = cancelVC?.view
  290. cancelBox.fillColor = NSColor.clear
  291. cancelVC?.target = self
  292. cancelVC?.action = #selector(cancelCutImageAction)
  293. cancelVC?.button(type: .Sec, size: .m)
  294. cancelVC?.stringValue = NSLocalizedString("Cancel", comment: "")
  295. cancelVC?.borderColor = KMAppearance.Interactive.s0Color()
  296. cancelVC?.background = KMAppearance.Layout.l1Color()
  297. cancelVC?.background_hov = KMAppearance.Layout.l1Color()
  298. cancelVC?.background_focus = KMAppearance.Layout.l1Color()
  299. cancelVC?.textColor = KMAppearance.Layout.h1Color()
  300. cancelVC?.textColor_hov = KMAppearance.Layout.h1Color()
  301. cancelVC?.textColor_focus = KMAppearance.Layout.h1Color()
  302. cancelVC?.updateUI()
  303. replaceVC = KMDesignButton.init(withType: .Text)
  304. replaceBox.contentView = replaceVC?.view
  305. replaceBox.fillColor = NSColor.clear
  306. replaceVC?.target = self
  307. replaceVC?.action = #selector(replaceImageAction)
  308. replaceVC?.button(type: .Sec, size: .m)
  309. replaceVC?.stringValue = NSLocalizedString("Replace", comment: "")
  310. replaceVC?.borderColor = KMAppearance.Interactive.s0Color()
  311. replaceVC?.borderColor_hov = KMAppearance.Interactive.s0Color()
  312. replaceVC?.borderColor_disabled = KMAppearance.Interactive.s0Color()
  313. replaceVC?.background = KMAppearance.Layout.l1Color()
  314. replaceVC?.background_hov = KMAppearance.Layout.l1Color()
  315. replaceVC?.background_focus = KMAppearance.Layout.l1Color()
  316. replaceVC?.background_disabled = KMAppearance.Layout.l1Color()
  317. replaceVC?.textColor = KMAppearance.Layout.h1Color()
  318. replaceVC?.textColor_hov = KMAppearance.Layout.h1Color()
  319. replaceVC?.textColor_focus = KMAppearance.Layout.h1Color()
  320. replaceVC?.updateUI()
  321. exportVC = KMDesignButton.init(withType: .Text)
  322. exportBox.contentView = exportVC?.view
  323. exportBox.fillColor = NSColor.clear
  324. exportVC?.target = self
  325. exportVC?.action = #selector(exportImageAction)
  326. exportVC?.button(type: .Sec, size: .m)
  327. exportVC?.stringValue = NSLocalizedString("Export", comment: "")
  328. exportVC?.borderColor = KMAppearance.Interactive.s0Color()
  329. exportVC?.borderColor_hov = KMAppearance.Interactive.s0Color()
  330. exportVC?.borderColor_disabled = KMAppearance.Interactive.s0Color()
  331. exportVC?.background = KMAppearance.Layout.l1Color()
  332. exportVC?.background_hov = KMAppearance.Layout.l1Color()
  333. exportVC?.background_focus = KMAppearance.Layout.l1Color()
  334. exportVC?.background_disabled = KMAppearance.Layout.l1Color()
  335. exportVC?.textColor = KMAppearance.Layout.h1Color()
  336. exportVC?.textColor_hov = KMAppearance.Layout.h1Color()
  337. exportVC?.textColor_focus = KMAppearance.Layout.h1Color()
  338. exportVC?.updateUI()
  339. self.noTitleLabel.textColor = KMAppearance.Layout.h1Color()
  340. self.noTitleLabel.font = NSFont.SFProTextRegularFont(14)
  341. self.noImageView.image = NSImage(named: "No_addimage")
  342. self.noImageView.alphaValue = 0.4
  343. let string = self.noSubtitleLabel.stringValue
  344. let paragraphStyle = NSMutableParagraphStyle()
  345. paragraphStyle.lineHeightMultiple = 1.32
  346. paragraphStyle.alignment = .center
  347. self.noSubtitleLabel.attributedStringValue = NSMutableAttributedString(string: NSLocalizedString(string, comment: ""), attributes: [NSAttributedString.Key.paragraphStyle: paragraphStyle, .foregroundColor : NSColor.km_init(hex: "#94989C"), NSAttributedString.Key.font: NSFont.SFProTextRegularFont(12.0)])
  348. self.noBackgroundView.backgroundColor(NSColor.km_init(hex: "#F7F8FA"))
  349. }
  350. func addNotification() {
  351. NotificationCenter.default.addObserver(self, selector: #selector(changeEffectiveAppearance), name: NSNotification.Name(rawValue: "kEffectiveAppearance"), object: nil)
  352. }
  353. func removeNotification() {
  354. NotificationCenter.default.removeObserver(self)
  355. DistributedNotificationCenter.default().removeObserver(self)
  356. }
  357. // @objc func changeEffectiveAppearance() {
  358. // let isDarkModel = KMAdvertisementConfig.isDarkModel()
  359. // if isDarkModel {
  360. // self.view.appearance = NSAppearance(named: .darkAqua)
  361. // } else {
  362. // self.view.appearance = NSAppearance(named: .aqua)
  363. // }
  364. // self.updateUI()
  365. // }
  366. func updateUI() {
  367. let isDarkModel = NSView.isDarkModel()
  368. if isDarkModel {
  369. self.backgroundView.backgroundColor(NSColor.km_init(hex: "#252526"))
  370. self.noBackgroundView.backgroundColor(NSColor.km_init(hex: "#252526"))
  371. self.whTitleLabel.textColor = NSColor.km_init(hex: "#C8C9CC")
  372. self.rotateTitleLabel.textColor = NSColor.km_init(hex: "#C8C9CC")
  373. self.opacityTitleLabel.textColor = NSColor.km_init(hex: "#C8C9CC")
  374. self.toolsTitleLabel.textColor = NSColor.km_init(hex: "#C8C9CC")
  375. self.alignmentView.alignmentLabel.textColor = NSColor.km_init(hex: "#C8C9CC")
  376. whVC?.background_hov = NSColor(red: 71/255, green: 72/255, blue: 75/255, alpha: 1)
  377. // whVC?.background_focus = KMAppearance.Layout.l1Color()
  378. whVC?.background_disabled = KMAppearance.buttonDisabledColor()
  379. leftRotateVC?.borderColor = KMAppearance.Interactive.s0Color()
  380. leftRotateVC?.background = KMAppearance.Layout.l1Color()
  381. leftRotateVC?.background_hov = NSColor(red: 71/255, green: 72/255, blue: 75/255, alpha: 1)
  382. leftRotateVC?.background_focus = KMAppearance.Layout.l1Color()
  383. leftRotateVC?.textColor = KMAppearance.Layout.h1Color()
  384. leftRotateVC?.textColor_hov = KMAppearance.Layout.h1Color()
  385. leftRotateVC?.textColor_focus = KMAppearance.Layout.h1Color()
  386. leftRotateVC?.borderColor_hov = KMAppearance.Interactive.s0Color()
  387. leftRotateVC?.background_disabled = NSColor(white: 96/255.0, alpha: 1.0)
  388. // cropVC?.borderColor = KMAppearance.Interactive.s0Color()
  389. // cropVC?.borderColor_hov = KMAppearance.Interactive.s0Color()
  390. // leftRotateVC?.borderColor_disabled = KMAppearance.Interactive.s0Color()
  391. // leftRotateVC?.background = KMAppearance.Layout.l1Color()
  392. // leftRotateVC?.background_hov = KMAppearance.Layout.l1Color()
  393. // leftRotateVC?.background_focus = KMAppearance.Layout.l1Color()
  394. // leftRotateVC?.background_disabled = KMAppearance.Layout.l1Color()
  395. // cropVC?.textColor = KMAppearance.Layout.h1Color()
  396. // cropVC?.textColor_hov = KMAppearance.Layout.h1Color()
  397. // cropVC?.textColor_focus = KMAppearance.Layout.h1Color()
  398. leftRotateVC?.updateUI()
  399. rightRotateVC?.borderColor = KMAppearance.Interactive.s0Color()
  400. rightRotateVC?.background = KMAppearance.Layout.l1Color()
  401. rightRotateVC?.background_hov = NSColor(red: 71/255, green: 72/255, blue: 75/255, alpha: 1)
  402. rightRotateVC?.background_focus = KMAppearance.Layout.l1Color()
  403. rightRotateVC?.textColor = KMAppearance.Layout.h1Color()
  404. rightRotateVC?.textColor_hov = KMAppearance.Layout.h1Color()
  405. rightRotateVC?.textColor_focus = KMAppearance.Layout.h1Color()
  406. rightRotateVC?.borderColor_hov = KMAppearance.Interactive.s0Color()
  407. rightRotateVC?.background_disabled = NSColor(white: 96/255.0, alpha: 1.0)
  408. rightRotateVC?.updateUI()
  409. reverseXVC?.borderColor = KMAppearance.Interactive.s0Color()
  410. reverseXVC?.background = KMAppearance.Layout.l1Color()
  411. reverseXVC?.background_hov = NSColor(red: 71/255, green: 72/255, blue: 75/255, alpha: 1)
  412. reverseXVC?.background_focus = KMAppearance.Layout.l1Color()
  413. reverseXVC?.textColor = KMAppearance.Layout.h1Color()
  414. reverseXVC?.textColor_hov = KMAppearance.Layout.h1Color()
  415. reverseXVC?.textColor_focus = KMAppearance.Interactive.a0Color()
  416. reverseXVC?.borderColor_hov = KMAppearance.Interactive.s0Color()
  417. reverseXVC?.background_disabled = NSColor(white: 96/255.0, alpha: 1.0)
  418. reverseXVC?.updateUI()
  419. reverseYVC?.borderColor = KMAppearance.Interactive.s0Color()
  420. reverseYVC?.background = KMAppearance.Layout.l1Color()
  421. reverseYVC?.background_hov = NSColor(red: 71/255, green: 72/255, blue: 75/255, alpha: 1)
  422. reverseYVC?.background_focus = KMAppearance.Layout.l1Color()
  423. reverseYVC?.textColor = KMAppearance.Layout.h1Color()
  424. reverseYVC?.textColor_hov = KMAppearance.Layout.h1Color()
  425. reverseYVC?.textColor_focus = KMAppearance.Interactive.a0Color()
  426. reverseYVC?.borderColor_hov = KMAppearance.Interactive.s0Color()
  427. reverseYVC?.background_disabled = NSColor(white: 96/255.0, alpha: 1.0)
  428. reverseYVC?.updateUI()
  429. self.wTextFieldItemView.backgroundView.layer?.backgroundColor = KMAppearance.Layout.w15Color().cgColor
  430. self.wTextFieldItemView.backgroundView.layer?.borderColor = KMAppearance.Layout.w15Color().cgColor
  431. self.hTextFieldItemView.backgroundView.layer?.backgroundColor = KMAppearance.Layout.w15Color().cgColor
  432. self.hTextFieldItemView.backgroundView.layer?.borderColor = KMAppearance.Layout.w15Color().cgColor
  433. self.rotateTextFieldItemView.fillColor = KMAppearance.Layout.w15Color()
  434. self.rotateTextFieldItemView.textF?.backgroundView.layer?.backgroundColor = KMAppearance.Layout.w15Color().cgColor
  435. // self.rotateTextFieldItemView.updateUI(state: self.rotateTextFieldItemView.state)
  436. if self.rotateTextFieldItemView.state == .Focus {
  437. let borderColor = NSColor.km_init(hex: "68ACF8")
  438. self.rotateTextFieldItemView.textF?.backgroundView.layer?.borderColor = borderColor.cgColor
  439. } else {
  440. let borderColor = NSColor.km_init(hex: "56585A")
  441. self.rotateTextFieldItemView.textF?.backgroundView.layer?.borderColor = borderColor.cgColor
  442. }
  443. } else {
  444. self.backgroundView.backgroundColor(NSColor.km_init(hex: "#FAFAFA"))
  445. self.noBackgroundView.backgroundColor(NSColor.km_init(hex: "#F7F8FA"))
  446. self.whTitleLabel.textColor = NSColor.km_init(hex: "#616469")
  447. self.rotateTitleLabel.textColor = NSColor.km_init(hex: "#616469")
  448. self.opacityTitleLabel.textColor = NSColor.km_init(hex: "#616469")
  449. self.toolsTitleLabel.textColor = NSColor.km_init(hex: "#616469")
  450. self.alignmentView.alignmentLabel.textColor = NSColor.km_init(hex: "#616469")
  451. leftRotateVC?.borderColor = KMAppearance.Interactive.s0Color()
  452. leftRotateVC?.background = KMAppearance.Layout.l1Color()
  453. leftRotateVC?.background_hov = KMAppearance.view_bg_dis_color()
  454. leftRotateVC?.background_focus = KMAppearance.Layout.l1Color()
  455. leftRotateVC?.textColor = KMAppearance.Layout.h1Color()
  456. leftRotateVC?.textColor_hov = KMAppearance.Layout.h1Color()
  457. leftRotateVC?.textColor_focus = KMAppearance.Layout.h1Color()
  458. leftRotateVC?.borderColor_hov = KMAppearance.Interactive.s0Color()
  459. leftRotateVC?.borderColor_disabled = KMAppearance.Interactive.s0Color()
  460. leftRotateVC?.background = KMAppearance.Layout.l1Color()
  461. leftRotateVC?.background_hov = KMAppearance.Layout.l1Color()
  462. leftRotateVC?.background_focus = KMAppearance.Layout.l1Color()
  463. leftRotateVC?.background_disabled = KMAppearance.Layout.l1Color()
  464. leftRotateVC?.updateUI()
  465. whVC?.background_hov = KMAppearance.view_bg_dis_color()
  466. // whVC?.background_focus = KMAppearance.Layout.l1Color()
  467. whVC?.background_disabled = KMAppearance.buttonDisabledColor()
  468. reverseXVC?.borderColor = KMAppearance.Interactive.s0Color()
  469. reverseXVC?.background = KMAppearance.Layout.l1Color()
  470. reverseXVC?.background_hov = KMAppearance.view_bg_dis_color()
  471. reverseXVC?.background_focus = KMAppearance.Layout.l1Color()
  472. reverseXVC?.textColor = KMAppearance.Layout.h1Color()
  473. reverseXVC?.textColor_hov = KMAppearance.Layout.h1Color()
  474. reverseXVC?.textColor_focus = KMAppearance.Layout.h1Color()
  475. reverseXVC?.borderColor_hov = KMAppearance.Interactive.s0Color()
  476. reverseXVC?.updateUI()
  477. reverseYVC?.borderColor = KMAppearance.Interactive.s0Color()
  478. reverseYVC?.background = KMAppearance.Layout.l1Color()
  479. reverseYVC?.background_hov = KMAppearance.view_bg_dis_color()
  480. reverseYVC?.background_focus = KMAppearance.Layout.l1Color()
  481. reverseYVC?.textColor = KMAppearance.Layout.h1Color()
  482. reverseYVC?.textColor_hov = KMAppearance.Layout.h1Color()
  483. reverseYVC?.textColor_focus = KMAppearance.Layout.h1Color()
  484. reverseYVC?.borderColor_hov = KMAppearance.Interactive.s0Color()
  485. reverseYVC?.updateUI()
  486. self.wTextFieldItemView.backgroundView.layer?.backgroundColor = .white
  487. self.wTextFieldItemView.backgroundView.layer?.borderColor = NSColor.km_init(hex: "#DFE1E5").cgColor
  488. self.hTextFieldItemView.backgroundView.layer?.backgroundColor = .white
  489. self.hTextFieldItemView.backgroundView.layer?.borderColor = NSColor.km_init(hex: "#DFE1E5").cgColor
  490. self.rightRotateVC?.initDefaultValue()
  491. self.leftRotateVC?.initDefaultValue()
  492. self.rotateTextFieldItemView.fillColor = .white
  493. // self.rotateTextFieldItemView.updateUI(state: self.rotateTextFieldItemView.state)
  494. self.rotateTextFieldItemView.textF?.backgroundView.layer?.backgroundColor = .white
  495. if self.rotateTextFieldItemView.state == .Focus {
  496. let borderColor = NSColor.km_init(hex: "68ACF8")
  497. self.rotateTextFieldItemView.textF?.backgroundView.layer?.borderColor = borderColor.cgColor
  498. } else {
  499. let borderColor = NSColor.km_init(hex: "DFE1E5")
  500. self.rotateTextFieldItemView.textF?.backgroundView.layer?.borderColor = borderColor.cgColor
  501. }
  502. }
  503. // for label in [self.whTitleLabel, self.opacityTitleLabel,
  504. // self.turnOverTitleLabel] {
  505. // label?.font = NSFont.SFProTextSemiboldFont(12.0)
  506. // label?.textColor = NSColor.km_init(hex: "#616469")
  507. // }
  508. }
  509. func updateLanguage() {
  510. let areas = self.editingAreas
  511. if imagesAreas.count > 0 && imagesAreas.count != areas.count { //多选图片跟文字
  512. self.titleLabel.stringValue = NSLocalizedString("General Properties", comment: "")
  513. } else {
  514. self.titleLabel.stringValue = NSLocalizedString("Image", comment: "")
  515. }
  516. self.noTitleLabel.stringValue = NSLocalizedString("Add Image", comment: "")
  517. self.opacityTitleLabel.stringValue = NSLocalizedString("Opacity", comment: "")
  518. self.cropVC?.stringValue = NSLocalizedString("Crop", comment: "")
  519. self.confirmVC?.stringValue = NSLocalizedString("Confirm", comment: "")
  520. self.cancelVC?.stringValue = NSLocalizedString("Cancel", comment: "")
  521. self.replaceVC?.stringValue = NSLocalizedString("Replace", comment: "")
  522. self.exportVC?.stringValue = NSLocalizedString("Export", comment: "")
  523. }
  524. func reloadData() {
  525. imagesAreas = []
  526. if self.editingAreas.count > 0 {
  527. self.noBackgroundView.isHidden = true
  528. let areas = self.editingAreas
  529. self.alignmentView.editingAreas = areas
  530. for i in 0 ... areas.count-1 {
  531. if areas[i] is CPDFEditImageArea {
  532. imagesAreas.append(areas[i] as! CPDFEditImageArea)
  533. }
  534. }
  535. if imagesAreas.count == 1 && imagesAreas.count == areas.count{ //单个图片
  536. self.listView.selectImageAreas = imagesAreas.first
  537. self.headerBox.isHidden = false
  538. self.headerBoxHeight.constant = 176
  539. self.opacityBox.isHidden = false
  540. self.opacityBoxHeight.constant = 56
  541. self.opacityBoxBottomMaginConstraint.constant = 66
  542. self.opacityBoxTopConstraint.constant = 16
  543. self.buttonBox.isHidden = false
  544. self.buttonBoxHeight.constant = 112+16+8
  545. self.alignmentViewTopConstraint.constant = 16
  546. let opacity: CGFloat = self.listView.opacity(for: imagesAreas.first)
  547. self.updateImageAreasOpacity(opacity: opacity, state: .ended, needListView: false)
  548. self.turnOverBox.isHidden = false
  549. self.opacityTitleLabel.isHidden = false
  550. // self.whVC?.state = self._getKeepProportionsButtonState() ? .Act : .Norm
  551. let whSta: KMDesignTokenState = self._getKeepProportionsButtonState() ? .Act : .Norm
  552. self.whVC?.state = .Norm
  553. self._setWHButtonState(whSta)
  554. self.cropVC?.state = .Norm
  555. let isCropMode = self.listView.isCropMode
  556. self.replaceVC?.state = isCropMode ? .Disabled : .Norm
  557. } else if imagesAreas.count > 1 && imagesAreas.count == areas.count { //多选图片
  558. self.headerBox.isHidden = false
  559. self.headerBoxHeight.constant = 176
  560. self.opacityBox.isHidden = false
  561. self.opacityBoxHeight.constant = 56
  562. self.opacityBoxBottomMaginConstraint.constant = 66
  563. self.opacityBoxTopConstraint.constant = 0
  564. self.buttonBox.isHidden = false
  565. self.buttonBoxHeight.constant = 112+16+8
  566. self.alignmentViewTopConstraint.constant = 16
  567. var opacity = self.listView.opacity(for: imagesAreas.first)
  568. for area in imagesAreas {
  569. let newOpacity = self.listView.opacity(for: area)
  570. if (opacity < newOpacity) {
  571. opacity = newOpacity
  572. }
  573. }
  574. self.updateImageAreasOpacity(opacity: opacity, state: .ended, needListView: false)
  575. self.turnOverBox.isHidden = false
  576. self.opacityTitleLabel.isHidden = false
  577. self._updateWHProportionButtonEnableState()
  578. self.cropVC?.state = .Disabled
  579. self.replaceVC?.state = .Disabled
  580. if let data = self.handdler?.editAreasOpacityIsEqual(), data {
  581. self.opacitySlider.isEnabled = true
  582. self.opacityVC?.state = .Norm
  583. } else {
  584. self.opacitySlider.isEnabled = true
  585. self.opacityVC?.state = .Norm
  586. self.opacitySlider.doubleValue = 0
  587. self.opacityVC?.stringValue = "--";
  588. }
  589. } else if imagesAreas.count > 0 && imagesAreas.count != areas.count { //多选图片跟文字
  590. self.opacityBoxBottomMaginConstraint.constant = 8
  591. self.alignmentViewTopConstraint.constant = 0
  592. self.opacityBoxTopConstraint.constant = 0
  593. self.headerBoxHeight.constant = 40
  594. self.opacityBox.isHidden = true
  595. self.opacityBoxHeight.constant = 0
  596. self.buttonBox.isHidden = true
  597. self.buttonBoxHeight.constant = 0
  598. self.turnOverBox.isHidden = true
  599. self.opacityTitleLabel.isHidden = true
  600. }
  601. self.updateLanguage()
  602. } else {
  603. self.noBackgroundView.isHidden = false
  604. }
  605. self.updateWHData()
  606. self.updateRotateData()
  607. }
  608. func updateButtonState(hidden: Bool) {
  609. if hidden {
  610. self.cancelBox.isHidden = true
  611. self.confirmBox.isHidden = true
  612. self.cropBox.isHidden = false
  613. self.replaceVC?.enabled = true
  614. self.replaceVC?.state = .Norm
  615. self.exportVC?.enabled = true
  616. self.opacitySlider.isEnabled = true
  617. self.opacityVC?.enabled = true
  618. self.rotateLeftVC?.button.isEnabled = true
  619. self.rotateLeftVC?.view.alphaValue = 1.0
  620. self.rotateRightVC?.button.isEnabled = true
  621. self.rotateRightVC?.view.alphaValue = 1.0
  622. self.flipVerticalVC?.button.isEnabled = true
  623. self.flipVerticalVC?.view.alphaValue = 1.0
  624. self.flipHorizontalVC?.button.isEnabled = true
  625. self.flipHorizontalVC?.view.alphaValue = 1.0
  626. self.wTextFieldItemView.textFiled.isEnabled = true
  627. self.hTextFieldItemView.textFiled.isEnabled = true
  628. // self.whVC?.state = self._getKeepProportionsButtonState() ? .Act : .Norm
  629. let whSta: KMDesignTokenState = self._getKeepProportionsButtonState() ? .Act : .Norm
  630. self.whVC?.state = .Norm
  631. self._setWHButtonState(whSta)
  632. self.rotateTextFieldItemView.state = .Norm
  633. self.rotateTextFieldItemView.kmEnabled = true
  634. self.leftRotateVC?.state = .Norm
  635. self.leftRotateVC?.button.isEnabled = true
  636. self.rightRotateVC?.state = .Norm
  637. self.leftRotateVC?.button.isEnabled = true
  638. self.reverseXVC?.state = .Norm
  639. self.reverseXVC?.button.isEnabled = true
  640. self.reverseYVC?.state = .Norm
  641. self.reverseYVC?.button.isEnabled = true
  642. self.leftRotateVC?.button.isEnabled = true
  643. self.rightRotateVC?.button.isEnabled = true
  644. self.reverseXVC?.button.isEnabled = true
  645. self.reverseYVC?.button.isEnabled = true
  646. } else {
  647. self.cancelBox.isHidden = false
  648. self.confirmBox.isHidden = false
  649. self.cropBox.isHidden = true
  650. self.replaceVC?.enabled = false
  651. self.replaceVC?.state = .Disabled
  652. self.exportVC?.enabled = false
  653. self.opacitySlider.isEnabled = false
  654. self.opacityVC?.enabled = false
  655. self.rotateLeftVC?.button.isEnabled = false
  656. self.rotateLeftVC?.view.alphaValue = 0.5
  657. self.rotateRightVC?.button.isEnabled = false
  658. self.rotateRightVC?.view.alphaValue = 0.5
  659. self.flipVerticalVC?.button.isEnabled = false
  660. self.flipVerticalVC?.view.alphaValue = 0.5
  661. self.flipHorizontalVC?.button.isEnabled = false
  662. self.flipHorizontalVC?.view.alphaValue = 0.5
  663. self.wTextFieldItemView.textFiled.isEnabled = false
  664. self.hTextFieldItemView.textFiled.isEnabled = false
  665. self.whVC?.state = .Disabled
  666. self._setWHButtonState(.Disabled)
  667. self.rotateTextFieldItemView.state = .Disabled
  668. self.rotateTextFieldItemView.kmEnabled = false
  669. self.leftRotateVC?.state = .Disabled
  670. self.leftRotateVC?.button.isEnabled = false
  671. self.rightRotateVC?.state = .Disabled
  672. self.leftRotateVC?.button.isEnabled = false
  673. self.reverseXVC?.state = .Disabled
  674. self.reverseXVC?.button.isEnabled = false
  675. self.reverseYVC?.state = .Disabled
  676. self.reverseYVC?.button.isEnabled = false
  677. self.leftRotateVC?.button.isEnabled = false
  678. self.rightRotateVC?.button.isEnabled = false
  679. self.reverseXVC?.button.isEnabled = false
  680. self.reverseYVC?.button.isEnabled = false
  681. }
  682. self.reloadData()
  683. if self.noBackgroundView.isHidden == false {
  684. self.opacityVC?.enabled = false
  685. }
  686. }
  687. func updateWHData() {
  688. if let data = self.handdler?.editAreasBoundsIsEqualForWidth(), data {
  689. let areaFrame = self.handdler?.editingAreas.first?.bounds ?? .zero
  690. self.wTextFieldItemView.stringValue = String(format: "%.1f", areaFrame.size.width)
  691. } else {
  692. self.wTextFieldItemView.stringValue = "--"
  693. }
  694. if let data = self.handdler?.editAreasBoundsIsEqualForHeight(), data {
  695. let areaFrame = self.handdler?.editingAreas.first?.bounds ?? .zero
  696. self.hTextFieldItemView.stringValue = String(format: "%.1f", areaFrame.size.height)
  697. } else {
  698. self.hTextFieldItemView.stringValue = "--"
  699. }
  700. }
  701. func updateRotateData() {
  702. if let data = self.handdler?.editAreasRotateIsEqual(), data {
  703. let area = self.handdler?.editingImageAreas.first
  704. let rotate = self.listView.getRotateWith(area)
  705. let stringValue = self.rotateTextFieldItemView.stringValue?.replacingOccurrences(of: "°", with: "")
  706. let number = NSNumber(value: rotate)
  707. let formatter = NumberFormatter()
  708. let string = formatter.string(for: number)
  709. self.rotateTextFieldItemView.stringValue = "\(string ?? "0") °"
  710. } else {
  711. self.rotateTextFieldItemView.stringValue = "--"
  712. }
  713. }
  714. func updateImageAreasOpacity(opacity: CGFloat, state: KMOperationState, needListView: Bool = true) {
  715. if self.editingAreas.count >= 1 {
  716. if needListView {
  717. for area in imagesAreas {
  718. self.listView.setOpacityEditArea(area, opacity: opacity)
  719. }
  720. }
  721. // self.listView.editingAreas()!.count == 1 &&
  722. if (self.listView.editingAreas()?.first is CPDFEditImageArea) {
  723. self.listView.selectImageAreas = self.listView.editingAreas()!.first as? CPDFEditImageArea
  724. // self.editImageView.image = self.listView.selectImageAreas.thumbnailImage
  725. self.opacitySlider.objectValue = self.listView.opacity(for: self.listView.selectImageAreas)
  726. self.opacityVC?.stringValue = Int(opacity * 100).description + "%"
  727. // self.editImageView.alphaValue = opacity
  728. }
  729. }
  730. }
  731. // MARK: - Private Methods
  732. private func changeAreasAlign(areas:[Any],newBounds:[String]) {
  733. var oldBounds : [String] = []
  734. for i in 0 ... areas.count-1 {
  735. let area : CPDFEditArea = areas[i] as! CPDFEditArea
  736. oldBounds.append(NSStringFromRect(area.bounds))
  737. self.listView.setBoundsEditArea(area, withBounds: NSRectFromString(newBounds[i]))
  738. }
  739. self.listView.setNeedsDisplayForVisiblePages()
  740. }
  741. private func _setKeepProportionsButtonState(state: Bool) {
  742. KMDataManager.ud_set(state, forKey: Self.keepProportionsKey)
  743. }
  744. private func _getKeepProportionsButtonState() -> Bool {
  745. let defaultV = true
  746. guard let state = KMDataManager.ud_object(forKey: Self.keepProportionsKey) else {
  747. return defaultV
  748. }
  749. return state as? Bool ?? defaultV
  750. }
  751. private func _updateWHProportionButtonEnableState() {
  752. let wEqual = self.handdler?.editAreasBoundsIsEqualForWidth() ?? false
  753. let hEqual = self.handdler?.editAreasBoundsIsEqualForHeight() ?? false
  754. if (wEqual && hEqual) {
  755. // self.whVC?.state = self._getKeepProportionsButtonState() ? .Act : .Norm
  756. let state: KMDesignTokenState = self._getKeepProportionsButtonState() ? .Act : .Norm
  757. self.whVC?.state = .Norm
  758. self._setWHButtonState(state)
  759. } else {
  760. self.whVC?.state = .Disabled
  761. self._setWHButtonState(.Disabled)
  762. }
  763. }
  764. private func _setWHButtonState(_ state: KMDesignTokenState) {
  765. self.whState_ = state
  766. if state == .Act {
  767. self.whVC?.image = NSImage(named: "KMImageNameEditPDFWhExchangeSelected")!
  768. } else if state == .Norm {
  769. self.whVC?.image = NSImage(named: "KMImageNameEditPDFWhExchange")!
  770. }
  771. }
  772. private func _trackEvent() {
  773. // kTrackEventManager.trackEvent(event: "SubTbr_EditPDF", withProperties: ["SubTbr_EditImage":"SubTbr_EditImage_PropertiesPanel_All"])
  774. }
  775. private func _trackAlignEvent() {
  776. // kTrackEventManager.trackEvent(event: "SubTbr_EditPDF", withProperties: ["SubTbr_EditText":"SubTbr_EditContent_AlignContent"])
  777. }
  778. //MARK: - MouseEvent
  779. override func mouseDown(with event: NSEvent) {
  780. super.mouseDown(with: event)
  781. self.view.window?.makeFirstResponder(nil)
  782. }
  783. }
  784. //MARK: - Action
  785. extension KMEditImagePropertyViewController {
  786. @objc func sliderValueChanged(_ sender: NSSlider) {
  787. let sliderValue = sender.floatValue
  788. self.updateImageAreasOpacity(opacity:CGFloat(sliderValue), state: .changed)
  789. }
  790. @objc func whAction(_ sender: AnyObject) {
  791. // let state = self.whVC?.state ?? .None
  792. let state = self.whState_
  793. if state == .Disabled {
  794. return
  795. }
  796. if state == .Act {
  797. // self.whVC?.state = .Norm
  798. self.whVC?.state = .Norm
  799. self._setWHButtonState(.Norm)
  800. self._setKeepProportionsButtonState(state: false)
  801. } else {
  802. // self.whVC?.state = .Act
  803. self.whVC?.state = .Norm
  804. self._setWHButtonState(.Act)
  805. self._setKeepProportionsButtonState(state: true)
  806. }
  807. }
  808. @IBAction func rightRotationImageAction(_ sender: Any) {
  809. if self.listView.editingAreas()?.count ?? 0 < 1 {
  810. return
  811. }
  812. // FMTrackEventManager.defaultManager.trackEvent(event: "SubTbr_PageEdit", withProperties: ["SubTbr_Btn": "Btn_SubTbr_PageEdit_Rotate"])
  813. self._trackEvent()
  814. let areas = self.listView.editingAreas()
  815. if areas!.count == 1 && (areas!.first is CPDFEditImageArea) {
  816. let imageArea = areas!.first as! CPDFEditImageArea
  817. self.listView.rotate(with: imageArea, rotate: 90)
  818. self.listView.selectImageAreas = imageArea
  819. // self.editImageView.image = self.listView.selectImageAreas.thumbnailImage
  820. } else if areas!.count > 1 {
  821. for imagesArea in imagesAreas {
  822. self.listView.rotate(with: imagesArea, rotate: 90)
  823. }
  824. }
  825. self.delegate?.editImagePropertyViewControllerDidChanged(controller: self, type: .rotate)
  826. }
  827. @IBAction func leftRotationImageAction(_ sender: Any) {
  828. if self.listView.editingAreas()?.count ?? 0 < 1 {
  829. return
  830. }
  831. // FMTrackEventManager.defaultManager.trackEvent(event: "SubTbr_PageEdit", withProperties: ["SubTbr_Btn": "Btn_SubTbr_PageEdit_Rotate"])
  832. self._trackEvent()
  833. let areas = self.listView.editingAreas()
  834. if areas!.count == 1 && (areas!.first is CPDFEditImageArea) {
  835. self.listView.rotate(with: self.listView.selectImageAreas, rotate: -90)
  836. if self.listView.editingAreas()!.count == 1 && (self.listView.editingAreas()!.first is CPDFEditImageArea) {
  837. self.listView.selectImageAreas = self.listView.editingAreas()!.first as? CPDFEditImageArea
  838. }
  839. // self.editImageView.image = self.listView.selectImageAreas.thumbnailImage
  840. } else {
  841. for imagesArea in imagesAreas {
  842. self.listView.rotate(with: imagesArea, rotate: -90)
  843. }
  844. }
  845. self.delegate?.editImagePropertyViewControllerDidChanged(controller: self, type: .rotate)
  846. }
  847. @IBAction func flipHorizontalImageAction(_ sender: Any) {
  848. if self.listView.editingAreas()?.count ?? 0 < 1 {
  849. return
  850. }
  851. // FMTrackEventManager.defaultManager.trackEvent(event: "SubTbr_PageEdit", withProperties: ["SubTbr_Btn": "Btn_SubTbr_PageEdit_Reverse"])
  852. self._trackEvent()
  853. let areas = self.listView.editingAreas()
  854. if areas != nil {
  855. for item in areas! {
  856. if item is CPDFEditImageArea {
  857. self.listView.horizontalMirror(with: item as? CPDFEditImageArea)
  858. if areas?.count == 1 {
  859. // self.editImageView.image = (item as AnyObject).thumbnailImage
  860. }
  861. } else {
  862. for imagesArea in imagesAreas {
  863. self.listView.horizontalMirror(with: imagesArea)
  864. }
  865. }
  866. }
  867. }
  868. self.delegate?.editImagePropertyViewControllerDidChanged(controller: self, type: .flip)
  869. }
  870. @IBAction func flipVerticalImageAction(_ sender: Any) {
  871. if self.listView.editingAreas()?.count ?? 0 < 1 {
  872. return
  873. }
  874. // FMTrackEventManager.defaultManager.trackEvent(event: "SubTbr_PageEdit", withProperties: ["SubTbr_Btn": "Btn_SubTbr_PageEdit_Reverse"])
  875. self._trackEvent()
  876. let areas = self.listView.editingAreas()
  877. if areas != nil {
  878. for item in areas! {
  879. if item is CPDFEditImageArea {
  880. self.listView.verticalMirror(with: item as? CPDFEditImageArea)
  881. // self.editImageView.image = self.listView.selectImageAreas.thumbnailImage
  882. } else {
  883. for imagesArea in imagesAreas {
  884. self.listView.verticalMirror(with: imagesArea)
  885. }
  886. }
  887. }
  888. }
  889. self.delegate?.editImagePropertyViewControllerDidChanged(controller: self, type: .flip)
  890. }
  891. @IBAction func cutImageAction(_ sender: Any) {
  892. let state = self.cropVC?.state ?? .None
  893. if state == .Disabled {
  894. return
  895. }
  896. self._trackEvent()
  897. self.handdler?.cropAction()
  898. }
  899. @IBAction func confirmVCImageAction(_ sender: Any) {
  900. self.handdler?.cropComfirmAction()
  901. }
  902. @IBAction func cancelCutImageAction(_ sender: Any) {
  903. self.handdler?.cropCancelAction()
  904. }
  905. @IBAction func restoreCutImageAction(_ sender: Any) {
  906. if self.listView.selectImageAreas == nil {
  907. return
  908. }
  909. self.listView.resetCrop(with: self.listView.selectImageAreas)
  910. }
  911. @IBAction func replaceImageAction(_ sender: NSButton) {
  912. let state = self.replaceVC?.state ?? .None
  913. if state == .Disabled {
  914. return
  915. }
  916. if self.listView.selectImageAreas == nil {
  917. return
  918. }
  919. // let panel = NSOpenPanel()
  920. // panel.allowsMultipleSelection = false
  921. // panel.allowedFileTypes = ["png","jpg"]
  922. // panel.beginSheetModal(for: NSApp.mainWindow!) { response in
  923. // if response == .OK {
  924. // let openPath = panel.url?.path
  925. //// let s = self.listView.extractImage(with: self.selectImageAreas, toImagePath: openPath!)
  926. // let s = self.listView.replace(self.listView.selectImageAreas, imagePath: openPath!)
  927. // if s {
  928. // FMTrackEventManager.defaultManager.trackEvent(event: "SubTbr_PageEdit", withProperties: ["SubTbr_Btn": "Btn_SubTbr_PageEdit_Replace"])
  929. self._trackEvent()
  930. self.handdler?.replaceAction()
  931. }
  932. @IBAction func exportImageAction(_ sender: Any) {
  933. self._trackEvent()
  934. if let data = sender as? NSView {
  935. self.handdler?.showExportMenu(data)
  936. }
  937. }
  938. }
  939. // MARK: - KMSelectPopButtonDelegate
  940. extension KMEditImagePropertyViewController: KMSelectPopButtonDelegate {
  941. func km_comboBoxSelectionDidChange(_ obj: KMDesignSelect) {
  942. if obj == opacityVC {
  943. let index = obj.indexOfSelectedItem
  944. var string = obj.items[index]
  945. string = string.replacingOccurrences(of: "%", with: "")
  946. let value = CGFloat(Float(string)! * 0.01)
  947. self.updateImageAreasOpacity(opacity: value, state: .changed)
  948. }
  949. }
  950. func km_controlTextDidEndEditing(_ obj: KMDesignSelect) {
  951. if obj == opacityVC {
  952. let index = obj.indexOfSelectedItem
  953. var string = obj.items[index]
  954. string = string.replacingOccurrences(of: "%", with: "")
  955. let value = CGFloat(Float(string)! * 0.01)
  956. self.updateImageAreasOpacity(opacity: value, state: .ended)
  957. }
  958. }
  959. }
  960. // MARK: - KMTextFieldDelegate
  961. extension KMEditImagePropertyViewController: KMTextFieldDelegate {
  962. func km_didBecomeFirstResponder(textField: AnyObject) {
  963. if self.wTextFieldItemView.isEqual(to: textField) {
  964. if KMAppearance.isDarkMode() {
  965. self.wTextFieldItemView.backgroundView.layer?.backgroundColor = KMAppearance.Layout.w15Color().cgColor
  966. self.wTextFieldItemView.backgroundView.layer?.borderColor = KMAppearance.Interactive.a0Color().cgColor
  967. } else {
  968. self.wTextFieldItemView.backgroundView.layer?.backgroundColor = .white
  969. self.wTextFieldItemView.backgroundView.layer?.borderColor = NSColor.km_init(hex: "#4982E6").cgColor
  970. }
  971. self.wTextFieldItemView.backgroundView.layer?.borderWidth = 1
  972. self.wTextFieldItemView.backgroundView.layer?.cornerRadius = 4
  973. } else if self.hTextFieldItemView.isEqual(to: textField) {
  974. if KMAppearance.isDarkMode() {
  975. self.hTextFieldItemView.backgroundView.layer?.backgroundColor = KMAppearance.Layout.w15Color().cgColor
  976. self.hTextFieldItemView.backgroundView.layer?.borderColor = KMAppearance.Interactive.a0Color().cgColor
  977. } else {
  978. self.hTextFieldItemView.backgroundView.layer?.backgroundColor = .white
  979. self.hTextFieldItemView.backgroundView.layer?.borderColor = NSColor.km_init(hex: "#4982E6").cgColor
  980. }
  981. }
  982. }
  983. func km_controlTextDidEndEditing(textField: AnyObject) {
  984. if self.wTextFieldItemView.isEqual(to: textField) {
  985. if KMAppearance.isDarkMode() {
  986. self.wTextFieldItemView.backgroundView.layer?.backgroundColor = KMAppearance.Layout.w15Color().cgColor
  987. self.wTextFieldItemView.backgroundView.layer?.borderColor = KMAppearance.Layout.w15Color().cgColor
  988. } else {
  989. self.wTextFieldItemView.backgroundView.layer?.backgroundColor = .white
  990. self.wTextFieldItemView.backgroundView.layer?.borderColor = NSColor.km_init(hex: "#DFE1E5").cgColor
  991. }
  992. self.wTextFieldItemView.backgroundView.layer?.borderWidth = 1
  993. self.wTextFieldItemView.backgroundView.layer?.cornerRadius = 4
  994. let value = self.wTextFieldItemView.stringValue?.stringToCGFloat() ?? 0
  995. let ss = String(format: "%.1f", value)
  996. self.wTextFieldItemView.stringValue = ss
  997. } else if self.hTextFieldItemView.isEqual(to: textField) {
  998. if KMAppearance.isDarkMode() {
  999. self.hTextFieldItemView.backgroundView.layer?.backgroundColor = KMAppearance.Layout.w15Color().cgColor
  1000. self.hTextFieldItemView.backgroundView.layer?.borderColor = KMAppearance.Layout.w15Color().cgColor
  1001. } else {
  1002. self.hTextFieldItemView.backgroundView.layer?.backgroundColor = .white
  1003. self.hTextFieldItemView.backgroundView.layer?.borderColor = NSColor.km_init(hex: "#DFE1E5").cgColor
  1004. }
  1005. let value = self.hTextFieldItemView.stringValue?.stringToCGFloat() ?? 0
  1006. let ss = String(format: "%.1f", value)
  1007. self.hTextFieldItemView.stringValue = ss
  1008. } else if self.rotateTextFieldItemView.isEqual(to: textField) {
  1009. let stringValue = self.rotateTextFieldItemView.stringValue?.replacingOccurrences(of: "°", with: "")
  1010. let formatter = NumberFormatter()
  1011. let number = NSNumber(value: Int(stringValue ?? "") ?? 0)
  1012. let string = formatter.string(for: number)
  1013. self.rotateTextFieldItemView.stringValue = "\(string ?? "0") °"
  1014. let areas = self.handdler?.editingImageAreas ?? []
  1015. for area in areas {
  1016. let rotate = self.listView.getRotateWith(area)
  1017. let rotate2 = string?.stringToCGFloat() ?? 0
  1018. let offset = rotate2-rotate
  1019. if offset != 0 {
  1020. self.listView.rotate(with: area, rotate: offset)
  1021. // kTrackEventManager.trackEvent(event: "SubTbr_EditPDF", withProperties: ["SubTbr_EditImage":"SubTbr_EditImage_PropertiesPanel_CustomRotate"])
  1022. }
  1023. }
  1024. }
  1025. }
  1026. func km_controlTextDidChange(textField: AnyObject) {
  1027. if self.wTextFieldItemView.isEqual(to: textField) {
  1028. let number = NSNumber(value: Int(self.wTextFieldItemView.stringValue ?? "") ?? 0)
  1029. let string = self.wTextFieldItemView.textFiled.formatter?.string(for: number)
  1030. self.wTextFieldItemView.stringValue = string ?? ""
  1031. let areas = self.handdler?.editingImageAreas ?? []
  1032. self.listView.editAreaBoundUpdating = true
  1033. for area in areas {
  1034. // let state = self.whVC?.state ?? .None
  1035. let state = self.whState_
  1036. var bounds = area.bounds
  1037. var width = string?.stringToCGFloat() ?? 0
  1038. if width == 0 {
  1039. return
  1040. }
  1041. let pageBounds = area.page?.bounds ?? .zero
  1042. if width > NSWidth(pageBounds) {
  1043. width = NSWidth(pageBounds)
  1044. let value = String(format: "%.1f", width)
  1045. self.wTextFieldItemView.stringValue = value
  1046. }
  1047. let wOffset = width-bounds.size.width
  1048. // bounds.origin.x = max(0, (pageBounds.size.width-width)*0.5)
  1049. bounds.origin.x = max(0, bounds.origin.x-wOffset * 0.5)
  1050. bounds.origin.x = min(bounds.origin.x, pageBounds.size.width-width)
  1051. if state == .Act { // 寛高约束
  1052. var scale: CGFloat = 0
  1053. if bounds.size.width != 0 {
  1054. scale = width / bounds.size.width
  1055. }
  1056. bounds.size.height *= scale
  1057. bounds.size.width = width
  1058. let value = String(format: "%.1f", bounds.size.height)
  1059. self.hTextFieldItemView.stringValue = value
  1060. } else {
  1061. bounds.size.width = width
  1062. }
  1063. self.listView.setBoundsEditArea(area, withBounds: bounds)
  1064. }
  1065. DispatchQueue.main.async {
  1066. self._updateWHProportionButtonEnableState()
  1067. }
  1068. } else if self.hTextFieldItemView.isEqual(to: textField) {
  1069. let number = NSNumber(value: Int(self.hTextFieldItemView.stringValue ?? "") ?? 0)
  1070. let string = self.hTextFieldItemView.textFiled.formatter?.string(for: number)
  1071. self.hTextFieldItemView.stringValue = string ?? ""
  1072. // let areas = self.listView?.editingAreas() as? [CPDFEditArea] ?? []
  1073. let areas = self.handdler?.editingImageAreas ?? []
  1074. self.listView.editAreaBoundUpdating = true
  1075. for area in areas {
  1076. // if let area = areas.first as? CPDFEditImageArea {
  1077. // let state = self.whVC?.state ?? .None
  1078. let state = self.whState_
  1079. var bounds = area.bounds
  1080. var height = string?.stringToCGFloat() ?? 0
  1081. if height == 0 {
  1082. return
  1083. }
  1084. let pageBounds = area.page?.bounds ?? .zero
  1085. if height > NSHeight(pageBounds) {
  1086. height = NSHeight(pageBounds)
  1087. let value = String(format: "%.1f", height)
  1088. self.hTextFieldItemView.stringValue = value
  1089. }
  1090. let hOffset = height-bounds.size.height
  1091. // bounds.origin.y = max(0, (pageBounds.size.height-height)*0.5)
  1092. bounds.origin.y = max(0, bounds.origin.y-hOffset * 0.5)
  1093. bounds.origin.y = min(bounds.origin.y, pageBounds.size.height-height)
  1094. if state == .Act { // 寛高约束
  1095. var scale: CGFloat = 0
  1096. if bounds.size.height != 0 {
  1097. scale = height / bounds.size.height
  1098. }
  1099. bounds.size.width *= scale
  1100. bounds.size.height = height
  1101. let value = String(format: "%.1f", bounds.size.width)
  1102. self.wTextFieldItemView.stringValue = value
  1103. } else {
  1104. bounds.size.height = height
  1105. }
  1106. self.listView.setBoundsEditArea(area, withBounds: bounds)
  1107. }
  1108. DispatchQueue.main.async {
  1109. self._updateWHProportionButtonEnableState()
  1110. }
  1111. } else if self.rotateTextFieldItemView.isEqual(to: textField) {
  1112. // let stringValue = self.rotateTextFieldItemView.stringValue
  1113. // let number = NSNumber(value: Int(stringValue ?? "") ?? 0)
  1114. // let formatter = NumberFormatter()
  1115. // let string = formatter.string(for: number)
  1116. // let string = self.rotateTextFieldItemView.kFormatter?.string(for: number)
  1117. // self.rotateTextFieldItemView.stringValue = string
  1118. }
  1119. }
  1120. }
  1121. class CustomSliderCell: NSSliderCell {
  1122. var knobImage: NSImage?
  1123. override func drawKnob(_ knobRect: NSRect) {
  1124. if let image = knobImage {
  1125. let imageSize = NSSize(width: knobRect.size.height, height: knobRect.size.height)
  1126. let imageRect = NSRect(origin: knobRect.origin, size: imageSize)
  1127. image.draw(in: imageRect)
  1128. } else {
  1129. super.drawKnob(knobRect)
  1130. }
  1131. }
  1132. }