KMNPDFInsertBlankWindowController.swift 34 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712
  1. //
  2. // KMNPDFInsertBlankWindowController.swift
  3. // PDF Reader Pro
  4. //
  5. // Created by 丁林圭 on 2024/10/23.
  6. //
  7. import Cocoa
  8. import KMComponentLibrary
  9. class KMNPDFInsertBlankWindowController: KMNBaseWindowController {
  10. @IBOutlet var titleLabel: NSTextField!
  11. @IBOutlet var sizeLabel: NSTextField!
  12. @IBOutlet var currentRadioButton: ComponentRadio!
  13. @IBOutlet var standardRadioButton: ComponentRadio!
  14. @IBOutlet var standardSelect: ComponentSelect!
  15. @IBOutlet var customizationRadioButton: ComponentRadio!
  16. @IBOutlet var widthInput: ComponentInput!
  17. @IBOutlet var heightInput: ComponentInput!
  18. @IBOutlet var unitSelect: ComponentSelect!
  19. @IBOutlet var directionLabel: NSTextField!
  20. @IBOutlet var verticalRadioButton: ComponentRadio!
  21. @IBOutlet var horizontalRadioButton: ComponentRadio!
  22. @IBOutlet var positionLabel: NSTextField!
  23. @IBOutlet var firstRadioButton: ComponentRadio!
  24. @IBOutlet var lastRadioButton: ComponentRadio!
  25. @IBOutlet var pageRadioButton: ComponentRadio!
  26. @IBOutlet var pageNumInput: ComponentInputNumber!
  27. @IBOutlet var pageCountLabel: NSTextField!
  28. @IBOutlet var positionSelect: ComponentSelect!
  29. @IBOutlet var cancelButton: ComponentButton!
  30. @IBOutlet var insertButton: ComponentButton!
  31. @IBOutlet var standardRadioWidthButton:NSLayoutConstraint!
  32. @IBOutlet var customizationRadioWidthButton:NSLayoutConstraint!
  33. @IBOutlet var verticalRadioWidthButton:NSLayoutConstraint!
  34. @IBOutlet var horizontalRadioWidthButton:NSLayoutConstraint!
  35. @IBOutlet var cancelWidthButton:NSLayoutConstraint!
  36. @IBOutlet var insertWidthButton:NSLayoutConstraint!
  37. @IBOutlet var pageRadioWidthButton:NSLayoutConstraint!
  38. private var orgDocument:CPDFDocument?
  39. private var selectionIndexPaths:Set<IndexPath>?
  40. private var lastUnitString: String = "mm"
  41. var callback: ((CGSize, Int) -> Void)?
  42. convenience init(_ document: CPDFDocument?, selectionIndexPaths: Set<IndexPath>?) {
  43. self.init(windowNibName: "KMNPDFInsertBlankWindowController")
  44. orgDocument = document
  45. self.selectionIndexPaths = selectionIndexPaths
  46. }
  47. convenience init(_ filePath: String,password:String?) {
  48. self.init(windowNibName: "KMNPDFInsertBlankWindowController")
  49. let document = CPDFDocument.init(url: URL(fileURLWithPath: filePath))
  50. if password != nil {
  51. document?.unlock(withPassword: password as String?)
  52. }
  53. orgDocument = document
  54. }
  55. private func setUpPositionSelctProperty() {
  56. var menuItemArr: [ComponentMenuitemProperty] = []
  57. for language in [KMLocalizedString("After"),KMLocalizedString("Before")] {
  58. let itemProperty: ComponentMenuitemProperty = ComponentMenuitemProperty(multipleSelect: false,
  59. itemSelected: false,
  60. isDisabled: false,
  61. keyEquivalent: nil,
  62. text: language)
  63. menuItemArr.append(itemProperty)
  64. }
  65. positionSelect.updateMenuItemsArr(menuItemArr)
  66. }
  67. private func setUpUnitSelctProperty() {
  68. var menuItemArr: [ComponentMenuitemProperty] = []
  69. for language in ["mm","cm","in"] {
  70. let itemProperty: ComponentMenuitemProperty = ComponentMenuitemProperty(multipleSelect: false,
  71. itemSelected: false,
  72. isDisabled: false,
  73. keyEquivalent: nil,
  74. text: language)
  75. menuItemArr.append(itemProperty)
  76. }
  77. unitSelect.updateMenuItemsArr(menuItemArr)
  78. }
  79. private func setUpPageSizeSelctProperty() {
  80. let paperArray = KMNPageSizeTool.paperSize()
  81. var menuItemArr: [ComponentMenuitemProperty] = []
  82. for pageSizeString in paperArray {
  83. let itemProperty: ComponentMenuitemProperty = ComponentMenuitemProperty(multipleSelect: false,
  84. itemSelected: false,
  85. isDisabled: false,
  86. keyEquivalent: nil,
  87. text: pageSizeString)
  88. menuItemArr.append(itemProperty)
  89. }
  90. standardSelect.updateMenuItemsArr(menuItemArr)
  91. }
  92. override func updateUIThemeColor() {
  93. super.updateUIThemeColor()
  94. window?.contentView?.wantsLayer = true
  95. window?.contentView?.layer?.backgroundColor = ComponentLibrary.shared.getComponentColorFromKey("colorBg/popup").cgColor
  96. titleLabel.textColor = ComponentLibrary.shared.getComponentColorFromKey("colorText/1")
  97. sizeLabel.textColor = ComponentLibrary.shared.getComponentColorFromKey("colorText/2")
  98. directionLabel.textColor = ComponentLibrary.shared.getComponentColorFromKey("colorText/2")
  99. positionLabel.textColor = ComponentLibrary.shared.getComponentColorFromKey("colorText/2")
  100. currentRadioButton.reloadData()
  101. standardRadioButton.reloadData()
  102. customizationRadioButton.reloadData()
  103. firstRadioButton.reloadData()
  104. lastRadioButton.reloadData()
  105. pageRadioButton.reloadData()
  106. insertButton.reloadData()
  107. cancelButton.reloadData()
  108. unitSelect.reloadData()
  109. standardSelect.reloadData()
  110. positionSelect.reloadData()
  111. }
  112. override func updateUILanguage() {
  113. super.updateUILanguage()
  114. titleLabel.stringValue = KMLocalizedString("Insert Blank Page")
  115. sizeLabel.stringValue = KMLocalizedString("Page Size")
  116. directionLabel.stringValue = KMLocalizedString("Direction")
  117. positionLabel.stringValue = KMLocalizedString("Where to insert?")
  118. currentRadioButton.properties.text = KMLocalizedString("Current Size")
  119. standardRadioButton.properties.text = KMLocalizedString("Standard")
  120. customizationRadioButton.properties.text = KMLocalizedString("Custom")
  121. firstRadioButton.properties.text = KMLocalizedString("First")
  122. lastRadioButton.properties.text = KMLocalizedString(("Last"))
  123. pageRadioButton.properties.text = KMLocalizedString("Page")
  124. verticalRadioButton.properties.text = KMLocalizedString("Landscape pages")
  125. horizontalRadioButton.properties.text = KMLocalizedString("Portrait pages")
  126. setUpPositionSelctProperty()
  127. setUpUnitSelctProperty()
  128. setUpPageSizeSelctProperty()
  129. insertButton.properties.buttonText = KMLocalizedString("Insert")
  130. cancelButton.properties.buttonText = KMLocalizedString("Cancel")
  131. let positionSelectIndex = positionSelect.indexOfSelect()
  132. if positionSelectIndex == 0 {
  133. positionSelect.properties.text = KMLocalizedString("After")
  134. } else {
  135. positionSelect.properties.text = KMLocalizedString("Before")
  136. }
  137. currentRadioButton.reloadData()
  138. standardRadioButton.reloadData()
  139. customizationRadioButton.reloadData()
  140. firstRadioButton.reloadData()
  141. lastRadioButton.reloadData()
  142. pageRadioButton.reloadData()
  143. insertButton.reloadData()
  144. cancelButton.reloadData()
  145. positionSelect.reloadData()
  146. verticalRadioButton.reloadData()
  147. horizontalRadioButton.reloadData()
  148. pageRadioWidthButton.constant = pageRadioButton.properties.propertyInfo.viewWidth
  149. cancelWidthButton.constant = cancelButton.properties.propertyInfo.viewWidth
  150. insertWidthButton.constant = insertButton.properties.propertyInfo.viewWidth
  151. standardRadioWidthButton.constant = standardRadioButton.properties.propertyInfo.viewWidth
  152. customizationRadioWidthButton.constant = customizationRadioButton.properties.propertyInfo.viewWidth
  153. verticalRadioWidthButton.constant = verticalRadioButton.properties.propertyInfo.viewWidth
  154. horizontalRadioWidthButton.constant = horizontalRadioButton.properties.propertyInfo.viewWidth
  155. }
  156. override func initContentView() {
  157. super.initContentView()
  158. titleLabel.font = ComponentLibrary.shared.getFontFromKey("mac/body-m-medium")
  159. sizeLabel.font = ComponentLibrary.shared.getFontFromKey("mac/body-m-medium")
  160. directionLabel.font = ComponentLibrary.shared.getFontFromKey("mac/body-m-medium")
  161. sizeLabel.font = ComponentLibrary.shared.getFontFromKey("mac/body-m-medium")
  162. positionLabel.font = ComponentLibrary.shared.getFontFromKey("mac/body-m-medium")
  163. var currentPageDex = 0
  164. if(selectionIndexPaths?.count ?? 0 > 0) {
  165. pageRadioButton.properties.checkboxType = .selected
  166. let maxmumIndexPath = selectionIndexPaths?.max(by: { $0 < $1 })
  167. currentPageDex = maxmumIndexPath?.item ?? 0
  168. }
  169. let page = self.orgDocument?.page(at: UInt(currentPageDex)) as? CPDFPage
  170. let rect = page?.bounds ?? CGRectZero
  171. var width = KMNPageSizeTool.conversion(withUnit: "mm", value: (NSWidth(rect)/595 * 210))
  172. var height = KMNPageSizeTool.conversion(withUnit: "mm", value: (NSHeight(rect)/842 * 297))
  173. if((page?.rotation ?? 0) % 180 != 0) {
  174. width = KMNPageSizeTool.conversion(withUnit: "mm", value: (NSHeight(rect)/595 * 210))
  175. height = KMNPageSizeTool.conversion(withUnit: "mm", value: (NSWidth(rect)/842 * 297))
  176. }
  177. currentRadioButton.properties = ComponentCheckBoxProperty(size: .s,
  178. state: .normal,
  179. isDisabled: false,
  180. showhelp: false,
  181. text: String(format: "%@(%@ x %@mm)", KMLocalizedString("Current Size"),width,height),
  182. checkboxType: .selected)
  183. standardRadioButton.properties = ComponentCheckBoxProperty(size: .s,
  184. state: .normal,
  185. isDisabled: false,
  186. showhelp: false,
  187. text: KMLocalizedString("Standard"),
  188. checkboxType: .normal)
  189. customizationRadioButton.properties = ComponentCheckBoxProperty(size: .s,
  190. state: .normal,
  191. isDisabled: false,
  192. showhelp: false,
  193. text: KMLocalizedString("Custom"),
  194. checkboxType: .normal)
  195. firstRadioButton.properties = ComponentCheckBoxProperty(size: .s,
  196. state: .normal,
  197. isDisabled: false,
  198. showhelp: false,
  199. text: KMLocalizedString("First"),
  200. checkboxType: .normal)
  201. lastRadioButton.properties = ComponentCheckBoxProperty(size: .s,
  202. state: .normal,
  203. isDisabled: false,
  204. showhelp: false,
  205. text: KMLocalizedString("Last"),
  206. checkboxType: .normal)
  207. pageRadioButton.properties = ComponentCheckBoxProperty(size: .s,
  208. state: .normal,
  209. isDisabled: false,
  210. showhelp: false,
  211. text: KMLocalizedString("Page"),
  212. checkboxType: .normal)
  213. verticalRadioButton.properties = ComponentCheckBoxProperty(size: .s,
  214. state: .normal,
  215. isDisabled: false,
  216. showhelp: false,
  217. text: KMLocalizedString("Landscape pages"),
  218. checkboxType: .normal)
  219. horizontalRadioButton.properties = ComponentCheckBoxProperty(size: .s,
  220. state: .normal,
  221. isDisabled: false,
  222. showhelp: false,
  223. text: KMLocalizedString("Portrait pages"),
  224. checkboxType: .selected)
  225. unitSelect.properties = ComponentSelectProperties(size: .s,
  226. state: .normal,
  227. isDisabled: true,
  228. isError: false,
  229. leftIcon: false,
  230. placeholder: nil,
  231. errorText: nil,
  232. creatable: false,
  233. text: KMLocalizedString("mm", comment: ""))
  234. unitSelect.delegate = self
  235. standardSelect.properties = ComponentSelectProperties(size: .s,
  236. state: .normal,
  237. isDisabled: true,
  238. isError: false,
  239. leftIcon: false,
  240. placeholder: nil,
  241. errorText: nil,
  242. creatable: false,
  243. text: KMLocalizedString("A4", comment: ""))
  244. standardSelect.delegate = self
  245. positionSelect.properties = ComponentSelectProperties(size: .s,
  246. state: .normal,
  247. isDisabled: false,
  248. isError: false,
  249. leftIcon: false,
  250. placeholder: nil,
  251. errorText: nil,
  252. creatable: false,
  253. text: KMLocalizedString("After", comment: ""))
  254. positionSelect.delegate = self
  255. insertButton.properties = ComponentButtonProperty(type: .primary,
  256. size: .s,
  257. state: .normal,
  258. isDisable: false,
  259. buttonText: KMLocalizedString("Insert"),keepPressState: false)
  260. insertButton.setTarget(self, action: #selector(insertButtonClicked(_ :)))
  261. insertWidthButton.constant = insertButton.properties.propertyInfo.viewWidth
  262. cancelButton.properties = ComponentButtonProperty(type: .default_tertiary,
  263. size: .s,
  264. state: .normal,
  265. buttonText: KMLocalizedString("Cancel"))
  266. cancelButton.setTarget(self, action: #selector(cancelButtonClicked(_ :)))
  267. cancelWidthButton.constant = cancelButton.properties.propertyInfo.viewWidth
  268. firstRadioButton.setTarget(self, action: #selector(insertPositionAction(_:)))
  269. lastRadioButton.setTarget(self, action: #selector(insertPositionAction(_:)))
  270. pageRadioButton.setTarget(self, action: #selector(insertPositionAction(_:)))
  271. verticalRadioButton.setTarget(self, action: #selector(insertDirectionAction(_:)))
  272. horizontalRadioButton.setTarget(self, action: #selector(insertDirectionAction(_:)))
  273. currentRadioButton.setTarget(self, action: #selector(insertPageSizeAction(_:)))
  274. standardRadioButton.setTarget(self, action: #selector(insertPageSizeAction(_:)))
  275. customizationRadioButton.setTarget(self, action: #selector(insertPageSizeAction(_:)))
  276. pageNumInput.properties = ComponentInputNumberProperty(alignment: .left,
  277. size: .s,
  278. state: .normal,
  279. isError: false,
  280. showErrorInfo: false,
  281. isDisabled: true,
  282. showPrefix: false,
  283. showSuffix: false,
  284. minSize: 1,
  285. maxSize: Int(orgDocument?.pageCount ?? 1),
  286. text: SettingsManager.sharedInstance.autoSaveMinutes)
  287. pageNumInput.delegate = self
  288. let inputWidthProperty: ComponentInputProperty = ComponentInputProperty(size: .s,
  289. state:.pressed ,
  290. isError: false,
  291. showPrefix: false,
  292. showSuffix: false,
  293. showClear: false,
  294. isDisabled: true,
  295. placeholder: "210",
  296. text: "210")
  297. widthInput.properties = inputWidthProperty
  298. widthInput.delegate = self
  299. let inputHeightProperty: ComponentInputProperty = ComponentInputProperty(size: .s,
  300. state:.pressed ,
  301. isError: false,
  302. showPrefix: false,
  303. showSuffix: false,
  304. showClear: false,
  305. isDisabled: true,
  306. placeholder: "297",
  307. text: "297")
  308. heightInput.properties = inputHeightProperty
  309. heightInput.delegate = self
  310. pageCountLabel.stringValue = "/" + String(format: "%d", orgDocument?.pageCount ?? 0)
  311. setUpPositionSelctProperty()
  312. setUpUnitSelctProperty()
  313. setUpPageSizeSelctProperty()
  314. }
  315. override func windowDidLoad() {
  316. super.windowDidLoad()
  317. positionSelect.selectItemAtIndex(0)
  318. unitSelect.selectItemAtIndex(0)
  319. standardSelect.selectItemAtIndex(1)
  320. pageCountLabel.stringValue = "/" + String(format: "%d", orgDocument?.pageCount ?? 0)
  321. if(selectionIndexPaths?.count ?? 0 > 0) {
  322. pageRadioButton.properties.checkboxType = .selected
  323. let maxmumIndexPath = selectionIndexPaths?.max(by: { $0 < $1 })
  324. pageNumInput.properties.text = String((maxmumIndexPath?.item ?? 0)+1)
  325. pageNumInput.properties.isDisabled = false
  326. pageRadioButton.reloadData()
  327. pageNumInput.reloadData()
  328. } else {
  329. firstRadioButton.properties.checkboxType = .selected
  330. pageRadioButton.properties.checkboxType = .normal
  331. positionSelect.properties.isDisabled = true
  332. pageNumInput.properties.isDisabled = true
  333. firstRadioButton.reloadData()
  334. pageRadioButton.reloadData()
  335. pageNumInput.reloadData()
  336. positionSelect.reloadData()
  337. }
  338. }
  339. //MARK: - Action
  340. @objc func cancelButtonClicked(_ sender: NSView) {
  341. own_closeEndSheet()
  342. }
  343. @objc func insertButtonClicked(_ sender: NSView) {
  344. self.window?.makeFirstResponder(nil)
  345. var currentPageDex = 0
  346. if(selectionIndexPaths?.count ?? 0 > 0) {
  347. pageRadioButton.properties.checkboxType = .selected
  348. let maxmumIndexPath = selectionIndexPaths?.max(by: { $0 < $1 })
  349. currentPageDex = maxmumIndexPath?.item ?? 0
  350. }
  351. var height: CGFloat = 0
  352. var width: CGFloat = 0
  353. if currentRadioButton.properties.checkboxType == .selected {
  354. let page = self.orgDocument?.page(at: UInt(currentPageDex)) as? CPDFPage
  355. let rect = page?.bounds ?? .zero
  356. width = rect.size.width
  357. height = rect.size.height
  358. let w = KMPageSizeTool.conversion(withUnit: "mm", value: (NSWidth(rect)/595 * 210))
  359. let h = KMPageSizeTool.conversion(withUnit: "mm", value: (NSHeight(rect)/842 * 297))
  360. width = w.stringToCGFloat()
  361. height = h.stringToCGFloat()
  362. if((page?.rotation ?? 0) % 180 != 0) {
  363. width = h.stringToCGFloat()
  364. height = w.stringToCGFloat()
  365. }
  366. } else if standardRadioButton.properties.checkboxType == .selected {
  367. let sizeString = standardSelect.properties.text ?? KMNPaperSizeNameA4
  368. let tSize = KMPageSizeTool.getPaperSize(paperName: sizeString)
  369. width = KMPageSizeTool.conversion(withUnit: "mm", value: tSize.width).stringToCGFloat()
  370. height = KMPageSizeTool.conversion(withUnit: "mm", value: tSize.height).stringToCGFloat()
  371. } else if customizationRadioButton.properties.checkboxType == .selected {
  372. let unitString = unitSelect.properties.text ?? "mm"
  373. height = widthInput.properties.text.stringToCGFloat()
  374. width = heightInput.properties.text.stringToCGFloat()
  375. width = KMPageSizeTool.conversion(with: "mm", from: unitString, value: width).stringToCGFloat()
  376. height = KMPageSizeTool.conversion(with: "mm", from: unitString, value: height).stringToCGFloat()
  377. if(!(width > 0 && height > 0)) {
  378. let alert = NSAlert()
  379. alert.alertStyle = .critical
  380. alert.messageText = KMLocalizedString("Please enter the correct size.")
  381. alert.runModal()
  382. return
  383. }
  384. }
  385. width *= 595 / 210
  386. height *= 842 / 297
  387. var insertSize = CGSize(width: width, height: height)
  388. if(verticalRadioButton.properties.checkboxType == .selected) {
  389. if(width < height) {
  390. insertSize = CGSize(width: height, height: width)
  391. }
  392. } else if(horizontalRadioButton.properties.checkboxType == .selected){
  393. if(width > height) {
  394. insertSize = CGSize(width: height, height: width)
  395. }
  396. }
  397. var insetPageDex:Int = 0
  398. if firstRadioButton.properties.checkboxType == .selected {
  399. insetPageDex = 0
  400. } else if lastRadioButton.properties.checkboxType == .selected {
  401. insetPageDex = Int(orgDocument?.pageCount ?? 0)
  402. } else if pageRadioButton.properties.checkboxType == .selected {
  403. insetPageDex = Int(pageNumInput.properties.text ?? "0") ?? 0
  404. let positionSelectIndex = positionSelect.indexOfSelect()
  405. if (1 == positionSelectIndex) {
  406. insetPageDex -= 1
  407. }
  408. }
  409. callback?(insertSize, insetPageDex)
  410. own_closeEndSheet()
  411. }
  412. @objc func insertPositionAction(_ sender: NSView) {
  413. var oldSelectRadioBtn:ComponentRadio? = nil
  414. if sender == lastRadioButton {
  415. if(firstRadioButton.properties.checkboxType == .selected) {
  416. oldSelectRadioBtn = firstRadioButton
  417. } else if (pageRadioButton.properties.checkboxType == .selected){
  418. oldSelectRadioBtn = pageRadioButton
  419. pageNumInput.properties.isDisabled = true
  420. positionSelect.properties.isDisabled = true
  421. pageNumInput.reloadData()
  422. positionSelect.reloadData()
  423. }
  424. } else if sender == firstRadioButton {
  425. if(lastRadioButton.properties.checkboxType == .selected) {
  426. oldSelectRadioBtn = lastRadioButton
  427. } else if (pageRadioButton.properties.checkboxType == .selected){
  428. oldSelectRadioBtn = pageRadioButton
  429. pageNumInput.properties.isDisabled = true
  430. positionSelect.properties.isDisabled = true
  431. pageNumInput.reloadData()
  432. positionSelect.reloadData()
  433. }
  434. } else if sender == pageRadioButton {
  435. if(lastRadioButton.properties.checkboxType == .selected) {
  436. oldSelectRadioBtn = lastRadioButton
  437. } else if (firstRadioButton.properties.checkboxType == .selected){
  438. oldSelectRadioBtn = firstRadioButton
  439. }
  440. pageNumInput.properties.isDisabled = false
  441. positionSelect.properties.isDisabled = false
  442. pageNumInput.reloadData()
  443. positionSelect.reloadData()
  444. }
  445. if(oldSelectRadioBtn != nil) {
  446. oldSelectRadioBtn?.properties.checkboxType = .normal
  447. oldSelectRadioBtn?.reloadData()
  448. }
  449. }
  450. @objc func insertDirectionAction(_ sender: NSView) {
  451. var oldSelectRadioBtn:ComponentRadio? = nil
  452. if sender == verticalRadioButton {
  453. if(horizontalRadioButton.properties.checkboxType == .selected) {
  454. oldSelectRadioBtn = horizontalRadioButton
  455. }
  456. } else if sender == horizontalRadioButton {
  457. if(verticalRadioButton.properties.checkboxType == .selected) {
  458. oldSelectRadioBtn = verticalRadioButton
  459. }
  460. }
  461. if(oldSelectRadioBtn != nil) {
  462. oldSelectRadioBtn?.properties.checkboxType = .normal
  463. oldSelectRadioBtn?.reloadData()
  464. }
  465. let widthString = widthInput.properties.text
  466. let heightString = heightInput.properties.text
  467. var width = widthString.stringToCGFloat()
  468. var height = heightString.stringToCGFloat()
  469. if(verticalRadioButton.properties.checkboxType == .selected) {
  470. if(width < height) {
  471. widthInput.properties.text = heightString
  472. heightInput.properties.text = widthString
  473. widthInput.reloadData()
  474. heightInput.reloadData()
  475. }
  476. } else if(horizontalRadioButton.properties.checkboxType == .selected){
  477. if(width > height) {
  478. widthInput.properties.text = heightString
  479. heightInput.properties.text = widthString
  480. widthInput.reloadData()
  481. heightInput.reloadData()
  482. }
  483. }
  484. }
  485. @objc func insertPageSizeAction(_ sender: NSView) {
  486. var oldSelectRadioBtn:ComponentRadio? = nil
  487. if sender == currentRadioButton {
  488. if(standardRadioButton.properties.checkboxType == .selected) {
  489. oldSelectRadioBtn = standardRadioButton
  490. standardSelect.properties.isDisabled = true
  491. standardSelect.reloadData()
  492. } else if (customizationRadioButton.properties.checkboxType == .selected){
  493. oldSelectRadioBtn = customizationRadioButton
  494. widthInput.properties.isDisabled = true
  495. widthInput.reloadData()
  496. heightInput.properties.isDisabled = true
  497. heightInput.reloadData()
  498. unitSelect.properties.isDisabled = true
  499. unitSelect.reloadData()
  500. }
  501. } else if sender == standardRadioButton {
  502. if(currentRadioButton.properties.checkboxType == .selected) {
  503. oldSelectRadioBtn = currentRadioButton
  504. } else if (customizationRadioButton.properties.checkboxType == .selected){
  505. oldSelectRadioBtn = customizationRadioButton
  506. widthInput.properties.isDisabled = true
  507. widthInput.reloadData()
  508. heightInput.properties.isDisabled = true
  509. heightInput.reloadData()
  510. unitSelect.properties.isDisabled = true
  511. unitSelect.reloadData()
  512. }
  513. standardSelect.properties.isDisabled = false
  514. standardSelect.reloadData()
  515. } else if sender == customizationRadioButton {
  516. if(currentRadioButton.properties.checkboxType == .selected) {
  517. oldSelectRadioBtn = currentRadioButton
  518. } else if (standardRadioButton.properties.checkboxType == .selected){
  519. oldSelectRadioBtn = standardRadioButton
  520. standardSelect.properties.isDisabled = true
  521. standardSelect.reloadData()
  522. }
  523. widthInput.properties.isDisabled = false
  524. widthInput.reloadData()
  525. heightInput.properties.isDisabled = false
  526. heightInput.reloadData()
  527. unitSelect.properties.isDisabled = false
  528. unitSelect.reloadData()
  529. }
  530. if(oldSelectRadioBtn != nil) {
  531. oldSelectRadioBtn?.properties.checkboxType = .normal
  532. oldSelectRadioBtn?.reloadData()
  533. }
  534. }
  535. }
  536. //MARK: - ComponentInputNumberDelegate
  537. extension KMNPDFInsertBlankWindowController: ComponentInputNumberDelegate {
  538. func componentInputNumberDidValueChanged(inputNumber: ComponentInputNumber?) {
  539. if(inputNumber == pageNumInput) {
  540. }
  541. }
  542. }
  543. //MARK: - ComponentSelectDelegate
  544. extension KMNPDFInsertBlankWindowController: ComponentSelectDelegate {
  545. func componentSelectDidSelect(view: ComponentSelect?, menuItemProperty: ComponentMenuitemProperty?) {
  546. if(view == unitSelect) {
  547. let width = widthInput.properties.text.stringToCGFloat()
  548. let height = heightInput.properties.text.stringToCGFloat()
  549. widthInput.properties.text = KMNPageSizeTool.conversion(with: unitSelect.properties.text ?? "mm", from: lastUnitString, value: width)
  550. heightInput.properties.text = KMNPageSizeTool.conversion(with: unitSelect.properties.text ?? "mm", from: lastUnitString, value: height)
  551. lastUnitString = unitSelect.properties.text ?? "mm"
  552. widthInput.reloadData()
  553. heightInput.reloadData()
  554. } else if (view == standardSelect) {
  555. } else if (view == positionSelect) {
  556. }
  557. }
  558. }
  559. //MARK: - ComponentInputDelegate
  560. extension KMNPDFInsertBlankWindowController: ComponentInputDelegate {
  561. //文字变化
  562. func componentInputDidChanged(inputView: ComponentInput) {
  563. if (inputView == widthInput) {
  564. let fValue = widthInput.properties.text.stringToCGFloat()
  565. if fValue > heightInput.properties.text.stringToCGFloat() {
  566. verticalRadioButton.properties.checkboxType = .selected
  567. horizontalRadioButton.properties.checkboxType = .normal
  568. } else {
  569. verticalRadioButton.properties.checkboxType = .normal
  570. horizontalRadioButton.properties.checkboxType = .selected
  571. }
  572. verticalRadioButton.reloadData()
  573. horizontalRadioButton.reloadData()
  574. } else if (inputView == heightInput) {
  575. let fValue = heightInput.properties.text.stringToCGFloat()
  576. if fValue > widthInput.properties.text.stringToCGFloat() {
  577. verticalRadioButton.properties.checkboxType = .normal
  578. horizontalRadioButton.properties.checkboxType = .selected
  579. } else {
  580. verticalRadioButton.properties.checkboxType = .selected
  581. horizontalRadioButton.properties.checkboxType = .normal
  582. }
  583. verticalRadioButton.reloadData()
  584. horizontalRadioButton.reloadData()
  585. }
  586. }
  587. }