KMToolbarCustomViewController.swift 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401
  1. //
  2. // KMToolbarCustomViewController.swift
  3. // PDF Reader Pro
  4. //
  5. // Created by tangchao on 2023/10/26.
  6. //
  7. import Cocoa
  8. private let KMPasteboardTypeAllowedIndexSet: NSPasteboard.PasteboardType = NSPasteboard.PasteboardType(rawValue: "KMPasteboardTypeAllowedIndexSet")
  9. private let KMPasteboardTypeDefaultIndexSet: NSPasteboard.PasteboardType = NSPasteboard.PasteboardType(rawValue: "KMPasteboardTypeDefaultIndexSet")
  10. private let KMToolbarImageToPDFItemIdentifier = "KMToolbarImageToPDFItemIdentifier"
  11. private let KMToolbarRotateRightItemIdentifier = "KMToolbarRotateRightItemIdentifier"
  12. private let KMToolbarRotateLeftItemIdentifier = "KMToolbarRotateLeftItemIdentifier"
  13. private let KMToolbarPageBreaksItemIdentifier = "KMToolbarPageBreaksItemIdentifier"
  14. private let KMToolbarViewModeItemIdentifier = "KMToolbarViewModeItemIdentifier"
  15. private let KMToolbarDisplayModeItemIdentifier = "KMToolbarDisplayModeItemIdentifier"
  16. private let KMToolbarSpaceItemIdentifier = "KMToolbarSpaceItemIdentifier"
  17. //private let KMImageNameToolbarSpace = "KMImageNameToolbarSpace"
  18. private let KMToolbarFormAlignmentIdentifier = "KMToolbarFormAlignmentIdentifier"
  19. class KMToolbarCellView: NSTableCellView {
  20. @IBOutlet weak var tickImageView: NSImageView!
  21. }
  22. class KMToolbarCustomViewController: NSViewController {
  23. @IBOutlet weak var titleLabel: NSTextField!
  24. @IBOutlet weak var leftLabel: NSTextField!
  25. @IBOutlet weak var rightLabel: NSTextField!
  26. @IBOutlet weak var subTitleLabel: NSTextField!
  27. @IBOutlet weak var okButton: NSButton!
  28. @IBOutlet weak var cancelButton: NSButton!
  29. @IBOutlet weak var resetButton: NSButton!
  30. @IBOutlet weak var addButton: NSButton!
  31. @IBOutlet weak var removeButton: NSButton!
  32. @IBOutlet weak var allowedItemsTableView: NSTableView!
  33. @IBOutlet weak var defaultItemsTableView: NSTableView!
  34. private var _allowedItems: [String] = []
  35. private var _defaultItems: [String] = []
  36. weak var toolbar: KMToolbarView?
  37. var itemClick: KMCommonClickBlock?
  38. override func loadView() {
  39. super.loadView()
  40. self.titleLabel.stringValue = KMLocalizedString("Customize Toolbar")
  41. self.subTitleLabel.stringValue = String(format: "%@", KMLocalizedString("Drag-and-drop tools to change their order"))
  42. self.leftLabel.stringValue = KMLocalizedString("Choose tools to add:")
  43. self.rightLabel.stringValue = KMLocalizedString("Tools to show in Toolbar:")
  44. self.okButton.title = KMLocalizedString("Done")
  45. self.cancelButton.title = KMLocalizedString("Cancel")
  46. self.resetButton.title = KMLocalizedString("Reset")
  47. self.resetButton.toolTip = KMLocalizedString("Reset Toolbars")
  48. self.addButton.title = String(format: "%@ >>", KMLocalizedString("Add"))
  49. self.addButton.toolTip = KMLocalizedString("Add to Toolbar")
  50. self.removeButton.title = String(format: "<< %@", KMLocalizedString("Remove"))
  51. self.removeButton.toolTip = KMLocalizedString("Remove from Toolbar")
  52. var color = NSColor(red: 51.0/255.0, green: 51.0/255.0, blue: 51.0/255.0, alpha: 1.0)
  53. if (KMAppearance.isDarkMode()) {
  54. color = NSColor(red: 1, green: 1, blue: 1, alpha: 0.5)
  55. }
  56. self.titleLabel.textColor = color
  57. self.subTitleLabel.textColor = color
  58. self.leftLabel.textColor = color
  59. self.rightLabel.textColor = color
  60. self.addButton.wantsLayer = true
  61. self.addButton.layer?.borderColor = NSColor(red: 153.0/255.0, green: 153.0/255.0, blue: 153.0/255.0, alpha: 1).cgColor
  62. self.addButton.layer?.borderWidth = 1.0
  63. self.addButton.layer?.cornerRadius = 4.0
  64. self.removeButton.wantsLayer = true
  65. self.removeButton.layer?.borderColor = NSColor(red: 153.0/255.0, green: 153.0/255.0, blue: 153.0/255.0, alpha: 1).cgColor
  66. self.removeButton.layer?.borderWidth = 1.0
  67. self.removeButton.layer?.cornerRadius = 4.0
  68. self.addButton.isEnabled = false
  69. self.removeButton.isEnabled = false
  70. self.defaultItemsTableView.registerForDraggedTypes([KMPasteboardTypeAllowedIndexSet, KMPasteboardTypeDefaultIndexSet])
  71. self._loadAllowedItems()
  72. self._loadDefaultItems()
  73. }
  74. override func viewDidLoad() {
  75. super.viewDidLoad()
  76. // Do view setup here.
  77. }
  78. @IBAction @objc func okButtonAction(_ sender: NSButton) {
  79. if self.toolbar?.toolbarIdentifier != nil && self._defaultItems.count > 0 {
  80. if let data = self.toolbar?.toolbarIdentifier {
  81. KMDataManager.ud_set(self._defaultItems, forKey: data)
  82. }
  83. }
  84. self.itemClick?(1)
  85. self.view.window?.close()
  86. }
  87. @IBAction @objc func cancelButtonAction(_ sender: NSButton) {
  88. self.itemClick?(0)
  89. self.view.window?.close()
  90. }
  91. @IBAction @objc func resetButtonAction(_ sender: NSButton) {
  92. if let data = self.toolbar?.toolbarIdentifier {
  93. KMDataManager.ud_removeObject(forKey: data)
  94. }
  95. self._loadDefaultItems()
  96. self.removeButton.isEnabled = true
  97. self.allowedItemsTableView.reloadData()
  98. self.defaultItemsTableView.reloadData()
  99. self.itemClick?(2)
  100. }
  101. @IBAction @objc func addButtonAction(_ sender: NSButton) {
  102. let selectedRowIndexes = self.allowedItemsTableView.selectedRowIndexes
  103. if (selectedRowIndexes.count <= 0) {
  104. return
  105. }
  106. var itemIdentifiers: [String] = []
  107. for index in selectedRowIndexes {
  108. itemIdentifiers.append(self._allowedItems[index])
  109. }
  110. if (itemIdentifiers.count > 0) {
  111. var isAdded = false
  112. for itemIdentifier in itemIdentifiers {
  113. if self._defaultItems.contains(itemIdentifier) == false || itemIdentifier == KMToolbarSpaceItemIdentifier {
  114. self._defaultItems.append(itemIdentifier)
  115. isAdded = true
  116. }
  117. }
  118. if (isAdded) {
  119. self.allowedItemsTableView.reloadData()
  120. self.defaultItemsTableView.reloadData()
  121. self.defaultItemsTableView.scrollRowToVisible(self._defaultItems.count-1)
  122. self.addButton.isEnabled = false
  123. self.removeButton.isEnabled = true
  124. } else {
  125. }
  126. }
  127. }
  128. @IBAction @objc func removeButtonAction(_ sender: NSButton) {
  129. let selectedRowIndexes = self.defaultItemsTableView.selectedRowIndexes
  130. if (selectedRowIndexes.count <= 0) {
  131. return
  132. }
  133. for index in selectedRowIndexes.reversed() {
  134. if index < self._defaultItems.count {
  135. self._defaultItems.remove(at: index)
  136. }
  137. if self._defaultItems.count <= 6 {
  138. self.removeButton.isEnabled = false
  139. break
  140. }
  141. }
  142. self.allowedItemsTableView.reloadData()
  143. self.defaultItemsTableView.reloadData()
  144. self.removeButton.isEnabled = false
  145. }
  146. @objc func delete(_ sender: Any?) {
  147. if self.defaultItemsTableView.isEqual(to: self.view.window?.firstResponder) {
  148. self.removeButtonAction(self.removeButton)
  149. }
  150. }
  151. }
  152. extension KMToolbarCustomViewController: NSTableViewDelegate, NSTableViewDataSource {
  153. func numberOfRows(in tableView: NSTableView) -> Int {
  154. var rows = 0
  155. if (self.allowedItemsTableView.isEqual(to: tableView)) {
  156. rows = self._allowedItems.count
  157. } else if (self.defaultItemsTableView.isEqual(to: tableView)) {
  158. rows = self._defaultItems.count
  159. }
  160. return rows
  161. }
  162. func tableView(_ tableView: NSTableView, viewFor tableColumn: NSTableColumn?, row: Int) -> NSView? {
  163. var cellView: NSTableCellView?
  164. var itemIdentifier: String?
  165. if (self.allowedItemsTableView == tableView) {
  166. cellView = tableView.makeView(withIdentifier: NSUserInterfaceItemIdentifier(rawValue: "KMAllowedItemsCell"), owner: self) as! NSTableCellView
  167. itemIdentifier = self._allowedItems[row]
  168. } else if (self.defaultItemsTableView == tableView) {
  169. cellView = tableView.makeView(withIdentifier: NSUserInterfaceItemIdentifier(rawValue: "KMDefaultItemsCell"), owner: self) as! NSTableCellView
  170. itemIdentifier = self._defaultItems[row]
  171. }
  172. if itemIdentifier == KMToolbarSpaceItemIdentifier || itemIdentifier == KMNewToolbarSpaceItemIdentifier {
  173. cellView?.textField?.stringValue = KMLocalizedString("")
  174. cellView?.imageView?.image = NSImage(named: KMImageNameToolbarSpace)
  175. } else {
  176. if let data = self.toolbar?.delegate?.toolbar?(self.toolbar!, itemFor: itemIdentifier!) {
  177. if itemIdentifier == KMToolbarFormAlignmentIdentifier {
  178. cellView?.textField?.stringValue = KMLocalizedString("Alignment")
  179. } else {
  180. cellView?.textField?.stringValue = data.titleName ?? ""
  181. }
  182. cellView?.imageView?.image = data.image
  183. }
  184. }
  185. if let data = cellView?.isKind(of: KMToolbarCellView.self), data {
  186. if self._defaultItems.contains(itemIdentifier!) && itemIdentifier != KMToolbarSpaceItemIdentifier {
  187. (cellView as? KMToolbarCellView)?.tickImageView.image = NSImage(named: "KMImageNameToolbarSelected")
  188. } else {
  189. (cellView as? KMToolbarCellView)?.tickImageView.image = nil
  190. }
  191. }
  192. return cellView;
  193. }
  194. func tableView(_ tableView: NSTableView, shouldSelectRow row: Int) -> Bool {
  195. if (self.allowedItemsTableView == tableView) {
  196. let itemIdentifier = self._allowedItems[row]
  197. if (self._defaultItems.contains(itemIdentifier) && itemIdentifier != KMToolbarSpaceItemIdentifier) {
  198. return false
  199. }
  200. }
  201. return true
  202. }
  203. func tableViewSelectionDidChange(_ notification: Notification) {
  204. self.allowedItemsTableView.delegate = nil
  205. self.defaultItemsTableView.delegate = nil
  206. let tableView = notification.object as? NSTableView
  207. if (self.allowedItemsTableView == tableView) {
  208. self.addButton.isEnabled = tableView!.selectedRowIndexes.count > 0 ? true : false
  209. let set = NSIndexSet()
  210. self.defaultItemsTableView.selectRowIndexes(set as IndexSet, byExtendingSelection: false)
  211. self.removeButton.isEnabled = false
  212. } else if (self.defaultItemsTableView == tableView) {
  213. if self._defaultItems.count > 6 {
  214. self.removeButton.isEnabled = tableView!.selectedRowIndexes.count > 0 ? true : false
  215. } else {
  216. self.removeButton.isEnabled = false
  217. }
  218. let set = NSIndexSet()
  219. self.allowedItemsTableView.selectRowIndexes(set as IndexSet, byExtendingSelection: false)
  220. self.addButton.isEnabled = false
  221. }
  222. self.allowedItemsTableView.delegate = self
  223. self.defaultItemsTableView.delegate = self
  224. }
  225. func tableView(_ tableView: NSTableView, writeRowsWith rowIndexes: IndexSet, to pboard: NSPasteboard) -> Bool {
  226. let indexSetData = NSKeyedArchiver.archivedData(withRootObject: rowIndexes)
  227. if (self.allowedItemsTableView == tableView) {
  228. pboard.declareTypes([KMPasteboardTypeAllowedIndexSet], owner: self)
  229. pboard.setData(indexSetData, forType: KMPasteboardTypeAllowedIndexSet)
  230. } else if (self.defaultItemsTableView == tableView) {
  231. pboard.declareTypes([KMPasteboardTypeDefaultIndexSet], owner: self)
  232. pboard.setData(indexSetData, forType: KMPasteboardTypeDefaultIndexSet)
  233. }
  234. return true
  235. }
  236. func tableView(_ tableView: NSTableView, validateDrop info: NSDraggingInfo, proposedRow row: Int, proposedDropOperation dropOperation: NSTableView.DropOperation) -> NSDragOperation {
  237. if (self.defaultItemsTableView == tableView) {
  238. if (dropOperation == .on) {
  239. return NSDragOperation(rawValue: 0)
  240. }
  241. return .move
  242. }
  243. return NSDragOperation(rawValue: 0)
  244. }
  245. func tableView(_ tableView: NSTableView, acceptDrop info: NSDraggingInfo, row: Int, dropOperation: NSTableView.DropOperation) -> Bool {
  246. if (self.defaultItemsTableView == tableView) {
  247. let pboard = info.draggingPasteboard
  248. if pboard.availableType(from: [KMPasteboardTypeAllowedIndexSet]) != nil {
  249. let rowData = pboard.data(forType: KMPasteboardTypeAllowedIndexSet)
  250. let rowIndexes: IndexSet = NSKeyedUnarchiver.unarchiveObject(with: rowData!) as! IndexSet
  251. var isAdded = false
  252. for idx in rowIndexes.reversed() {
  253. let itemIdentifier = self._allowedItems[idx]
  254. if self._defaultItems.contains(itemIdentifier) == false || itemIdentifier == KMToolbarSpaceItemIdentifier {
  255. self._defaultItems.insert(itemIdentifier, at: row)
  256. isAdded = true
  257. }
  258. }
  259. if (isAdded) {
  260. self.allowedItemsTableView.reloadData()
  261. self.defaultItemsTableView.reloadData()
  262. return true
  263. } else {
  264. }
  265. } else if pboard.availableType(from: [KMPasteboardTypeDefaultIndexSet]) != nil {
  266. let rowData = pboard.data(forType: KMPasteboardTypeDefaultIndexSet)
  267. let indexes: IndexSet = NSKeyedUnarchiver.unarchiveObject(with: rowData!) as! IndexSet
  268. var items: [String] = []
  269. for item in self._defaultItems {
  270. items.append(item)
  271. }
  272. var moveItems: [String] = []
  273. for index in indexes {
  274. moveItems.append(self._defaultItems[index])
  275. }
  276. var index = 0
  277. for index in indexes {
  278. self._defaultItems.remove(at: index)
  279. }
  280. var _row = row
  281. if (_row > 0) {
  282. var item: String? = items[row-1]
  283. while (moveItems.contains(item ?? "")) {
  284. _row -= 1
  285. if (_row < 0) {
  286. item = nil
  287. break
  288. } else {
  289. item = items[_row]
  290. }
  291. }
  292. if (item != nil) {
  293. index = self._defaultItems.firstIndex(of: item!)!+1
  294. }
  295. }
  296. for i in 0 ..< moveItems.count {
  297. self._defaultItems.insert(moveItems[i], at: index+i)
  298. }
  299. self.allowedItemsTableView.reloadData()
  300. self.defaultItemsTableView.reloadData()
  301. return true
  302. }
  303. }
  304. return false
  305. }
  306. }
  307. extension KMToolbarCustomViewController: NSMenuItemValidation {
  308. func validateMenuItem(_ menuItem: NSMenuItem) -> Bool {
  309. if (menuItem.action == #selector(delete)) {
  310. if (self.defaultItemsTableView != self.view.window!.firstResponder) {
  311. return false
  312. }
  313. }
  314. return true
  315. }
  316. }
  317. // MARK: - Private Methods
  318. extension KMToolbarCustomViewController {
  319. private func _loadAllowedItems() {
  320. if let data = self.toolbar?.delegate?.toolbarAllowedItemIdentifiers?(self.toolbar!) {
  321. self._allowedItems = data
  322. }
  323. let itemArray = [KMToolbarImageToPDFItemIdentifier, KMToolbarRotateRightItemIdentifier, KMToolbarRotateLeftItemIdentifier, KMToolbarPageBreaksItemIdentifier, KMToolbarViewModeItemIdentifier, KMToolbarDisplayModeItemIdentifier, KMToolbarDividerItemIdentifier, KMToolbarShowToolbarItemIdentifier]
  324. for itemStr in itemArray {
  325. if (self._allowedItems.contains(itemStr)) {
  326. self._allowedItems.removeObject(itemStr)
  327. }
  328. }
  329. }
  330. private func _loadDefaultItems() {
  331. var itemIdentifiers: [String]? = self.toolbar?.toolbarIdentifier != nil ? (UserDefaults.standard.object(forKey: self.toolbar!.toolbarIdentifier!) as? [String]) : []
  332. if itemIdentifiers == nil || itemIdentifiers!.count <= 0 {
  333. if let items = self.toolbar?.delegate?.toolbarDefaultItemIdentifiers?(self.toolbar!) {
  334. itemIdentifiers = items
  335. }
  336. }
  337. self._defaultItems.removeAll()
  338. if (itemIdentifiers != nil) {
  339. for itemI in itemIdentifiers! {
  340. if itemI != KMToolbarDividerItemIdentifier {
  341. self._defaultItems.append(itemI)
  342. }
  343. }
  344. } else {
  345. self._defaultItems = []
  346. }
  347. }
  348. }