KMMergeTitleBar.swift 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386
  1. //
  2. // KMMergeTitleBar.swift
  3. // PDF Master
  4. //
  5. // Created by tangchao on 2022/11/23.
  6. //
  7. import Cocoa
  8. enum KMMergeTitleBarButtonID: Int {
  9. case add = 0
  10. case delete = 1
  11. case zoomOut = 2
  12. case zoomIn = 3
  13. case merge = 4
  14. case cancel = 5
  15. }
  16. typealias KMMergeTitleBarItemViewClick = (Int)->()
  17. class KMMergeTitleBarItem: NSView {
  18. lazy var leftButton = NSButton()
  19. lazy var rightButton = NSButton()
  20. var itemClick: KMMergeTitleBarItemViewClick!
  21. override init(frame frameRect: NSRect) {
  22. super.init(frame: frameRect)
  23. addSubview(leftButton)
  24. addSubview(rightButton)
  25. leftButton.isBordered = false
  26. rightButton.isBordered = false
  27. leftButton.title = ""
  28. leftButton.target = self
  29. leftButton.action = #selector(leftButtonAction)
  30. rightButton.target = self
  31. rightButton.action = #selector(rightButtonAction)
  32. }
  33. required init?(coder: NSCoder) {
  34. fatalError("init(coder:) has not been implemented")
  35. }
  36. override func layout() {
  37. super.layout()
  38. let width = NSWidth(self.bounds)
  39. let height = NSHeight(self.bounds)
  40. let leftSize: CGFloat = 18
  41. leftButton.frame = NSMakeRect(0, (height-leftSize)*0.5, leftSize, leftSize)
  42. rightButton.frame = NSMakeRect(leftSize, 0, width-leftSize, height)
  43. }
  44. @objc func leftButtonAction() {
  45. guard let callback = itemClick else {
  46. return
  47. }
  48. callback(1)
  49. }
  50. @objc func rightButtonAction() {
  51. guard let callback = itemClick else {
  52. return
  53. }
  54. callback(2)
  55. }
  56. }
  57. protocol KMMergeTitleBarDelegate : NSObjectProtocol {
  58. func titleBar(titleBar: KMMergeTitleBar, itemDidClick: KMMergeTitleBarButtonID)
  59. }
  60. typealias KMMergeTitleBarItemClick = (KMMergeTitleBarButtonID)->()
  61. typealias KMMergeTitleBarPageRangeClick = (_ index: Int, _ pageString: String)->()
  62. class KMMergeTitleBar: NSView {
  63. lazy var addItem = KMMergeTitleBarItem()
  64. private lazy var deleteItem = KMMergeTitleBarItem()
  65. private lazy var vLine = NSView()
  66. private lazy var zoomOutButton = NSButton()
  67. private lazy var zoomInButton = NSButton()
  68. private lazy var pagePageComboBox = NSComboBox()
  69. private lazy var mergeButton = NSButton()
  70. private lazy var cancelButton = NSButton()
  71. private lazy var bottomLine = NSView()
  72. weak open var delegate: KMMergeTitleBarDelegate?
  73. var itemClick: KMMergeTitleBarItemClick!
  74. var pageRangeClick: KMMergeTitleBarPageRangeClick!
  75. private var myModel: KMMergeFileModel!
  76. var model: KMMergeFileModel {
  77. get {
  78. myModel
  79. }
  80. set {
  81. myModel = newValue
  82. if newValue.pageRange == .custom {
  83. if !newValue.pageRangeString.isEmpty {
  84. pagePageComboBox.stringValue = newValue.pageRangeString
  85. }
  86. pagePageComboBox.isEditable = true
  87. } else if newValue.pageRange == .all {
  88. pagePageComboBox.stringValue = NSLocalizedString("全部页面", comment: "")
  89. } else if newValue.pageRange == .oddPages {
  90. pagePageComboBox.stringValue = NSLocalizedString("奇数页面", comment: "")
  91. } else if newValue.pageRange == .evenPages {
  92. pagePageComboBox.stringValue = NSLocalizedString("偶数页面", comment: "")
  93. }
  94. }
  95. }
  96. deinit {
  97. /// 释放
  98. }
  99. override init(frame frameRect: NSRect) {
  100. super.init(frame: frameRect)
  101. initDefaultValue()
  102. initSubViews()
  103. }
  104. required init?(coder: NSCoder) {
  105. fatalError("init(coder:) has not been implemented")
  106. }
  107. override var isFlipped: Bool {
  108. return true
  109. }
  110. func initDefaultValue() {
  111. addItem.wantsLayer = true
  112. addItem.rightButton.title = NSLocalizedString("新增文件", comment: "")
  113. addItem.itemClick = {
  114. [weak weakSelf = self] (index: Int) -> () in
  115. guard let itemClickAction = weakSelf?.itemClick else {
  116. return
  117. }
  118. itemClickAction(KMMergeTitleBarButtonID.add)
  119. }
  120. deleteItem.wantsLayer = true
  121. deleteItem.rightButton.title = NSLocalizedString("删除", comment: "")
  122. deleteItem.itemClick = {
  123. [weak weakSelf = self] (index: Int) -> () in
  124. guard let itemClickAction = weakSelf?.itemClick else {
  125. return
  126. }
  127. itemClickAction(KMMergeTitleBarButtonID.delete)
  128. }
  129. vLine.wantsLayer = true
  130. vLine.layer?.backgroundColor = NSColor.lightGray.cgColor
  131. zoomOutButton.wantsLayer = true
  132. zoomOutButton.isBordered = false
  133. zoomOutButton.title = "+"
  134. zoomOutButton.layer?.borderWidth = 1;
  135. zoomOutButton.layer?.borderColor = NSColor.gray.cgColor;
  136. zoomOutButton.layer?.cornerRadius = 12
  137. zoomOutButton.target = self
  138. zoomOutButton.action = #selector(zoomOutButtonAction)
  139. zoomInButton.wantsLayer = true
  140. zoomInButton.isBordered = false
  141. zoomInButton.title = "-"
  142. zoomInButton.layer?.borderWidth = 1;
  143. zoomInButton.layer?.borderColor = NSColor.gray.cgColor;
  144. zoomInButton.layer?.cornerRadius = 12
  145. zoomInButton.target = self
  146. zoomInButton.action = #selector(zoomInButtonAction)
  147. pagePageComboBox.stringValue = "全部页面"
  148. pagePageComboBox.placeholderString = NSLocalizedString("如1,3-5,10", comment: "")
  149. pagePageComboBox.delegate = self
  150. pagePageComboBox.isEditable = false
  151. pagePageComboBox.addItems(withObjectValues: [NSLocalizedString("全部页面", comment: ""),
  152. NSLocalizedString("奇数页面", comment: ""),
  153. NSLocalizedString("偶数页面", comment: ""),
  154. NSLocalizedString("自定义", comment: "")])
  155. mergeButton.wantsLayer = true
  156. mergeButton.isBordered = false
  157. mergeButton.title = NSLocalizedString("合并", comment: "")
  158. mergeButton.attributedTitle = NSMutableAttributedString(string: mergeButton.title, attributes: [NSAttributedString.Key.foregroundColor : NSColor.white])
  159. mergeButton.layer?.backgroundColor = NSColor.black.cgColor
  160. mergeButton.layer?.cornerRadius = 5
  161. mergeButton.target = self
  162. mergeButton.action = #selector(mergeButtonAction)
  163. cancelButton.wantsLayer = true
  164. cancelButton.isBordered = false
  165. cancelButton.title = NSLocalizedString("关闭", comment: "")
  166. cancelButton.layer?.backgroundColor = NSColor.white.cgColor
  167. cancelButton.layer?.cornerRadius = 5
  168. cancelButton.layer?.borderWidth = 1
  169. cancelButton.layer?.borderColor = NSColor.black.cgColor
  170. cancelButton.target = self
  171. cancelButton.action = #selector(cancelButtonAction)
  172. bottomLine.wantsLayer = true
  173. bottomLine.layer?.backgroundColor = NSColor.lightGray.cgColor
  174. }
  175. func initSubViews() {
  176. addSubview(addItem)
  177. addSubview(deleteItem)
  178. addSubview(vLine)
  179. addSubview(zoomOutButton)
  180. addSubview(zoomInButton)
  181. addSubview(pagePageComboBox)
  182. addSubview(mergeButton)
  183. addSubview(cancelButton)
  184. addSubview(bottomLine)
  185. }
  186. override func layout() {
  187. super.layout()
  188. let width = NSWidth(self.bounds)
  189. let height = NSHeight(self.bounds)
  190. let space: CGFloat = 12
  191. let centerX: CGFloat = width * 0.5
  192. let lineH: CGFloat = 18
  193. vLine.frame = NSMakeRect(centerX, (height-lineH)*0.5, 1, lineH);
  194. let itemH: CGFloat = 28
  195. let itemY: CGFloat = (height-itemH) * 0.5
  196. let deleteItemWidth: CGFloat = 60
  197. let deleteItemX = centerX-deleteItemWidth-space
  198. deleteItem.frame = NSMakeRect(deleteItemX, itemY, deleteItemWidth, itemH)
  199. let addItemWidth: CGFloat = 80
  200. let addItemX = deleteItemX-addItemWidth-space
  201. addItem.frame = NSMakeRect(addItemX, itemY, addItemWidth, itemH)
  202. let zoomButtonSize: CGFloat = 24
  203. let zoomOutButtonX = centerX + space
  204. let zoomButtonY = (height-zoomButtonSize) * 0.5
  205. zoomOutButton.frame = NSMakeRect(zoomOutButtonX, zoomButtonY, zoomButtonSize, zoomButtonSize)
  206. zoomInButton.frame = NSMakeRect(zoomOutButtonX+zoomButtonSize+space, zoomButtonY, zoomButtonSize, zoomButtonSize)
  207. let pagePageH: CGFloat = 24
  208. let pagePageX: CGFloat = zoomInButton.frame.maxX + 30
  209. pagePageComboBox.frame = NSMakeRect(pagePageX, (height-pagePageH) * 0.5, 160, pagePageH)
  210. let rightButtonH: CGFloat = 30
  211. let rightButtonW: CGFloat = 60
  212. let rightButtonY: CGFloat = (height-rightButtonH)*0.5
  213. let rightMargin: CGFloat = 28
  214. let cancelButtonX = width-rightMargin-rightButtonW
  215. cancelButton.frame = NSMakeRect(cancelButtonX, rightButtonY, rightButtonW, rightButtonH)
  216. mergeButton.frame = NSMakeRect(cancelButtonX-rightButtonW-12, rightButtonY, rightButtonW, rightButtonH)
  217. bottomLine.frame = NSMakeRect(0, height-1, width, 1)
  218. }
  219. /**
  220. * MARK: - User Action
  221. */
  222. @objc func addItemAction() {
  223. guard let itemClickAction = itemClick else {
  224. return
  225. }
  226. itemClickAction(KMMergeTitleBarButtonID.add)
  227. }
  228. @objc func deleteItemAction() {
  229. guard let itemClickAction = itemClick else {
  230. return
  231. }
  232. itemClickAction(KMMergeTitleBarButtonID.delete)
  233. }
  234. @objc func zoomOutButtonAction() {
  235. guard let itemClickAction = itemClick else {
  236. return
  237. }
  238. itemClickAction(KMMergeTitleBarButtonID.zoomOut)
  239. }
  240. @objc func zoomInButtonAction() {
  241. guard let itemClickAction = itemClick else {
  242. return
  243. }
  244. itemClickAction(KMMergeTitleBarButtonID.zoomIn)
  245. }
  246. @objc func mergeButtonAction() {
  247. guard let itemClickAction = itemClick else {
  248. return
  249. }
  250. itemClickAction(KMMergeTitleBarButtonID.merge)
  251. }
  252. @objc func cancelButtonAction() {
  253. guard let itemClickAction = itemClick else {
  254. return
  255. }
  256. itemClickAction(KMMergeTitleBarButtonID.cancel)
  257. }
  258. func setDeleteItemEnable(enable: Bool) {
  259. deleteItem.leftButton.isEnabled = enable
  260. deleteItem.rightButton.isEnabled = enable
  261. }
  262. func setZoomItemEnable(zoomIn: Bool, enable: Bool) {
  263. if zoomIn {
  264. zoomInButton.isEnabled = enable
  265. } else {
  266. zoomOutButton.isEnabled = enable
  267. }
  268. }
  269. func setPageRangeComboBoxHidden(isHidden: Bool) {
  270. pagePageComboBox.isHidden = isHidden
  271. }
  272. func setPagePageComboBoxString(string: String) {
  273. pagePageComboBox.stringValue = string
  274. }
  275. }
  276. extension KMMergeTitleBar: NSComboBoxDelegate {
  277. func comboBoxSelectionDidChange(_ notification: Notification) {
  278. if notification.object as! NSComboBox != pagePageComboBox {
  279. return
  280. }
  281. let box: NSComboBox = notification.object as! NSComboBox
  282. let index = box.indexOfSelectedItem
  283. if index < 0 {
  284. return
  285. }
  286. if index == 3 {
  287. pagePageComboBox.isEditable = true
  288. pagePageComboBox.stringValue = "如1,3-5,10"
  289. self.window?.makeFirstResponder(pagePageComboBox)
  290. } else {
  291. pagePageComboBox.isEditable = false
  292. guard let callback = pageRangeClick else {
  293. return
  294. }
  295. callback(index, "")
  296. }
  297. }
  298. func controlTextDidEndEditing(_ obj: Notification) {
  299. if obj.object as! NSComboBox != pagePageComboBox {
  300. return
  301. }
  302. let box: NSComboBox = obj.object as! NSComboBox
  303. pagePageComboBox.isEditable = false
  304. guard let callback = pageRangeClick else {
  305. return
  306. }
  307. callback(3, pagePageComboBox.stringValue)
  308. }
  309. }