CDistanceMeasureInfoWindowController.swift 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. //
  2. // CDistanceMeasureInfoWindowController.swift
  3. // Cisdem PDFMaster
  4. //
  5. // Created by wanjun on 2024/7/16.
  6. //
  7. import Cocoa
  8. protocol CDistanceMeasureInfoWindowControllerDelegate: AnyObject {
  9. func distanceMeasureInfoWindowControllerSetting(_ distanceMeasureInfoWindowController: CDistanceMeasureInfoWindowController)
  10. func cancelMeasureInfoWindowControllerSetting(_ distanceMeasureInfoWindowController: CDistanceMeasureInfoWindowController)
  11. }
  12. @objcMembers
  13. class CDistanceMeasureInfoWindowController: NSWindowController {
  14. @IBOutlet weak var titleLabel: NSTextField!
  15. @IBOutlet weak var scaleTitleLabel: NSTextField!
  16. @IBOutlet weak var lengthTitleLabel: NSTextField!
  17. @IBOutlet weak var precisionTitleLabel: NSTextField!
  18. @IBOutlet weak var angleTitleLabel: NSTextField!
  19. @IBOutlet weak var xTitleLabel: NSTextField!
  20. @IBOutlet weak var yTitleLabel: NSTextField!
  21. @IBOutlet weak var scaleLabel: NSTextField!
  22. @IBOutlet weak var lengthLabel: NSTextField!
  23. @IBOutlet weak var precisionLabel: NSTextField!
  24. @IBOutlet weak var angleLabel: NSTextField!
  25. @IBOutlet weak var xLabel: NSTextField!
  26. @IBOutlet weak var yLabel: NSTextField!
  27. @IBOutlet weak var cancelButton: NSButton!
  28. @IBOutlet weak var settingsButton: NSButton!
  29. weak var delegate: CDistanceMeasureInfoWindowControllerDelegate?
  30. var measureInfo: CPDFDistanceMeasureInfo = CPDFDistanceMeasureInfo()
  31. convenience init() {
  32. self.init(windowNibName: "CDistanceMeasureInfoWindowController")
  33. }
  34. override func windowDidLoad() {
  35. super.windowDidLoad()
  36. cancelButton.stringValue = NSLocalizedString("Cancel", comment: "")
  37. self.window?.level = .floating
  38. commonInit()
  39. configurationLanguage()
  40. }
  41. private func configurationLanguage() {
  42. titleLabel.stringValue = NSLocalizedString("Distance Measurement", comment: "")
  43. scaleTitleLabel.stringValue = NSLocalizedString("Scale:", comment: "")
  44. lengthTitleLabel.stringValue = NSLocalizedString("Distance", comment: "") + ":"
  45. precisionTitleLabel.stringValue = NSLocalizedString("Precision", comment: "") + ":"
  46. angleTitleLabel.stringValue = NSLocalizedString("Angle", comment: "") + ":"
  47. xTitleLabel.stringValue = NSLocalizedString("X", comment: "") + ":"
  48. yTitleLabel.stringValue = NSLocalizedString("Y", comment: "") + ""
  49. settingsButton.title = NSLocalizedString("Settings", comment: "")
  50. }
  51. func hideFloatingWindow() {
  52. self.window?.orderOut(nil)
  53. }
  54. func reloadData(with measureInfo: CPDFDistanceMeasureInfo) {
  55. self.measureInfo = measureInfo
  56. updateLabels()
  57. }
  58. func clearData() {
  59. self.lengthLabel.stringValue = ""
  60. self.angleLabel.stringValue = ""
  61. self.xLabel.stringValue = ""
  62. self.yLabel.stringValue = ""
  63. self.measureInfo = CPDFDistanceMeasureInfo()
  64. updateLabels()
  65. }
  66. private func commonInit() {
  67. updateLabels()
  68. }
  69. private func updateLabels() {
  70. scaleLabel.stringValue = String(format: "%0.0f %@ = %0.0f %@", measureInfo.rulerBase, measureInfo.rulerBaseUnit, measureInfo.rulerTranslate, measureInfo.rulerTranslateUnit)
  71. switch measureInfo.precision {
  72. case 1:
  73. precisionLabel.stringValue = "1"
  74. case 10:
  75. precisionLabel.stringValue = "0.1"
  76. case 100:
  77. precisionLabel.stringValue = "0.01"
  78. case 1000:
  79. precisionLabel.stringValue = "0.001"
  80. case 10000:
  81. precisionLabel.stringValue = "0.0001"
  82. default:
  83. precisionLabel.stringValue = ""
  84. }
  85. let formatValue = measureInfo.formatValue
  86. if formatValue.count > 0 {
  87. let range = formatValue.index(formatValue.startIndex, offsetBy: 4)..<formatValue.endIndex
  88. lengthLabel.stringValue = String(formatValue[range])
  89. }
  90. }
  91. // MARK: Action
  92. @IBAction func buttonItemClick_Setting(_ sender: Any) {
  93. delegate?.distanceMeasureInfoWindowControllerSetting(self)
  94. }
  95. @IBAction func buttonItemClick_Cancel(_ sender: Any) {
  96. delegate?.cancelMeasureInfoWindowControllerSetting(self)
  97. }
  98. }