KMNPopAnnotationWindowController.swift 59 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266
  1. //
  2. // KMNPopAnnotationWindowController.swift
  3. // PDF Reader Pro
  4. //
  5. // Created by 丁林圭 on 2024/12/2.
  6. //
  7. import Cocoa
  8. import KMComponentLibrary
  9. let popOffSet:CGFloat = 8.0
  10. typealias PaneCallback = (_ isOpenPane: Bool) -> ()
  11. typealias UpdatePDFViewCallback = () -> ()
  12. class KMNPopOperationWindow: NSWindow {
  13. override var canBecomeKey: Bool {
  14. return true
  15. }
  16. override var canBecomeMain: Bool {
  17. return true
  18. }
  19. }
  20. class KMNPopAnnotationWindowController: KMNBaseWindowController {
  21. public var popType:ListViewPopType = .popTypeNone {
  22. didSet {
  23. rightOffsetConstraint.constant = 45.0
  24. operationHeightConstraint.constant = 32.0
  25. paneBox.isHidden = false
  26. switch popType {
  27. case .generaAnnotation :
  28. generaPopView.layoutSubtreeIfNeeded()
  29. operationWidthConstraint.constant = generaPopView.bounds.width
  30. self.window?.display() //需刷新约束才会有值,不然会变化
  31. operationBox.contentView = generaPopView
  32. break
  33. case .freeTextAnnotation:
  34. fontPopView.layoutSubtreeIfNeeded()
  35. operationWidthConstraint.constant = fontPopView.bounds.width
  36. self.window?.display() //需刷新约束才会有值,不然会变化
  37. operationBox.contentView = fontPopView
  38. break
  39. case .shapeAnnotation:
  40. shapeView.layoutSubtreeIfNeeded()
  41. operationWidthConstraint.constant = shapeView.bounds.width
  42. self.window?.display() //需刷新约束才会有值,不然会变化
  43. operationBox.contentView = shapeView
  44. break
  45. case .linkAnnotation:
  46. if let newLinkAnnotation = annotationPopMode.annotation as? CPDFLinkAnnotation {
  47. linkAnnotation = newLinkAnnotation
  48. }
  49. break
  50. case .formAnnotation:
  51. formRightConstraint.constant = 0
  52. gropNameInput.isHidden = true
  53. formView.layoutSubtreeIfNeeded()
  54. operationWidthConstraint.constant = formView.bounds.width
  55. self.window?.display() //需刷新约束才会有值,不然会变化
  56. operationBox.contentView = formView
  57. break
  58. case .formRadioAnnotation:
  59. formRightConstraint.constant = 110
  60. gropNameInput.isHidden = false
  61. formView.layoutSubtreeIfNeeded()
  62. operationWidthConstraint.constant = formView.bounds.width
  63. self.window?.display() //需刷新约束才会有值,不然会变化
  64. operationBox.contentView = formView
  65. break
  66. case .multpleAnnotation:
  67. alightView.layoutSubtreeIfNeeded()
  68. operationWidthConstraint.constant = alightView.bounds.width
  69. self.window?.display() //需刷新约束才会有值,不然会变化
  70. operationBox.contentView = alightView
  71. break
  72. case .popTypeNone:
  73. break
  74. }
  75. }
  76. }
  77. public var isOpenPane = false {
  78. didSet {
  79. if isOpenPane == true {
  80. paneSelectorItem.properties.state = .pressed
  81. } else {
  82. paneSelectorItem.properties.state = .normal
  83. }
  84. paneSelectorItem.reloadData()
  85. }
  86. }
  87. public var annotationPopMode:KMNAnnotationPopMode = KMNAnnotationPopMode(pdfAnnotations: []) {
  88. didSet {
  89. let annotationPopType = annotationPopMode.popType
  90. popType = annotationPopType
  91. if annotationPopType == .generaAnnotation {
  92. let newColor = annotationPopMode.annotationColor()
  93. let oldColor = annotationColor
  94. if !CPDFListView.isTheSameColor(newColor, anotherColor: oldColor) {
  95. annotationColor = newColor
  96. }
  97. } else if(annotationPopType == .shapeAnnotation) {
  98. let newColor = annotationPopMode.annotationColor()
  99. let oldColor = annotationColor
  100. if !CPDFListView.isTheSameColor(newColor, anotherColor: oldColor) {
  101. annotationColor = newColor
  102. }
  103. } else if(annotationPopType == .freeTextAnnotation) {
  104. let newColor = annotationPopMode.annotationFontColor()
  105. let oldColor = fontColor
  106. if !CPDFListView.isTheSameColor(newColor, anotherColor: oldColor) {
  107. fontColor = newColor
  108. }
  109. let alignment = annotationPopMode.annotationAlignment()
  110. if(alignment != self.fontAlight) {
  111. fontAlight = alignment
  112. }
  113. let font = annotationPopMode.annotationFontName()
  114. if(font?.familyName != compdfFont?.familyName || font?.styleName != compdfFont?.styleName) {
  115. compdfFont = font
  116. }
  117. } else if annotationPopType == .linkAnnotation {
  118. if annotationPopMode.annotation != nil {
  119. linkAnnotation = annotationPopMode.annotation as! CPDFLinkAnnotation
  120. }
  121. } else if annotationPopType == .formAnnotation {
  122. if annotationPopMode.annotationFieldName() != fildNameString {
  123. fildNameString = annotationPopMode.annotationFieldName() ?? ""
  124. }
  125. } else if annotationPopType == .formRadioAnnotation {
  126. if annotationPopMode.annotationFieldName() != fildNameString {
  127. fildNameString = annotationPopMode.annotationFieldName() ?? ""
  128. }
  129. if annotationPopMode.annotationGropName() != gropNameString {
  130. gropNameString = annotationPopMode.annotationGropName() ?? ""
  131. }
  132. }
  133. }
  134. }
  135. private var annotationColor:NSColor?{
  136. didSet {
  137. if popType == .generaAnnotation {
  138. generaColorGroup.currentColor = annotationColor
  139. generaColorGroup.refreshUI()
  140. } else if popType == .shapeAnnotation {
  141. shapeColorGroup.currentColor = annotationColor
  142. shapeColorGroup.refreshUI()
  143. }
  144. }
  145. }
  146. private var fontColor:NSColor?{
  147. didSet {
  148. fontColorItem.properties?.color = fontColor
  149. fontColorItem.reloadData()
  150. }
  151. }
  152. private var linkAnnotation:CPDFLinkAnnotation = CPDFLinkAnnotation(){
  153. didSet {
  154. if let destination = linkAnnotation.destination() {
  155. linkType = .page
  156. } else if let annotationURL = linkAnnotation.url(){
  157. var urlString = annotationURL.fileURL.absoluteString
  158. if annotationURL.hasPrefix("mailto:") {
  159. linkType = .email
  160. } else {
  161. linkType = .url
  162. }
  163. } else {
  164. linkType = .linkSetting
  165. }
  166. }
  167. }
  168. private var fildNameString:String = ""{
  169. didSet {
  170. fildNameInput.properties.text = fildNameString
  171. fildNameInput.reloadData()
  172. }
  173. }
  174. private var gropNameString:String = ""{
  175. didSet {
  176. gropNameInput.properties.text = gropNameString
  177. gropNameInput.reloadData()
  178. }
  179. }
  180. private var compdfFont:CPDFFont?{
  181. didSet {
  182. if(compdfFont != nil) {
  183. let styleName = compdfFont!.styleName
  184. if(styleName?.isEmpty == true) {
  185. fontNameSelect.properties.text = compdfFont!.familyName
  186. } else {
  187. fontNameSelect.properties.text = compdfFont!.familyName + "-" + (compdfFont!.styleName ?? "")
  188. }
  189. } else {
  190. fontNameSelect.properties.text = "-"
  191. }
  192. fontNameSelect.selectItemAtIndex(0)
  193. fontNameSelect.reloadData()
  194. }
  195. }
  196. private var fontAlight:NSTextAlignment = .left {
  197. didSet {
  198. if fontAlight == .center {
  199. fontAlightButton.properties.icon = NSImage(named: "KMNImageNameListViewPopAlightCenter")
  200. } else if fontAlight == .right {
  201. fontAlightButton.properties.icon = NSImage(named: "KMNImageNameListViewPopAlightRight")
  202. } else {
  203. fontAlightButton.properties.icon = NSImage(named: "KMNImageNameListViewPopAlightLeft")
  204. }
  205. fontAlightButton.reloadData()
  206. }
  207. }
  208. private var objectAlignType:ObjectAlignType = .alightLeft {
  209. didSet {
  210. var zeroRect = NSRect.null
  211. var highestRect = NSZeroRect
  212. var widthestRect = NSZeroRect
  213. let objects = annotationPopMode.annotations
  214. let fristObject : CPDFAnnotation = objects.first ?? CPDFAnnotation()
  215. var leftestRect = fristObject.bounds
  216. var rightestRect = fristObject.bounds
  217. var topestRect = fristObject.bounds
  218. var bottomestRect = fristObject.bounds
  219. var leftestObject : CPDFAnnotation = fristObject
  220. var rightestObject : CPDFAnnotation = fristObject
  221. var topestObject : CPDFAnnotation = fristObject
  222. var bottomestObject: CPDFAnnotation = fristObject
  223. var totalWidth = 0.0
  224. var totalHeight = 0.0
  225. for i in 0 ..< objects.count {
  226. let annotation : CPDFAnnotation = objects[i]
  227. zeroRect = zeroRect.union(annotation.bounds)
  228. totalWidth = totalWidth + annotation.bounds.width
  229. totalHeight = totalHeight + annotation.bounds.height
  230. if annotation.bounds.height > highestRect.height {
  231. highestRect = annotation.bounds
  232. }
  233. if annotation.bounds.width > widthestRect.width {
  234. widthestRect = annotation.bounds
  235. }
  236. if leftestRect.minX > annotation.bounds.minX {
  237. leftestRect = annotation.bounds
  238. leftestObject = annotation
  239. }
  240. if annotation.bounds.maxX > rightestRect.maxX {
  241. rightestRect = annotation.bounds
  242. rightestObject = annotation
  243. }
  244. if annotation.bounds.maxY > topestRect.maxY {
  245. topestRect = annotation.bounds
  246. topestObject = annotation
  247. }
  248. if bottomestRect.minY > annotation.bounds.minY {
  249. bottomestRect = annotation.bounds
  250. bottomestObject = annotation
  251. }
  252. }
  253. var resultAreasArray: [CPDFAnnotation] = []
  254. var newBoundsArray: [String] = []
  255. switch objectAlignType {
  256. case .alightLeft :
  257. for i in 0 ..< objects.count {
  258. let object = objects[i]
  259. var bounds = object.bounds
  260. bounds.origin.x = zeroRect.origin.x
  261. newBoundsArray.append(NSStringFromRect(bounds))
  262. }
  263. resultAreasArray = objects
  264. alightButton.properties.icon = NSImage(named: "KMNImageNameObjectPopAlightLeft")
  265. break
  266. case .alightRight:
  267. for i in 0 ..< objects.count {
  268. let object = objects[i]
  269. var bounds = object.bounds
  270. bounds.origin.x = zeroRect.maxX - bounds.size.width
  271. newBoundsArray.append(NSStringFromRect(bounds))
  272. }
  273. resultAreasArray = objects
  274. alightButton.properties.icon = NSImage(named: "KMNImageNameObjectPopAlightRight")
  275. break
  276. case .alightTop:
  277. for i in 0 ..< objects.count {
  278. let object = objects[i]
  279. var bounds = object.bounds
  280. bounds.origin.y = zeroRect.maxY - bounds.size.height
  281. newBoundsArray.append(NSStringFromRect(bounds))
  282. }
  283. resultAreasArray = objects
  284. alightButton.properties.icon = NSImage(named: "KMNImageNameObjectPopAlightTop")
  285. break
  286. case .alightVer:
  287. for i in 0 ..< objects.count {
  288. let object = objects[i]
  289. var bounds = object.bounds
  290. bounds.origin.x = widthestRect.midX - bounds.width/2
  291. newBoundsArray.append(NSStringFromRect(bounds))
  292. }
  293. resultAreasArray = objects
  294. alightButton.properties.icon = NSImage(named: "KMNImageNameObjectPopAlightVer")
  295. break
  296. case .alightHor:
  297. for i in 0 ..< objects.count {
  298. let object = objects[i]
  299. var bounds = object.bounds
  300. bounds.origin.y = highestRect.midY - bounds.height/2
  301. newBoundsArray.append(NSStringFromRect(bounds))
  302. }
  303. resultAreasArray = objects
  304. alightButton.properties.icon = NSImage(named: "KMNImageNameObjectPopAlightHor")
  305. break
  306. case .alightBottom:
  307. for i in 0 ..< objects.count {
  308. let object = objects[i]
  309. var bounds = object.bounds
  310. bounds.origin.y = zeroRect.minY
  311. newBoundsArray.append(NSStringFromRect(bounds))
  312. }
  313. resultAreasArray = objects
  314. alightButton.properties.icon = NSImage(named: "KMNImageNameObjectPopAlightBottom")
  315. break
  316. case .averageVer:
  317. let middleGap = zeroRect.height - topestRect.height - bottomestRect.height
  318. let otherAreasTotalHeight = totalHeight - topestRect.height - bottomestRect.height
  319. let gap = (middleGap - otherAreasTotalHeight)/CGFloat(objects.count - 1)
  320. var areasCopyArray : [CPDFAnnotation] = objects
  321. areasCopyArray.sorted(by: { obj1, obj2 in
  322. let area1 = obj1
  323. let area2 = obj2
  324. if area1.bounds.origin.x < area2.bounds.origin.x {
  325. return true
  326. } else {
  327. return false
  328. }
  329. })
  330. if let index = areasCopyArray.firstIndex(of: topestObject) {
  331. areasCopyArray.remove(at: index)
  332. }
  333. if let index = areasCopyArray.firstIndex(of: bottomestObject) {
  334. areasCopyArray.remove(at: index)
  335. }
  336. var bottomStartY = bottomestRect.maxY + gap
  337. for i in 0 ..< areasCopyArray.count {
  338. let areas = areasCopyArray[i]
  339. var bounds = areas.bounds
  340. bounds.origin.y = bottomStartY
  341. newBoundsArray.append(NSStringFromRect(bounds))
  342. bottomStartY = bottomStartY + bounds.height + gap
  343. }
  344. resultAreasArray = areasCopyArray
  345. alightButton.properties.icon = NSImage(named: "KMNImageNameObjecAlignAverageVer")
  346. break
  347. case .averageHor:
  348. let middleGap = zeroRect.width - leftestRect.width - rightestRect.width
  349. let otherAreasTotalWidth = totalWidth - leftestRect.width - rightestRect.width
  350. let gap = (middleGap - otherAreasTotalWidth)/CGFloat(objects.count - 1)
  351. var areasCopyArray : [CPDFAnnotation] = objects
  352. areasCopyArray.sorted(by: { obj1, obj2 in
  353. let area1 = obj1
  354. let area2 = obj2
  355. if area1.bounds.origin.x < area2.bounds.origin.x {
  356. return true
  357. } else {
  358. return false
  359. }
  360. })
  361. if let index = areasCopyArray.firstIndex(of: leftestObject) {
  362. areasCopyArray.remove(at: index)
  363. }
  364. if let index = areasCopyArray.firstIndex(of: rightestObject) {
  365. areasCopyArray.remove(at: index)
  366. }
  367. var leftStartX = leftestRect.maxX + gap
  368. for i in 0 ..< areasCopyArray.count {
  369. let areas = areasCopyArray[i]
  370. var bounds = areas.bounds
  371. bounds.origin.x = leftStartX
  372. newBoundsArray.append(NSStringFromRect(bounds))
  373. leftStartX = leftStartX + bounds.width + gap
  374. }
  375. resultAreasArray = areasCopyArray
  376. alightButton.properties.icon = NSImage(named: "KMNImageNameObjecAlignAverageHor")
  377. break
  378. }
  379. alightButton.reloadData()
  380. for i in 0 ..< resultAreasArray.count {
  381. let annotation : CPDFAnnotation = resultAreasArray[i]
  382. annotation.bounds = NSRectFromString(newBoundsArray[i])
  383. }
  384. }
  385. }
  386. var paneCallback: PaneCallback?
  387. var updatePDFViewCallback: UpdatePDFViewCallback?
  388. static let shared: KMNPopAnnotationWindowController = {
  389. let windowC = KMNPopAnnotationWindowController(windowNibName: "KMNPopAnnotationWindowController")
  390. return windowC
  391. }()
  392. var linkType:linkDetailType = .linkSetting {
  393. didSet {
  394. if linkType == .linkSetting {
  395. linkView.layoutSubtreeIfNeeded()
  396. rightOffsetConstraint.constant = 45.0
  397. operationHeightConstraint.constant = 32.0
  398. operationWidthConstraint.constant = linkView.bounds.width
  399. self.window?.display() //需刷新约束才会有值,不然会变化
  400. operationBox.contentView = linkView
  401. paneBox.isHidden = false
  402. } else if linkType == .page {
  403. linkPageDetailView.layoutSubtreeIfNeeded()
  404. rightOffsetConstraint.constant = 8
  405. operationWidthConstraint.constant = linkPageDetailView.bounds.width
  406. operationHeightConstraint.constant = 224
  407. self.window?.display() //需刷新约束才会有值,不然会变化
  408. operationBox.contentView = linkPageDetailView
  409. paneBox.isHidden = true
  410. if let destination = linkAnnotation.destination() {
  411. reloadPageUrlData()
  412. refreshPageThum()
  413. } else {
  414. paginationView.properties.currentIndex = Int(((listView?.currentPageIndex ?? 0) + 1))
  415. paginationView.reloadData()
  416. refreshPageThum()
  417. }
  418. } else if linkType == .url {
  419. linkUrlDetailView.layoutSubtreeIfNeeded()
  420. rightOffsetConstraint.constant = 8
  421. operationWidthConstraint.constant = linkUrlDetailView.bounds.width
  422. operationHeightConstraint.constant = linkUrlDetailView.bounds.height
  423. self.window?.display() //需刷新约束才会有值,不然会变化
  424. operationBox.contentView = linkUrlDetailView
  425. paneBox.isHidden = true
  426. urlInput.properties.placeholder = KMNCompanyWebsite
  427. if let annotationURL = linkAnnotation.url(){
  428. if annotationURL.hasPrefix("mailto:") {
  429. urlInput.properties.text = ""
  430. } else {
  431. urlInput.properties.text = annotationURL
  432. }
  433. } else {
  434. urlInput.properties.text = ""
  435. }
  436. urlInput.properties.state = .pressed
  437. urlInput.beginEditing()
  438. urlInput.reloadData()
  439. } else if linkType == .email {
  440. linkUrlDetailView.layoutSubtreeIfNeeded()
  441. rightOffsetConstraint.constant = 8
  442. operationWidthConstraint.constant = linkUrlDetailView.bounds.width
  443. operationHeightConstraint.constant = linkUrlDetailView.bounds.height
  444. self.window?.display() //需刷新约束才会有值,不然会变化
  445. operationBox.contentView = linkUrlDetailView
  446. paneBox.isHidden = true
  447. urlInput.properties.placeholder = KMNCompanyEamil
  448. if let annotationURL = linkAnnotation.url(){
  449. if annotationURL.hasPrefix("mailto:") {
  450. if let range = annotationURL.range(of: "mailto:") {
  451. let trimmedString = String(annotationURL[range.upperBound...])
  452. urlInput.properties.text = trimmedString
  453. }
  454. } else {
  455. urlInput.properties.text = ""
  456. }
  457. } else {
  458. urlInput.properties.text = ""
  459. }
  460. urlInput.properties.state = .pressed
  461. urlInput.beginEditing()
  462. urlInput.reloadData()
  463. }
  464. updateFrame(listView: listView ?? CPDFListView())
  465. }
  466. }
  467. @IBOutlet weak var contentBox: NSBox!
  468. @IBOutlet weak var operationBox: NSBox!
  469. @IBOutlet weak var paneBox: NSBox!
  470. @IBOutlet weak var lineBox: NSBox!
  471. @IBOutlet var paneSelectorItem: ComponentCSelector!
  472. @IBOutlet weak var operationWidthConstraint: NSLayoutConstraint!
  473. @IBOutlet weak var rightOffsetConstraint: NSLayoutConstraint!
  474. @IBOutlet weak var operationHeightConstraint: NSLayoutConstraint!
  475. @IBOutlet var generaPopView: NSView!
  476. @IBOutlet var generaColorGroup: ComponentCColorGroup!
  477. @IBOutlet var fontPopView: NSView!
  478. @IBOutlet var fontColorItem: ComponentCColorItem!
  479. @IBOutlet var fontNameSelect: ComponentSelect!
  480. @IBOutlet var fontSizeZoomOutButton: ComponentButton!
  481. @IBOutlet var fontSizeZoomInButton: ComponentButton!
  482. @IBOutlet var fontAlightButton: ComponentButton!
  483. @IBOutlet var shapeView: NSView!
  484. @IBOutlet var shapeColorGroup: ComponentCColorGroup!
  485. @IBOutlet var widthZoomOutButton: ComponentButton!
  486. @IBOutlet var widthZoomInButton: ComponentButton!
  487. @IBOutlet var linkView: NSView!
  488. @IBOutlet var pageLinkButton: ComponentButton!
  489. @IBOutlet var urlLinkButton: ComponentButton!
  490. @IBOutlet var emailLinkButton: ComponentButton!
  491. @IBOutlet weak var pageLinkWidthConstraint: NSLayoutConstraint!
  492. @IBOutlet weak var urlLinkWidthConstraint: NSLayoutConstraint!
  493. @IBOutlet weak var emailLinkWidthConstraint: NSLayoutConstraint!
  494. @IBOutlet var formView: NSView!
  495. @IBOutlet var fildNameInput: ComponentInput!
  496. @IBOutlet var gropNameInput: ComponentInput!
  497. @IBOutlet var formRightConstraint: NSLayoutConstraint!
  498. @IBOutlet var alightView: NSView!
  499. @IBOutlet var alightButton: ComponentButton!
  500. @IBOutlet var linkPageDetailView: NSView!
  501. @IBOutlet var backPageLinkButton: ComponentButton!
  502. @IBOutlet var paginationView: ComponentPagination!
  503. @IBOutlet var paginationDesBox: NSBox!
  504. @IBOutlet var pageThumImageView: NSImageView!
  505. @IBOutlet var goPageLinkButton: ComponentButton!
  506. @IBOutlet weak var backPageWidthConstraint: NSLayoutConstraint!
  507. @IBOutlet var linkUrlDetailView: NSView!
  508. @IBOutlet var backUrlLinkButton: ComponentButton!
  509. @IBOutlet var goUrlLinkButton: ComponentButton!
  510. @IBOutlet var urlInput: ComponentInput!
  511. @IBOutlet weak var backUrlWidthConstraint: NSLayoutConstraint!
  512. private var fontPopover:NSPopover?
  513. private var ObjectPopover:NSPopover?
  514. weak var listView:CPDFListView? {
  515. didSet {
  516. paginationView.properties.totalCount = Int(listView?.document.pageCount ?? 1)
  517. paginationView.reloadData()
  518. }
  519. }
  520. override func windowDidLoad() {
  521. super.windowDidLoad()
  522. }
  523. override func initContentView() {
  524. super.initContentView()
  525. configFormPopUI()
  526. congfigFontPopUI()
  527. congfigGeneraPopUI()
  528. congfigShapePopUI()
  529. paneSelectorItem.properties = ComponentCSelectorProperty(iconImage:NSImage(named: "KMNImageNameListViewPopPane"))
  530. paneSelectorItem.setTarget(self, action: #selector(paneButtonClicked(_ :)))
  531. fontSizeZoomOutButton.properties = ComponentButtonProperty(type: .text_gray,
  532. size: .s,
  533. state: .normal,
  534. onlyIcon: true,
  535. icon:NSImage(named: "KMNImageNameListViewPopFontSizeZoomOut"),
  536. keepPressState: false)
  537. fontSizeZoomOutButton.setTarget(self, action: #selector(fontSizeZoomOutButtonClicked(_ :)))
  538. fontSizeZoomInButton.properties = ComponentButtonProperty(type: .text_gray,
  539. size: .s,
  540. state: .normal,
  541. onlyIcon: true,
  542. icon:NSImage(named: "KMNImageNameListViewPopFontSizeZoomIn"),
  543. keepPressState: false)
  544. fontSizeZoomInButton.setTarget(self, action: #selector(fontSizeZoomInButtonClicked(_ :)))
  545. fontAlightButton.properties = ComponentButtonProperty(type: .text_gray,
  546. size: .s,
  547. state: .normal,
  548. onlyIcon: true,
  549. icon:NSImage(named: "KMNImageNameListViewPopAlightLeft"))
  550. fontAlightButton.setTarget(self, action: #selector(fontAlightButtonClicked(_ :)))
  551. widthZoomOutButton.properties = ComponentButtonProperty(type: .text_gray,
  552. size: .s,
  553. state: .normal,
  554. onlyIcon: true,
  555. icon:NSImage(named: "KMNImageNameListViewPopWidthZoomOut"),
  556. keepPressState: false)
  557. widthZoomOutButton.setTarget(self, action: #selector(widthZoomOutButtonClicked(_ :)))
  558. widthZoomInButton.properties = ComponentButtonProperty(type: .text_gray,
  559. size: .s,
  560. state: .normal,
  561. onlyIcon: true,
  562. icon:NSImage(named: "KMNImageNameListViewPopWidthZoomIn"),
  563. keepPressState: false)
  564. widthZoomInButton.setTarget(self, action: #selector(widthZoomInButtonClicked(_ :)))
  565. pageLinkButton.setTarget(self, action: #selector(pageLinkButtonClicked(_ :)))
  566. urlLinkButton.setTarget(self, action: #selector(urlLinkButtonClicked(_ :)))
  567. emailLinkButton.setTarget(self, action: #selector(emailLinkButtonClicked(_ :)))
  568. alightButton.properties = ComponentButtonProperty(type: .text_gray,
  569. size: .s,
  570. state: .normal,
  571. onlyIcon: true,
  572. icon:NSImage(named: "KMNImageNameObjectPopAlightLeft"))
  573. alightButton.setTarget(self, action: #selector(alightButtonClicked(_ :)))
  574. goUrlLinkButton.properties = ComponentButtonProperty(type: .text_gray,
  575. size: .s,
  576. state: .normal,
  577. onlyIcon: true,
  578. icon:NSImage(named: "KMNImageNamePopLinkGO"),keepPressState: false)
  579. goUrlLinkButton.setTarget(self, action: #selector(linkGoButtonClicked(_ :)))
  580. goPageLinkButton.properties = ComponentButtonProperty(type: .text_gray,
  581. size: .s,
  582. state: .normal,
  583. onlyIcon: true,
  584. icon:NSImage(named: "KMNImageNamePopLinkGO"),keepPressState: false)
  585. goPageLinkButton.setTarget(self, action: #selector(linkGoButtonClicked(_ :)))
  586. paginationView.properties = ComponentPaginationProperty(doubleArrow_show: false, currentIndex: 0, totalCount: 1)
  587. paginationView.delegate = self
  588. let inputFiedProperty: ComponentInputProperty = ComponentInputProperty(size: .s,
  589. state:.pressed ,
  590. isError: false,
  591. showPrefix: false,
  592. showSuffix: false,
  593. showClear: false,
  594. isDisabled: false,
  595. placeholder: KMNCompanyWebsite,
  596. text: "")
  597. urlInput.properties = inputFiedProperty
  598. urlInput.delegate = self
  599. }
  600. override func updateUIThemeColor() {
  601. super.updateUIThemeColor()
  602. lineBox.fillColor = ComponentLibrary.shared.getComponentColorFromKey("colorBorder/divider")
  603. contentBox.fillColor = ComponentLibrary.shared.getComponentColorFromKey("colorBg/popup")
  604. contentBox.borderColor = ComponentLibrary.shared.getComponentColorFromKey("colorBorder/3-default")
  605. paginationDesBox.fillColor = ComponentLibrary.shared.getComponentColorFromKey("colorFill/4")
  606. paginationDesBox.borderColor = ComponentLibrary.shared.getComponentColorFromKey("colorBorder/4")
  607. pageThumImageView.wantsLayer = true
  608. paginationDesBox.layer?.borderColor = ComponentLibrary.shared.getComponentColorFromKey("colorBorder/divider").cgColor
  609. }
  610. override func updateUILanguage() {
  611. super.updateUILanguage()
  612. pageLinkButton.properties = ComponentButtonProperty(type: .text_gray,
  613. size: .s,
  614. state: .normal,
  615. onlyIcon: false,
  616. showLeftIcon:true,
  617. buttonText:KMLocalizedString("Page"),
  618. icon:NSImage(named: "KMNImageNameListViewLinkPage"),keepPressState: false)
  619. urlLinkButton.properties = ComponentButtonProperty(type: .text_gray,
  620. size: .s,
  621. state: .normal,
  622. onlyIcon: false,
  623. showLeftIcon:true,
  624. buttonText:KMLocalizedString("Web"),
  625. icon:NSImage(named: "KMNImageNameListViewLinkUrl"),keepPressState: false)
  626. emailLinkButton.properties = ComponentButtonProperty(type: .text_gray,
  627. size: .s,
  628. state: .normal,
  629. onlyIcon: false,
  630. showLeftIcon:true,
  631. buttonText:KMLocalizedString("Email"),
  632. icon:NSImage(named: "KMNImageNameListViewLinkEmail"),keepPressState: false)
  633. pageLinkWidthConstraint.constant = pageLinkButton.properties.propertyInfo.viewWidth
  634. urlLinkWidthConstraint.constant = urlLinkButton.properties.propertyInfo.viewWidth
  635. emailLinkWidthConstraint.constant = emailLinkButton.properties.propertyInfo.viewWidth
  636. backUrlLinkButton.properties = ComponentButtonProperty(type: .text_primary,
  637. size: .s,
  638. state: .normal,
  639. onlyIcon: false,
  640. showLeftIcon: true,
  641. buttonText: KMLocalizedString("Back"),
  642. icon:NSImage(named: "KMNImageNamePopLinkBack"),keepPressState: false)
  643. backUrlLinkButton.setTarget(self, action: #selector(linkBackButtonClicked(_ :)))
  644. backUrlWidthConstraint.constant = backUrlLinkButton.properties.propertyInfo.viewWidth
  645. backUrlLinkButton.reloadData()
  646. backPageLinkButton.properties = ComponentButtonProperty(type: .text_primary,
  647. size: .s,
  648. onlyIcon: false,
  649. showLeftIcon:true,
  650. buttonText: KMLocalizedString("Back"),
  651. icon:NSImage(named: "KMNImageNamePopLinkBack"),keepPressState: false)
  652. backPageLinkButton.setTarget(self, action: #selector(linkBackButtonClicked(_ :)))
  653. backPageWidthConstraint.constant = backPageLinkButton.properties.propertyInfo.viewWidth
  654. }
  655. //MARK: - Public
  656. public func updateFrame(listView:CPDFListView) {
  657. let activeAnnotations = annotationPopMode.annotations
  658. let page = activeAnnotations.first?.page
  659. let windowFram = listView.window?.frame ?? CGRectZero
  660. if(page != nil) {
  661. let pageRect = listView.selectionMultipleBounds(with: activeAnnotations)
  662. let positioningRect = listView.convert(pageRect, from: page!)
  663. if (CGRectIntersectsRect(positioningRect, listView.frame)) {
  664. let view: NSView? = nil
  665. let position = listView.convert(positioningRect, to: view)
  666. var positionNew = position.origin
  667. positionNew.x += windowFram.origin.x + position.width/2
  668. positionNew.y += windowFram.origin.y + position.height
  669. var positionRect = self.window?.frame ?? CGRectZero
  670. positionRect.origin.x = positionNew.x - positionRect.width/2
  671. positionRect.origin.y = positionNew.y + popOffSet
  672. var listViewWindRect = listView.convert(listView.frame, to: view)
  673. listViewWindRect.origin.x += windowFram.origin.x
  674. listViewWindRect.origin.y += windowFram.origin.y
  675. if CGRectGetMaxY(positionRect) > CGRectGetMaxY(listViewWindRect) {
  676. positionRect.origin.y = CGRectGetMaxY(listViewWindRect)
  677. }
  678. if CGRectGetMinX(positionRect) < CGRectGetMinX(listViewWindRect) {
  679. positionRect.origin.x = CGRectGetMinX(listViewWindRect)
  680. }
  681. if CGRectGetMaxX(positionRect) > CGRectGetMaxX(listViewWindRect) {
  682. positionRect.origin.x = CGRectGetMaxX(listViewWindRect) - positionRect.width
  683. }
  684. self.window?.setFrame(positionRect, display: true)
  685. updateUILanguage()
  686. } else {
  687. }
  688. }
  689. }
  690. public func closeWindow(listView:CPDFListView?) {
  691. if self.window?.isVisible == true {
  692. listView?.window?.removeChildWindow(self.window ?? NSWindow())
  693. self.window?.close()
  694. }
  695. if ObjectPopover?.isShown == true {
  696. ObjectPopover?.close()
  697. }
  698. if fontPopover?.isShown == true {
  699. fontPopover?.close()
  700. }
  701. }
  702. //MARK: - private
  703. private func congfigGeneraPopUI() {
  704. var colors = KMAnnotationPropertiesColorManager.manager.markHighlightColors
  705. if(annotationPopMode.annotation is CPDFMarkupAnnotation) {
  706. let markupAnnotation:CPDFMarkupAnnotation = annotationPopMode.annotation as! CPDFMarkupAnnotation
  707. if markupAnnotation.markupType() == .highlight {
  708. colors = KMAnnotationPropertiesColorManager.manager.markHighlightColors
  709. } else {
  710. colors = KMAnnotationPropertiesColorManager.manager.markOtherColors
  711. }
  712. }
  713. let colorAProperty = ComponentCColorProperty(colorType: .color, state: .normal, isCustom: false, color: colors[0])
  714. let colorBProperty = ComponentCColorProperty(colorType: .color, state: .normal, isCustom: false, color: colors[1])
  715. let colorCProperty = ComponentCColorProperty(colorType: .color, state: .normal, isCustom: false, color: colors[2])
  716. let colorDProperty = ComponentCColorProperty(colorType: .color, state: .normal, isCustom: false, color: colors[3])
  717. let colorEProperty = ComponentCColorProperty(colorType: .color, state: .normal, isCustom: true, color: colors[4])
  718. generaColorGroup.setUpWithColorPropertys([colorAProperty, colorBProperty, colorCProperty, colorDProperty], customItemProperty: colorEProperty)
  719. generaColorGroup.delegate = self
  720. }
  721. private func congfigFontPopUI() {
  722. fontNameSelect.properties = ComponentSelectProperties(size: .s,
  723. state: .normal,
  724. isDisabled: false,
  725. isError: false,
  726. leftIcon: false,
  727. placeholder: nil,
  728. errorText: nil,
  729. creatable: false,
  730. text: NSLocalizedString("", comment: ""))
  731. fontNameSelect.delegate = self
  732. var menuItemArr: [ComponentMenuitemProperty] = []
  733. for familyName in CPDFFont.familyNames {
  734. let fontNames = CPDFFont.fontNames(forFamilyName: familyName)
  735. let itemProperty: ComponentMenuitemProperty = ComponentMenuitemProperty(multipleSelect: false,
  736. itemSelected: false,
  737. isDisabled: false,
  738. keyEquivalent: nil,
  739. text: familyName,identifier: "1")
  740. var subMenuItemArr: [ComponentMenuitemProperty] = []
  741. for fontName in fontNames {
  742. let subItemProperty: ComponentMenuitemProperty = ComponentMenuitemProperty(multipleSelect: false,
  743. itemSelected: false,
  744. isDisabled: false,
  745. keyEquivalent: nil,
  746. text: fontName,identifier: "2")
  747. subMenuItemArr.append(subItemProperty)
  748. }
  749. itemProperty.subPropertys = subMenuItemArr
  750. menuItemArr.append(itemProperty)
  751. }
  752. fontNameSelect.updateMenuItemsArr(menuItemArr)
  753. fontColorItem.properties = ComponentCColorProperty(colorType: .color,
  754. state: .normal,
  755. isCustom: true,
  756. color: NSColor.red)
  757. fontColorItem.delegate = self
  758. fontColorItem.reloadData()
  759. }
  760. private func congfigShapePopUI() {
  761. var colors = KMAnnotationPropertiesColorManager.manager.inkColors
  762. let annotation = annotationPopMode.annotation ?? CPDFAnnotation()
  763. if annotation.isKind(of: CPDFInkAnnotation.self) {
  764. colors = KMAnnotationPropertiesColorManager.manager.inkColors
  765. } else if annotation.isKind(of: CPDFCircleAnnotation.self) ||
  766. annotation.isKind(of: CPDFSquareAnnotation.self) {
  767. colors = KMAnnotationPropertiesColorManager.manager.inkColors
  768. } else if annotation.isKind(of: CPDFLineAnnotation.self) {
  769. if let lineAnnotation:CPDFLineAnnotation = annotation as? CPDFLineAnnotation {
  770. if lineAnnotation.isMeasure {
  771. colors = KMAnnotationPropertiesColorManager.manager.measureColors
  772. } else {
  773. colors = KMAnnotationPropertiesColorManager.manager.inkColors
  774. }
  775. }
  776. } else if (annotation.isKind(of: CPDFPolygonAnnotation.self) ||
  777. annotation.isKind(of: CPDFPolylineAnnotation.self)) {
  778. colors = KMAnnotationPropertiesColorManager.manager.measureColors
  779. } else if annotation.isKind(of: CPDFTextAnnotation.self) {
  780. colors = KMAnnotationPropertiesColorManager.manager.anchoredNoteColors
  781. } else if annotation.isKind(of: CSelfSignAnnotation.self) {
  782. colors = KMAnnotationPropertiesColorManager.manager.selfSignColors
  783. }
  784. let colorAProperty = ComponentCColorProperty(colorType: .color, state: .normal, isCustom: false, color: colors[0])
  785. let colorBProperty = ComponentCColorProperty(colorType: .color, state: .normal, isCustom: false, color: colors[1])
  786. let colorCProperty = ComponentCColorProperty(colorType: .color, state: .normal, isCustom: false, color: colors[2])
  787. let colorDProperty = ComponentCColorProperty(colorType: .color, state: .normal, isCustom: false, color: colors[3])
  788. let colorEProperty = ComponentCColorProperty(colorType: .color, state: .normal, isCustom: true, color: colors[4])
  789. shapeColorGroup.setUpWithColorPropertys([colorAProperty, colorBProperty, colorCProperty, colorDProperty], customItemProperty: colorEProperty)
  790. shapeColorGroup.delegate = self
  791. }
  792. private func configFormPopUI() {
  793. let inputFiedProperty: ComponentInputProperty = ComponentInputProperty(size: .s,
  794. state:.pressed ,
  795. isError: false,
  796. showPrefix: false,
  797. showSuffix: false,
  798. showClear: false,
  799. isDisabled: false,
  800. placeholder: "File Name",
  801. text: "")
  802. fildNameInput.properties = inputFiedProperty
  803. fildNameInput.delegate = self
  804. let inputGropProperty: ComponentInputProperty = ComponentInputProperty(size: .s,
  805. state:.pressed ,
  806. isError: false,
  807. showPrefix: false,
  808. showSuffix: false,
  809. showClear: false,
  810. isDisabled: false,
  811. placeholder: "Grop Name",
  812. text: "")
  813. gropNameInput.properties = inputGropProperty
  814. gropNameInput.delegate = self
  815. }
  816. private func reloadPageUrlData() {
  817. if let destination = linkAnnotation.destination() {
  818. if let page = destination.page() {
  819. paginationView.properties.currentIndex = Int(page.pageIndex() + 1)
  820. paginationView.reloadData()
  821. return
  822. }
  823. }
  824. paginationView.properties.currentIndex = Int(((listView?.currentPageIndex ?? 0) + 1))
  825. paginationView.reloadData()
  826. }
  827. private func refreshPageThum() {
  828. guard let pdfView = listView else {
  829. return
  830. }
  831. let thumbnail = KMNThumbnail.init(document: pdfView.document, currentPageIndex:Int(paginationView.properties.currentIndex - 1))
  832. thumbnail.generateThumImage {[weak self] image in
  833. self?.pageThumImageView.image = image
  834. }
  835. }
  836. //MARK: - Action
  837. @objc func paneButtonClicked(_ sender: NSView) {
  838. isOpenPane = !isOpenPane
  839. paneCallback?(isOpenPane)
  840. }
  841. @objc func fontSizeZoomOutButtonClicked(_ sender: NSView) {
  842. if let freeText = annotationPopMode.annotation as? CPDFFreeTextAnnotation {
  843. if listView?.isEdit(withCurrentFreeText: freeText) == true {
  844. listView?.commitEditAnnotationFreeText(freeText)
  845. }
  846. if annotationPopMode.zoomOutFontSize() {
  847. updatePDFViewCallback?()
  848. }
  849. }
  850. }
  851. @objc func fontSizeZoomInButtonClicked(_ sender: NSView) {
  852. if let freeText = annotationPopMode.annotation as? CPDFFreeTextAnnotation {
  853. if listView?.isEdit(withCurrentFreeText: freeText) == true {
  854. listView?.commitEditAnnotationFreeText(freeText)
  855. }
  856. if annotationPopMode.zoomInFontSize() {
  857. updatePDFViewCallback?()
  858. }
  859. }
  860. }
  861. @objc func fontAlightButtonClicked(_ sender: NSView) {
  862. if fontPopover?.isShown == true {
  863. fontPopover?.close()
  864. } else {
  865. let vc = KMNPopDetailsViewController(nibName: "KMNPopDetailsViewController", bundle: nil)
  866. let createFilePopover: NSPopover = NSPopover.init()
  867. createFilePopover.contentViewController = vc
  868. createFilePopover.delegate = self
  869. createFilePopover.animates = true
  870. createFilePopover.behavior = .semitransient
  871. createFilePopover.setValue(true, forKey: "shouldHideAnchor")
  872. createFilePopover.show(relativeTo: CGRect(x: sender.bounds.origin.x, y: 20, width: sender.bounds.size.width, height: sender.bounds.size.height), of: sender, preferredEdge: .maxY)
  873. vc.detailPopType = .freeTextAlight
  874. vc.fontChangeAlighCallback = { [weak self] alight in
  875. if let freeText = self?.annotationPopMode.annotation as? CPDFFreeTextAnnotation {
  876. if self?.listView?.isEdit(withCurrentFreeText: freeText) == true {
  877. self?.listView?.commitEditAnnotationFreeText(freeText)
  878. }
  879. }
  880. self?.annotationPopMode.setAnnotationAlignment(annotationAlignment: alight)
  881. self?.updatePDFViewCallback?()
  882. self?.fontAlight = alight
  883. }
  884. fontPopover = createFilePopover
  885. vc.fontAlight = fontAlight
  886. }
  887. }
  888. @objc func widthZoomOutButtonClicked(_ sender: NSView) {
  889. if annotationPopMode.zoomOutShapeWidth() {
  890. updatePDFViewCallback?()
  891. }
  892. }
  893. @objc func widthZoomInButtonClicked(_ sender: NSView) {
  894. if annotationPopMode.zoomInShapeWidth() {
  895. updatePDFViewCallback?()
  896. }
  897. }
  898. @objc func pageLinkButtonClicked(_ sender: NSView) {
  899. linkType = .page
  900. }
  901. @objc func urlLinkButtonClicked(_ sender: NSView) {
  902. linkType = .url
  903. }
  904. @objc func emailLinkButtonClicked(_ sender: NSView) {
  905. linkType = .email
  906. }
  907. @objc func alightButtonClicked(_ sender: NSView) {
  908. if ObjectPopover?.isShown == true {
  909. ObjectPopover?.close()
  910. } else {
  911. let vc = KMNPopDetailsViewController(nibName: "KMNPopDetailsViewController", bundle: nil)
  912. let createFilePopover: NSPopover = NSPopover.init()
  913. createFilePopover.contentViewController = vc
  914. createFilePopover.delegate = self
  915. createFilePopover.animates = true
  916. createFilePopover.behavior = .semitransient
  917. createFilePopover.setValue(true, forKey: "shouldHideAnchor")
  918. createFilePopover.show(relativeTo: CGRect(x: sender.bounds.origin.x, y: 20, width: sender.bounds.size.width, height: sender.bounds.size.height), of: sender, preferredEdge: .maxY)
  919. if (annotationPopMode.annotations.count == 2) {
  920. vc.detailPopType = .freeTextDoubleAlight
  921. } else if (annotationPopMode.annotations.count > 2) {
  922. vc.detailPopType = .freeTextMultpleAlight
  923. }
  924. vc.objectChangeAlighCallback = { [weak self] alight in
  925. self?.objectAlignType = alight
  926. self?.updatePDFViewCallback?()
  927. }
  928. ObjectPopover = createFilePopover
  929. vc.objectAlignType = objectAlignType
  930. }
  931. }
  932. @objc func linkGoButtonClicked(_ sender: NSView) {
  933. if linkType == .url {
  934. guard let webString = linkAnnotation.url() else { return }
  935. if let data = URL(string: webString) {
  936. NSWorkspace.shared.open(data)
  937. }
  938. } else if linkType == .email {
  939. guard let emailString = linkAnnotation.url() else {
  940. let alert = NSAlert()
  941. alert.alertStyle = .critical
  942. alert.messageText = NSLocalizedString("Invalid Email. Please try again.", comment: "")
  943. alert.runModal()
  944. return
  945. }
  946. if KMNTools.isValidateEmail(emailString) {
  947. let alert = NSAlert()
  948. alert.alertStyle = .critical
  949. alert.messageText = NSLocalizedString("Invalid Email. Please try again.", comment: "")
  950. alert.runModal()
  951. } else {
  952. NSWorkspace.shared.open(URL(string: emailString)!)
  953. }
  954. } else if linkType == .page {
  955. if let destination = linkAnnotation.destination() {
  956. listView?.go(to: destination)
  957. closeWindow(listView: listView)
  958. }
  959. }
  960. }
  961. @objc func linkBackButtonClicked(_ sender: NSView) {
  962. linkType = .linkSetting
  963. }
  964. @objc func fontColorChange(_ sender: Any) {
  965. if let color = (sender as? NSColorPanel)?.color {
  966. if let freeText = annotationPopMode.annotation as? CPDFFreeTextAnnotation {
  967. if listView?.isEdit(withCurrentFreeText: freeText) == true {
  968. listView?.commitEditAnnotationFreeText(freeText)
  969. }
  970. }
  971. annotationPopMode.setAnnotationFontColor(annotationFontColor: color)
  972. fontColor = color
  973. updatePDFViewCallback?()
  974. }
  975. }
  976. }
  977. //MARK: - ComponentCColorDelegate
  978. extension KMNPopAnnotationWindowController: ComponentCColorDelegate {
  979. func componentCColorDidChooseColor(_ view: NSView, _ color: NSColor?) {
  980. if(view == generaColorGroup) {
  981. self.annotationPopMode.setAnnotationColor(annotationColor: color)
  982. updatePDFViewCallback?()
  983. } else if (view == shapeColorGroup) {
  984. self.annotationPopMode.setAnnotationColor(annotationColor: color)
  985. updatePDFViewCallback?()
  986. } else if (view == fontColorItem) {
  987. fontColorItem.properties?.state = .normal
  988. fontColorItem.reloadData()
  989. let colorPanel = NSColorPanel.shared
  990. colorPanel.setTarget(self)
  991. colorPanel.showsAlpha = false
  992. colorPanel.setAction(#selector(self.fontColorChange(_:)))
  993. colorPanel.orderFront(nil)
  994. }
  995. }
  996. }
  997. //MARK: - ComponentInputDelegate
  998. extension KMNPopAnnotationWindowController: ComponentInputDelegate {
  999. func componentInputDidChanged(inputView: ComponentInput) {
  1000. if (inputView == fildNameInput) {
  1001. for i in 0 ..< annotationPopMode.annotations.count {
  1002. let annotation = annotationPopMode.annotations[i]
  1003. if let widgt = annotation as? CPDFWidgetAnnotation {
  1004. widgt.setFieldName(inputView.properties.text)
  1005. }
  1006. }
  1007. updatePDFViewCallback?()
  1008. } else if (inputView == gropNameInput) {
  1009. for i in 0 ..< annotationPopMode.annotations.count {
  1010. let annotation = annotationPopMode.annotations[i]
  1011. if let button = annotation as? CPDFButtonWidgetAnnotation {
  1012. button.setButtonWidgetStateString(inputView.properties.text)
  1013. }
  1014. }
  1015. updatePDFViewCallback?()
  1016. } else if (inputView == urlInput) {
  1017. }
  1018. }
  1019. func componentInputDidEndEditing(inputView: ComponentInput) {
  1020. if (inputView == fildNameInput) {
  1021. } else if (inputView == gropNameInput) {
  1022. } else if (inputView == urlInput) {
  1023. if linkType == .email {
  1024. let emailString = urlInput.properties.text
  1025. if KMNTools.isValidateEmail(emailString) {
  1026. linkAnnotation.setDestination(nil)
  1027. let linkUrlPath = KMNTools.judgeEmailURL(emailString)
  1028. linkAnnotation.setURL(linkUrlPath)
  1029. updatePDFViewCallback?()
  1030. }
  1031. } else if linkType == .url {
  1032. let webString = urlInput.properties.text
  1033. linkAnnotation.setDestination(nil)
  1034. let linkUrlPath = KMNTools.judgeWebURL(webString)
  1035. linkAnnotation.setURL(linkUrlPath)
  1036. updatePDFViewCallback?()
  1037. }
  1038. }
  1039. }
  1040. }
  1041. //MARK: - ComponentSelectDelegate
  1042. extension KMNPopAnnotationWindowController: ComponentSelectDelegate {
  1043. func componentSelectDidSelect(view: ComponentSelect?, menuItemProperty: ComponentMenuitemProperty?) {
  1044. if(view == fontNameSelect) {
  1045. if menuItemProperty?.identifier == "1" {
  1046. let familyName = fontNameSelect.properties.text ?? "Helvetica"
  1047. let styles = CPDFFont.fontNames(forFamilyName: familyName)
  1048. compdfFont = CPDFFont(familyName: familyName, fontStyle: styles.first ?? "")
  1049. annotationPopMode.setAnnotationFont(font: compdfFont ?? CPDFFont.init(familyName: "Helvetica", fontStyle: ""))
  1050. } else if menuItemProperty?.identifier == "2"{ //字体样式
  1051. compdfFont = CPDFFont(familyName: compdfFont?.familyName ?? "Helvetica", fontStyle: menuItemProperty?.text ?? "")
  1052. annotationPopMode.setAnnotationFont(font: compdfFont ?? CPDFFont.init(familyName: "Helvetica", fontStyle: ""))
  1053. }
  1054. }
  1055. }
  1056. }
  1057. //MARK: - ComponentSelectDelegate
  1058. extension KMNPopAnnotationWindowController: NSPopoverDelegate {
  1059. func popoverWillClose(_ notification: Notification) {
  1060. if fontPopover == (notification.object as? NSPopover) {
  1061. if(fontAlightButton.properties.state == .pressed) {
  1062. fontAlightButton.properties.state = .normal
  1063. fontAlightButton.reloadData()
  1064. }
  1065. } else if ObjectPopover == (notification.object as? NSPopover) {
  1066. if(alightButton.properties.state == .pressed) {
  1067. alightButton.properties.state = .normal
  1068. alightButton.reloadData()
  1069. }
  1070. }
  1071. }
  1072. }
  1073. //MARK: - ComponentPaginationDelegate
  1074. extension KMNPopAnnotationWindowController: ComponentPaginationDelegate {
  1075. public func componentPaginationDidValueChanged(pagination: ComponentPagination) {
  1076. let pageIndex = pagination.properties.currentIndex
  1077. if let page = listView?.document.page(at: UInt(pageIndex)) {
  1078. linkAnnotation.setURL(nil)
  1079. let destination = CPDFDestination(document: listView?.document ?? CPDFDocument(), pageIndex: Int(paginationView.properties.currentIndex - 1))
  1080. linkAnnotation.setDestination(destination)
  1081. refreshPageThum()
  1082. reloadPageUrlData()
  1083. updatePDFViewCallback?()
  1084. }
  1085. }
  1086. }