123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294 |
- //
- // KMToolbarConfigModel.swift
- // PDF Reader Pro
- //
- // Created by tangchao on 2024/5/28.
- //
- import Cocoa
- class KMToolbarConfigModel: NSObject {
- var leftCellIdentifiers: [String]? {
- didSet {
- let ids = self.leftCellIdentifiers ?? []
-
- self.leftWidth = 0
- if ids.isEmpty == false {
- self.leftWidth += self.leftMargin + 5
- }
-
- for itemId in ids {
- let item = KMToolbarItemView(itemIdentifier: itemId)
- self.leftWidth += (item.itemWidth + 5)
- }
- }
- }
-
- var leftCount: Int {
- get {
- return self.leftCellIdentifiers?.count ?? 0
- }
- }
-
- var centerCellIdentifiers: [String]? {
- didSet {
- let ids = self.centerCellIdentifiers ?? []
-
- self.centerWidth = 0
- for itemId in ids {
- let item = KMToolbarItemView(itemIdentifier: itemId)
- self.centerWidth += (item.itemWidth + 5)
- }
- }
- }
-
- var centerCount: Int {
- get {
- return self.centerCellIdentifiers?.count ?? 0
- }
- }
-
- var rightCellIdentifiers: [String]? {
- didSet {
- let ids = self.rightCellIdentifiers ?? []
-
- self.rightWidth = 0
- if ids.isEmpty == false {
- self.rightWidth += self.rightMargin + 5
- }
- for itemId in ids {
- let item = KMToolbarItemView(itemIdentifier: itemId)
- self.rightWidth += (item.itemWidth + 5)
- }
- }
- }
-
- var rightCount: Int {
- get {
- return self.rightCellIdentifiers?.count ?? 0
- }
- }
-
- var defaultCellIdentifiers: [String]?
- var allowedCellIdentifiers: [String]? {
- get {
- let left = self.leftCellIdentifiers ?? []
- let center = self.centerCellIdentifiers ?? []
- let right = self.rightCellIdentifiers ?? []
- return left + center + right
- }
- }
-
- var removedCellIdentifiers: [String]? {
- get {
- let ids = self.defaultCellIdentifiers ?? []
- if ids.isEmpty {
- return [KMToolbarFixedSpaceItemIdentifier]
- }
-
- var removedIds: [String] = []
- for itemId in ids {
- if self.allowedCellIdentifiers?.contains(itemId) == false {
- removedIds.append(itemId)
- }
- }
- removedIds.insert(KMToolbarFixedSpaceItemIdentifier, at: 0)
- return removedIds
- }
- }
-
- var leftWidth: CGFloat = 0
- var centerWidth: CGFloat = 0
- var rightWidth: CGFloat = 0
-
- var leftMargin: CGFloat = 10
- var rightMargin: CGFloat = 10
- var itemH: CGFloat = 48
-
- var contentWidth: CGFloat {
- get {
- return self.leftWidth + self.centerWidth + self.rightWidth
- }
- }
-
- var windowWidth: CGFloat = 1480
-
- var segItemWidth: CGFloat {
- get {
- return max((self.windowWidth - self.contentWidth - 26 * 2) * 0.5, 20)
- }
- }
-
- func isLeft(at idx: Int) -> Bool {
- if self.leftCount <= 0 {
- return false
- }
- let stardIdx = 0
- let endIdx = self.leftCount
- if idx >= stardIdx && idx < endIdx {
- return true
- }
- return false
- }
-
- func isCenter(at idx: Int) -> Bool {
- if self.centerCount <= 0 {
- return false
- }
- let stardIdx = self.leftCount+1
- let endIdx = stardIdx+self.centerCount
- if idx >= stardIdx && idx < endIdx {
- return true
- }
- return false
- }
-
- func isRight(at idx: Int) -> Bool {
- if self.rightCount <= 0 {
- return false
- }
- let stardIdx = self.leftCount+self.centerCount+2
- let endIdx = stardIdx+self.rightCount
- if idx >= stardIdx && idx < endIdx {
- return true
- }
- return false
- }
-
- func isFirstSegI(at idx: Int) -> Bool {
- return idx == self.leftCount
- }
-
- func isSecondSegI(at idx: Int) -> Bool {
- return idx == (self.leftCount+self.centerCount+1)
- }
-
- func isSeg(at idx: Int) -> Bool {
- if self.isFirstSegI(at: idx) || self.isSecondSegI(at: idx) {
- return true
- }
- return false
- }
-
- func findAllowedItem(at idx: Int) -> String? {
- if self.isLeft(at: idx) {
- return self.leftCellIdentifiers?[idx]
- }
- if self.isCenter(at: idx) {
- let theIdx = idx-self.leftCount-1
- return self.centerCellIdentifiers?[theIdx]
- }
- if self.isRight(at: idx) {
- let theIdx = idx-self.leftCount-self.centerCount-2
- return self.rightCellIdentifiers?[theIdx]
- }
- return nil
- }
-
- func removeItem(at idx: Int) -> Bool {
- if self.isLeft(at: idx) {
- self.leftCellIdentifiers?.remove(at: idx)
- return true
- }
- if self.isCenter(at: idx) {
- let theIdx = idx-self.leftCount-1
- self.centerCellIdentifiers?.remove(at: theIdx)
- return true
- }
- if self.isRight(at: idx) {
- let theIdx = idx-self.leftCount-self.centerCount-2
- self.rightCellIdentifiers?.remove(at: theIdx)
- return true
- }
- return false
- }
-
- func addItem(itemId: String, at idx: Int) -> Bool {
- let isFixedSpaceItem = itemId == KMToolbarFixedSpaceItemIdentifier
- if self.isLeft(at: idx) || idx == self.leftCount {
- let contains = self.leftCellIdentifiers?.contains(itemId) ?? false
- if contains == false || isFixedSpaceItem {
- self.leftCellIdentifiers?.insert(itemId, at: idx)
- }
- return true
- }
- if self.isCenter(at: idx) || idx == (self.leftCount+self.centerCount+1) {
- let theIdx = idx-self.leftCount-1
- let contains = self.centerCellIdentifiers?.contains(itemId) ?? false
- if contains == false || isFixedSpaceItem {
- self.centerCellIdentifiers?.insert(itemId, at: theIdx)
- }
- return true
- }
- if self.isRight(at: idx) || idx == (self.leftCount+self.centerCount+self.rightCount+2) {
- let theIdx = idx-self.leftCount-self.centerCount-2
- let contains = self.rightCellIdentifiers?.contains(itemId) ?? false
- if contains == false || isFixedSpaceItem {
- self.rightCellIdentifiers?.insert(itemId, at: theIdx)
- }
- return true
- }
- return false
- }
-
- func moveItem(itemIdx: Int, to idx: Int) -> Bool {
- if self.isLeft(at: itemIdx) { // left
- if self.isLeft(at: idx) { // left -> left [ || idx == self.leftCount]
- self.leftCellIdentifiers?.swapAt(itemIdx, idx)
- return true
- } else if self.isCenter(at: idx) || idx == (self.leftCount+self.centerCount+1) { // left -> center
- if let itemId = self.findAllowedItem(at: itemIdx) {
- _ = self.addItem(itemId: itemId, at: idx)
- self.leftCellIdentifiers?.remove(at: itemIdx)
- return true
- }
-
- } else if self.isRight(at: idx) || idx == self.leftCount+self.centerCount+self.rightCount+2 { // left -> right
- if let itemId = self.findAllowedItem(at: itemIdx) {
- _ = self.addItem(itemId: itemId, at: idx)
- self.leftCellIdentifiers?.remove(at: itemIdx)
- return true
- }
- }
- } else if self.isCenter(at: itemIdx) { // center
- if self.isLeft(at: idx) || idx == self.leftCount { // center -> left
- if let itemId = self.findAllowedItem(at: itemIdx) {
- _ = self.removeItem(at: itemIdx)
- _ = self.addItem(itemId: itemId, at: idx)
- return true
- }
- } else if self.isCenter(at: idx) { // center -> center [|| idx == (self.leftCount+self.centerCount+1]
- let offset = self.leftCount+1
- self.centerCellIdentifiers?.swapAt(itemIdx-offset, idx-offset)
- return true
- } else if self.isRight(at: idx) || idx == self.leftCount+self.centerCount+self.rightCount+2 { // center -> right
- if let itemId = self.findAllowedItem(at: itemIdx) {
- _ = self.addItem(itemId: itemId, at: idx)
- _ = self.removeItem(at: itemIdx)
- return true
- }
- }
- } else if self.isRight(at: itemIdx) { // right
- if self.isLeft(at: idx) || idx == self.leftCount { // right -> left
- if let itemId = self.findAllowedItem(at: itemIdx) {
- _ = self.removeItem(at: itemIdx)
- _ = self.addItem(itemId: itemId, at: idx)
- return true
- }
- } else if self.isCenter(at: idx) || idx == (self.leftCount+self.centerCount+1) { // right -> center
- if let itemId = self.findAllowedItem(at: itemIdx) {
- _ = self.removeItem(at: itemIdx)
- _ = self.addItem(itemId: itemId, at: idx)
- return true
- }
- } else if self.isRight(at: idx) { // right -> right [|| idx == self.leftCount+self.centerCount+self.rightCount+2]
- let offset = self.leftCount+self.centerCount+2
- self.rightCellIdentifiers?.swapAt(itemIdx-offset, idx-offset)
- return true
- }
- }
-
- return false
- }
- }
|