KMToolbarPreviousNextItemView.swift 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394
  1. //
  2. // KMToolbarPreviousNextItemView.swift
  3. // PDF Reader Pro
  4. //
  5. // Created by tangchao on 2023/12/15.
  6. //
  7. import Cocoa
  8. private func _KMPreviousNextString() -> String {
  9. return NSLocalizedString("Previous/Next", comment: "")
  10. }
  11. private let _minWidth: CGFloat = 24 * 2
  12. class KMToolbarPreviousNextItemView: NSView {
  13. var callback: KMCommonClickBlock?
  14. lazy var previousButton: KMCoverButton = {
  15. let button = KMCoverButton()
  16. button.wantsLayer = true
  17. button.isBordered = false
  18. button.layer?.cornerRadius = 6
  19. button.image = NSImage(named: "KMImageNameToolbarPagepreviousNor")
  20. button.toolTip = NSLocalizedString("Go To Previous Page", comment: "")
  21. button.imagePosition = .imageOnly
  22. button.target = self
  23. button.action = #selector(buttonClicked)
  24. button.coverAction = { cbtn, caction in
  25. if caction == .enter {
  26. cbtn.layer?.backgroundColor = KMToolbarItemView.selectedBackgroundColor.cgColor
  27. } else if caction == .exit {
  28. cbtn.layer?.backgroundColor = KMToolbarItemView.normalBackgroundColor.cgColor
  29. }
  30. }
  31. return button
  32. }()
  33. lazy var nextButton: KMCoverButton = {
  34. let button = KMCoverButton()
  35. button.wantsLayer = true
  36. button.isBordered = false
  37. button.layer?.cornerRadius = 6
  38. button.image = NSImage(named: "KMImageNameToolbarPagenextNor")
  39. button.toolTip = NSLocalizedString("Go To Next Page", comment: "")
  40. button.imagePosition = .imageOnly
  41. button.target = self
  42. button.action = #selector(buttonClicked)
  43. button.coverAction = { cbtn, caction in
  44. if caction == .enter {
  45. cbtn.layer?.backgroundColor = KMToolbarItemView.selectedBackgroundColor.cgColor
  46. } else if caction == .exit {
  47. cbtn.layer?.backgroundColor = KMToolbarItemView.normalBackgroundColor.cgColor
  48. }
  49. }
  50. return button
  51. }()
  52. lazy var titleLabel: NSTextField = {
  53. let label = NSTextField(labelWithString: _KMPreviousNextString())
  54. label.font = KMToolbarMainItemView.textFont
  55. label.textColor = KMToolbarMainItemView.fetchTextNormalColor()
  56. return label
  57. }()
  58. convenience init() {
  59. self.init(frame: NSMakeRect(0, 0, Self.itemWidth, 40))
  60. }
  61. override init(frame frameRect: NSRect) {
  62. super.init(frame: frameRect)
  63. self.initSubview()
  64. }
  65. required init?(coder: NSCoder) {
  66. super.init(coder: coder)
  67. self.initSubview()
  68. }
  69. func initSubview() {
  70. self.addSubview(self.previousButton)
  71. self.addSubview(self.nextButton)
  72. self.addSubview(self.titleLabel)
  73. self.previousButton.km_add_left_constraint()
  74. self.previousButton.km_add_right_constraint(equalTo: self.nextButton, attribute: .left)
  75. self.previousButton.km_add_top_constraint()
  76. self.previousButton.km_add_height_constraint(constant: 24)
  77. self.nextButton.km_add_right_constraint()
  78. self.nextButton.km_add_width_constraint(equalTo: self.previousButton, attribute: .width)
  79. self.nextButton.km_add_top_constraint()
  80. self.nextButton.km_add_height_constraint(constant: 24)
  81. self.titleLabel.km_add_bottom_constraint()
  82. self.titleLabel.km_add_height_constraint(constant: 14)
  83. self.titleLabel.km_add_left_constraint()
  84. self.titleLabel.km_add_right_constraint()
  85. self.titleLabel.km_add_width_constraint(constant: Self.itemWidth)
  86. // self.titleLabel.km_add_top_constraint(equalTo: self.previousButton, attribute: .bottom)
  87. }
  88. class var itemWidth: CGFloat {
  89. get {
  90. let string = _KMPreviousNextString()
  91. let width = string.boundingRect(with: NSSize(width: CGFloat.greatestFiniteMagnitude, height: 20), options: [.usesLineFragmentOrigin, .truncatesLastVisibleLine], attributes: [.font : KMToolbarMainItemView.textFont]).size.width + 5 * 2
  92. if width < _minWidth {
  93. return _minWidth
  94. }
  95. return width
  96. }
  97. }
  98. override func draw(_ dirtyRect: NSRect) {
  99. super.draw(dirtyRect)
  100. // Drawing code here.
  101. }
  102. @objc func buttonClicked(_ sender: NSButton) {
  103. guard let block = self.callback else {
  104. return
  105. }
  106. if self.previousButton.isEqual(to: sender) {
  107. block(1)
  108. } else if self.nextButton.isEqual(to: sender) {
  109. block(2)
  110. }
  111. }
  112. }
  113. private func _KMPreviousBackString() -> String {
  114. return "\(NSLocalizedString("Forward", comment: ""))/\(NSLocalizedString("Back", comment: ""))"
  115. }
  116. //private let _minWidth: CGFloat = 24 * 2
  117. class KMToolbarPreviousBackItemView: NSView {
  118. var callback: KMCommonClickBlock?
  119. lazy var previousButton: KMCoverButton = {
  120. let button = KMCoverButton()
  121. button.wantsLayer = true
  122. button.isBordered = false
  123. button.layer?.cornerRadius = 6
  124. button.image = NSImage(named: "KMImageNameToolbarForwardNor")
  125. button.toolTip = NSLocalizedString("Go To Previous Page", comment: "")
  126. button.imagePosition = .imageOnly
  127. button.target = self
  128. button.action = #selector(buttonClicked)
  129. button.coverAction = { cbtn, caction in
  130. if caction == .enter {
  131. cbtn.layer?.backgroundColor = KMToolbarItemView.selectedBackgroundColor.cgColor
  132. } else if caction == .exit {
  133. cbtn.layer?.backgroundColor = KMToolbarItemView.normalBackgroundColor.cgColor
  134. }
  135. }
  136. return button
  137. }()
  138. lazy var nextButton: KMCoverButton = {
  139. let button = KMCoverButton()
  140. button.wantsLayer = true
  141. button.isBordered = false
  142. button.layer?.cornerRadius = 6
  143. button.image = NSImage(named: "KMImageNameToolbarBackwardNor")
  144. button.toolTip = NSLocalizedString("Go To Next Page", comment: "")
  145. button.imagePosition = .imageOnly
  146. button.target = self
  147. button.action = #selector(buttonClicked)
  148. button.coverAction = { cbtn, caction in
  149. if caction == .enter {
  150. cbtn.layer?.backgroundColor = KMToolbarItemView.selectedBackgroundColor.cgColor
  151. } else if caction == .exit {
  152. cbtn.layer?.backgroundColor = KMToolbarItemView.normalBackgroundColor.cgColor
  153. }
  154. }
  155. return button
  156. }()
  157. lazy var titleLabel: NSTextField = {
  158. let label = NSTextField(labelWithString: _KMPreviousBackString())
  159. label.font = KMToolbarMainItemView.textFont
  160. label.textColor = KMToolbarMainItemView.fetchTextNormalColor()
  161. return label
  162. }()
  163. convenience init() {
  164. self.init(frame: NSMakeRect(0, 0, Self.itemWidth, 40))
  165. }
  166. override init(frame frameRect: NSRect) {
  167. super.init(frame: frameRect)
  168. self.initSubview()
  169. }
  170. required init?(coder: NSCoder) {
  171. super.init(coder: coder)
  172. self.initSubview()
  173. }
  174. func initSubview() {
  175. self.addSubview(self.previousButton)
  176. self.addSubview(self.nextButton)
  177. self.addSubview(self.titleLabel)
  178. self.previousButton.km_add_left_constraint()
  179. self.previousButton.km_add_right_constraint(equalTo: self.nextButton, attribute: .left)
  180. self.previousButton.km_add_top_constraint()
  181. self.previousButton.km_add_height_constraint(constant: 24)
  182. self.nextButton.km_add_right_constraint()
  183. self.nextButton.km_add_width_constraint(equalTo: self.previousButton, attribute: .width)
  184. self.nextButton.km_add_top_constraint()
  185. self.nextButton.km_add_height_constraint(constant: 24)
  186. self.titleLabel.km_add_bottom_constraint()
  187. self.titleLabel.km_add_height_constraint(constant: 14)
  188. self.titleLabel.km_add_left_constraint()
  189. self.titleLabel.km_add_right_constraint()
  190. self.titleLabel.km_add_width_constraint(constant: Self.itemWidth)
  191. // self.titleLabel.km_add_top_constraint(equalTo: self.previousButton, attribute: .bottom)
  192. }
  193. class var itemWidth: CGFloat {
  194. get {
  195. let string = _KMPreviousBackString()
  196. let width = string.boundingRect(with: NSSize(width: CGFloat.greatestFiniteMagnitude, height: 20), options: [.usesLineFragmentOrigin, .truncatesLastVisibleLine], attributes: [.font : KMToolbarMainItemView.textFont]).size.width + 5 * 2
  197. if width < _minWidth {
  198. return _minWidth
  199. }
  200. return width
  201. }
  202. }
  203. override func draw(_ dirtyRect: NSRect) {
  204. super.draw(dirtyRect)
  205. // Drawing code here.
  206. }
  207. @objc func buttonClicked(_ sender: NSButton) {
  208. guard let block = self.callback else {
  209. return
  210. }
  211. if self.previousButton.isEqual(to: sender) {
  212. block(1)
  213. } else if self.nextButton.isEqual(to: sender) {
  214. block(2)
  215. }
  216. }
  217. }
  218. private func _KMFirstLastString() -> String {
  219. return "\(NSLocalizedString("First", comment: ""))/\(NSLocalizedString("Last", comment: ""))"
  220. }
  221. //private let _minWidth: CGFloat = 24 * 2
  222. class KMToolbarFirstLastItemView: NSView {
  223. var callback: KMCommonClickBlock?
  224. lazy var previousButton: KMCoverButton = {
  225. let button = KMCoverButton()
  226. button.wantsLayer = true
  227. button.isBordered = false
  228. button.layer?.cornerRadius = 6
  229. button.image = NSImage(named: "KMImageNameToolbarFirstpageNor")
  230. button.toolTip = NSLocalizedString("Go To Previous Page", comment: "")
  231. button.imagePosition = .imageOnly
  232. button.target = self
  233. button.action = #selector(buttonClicked)
  234. button.coverAction = { cbtn, caction in
  235. if caction == .enter {
  236. cbtn.layer?.backgroundColor = KMToolbarItemView.selectedBackgroundColor.cgColor
  237. } else if caction == .exit {
  238. cbtn.layer?.backgroundColor = KMToolbarItemView.normalBackgroundColor.cgColor
  239. }
  240. }
  241. return button
  242. }()
  243. lazy var nextButton: KMCoverButton = {
  244. let button = KMCoverButton()
  245. button.wantsLayer = true
  246. button.isBordered = false
  247. button.layer?.cornerRadius = 6
  248. button.image = NSImage(named: "KMImageNameToolbarLastpageNor")
  249. button.toolTip = NSLocalizedString("Go To Next Page", comment: "")
  250. button.imagePosition = .imageOnly
  251. button.target = self
  252. button.action = #selector(buttonClicked)
  253. button.coverAction = { cbtn, caction in
  254. if caction == .enter {
  255. cbtn.layer?.backgroundColor = KMToolbarItemView.selectedBackgroundColor.cgColor
  256. } else if caction == .exit {
  257. cbtn.layer?.backgroundColor = KMToolbarItemView.normalBackgroundColor.cgColor
  258. }
  259. }
  260. return button
  261. }()
  262. lazy var titleLabel: NSTextField = {
  263. let label = NSTextField(labelWithString: _KMFirstLastString())
  264. label.font = KMToolbarMainItemView.textFont
  265. label.textColor = KMToolbarMainItemView.fetchTextNormalColor()
  266. return label
  267. }()
  268. convenience init() {
  269. self.init(frame: NSMakeRect(0, 0, Self.itemWidth, 40))
  270. }
  271. override init(frame frameRect: NSRect) {
  272. super.init(frame: frameRect)
  273. self.initSubview()
  274. }
  275. required init?(coder: NSCoder) {
  276. super.init(coder: coder)
  277. self.initSubview()
  278. }
  279. func initSubview() {
  280. self.addSubview(self.previousButton)
  281. self.addSubview(self.nextButton)
  282. self.addSubview(self.titleLabel)
  283. self.previousButton.km_add_left_constraint()
  284. self.previousButton.km_add_right_constraint(equalTo: self.nextButton, attribute: .left)
  285. self.previousButton.km_add_top_constraint()
  286. self.previousButton.km_add_height_constraint(constant: 24)
  287. self.nextButton.km_add_right_constraint()
  288. self.nextButton.km_add_width_constraint(equalTo: self.previousButton, attribute: .width)
  289. self.nextButton.km_add_top_constraint()
  290. self.nextButton.km_add_height_constraint(constant: 24)
  291. self.titleLabel.km_add_bottom_constraint()
  292. self.titleLabel.km_add_height_constraint(constant: 14)
  293. self.titleLabel.km_add_left_constraint()
  294. self.titleLabel.km_add_right_constraint()
  295. self.titleLabel.km_add_width_constraint(constant: Self.itemWidth)
  296. // self.titleLabel.km_add_top_constraint(equalTo: self.previousButton, attribute: .bottom)
  297. }
  298. class var itemWidth: CGFloat {
  299. get {
  300. let string = _KMFirstLastString()
  301. let width = string.boundingRect(with: NSSize(width: CGFloat.greatestFiniteMagnitude, height: 20), options: [.usesLineFragmentOrigin, .truncatesLastVisibleLine], attributes: [.font : KMToolbarMainItemView.textFont]).size.width + 5 * 2
  302. if width < _minWidth {
  303. return _minWidth
  304. }
  305. return width
  306. }
  307. }
  308. override func draw(_ dirtyRect: NSRect) {
  309. super.draw(dirtyRect)
  310. // Drawing code here.
  311. }
  312. @objc func buttonClicked(_ sender: NSButton) {
  313. guard let block = self.callback else {
  314. return
  315. }
  316. if self.previousButton.isEqual(to: sender) {
  317. block(1)
  318. } else if self.nextButton.isEqual(to: sender) {
  319. block(2)
  320. }
  321. }
  322. }