KMEditPDFPopToolBarController.swift 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697
  1. //
  2. // KMEditPDFPopToolBarController.swift
  3. // PDF Reader Pro
  4. //
  5. // Created by tangchao on 2024/6/18.
  6. //
  7. import Cocoa
  8. @objc enum KMEditPDFToolbarItemKey: Int {
  9. case none
  10. case color
  11. case fontStyle
  12. case fontAdd
  13. case fontReduce
  14. case fontBold
  15. case fontItalic
  16. case textAlignment
  17. case leftRotate
  18. case rightRotate
  19. case reverseX // 左右翻转
  20. case reverseY // 上下翻转
  21. case crop
  22. case replace
  23. case export
  24. case alignmentLeft
  25. case alignmentRight
  26. case alignmentCenterX
  27. case alignmentjustifiedX // 左右两端对齐
  28. case alignmentTop
  29. case alignmentBottom
  30. case alignmentCenterY
  31. case alignmentjustifiedY // 上下两端对齐
  32. case separator // 分割线
  33. func isRotate() -> Bool {
  34. return self == .leftRotate || self == .rightRotate
  35. }
  36. func isReverse() -> Bool {
  37. return self == .reverseX || self == .reverseY
  38. }
  39. func isAlign() -> Bool {
  40. return self.isHorAlign() || self.isVerAlign()
  41. }
  42. func isHorAlign() -> Bool {
  43. return self == .alignmentLeft || self == .alignmentCenterX || self == .alignmentRight || self == .alignmentjustifiedX
  44. }
  45. func isVerAlign() -> Bool {
  46. return self == .alignmentTop || self == .alignmentCenterY || self == .alignmentBottom || self == .alignmentjustifiedY
  47. }
  48. }
  49. class KMEditPDFToolbarModel: NSObject {
  50. var itemKey: KMEditPDFToolbarItemKey = .none
  51. var isEnabled = true
  52. var isSelected = false
  53. var isChanged = true
  54. var fontName: String?
  55. var state: NSControl.StateValue = .mixed
  56. var textAlign: NSTextAlignment = .center
  57. }
  58. class KMSeparatorLineView: NSView {
  59. var strokeColor: NSColor = .black {
  60. didSet {
  61. self.needsDisplay = true
  62. }
  63. }
  64. override func draw(_ dirtyRect: NSRect) {
  65. super.draw(dirtyRect)
  66. let rect = self.bounds
  67. let ctx = NSGraphicsContext.current?.cgContext
  68. ctx?.saveGState()
  69. ctx?.setLineWidth(1)
  70. ctx?.setStrokeColor(self.strokeColor.cgColor)
  71. let startP = NSPoint(x: rect.size.width*0.5, y: 6+2)
  72. ctx?.move(to: startP)
  73. let endP = NSPoint(x: rect.size.width*0.5, y: rect.size.height-8)
  74. ctx?.addLine(to: endP)
  75. ctx?.strokePath()
  76. ctx?.restoreGState()
  77. }
  78. }
  79. struct KMEditPDFToolbarStyle: OptionSet {
  80. let rawValue: Int
  81. static var text = KMEditPDFToolbarStyle(rawValue: 1 << 0)
  82. static var image = KMEditPDFToolbarStyle(rawValue: 1 << 1)
  83. }
  84. class KMEditPDFPopToolBarController: NSViewController {
  85. deinit {
  86. KMPrint("KMEditPDFPopToolBarController deinit.")
  87. }
  88. convenience init() {
  89. self.init(nibName: "KMEditPDFPopToolBarController", bundle: nil)
  90. }
  91. var toolbarView: KMEditPDFToolbarView?
  92. var itemKeys: [KMEditPDFToolbarItemKey] = [] {
  93. didSet {
  94. // self.toolbarView?.reloadData()
  95. }
  96. }
  97. var datas: [KMEditPDFToolbarModel] = [] {
  98. didSet {
  99. self.toolbarView?.reloadData()
  100. }
  101. }
  102. var fontColor: NSColor?
  103. var fontName: String?
  104. var areaCount: Int = 0
  105. var itemClick: ((KMEditPDFToolbarItemKey, Any?)->Void)?
  106. private weak var popover_: NSPopover?
  107. override func viewWillAppear() {
  108. super.viewWillAppear()
  109. self.interfaceThemeDidChanged(NSApp.appearance?.name ?? .aqua)
  110. }
  111. override func viewDidLoad() {
  112. super.viewDidLoad()
  113. // Do view setup here.
  114. // self.interfaceThemeDidChanged(NSApp.appearance?.name ?? .aqua)
  115. let toolbarView = KMEditPDFToolbarView()
  116. self.toolbarView = toolbarView
  117. toolbarView.frame = self.view.bounds
  118. toolbarView.autoresizingMask = [.width, .height]
  119. self.view.addSubview(toolbarView)
  120. toolbarView.delegate = self
  121. toolbarView.inset = .init(top: 6, left: 4, bottom: 0, right: 0)
  122. toolbarView.reloadData()
  123. }
  124. override func viewDidDisappear() {
  125. super.viewDidDisappear()
  126. let itemView = (self.toolbarView?.itemViews.safe_element(for: 1) as? KMEditPDFToolbarItemView)
  127. (itemView?.obj as? KMDesignSelect)?.createFilePopover.close()
  128. self.popover_?.close()
  129. }
  130. @objc func _buttonClick(_ sender: NSButton) {
  131. let idx = sender.tag
  132. if idx >= 0 && idx < self.itemKeys.count {
  133. let key = self.itemKeys[idx]
  134. self.itemClick?(key, sender)
  135. let itemView = (self.toolbarView?.itemViews.safe_element(for: idx) as? KMEditPDFToolbarItemView)
  136. let viewC = (itemView?.obj as? KMDesignButton)
  137. if key == .fontAdd || key == .fontReduce {
  138. } else {
  139. let state = viewC?.state ?? .None
  140. if state == .Act {
  141. viewC?.state = .Norm
  142. } else {
  143. viewC?.state = .Act
  144. }
  145. }
  146. self._trackEvent(key: key, style: self._fetchStyle())
  147. }
  148. }
  149. @objc func alignmentItemClick(_ sender: NSButton) {
  150. self._trackEvent(key: .alignmentTop, style: self._fetchStyle())
  151. let key = self.itemKeys.safe_element(for: sender.tag) as? KMEditPDFToolbarItemKey ?? .none
  152. let isHor = (key == .alignmentLeft || key == .alignmentCenterX || key == .alignmentRight || key == .alignmentjustifiedX)
  153. let vc = KMAlignmentController(withIsHor: isHor, areaCount: self.areaCount)
  154. let createFilePopover: NSPopover = NSPopover.init()
  155. createFilePopover.contentViewController = vc
  156. // createFilePopover.delegate = self
  157. createFilePopover.animates = true
  158. createFilePopover.behavior = .semitransient
  159. createFilePopover.setValue(true, forKey: "shouldHideAnchor")
  160. createFilePopover.show(relativeTo: CGRect(x: sender.bounds.origin.x, y: 20, width: sender.bounds.size.width, height: sender.bounds.size.height), of: sender, preferredEdge: .maxY)
  161. self.popover_ = createFilePopover
  162. vc.itemAction = { [weak self] idx, _ in
  163. var key: KMEditPDFToolbarItemKey = .none
  164. if idx == 0 {
  165. key = isHor ? .alignmentLeft : .alignmentTop
  166. } else if idx == 1 {
  167. key = isHor ? .alignmentCenterX : .alignmentCenterY
  168. } else if idx == 2 {
  169. key = isHor ? .alignmentRight : .alignmentBottom
  170. } else if idx == 3 {
  171. key = isHor ? .alignmentjustifiedX : .alignmentjustifiedY
  172. }
  173. self?.itemClick?(key, nil)
  174. let model = self?.datas.safe_element(for: sender.tag) as? KMEditPDFToolbarModel
  175. model?.itemKey = key
  176. self?.toolbarView?.reloadData()
  177. self?.popover_?.close()
  178. }
  179. }
  180. @objc func textAlignmentItemClick(_ sender: NSButton) {
  181. self._trackEvent(key: .textAlignment)
  182. let itemView = (self.toolbarView?.itemViews.safe_element(for: sender.tag) as? KMEditPDFToolbarItemView)
  183. let viewC = (itemView?.obj as? KMDesignButton)
  184. viewC?.state = .Act
  185. let vc = KMTextAlignmentController(nibName: "KMTextAlignmentController", bundle: nil)
  186. vc.view.appearance = self.view.appearance
  187. let createFilePopover: NSPopover = NSPopover.init()
  188. createFilePopover.contentViewController = vc
  189. // createFilePopover.delegate = self
  190. createFilePopover.animates = true
  191. createFilePopover.behavior = .semitransient
  192. createFilePopover.setValue(true, forKey: "shouldHideAnchor")
  193. createFilePopover.show(relativeTo: CGRect(x: sender.bounds.origin.x, y: 20, width: sender.bounds.size.width, height: sender.bounds.size.height), of: sender, preferredEdge: .maxY)
  194. let model = self.datas.safe_element(for: sender.tag) as? KMEditPDFToolbarModel
  195. vc.align = model?.textAlign ?? .center
  196. self.popover_ = createFilePopover
  197. vc.itemAction = { [weak self] idx, _ in
  198. viewC?.state = .Norm
  199. var data: NSTextAlignment = .left
  200. if idx == 0 {
  201. data = .left
  202. } else if idx == 1 {
  203. data = .center
  204. } else if idx == 2 {
  205. data = .right
  206. }
  207. self?.itemClick?(.textAlignment, data)
  208. self?.popover_?.close()
  209. let model = self?.datas.safe_element(for: sender.tag) as? KMEditPDFToolbarModel
  210. model?.textAlign = data
  211. // self?.toolbarView?.reloadData()
  212. if data == .left {
  213. viewC?.image = NSImage(named: "KMImageNameEditPDFAlignLeftSelect")!
  214. } else if data == .center {
  215. viewC?.image = NSImage(named: "KMImageNameEditPDFAlignCenterSelect")!
  216. } else if data == .right {
  217. viewC?.image = NSImage(named: "KMImageNameEditPDFAlignRightSelect")!
  218. }
  219. }
  220. }
  221. @objc func colorPanelAction(_ sender: NSColorPanel) {
  222. let color = sender.color
  223. let colorView = (self.toolbarView?.itemViews.first as? KMEditPDFToolbarItemView)?.view as? KMEditPDFColorView
  224. colorView?.colorBtn.image = nil
  225. colorView?.colorBtn.layer?.backgroundColor = color.cgColor
  226. self.itemClick?(.color, color)
  227. }
  228. override func interfaceThemeDidChanged(_ appearance: NSAppearance.Name) {
  229. super.interfaceThemeDidChanged(appearance)
  230. self.view.wantsLayer = true
  231. if appearance == .darkAqua {
  232. self.view.layer?.backgroundColor = NSColor(red: 37/255.0, green: 37/255.0, blue: 38/255.0, alpha: 1.0).cgColor
  233. } else {
  234. self.view.layer?.backgroundColor = .white
  235. }
  236. Task { @MainActor in
  237. self.toolbarView?.reloadData()
  238. }
  239. }
  240. // MARK: - Track Events
  241. private func _trackEvent(key: KMEditPDFToolbarItemKey, style: KMEditPDFToolbarStyle = []) {
  242. let catetoryString = "SubTbr_EditPDF"
  243. let label_text = "SubTbr_EditText"
  244. let label_image = "SubTbr_EditImage"
  245. // Text
  246. // if key == .color {
  247. // let eventString = "SubTbr_EditText_FloatBar_Color"
  248. // kTrackEventManager.trackEvent(event: catetoryString, withProperties: [label_text : eventString])
  249. // } else if key == .fontStyle {
  250. // let eventString = "SubTbr_EditText_FloatBar_FontStyle"
  251. // kTrackEventManager.trackEvent(event: catetoryString, withProperties: [label_text : eventString])
  252. // } else if key == .fontAdd || key == .fontReduce {
  253. // let eventString = "SubTbr_EditText_FloatBar_FontSize"
  254. // kTrackEventManager.trackEvent(event: catetoryString, withProperties: [label_text : eventString])
  255. // } else if key == .fontBold {
  256. // let eventString = "SubTbr_EditText_FloatBar_Bold"
  257. // kTrackEventManager.trackEvent(event: catetoryString, withProperties: [label_text : eventString])
  258. // } else if key == .fontItalic {
  259. // let eventString = "SubTbr_EditText_FloatBar_Italic"
  260. // kTrackEventManager.trackEvent(event: catetoryString, withProperties: [label_text : eventString])
  261. // } else if key == .textAlignment {
  262. // let eventString = "SubTbr_EditText_FloatBar_AlignText"
  263. // kTrackEventManager.trackEvent(event: catetoryString, withProperties: [label_text : eventString])
  264. // }
  265. // // Image
  266. // else if key.isRotate() {
  267. // let eventString = "SubTbr_EditImage_FloatBar_Rotate"
  268. // kTrackEventManager.trackEvent(event: catetoryString, withProperties: [label_image : eventString])
  269. // } else if key.isReverse() {
  270. // let eventString = "SubTbr_EditImage_FloatBar_Flip"
  271. // kTrackEventManager.trackEvent(event: catetoryString, withProperties: [label_image : eventString])
  272. // } else if key == .crop {
  273. // let eventString = "SubTbr_EditImage_FloatBar_Crop"
  274. // kTrackEventManager.trackEvent(event: catetoryString, withProperties: [label_image : eventString])
  275. // } else if key == .replace {
  276. // let eventString = "SubTbr_EditImage_FloatBar_Replace"
  277. // kTrackEventManager.trackEvent(event: catetoryString, withProperties: [label_image : eventString])
  278. // } else if key == .export {
  279. // let eventString = "SubTbr_EditImage_FloatBar_Export"
  280. // kTrackEventManager.trackEvent(event: catetoryString, withProperties: [label_image : eventString])
  281. // }
  282. // // 对齐
  283. // else if key.isAlign() {
  284. // if style.contains(.text) && style.contains(.image) {
  285. // let eventString = "SubTbr_EditContent_AlignContent"
  286. // kTrackEventManager.trackEvent(event: catetoryString, withProperties: [label_text : eventString])
  287. // } else if style.contains(.text) {
  288. // let eventString = "SubTbr_EditText_FloatBar_AlignTextBoxes"
  289. // kTrackEventManager.trackEvent(event: catetoryString, withProperties: [label_text : eventString])
  290. // } else if style.contains(.image) {
  291. // let eventString = "SubTbr_EditImage_FloatBar_Align"
  292. // kTrackEventManager.trackEvent(event: catetoryString, withProperties: [label_image : eventString])
  293. // }
  294. // }
  295. }
  296. private func _fetchStyle() -> KMEditPDFToolbarStyle {
  297. var style: KMEditPDFToolbarStyle = []
  298. if let data = self.itemKeys.first {
  299. if data == .color {
  300. style.insert(.text)
  301. } else if data.isRotate() {
  302. style.insert(.image)
  303. } else if data.isAlign() {
  304. style.insert(.text)
  305. style.insert(.image)
  306. }
  307. }
  308. return style
  309. }
  310. }
  311. extension KMEditPDFPopToolBarController: KMEditPDFToolbarViewDelegate {
  312. func numberOfItems(in toolbarView: KMEditPDFToolbarView) -> Int {
  313. return self.datas.count
  314. }
  315. func toolbarView(_ toolbarView: KMEditPDFToolbarView, viewFor index: Int) -> NSView? {
  316. let model = self.datas[index]
  317. let itemKey = model.itemKey
  318. if itemKey == .color {
  319. let colorView = KMEditPDFToolbarItemView()
  320. let view = KMEditPDFColorView()
  321. // view.isEnabled = model.isEnabled
  322. if model.isEnabled {
  323. view.colorBtn.layer?.backgroundColor = self.fontColor?.cgColor
  324. } else {
  325. view.colorBtn.layer?.backgroundColor = .clear
  326. view.colorBtn.image = NSImage(named: "KMImageNameEditPDFColorDisabled")
  327. }
  328. view.itemClick = { [weak self] idx, _ in
  329. self?._trackEvent(key: .color)
  330. let panel = NSColorPanel.shared
  331. panel.setTarget(self)
  332. panel.setAction(#selector(self?.colorPanelAction))
  333. panel.orderFront(nil)
  334. }
  335. colorView.view = view
  336. return colorView
  337. } else if itemKey == .fontStyle {
  338. let fontStyleView = KMEditPDFToolbarItemView()
  339. let viewC = KMDesignSelect.init(withType: .Combox)
  340. viewC.isScrollPop = true
  341. viewC.showVerticalScroller = true
  342. fontStyleView.view = viewC.view
  343. fontStyleView.obj = viewC
  344. viewC.borderColor = KMAppearance.Interactive.s0Color()
  345. viewC.background = KMAppearance.Layout.l1Color()
  346. viewC.background_hov = KMAppearance.Layout.l1Color()
  347. viewC.background_focus = KMAppearance.Layout.l1Color()
  348. viewC.textColor = KMAppearance.Layout.h1Color()
  349. viewC.textColor_hov = KMAppearance.Layout.h1Color()
  350. viewC.textColor_focus = KMAppearance.Layout.h1Color()
  351. if KMAppearance.isDarkMode() {
  352. let color = NSColor.km_init(hex: "#0E1114")
  353. viewC.popViewControllerBackground = color
  354. } else {
  355. let color = NSColor.km_init(hex: "#DADBDE")
  356. // viewC.popViewControllerBackground = color
  357. }
  358. // viewC.popViewControllerBackground = KMAppearance.Layout.bgColor()
  359. viewC.popViewControllerTextColor = KMAppearance.Layout.h0Color()
  360. viewC.popViewControllerEnterFillColor = KMAppearance.Interactive.s0Color()
  361. viewC.updateUI()
  362. let familyNames = CPDFFont.familyNames
  363. viewC.removeAllItems()
  364. viewC.addItems(withObjectValues: familyNames)
  365. if let data = model.fontName {
  366. viewC.stringValue = data
  367. } else {
  368. // viewC.selectItem(at: 0)
  369. viewC.stringValue = "--"
  370. }
  371. viewC.delete = self
  372. return fontStyleView
  373. } else if itemKey == .separator {
  374. let colorView = KMEditPDFToolbarItemView()
  375. let view = KMSeparatorLineView()
  376. view.strokeColor = KMAppearance.separatorLineColor()
  377. colorView.view = view
  378. return colorView
  379. } else if itemKey == .textAlignment {
  380. let colorView = KMEditPDFToolbarItemView()
  381. let viewC = KMDesignButton(withType: .Image)
  382. colorView.view = viewC.view
  383. colorView.obj = viewC
  384. viewC.pagination()
  385. if KMAppearance.isDarkMode() {
  386. viewC.background_hov = NSColor(red: 71/255, green: 72/255, blue: 75/255, alpha: 1)
  387. // viewC.background_sel = KMAppearance.Interactive.m1Color()
  388. } else {
  389. viewC.background_hov = NSColor.km_init(hex: "#EDEEF0")
  390. }
  391. viewC.tag = index
  392. viewC.target = self
  393. viewC.action = #selector(textAlignmentItemClick)
  394. if model.textAlign == .left {
  395. viewC.image = NSImage(named: "KMImageNameEditPDFAlignLeftSelect")!
  396. } else if model.textAlign == .center {
  397. viewC.image = NSImage(named: "KMImageNameEditPDFAlignCenterSelect")!
  398. } else if model.textAlign == .right {
  399. viewC.image = NSImage(named: "KMImageNameEditPDFAlignRightSelect")!
  400. }
  401. viewC.initDefaultValue()
  402. viewC.borderWidth = 0
  403. viewC.borderWidth_hov = 0
  404. viewC.borderWidth_act = 0
  405. if KMAppearance.isDarkMode() {
  406. viewC.background = NSColor.clear
  407. viewC.background_hov = NSColor(red: 71/255, green: 72/255, blue: 75/255, alpha: 1)
  408. viewC.background_act = KMAppearance.Interactive.m1Color()
  409. } else {
  410. }
  411. viewC.state = .Norm
  412. // DispatchQueue.main.async {
  413. // viewC.state = model.isSelected ? .Act : .Norm
  414. // }
  415. return colorView
  416. } else if itemKey.isAlign() {
  417. let itemView = KMEditPDFToolbarItemView()
  418. let viewC = KMDesignButton(withType: .Image)
  419. itemView.view = viewC.view
  420. itemView.obj = viewC
  421. viewC.pagination()
  422. if KMAppearance.isDarkMode() {
  423. viewC.background_hov = NSColor(red: 71/255, green: 72/255, blue: 75/255, alpha: 1)
  424. viewC.background_sel = KMAppearance.Interactive.m1Color()
  425. } else {
  426. viewC.background_hov = NSColor.km_init(hex: "#EDEEF0")
  427. }
  428. viewC.tag = index
  429. viewC.state = model.isEnabled ? .Norm : .Disabled
  430. viewC.button.isEnabled = model.isEnabled
  431. viewC.target = self
  432. viewC.background_disabled = .clear
  433. viewC.borderColor_disabled = .clear
  434. let style = self._fetchStyle()
  435. if style.contains(.text) && style.contains(.image) {
  436. viewC.action = #selector(_buttonClick)
  437. } else {
  438. viewC.action = #selector(alignmentItemClick)
  439. }
  440. let hasTip = style.contains(.text) && style.contains(.image)
  441. if itemKey == .alignmentLeft {
  442. viewC.image = NSImage(named: "KMImageNameEditPDFImage45_1")!
  443. if hasTip {
  444. itemView.popToolTip = NSLocalizedString("Align left", comment: "")
  445. }
  446. } else if itemKey == .alignmentCenterX {
  447. viewC.image = NSImage(named: "KMImageNameEditPDFImage46_1")!
  448. if hasTip {
  449. itemView.popToolTip = NSLocalizedString("Align center", comment: "")
  450. }
  451. } else if itemKey == .alignmentRight {
  452. viewC.image = NSImage(named: "KMImageNameEditPDFImage47_1")!
  453. if hasTip {
  454. itemView.popToolTip = NSLocalizedString("Align right", comment: "")
  455. }
  456. } else if itemKey == .alignmentjustifiedX {
  457. viewC.image = NSImage(named: "KMImageNameEditPDFImage21_1")!
  458. viewC._image_disabled = NSImage(named: "KMImageNameEditPDFImage21")!
  459. if hasTip {
  460. itemView.popToolTip = NSLocalizedString("Align both ends", comment: "")
  461. }
  462. } else if itemKey == .alignmentTop {
  463. viewC.image = NSImage(named: "KMImageNameEditPDFImage48_1")!
  464. if hasTip {
  465. itemView.popToolTip = NSLocalizedString("Align top", comment: "")
  466. }
  467. } else if itemKey == .alignmentCenterY {
  468. viewC.image = NSImage(named: "KMImageNameEditPDFImage49_1")!
  469. if hasTip {
  470. itemView.popToolTip = NSLocalizedString("Align center in vertical", comment: "")
  471. }
  472. } else if itemKey == .alignmentBottom {
  473. viewC.image = NSImage(named: "KMImageNameEditPDFImage50_1")!
  474. if hasTip {
  475. itemView.popToolTip = NSLocalizedString("Align bottom", comment: "")
  476. }
  477. } else if itemKey == .alignmentjustifiedY {
  478. viewC.image = NSImage(named: "KMImageNameEditPDFImage20_1")!
  479. viewC._image_disabled = NSImage(named: "KMImageNameEditPDFImage20")!
  480. if hasTip {
  481. itemView.popToolTip = NSLocalizedString("Align ends in vertical", comment: "")
  482. }
  483. }
  484. viewC.updateUI()
  485. return itemView
  486. }
  487. let colorView = KMEditPDFToolbarItemView()
  488. let viewC = KMDesignButton(withType: .Image)
  489. colorView.view = viewC.view
  490. colorView.obj = viewC
  491. viewC.pagination()
  492. if KMAppearance.isDarkMode() {
  493. viewC.background_hov = NSColor(red: 71/255, green: 72/255, blue: 75/255, alpha: 1)
  494. } else {
  495. // viewC.background_hov = NSColor(hex: "#365898")
  496. }
  497. viewC.tag = index
  498. viewC.state = model.isEnabled ? .Norm : .Disabled
  499. viewC.button.isEnabled = model.isEnabled
  500. viewC.target = self
  501. viewC.action = #selector(_buttonClick)
  502. viewC.background_disabled = .clear
  503. viewC.borderColor_disabled = .clear
  504. if itemKey == .fontAdd {
  505. viewC.image = NSImage(named: "KMImageNameEditPDFFontAdd")!
  506. viewC.image_disabled = NSImage(named: "KMImageNameEditPDFFontAddDisabled")!
  507. colorView.popToolTip = NSLocalizedString("Larger font size", comment: "")
  508. } else if itemKey == .fontReduce {
  509. viewC.image = NSImage(named: "KMImageNameEditPDFFontReduce")!
  510. viewC.image_disabled = NSImage(named: "KMImageNameEditPDFFontReduceDisabled")!
  511. colorView.popToolTip = NSLocalizedString("Reduce font size", comment: "")
  512. } else if itemKey == .fontBold {
  513. viewC.image = NSImage(named: "KMImageNameEditPDFFontBold")!
  514. colorView.popToolTip = NSLocalizedString("Bold font", comment: "")
  515. // viewC.button.keyEquivalent = "b"
  516. // viewC.button.keyEquivalentModifierMask = [.control]
  517. viewC.initDefaultValue()
  518. viewC.borderWidth = 0
  519. viewC.borderWidth_hov = 0
  520. viewC.borderWidth_act = 0
  521. DispatchQueue.main.async {
  522. if model.isEnabled {
  523. viewC.state = model.isSelected ? .Act : .Norm
  524. } else {
  525. viewC.state = .Disabled
  526. }
  527. }
  528. if KMAppearance.isDarkMode() {
  529. viewC.background = NSColor.clear
  530. viewC.background_hov = NSColor(red: 71/255, green: 72/255, blue: 75/255, alpha: 1)
  531. viewC.background_act = KMAppearance.Interactive.m1Color()
  532. } else {
  533. }
  534. } else if itemKey == .fontItalic {
  535. viewC.image = NSImage(named: "KMImageNameEditPDFFontItalic")!
  536. colorView.popToolTip = NSLocalizedString("Italic font", comment: "")
  537. viewC.initDefaultValue()
  538. viewC.borderWidth = 0
  539. viewC.borderWidth_hov = 0
  540. viewC.borderWidth_act = 0
  541. DispatchQueue.main.async {
  542. if model.isEnabled {
  543. viewC.state = model.isSelected ? .Act : .Norm
  544. } else {
  545. viewC.state = .Disabled
  546. }
  547. }
  548. if KMAppearance.isDarkMode() {
  549. viewC.background = NSColor.clear
  550. viewC.background_hov = NSColor(red: 71/255, green: 72/255, blue: 75/255, alpha: 1)
  551. viewC.background_act = KMAppearance.Interactive.m1Color()
  552. } else {
  553. }
  554. } else if itemKey == .textAlignment {
  555. viewC.image = NSImage(named: "KMImageNameEditPDFAlignCenterSelect")!
  556. colorView.popToolTip = NSLocalizedString("Center text", comment: "")
  557. }
  558. // 图片
  559. else if itemKey == .leftRotate {
  560. viewC.image = NSImage(named: "KMImageNameEditPDFLeftRotateNew")!
  561. colorView.popToolTip = NSLocalizedString("Rotate left", comment: "")
  562. } else if itemKey == .rightRotate {
  563. viewC.image = NSImage(named: "KMImageNameEditPDFRightRotateNew")!
  564. colorView.popToolTip = NSLocalizedString("Rotate right", comment: "")
  565. } else if itemKey == .reverseX {
  566. viewC.image = NSImage(named: "KMImageNameEditPDFReverseX")!
  567. colorView.popToolTip = NSLocalizedString("Flip horizontal", comment: "")
  568. } else if itemKey == .reverseY {
  569. viewC.image = NSImage(named: "KMImageNameEditPDFReverseY")!
  570. colorView.popToolTip = NSLocalizedString("Flip vertical", comment: "")
  571. } else if itemKey == .crop {
  572. viewC.image = NSImage(named: "KMImageNameEditPDFCrop")!
  573. viewC.image_disabled = NSImage(named: "KMImageNameEditPDFCropDisabled")!
  574. colorView.popToolTip = NSLocalizedString("Crop", comment: "")
  575. } else if itemKey == .replace {
  576. viewC.image = NSImage(named: "KMImageNameEditPDFReplace")!
  577. viewC.image_disabled = NSImage(named: "KMImageNameEditPDFReplaceDisabled")!
  578. colorView.popToolTip = NSLocalizedString("Replace", comment: "")
  579. } else if itemKey == .export {
  580. viewC.image = NSImage(named: "KMImageNameEditPDFExport")!
  581. colorView.popToolTip = NSLocalizedString("Export file", comment: "")
  582. }
  583. viewC.updateUI()
  584. return colorView
  585. }
  586. func toolbarView(_ toolbarView: KMEditPDFToolbarView, sizeForItemAt index: Int) -> NSSize {
  587. let itemKey = self.itemKeys[index]
  588. if itemKey == .color {
  589. return NSMakeSize(56, 32)
  590. } else if itemKey == .fontStyle {
  591. return NSMakeSize(148, 32)
  592. } else if itemKey == .separator {
  593. return NSMakeSize(16, 32)
  594. }
  595. return NSMakeSize(32, 32)
  596. }
  597. }
  598. extension KMEditPDFPopToolBarController: KMSelectPopButtonDelegate {
  599. func km_SelectPopoverWillShow(_ obj: KMDesignSelect) {
  600. self._trackEvent(key: .fontStyle)
  601. }
  602. func km_comboBoxSelectionDidChange(_ obj: KMDesignSelect) {
  603. self.itemClick?(.fontStyle, obj.stringValue)
  604. }
  605. func km_controlTextDidEndEditing(_ obj: KMDesignSelect) {
  606. }
  607. }