KMToolbarCustomViewController.swift 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390
  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", nil)
  41. self.subTitleLabel.stringValue = String(format: "%@", KMLocalizedString("Drag-and-drop tools to change their order", nil))
  42. self.leftLabel.stringValue = KMLocalizedString("Choose tools to add:", nil)
  43. self.rightLabel.stringValue = KMLocalizedString("Tools to show in Toolbar:", nil)
  44. self.okButton.title = KMLocalizedString("Done", nil)
  45. self.cancelButton.title = KMLocalizedString("Cancel", nil)
  46. self.resetButton.title = KMLocalizedString("Reset", nil)
  47. self.resetButton.toolTip = KMLocalizedString("Reset Toolbars", nil)
  48. self.addButton.title = String(format: "%@ >>", KMLocalizedString("Add", nil))
  49. self.addButton.toolTip = KMLocalizedString("Add to Toolbar", nil)
  50. self.removeButton.title = String(format: "<< %@", KMLocalizedString("Remove", nil))
  51. self.removeButton.toolTip = KMLocalizedString("Remove from Toolbar", nil)
  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.allowedItemsTableView.reloadData()
  97. self.defaultItemsTableView.reloadData()
  98. self.itemClick?(2)
  99. }
  100. @IBAction @objc func addButtonAction(_ sender: NSButton) {
  101. let selectedRowIndexes = self.allowedItemsTableView.selectedRowIndexes
  102. if (selectedRowIndexes.count <= 0) {
  103. return
  104. }
  105. var itemIdentifiers: [String] = []
  106. for index in selectedRowIndexes {
  107. itemIdentifiers.append(self._allowedItems[index])
  108. }
  109. if (itemIdentifiers.count > 0) {
  110. var isAdded = false
  111. for itemIdentifier in itemIdentifiers {
  112. if self._defaultItems.contains(itemIdentifier) == false || itemIdentifier == KMToolbarSpaceItemIdentifier {
  113. self._defaultItems.append(itemIdentifier)
  114. isAdded = true
  115. }
  116. }
  117. if (isAdded) {
  118. self.allowedItemsTableView.reloadData()
  119. self.defaultItemsTableView.reloadData()
  120. self.defaultItemsTableView.scrollRowToVisible(self._defaultItems.count-1)
  121. self.addButton.isEnabled = false
  122. } else {
  123. }
  124. }
  125. }
  126. @IBAction @objc func removeButtonAction(_ sender: NSButton) {
  127. let selectedRowIndexes = self.defaultItemsTableView.selectedRowIndexes
  128. if (selectedRowIndexes.count <= 0) {
  129. return
  130. }
  131. for index in selectedRowIndexes {
  132. if index < self._defaultItems.count {
  133. self._defaultItems.remove(at: index)
  134. }
  135. }
  136. self.allowedItemsTableView.reloadData()
  137. self.defaultItemsTableView.reloadData()
  138. self.removeButton.isEnabled = false
  139. }
  140. @objc func delete(_ sender: Any?) {
  141. if self.defaultItemsTableView.isEqual(to: self.view.window?.firstResponder) {
  142. self.removeButtonAction(self.removeButton)
  143. }
  144. }
  145. }
  146. extension KMToolbarCustomViewController: NSTableViewDelegate, NSTableViewDataSource {
  147. func numberOfRows(in tableView: NSTableView) -> Int {
  148. var rows = 0
  149. if (self.allowedItemsTableView.isEqual(to: tableView)) {
  150. rows = self._allowedItems.count
  151. } else if (self.defaultItemsTableView.isEqual(to: tableView)) {
  152. rows = self._defaultItems.count
  153. }
  154. return rows
  155. }
  156. func tableView(_ tableView: NSTableView, viewFor tableColumn: NSTableColumn?, row: Int) -> NSView? {
  157. var cellView: NSTableCellView?
  158. var itemIdentifier: String?
  159. if (self.allowedItemsTableView == tableView) {
  160. cellView = tableView.makeView(withIdentifier: NSUserInterfaceItemIdentifier(rawValue: "KMAllowedItemsCell"), owner: self) as! NSTableCellView
  161. itemIdentifier = self._allowedItems[row]
  162. } else if (self.defaultItemsTableView == tableView) {
  163. cellView = tableView.makeView(withIdentifier: NSUserInterfaceItemIdentifier(rawValue: "KMDefaultItemsCell"), owner: self) as! NSTableCellView
  164. itemIdentifier = self._defaultItems[row]
  165. }
  166. if itemIdentifier == KMToolbarSpaceItemIdentifier || itemIdentifier == KMNewToolbarSpaceItemIdentifier {
  167. cellView?.textField?.stringValue = KMLocalizedString("", nil)
  168. cellView?.imageView?.image = NSImage(named: KMImageNameToolbarSpace)
  169. } else {
  170. if let data = self.toolbar?.delegate?.toolbar?(self.toolbar!, itemFor: itemIdentifier!) {
  171. if itemIdentifier == KMToolbarFormAlignmentIdentifier {
  172. cellView?.textField?.stringValue = KMLocalizedString("Alignment", nil)
  173. } else {
  174. cellView?.textField?.stringValue = data.titleName ?? ""
  175. }
  176. cellView?.imageView?.image = data.image
  177. }
  178. }
  179. if let data = cellView?.isKind(of: KMToolbarCellView.self), data {
  180. if self._defaultItems.contains(itemIdentifier!) && itemIdentifier != KMToolbarSpaceItemIdentifier {
  181. (cellView as? KMToolbarCellView)?.tickImageView.image = NSImage(named: "KMImageNameToolbarSelected")
  182. } else {
  183. (cellView as? KMToolbarCellView)?.tickImageView.image = nil
  184. }
  185. }
  186. return cellView;
  187. }
  188. func tableView(_ tableView: NSTableView, shouldSelectRow row: Int) -> Bool {
  189. if (self.allowedItemsTableView == tableView) {
  190. let itemIdentifier = self._allowedItems[row]
  191. if (self._defaultItems.contains(itemIdentifier) && itemIdentifier != KMToolbarSpaceItemIdentifier) {
  192. return false
  193. }
  194. }
  195. return true
  196. }
  197. func tableViewSelectionDidChange(_ notification: Notification) {
  198. self.allowedItemsTableView.delegate = nil
  199. self.defaultItemsTableView.delegate = nil
  200. let tableView = notification.object as? NSTableView
  201. if (self.allowedItemsTableView == tableView) {
  202. self.addButton.isEnabled = tableView!.selectedRowIndexes.count > 0 ? true : false
  203. let set = NSIndexSet()
  204. self.defaultItemsTableView.selectRowIndexes(set as IndexSet, byExtendingSelection: false)
  205. self.removeButton.isEnabled = false
  206. } else if (self.defaultItemsTableView == tableView) {
  207. self.removeButton.isEnabled = tableView!.selectedRowIndexes.count > 0 ? true : false
  208. let set = NSIndexSet()
  209. self.allowedItemsTableView.selectRowIndexes(set as IndexSet, byExtendingSelection: false)
  210. self.addButton.isEnabled = false
  211. }
  212. self.allowedItemsTableView.delegate = self
  213. self.defaultItemsTableView.delegate = self
  214. }
  215. func tableView(_ tableView: NSTableView, writeRowsWith rowIndexes: IndexSet, to pboard: NSPasteboard) -> Bool {
  216. let indexSetData = NSKeyedArchiver.archivedData(withRootObject: rowIndexes)
  217. if (self.allowedItemsTableView == tableView) {
  218. pboard.declareTypes([KMPasteboardTypeAllowedIndexSet], owner: self)
  219. pboard.setData(indexSetData, forType: KMPasteboardTypeAllowedIndexSet)
  220. } else if (self.defaultItemsTableView == tableView) {
  221. pboard.declareTypes([KMPasteboardTypeDefaultIndexSet], owner: self)
  222. pboard.setData(indexSetData, forType: KMPasteboardTypeDefaultIndexSet)
  223. }
  224. return true
  225. }
  226. func tableView(_ tableView: NSTableView, validateDrop info: NSDraggingInfo, proposedRow row: Int, proposedDropOperation dropOperation: NSTableView.DropOperation) -> NSDragOperation {
  227. if (self.defaultItemsTableView == tableView) {
  228. if (dropOperation == .on) {
  229. return NSDragOperation(rawValue: 0)
  230. }
  231. return .move
  232. }
  233. return NSDragOperation(rawValue: 0)
  234. }
  235. func tableView(_ tableView: NSTableView, acceptDrop info: NSDraggingInfo, row: Int, dropOperation: NSTableView.DropOperation) -> Bool {
  236. if (self.defaultItemsTableView == tableView) {
  237. let pboard = info.draggingPasteboard
  238. if pboard.availableType(from: [KMPasteboardTypeAllowedIndexSet]) != nil {
  239. let rowData = pboard.data(forType: KMPasteboardTypeAllowedIndexSet)
  240. let rowIndexes: IndexSet = NSKeyedUnarchiver.unarchiveObject(with: rowData!) as! IndexSet
  241. var isAdded = false
  242. for idx in rowIndexes.reversed() {
  243. let itemIdentifier = self._allowedItems[idx]
  244. if self._defaultItems.contains(itemIdentifier) == false || itemIdentifier == KMToolbarSpaceItemIdentifier {
  245. self._defaultItems.insert(itemIdentifier, at: row)
  246. isAdded = true
  247. }
  248. }
  249. if (isAdded) {
  250. self.allowedItemsTableView.reloadData()
  251. self.defaultItemsTableView.reloadData()
  252. return true
  253. } else {
  254. }
  255. } else if pboard.availableType(from: [KMPasteboardTypeDefaultIndexSet]) != nil {
  256. let rowData = pboard.data(forType: KMPasteboardTypeDefaultIndexSet)
  257. let indexes: IndexSet = NSKeyedUnarchiver.unarchiveObject(with: rowData!) as! IndexSet
  258. var items: [String] = []
  259. for item in self._defaultItems {
  260. items.append(item)
  261. }
  262. var moveItems: [String] = []
  263. for index in indexes {
  264. moveItems.append(self._defaultItems[index])
  265. }
  266. var index = 0
  267. for index in indexes {
  268. self._defaultItems.remove(at: index)
  269. }
  270. var _row = row
  271. if (_row > 0) {
  272. var item: String? = items[row-1]
  273. while (moveItems.contains(item ?? "")) {
  274. _row -= 1
  275. if (_row < 0) {
  276. item = nil
  277. break
  278. } else {
  279. item = items[_row]
  280. }
  281. }
  282. if (item != nil) {
  283. index = self._defaultItems.firstIndex(of: item!)!+1
  284. }
  285. }
  286. for i in 0 ..< moveItems.count {
  287. self._defaultItems.insert(moveItems[i], at: index+i)
  288. }
  289. self.allowedItemsTableView.reloadData()
  290. self.defaultItemsTableView.reloadData()
  291. return true
  292. }
  293. }
  294. return false
  295. }
  296. }
  297. extension KMToolbarCustomViewController: NSMenuItemValidation {
  298. func validateMenuItem(_ menuItem: NSMenuItem) -> Bool {
  299. if (menuItem.action == #selector(delete)) {
  300. if (self.defaultItemsTableView != self.view.window!.firstResponder) {
  301. return false
  302. }
  303. }
  304. return true
  305. }
  306. }
  307. // MARK: - Private Methods
  308. extension KMToolbarCustomViewController {
  309. private func _loadAllowedItems() {
  310. if let data = self.toolbar?.delegate?.toolbarAllowedItemIdentifiers?(self.toolbar!) {
  311. self._allowedItems = data
  312. }
  313. let itemArray = [KMToolbarImageToPDFItemIdentifier, KMToolbarRotateRightItemIdentifier, KMToolbarRotateLeftItemIdentifier, KMToolbarPageBreaksItemIdentifier, KMToolbarViewModeItemIdentifier, KMToolbarDisplayModeItemIdentifier, KMToolbarDividerItemIdentifier, KMToolbarShowToolbarItemIdentifier]
  314. for itemStr in itemArray {
  315. if (self._allowedItems.contains(itemStr)) {
  316. self._allowedItems.removeObject(itemStr)
  317. }
  318. }
  319. }
  320. private func _loadDefaultItems() {
  321. var itemIdentifiers: [String]? = self.toolbar?.toolbarIdentifier != nil ? (UserDefaults.standard.object(forKey: self.toolbar!.toolbarIdentifier!) as? [String]) : []
  322. if itemIdentifiers == nil || itemIdentifiers!.count <= 0 {
  323. if let items = self.toolbar?.delegate?.toolbarDefaultItemIdentifiers?(self.toolbar!) {
  324. itemIdentifiers = items
  325. }
  326. }
  327. self._defaultItems.removeAll()
  328. if (itemIdentifiers != nil) {
  329. for itemI in itemIdentifiers! {
  330. if itemI != KMToolbarDividerItemIdentifier {
  331. self._defaultItems.append(itemI)
  332. }
  333. }
  334. } else {
  335. self._defaultItems = []
  336. }
  337. }
  338. }