KMToolbarConfigModel.swift 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294
  1. //
  2. // KMToolbarConfigModel.swift
  3. // PDF Reader Pro
  4. //
  5. // Created by tangchao on 2024/5/28.
  6. //
  7. import Cocoa
  8. class KMToolbarConfigModel: NSObject {
  9. var leftCellIdentifiers: [String]? {
  10. didSet {
  11. let ids = self.leftCellIdentifiers ?? []
  12. self.leftWidth = 0
  13. if ids.isEmpty == false {
  14. self.leftWidth += self.leftMargin + 5
  15. }
  16. for itemId in ids {
  17. let item = KMToolbarItemView(itemIdentifier: itemId)
  18. self.leftWidth += (item.itemWidth + 5)
  19. }
  20. }
  21. }
  22. var leftCount: Int {
  23. get {
  24. return self.leftCellIdentifiers?.count ?? 0
  25. }
  26. }
  27. var centerCellIdentifiers: [String]? {
  28. didSet {
  29. let ids = self.centerCellIdentifiers ?? []
  30. self.centerWidth = 0
  31. for itemId in ids {
  32. let item = KMToolbarItemView(itemIdentifier: itemId)
  33. self.centerWidth += (item.itemWidth + 5)
  34. }
  35. }
  36. }
  37. var centerCount: Int {
  38. get {
  39. return self.centerCellIdentifiers?.count ?? 0
  40. }
  41. }
  42. var rightCellIdentifiers: [String]? {
  43. didSet {
  44. let ids = self.rightCellIdentifiers ?? []
  45. self.rightWidth = 0
  46. if ids.isEmpty == false {
  47. self.rightWidth += self.rightMargin + 5
  48. }
  49. for itemId in ids {
  50. let item = KMToolbarItemView(itemIdentifier: itemId)
  51. self.rightWidth += (item.itemWidth + 5)
  52. }
  53. }
  54. }
  55. var rightCount: Int {
  56. get {
  57. return self.rightCellIdentifiers?.count ?? 0
  58. }
  59. }
  60. var defaultCellIdentifiers: [String]?
  61. var allowedCellIdentifiers: [String]? {
  62. get {
  63. let left = self.leftCellIdentifiers ?? []
  64. let center = self.centerCellIdentifiers ?? []
  65. let right = self.rightCellIdentifiers ?? []
  66. return left + center + right
  67. }
  68. }
  69. var removedCellIdentifiers: [String]? {
  70. get {
  71. let ids = self.defaultCellIdentifiers ?? []
  72. if ids.isEmpty {
  73. return [KMToolbarFixedSpaceItemIdentifier]
  74. }
  75. var removedIds: [String] = []
  76. for itemId in ids {
  77. if self.allowedCellIdentifiers?.contains(itemId) == false {
  78. removedIds.append(itemId)
  79. }
  80. }
  81. removedIds.insert(KMToolbarFixedSpaceItemIdentifier, at: 0)
  82. return removedIds
  83. }
  84. }
  85. var leftWidth: CGFloat = 0
  86. var centerWidth: CGFloat = 0
  87. var rightWidth: CGFloat = 0
  88. var leftMargin: CGFloat = 10
  89. var rightMargin: CGFloat = 10
  90. var itemH: CGFloat = 48
  91. var contentWidth: CGFloat {
  92. get {
  93. return self.leftWidth + self.centerWidth + self.rightWidth
  94. }
  95. }
  96. var windowWidth: CGFloat = 1480
  97. var segItemWidth: CGFloat {
  98. get {
  99. return max((self.windowWidth - self.contentWidth - 26 * 2) * 0.5, 20)
  100. }
  101. }
  102. func isLeft(at idx: Int) -> Bool {
  103. if self.leftCount <= 0 {
  104. return false
  105. }
  106. let stardIdx = 0
  107. let endIdx = self.leftCount
  108. if idx >= stardIdx && idx < endIdx {
  109. return true
  110. }
  111. return false
  112. }
  113. func isCenter(at idx: Int) -> Bool {
  114. if self.centerCount <= 0 {
  115. return false
  116. }
  117. let stardIdx = self.leftCount+1
  118. let endIdx = stardIdx+self.centerCount
  119. if idx >= stardIdx && idx < endIdx {
  120. return true
  121. }
  122. return false
  123. }
  124. func isRight(at idx: Int) -> Bool {
  125. if self.rightCount <= 0 {
  126. return false
  127. }
  128. let stardIdx = self.leftCount+self.centerCount+2
  129. let endIdx = stardIdx+self.rightCount
  130. if idx >= stardIdx && idx < endIdx {
  131. return true
  132. }
  133. return false
  134. }
  135. func isFirstSegI(at idx: Int) -> Bool {
  136. return idx == self.leftCount
  137. }
  138. func isSecondSegI(at idx: Int) -> Bool {
  139. return idx == (self.leftCount+self.centerCount+1)
  140. }
  141. func isSeg(at idx: Int) -> Bool {
  142. if self.isFirstSegI(at: idx) || self.isSecondSegI(at: idx) {
  143. return true
  144. }
  145. return false
  146. }
  147. func findAllowedItem(at idx: Int) -> String? {
  148. if self.isLeft(at: idx) {
  149. return self.leftCellIdentifiers?[idx]
  150. }
  151. if self.isCenter(at: idx) {
  152. let theIdx = idx-self.leftCount-1
  153. return self.centerCellIdentifiers?[theIdx]
  154. }
  155. if self.isRight(at: idx) {
  156. let theIdx = idx-self.leftCount-self.centerCount-2
  157. return self.rightCellIdentifiers?[theIdx]
  158. }
  159. return nil
  160. }
  161. func removeItem(at idx: Int) -> Bool {
  162. if self.isLeft(at: idx) {
  163. self.leftCellIdentifiers?.remove(at: idx)
  164. return true
  165. }
  166. if self.isCenter(at: idx) {
  167. let theIdx = idx-self.leftCount-1
  168. self.centerCellIdentifiers?.remove(at: theIdx)
  169. return true
  170. }
  171. if self.isRight(at: idx) {
  172. let theIdx = idx-self.leftCount-self.centerCount-2
  173. self.rightCellIdentifiers?.remove(at: theIdx)
  174. return true
  175. }
  176. return false
  177. }
  178. func addItem(itemId: String, at idx: Int) -> Bool {
  179. let isFixedSpaceItem = itemId == KMToolbarFixedSpaceItemIdentifier
  180. if self.isLeft(at: idx) || idx == self.leftCount {
  181. let contains = self.leftCellIdentifiers?.contains(itemId) ?? false
  182. if contains == false || isFixedSpaceItem {
  183. self.leftCellIdentifiers?.insert(itemId, at: idx)
  184. }
  185. return true
  186. }
  187. if self.isCenter(at: idx) || idx == (self.leftCount+self.centerCount+1) {
  188. let theIdx = idx-self.leftCount-1
  189. let contains = self.centerCellIdentifiers?.contains(itemId) ?? false
  190. if contains == false || isFixedSpaceItem {
  191. self.centerCellIdentifiers?.insert(itemId, at: theIdx)
  192. }
  193. return true
  194. }
  195. if self.isRight(at: idx) || idx == (self.leftCount+self.centerCount+self.rightCount+2) {
  196. let theIdx = idx-self.leftCount-self.centerCount-2
  197. let contains = self.rightCellIdentifiers?.contains(itemId) ?? false
  198. if contains == false || isFixedSpaceItem {
  199. self.rightCellIdentifiers?.insert(itemId, at: theIdx)
  200. }
  201. return true
  202. }
  203. return false
  204. }
  205. func moveItem(itemIdx: Int, to idx: Int) -> Bool {
  206. if self.isLeft(at: itemIdx) { // left
  207. if self.isLeft(at: idx) { // left -> left [ || idx == self.leftCount]
  208. self.leftCellIdentifiers?.swapAt(itemIdx, idx)
  209. return true
  210. } else if self.isCenter(at: idx) || idx == (self.leftCount+self.centerCount+1) { // left -> center
  211. if let itemId = self.findAllowedItem(at: itemIdx) {
  212. _ = self.addItem(itemId: itemId, at: idx)
  213. self.leftCellIdentifiers?.remove(at: itemIdx)
  214. return true
  215. }
  216. } else if self.isRight(at: idx) || idx == self.leftCount+self.centerCount+self.rightCount+2 { // left -> right
  217. if let itemId = self.findAllowedItem(at: itemIdx) {
  218. _ = self.addItem(itemId: itemId, at: idx)
  219. self.leftCellIdentifiers?.remove(at: itemIdx)
  220. return true
  221. }
  222. }
  223. } else if self.isCenter(at: itemIdx) { // center
  224. if self.isLeft(at: idx) || idx == self.leftCount { // center -> left
  225. if let itemId = self.findAllowedItem(at: itemIdx) {
  226. _ = self.removeItem(at: itemIdx)
  227. _ = self.addItem(itemId: itemId, at: idx)
  228. return true
  229. }
  230. } else if self.isCenter(at: idx) { // center -> center [|| idx == (self.leftCount+self.centerCount+1]
  231. let offset = self.leftCount+1
  232. self.centerCellIdentifiers?.swapAt(itemIdx-offset, idx-offset)
  233. return true
  234. } else if self.isRight(at: idx) || idx == self.leftCount+self.centerCount+self.rightCount+2 { // center -> right
  235. if let itemId = self.findAllowedItem(at: itemIdx) {
  236. _ = self.addItem(itemId: itemId, at: idx)
  237. _ = self.removeItem(at: itemIdx)
  238. return true
  239. }
  240. }
  241. } else if self.isRight(at: itemIdx) { // right
  242. if self.isLeft(at: idx) || idx == self.leftCount { // right -> left
  243. if let itemId = self.findAllowedItem(at: itemIdx) {
  244. _ = self.removeItem(at: itemIdx)
  245. _ = self.addItem(itemId: itemId, at: idx)
  246. return true
  247. }
  248. } else if self.isCenter(at: idx) || idx == (self.leftCount+self.centerCount+1) { // right -> center
  249. if let itemId = self.findAllowedItem(at: itemIdx) {
  250. _ = self.removeItem(at: itemIdx)
  251. _ = self.addItem(itemId: itemId, at: idx)
  252. return true
  253. }
  254. } else if self.isRight(at: idx) { // right -> right [|| idx == self.leftCount+self.centerCount+self.rightCount+2]
  255. let offset = self.leftCount+self.centerCount+2
  256. self.rightCellIdentifiers?.swapAt(itemIdx-offset, idx-offset)
  257. return true
  258. }
  259. }
  260. return false
  261. }
  262. }