KMPageEditInsertDirectionItemView.swift 2.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  1. //
  2. // KMPageEditInsertDirectionItemView.swift
  3. // PDF Master
  4. //
  5. // Created by tangchao on 2023/1/11.
  6. //
  7. import Cocoa
  8. class KMPageEditInsertDirectionItemView: KMPageEditBaseItemView {
  9. private lazy var horRadio: NSButton = {
  10. let button = NSButton(radioButtonWithTitle: NSLocalizedString("Horizontal page", comment: ""), target: self, action: #selector(radioButtonAction))
  11. return button
  12. }()
  13. private lazy var verRadio: NSButton = {
  14. let button = NSButton(radioButtonWithTitle: NSLocalizedString("Vertical pages", comment: ""), target: self, action: #selector(radioButtonAction))
  15. return button
  16. }()
  17. override func initSubviews() {
  18. super.initSubviews()
  19. self.addSubview(self.horRadio)
  20. self.addSubview(self.verRadio)
  21. self.titleLabel.stringValue = NSLocalizedString("Direction", comment: "")
  22. self.horRadio.state = .off
  23. self.verRadio.state = .on
  24. }
  25. override func layout() {
  26. super.layout()
  27. let raidoY: CGFloat = self.titleLabel.frame.maxY+12
  28. let radioSize = NSSize(width: 120, height: 22)
  29. self.verRadio.frame = NSMakeRect(self.contentInset.left, raidoY, radioSize.width, radioSize.height)
  30. self.horRadio.frame = NSMakeRect(self.horRadio.frame.maxX,raidoY , radioSize.width, radioSize.height)
  31. }
  32. @objc func radioButtonAction(sender: NSButton) {
  33. for radio in [self.horRadio, self.verRadio] {
  34. if (radio.isEqual(to: sender)) {
  35. radio.state = .on
  36. } else {
  37. radio.state = .off
  38. }
  39. }
  40. guard let callback = self.itemClick else {
  41. return
  42. }
  43. var index: Int = 0
  44. if (sender == self.horRadio) {
  45. index = 1
  46. } else if (sender == self.verRadio) {
  47. index = 2
  48. }
  49. if (index > 0) {
  50. callback(index, "")
  51. }
  52. }
  53. public func switchDirection(isHor: Bool) {
  54. for radio in [self.horRadio, self.verRadio] {
  55. radio.state = .off
  56. }
  57. if (isHor) {
  58. self.horRadio.state = .on
  59. } else {
  60. self.verRadio.state = .on
  61. }
  62. }
  63. public func getDirection() -> Int {
  64. if (self.horRadio.state == .on) {
  65. return 1
  66. }
  67. return 0
  68. }
  69. }