KMEditImagePropertyViewController.swift 58 KB

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