KMLeftSideViewController.swift 138 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015
  1. //
  2. // KMLeftSideViewController.swift
  3. // PDF Reader Pro
  4. //
  5. // Created by lxy on 2022/10/10.
  6. //
  7. import Cocoa
  8. @objc protocol KMLeftSideViewControllerDelegate {
  9. @objc optional func controlStateChange(_ obj: KMLeftSideViewController,show:Bool)
  10. @objc optional func enterEditMode(_ obj: KMLeftSideViewController, _ pages: [Int])
  11. @objc optional func searchAction(searchString:String, isCase:Bool)
  12. @objc optional func controller(_ controller: KMLeftSideViewController, dispayDidChange dispay: KMPDFDisplayType)
  13. @objc optional func controller(controller: KMLeftSideViewController, itemClick item: Any?, itemKey: KMItemKey, params: Any?)
  14. @objc optional func controller(controller: KMLeftSideViewController, bookMarkDidChange bookMarks: [KMBookMarkItem])
  15. @objc optional func controller(controller: KMLeftSideViewController, rotateType: KMRotateType)
  16. @objc optional func controller(controller: KMLeftSideViewController, listViewSelectionDidChange object: Any?, info: [String : Any]?)
  17. }
  18. extension KMLeftSideViewController.Key {
  19. static let disableTableToolTipsKey = "SKDisableTableToolTips"
  20. }
  21. class KMLeftSideViewController: KMSideViewController {
  22. @IBOutlet var segmentedControl: KMSegmentedControl!
  23. @IBOutlet var thumbnailTableView: KMThumbnailTableView!
  24. @IBOutlet var tocOutlineView: KMTocOutlineView!
  25. @IBOutlet var noteOutlineView: KMNoteOutlineView!
  26. @IBOutlet var findTableView: KMBotaTableView!
  27. @IBOutlet var groupedFindTableView: KMBotaTableView!
  28. @IBOutlet var snapshotTableView: KMBotaTableView!
  29. @IBOutlet weak var leftListView: NSView!
  30. @IBOutlet var searchViewController: KMBotaSearchViewController!
  31. @IBOutlet weak var toolButtonBox: NSBox!
  32. @IBOutlet weak var toolButtonBoxLayoutConstraint: NSLayoutConstraint!
  33. @IBOutlet weak var snapshotNormalView: NSView!
  34. @IBOutlet weak var snapshotLabel: NSTextField!
  35. @IBOutlet weak var snapshotNormalMoreButton: NSButton!
  36. @IBOutlet weak var snapshotNormalSearchButton: NSButton!
  37. @IBOutlet weak var snapshotNormalZoomOutButton: NSButton!
  38. @IBOutlet weak var snapshotNormalZoomInButton: NSButton!
  39. @IBOutlet weak var snapshotSearchZoomOutButton: NSButton!
  40. @IBOutlet weak var snapshotSearchZoomInButton: NSButton!
  41. @IBOutlet weak var snapshotSearchField: KMLeftSideViewSearchField!
  42. @IBOutlet weak var snapshotDoneButton: NSButton!
  43. @IBOutlet weak var outlineView: NSView!
  44. @IBOutlet weak var outlineMoreButton: NSButton!
  45. @IBOutlet weak var outlineAddButton: NSButton!
  46. @IBOutlet weak var outlineSearchButton: NSButton!
  47. @IBOutlet weak var outlineLabel: NSTextField!
  48. @IBOutlet weak var outlineSearchField: KMLeftSideViewSearchField!
  49. @IBOutlet weak var outlineDoneButton: NSButton!
  50. @IBOutlet weak var noteView: NSView!
  51. @IBOutlet weak var noteMoreButton: NSButton!
  52. @IBOutlet weak var noteFilterButton: NSButton!
  53. @IBOutlet weak var noteSearchButton: NSButton!
  54. @IBOutlet weak var noteSearchField: KMLeftSideViewSearchField!
  55. @IBOutlet weak var noteTitleLabel: NSTextField!
  56. @IBOutlet weak var noteHeaderView: NSView!
  57. @IBOutlet weak var sortTypeBox: KMBox!
  58. @IBOutlet weak var sortTypeLabel: NSTextField!
  59. @IBOutlet weak var noteSortButton: NSButton!
  60. @IBOutlet weak var noteDoneButton: NSButton!
  61. @IBOutlet weak var thumbnailView: NSView!
  62. @IBOutlet weak var thumbnailZoomOutButton: NSButton!
  63. @IBOutlet weak var thumbnailZoomInButton: NSButton!
  64. @IBOutlet weak var thumbnailTitleLabel: NSTextField!
  65. @IBOutlet weak var emptySearchBox: NSBox!
  66. @IBOutlet weak var emptySearchLabel: NSTextField!
  67. var bookmarkViewC: KMBookMarkViewController?
  68. var stopRepeatLoad: Bool = false
  69. lazy var leftSideEmptyVC: KMLeftSideEmptyFileViewController = {
  70. let vc = KMLeftSideEmptyFileViewController(nibName: "KMLeftSideEmptyFileViewController", bundle: nil)
  71. vc.view.wantsLayer = true
  72. return vc
  73. }()
  74. lazy var leftView: KMBotaLeftView = {
  75. let view = KMBotaLeftView()
  76. return view
  77. }()
  78. static let kMinSidePaneWidth: CGFloat = 270
  79. static let kTinySize: Float = 32.0
  80. static let kSmallSize: Float = 64.0
  81. static let kLargeSize: Float = 128.0
  82. static let kHugeSize: Float = 256.0
  83. static let kFudgeSize: Float = 0.1
  84. static let kOutlineRootBookmarkItem = "Bookmarks"
  85. var isFirst = false //打开阅读页初始化
  86. var type : KMLeftMethodMode = KMLeftMethodMode()
  87. var isShowPanel : Bool = false
  88. weak var delegate: KMLeftSideViewControllerDelegate?
  89. let noteColumnId = NSUserInterfaceItemIdentifier(rawValue: "note")
  90. let authorColumnId = NSUserInterfaceItemIdentifier(rawValue: "author")
  91. struct Key {}
  92. var mwcFlags: MwcFlags = MwcFlags()
  93. let scalingIncrement: Float = 0.1
  94. var filterButtonLayer: NSView?
  95. var moreButtonLayer: KMButtonLayer?
  96. // 缩略图
  97. var thumbnails: [KMThumbnail] = []
  98. var isDisplayPageSize = false
  99. var thumbnailCacheSize: CGFloat = 32 * 3
  100. var updatingThumbnailSelection = false
  101. // 大纲
  102. var isSearchOutlineMode = false
  103. var outlineIgnoreCaseFlag = false
  104. var tocType: KMFoldType = .none
  105. var renamePDFOutline: CPDFOutline?
  106. var renamePDFOutlineTextField: NSTextField?
  107. private var _dragPDFOutline: CPDFOutline?
  108. var updatingOutlineSelection = false
  109. // 注释
  110. var noteTypeDict: [String : Any] = [:]
  111. private var _noteSortType: KMNoteSortType = .none
  112. var noteSortType: KMNoteSortType {
  113. get {
  114. return self._noteSortType
  115. }
  116. set {
  117. self._noteSortType = newValue
  118. NotificationCenter.default.post(name: NSNotification.Name(rawValue: "KMAnnotationSortTypeKeyNotification"), object: self)
  119. }
  120. }
  121. var isAscendSort = false {
  122. didSet {
  123. NotificationCenter.default.post(name: NSNotification.Name(rawValue: "KMAnnotationSortTypeKeyNotification"), object: self)
  124. }
  125. }
  126. var isRenameNoteOutline = false
  127. // 所有注释
  128. var allAnnotations: [CPDFAnnotation] = []
  129. // 注释搜索模式标记
  130. var noteSearchMode = false
  131. // 注释搜索数组
  132. var noteSearchArray: [KMBotaAnnotationBaseModel] = []
  133. // 注释搜索 忽略大小写标识
  134. var caseInsensitiveNoteSearch = false
  135. // 注释列表数据源
  136. var annoListModel: KMAnnotationListModel?
  137. var editNoteTextField: NSTextField?
  138. weak var editNote: CPDFAnnotation?
  139. // 快照
  140. var snapshotCacheSize: CGFloat = 32 * 3
  141. var isSearchSnapshotMode = false
  142. var snapshots: [KMSnapshotModel] = []
  143. var dirtySnapshots: [KMSnapshotWindowController] = []
  144. var searchSnapshots: [KMSnapshotModel] = []
  145. var dataUpdating = false
  146. // 搜索
  147. private var _findState: KMFindState = .none
  148. var findState: KMFindState {
  149. get {
  150. return self._findState
  151. }
  152. set {
  153. if self._findState != newValue {
  154. self._findState = newValue
  155. self.displayFindState()
  156. if self._findState == .content {
  157. self.search(self.searchField)
  158. } else if self.findState == .note {
  159. self.searchNotes(nil)
  160. } else if self.findState == .snapshot {
  161. self.searchNotes(self.searchField)
  162. }
  163. }
  164. }
  165. }
  166. var searchResults : [KMSearchMode] = [] {
  167. didSet {
  168. self.updataLeftSideFindView()
  169. }
  170. }
  171. var groupSearchResults: [KMSearchMode] = []
  172. var findPaneState: KMFindPaneState = .singular
  173. var searchResultIndex: Int = 0
  174. var leftMargin: CGFloat = 0
  175. private var _copysPages: [CPDFPage] = []
  176. var copyPages: [CPDFPage] {
  177. get {
  178. return self._copysPages
  179. }
  180. }
  181. var dragIn = false
  182. let model = KMBotaModel()
  183. private let KPDFThumbnailDoucumentURLForDraggedTypes = NSPasteboard.PasteboardType("KPDFThumbnailDoucumentURLForDraggedTypes")
  184. private var deletePages_ = Set<CPDFPage>()
  185. let userFbHanddler = KMUserFeekbackHanddler()
  186. let noteReplyHanddler = KMNoteReplyHanddler()
  187. deinit {
  188. KMPrint("KMLeftSideViewController deinit.")
  189. NotificationCenter.default.removeObserver(self)
  190. DistributedNotificationCenter.default().removeObserver(self)
  191. }
  192. override var nibName: NSNib.Name? {
  193. return "LeftSideView"
  194. }
  195. convenience init(type : KMLeftMethodMode) {
  196. self.init()
  197. self.type = type
  198. }
  199. override func viewDidLoad() {
  200. super.viewDidLoad()
  201. self.noteReplyHanddler.viewC = self
  202. self.reloadThumbnailSize()
  203. self.reloadSnapshotSize()
  204. }
  205. func showPanelView(show: Bool) {
  206. self.isShowPanel = show
  207. if show {
  208. self.leftView.segmentedControl.selectedSegment = 1
  209. } else {
  210. self.leftView.segmentedControl.selectedSegment = UInt8.max
  211. self.type.methodType = .None
  212. self.delegate?.controlStateChange?(self, show: false)
  213. }
  214. }
  215. func refreshMethodType(methodType: BotaType) {
  216. let newType = KMLeftMethodMode()
  217. var show = true
  218. if self.type.methodType != methodType {
  219. newType.methodType = methodType
  220. }
  221. if self.type.methodType == methodType {
  222. show = false
  223. } else if methodType == .None {
  224. show = false
  225. }
  226. self.type = newType;
  227. self.delegate?.controlStateChange?(self,show:show)
  228. }
  229. override func loadView() {
  230. super.loadView()
  231. self.initSubView()
  232. self.initDefalutValue()
  233. self.searchViewController.loadView()
  234. self.searchViewController.contentView = self.findTableView.enclosingScrollView
  235. self.searchField = self.searchViewController.searchField
  236. self.searchViewController.segmentedControl.setSegmentCount(2, with: 25)
  237. self.searchViewController.segmentedControl.setImage(NSImage(named: KMImageNameUXIconBtnSidebarListNor)!, for: 0)
  238. self.searchViewController.segmentedControl.setImage(NSImage(named: KMImageNameUXIconBtnSidebarPageNor)!, for: 1)
  239. self.searchViewController.segmentedControl.setToolTip(KMLocalizedString("Separate search results", nil), for: 0)
  240. self.searchViewController.segmentedControl.setToolTip(KMLocalizedString("Group search results by page", nil), for: 1)
  241. self.searchViewController.segmentedControl.isBackgroundHighlighted = true
  242. self.searchViewController.segmentedControl.selectedSegment = 0
  243. self.searchViewController.segmentedControl.block = { [weak self] segIndex in
  244. if segIndex == 0 {
  245. self?.findPaneState = .singular
  246. self?.displayFindViewAnimating(false)
  247. } else {
  248. self?.findPaneState = .grouped
  249. self?.displayGroupedFindViewAnimating(false)
  250. }
  251. }
  252. self.leftView.helpButton.coverAction = { [weak self] button, action in
  253. if action == .enter {
  254. let hasEnterPageEdit = self?.mainViewController?.hasEnterPageEdit() ?? false
  255. if hasEnterPageEdit {
  256. return
  257. }
  258. let hasEnterRedact = self?.mainViewController?.hasEnterRedact() ?? false
  259. if hasEnterRedact {
  260. return
  261. }
  262. self?.userFbHanddler.showUserFbPopView(sender: button)
  263. }
  264. }
  265. self.leftView.itemClick = { itemV, idx in
  266. if idx == 1 { // TTS
  267. self.delegate?.controller?(controller: self, itemClick: nil, itemKey: .tts, params: nil)
  268. }
  269. }
  270. self.leftView.segmentedControl.block = { [unowned self] segIndex in
  271. self.toolButtonBox.isHidden = false
  272. self.toolButtonBoxLayoutConstraint.constant = 40.0
  273. if (segIndex == 1) {
  274. if self.type.methodType == .Thumbnail {
  275. self.leftView.segmentedControl.selectedSegment = UInt8.max
  276. self.refreshMethodType(methodType: .None)
  277. return
  278. }
  279. FMTrackEventManager.defaultManager.trackEvent(event: "LeftSidebar", withProperties: ["LeftSidebar_Btn": "Btn_LeftSidebar_Thumbnail"])
  280. self.refreshMethodType(methodType: .Thumbnail)
  281. KMMainThreadExecute {
  282. self.toolButtonBox.contentView = self.thumbnailView
  283. self.displayThumbnailViewAnimating(false)
  284. }
  285. } else if (segIndex == 2) {
  286. if self.type.methodType == .Outline {
  287. self.leftView.segmentedControl.selectedSegment = UInt8.max
  288. self.refreshMethodType(methodType: .None)
  289. return
  290. }
  291. FMTrackEventManager.defaultManager.trackEvent(event: "LeftSidebar", withProperties: ["LeftSidebar_Btn": "Btn_LeftSidebar_Outline"])
  292. self.refreshMethodType(methodType: .Outline)
  293. DispatchQueue.main.async {
  294. self.toolButtonBox.contentView = self.outlineView
  295. self.bookmarkViewC?.view.isHidden = true
  296. self.displayTocViewAnimating(false)
  297. }
  298. } else if (segIndex == 3) {
  299. if self.type.methodType == .Annotation {
  300. self.leftView.segmentedControl.selectedSegment = UInt8.max
  301. self.refreshMethodType(methodType: .None)
  302. return
  303. }
  304. FMTrackEventManager.defaultManager.trackEvent(event: "LeftSidebar", withProperties: ["LeftSidebar_Btn": "Btn_LeftSidebar_Annotation"])
  305. self.refreshMethodType(methodType: .Annotation)
  306. DispatchQueue.main.async {
  307. self.toolButtonBox.contentView = self.noteView
  308. self.bookmarkViewC?.view.isHidden = true
  309. self.displayNoteViewAnimating(false)
  310. }
  311. } else if (segIndex == 4) {
  312. if self.type.methodType == .BookMark {
  313. self.leftView.segmentedControl.selectedSegment = UInt8.max
  314. self.refreshMethodType(methodType: .None)
  315. return
  316. }
  317. // FMTrackEventManager.defaultManager.trackEvent(event: "LeftSidebar", withProperties: ["LeftSidebar_Btn": "Btn_LeftSidebar_Snapshot"])
  318. self.refreshMethodType(methodType: .BookMark)
  319. DispatchQueue.main.async {
  320. self.toolButtonBox.isHidden = true
  321. self.toolButtonBoxLayoutConstraint.constant = 0
  322. self.displayBookmarkController()
  323. }
  324. } else if (segIndex == 5) {
  325. if self.type.methodType == .snapshot {
  326. self.leftView.segmentedControl.selectedSegment = UInt8.max
  327. self.refreshMethodType(methodType: .None)
  328. return
  329. }
  330. FMTrackEventManager.defaultManager.trackEvent(event: "LeftSidebar", withProperties: ["LeftSidebar_Btn": "Btn_LeftSidebar_Snapshot"])
  331. self.refreshMethodType(methodType: .snapshot)
  332. self.toolButtonBox.contentView = self.snapshotNormalView
  333. self.updateSnapshotFilterPredicate()
  334. self.displaySnapshotViewAnimating(false)
  335. self.updataLeftSideSnapView()
  336. } else if (segIndex == 0) {
  337. if self.type.methodType == .Search {
  338. self.leftView.segmentedControl.selectedSegment = UInt8.max
  339. self.refreshMethodType(methodType: .None)
  340. return
  341. }
  342. FMTrackEventManager.defaultManager.trackEvent(event: "LeftSidebar", withProperties: ["LeftSidebar_Btn": "Btn_LeftSidebar_Search"])
  343. self.refreshMethodType(methodType: .Search)
  344. DispatchQueue.main.async {
  345. self.toolButtonBox.isHidden = true
  346. self.toolButtonBoxLayoutConstraint.constant = 0
  347. self.bookmarkViewC?.view.isHidden = true
  348. self.displayFindViewAnimating(false)
  349. }
  350. }
  351. }
  352. let menu = NSMenu()
  353. _ = menu.addItem(title: KMLocalizedString("Whole Words Only", "Menu item title"), action: #selector(toggleWholeWordSearch), target: self)
  354. _ = menu.addItem(title: KMLocalizedString("Ignore Case", "Menu item title"), action: #selector(toggleCaseInsensitiveSearch), target: self)
  355. (self.searchField.cell as? NSSearchFieldCell)?.searchMenuTemplate = menu
  356. (self.searchField.cell as? NSSearchFieldCell)?.placeholderString = KMLocalizedString("Search PDF", "placeholder")
  357. self.searchField.action = #selector(search)
  358. self.searchField.target = self
  359. // [thumbnailTableView setTypeSelectHelper:[SKTypeSelectHelper typeSelectHelperWithMatchOption:SKFullStringMatch]];
  360. // //支持拖拽的文字类型
  361. // [tocOutlineView setTypeSelectHelper:[SKTypeSelectHelper typeSelectHelperWithMatchOption:SKSubstringMatch]];
  362. // NSSortDescriptor *countDescriptor = [[[NSSortDescriptor alloc] initWithKey:SKGroupedSearchResultCountKey ascending:NO] autorelease];
  363. // [groupedFindArrayController setSortDescriptors:[NSArray arrayWithObjects:countDescriptor, nil]];
  364. if KMDataManager.ud_bool(forKey: Self.Key.disableTableToolTipsKey) == false {
  365. self.tocOutlineView.hasImageToolTips = true
  366. self.findTableView.hasImageToolTips = true
  367. self.groupedFindTableView.hasImageToolTips = true
  368. }
  369. self._updateViewColor()
  370. }
  371. func initSubView() {
  372. self.view.addSubview(self.leftView)
  373. // 阿拉伯语言需要设置布局
  374. // self.leftView.frame = NSMakeRect(0, 0, 44, NSHeight(self.view.frame))
  375. // self.leftView.autoresizingMask = [.height]
  376. self.leftView.km_add_leading_constraint()
  377. self.leftView.km_add_top_constraint()
  378. self.leftView.km_add_height_constraint(equalTo: self.view)
  379. self.leftView.km_add_width_constraint(constant: 44)
  380. self.thumb_initSubViews()
  381. self.outline_initSubViews()
  382. self.note_initSubViews()
  383. self.snapshot_initSubViews()
  384. self.search_initSubViews()
  385. }
  386. func initDefalutValue() {
  387. self.view.wantsLayer = true
  388. self.view.layer?.backgroundColor = KMAppearance.Layout.l0Color().cgColor
  389. self.leftView.wantsLayer = true
  390. self.leftView.layer?.backgroundColor = .white
  391. self.leftListView.wantsLayer = true
  392. self.leftListView.layer?.backgroundColor = KMAppearance.Layout.l0Color().cgColor
  393. self.emptySearchLabel.stringValue = KMLocalizedString("No Results",nil)
  394. self.emptySearchLabel.textColor = KMAppearance.Layout.h0Color()
  395. self.emptySearchBox.isHidden = true
  396. self.thumb_initDefalutValue()
  397. self.outline_initDefalutValue()
  398. self.note_initDefalutValue()
  399. self.snapshot_initDefalutValue()
  400. self.search_initDefalutValue()
  401. }
  402. func displayThumbnailViewAnimating(_ animate: Bool) {
  403. self.bookmarkViewC?.view.isHidden = true
  404. if let enclosingScrollView = self.thumbnailTableView.enclosingScrollView {
  405. self.replaceSideView(enclosingScrollView, animate: animate)
  406. }
  407. var frame = self.thumbnailTableView.enclosingScrollView?.frame ?? .zero
  408. frame.origin.y = 0
  409. frame.origin.x = self.leftMargin
  410. frame.size.height = self.thumbnailTableView.enclosingScrollView?.superview?.frame.size.height ?? 0
  411. self.thumbnailTableView.enclosingScrollView?.frame = frame
  412. self.resetThumbnails()
  413. frame = self.noteOutlineView.enclosingScrollView?.frame ?? .zero
  414. frame.origin.y = 0
  415. frame.origin.x = self.leftMargin
  416. frame.size.height = self.noteOutlineView.enclosingScrollView?.superview?.frame.size.height ?? 0
  417. self.noteOutlineView.enclosingScrollView?.frame = frame
  418. frame = self.snapshotTableView.enclosingScrollView?.frame ?? .zero
  419. frame.origin.y = 0
  420. frame.origin.x = self.leftMargin
  421. frame.size.height = self.snapshotTableView.enclosingScrollView?.superview?.frame.size.height ?? 0
  422. self.snapshotTableView.enclosingScrollView?.frame = frame
  423. self.updateThumbnailSelection()
  424. }
  425. func displayFindViewAnimating(_ animate: Bool) {
  426. self.replaceSideView(self.searchViewController.view, animate: animate)
  427. if (self.findState != .content) {
  428. self.findState = .content
  429. } else {
  430. self.displayFindState()
  431. }
  432. var frame = self.searchViewController.view.frame
  433. frame.origin.y = 0
  434. frame.size.height = self.searchViewController.view.superview?.frame.size.height ?? .zero
  435. self.searchViewController.view.frame = frame
  436. frame = self.noteOutlineView.enclosingScrollView?.frame ?? .zero
  437. frame.origin.y = 0
  438. frame.size.height = self.noteOutlineView.enclosingScrollView?.superview?.frame.size.height ?? 0
  439. self.noteOutlineView.enclosingScrollView?.frame = frame
  440. frame = self.snapshotTableView.enclosingScrollView?.frame ?? .zero
  441. frame.origin.y = 0
  442. frame.size.height = self.snapshotTableView.enclosingScrollView?.superview?.frame.size.height ?? 0
  443. self.snapshotTableView.enclosingScrollView?.frame = frame
  444. self.leftSideEmptyVC.emptySnapView.removeFromSuperview()
  445. self.updataLeftSideSnapView()
  446. }
  447. func displayFindState() {
  448. if (self.findState == .content) {
  449. self.displayFind()
  450. } else if (self.findState == .note) {
  451. self.displayNoteFind()
  452. } else if (self.findState == .snapshot) {
  453. self.displaySnapshotFind()
  454. }
  455. }
  456. func updataLeftSideFindView() {
  457. if (self.findState != .content) {
  458. return
  459. }
  460. if (self.searchResults.count > 0) {
  461. self.searchViewController.emptyBox.isHidden = true
  462. self.searchViewController.searchResultsView.isHidden = false
  463. self.searchViewController.searchResultsLabel.stringValue = String(format: KMLocalizedString("%ld Results", "Message in search table header"), self.searchResults.count)
  464. } else {
  465. self.searchViewController.emptyBox.isHidden = false
  466. self.searchViewController.searchResultsView.isHidden = true
  467. }
  468. }
  469. func displayGroupedFindViewAnimating(_ animate: Bool) {
  470. self.replaceSideView(self.searchViewController.view , animate: animate)
  471. if (self.findState != .content) {
  472. self.findState = .content
  473. } else {
  474. self.displayFindState()
  475. }
  476. var frame = self.searchViewController.view.frame
  477. frame.origin.y = 0
  478. frame.size.height = self.searchViewController.view.superview?.frame.size.height ?? 0
  479. self.searchViewController.view.frame = frame
  480. frame = self.noteOutlineView.enclosingScrollView?.frame ?? .zero
  481. frame.origin.y = 0
  482. frame.size.height = self.noteOutlineView.enclosingScrollView?.superview?.frame.size.height ?? 0
  483. self.noteOutlineView.enclosingScrollView?.frame = frame
  484. frame = self.snapshotTableView.enclosingScrollView?.frame ?? .zero
  485. frame.origin.y = 0
  486. frame.size.height = self.snapshotTableView.enclosingScrollView?.superview?.frame.size.height ?? 0
  487. self.snapshotTableView.enclosingScrollView?.frame = frame
  488. self.updataLeftSideSnapView()
  489. }
  490. func displayNoteViewAnimating(_ animate: Bool) {
  491. self.searchViewController.contentView = nil
  492. if let data = self.noteOutlineView.enclosingScrollView {
  493. self.replaceSideView(data, animate: animate)
  494. }
  495. if (self.findState != .note) {
  496. self.findState = .note
  497. } else {
  498. self.displayFindState()
  499. }
  500. var frame = self.noteOutlineView.enclosingScrollView?.frame ?? .zero
  501. frame.origin.y = 0
  502. frame.size.height = self.noteOutlineView.enclosingScrollView?.superview?.frame.size.height ?? 0
  503. self.noteOutlineView.enclosingScrollView?.frame = frame
  504. frame = self.snapshotTableView.enclosingScrollView?.frame ?? .zero
  505. frame.origin.y = 0
  506. frame.size.height = self.snapshotTableView.enclosingScrollView?.superview?.frame.size.height ?? 0
  507. self.snapshotTableView.enclosingScrollView?.frame = frame
  508. let view = self.noteOutlineView.enclosingScrollView
  509. let viewFrame = view?.frame ?? .zero
  510. let emptyVcSize = self.leftSideEmptyVC.emptyAnnotationView.frame.size
  511. self.leftSideEmptyVC.emptyAnnotationView.frame = NSMakeRect((viewFrame.size.width-emptyVcSize.width)/2.0,(viewFrame.size.height-emptyVcSize.height)/2.0, emptyVcSize.width, emptyVcSize.height)
  512. self.reloadAnnotation()
  513. }
  514. func displayBookmarkController() {
  515. if self.bookmarkViewC == nil {
  516. let viewC = KMBookMarkViewController(nibName: "KMBookMarkViewController", bundle: nil)
  517. self.bookmarkViewC = viewC
  518. self.bookmarkViewC?.listView = self.listView
  519. }
  520. if let view = self.bookmarkViewC?.view {
  521. self.bookmarkViewC?.view.isHidden = false
  522. self.bookmarkViewC?.listView = self.listView
  523. self.replaceSideView(view, animate: false)
  524. self.bookmarkViewC?.reloadData()
  525. }
  526. }
  527. func displaySnapshotViewAnimating(_ animate: Bool) {
  528. self.searchViewController.contentView = nil
  529. self.bookmarkViewC?.view.isHidden = true
  530. if let data = self.snapshotTableView.enclosingScrollView {
  531. self.replaceSideView(data, animate: animate)
  532. }
  533. if (self.findState != .snapshot) {
  534. self.findState = .snapshot
  535. } else {
  536. self.displayFindState()
  537. }
  538. var frame = self.snapshotTableView.enclosingScrollView?.frame ?? .zero
  539. frame.origin.y = 0
  540. frame.size.height = self.snapshotTableView.enclosingScrollView?.superview?.frame.size.height ?? 0
  541. self.snapshotTableView.enclosingScrollView?.frame = frame
  542. frame = self.noteOutlineView.enclosingScrollView?.frame ?? .zero
  543. frame.origin.y = 0
  544. frame.size.height = self.noteOutlineView.enclosingScrollView?.superview?.frame.size.height ?? 0
  545. self.noteOutlineView.enclosingScrollView?.frame = frame
  546. frame = self.tocOutlineView.enclosingScrollView?.frame ?? .zero
  547. frame.origin.y = 0
  548. frame.size.height = self.tocOutlineView.enclosingScrollView?.superview?.frame.size.height ?? 0
  549. self.tocOutlineView.enclosingScrollView?.frame = frame
  550. Task { @MainActor in
  551. self.snapshotTableView.reloadData()
  552. }
  553. }
  554. func displayFind() {
  555. self.searchField = self.searchViewController.searchField
  556. let menu = NSMenu()
  557. _ = menu.addItem(title: KMLocalizedString("Whole Words Only", "Menu item title"), action: #selector(toggleWholeWordSearch), target: self)
  558. _ = menu.addItem(title: KMLocalizedString("Ignore Case", "Menu item title"), action: #selector(toggleCaseInsensitiveSearch), target: self)
  559. (self.searchViewController.searchField.cell as? NSSearchFieldCell)?.searchMenuTemplate = menu
  560. (self.searchViewController.searchField.cell as? NSSearchFieldCell)?.placeholderString = KMLocalizedString("Search PDF", "placeholder")
  561. self.searchViewController.searchField.action = #selector(search)
  562. self.searchViewController.searchField.target = self
  563. if (self.findPaneState == .singular) {
  564. self.searchViewController.contentView = self.findTableView.enclosingScrollView
  565. self.findTableView.wantsLayer = true
  566. self.findTableView.layer?.backgroundColor = NSColor.red.cgColor
  567. DispatchQueue.main.async {
  568. self.findTableView.reloadData()
  569. }
  570. } else if (self.findPaneState == .grouped) {
  571. self.searchViewController.contentView = self.groupedFindTableView.enclosingScrollView
  572. var array = KMSearchMode.sortSearchResult(results: self.searchResults)
  573. array.sort(){$0.datas.count > $1.datas.count}
  574. self.groupSearchResults = array
  575. self.groupedFindTableView.reloadData()
  576. }
  577. }
  578. func displayTocViewAnimating(_ animate: Bool) {
  579. if Thread.current.isMainThread {
  580. self.tocOutlineView.reloadData()
  581. } else {
  582. Task { @MainActor in
  583. self.tocOutlineView.reloadData()
  584. }
  585. }
  586. if let data = self.tocOutlineView.enclosingScrollView {
  587. self.replaceSideView(data, animate: animate)
  588. }
  589. var frame = self.tocOutlineView.enclosingScrollView?.frame ?? .zero
  590. frame.origin.y = 0
  591. frame.size.height = self.tocOutlineView.enclosingScrollView?.superview?.frame.size.height ?? 0
  592. self.tocOutlineView.enclosingScrollView?.frame = frame
  593. frame = self.noteOutlineView.enclosingScrollView?.frame ?? .zero
  594. frame.origin.y = 0
  595. frame.size.height = self.noteOutlineView.enclosingScrollView?.superview?.frame.size.height ?? 0
  596. self.noteOutlineView.enclosingScrollView?.frame = frame
  597. frame = self.snapshotTableView.enclosingScrollView?.frame ?? .zero
  598. frame.origin.y = 0
  599. frame.size.height = self.snapshotTableView.enclosingScrollView?.superview?.frame.size.height ?? 0
  600. self.snapshotTableView.enclosingScrollView?.frame = frame
  601. let view = self.tocOutlineView.enclosingScrollView
  602. let viewFrame = view?.frame ?? .zero
  603. let emptyVcSize = self.leftSideEmptyVC.emptyOutlineView.frame.size
  604. self.leftSideEmptyVC.emptyOutlineView.frame = NSMakeRect((viewFrame.size.width-emptyVcSize.width)/2.0,(viewFrame.size.height-emptyVcSize.height)/2.0, emptyVcSize.width, emptyVcSize.height);
  605. DispatchQueue.main.async {
  606. self.tocOutlineView.reloadData()
  607. self.updateOutlineSelection()
  608. }
  609. }
  610. func displayNoteFind() {
  611. self.searchField = self.noteSearchField
  612. let menu = NSMenu()
  613. _ = menu.addItem(title: KMLocalizedString("Ignore Case", "Menu item title"), action: #selector(toggleCaseInsensitiveNoteSearch), target: self)
  614. (self.noteSearchField.cell as? NSSearchFieldCell)?.searchMenuTemplate = menu
  615. (self.noteSearchField.cell as? NSSearchFieldCell)?.placeholderString = KMLocalizedString("Search Notes", "placeholder")
  616. self.noteSearchField.action = #selector(searchNotes)
  617. self.noteSearchField.target = self
  618. }
  619. func displaySnapshotFind() {
  620. self.searchField = self.snapshotSearchField;
  621. let menu = NSMenu()
  622. _ = menu.addItem(title: KMLocalizedString("Ignore Case", "Menu item title"), action: #selector(toggleCaseInsensitiveNoteSearch), target: self)
  623. (self.snapshotSearchField.cell as? NSSearchFieldCell)?.searchMenuTemplate = menu
  624. (self.snapshotSearchField.cell as? NSSearchFieldCell)?.placeholderString = KMLocalizedString("Search Snapshots", "placeholder")
  625. self.snapshotSearchField.action = #selector(searchNotes)
  626. self.snapshotSearchField.target = self
  627. self.snapshotSearchField.delegate = self
  628. }
  629. func resetThumbnails(ks: Bool = true) {
  630. self.thumbnails.removeAll()
  631. KMThumbnailCache.shared.clearCache()
  632. let pageLabels = self.pdfDocument()?.pageLabels() ?? []
  633. if (pageLabels.isEmpty == false) {
  634. for (i, label) in pageLabels.enumerated() {
  635. let thumbnail = KMThumbnail(image: nil, label: label, pageIndex: i)
  636. thumbnail.dirty = true
  637. self.thumbnails.append(thumbnail)
  638. }
  639. }
  640. KMMainThreadExecute {
  641. let ris = self.thumbnailTableView.selectedRowIndexes
  642. self.thumbnailTableView.reloadData()
  643. if ks {
  644. self.thumbnailTableView.km_safe_selectRowIndexes(ris, byExtendingSelection: false)
  645. }
  646. }
  647. }
  648. @IBAction func leftSideViewDoneButtonAction(_ sender: AnyObject?) {
  649. let button = sender as? NSButton
  650. let tag = button?.tag ?? 0
  651. if (tag == 310) {
  652. self.outlineSearchField.isHidden = true
  653. self.outlineDoneButton.isHidden = true
  654. self.outlineLabel.isHidden = false
  655. self.outlineSearchButton.isHidden = false
  656. self.outlineMoreButton.isHidden = false
  657. self.outlineAddButton.isHidden = false
  658. } else if (tag == 311) {
  659. self.noteSearchField.isHidden = true
  660. self.noteTitleLabel.isHidden = false
  661. self.noteSearchButton.isHidden = false
  662. self.noteDoneButton.isHidden = true
  663. self.noteFilterButton.isHidden = false
  664. self.noteMoreButton.isHidden = false
  665. } else if (tag == 312) {
  666. self.snapshotSearchField.isHidden = true
  667. // leftSideController.snapshotSearchZoomInButton.hidden = YES;
  668. self.snapshotNormalSearchButton.isHidden = false
  669. self.snapshotDoneButton.isHidden = true
  670. self.snapshotLabel.isHidden = false
  671. self.snapshotNormalZoomOutButton.isHidden = false
  672. self.snapshotNormalZoomInButton.isHidden = false
  673. }
  674. }
  675. @IBAction func thumbnailSizeScaling(_ sender: NSButton) {
  676. let tag = sender.tag
  677. if (tag == 0 || tag == 1) {
  678. var scaling = KMDataManager.ud_float(forKey: Self.Key.thumbSizeScaling)
  679. if (scaling <= 0) {
  680. scaling = 1
  681. }
  682. if (tag == 0) { // Zoom In
  683. scaling += self.scalingIncrement
  684. } else if (tag == 1) { // Zoom Out
  685. scaling -= self.scalingIncrement
  686. if scaling <= 0.4 {
  687. return
  688. }
  689. }
  690. KMDataManager.ud_set(scaling, forKey: Self.Key.thumbSizeScaling)
  691. let selectRow = self.thumbnailTableView.selectedRow
  692. self.thumbnailTableView.reloadData()
  693. self.thumbnailTableView.selectRowIndexes(IndexSet(integer: selectRow), byExtendingSelection: false)
  694. } else if (tag == 2 || tag == 3) {
  695. var scaling = KMDataManager.ud_float(forKey: Self.Key.snapshotSizeScaling)
  696. if (scaling <= 0) {
  697. scaling = 1
  698. }
  699. if (tag == 2) { // Zoom In
  700. scaling += self.scalingIncrement
  701. } else if (tag == 3) { // Zoom Out
  702. scaling -= self.scalingIncrement
  703. }
  704. KMDataManager.ud_set(scaling, forKey: Self.Key.snapshotSizeScaling)
  705. let selectRow = self.snapshotTableView.selectedRow
  706. self.snapshotTableView.reloadData()
  707. self.snapshotTableView.selectRowIndexes(IndexSet(integer: selectRow), byExtendingSelection: false)
  708. }
  709. }
  710. func tableView(_ tv: NSTableView, cutRowsWithIndexes rowIndexes: IndexSet) {
  711. if tv.isEqual(to: self.thumbnailTableView) {
  712. self._copysPages.removeAll()
  713. for idx in rowIndexes {
  714. if (idx != NSNotFound) {
  715. if let page = self.pdfDocument()?.page(at: UInt(idx))?.copy() as? CPDFPage {
  716. self._copysPages.append(page)
  717. }
  718. }
  719. }
  720. self.tableView(tv, deleteRowsWithIndexes: rowIndexes)
  721. }
  722. }
  723. }
  724. // MARK: - KMBotaTableViewDelegate
  725. extension KMLeftSideViewController: KMBotaTableViewDelegate {
  726. func tableView(_ aTableView: NSTableView, canCopyRowsWithIndexes rowIndexes: IndexSet) -> Bool {
  727. if aTableView.isEqual(to: self.thumbnailTableView) || aTableView.isEqual(to: self.findTableView) || aTableView.isEqual(to: self.groupedFindTableView) {
  728. return rowIndexes.count > 0
  729. }
  730. return false
  731. }
  732. func tableView(_ aTableView: NSTableView, canPasteFromPasteboard pboard: NSPasteboard) -> Bool {
  733. if aTableView.isEqual(to: self.thumbnailTableView) {
  734. if self._copysPages.count > 0 {
  735. return true
  736. }
  737. }
  738. return false
  739. }
  740. func tableView(_ aTableView: NSTableView, canDeleteRowsWithIndexes rowIndexes: IndexSet) -> Bool {
  741. if aTableView.isEqual(to: self.snapshotTableView) {
  742. return rowIndexes.count > 0
  743. } else if aTableView.isEqual(to: self.thumbnailTableView) {
  744. if self.pageCount() <= 1 {
  745. return false
  746. }
  747. return true
  748. }
  749. return false
  750. }
  751. func tableView(_ aTableView: NSTableView, pasteFromPasteboard pboard: NSPasteboard?) {
  752. if IAPProductsManager.default().isAvailableAllFunction() == false {
  753. KMPurchaseCompareWindowController.sharedInstance().showWindow(nil)
  754. return
  755. }
  756. if aTableView.isEqual(to: self.thumbnailTableView) {
  757. if self._copysPages.count > 0 {
  758. let index = self.currentPageIndex()
  759. if (index == NSNotFound) {
  760. return
  761. }
  762. let idx = index + 1
  763. for page in self._copysPages.reversed() {
  764. self.pdfDocument()?.insertPageObject(page, at: UInt(idx))
  765. self.layoutDocumentView()
  766. self.resetThumbnails()
  767. let pageIndex = min(idx, self.pageCount()-1)
  768. self.listView?.go(toPageIndex: Int(pageIndex), animated: true)
  769. (self.listView?.undoManager?.prepare(withInvocationTarget: self) as AnyObject).tableView(self.thumbnailTableView, deleteRowsWithIndexes: IndexSet(integer: IndexSet.Element(index)))
  770. }
  771. }
  772. }
  773. }
  774. func tableViewMoveLeft(_ aTableView: NSTableView) {
  775. if aTableView.isEqual(to: self.findTableView) || aTableView.isEqual(to: self.groupedFindTableView) {
  776. self.updateFindResultHighlightsForDirection(.selectingPrevious)
  777. }
  778. }
  779. func tableViewMoveRight(_ aTableView: NSTableView) {
  780. if aTableView.isEqual(to: self.findTableView) || aTableView.isEqual(to: self.groupedFindTableView) {
  781. self.updateFindResultHighlightsForDirection(.selectingNext)
  782. }
  783. }
  784. func tableViewMoveUp(_ aTableView: NSTableView) {
  785. if aTableView.isEqual(to: self.findTableView) {
  786. self.findTableView.km_safe_selectRowIndexes(.init(integer: self.findTableView.selectedRow-1), byExtendingSelection: false)
  787. self.findTableView.scrollRowToVisible(self.findTableView.selectedRow)
  788. }
  789. }
  790. func tableViewMoveDown(_ aTableView: NSTableView) {
  791. if aTableView.isEqual(to: self.findTableView) {
  792. self.findTableView.km_safe_selectRowIndexes(.init(integer: self.findTableView.selectedRow+1), byExtendingSelection: false)
  793. self.findTableView.scrollRowToVisible(self.findTableView.selectedRow)
  794. }
  795. }
  796. func tableView(_ aTableView: NSTableView, imageContextForRow rowIndex: Int) -> AnyObject? {
  797. if aTableView.isEqual(to: self.findTableView) {
  798. if rowIndex >= self.searchResults.count {
  799. return nil
  800. }
  801. let model = self.searchResults[rowIndex]
  802. let selection = model.selection
  803. let x = selection.bounds.origin.x + NSWidth(selection.bounds) * 0.5
  804. let y = selection.bounds.origin.y + NSHeight(selection.bounds) * 0.5
  805. let point = NSPoint(x: x, y: y)
  806. return CPDFDestination(document: self.pdfDocument(), pageIndex: Int(model.selectionPageIndex), at: point, zoom: self.scaleFactor().cgFloat)
  807. } else if aTableView.isEqual(to: self.groupedFindTableView) {
  808. if rowIndex >= self.groupSearchResults.count {
  809. return nil
  810. }
  811. // let model = self.groupSearchResults[rowIndex]
  812. // let selection = model.selection
  813. // let point = NSPoint(x: NSWidth(selection.bounds) * 0.5, y: NSHeight(selection.bounds) * 0.5)
  814. // return CPDFDestination(document: self.listView.document, pageIndex: Int(selection.page.pageIndex()), at: point, zoom: self.listView.scaleFactor)
  815. }
  816. return nil
  817. }
  818. }
  819. // MARK: - 扩展
  820. extension KMLeftSideViewController {
  821. public func selectType(_ type: BotaType) {
  822. // 更新 type
  823. var show = true
  824. if (self.type.methodType == .None) {
  825. show = true
  826. } else {
  827. if (self.type.methodType == type) {
  828. show = false
  829. } else {
  830. show = true
  831. }
  832. }
  833. self.type = self.getMethodMode(show ? type : .None)
  834. // 将事件传递出去
  835. self.delegate?.controlStateChange?(self,show:show)
  836. }
  837. private func getMethodMode(_ type: BotaType) -> KMLeftMethodMode {
  838. let mode = KMLeftMethodMode()
  839. mode.methodType = type
  840. switch type {
  841. case .None:
  842. mode.methodName = ""
  843. break
  844. case .Thumbnail:
  845. mode.methodName = thumbnailMethodKey
  846. break
  847. case .Outline:
  848. mode.methodName = outlineMethodKey
  849. break
  850. case .BookMark:
  851. mode.methodName = bookMarkMethodKey
  852. break
  853. case .Annotation:
  854. mode.methodName = anntationMethodKey
  855. break
  856. case .Search:
  857. mode.methodName = searchMethodKey
  858. break
  859. case .From:
  860. mode.methodName = formMethodKey
  861. break
  862. case .Signature:
  863. mode.methodName = signatureMethodKey
  864. break
  865. case .snapshot:
  866. mode.methodName = snapshotMethodKey
  867. }
  868. return mode
  869. }
  870. }
  871. //MARK: Cache
  872. extension KMLeftSideViewController {
  873. func clearNotification() {}
  874. }
  875. // MARK: - Private Methods
  876. extension KMLeftSideViewController {
  877. func updateViewColor() {
  878. self._updateViewColor()
  879. }
  880. private func _updateViewColor() {
  881. if(KMAppearance.isDarkMode()){
  882. self.leftListView.layer?.backgroundColor = NSColor(red: 0.149, green: 0.157, blue: 0.169, alpha: 1).cgColor
  883. self.snapshotNormalView.layer?.backgroundColor = NSColor(red: 0.149, green: 0.157, blue: 0.169, alpha: 1).cgColor
  884. self.thumbnailView.layer?.backgroundColor = NSColor(red: 0.149, green: 0.157, blue: 0.169, alpha: 1).cgColor
  885. self.noteView.layer?.backgroundColor = NSColor(red: 0.149, green: 0.157, blue: 0.169, alpha: 1).cgColor
  886. self.outlineView.layer?.backgroundColor = NSColor(red: 0.149, green: 0.157, blue: 0.169, alpha: 1).cgColor
  887. self.view.layer?.backgroundColor = NSColor(red: 0.149, green: 0.157, blue: 0.169, alpha: 1).cgColor
  888. self.thumbnailTableView.backgroundColor = NSColor(red: 0.149, green: 0.157, blue: 0.169, alpha: 1)
  889. self.groupedFindTableView.backgroundColor = NSColor(red: 0.149, green: 0.157, blue: 0.169, alpha: 1)
  890. self.tocOutlineView.backgroundColor = NSColor(red: 0.149, green: 0.157, blue: 0.169, alpha: 1)
  891. self.findTableView.backgroundColor = NSColor(red: 0.149, green: 0.157, blue: 0.169, alpha: 1)
  892. self.snapshotSearchField.layer?.backgroundColor = NSColor(red: 0.224, green: 0.235, blue: 0.243, alpha: 1).cgColor
  893. self.outlineSearchField.layer?.backgroundColor = NSColor(red: 0.224, green: 0.235, blue: 0.243, alpha: 1).cgColor
  894. self.noteSearchField.layer?.backgroundColor = NSColor(red: 0.224, green: 0.235, blue: 0.243, alpha: 1).cgColor
  895. self.segmentedControl.layer?.backgroundColor = NSColor(red: 0.224, green: 0.235, blue: 0.243, alpha: 1).cgColor
  896. self.snapshotSearchField.backgroundColor = NSColor(red: 0.224, green: 0.235, blue: 0.243, alpha: 1)
  897. self.outlineSearchField.backgroundColor = NSColor(red: 0.224, green: 0.235, blue: 0.243, alpha: 1)
  898. self.noteSearchField.backgroundColor = NSColor(red: 0.224, green: 0.235, blue: 0.243, alpha: 1)
  899. } else {
  900. self.leftListView.layer?.backgroundColor = NSColor(red: 0.988, green: 0.992, blue: 1, alpha: 1).cgColor
  901. self.snapshotNormalView.layer?.backgroundColor = NSColor(red: 0.988, green: 0.992, blue: 1, alpha: 1).cgColor
  902. self.thumbnailView.layer?.backgroundColor = NSColor(red: 0.988, green: 0.992, blue: 1, alpha: 1).cgColor
  903. self.noteView.layer?.backgroundColor = NSColor(red: 0.988, green: 0.992, blue: 1, alpha: 1).cgColor
  904. self.outlineView.layer?.backgroundColor = NSColor(red: 0.988, green: 0.992, blue: 1, alpha: 1).cgColor
  905. self.view.layer?.backgroundColor = NSColor(red: 0.988, green: 0.992, blue: 1, alpha: 1).cgColor
  906. self.thumbnailTableView.backgroundColor = NSColor(red: 0.988, green: 0.992, blue: 1.000, alpha: 1)
  907. self.groupedFindTableView.backgroundColor = NSColor(red: 0.988, green: 0.992, blue: 1.000, alpha: 1)
  908. self.tocOutlineView.backgroundColor = NSColor(red: 0.988, green: 0.992, blue: 1.000, alpha: 1)
  909. self.findTableView.backgroundColor = NSColor(red: 0.988, green: 0.992, blue: 1.000, alpha: 1)
  910. self.snapshotSearchField.layer?.backgroundColor = NSColor(red: 0.922, green: 0.925, blue: 0.941, alpha: 1).cgColor
  911. self.outlineSearchField.layer?.backgroundColor = NSColor(red: 0.922, green: 0.925, blue: 0.941, alpha: 1).cgColor
  912. self.noteSearchField.layer?.backgroundColor = NSColor(red: 0.922, green: 0.925, blue: 0.941, alpha: 1).cgColor
  913. self.segmentedControl.layer?.backgroundColor = NSColor(red: 0.922, green: 0.925, blue: 0.941, alpha: 1).cgColor
  914. self.snapshotSearchField.backgroundColor = NSColor(red: 0.922, green: 0.925, blue: 0.941, alpha: 1)
  915. self.outlineSearchField.backgroundColor = NSColor(red: 0.922, green: 0.925, blue: 0.941, alpha: 1)
  916. self.noteSearchField.backgroundColor = NSColor(red: 0.922, green: 0.925, blue: 0.941, alpha: 1)
  917. }
  918. }
  919. func hasContainString(_ searchString: String, rootOutline outline: CPDFOutline) -> Bool {
  920. var label = outline.label ?? ""
  921. var searchLabel = searchString
  922. if self.outlineIgnoreCaseFlag {
  923. label = label.lowercased()
  924. searchLabel = searchLabel.lowercased()
  925. }
  926. if label.contains(searchLabel) {
  927. // if ([outline.label rangeOfString:searchString options:self.outlineIgnoreCaseFlag?NSCaseInsensitiveSearch:0].location != NSNotFound){
  928. return true
  929. } else {
  930. var subHas = false
  931. for i in 0 ..< outline.numberOfChildren {
  932. if let subOutline = outline.child(at: i) {
  933. subHas = self.hasContainString(searchString, rootOutline: subOutline)
  934. } else {
  935. continue
  936. }
  937. if (subHas) {
  938. break
  939. }
  940. }
  941. return subHas
  942. }
  943. }
  944. }
  945. // MARK: - NSTableViewDelegate, NSTableViewDataSource
  946. extension KMLeftSideViewController: NSTableViewDelegate, NSTableViewDataSource {
  947. func numberOfRows(in tableView: NSTableView) -> Int {
  948. if tableView.isEqual(to: self.thumbnailTableView) {
  949. return self.thumbnails.count
  950. } else if tableView.isEqual(to: self.findTableView) {
  951. return self.searchResults.count
  952. } else if tableView.isEqual(to: self.groupedFindTableView) {
  953. return self.groupSearchResults.count
  954. } else if tableView.isEqual(to: self.snapshotTableView) {
  955. if self.isSearchSnapshotMode {
  956. return self.searchSnapshots.count
  957. }
  958. return self.snapshots.count
  959. }
  960. return 0
  961. }
  962. func tableView(_ tableView: NSTableView, viewFor tableColumn: NSTableColumn?, row: Int) -> NSView? {
  963. if tableView.isEqual(to: self.thumbnailTableView) {
  964. let cell = tableView.makeView(withIdentifier: KMThumbnailTableviewCell.km_identifier, owner: self) as! KMThumbnailTableviewCell
  965. if self.thumbnails.count > row {
  966. let thumbnail = self.thumbnails[row]
  967. let page = self.pdfDocument()?.page(at: UInt(row))
  968. cell.pageNumLabel.stringValue = thumbnail.label
  969. cell.pageView.isThumb = true
  970. cell.pageView.page = page
  971. let bks = self.listView?.document.bookmarks()
  972. var hasBookmark = false
  973. for bk in bks ?? [] {
  974. if row == bk.pageIndex {
  975. hasBookmark = true
  976. break
  977. }
  978. }
  979. cell.bkIcon.isHidden = !hasBookmark
  980. if (self.isDisplayPageSize) {
  981. cell.sizeLabel.isHidden = false
  982. //获取Page的真实尺寸
  983. let rect = page?.bounds(for: .cropBox) ?? .zero
  984. let w = KMPageSizeTool.conversion(withUnit: "mm", value: (NSWidth(rect)/595 * 210))
  985. let h = KMPageSizeTool.conversion(withUnit: "mm", value: (NSHeight(rect)/842 * 297))
  986. if let data = page?.rotation, data == 90 || data == 270 {
  987. cell.sizeLabel.stringValue = String(format: "%.f × %.f %@", h.stringToCGFloat(), w.stringToCGFloat(), KMLocalizedString("mm", nil))
  988. } else {
  989. cell.sizeLabel.stringValue = String(format: "%.f × %.f %@", w.stringToCGFloat(), h.stringToCGFloat(), KMLocalizedString("mm", nil))
  990. }
  991. } else {
  992. cell.sizeLabel.stringValue = "0 mm"
  993. cell.sizeLabel.isHidden = true
  994. }
  995. cell.sizeTopConstant.constant = cell.sizeLabel.isHidden ? -cell.sizeLabel.frame.size.height : 0
  996. if (self.thumbnailTableView.selectedRowIndexes.contains(row)) {
  997. cell.isSelectCell = true
  998. } else {
  999. cell.isSelectCell = false
  1000. }
  1001. }
  1002. return cell
  1003. } else if (tableView.isEqual(to: self.findTableView)) {
  1004. let cell = tableView.makeView(withIdentifier: KMFindTableviewCell.km_identifier, owner: self) as! KMFindTableviewCell
  1005. if searchResults.count > row {
  1006. let selection = searchResults[row]
  1007. if let data = tableColumn?.identifier.rawValue, data == kResultsColumnId.rawValue {
  1008. cell.resultLabel.attributedStringValue = selection.attributedString
  1009. cell.resultLabel.textColor = KMAppearance.Layout.h0Color()
  1010. } else if let data = tableColumn?.identifier.rawValue, data == kPageColumnId.rawValue {
  1011. cell.resultLabel.stringValue = "\(Int(selection.selectionPageIndex) + 1)"
  1012. cell.resultLabel.textColor = KMAppearance.Layout.h2Color()
  1013. }
  1014. }
  1015. return cell
  1016. } else if tableView.isEqual(to: self.snapshotTableView) {
  1017. let cell = tableView.makeView(withIdentifier: KMSnapshotTableViewCell.km_identifier, owner: self) as! KMSnapshotTableViewCell
  1018. var snapshot: KMSnapshotModel?
  1019. if self.isSearchSnapshotMode {
  1020. if self.searchSnapshots.count > row {
  1021. snapshot = self.searchSnapshots[row]
  1022. }
  1023. } else {
  1024. if self.snapshots.count > row {
  1025. snapshot = self.snapshots[row]
  1026. }
  1027. }
  1028. cell.snapshotImage.image = snapshot?.windowC?.thumbnail
  1029. cell.snapshotLabel.stringValue = snapshot?.windowC?.pageLabel ?? ""
  1030. if let data = snapshot?.windowC?.hasWindow, data {
  1031. cell.snapshotImageView.isHidden = false
  1032. } else {
  1033. cell.snapshotImageView.isHidden = true
  1034. }
  1035. cell.isSelectCell = snapshot?.isSelected ?? false
  1036. if (row == tableView.selectedRow) {
  1037. cell.snapshotImageView.image = NSImage(named: KMImageNameUXIconSidebarSnapshotWindowSel)
  1038. } else {
  1039. cell.snapshotImageView.image = NSImage(named: KMImageNameUXIconSidebarSnapshotWindowNor)
  1040. }
  1041. return cell
  1042. }
  1043. return nil
  1044. }
  1045. func tableView(_ tableView: NSTableView, heightOfRow row: Int) -> CGFloat {
  1046. if tableView.isEqual(to: self.thumbnailTableView) {
  1047. let scaling = KMDataManager.ud_float(forKey: Self.Key.thumbSizeScaling)
  1048. let thumbnailSize = NSMakeSize(self.thumbnailCacheSize, self.thumbnailCacheSize)
  1049. let newScaling: CGFloat = scaling.cgFloat + 0.1
  1050. let newThumbnailHeight = thumbnailSize.width * newScaling
  1051. if (newThumbnailHeight > Self.kMinSidePaneWidth) {
  1052. self.thumbnailZoomOutButton.isEnabled = false
  1053. } else {
  1054. self.thumbnailZoomOutButton.isEnabled = true
  1055. }
  1056. if ((scaling - 0.1) < 0.4) {
  1057. self.thumbnailZoomInButton.isEnabled = false
  1058. } else {
  1059. self.thumbnailZoomInButton.isEnabled = true
  1060. }
  1061. var labelHeight = 0.0
  1062. if (self.isDisplayPageSize) {
  1063. labelHeight = 56.0
  1064. } else {
  1065. labelHeight = 41.5
  1066. }
  1067. let cellHeight = thumbnailSize.height + labelHeight
  1068. var thumbSize: NSSize = .zero
  1069. if (scaling > 0) {
  1070. thumbSize = NSMakeSize(thumbnailSize.width * scaling.cgFloat, cellHeight * scaling.cgFloat)
  1071. } else {
  1072. thumbSize = NSMakeSize(thumbnailSize.width, cellHeight)
  1073. }
  1074. return thumbSize.height
  1075. } else if tableView.isEqual(to: self.snapshotTableView) {
  1076. let scaling = KMDataManager.ud_float(forKey: Self.Key.snapshotSizeScaling)
  1077. let snapshotSize = (self.snapshots.safe_element(for: row) as? KMSnapshotModel)?.windowC?.thumbnail?.size ?? NSMakeSize(120, 63)
  1078. var newScaling = scaling + 0.1
  1079. let newSnapshotHeight = snapshotSize.width * newScaling.cgFloat
  1080. if (newSnapshotHeight > Self.kMinSidePaneWidth) {
  1081. self.snapshotNormalZoomInButton.isEnabled = false
  1082. } else {
  1083. self.snapshotNormalZoomInButton.isEnabled = true
  1084. }
  1085. if ((scaling - 0.1) < 0.3 || (newSnapshotHeight < 150.0)) {
  1086. self.snapshotNormalZoomOutButton.isEnabled = false
  1087. } else {
  1088. self.snapshotNormalZoomOutButton.isEnabled = true
  1089. }
  1090. let cellHeight = snapshotSize.height + 24.0
  1091. var thumbSize: NSSize = .zero
  1092. if (scaling > 0) {
  1093. thumbSize = NSMakeSize(snapshotSize.width * scaling.cgFloat, cellHeight * scaling.cgFloat)
  1094. } else {
  1095. thumbSize = NSMakeSize(snapshotSize.width, cellHeight)
  1096. }
  1097. return thumbSize.height
  1098. } else if (tableView.isEqual(to: self.findTableView)) {
  1099. return 40.0
  1100. } else if tableView.isEqual(to: self.groupedFindTableView) {
  1101. return 16
  1102. }
  1103. return tableView.rowHeight
  1104. }
  1105. func tableView(_ tableView: NSTableView, rowViewForRow row: Int) -> NSTableRowView? {
  1106. if (tableView.isEqual(to: self.findTableView)) {
  1107. let rowView = KMBotaTableRowView()
  1108. return rowView
  1109. } else if tableView.isEqual(to: self.groupedFindTableView) {
  1110. let rowView = KMGroupFindTableRowView()
  1111. rowView.totalNumber = self.groupSearchResults.first?.datas.count ?? 0
  1112. let model = self.groupSearchResults[row]
  1113. rowView.number = model.datas.count
  1114. rowView.pageIndex = Int(model.selectionPageIndex) + 1
  1115. return rowView
  1116. }
  1117. return nil
  1118. }
  1119. func tableViewSelectionIsChanging(_ notification: Notification) {
  1120. }
  1121. func tableViewSelectionDidChange(_ notification: Notification) {
  1122. if self.stopRepeatLoad == true {
  1123. } else {
  1124. self.delegate?.controller?(controller: self, listViewSelectionDidChange: notification.object, info: nil)
  1125. }
  1126. if self.findTableView.isEqual(to: notification.object) {
  1127. // [self updateFindResultHighlightsForDirection:NSDirectSelection];
  1128. let row = self.findTableView.selectedRow
  1129. if row >= 0 {
  1130. let model = self.searchResults[row]
  1131. let isEditing = self.listView?.isEditing() ?? false
  1132. if isEditing {
  1133. self.mainViewController?.srHanddler.showSelection(model.selection)
  1134. return
  1135. }
  1136. self.listView?.go(to: model.selection, animated: true)
  1137. // self.listView?.setHighlightedSelection(model.selection, animated: true)
  1138. DispatchQueue.main.asyncAfter(deadline: .now()+0.3) {
  1139. self.listView?.setHighlightedSelections([model.selection])
  1140. self.listView?.setNeedsDisplayAnnotationViewForVisiblePages()
  1141. }
  1142. }
  1143. } else if self.groupedFindTableView.isEqual(to: notification.object) {
  1144. // [self updateFindResultHighlightsForDirection:NSDirectSelection];
  1145. } else if self.thumbnailTableView.isEqual(to: notification.object) {
  1146. self.thumbnailTableView.ks_reloadData()
  1147. } else if self.snapshotTableView.isEqual(to: notification.object) {
  1148. let row = self.snapshotTableView.selectedRow
  1149. // 更新选中数据
  1150. for (i, model) in self.snapshots.enumerated() {
  1151. model.isSelected = i == row
  1152. }
  1153. if (row != -1) {
  1154. let controller = (self.snapshots.safe_element(for: row) as? KMSnapshotModel)?.windowC
  1155. if let data = controller?.window?.isVisible, data {
  1156. controller?.window?.orderFront(self)
  1157. }
  1158. }
  1159. self.snapshotTableView.ks_reloadData()
  1160. }
  1161. }
  1162. func tableView(_ tableView: NSTableView, validateDrop info: NSDraggingInfo, proposedRow row: Int, proposedDropOperation dropOperation: NSTableView.DropOperation) -> NSDragOperation {
  1163. if dropOperation == .on || tableView.isEqual(to: self.thumbnailTableView) == false {
  1164. return NSDragOperation(rawValue: 0)
  1165. }
  1166. let pboard = info.draggingPasteboard
  1167. if (pboard.availableType(from: [.localDraggedTypes]) != nil) {
  1168. return .move
  1169. } else if (pboard.availableType(from: [.fileURL]) != nil) && tableView.isEqual(to: self.thumbnailTableView) {
  1170. guard let pbItems = pboard.pasteboardItems else {
  1171. return NSDragOperation(rawValue: 0)
  1172. }
  1173. var hasValidFile = false
  1174. for item in pbItems {
  1175. guard let data = item.string(forType: .fileURL), let _url = URL(string: data) else {
  1176. continue
  1177. }
  1178. let type = _url.pathExtension.lowercased()
  1179. if type == "pdf" || KMImageToPDFMethod.supportedImageTypes().contains(type) {
  1180. hasValidFile = true
  1181. }
  1182. }
  1183. if (!hasValidFile) {
  1184. return NSDragOperation(rawValue: 0)
  1185. } else {
  1186. return .move
  1187. }
  1188. }
  1189. return NSDragOperation(rawValue: 0)
  1190. }
  1191. func tableView(_ tableView: NSTableView, acceptDrop info: NSDraggingInfo, row: Int, dropOperation: NSTableView.DropOperation) -> Bool {
  1192. var result = false
  1193. if tableView.isEqual(to: self.thumbnailTableView) == false {
  1194. return result
  1195. }
  1196. let pasteboard = info.draggingPasteboard
  1197. if (pasteboard.availableType(from: [.localDraggedTypes]) != nil) {
  1198. result = true
  1199. guard let rowData = pasteboard.data(forType: .localDraggedTypes) else {
  1200. return false
  1201. }
  1202. let rowIndexes = NSKeyedUnarchiver.unarchiveObject(with: rowData) as? IndexSet ?? IndexSet()
  1203. guard let urlData = pasteboard.data(forType: KPDFThumbnailDoucumentURLForDraggedTypes) else {
  1204. return false
  1205. }
  1206. let url = NSKeyedUnarchiver.unarchiveObject(with: urlData) as? String
  1207. if url != nil && url != self.pdfDocument()?.documentURL.absoluteString {
  1208. if (pasteboard.availableType(from: [.fileURL]) != nil) {
  1209. let fileNames = pasteboard.propertyList(forType: .fileURL) as? [String] ?? []
  1210. if (fileNames.count == 1) {
  1211. let path = fileNames.first ?? ""
  1212. let pathExtension = path.pathExtension.lowercased()
  1213. if pathExtension == "pdf" {
  1214. var index = row
  1215. var insertIndexSet = IndexSet()
  1216. let pdf = CPDFDocument(url: URL(fileURLWithPath: path))
  1217. if let data = pdf?.isEncrypted, data {
  1218. KMBaseWindowController.checkPassword(url: URL(fileURLWithPath: path), type: .owner) { success, resultPassword in
  1219. if success && resultPassword.isEmpty == false {
  1220. self.model.insertedDocumentSet.insert(pdf!)
  1221. for i in 0 ..< (pdf?.pageCount ?? 0) {
  1222. if let page = pdf?.page(at: i).copy() as? CPDFPage {
  1223. self.pdfDocument()?.insertPageObject(page, at: UInt(index))
  1224. insertIndexSet.insert(index)
  1225. index += 1
  1226. }
  1227. }
  1228. self.insertPages(insertIndexSet, pageAt: index-1)
  1229. }
  1230. }
  1231. } else {
  1232. if let data = pdf {
  1233. self.model.insertedDocumentSet.insert(data)
  1234. }
  1235. for i in 0 ..< (pdf?.pageCount ?? 0) {
  1236. let page = pdf?.page(at: i)
  1237. self.pdfDocument()?.insertPageObject(page, at: UInt(index))
  1238. insertIndexSet.insert(index)
  1239. index += 1
  1240. }
  1241. self.insertPages(insertIndexSet, pageAt: index-1)
  1242. }
  1243. }
  1244. return true
  1245. }
  1246. }
  1247. }
  1248. var pageIndex = 0
  1249. var pages: [CPDFPage] = []
  1250. for idx in rowIndexes {
  1251. if let page = self.pdfDocument()?.page(at: UInt(idx))?.copy() as? CPDFPage {
  1252. pages.append(page)
  1253. }
  1254. }
  1255. var toPage: CPDFPage?
  1256. if row < self.pageCount() {
  1257. toPage = self.pdfDocument()?.page(at: UInt(row))
  1258. }
  1259. var toPageIndex = 0
  1260. if (toPage != nil) {
  1261. toPageIndex = Int(self.pdfDocument()?.index(for: toPage) ?? 0)
  1262. } else {
  1263. toPageIndex = self.pageCount()
  1264. }
  1265. for i in 0 ..< pages.count {
  1266. let page = pages[i]
  1267. self.pdfDocument()?.insertPageObject(page, at: UInt(toPageIndex+i))
  1268. pageIndex += 1
  1269. self.listView?.go(toPageIndex: toPageIndex+i-1, animated: false)
  1270. }
  1271. self.layoutDocumentView()
  1272. for idx in rowIndexes {
  1273. if idx < toPageIndex {
  1274. if((idx+pageIndex) < self.pageCount()) {
  1275. if let page = self.pdfDocument()?.page(at: UInt(idx)) {
  1276. self.deletePages_.insert(page)
  1277. }
  1278. self.pdfDocument()?.removePage(at: IndexSet(integer: idx))
  1279. }
  1280. } else {
  1281. if((idx+pageIndex) < self.pageCount()) {
  1282. if let page = self.pdfDocument()?.page(at: UInt(idx+pageIndex)) {
  1283. self.deletePages_.insert(page)
  1284. }
  1285. self.pdfDocument()?.removePage(at: IndexSet(integer: idx+pageIndex))
  1286. }
  1287. }
  1288. }
  1289. if rowIndexes.isEmpty == false {
  1290. self.listView?.updateActiveAnnotations([])
  1291. if let win = self.view.window {
  1292. KMTools.setDocumentEditedState(window: win)
  1293. }
  1294. }
  1295. self.layoutDocumentView()
  1296. self.resetThumbnails()
  1297. } else if pasteboard.availableType(from: [.fileURL]) != nil && tableView.isEqual(to: self.thumbnailTableView) {
  1298. if IAPProductsManager.default().isAvailableAllFunction() == false {
  1299. KMPurchaseCompareWindowController.sharedInstance().showWindow(nil)
  1300. return false
  1301. }
  1302. let fileNames = pasteboard.propertyList(forType: .fileURL)
  1303. var insertIndexSet = IndexSet()
  1304. var index = row
  1305. if fileNames != nil {
  1306. let path = fileNames as? String ?? ""
  1307. let url = URL(string: path)
  1308. let pathExtension = url?.pathExtension.lowercased() ?? ""
  1309. if pathExtension == "pdf" {
  1310. let pdf = CPDFDocument(url: url!)
  1311. if let data = pdf?.isEncrypted, data {
  1312. self.view.window?.windowController?.kCheckPassword(url: url!, type: .owner, completion: { [weak self] success, resultPassword in
  1313. if success && resultPassword.isEmpty == false {
  1314. pdf?.unlock(withPassword: resultPassword)
  1315. self?.model.insertedDocumentSet.insert(pdf!)
  1316. for i in 0 ..< (pdf?.pageCount ?? 0) {
  1317. if let page = pdf?.page(at: i) {
  1318. self?.pdfDocument()?.insertPageObject(page, at: UInt(index))
  1319. insertIndexSet.insert(index)
  1320. index += 1
  1321. }
  1322. }
  1323. self?.insertPages(insertIndexSet, pageAt: index-1)
  1324. }
  1325. })
  1326. } else {
  1327. if let data = pdf {
  1328. self.model.insertedDocumentSet.insert(data)
  1329. }
  1330. for i in 0 ..< (pdf?.pageCount ?? 0) {
  1331. if let page = pdf?.page(at: i) {
  1332. self.pdfDocument()?.insertPageObject(page, at: UInt(index))
  1333. insertIndexSet.insert(index)
  1334. index += 1
  1335. }
  1336. }
  1337. self.insertPages(insertIndexSet, pageAt: index-1)
  1338. }
  1339. } else if KMImageToPDFMethod.supportedImageTypes().contains(pathExtension) {
  1340. if let image = NSImage(contentsOfFile: url?.path ?? "") {
  1341. // PDFPage * page = [[[PDFPage alloc] initWithImage:image] autorelease];
  1342. // [pdfView.document insertPage:page atIndex:index];
  1343. _ = self.pdfDocument()?.km_insert(image: image, at: UInt(index))
  1344. insertIndexSet.insert(index)
  1345. self.insertPages(insertIndexSet, pageAt: index-1)
  1346. }
  1347. }
  1348. result = true
  1349. } else {
  1350. result = false
  1351. }
  1352. }
  1353. return result == false
  1354. }
  1355. func tableView(_ tableView: NSTableView, writeRowsWith rowIndexes: IndexSet, to pboard: NSPasteboard) -> Bool {
  1356. if tableView.isEqual(to: self.thumbnailTableView) {
  1357. let idx = rowIndexes.first ?? NSNotFound
  1358. if (idx != NSNotFound && self.isLocked() == false) {
  1359. let page = self.pdfDocument()?.page(at: UInt(idx))
  1360. var fileExt: String?
  1361. let tiffData = page?.PDFListViewTIFFData(for: page?.bounds(for: self.displayBox()) ?? .zero)
  1362. if self.allowsPrinting() {
  1363. // NSData *pdfData = [page dataRepresentation];
  1364. fileExt = "pdf"
  1365. // filenames
  1366. pboard.declareTypes([.pdf, .tiff, .fileURL, .filePromise, .localDraggedTypes, KPDFThumbnailDoucumentURLForDraggedTypes], owner: self)
  1367. pboard.setData(tiffData, forType: .pdf)
  1368. // NSData *zNSIndexSetData = [NSKeyedArchiver archivedDataWithRootObject:rowIndexes];
  1369. let zNSIndexSetData = NSKeyedArchiver.archivedData(withRootObject: rowIndexes)
  1370. pboard.setData(zNSIndexSetData, forType: .localDraggedTypes)
  1371. let documentURL = NSKeyedArchiver.archivedData(withRootObject: self.pdfDocument()?.documentURL?.absoluteString)
  1372. // NSString *docmentName = [[[pdfView.document.documentURL path] lastPathComponent] stringByDeletingPathExtension];
  1373. var docmentName = self.pdfDocument()?.documentURL.deletingPathExtension().lastPathComponent ?? ""
  1374. // __block NSMutableString *pagesName = nil;
  1375. var pagesName = ""
  1376. if (rowIndexes.count > 1) {
  1377. pagesName = " pages"
  1378. } else {
  1379. pagesName = " page"
  1380. }
  1381. let tFileName = String(format: "%@ %@", pagesName, self.fileNameWithSelectedPages(rowIndexes))
  1382. let pdf = CPDFDocument()
  1383. for idx in rowIndexes {
  1384. // var copyPage = self.listView.document.page(at: UInt(idx)).copy() as? CPDFPage
  1385. let copyPage = self.pdfDocument()?.page(at: UInt(idx))
  1386. pdf?.insertPageObject(copyPage, at: pdf?.pageCount ?? 0)
  1387. }
  1388. let paths = NSSearchPathForDirectoriesInDomains(.cachesDirectory, .userDomainMask, true)
  1389. var cachesDir = paths.first ?? ""
  1390. docmentName = String(format: "%@%@", docmentName, tFileName)
  1391. if (docmentName.count > 50) {
  1392. docmentName = docmentName.substring(to: 50)
  1393. }
  1394. // cachesDir = [[cachesDir stringByAppendingPathComponent:docmentName] stringByAppendingPathExtension:@"pdf"];
  1395. cachesDir = "\(cachesDir)/\(docmentName).pdf"
  1396. let success = pdf?.write(toFile: cachesDir) ?? false
  1397. if (success) {
  1398. // [pboard setPropertyList:@[cachesDir] forType:NSFilenamesPboardType];
  1399. pboard.setPropertyList([cachesDir], forType: .fileURL)
  1400. } else{
  1401. // [pboard setPropertyList:@[@""] forType:NSFilenamesPboardType];
  1402. pboard.setPropertyList([], forType: .fileURL)
  1403. }
  1404. pboard.setData(documentURL, forType: KPDFThumbnailDoucumentURLForDraggedTypes)
  1405. } else {
  1406. fileExt = "tiff"
  1407. pboard.declareTypes([.tiff, .filePromise], owner: self)
  1408. }
  1409. pboard.setData(tiffData, forType: .tiff)
  1410. // kPasteboardTypeFileURLPromise
  1411. pboard.setPropertyList([fileExt], forType: .filePromise)
  1412. return true
  1413. }
  1414. } else if self.snapshotTableView.isEqual(to: tableView) {
  1415. let idx = rowIndexes.first ?? NSNotFound
  1416. if (idx != NSNotFound) {
  1417. let snapshot = (self.snapshots.safe_element(for: idx) as? KMSnapshotModel)?.windowC
  1418. if let data = snapshot?.thumbnailWithSize(0)?.tiffRepresentation {
  1419. pboard.declareTypes([.tiff, .filePromise], owner: self)
  1420. pboard.setData(data, forType: .tiff)
  1421. pboard.setPropertyList(["tiff"], forType: .filePromise)
  1422. return true
  1423. }
  1424. }
  1425. }
  1426. return false
  1427. }
  1428. func tableView(_ tableView: NSTableView, draggingSession session: NSDraggingSession, willBeginAt screenPoint: NSPoint, forRowIndexes rowIndexes: IndexSet) {
  1429. self.dragIn = true
  1430. }
  1431. func tableView(_ tableView: NSTableView, draggingSession session: NSDraggingSession, endedAt screenPoint: NSPoint, operation: NSDragOperation) {
  1432. self.dragIn = false
  1433. }
  1434. @objc dynamic func tableView(_ aTableView: NSTableView, deleteRowsWithIndexes rowIndexes: IndexSet) {
  1435. if IAPProductsManager.default().isAvailableAllFunction() == false {
  1436. KMPurchaseCompareWindowController.sharedInstance().showWindow(nil)
  1437. return
  1438. }
  1439. if aTableView.isEqual(to: self.snapshotTableView) {
  1440. var controllers: [KMSnapshotWindowController] = []
  1441. for i in rowIndexes {
  1442. let model: KMSnapshotModel? = self.snapshots.safe_element(for: i) as? KMSnapshotModel
  1443. if let winC = model?.windowC {
  1444. controllers.append(winC)
  1445. }
  1446. }
  1447. for c in controllers {
  1448. c.close()
  1449. }
  1450. } else if aTableView.isEqual(to: self.thumbnailTableView) {
  1451. guard let last = rowIndexes.last, last < self.pageCount() else {
  1452. return
  1453. }
  1454. //删除page时移除选中状态的annotation
  1455. self.listView?.activeAnnotations = []
  1456. for idx in rowIndexes {
  1457. if let page = self.pdfDocument()?.page(at: UInt(idx)) {
  1458. // for anno in page.annotations {
  1459. // page.removeAnnotation(anno)
  1460. // }
  1461. (self.listView?.undoManager?.prepare(withInvocationTarget: self) as AnyObject).insertPage(page, pageAt: idx)
  1462. // self.pdfDocument()?.removePage(at: IndexSet(integer: idx))
  1463. self.deletePages_.insert(page)
  1464. }
  1465. }
  1466. self.pdfDocument()?.removePage(at: rowIndexes)
  1467. self.layoutDocumentView()
  1468. self.resetThumbnails()
  1469. let idx = rowIndexes.first ?? -1
  1470. let index = min(idx, self.pageCount()-1)
  1471. self.listView?.go(toPageIndex: index, animated: false)
  1472. }
  1473. }
  1474. func tableView(_ aTableView: NSTableView, copyRowsWithIndexes rowIndexes: IndexSet) {
  1475. if IAPProductsManager.default().isAvailableAllFunction() == false {
  1476. KMPurchaseCompareWindowController.sharedInstance().showWindow(nil)
  1477. return
  1478. }
  1479. if aTableView.isEqual(to: self.thumbnailTableView) {
  1480. self._copysPages.removeAll()
  1481. if self.isLocked() == false {
  1482. for idx in rowIndexes {
  1483. if let page = self.pdfDocument()?.page(at: UInt(idx))?.copy() as? CPDFPage {
  1484. self._copysPages.append(page)
  1485. }
  1486. }
  1487. }
  1488. } else if aTableView.isEqual(to: self.findTableView) {
  1489. var string = ""
  1490. for idx in rowIndexes {
  1491. let match = self.searchResults[idx].selection
  1492. string.append("* ")
  1493. // [string appendFormat:NSLocalizedString(@"Page %@", @""), [match firstPageLabel]];
  1494. string = string.appendingFormat(KMLocalizedString("Page %@", ""), "\(match.safeFirstPage()?.pageIndex() ?? 0)")
  1495. // [string appendFormat:@"", [[match contextString] string]];
  1496. string = string.appendingFormat(": %@\n", match.string() ?? "")
  1497. }
  1498. let pboard = NSPasteboard.general
  1499. pboard.clearContents()
  1500. pboard.writeObjects([string as NSPasteboardWriting])
  1501. } else if aTableView.isEqual(to: self.groupedFindTableView) {
  1502. var string = ""
  1503. for idx in rowIndexes {
  1504. let result = self.groupSearchResults[idx]
  1505. let matches = result.datas
  1506. string.append("* ")
  1507. string = string.appendingFormat(KMLocalizedString("Page %@", ""), "\(result.selectionPageIndex)")
  1508. string.append(": ")
  1509. string = string.appendingFormat(KMLocalizedString("%ld Results", ""), matches.count)
  1510. // [string appendFormat:@":\n\t%@\n", [[matches valueForKeyPath:@"contextString.string"] componentsJoinedByString:@"\n\t"]];
  1511. var tmpString = ""
  1512. for model in matches {
  1513. tmpString.append("\(model.selection.string() ?? "")")
  1514. }
  1515. string = string.appendingFormat(":\n\t%@\n", tmpString)
  1516. }
  1517. let pboard = NSPasteboard.general
  1518. pboard.clearContents()
  1519. pboard.writeObjects([string as NSPasteboardWriting])
  1520. }
  1521. }
  1522. func tableView(_ tableView: NSTableView, namesOfPromisedFilesDroppedAtDestination dropDestination: URL, forDraggedRowsWith indexSet: IndexSet) -> [String] {
  1523. if self.thumbnailTableView.isEqual(to: tableView) {
  1524. var fileURLArray: [String] = []
  1525. if indexSet.count > 1 {
  1526. var docmentName = ""
  1527. let tFileName = String(format: "%@", self.fileNameWithSelectedPages(indexSet))
  1528. let pdf = CPDFDocument()
  1529. for idx in indexSet {
  1530. if idx != NSNotFound && self.isLocked() == false {
  1531. let copyPage = self.pdfDocument()?.page(at: UInt(idx))
  1532. pdf?.insertPageObject(copyPage, at: pdf?.pageCount ?? 0)
  1533. }
  1534. }
  1535. let fileURL = dropDestination.appendingPathComponent(tFileName).appendingPathExtension("pdf").uniqueFileURL()
  1536. docmentName = fileURL.path
  1537. let success = pdf?.write(toFile: docmentName) ?? false
  1538. if(success) {
  1539. fileURLArray.append(fileURL.lastPathComponent)
  1540. }
  1541. } else {
  1542. if let page = self.pdfDocument()?.page(at: UInt(indexSet.first ?? 0)) {
  1543. var fileURL = dropDestination.appendingPathComponent(self.draggedFileName(for: page))
  1544. if self.allowsPrinting() {
  1545. fileURL = fileURL.appendingPathExtension("pdf").uniqueFileURL()
  1546. let pdf = CPDFDocument()
  1547. // let copyPage = page.copy() as? CPDFPage
  1548. let copyPage = page
  1549. pdf?.insertPageObject(copyPage, at: pdf?.pageCount ?? 0)
  1550. let success = pdf?.write(toFile: fileURL.path) ?? false
  1551. if success {
  1552. fileURLArray.append(fileURL.lastPathComponent)
  1553. }
  1554. } else {
  1555. fileURL = fileURL.appendingPathExtension("tiff").uniqueFileURL()
  1556. let fileData = page.PDFListViewTIFFData(for: page.bounds(for: self.displayBox()))
  1557. let success = (try?fileData?.write(to: fileURL)) != nil
  1558. if success {
  1559. fileURLArray.append(fileURL.lastPathComponent)
  1560. }
  1561. }
  1562. }
  1563. }
  1564. return fileURLArray
  1565. } else if self.snapshotTableView.isEqual(to: tableView) {
  1566. let idx = indexSet.first ?? NSNotFound
  1567. if (idx != NSNotFound) {
  1568. if let snapshot = (self.snapshots.safe_element(for: idx) as? KMSnapshotModel)?.windowC {
  1569. if let page = self.pdfDocument()?.page(at: snapshot.pageIndex()) {
  1570. var fileURL = dropDestination.appendingPathComponent(self.draggedFileName(for: page)).appendingPathExtension("tiff")
  1571. fileURL = fileURL.uniqueFileURL()
  1572. if ((try?snapshot.thumbnailWithSize(0)?.tiffRepresentation?.write(to: fileURL)) != nil) {
  1573. return [fileURL.lastPathComponent]
  1574. }
  1575. }
  1576. }
  1577. }
  1578. }
  1579. return []
  1580. }
  1581. func tableViewColumnDidResize(_ notification: Notification) {
  1582. guard let column = notification.userInfo?["NSTableColumn"] as? NSTableColumn else {
  1583. return
  1584. }
  1585. if column.identifier.rawValue == IMAGE_COLUMNID {
  1586. if self.thumbnailTableView.isEqual(to: notification.object) {
  1587. self.thumbnailTableView.noteHeightOfRows(withIndexesChanged: NSIndexSet(indexesIn: NSMakeRange(0, self.thumbnailTableView.numberOfRows)) as IndexSet)
  1588. } else if self.snapshotTableView.isEqual(to: notification.object) {
  1589. self.snapshotTableView.noteHeightOfRows(withIndexesChanged: NSIndexSet(indexesIn: NSMakeRange(0, self.snapshotTableView.numberOfRows)) as IndexSet)
  1590. }
  1591. }
  1592. }
  1593. func tableView(_ tableView: NSTableView, sortDescriptorsDidChange oldDescriptors: [NSSortDescriptor]) {
  1594. if tableView.isEqual(to: self.groupedFindTableView) {
  1595. // [leftSideController.groupedFindArrayController setSortDescriptors:[tv sortDescriptors]];
  1596. }
  1597. }
  1598. /*
  1599. #pragma mark NSTableView delegate protocol
  1600. - (void)tableView:(NSTableView *)tv shareRowsWithIndexes:(NSIndexSet *)rowIndexes {
  1601. if ([tv isEqual:leftSideController.thumbnailTableView]) {
  1602. NSUInteger idx = [rowIndexes firstIndex];
  1603. if (idx != NSNotFound) {
  1604. PDFPage *page = [[pdfView document] pageAtIndex:idx];
  1605. NSString *fileName = [[[pdfView document] documentURL] lastPathComponent];
  1606. NSString *folderPath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0];
  1607. NSString *filePath = [folderPath stringByAppendingPathComponent:[NSString stringWithFormat:@"Untitled"]];
  1608. filePath = [filePath stringByAppendingPathExtension:[fileName pathExtension]];
  1609. [self fileWithPage:page atPath:filePath];
  1610. if (rint(NSAppKitVersionNumber) < NSAppKitVersionNumber10_8) {
  1611. [KMMailHelper sendFileWithPaths:[NSArray arrayWithObject:filePath]];
  1612. } else {
  1613. NSSharingService *service = [NSSharingService sharingServiceNamed:NSSharingServiceNameComposeEmail];
  1614. [service performWithItems:[NSArray arrayWithObject:[NSURL fileURLWithPath:filePath]]];
  1615. }
  1616. }
  1617. }
  1618. }
  1619. - (void)fileWithPage:(PDFPage *)page atPath:(NSString *)filePath {
  1620. NSData *data = [page dataRepresentation];
  1621. PDFDocument *document = [[PDFDocument alloc] initWithData:data];
  1622. [[NSFileManager defaultManager] removeItemAtPath:filePath error:nil];
  1623. BOOL success = [document writeToURL:[NSURL fileURLWithPath:filePath]];
  1624. if (success) {
  1625. NSWorkspace *workspace = [NSWorkspace sharedWorkspace];
  1626. NSURL *url = [NSURL fileURLWithPath:filePath];
  1627. [workspace activateFileViewerSelectingURLs:[NSArray arrayWithObject:url]];
  1628. }
  1629. [document release];
  1630. */
  1631. }
  1632. // MARK: - NSOutlineViewDelegate, NSOutlineViewDataSource
  1633. extension KMLeftSideViewController: NSOutlineViewDelegate, NSOutlineViewDataSource {
  1634. func outlineView(_ outlineView: NSOutlineView, numberOfChildrenOfItem item: Any?) -> Int {
  1635. if outlineView.isEqual(to: self.tocOutlineView) {
  1636. if self.isLocked() {
  1637. return 0
  1638. }
  1639. if item == nil { // 第一层
  1640. // let bks = self.bookmarks() ?? []
  1641. // let bkNum = bks.isEmpty ? 0 : 1
  1642. let bks: [CPDFBookmark] = []
  1643. let bkNum = 0
  1644. guard let rootOL = self.outlineRoot() else {
  1645. if bkNum > 0 {
  1646. } else {
  1647. self.showOutlineEmptyView()
  1648. }
  1649. return 0 + bkNum
  1650. }
  1651. // 搜索按钮
  1652. self.outlineSearchButton.isEnabled = rootOL.numberOfChildren > 0
  1653. if self.isSearchOutlineMode { // 是否为搜索模块
  1654. if self.hasContainString(self.outlineSearchField.stringValue, rootOutline: rootOL) == false {
  1655. self.showSearchOutlineBlankState(true)
  1656. return 0
  1657. }
  1658. self.showSearchOutlineBlankState(false)
  1659. let ols = self.fetchOutlines(for: rootOL, searchString: self.outlineSearchField.stringValue)
  1660. return ols.count
  1661. }
  1662. if rootOL.numberOfChildren == 0 { // 没有数据
  1663. if bkNum > 0 {
  1664. } else {
  1665. self.showOutlineEmptyView()
  1666. }
  1667. return 0 + bkNum
  1668. }
  1669. // 有数据
  1670. self.hideOutlineEmptyView()
  1671. return Int(rootOL.numberOfChildren) + bkNum
  1672. } else { // 第二层 +
  1673. if item is CPDFBookmark { // 书签
  1674. return 0
  1675. }
  1676. if let data = item as? String, data == Self.kOutlineRootBookmarkItem { // 书签group
  1677. if self.isSearchOutlineMode {
  1678. return 0
  1679. }
  1680. return (self.bookmarks()?.count) ?? 0
  1681. }
  1682. guard let ol = item as? CPDFOutline, ol.numberOfChildren > 0 else {
  1683. return 0
  1684. }
  1685. if self.isSearchOutlineMode == false {
  1686. return Int(ol.numberOfChildren)
  1687. }
  1688. // 搜索模式
  1689. let ols = self.fetchOutlines(for: ol, searchString: self.outlineSearchField.stringValue)
  1690. return ols.count
  1691. }
  1692. } else if outlineView.isEqual(to: self.noteOutlineView) {
  1693. if self.noteSearchMode {
  1694. // return self.noteSearchArray.count
  1695. var cnt = 0
  1696. for model in self.noteSearchArray {
  1697. cnt += 1
  1698. guard let data = model as? KMBotaAnnotationModel else {
  1699. continue
  1700. }
  1701. for replyI in data.replyAnnos {
  1702. cnt += 1
  1703. }
  1704. }
  1705. return cnt
  1706. }
  1707. // let count = self.annoListModel?.datas.count ?? 0
  1708. var cnt = 0
  1709. for sectionM in self.annoListModel?.datas ?? [] {
  1710. if sectionM.items.count > 0 {
  1711. cnt += 1
  1712. if sectionM.isExpand == false {
  1713. continue
  1714. }
  1715. for item in sectionM.items {
  1716. cnt += 1
  1717. if let annoItem = item as? KMBotaAnnotationModel {
  1718. // if annoItem.isExpand == false {
  1719. // continue
  1720. // }
  1721. for replyItem in annoItem.replyAnnos {
  1722. cnt += 1
  1723. }
  1724. }
  1725. }
  1726. }
  1727. }
  1728. let hasAnno = self.allAnnotations.count >= 1
  1729. self.noteSearchButton.isEnabled = hasAnno
  1730. self.noteFilterButton.isEnabled = hasAnno
  1731. self.noteOutlineView.usesAlternatingRowBackgroundColors = false
  1732. if cnt < 1 {
  1733. self.showNoteEmptyView()
  1734. } else {
  1735. self.hideNoteEmptyView()
  1736. }
  1737. return cnt
  1738. }
  1739. return 0
  1740. }
  1741. func outlineView(_ outlineView: NSOutlineView, child index: Int, ofItem item: Any?) -> Any {
  1742. if outlineView.isEqual(to: self.tocOutlineView) {
  1743. if self.isLocked() {
  1744. return ""
  1745. }
  1746. // let bks = self.bookmarks() ?? []
  1747. // let bkNum = bks.isEmpty ? 0 : 1
  1748. let bks: [CPDFBookmark] = []
  1749. let bkNum = 0
  1750. if item == nil {
  1751. if self.isSearchOutlineMode {
  1752. guard let rootOL = self.outlineRoot(), rootOL.numberOfChildren > 0 else {
  1753. return ""
  1754. }
  1755. let ols = self.fetchOutlines(for: rootOL, searchString: self.outlineSearchField.stringValue)
  1756. return ols.safe_element(for: index) as Any
  1757. }
  1758. if index == 0 && bkNum == 1 {
  1759. return Self.kOutlineRootBookmarkItem
  1760. }
  1761. let idx = bkNum == 1 ? index-1 : index
  1762. let rootOL = self.outlineRoot()
  1763. return rootOL?.child(at: UInt(idx)) as Any
  1764. } else {
  1765. if let data = item as? String, data == Self.kOutlineRootBookmarkItem {
  1766. return bks[index]
  1767. }
  1768. if item is CPDFBookmark {
  1769. return ""
  1770. }
  1771. if let ol = item as? CPDFOutline {
  1772. if self.isSearchOutlineMode == false {
  1773. return ol.child(at: UInt(index)) as Any
  1774. }
  1775. let ols = self.fetchOutlines(for: ol, searchString: self.outlineSearchField.stringValue)
  1776. return ols.safe_element(for: index) as Any
  1777. }
  1778. }
  1779. } else if outlineView.isEqual(to: self.noteOutlineView) {
  1780. if self.noteSearchMode {
  1781. // return self.noteSearchArray[index]
  1782. var idx = 0
  1783. for model in self.noteSearchArray {
  1784. idx += 1
  1785. if index + 1 == idx {
  1786. return model
  1787. }
  1788. guard let data = model as? KMBotaAnnotationModel else {
  1789. continue
  1790. }
  1791. for replyI in data.replyAnnos {
  1792. idx += 1
  1793. if index + 1 == idx {
  1794. return replyI
  1795. }
  1796. }
  1797. }
  1798. return 0
  1799. }
  1800. var idx = 0
  1801. var flagSectionM: KMBotaAnnotationSectionModel?
  1802. var flagItem: KMBotaAnnotationBaseModel?
  1803. var replyIten: KMBotaAnnotationReplyModel?
  1804. var isSection = false
  1805. var isReply = false
  1806. for sectionM in self.annoListModel?.datas ?? [] {
  1807. idx += 1
  1808. if index + 1 == idx {
  1809. flagSectionM = sectionM
  1810. isSection = true
  1811. break
  1812. }
  1813. if sectionM.isExpand == false {
  1814. continue
  1815. }
  1816. for item in sectionM.items {
  1817. idx += 1
  1818. if index + 1 == idx {
  1819. flagSectionM = sectionM
  1820. flagItem = item
  1821. break
  1822. }
  1823. guard let annoItem = item as? KMBotaAnnotationModel else {
  1824. continue
  1825. }
  1826. // if annoItem.isExpand == false {
  1827. // flagItem = annoItem
  1828. // continue
  1829. // }
  1830. for replyI in annoItem.replyAnnos {
  1831. idx += 1
  1832. if index + 1 == idx {
  1833. flagSectionM = sectionM
  1834. flagItem = item
  1835. replyIten = replyI
  1836. isReply = true
  1837. break
  1838. }
  1839. }
  1840. }
  1841. }
  1842. if isSection {
  1843. return flagSectionM
  1844. }
  1845. if isReply {
  1846. return replyIten
  1847. }
  1848. return flagItem
  1849. }
  1850. return item as Any
  1851. }
  1852. func outlineView(_ outlineView: NSOutlineView, viewFor tableColumn: NSTableColumn?, item: Any) -> NSView? {
  1853. if outlineView.isEqual(to: self.tocOutlineView) {
  1854. let cell = outlineView.makeView(withIdentifier: KMTocTableCellView.km_identifier, owner: self) as! KMTocTableCellView
  1855. let columnId = tableColumn?.identifier.rawValue
  1856. var title = ""
  1857. var pageLabel = ""
  1858. if let data = item as? String, data == Self.kOutlineRootBookmarkItem {
  1859. title = NSLocalizedString("Bookmarks", comment: "")
  1860. } else if let ol = item as? CPDFOutline {
  1861. title = ol.label ?? ""
  1862. if ol.actionType == .page {
  1863. pageLabel = "\((ol.destination?.pageIndex ?? 0) + 1)"
  1864. }
  1865. } else if let bk = item as? CPDFBookmark {
  1866. title = bk.label ?? ""
  1867. pageLabel = "\(bk.pageIndex + 1)"
  1868. }
  1869. cell.pageLabel.stringValue = pageLabel
  1870. if columnId == kLabelColumnId.rawValue {
  1871. if self.isSearchOutlineMode {
  1872. let attriString = NSMutableAttributedString(string: title)
  1873. var searchString = self.outlineSearchField.stringValue
  1874. var roughString = title
  1875. if self.outlineIgnoreCaseFlag {
  1876. roughString = roughString.lowercased()
  1877. searchString = searchString.lowercased()
  1878. }
  1879. let ranges = roughString.ranges(of: searchString)
  1880. for range in ranges.nsRnage {
  1881. attriString.addAttribute(.font, value: NSFont.boldSystemFont(ofSize: 13), range: range)
  1882. }
  1883. cell.tocLabel.attributedStringValue = attriString
  1884. } else {
  1885. cell.tocLabel.stringValue = title
  1886. }
  1887. }
  1888. cell.tocLabel.textColor = KMAppearance.titleColor()
  1889. return cell
  1890. } else if outlineView.isEqual(to: self.noteOutlineView) {
  1891. if let data = item as? KMBotaAnnotationSectionModel {
  1892. var cell = outlineView.makeView(withIdentifier: KMSectionCellView.km_identifier, owner: nil) as? KMSectionCellView
  1893. if cell == nil {
  1894. cell = KMSectionCellView.createFromNib()
  1895. }
  1896. cell?.titleLabel.textColor = KMAppearance.titleColor()
  1897. cell?.countLabel.textColor = KMAppearance.titleColor()
  1898. cell?.isExpand = data.isExpand
  1899. cell?.contentInset = NSEdgeInsets(top: 0, left: 8, bottom: 0, right: 0)
  1900. if self.noteSortType == .page {
  1901. let pageIndex = data.items.first?.anno?.page.pageIndex() ?? 0
  1902. cell?.titleLabel.stringValue = NSLocalizedString("Page", comment: "") + " \(pageIndex + 1)"
  1903. } else {
  1904. if let date = data.items.first?.anno?.modificationDate() {
  1905. let string = KMTools.timeString(timeDate: date, formatString: "yyyy-MM-dd")
  1906. cell?.titleLabel.stringValue = string
  1907. } else {
  1908. cell?.titleLabel.stringValue = ""
  1909. }
  1910. }
  1911. cell?.countLabel.stringValue = "\(data.itemCount / 2)"
  1912. cell?.itemClick = { idx, _ in
  1913. if idx == 1 { // 收取 & 展开
  1914. data.isExpand = !data.isExpand
  1915. self.noteOutlineView.reloadData()
  1916. }
  1917. }
  1918. return cell
  1919. }
  1920. if let data = item as? KMBotaAnnotationFooterModel {
  1921. var cell = outlineView.makeView(withIdentifier: KMNoteFooterCellView.km_identifier, owner: self) as? KMNoteFooterCellView
  1922. if cell == nil {
  1923. cell = KMNoteFooterCellView.createFromNib()
  1924. }
  1925. cell?.model = data
  1926. let cnt = self.noteReplyHanddler.fetchReplyAnnotations(data.anno)?.count ?? 0
  1927. if cnt == 0 {
  1928. cell?.commentNumberLabel.stringValue = ""
  1929. } else {
  1930. cell?.commentNumberLabel.stringValue = "\(cnt)"
  1931. }
  1932. let state = self.noteReplyHanddler.fetchReviewState(data.anno) ?? .none
  1933. if state == .none {
  1934. cell?.operationIv.image = NSImage(named: "KMImageNameBotaNoteStateNone")
  1935. } else if state == .completed {
  1936. cell?.operationIv.image = NSImage(named: "KMImageNameBotaNoteStateCompleted")
  1937. } else if state == .canceled {
  1938. cell?.operationIv.image = NSImage(named: "KMImageNameBotaNoteStateCancelled")
  1939. } else if state == .accepted {
  1940. cell?.operationIv.image = NSImage(named: "KMImageNameBotaNoteStateAccepted")
  1941. } else if state == .rejected {
  1942. cell?.operationIv.image = NSImage(named: "KMImageNameBotaNoteStateRejected")
  1943. }
  1944. cell?.operationBox.toolTip = KMPDFAnnotationStateGetString(state: state)
  1945. if let con = data.replyModel?.replyAnno?.contents, con.isEmpty == false {
  1946. cell?.inputTextF.stringValue = con
  1947. DispatchQueue.main.async {
  1948. self.view.window?.makeFirstResponder(cell?.inputTextF)
  1949. }
  1950. } else {
  1951. cell?.inputTextF.stringValue = ""
  1952. }
  1953. cell?.updateUI(expand: data.isExpand, animated: data.animated)
  1954. data.animated = false
  1955. if data.isFirstResp {
  1956. DispatchQueue.main.async {
  1957. self.view.window?.makeFirstResponder(cell?.inputTextF)
  1958. }
  1959. data.isFirstResp = false
  1960. }
  1961. cell?.itemClick = { [weak self] idx, param in
  1962. if idx == 1 { // comment
  1963. data.isExpand = !data.isExpand
  1964. data.animated = true
  1965. data.isFirstResp = true
  1966. data.annoModel?.isExpand = data.isExpand
  1967. // self?.noteOutlineView.reloadItem(data)
  1968. self?.noteOutlineView.reloadData()
  1969. } else if idx == 2 { //
  1970. self?.noteReplyHanddler.showStatePopView(sender: param.first as! NSView, anno: data.anno)
  1971. } else if idx == 3 { // reply
  1972. let content = param.first as? String ?? ""
  1973. if content.isEmpty {
  1974. // Task {
  1975. // _ = await KMAlertTool.runModel(message: NSLocalizedString("Error", comment: ""))
  1976. // }
  1977. return
  1978. }
  1979. if let con = data.replyModel?.replyAnno?.contents, con.isEmpty == false { // 编辑
  1980. let model = data.replyModel
  1981. model?.replyAnno?.contents = content
  1982. model?.replyAnno?.setUserName(KMPreference.shared.author)
  1983. model?.replyAnno?.setModificationDate(Date())
  1984. self?.noteOutlineView.reloadData()
  1985. // 置空编辑状态
  1986. data.replyModel = nil
  1987. return
  1988. }
  1989. if let replyAnno = self?.noteReplyHanddler.createReplyAnnotation(data.anno, content: content, userName: KMPreference.shared.author) {
  1990. // self?.reloadAnnotation()
  1991. let model = KMBotaAnnotationReplyModel()
  1992. model.anno = data.anno
  1993. model.replyAnno = replyAnno
  1994. model.annoModel = data.annoModel
  1995. data.annoModel?.replyAnnos.append(model)
  1996. self?.noteOutlineView.reloadData()
  1997. // if let row = self?.noteOutlineView.row(forItem: data) {
  1998. // self?.noteOutlineView.scrollRowToVisible(row)
  1999. // }
  2000. // self?.noteOutlineView.ks_reloadData()
  2001. // DispatchQueue.main.async {
  2002. // }
  2003. // self?.noteOutlineView.reloadItem(data)
  2004. // }
  2005. }
  2006. } else if idx == 4 { // cancel
  2007. data.isExpand = false
  2008. data.replyModel = nil
  2009. self?.noteOutlineView.reloadItem(data)
  2010. }
  2011. }
  2012. return cell
  2013. }
  2014. if let data = item as? KMBotaAnnotationReplyModel {
  2015. var cell = outlineView.makeView(withIdentifier: KMNoteReplyCellView.km_identifier, owner: self) as? KMNoteReplyCellView
  2016. if cell == nil {
  2017. cell = KMNoteReplyCellView.createFromNib()
  2018. }
  2019. cell?.model = data
  2020. cell?.titleLabel.stringValue = data.replyAnno?.userName() ?? ""
  2021. if let data = data.replyAnno?.modificationDate() {
  2022. cell?.timeLabel.stringValue = KMTools.timeString(timeDate: data)
  2023. } else {
  2024. cell?.timeLabel.stringValue = ""
  2025. }
  2026. cell?.contentLabel.stringValue = data.replyAnno?.contents ?? ""
  2027. cell?.itemClick = { [weak self] idx, params in
  2028. if idx == 1 { // 更多
  2029. self?.noteReplyHanddler.showReplyMorePopView(sender: params.first as! NSView, replyModel: data)
  2030. }
  2031. }
  2032. return cell
  2033. }
  2034. let model = item as? KMBotaAnnotationModel
  2035. let note = (item as? KMBotaAnnotationModel)?.anno
  2036. let cell = outlineView.makeView(withIdentifier: KMNoteTableViewCell.km_identifier, owner: self) as! KMNoteTableViewCell
  2037. cell.cellNote = note
  2038. cell.model = model
  2039. let state = self.noteReplyHanddler.fetchAnnoState(note) ?? .unMarked
  2040. cell.state = state
  2041. if let data = note {
  2042. if data.isKind(of: CPDFStampAnnotation.self) && data.isKind(of: KMSelfSignAnnotation.self) == false {
  2043. } else {
  2044. if data.isKind(of: CPDFMarkupAnnotation.self) {
  2045. if (!cell.isFold) {
  2046. // self.allFoldNotes.append(data)
  2047. // model?.isExpand = model?.foldType == .unfold
  2048. } else {
  2049. cell.imageViewHeightConstraint.constant = 18.0 + 8
  2050. // model?.isExpand = true
  2051. }
  2052. } else {
  2053. cell.imageViewHeightConstraint.constant = 18.0 + 8
  2054. // model?.isExpand = true
  2055. }
  2056. }
  2057. }
  2058. cell.itemClick = { [weak self] idx , _ in
  2059. if idx == 1 { // Markup
  2060. let anno = model?.anno
  2061. guard let state = self?.noteReplyHanddler.fetchAnnoState(anno), state == .marked else {
  2062. self?.noteReplyHanddler.markAnnotation(anno)
  2063. // if let theA = anno {
  2064. // self?.note_reloadDataForAnnoIfNeed(anno: theA)
  2065. // }
  2066. self?.noteOutlineView.reloadItem(model)
  2067. return
  2068. }
  2069. self?.noteReplyHanddler.unMarkAnnotation(anno)
  2070. // if let theA = anno {
  2071. // self?.note_reloadDataForAnnoIfNeed(anno: theA)
  2072. // }
  2073. self?.noteOutlineView.reloadItem(model)
  2074. }
  2075. }
  2076. cell.isUnFoldNote = { [weak self] cellNote, isUnfold in
  2077. // 将折叠状态记录到模型
  2078. model?.foldType = isUnfold ? .unfold : .fold
  2079. // self?.note_reloadDataForAnnoIfNeed(anno: <#T##CPDFAnnotation#>)
  2080. model?.footerModel?.isExpand = isUnfold
  2081. model?.isExpand = isUnfold
  2082. // self?.noteOutlineView.reloadItem(model?.footerModel)
  2083. self?.noteOutlineView.reloadData()
  2084. }
  2085. return cell
  2086. }
  2087. return nil
  2088. }
  2089. func outlineView(_ outlineView: NSOutlineView, heightOfRowByItem item: Any) -> CGFloat {
  2090. if outlineView.isEqual(self.noteOutlineView) {
  2091. if let model = item as? KMBotaAnnotationModel {
  2092. if model.foldType == .fold {
  2093. return model.foldH
  2094. }
  2095. if let anno = model.anno {
  2096. return KMBOTAAnnotationTool.fetchCellHeight(annotation: anno, maxSize: CGSize(width: 260+40 - 16, height: 1000))
  2097. }
  2098. }
  2099. return 30
  2100. } else if outlineView.isEqual(self.tocOutlineView) {
  2101. if let ol = item as? CPDFOutline {
  2102. let string: NSString = ol.label as NSString
  2103. let ps = NSMutableParagraphStyle()
  2104. ps.lineHeightMultiple = 1.32
  2105. ps.alignment = .left
  2106. let attris = [NSAttributedString.Key.paragraphStyle : ps,
  2107. NSAttributedString.Key.font : NSFont.SFProTextRegularFont(14.0)]
  2108. let size = string.boundingRect(with: NSMakeSize(outlineView.frame.size.width - 30, 200), options: NSString.DrawingOptions(rawValue: 3), attributes: attris)
  2109. return max(40, size.height + 16)
  2110. }
  2111. return 40
  2112. }
  2113. return outlineView.rowHeight
  2114. }
  2115. func outlineView(_ outlineView: NSOutlineView, isItemExpandable item: Any) -> Bool {
  2116. if outlineView.isEqual(self.tocOutlineView) {
  2117. if let data = item as? String, data == Self.kOutlineRootBookmarkItem {
  2118. return true
  2119. }
  2120. if item is CPDFBookmark {
  2121. return false
  2122. }
  2123. guard let ol = item as? CPDFOutline else {
  2124. return false
  2125. }
  2126. if self.isSearchOutlineMode == false {
  2127. return ol.numberOfChildren > 0
  2128. }
  2129. let ols = self.fetchOutlines(for: ol, searchString: self.outlineSearchField.stringValue)
  2130. return ols.count > 0
  2131. } else if outlineView.isEqual(to: self.noteOutlineView) {
  2132. return false
  2133. }
  2134. return false
  2135. }
  2136. func outlineView(_ outlineView: NSOutlineView, rowViewForItem item: Any) -> NSTableRowView? {
  2137. if outlineView.isEqual(self.tocOutlineView) {
  2138. let itemView = KMBotaTableRowView()
  2139. return itemView
  2140. } else if outlineView.isEqual(self.noteOutlineView) {
  2141. let itemView = KMBotaTableRowView()
  2142. if let data = item as? KMBotaAnnotationBaseModel {
  2143. itemView.isSelected = data.isSelected
  2144. }
  2145. itemView.selectCallback = { theView in
  2146. let isSelected = theView.isSelected
  2147. if theView.numberOfColumns > 0 {
  2148. let cellView = theView.view(atColumn: 0)
  2149. if let data = cellView as? KMSectionCellView {
  2150. return
  2151. }
  2152. var model: KMBotaAnnotationModel?
  2153. var indexs = IndexSet()
  2154. if let data = cellView as? KMNoteFooterCellView {
  2155. model = data.model?.annoModel
  2156. model?.isSelected = isSelected
  2157. self.view.window?.makeFirstResponder(data.inputTextF)
  2158. }
  2159. if let data = cellView as? KMNoteReplyCellView {
  2160. model = data.model?.annoModel
  2161. model?.isSelected = isSelected
  2162. }
  2163. if let data = cellView as? KMNoteTableViewCell {
  2164. model = data.model
  2165. model?.isSelected = isSelected
  2166. }
  2167. //
  2168. if let data = model {
  2169. // let row = self.noteOutlineView.row(for: cellView as! NSView)
  2170. let row = self.noteOutlineView.row(forItem: data)
  2171. indexs.insert(row)
  2172. var i = 1
  2173. for item in model?.replyAnnos ?? [] {
  2174. indexs.insert(row+i)
  2175. i += 1
  2176. }
  2177. indexs.insert(row+i)
  2178. // self.noteOutlineView.km_safe_selectRowIndexes(indexs, byExtendingSelection: false)
  2179. // self.noteOutlineView.selectRowIndexes(indexs, byExtendingSelection: false)
  2180. // self.noteOutlineView.km_selectRow(indexs.first!)
  2181. // self.noteOutlineView.selectedItem()
  2182. }
  2183. KMPrint("")
  2184. }
  2185. }
  2186. return itemView;
  2187. }
  2188. return nil
  2189. }
  2190. func outlineView(_ outlineView: NSOutlineView, selectionIndexesForProposedSelection proposedSelectionIndexes: IndexSet) -> IndexSet {
  2191. if outlineView.isEqual(to: self.noteOutlineView) {
  2192. var indexs = proposedSelectionIndexes
  2193. for i in proposedSelectionIndexes {
  2194. let item = self.noteOutlineView.item(atRow: i)
  2195. var model: KMBotaAnnotationModel?
  2196. if let data = item as? KMBotaAnnotationSectionModel {
  2197. }
  2198. if let data = item as? KMBotaAnnotationModel {
  2199. model = data
  2200. }
  2201. if let data = item as? KMBotaAnnotationReplyModel {
  2202. model = data.annoModel
  2203. }
  2204. if let data = item as? KMBotaAnnotationFooterModel {
  2205. model = data.annoModel
  2206. }
  2207. if let data = model {
  2208. // let row = self.noteOutlineView.row(for: cellView as! NSView)
  2209. let row = self.noteOutlineView.row(forItem: data)
  2210. indexs.insert(row)
  2211. var i = 1
  2212. for item in model?.replyAnnos ?? [] {
  2213. indexs.insert(row+i)
  2214. i += 1
  2215. }
  2216. indexs.insert(row+i)
  2217. }
  2218. }
  2219. return indexs
  2220. }
  2221. return proposedSelectionIndexes
  2222. }
  2223. func outlineViewSelectionDidChange(_ notification: Notification) {
  2224. if self.tocOutlineView.isEqual(to: notification.object) {
  2225. if self.dragIn {
  2226. return
  2227. }
  2228. if self.updatingOutlineSelection == false {
  2229. self.updatingOutlineSelection = true
  2230. self.goToSelectedOutlineItem(nil)
  2231. self.updatingOutlineSelection = false
  2232. }
  2233. }
  2234. }
  2235. func outlineViewItemDidExpand(_ notification: Notification) {
  2236. // if self.tocOutlineView.isEqual(to: notification.object) {
  2237. // self.updateOutlineSelection()
  2238. // }
  2239. }
  2240. func outlineViewItemDidCollapse(_ notification: Notification) {
  2241. // if self.tocOutlineView.isEqual(to: notification.object) {
  2242. // self.updateOutlineSelection()
  2243. // }
  2244. }
  2245. func outlineView(_ outlineView: NSOutlineView, validateDrop info: NSDraggingInfo, proposedItem item: Any?, proposedChildIndex index: Int) -> NSDragOperation {
  2246. var dragOp = NSDragOperation(rawValue: 0)
  2247. if outlineView.isEqual(to: self.noteOutlineView) {
  2248. let pboard = info.draggingPasteboard
  2249. if pboard.canReadObject(forClasses: [NSColor.self], options: [:]) && index == NSOutlineViewDropOnItemIndex {
  2250. if let note = item as? CPDFAnnotation, note.type != nil {
  2251. dragOp = .every
  2252. }
  2253. }
  2254. } else if outlineView.isEqual(to: self.tocOutlineView) {
  2255. if (index == -1) {
  2256. dragOp = NSDragOperation(rawValue: 0)
  2257. } else {
  2258. dragOp = .move
  2259. }
  2260. }
  2261. return dragOp
  2262. }
  2263. func outlineView(_ outlineView: NSOutlineView, writeItems items: [Any], to pasteboard: NSPasteboard) -> Bool {
  2264. if outlineView.isEqual(to: self.tocOutlineView) {
  2265. if (self.tocOutlineView.selectedRowIndexes.count > 1) {
  2266. return false
  2267. }
  2268. let tIndex = IndexSet(integer: self.tocOutlineView.clickedRow)
  2269. self.tocOutlineView.deselectRow(tIndex.first ?? 0)
  2270. self._dragPDFOutline = items.first as? CPDFOutline
  2271. let set = IndexSet(integer: 0)
  2272. let zNSIndexSetData = NSKeyedArchiver.archivedData(withRootObject: set)
  2273. // [pasteboard declareTypes:[NSArray arrayWithObject:kKMPDFViewOutlineDragDataType] owner:self];
  2274. pasteboard.declareTypes([.localDraggedTypes], owner: self)
  2275. pasteboard.setData(zNSIndexSetData, forType: .localDraggedTypes)
  2276. return true
  2277. }
  2278. return false
  2279. }
  2280. func outlineView(_ outlineView: NSOutlineView, acceptDrop info: NSDraggingInfo, item: Any?, childIndex index: Int) -> Bool {
  2281. if outlineView.isEqual(to: self.noteOutlineView) {
  2282. let pboard = info.draggingPasteboard
  2283. if pboard.canReadObject(forClasses: [NSColor.self], options: [:]) {
  2284. // [item setColor:[NSColor colorFromPasteboard:pboard] alternate:isAlt updateDefaults:isShift];
  2285. let isShift = NSEvent.modifierFlags.contains(.shift)
  2286. let isAlt = NSEvent.modifierFlags.contains(.option)
  2287. guard let note = item as? CPDFAnnotation else {
  2288. NSSound.beep()
  2289. return false
  2290. }
  2291. if let color = NSColor(from: pboard) {
  2292. note.setColor(color, alternate: isAlt, updateDefaults: isShift)
  2293. return true
  2294. }
  2295. return false
  2296. }
  2297. } else if outlineView.isEqual(to: self.tocOutlineView) {
  2298. if (index < 0) {
  2299. return false
  2300. }
  2301. let outline = item as? CPDFOutline
  2302. guard let dragPDFOL = self._dragPDFOutline else {
  2303. return false
  2304. }
  2305. //root,drag item to root
  2306. if ((outline?.parent == nil)) {
  2307. //fetch root
  2308. var root = dragPDFOL
  2309. while (root.parent != nil) {
  2310. root = root.parent
  2311. }
  2312. if dragPDFOL.parent.isEqual(to: root) {
  2313. if dragPDFOL.index > index {
  2314. self.dragPDFOutline(self._dragPDFOutline, toIndex: index, newParentOutline: root)
  2315. } else {
  2316. // if let bks = self.bookmarks(), bks.count > 0 {
  2317. // if let data = item as? String, data == Self.kOutlineRootBookmarkItem {
  2318. //
  2319. // } else {
  2320. // if index > 1 {
  2321. // self.dragPDFOutline(self._dragPDFOutline, toIndex: index-2, newParentOutline: root)
  2322. // }
  2323. // }
  2324. // } else {
  2325. self.dragPDFOutline(self._dragPDFOutline, toIndex: index-1, newParentOutline: root)
  2326. // }
  2327. }
  2328. } else {
  2329. self.dragPDFOutline(self._dragPDFOutline, toIndex: index, newParentOutline: root)
  2330. }
  2331. } else {
  2332. //在同一个层级内移动
  2333. if dragPDFOL.parent.isEqual(to: item) {
  2334. if (dragPDFOL.index > 0) {
  2335. if dragPDFOL.index > index {
  2336. self.dragPDFOutline(self._dragPDFOutline, toIndex: index, newParentOutline: outline)
  2337. }else{
  2338. self.dragPDFOutline(self._dragPDFOutline, toIndex: index-1, newParentOutline: outline)
  2339. }
  2340. } else {
  2341. return false
  2342. }
  2343. } else {
  2344. var tOutlline: CPDFOutline? = outline
  2345. var isContains = false
  2346. while (tOutlline != nil) {
  2347. if tOutlline!.isEqual(to: self._dragPDFOutline) {
  2348. isContains = true
  2349. break
  2350. }
  2351. tOutlline = tOutlline?.parent
  2352. }
  2353. if (!isContains) {
  2354. self.dragPDFOutline(self._dragPDFOutline, toIndex: index, newParentOutline: outline)
  2355. }
  2356. }
  2357. }
  2358. return true
  2359. }
  2360. return false
  2361. }
  2362. func outlineView(_ outlineView: NSOutlineView, willDisplayOutlineCell cell: Any, for tableColumn: NSTableColumn?, item: Any) {
  2363. if outlineView.isEqual(to: self.tocOutlineView) {
  2364. if outlineView.selectionHighlightStyle == .regular && outlineView.isRowSelected(outlineView.row(forItem: item)) {
  2365. (cell as? NSCell)?.backgroundStyle = .lowered
  2366. }
  2367. }
  2368. }
  2369. func outlineView(_ outlineView: NSOutlineView, setObjectValue object: Any?, for tableColumn: NSTableColumn?, byItem item: Any?) {
  2370. if outlineView.isEqual(to: self.noteOutlineView) {
  2371. var note: CPDFAnnotation?
  2372. if let data = item as? CPDFAnnotation {
  2373. note = data
  2374. } else if let data = item as? KMBotaAnnotationModel {
  2375. note = data.anno
  2376. }
  2377. if let data = note?.type, data.isEmpty == false {
  2378. if tableColumn?.identifier.rawValue == self.noteColumnId.rawValue {
  2379. let string1 = (object as? String) ?? ""
  2380. let string2 = note?.string() ?? ""
  2381. if string1 != string2 {
  2382. note?.setString(string1)
  2383. }
  2384. } else if tableColumn?.identifier.rawValue == self.authorColumnId.rawValue {
  2385. let string1 = (object as? String) ?? ""
  2386. let string2 = note?.userName() ?? ""
  2387. if string1 != string2 {
  2388. note?.setUserName(string1)
  2389. }
  2390. }
  2391. }
  2392. }
  2393. }
  2394. func outlineView(_ outlineView: NSOutlineView, dataCellFor tableColumn: NSTableColumn?, item: Any) -> NSCell? {
  2395. if self.noteOutlineView.isEqual(to: outlineView) {
  2396. if tableColumn == nil {
  2397. if let anno = item as? CPDFAnnotation, anno.type == nil {
  2398. return outlineView.tableColumn(withIdentifier: self.noteColumnId)?.dataCell(forRow: outlineView.row(forItem: item)) as? NSCell
  2399. }
  2400. }
  2401. }
  2402. return tableColumn?.dataCell(forRow: outlineView.row(forItem: item)) as? NSCell
  2403. }
  2404. func outlineView(_ outlineView: NSOutlineView, shouldEdit tableColumn: NSTableColumn?, item: Any) -> Bool {
  2405. if outlineView.isEqual(to: self.noteOutlineView) {
  2406. if (tableColumn == nil) {
  2407. if self.hideNotes() == false {
  2408. if let anno = item as? CPDFAnnotation, anno.isNote() {
  2409. // if ([pdfView hideNotes] == NO && [[(SKNoteText *)item note] isNote]) {
  2410. // PDFAnnotation *annotation = [(SKNoteText *)item note];
  2411. self.listView?.scrollAnnotationToVisible(anno)
  2412. self.listView?.updateActiveAnnotations([anno])
  2413. // [self showNote:annotation];
  2414. // SKNoteWindowController *noteController = (SKNoteWindowController *)[self windowControllerForNote:annotation];
  2415. // [[noteController window] makeFirstResponder:[noteController textView]];
  2416. // [[noteController textView] selectAll:nil];
  2417. }
  2418. }
  2419. return false
  2420. } else if tableColumn?.identifier.rawValue == self.noteColumnId.rawValue || tableColumn?.identifier.rawValue == self.authorColumnId.rawValue {
  2421. return true
  2422. }
  2423. }
  2424. return false
  2425. }
  2426. func outlineView(_ outlineView: NSOutlineView, toolTipFor cell: NSCell, rect: NSRectPointer, tableColumn: NSTableColumn?, item: Any, mouseLocation: NSPoint) -> String {
  2427. if outlineView.isEqual(to: self.noteOutlineView) {
  2428. if tableColumn == nil || tableColumn?.identifier.rawValue == self.noteColumnId.rawValue {
  2429. return (item as? CPDFAnnotation)?.string() ?? ""
  2430. }
  2431. }
  2432. return ""
  2433. }
  2434. func outlineView(_ outlineView: NSOutlineView, draggingSession session: NSDraggingSession, willBeginAt screenPoint: NSPoint, forItems draggedItems: [Any]) {
  2435. self.dragIn = true
  2436. }
  2437. func outlineView(_ outlineView: NSOutlineView, draggingSession session: NSDraggingSession, endedAt screenPoint: NSPoint, operation: NSDragOperation) {
  2438. self.dragIn = false
  2439. }
  2440. /*
  2441. #pragma mark NSOutlineView delegate protocol
  2442. - (void)outlineView:(NSOutlineView *)ov didClickTableColumn:(NSTableColumn *)tableColumn {
  2443. if ([ov isEqual:rightSideController.noteOutlineView]) {
  2444. NSTableColumn *oldTableColumn = [ov highlightedTableColumn];
  2445. NSTableColumn *newTableColumn = ([NSEvent modifierFlags] & NSEventModifierFlagCommand) ? nil : tableColumn;
  2446. NSMutableArray *sortDescriptors = nil;
  2447. BOOL ascending = YES;
  2448. if ([oldTableColumn isEqual:newTableColumn]) {
  2449. sortDescriptors = [[[rightSideController.noteArrayController sortDescriptors] mutableCopy] autorelease];
  2450. [sortDescriptors replaceObjectAtIndex:0 withObject:[[sortDescriptors firstObject] reversedSortDescriptor]];
  2451. ascending = [[sortDescriptors firstObject] ascending];
  2452. } else {
  2453. NSString *tcID = [newTableColumn identifier];
  2454. NSSortDescriptor *pageIndexSortDescriptor = [[[NSSortDescriptor alloc] initWithKey:SKNPDFAnnotationPageIndexKey ascending:ascending] autorelease];
  2455. NSSortDescriptor *boundsSortDescriptor = [[[NSSortDescriptor alloc] initWithKey:SKPDFAnnotationBoundsOrderKey ascending:ascending selector:@selector(compare:)] autorelease];
  2456. sortDescriptors = [NSMutableArray arrayWithObjects:pageIndexSortDescriptor, boundsSortDescriptor, nil];
  2457. if ([tcID isEqualToString:TYPE_COLUMNID]) {
  2458. [sortDescriptors insertObject:[[[NSSortDescriptor alloc] initWithKey:SKNPDFAnnotationTypeKey ascending:YES selector:@selector(noteTypeCompare:)] autorelease] atIndex:0];
  2459. } else if ([tcID isEqualToString:COLOR_COLUMNID]) {
  2460. [sortDescriptors insertObject:[[[NSSortDescriptor alloc] initWithKey:SKNPDFAnnotationColorKey ascending:YES selector:@selector(colorCompare:)] autorelease] atIndex:0];
  2461. } else if ([tcID isEqualToString:NOTE_COLUMNID]) {
  2462. [sortDescriptors insertObject:[[[NSSortDescriptor alloc] initWithKey:SKNPDFAnnotationStringKey ascending:YES selector:@selector(localizedCaseInsensitiveNumericCompare:)] autorelease] atIndex:0];
  2463. } else if ([tcID isEqualToString:AUTHOR_COLUMNID]) {
  2464. [sortDescriptors insertObject:[[[NSSortDescriptor alloc] initWithKey:SKNPDFAnnotationUserNameKey ascending:YES selector:@selector(localizedCaseInsensitiveNumericCompare:)] autorelease] atIndex:0];
  2465. } else if ([tcID isEqualToString:DATE_COLUMNID]) {
  2466. [sortDescriptors insertObject:[[[NSSortDescriptor alloc] initWithKey:SKNPDFAnnotationModificationDateKey ascending:YES] autorelease] atIndex:0];
  2467. }
  2468. if (oldTableColumn)
  2469. [ov setIndicatorImage:nil inTableColumn:oldTableColumn];
  2470. [ov setHighlightedTableColumn:newTableColumn];
  2471. }
  2472. [rightSideController.noteArrayController setSortDescriptors:sortDescriptors];
  2473. if (newTableColumn)
  2474. [ov setIndicatorImage:[NSImage imageNamed:ascending ? @"NSAscendingSortIndicator" : @"NSDescendingSortIndicator"]
  2475. inTableColumn:newTableColumn];
  2476. [ov reloadData];
  2477. }
  2478. }
  2479. - (void)outlineViewColumnDidResize:(NSNotification *)notification{
  2480. if (mwcFlags.autoResizeNoteRows &&
  2481. [[notification object] isEqual:rightSideController.noteOutlineView] &&
  2482. [[[[notification userInfo] objectForKey:@"NSTableColumn"] identifier] isEqualToString:NOTE_COLUMNID] &&
  2483. [(SKScrollView *)[[notification object] enclosingScrollView] isResizingSubviews] == NO) {
  2484. [rowHeights removeAllFloats];
  2485. [rightSideController.noteOutlineView noteHeightOfRowsWithIndexesChanged:[NSIndexSet indexSetWithIndexesInRange:NSMakeRange(0, [rightSideController.noteOutlineView numberOfRows])]];
  2486. }
  2487. }
  2488. - (void)sizeOutlineViewToContents:(NSOutlineView*) outlineView;
  2489. {
  2490. NSInteger rowCount = [outlineView numberOfRows];
  2491. for (NSInteger i = 0; i < rowCount; i++){
  2492. CGFloat rowHeight = 0;
  2493. PDFOutline *outline = [leftSideController.tocOutlineView itemAtRow:i];
  2494. if (!outline){
  2495. continue;
  2496. }
  2497. NSMutableAttributedString *attributedString = [[[NSMutableAttributedString alloc]init] autorelease];
  2498. NSDictionary *dictAttr1 = @{NSForegroundColorAttributeName:[KMAppearance KMColor_Layout_H0]};
  2499. NSAttributedString *attr1 = [[NSAttributedString alloc]initWithString:outline.label attributes:dictAttr1];
  2500. [attributedString appendAttributedString:attr1];
  2501. // NSTableCellView *viewS = [leftSideController.tocOutlineView viewAtColumn:0 row:i makeIfNecessary:YES];
  2502. NSTableColumn *tableColumn = [leftSideController.tocOutlineView tableColumnWithIdentifier:LABEL_COLUMNID];
  2503. // id cell = [tableColumn dataCell];
  2504. id cell = [tableColumn dataCellForRow:i];
  2505. [cell setObjectValue:attributedString];
  2506. CGFloat w = leftSideController.view.frame.size.width - 86;//[tableColumn width] > 260 ? [tableColumn width] : 260;
  2507. NSInteger num = [self getNum:outline];
  2508. CGFloat gap = [leftSideController.tocOutlineView indentationPerLevel];
  2509. rowHeight = [cell cellSizeForBounds:NSMakeRect(0.0, 0.0, w - (num > 0?16:0) - gap*num, CGFLOAT_MAX)].height;
  2510. rowHeight = fmax(rowHeight, [leftSideController.tocOutlineView rowHeight]) + 25;
  2511. [rowHeights setFloat:rowHeight forKey:outline];
  2512. if (@available(macOS 10.13, *)) {
  2513. } else {
  2514. rowHeight = 40.0;
  2515. }
  2516. // CGRect fram = viewS.frame;
  2517. // viewS.frame = CGRectMake(fram.origin.x, fram.origin.y, fram.size.width, rowHeight);
  2518. }
  2519. [leftSideController.tocOutlineView reloadData];
  2520. }
  2521. */
  2522. func noteItems(_ items: NSArray) -> NSArray {
  2523. let noteItems = NSMutableArray()
  2524. for item in items {
  2525. guard let anno = (item as? KMBotaAnnotationModel)?.anno else {
  2526. continue
  2527. }
  2528. if anno.type == nil {
  2529. // item = [(SKNoteText *)item note];
  2530. }
  2531. if noteItems.contains(anno) == false {
  2532. noteItems.add(anno)
  2533. }
  2534. }
  2535. return noteItems
  2536. }
  2537. }
  2538. // MARK: - KMCustomOutlineViewDelegate, KMCustomOutlineViewDataSource
  2539. extension KMLeftSideViewController: KMCustomOutlineViewDelegate, KMCustomOutlineViewDataSource {
  2540. func outlineView(_ anOutlineView: NSOutlineView, canDeleteItems items: [Any]) -> Bool {
  2541. if anOutlineView.isEqual(to: self.noteOutlineView) {
  2542. return self.hideNotes() == false && items.count > 0
  2543. } else if anOutlineView.isEqual(to: self.tocOutlineView) {
  2544. return items.count > 0
  2545. }
  2546. return false
  2547. }
  2548. func outlineView(_ anOutlineView: NSOutlineView, deleteItems items: [Any]) {
  2549. if anOutlineView.isEqual(to: self.noteOutlineView) {
  2550. if (items.isEmpty) {
  2551. return
  2552. }
  2553. self.dataUpdating = true
  2554. for item in self.noteItems(items as NSArray) {
  2555. guard let anno = item as? CPDFAnnotation else {
  2556. continue
  2557. }
  2558. self.listView?.remove(anno)
  2559. }
  2560. for item in items {
  2561. if let data = item as? KMBotaAnnotationModel {
  2562. data.sectionModel?.items.removeObject(data)
  2563. if let footerModel = data.footerModel {
  2564. data.sectionModel?.items.removeObject(footerModel)
  2565. }
  2566. }
  2567. }
  2568. self.dataUpdating = false
  2569. self.listView?.undoManager?.setActionName(KMLocalizedString("Remove Note", "Undo action name"))
  2570. self.note_refrshUIIfNeed()
  2571. } else if anOutlineView.isEqual(to: self.tocOutlineView) {
  2572. self.outlineContextMenuItemClicked_RemoveEntry(nil)
  2573. }
  2574. }
  2575. func outlineView(_ anOutlineView: NSOutlineView, canCopyItems items: [Any]) -> Bool {
  2576. if anOutlineView.isEqual(to: self.noteOutlineView) {
  2577. if (items.count == 1) {
  2578. // PDFAnnotation *annotation = [[self noteItems:items] lastObject];
  2579. // if ([annotation isKindOfClass:[PDFAnnotationStamp class]] ||
  2580. // [annotation isKindOfClass:[PDFAnnotationLink class]]) {
  2581. // return NO;
  2582. // }
  2583. }
  2584. return items.count > 0
  2585. }
  2586. return false
  2587. }
  2588. func outlineView(_ anOutlineView: NSOutlineView, copyItems items: [Any]) {
  2589. if anOutlineView.isEqual(to: self.noteOutlineView) && items.isEmpty == false {
  2590. // let pboard = NSPasteboard.general
  2591. // var copiedItems: [Any] = []
  2592. // var attrString = NSMutableAttributedString()
  2593. // var isAttributed = false
  2594. // var item: AnyObject?
  2595. // for (item in [self noteItems:items]) {
  2596. // if ([item isMovable])
  2597. // [copiedItems addObject:item];
  2598. // }
  2599. // for (item in items) {
  2600. // if ([attrString length])
  2601. // [attrString replaceCharactersInRange:NSMakeRange([attrString length], 0) withString:@"\n\n"];
  2602. // if ([(PDFAnnotation *)item type] == nil && [[(SKNoteText *)item note] isNote]) {
  2603. // [attrString appendAttributedString:[(SKNoteText *)item text]];
  2604. // isAttributed = YES;
  2605. // } else {
  2606. // [attrString replaceCharactersInRange:NSMakeRange([attrString length], 0) withString:[item string] ?: @""];
  2607. // }
  2608. // }
  2609. //
  2610. // [pboard clearContents];
  2611. // if (isAttributed)
  2612. // [pboard writeObjects:[NSArray arrayWithObjects:attrString, nil]];
  2613. // else
  2614. // [pboard writeObjects:[NSArray arrayWithObjects:[attrString string], nil]];
  2615. // if ([copiedItems count] > 0)
  2616. // [pboard writeObjects:copiedItems];
  2617. }
  2618. }
  2619. func outlineView(_ anOutlineView: NSOutlineView, typeSelectHelperSelectionStrings aTypeSelectHelper: SKTypeSelectHelper) -> NSArray {
  2620. if self.noteOutlineView.isEqual(to: anOutlineView) {
  2621. let count = self.noteOutlineView.numberOfRows
  2622. let texts = NSMutableArray(capacity: count)
  2623. for i in 0 ..< count {
  2624. let item = self.noteOutlineView.item(atRow: i)
  2625. let string = (item as? CPDFAnnotation)?.string() ?? ""
  2626. texts.add(string)
  2627. }
  2628. return texts
  2629. } else if self.tocOutlineView.isEqual(to: anOutlineView) {
  2630. let count = self.tocOutlineView.numberOfRows
  2631. let array = NSMutableArray(capacity: count)
  2632. for i in 0 ..< count {
  2633. // [array addObject:[[(PDFOutline *)[leftSideController.tocOutlineView itemAtRow:i] label] lossyStringUsingEncoding:NSASCIIStringEncoding]];
  2634. let item = self.tocOutlineView.item(atRow: i) as? CPDFOutline
  2635. array.add(item?.label ?? "")
  2636. }
  2637. return array
  2638. }
  2639. return NSArray()
  2640. }
  2641. }
  2642. // MARK: - Other
  2643. extension KMLeftSideViewController {
  2644. func tableView(_ tv: NSTableView, extractRowsWithIndexes rowIndexes: IndexSet) {
  2645. if tv.isEqual(to: self.thumbnailTableView) {
  2646. guard let document = self.pdfDocument() else {
  2647. return
  2648. }
  2649. let pages = NSMutableArray()
  2650. for idx in self.thumbnailTableView.selectedRowIndexes {
  2651. if (idx < document.pageCount) {
  2652. if let page = self.pdfDocument()?.page(at: UInt(idx)) {
  2653. pages.add(page)
  2654. }
  2655. }
  2656. }
  2657. let fileName = document.getFileNameAccordingSelctPages(pages as! [CPDFPage])
  2658. let saveAccessCtr = KMSavePanelAccessoryController()
  2659. let outputSavePanel = NSSavePanel()
  2660. outputSavePanel.allowedFileTypes = ["pdf"]
  2661. outputSavePanel.accessoryView = saveAccessCtr.view
  2662. outputSavePanel.nameFieldStringValue = fileName
  2663. outputSavePanel.beginSheetModal(for: self.view.window!) { result in
  2664. if (result == .OK) {
  2665. DispatchQueue.main.asyncAfter(deadline: .now()+0.5) {
  2666. let vc = KMProgressWindowController()
  2667. self.view.window?.beginSheet(vc.window!)
  2668. let saveFilePath = outputSavePanel.url?.path
  2669. DispatchQueue.global().async {
  2670. let pdf = CPDFDocument()
  2671. let success = pdf?.extractAsOneDocument(withPages: pages as! [CPDFPage], savePath: saveFilePath) ?? false
  2672. DispatchQueue.main.async {
  2673. if (success) {
  2674. if (saveAccessCtr.openAutomaticButton.state == .on) {
  2675. NSDocumentController.shared.km_safe_openDocument(withContentsOf: outputSavePanel.url!, display: true) { _, _, _ in
  2676. }
  2677. } else {
  2678. KMTools.viewFile(at: saveFilePath!)
  2679. }
  2680. }
  2681. NSApp.endSheet(vc.window!)
  2682. vc.close()
  2683. }
  2684. }
  2685. }
  2686. }
  2687. }
  2688. }
  2689. }
  2690. func fileNameWithSelectedPages(_ itemIndexes: IndexSet) -> String {
  2691. var pagesName = ""
  2692. if (itemIndexes.count > 1) {
  2693. pagesName.append(" pages")
  2694. } else {
  2695. pagesName.append(" page")
  2696. }
  2697. let docmentName = self.pdfDocument()?.documentURL.deletingPathExtension().lastPathComponent ?? ""
  2698. let tFileName = String(format: "%@ %@", pagesName,KMTools.parseIndexSet(indexSet: itemIndexes))
  2699. return String(format: "%@%@", docmentName,tFileName)
  2700. }
  2701. func draggedFileName(for page: CPDFPage) -> String {
  2702. let pageIndex = "\(page.pageIndex() + 1)"
  2703. var fileName = ""
  2704. if let doc = self.view.window?.windowController?.document as? NSDocument {
  2705. // fileName = doc.displayName.deletingPathExtension
  2706. fileName = doc.fileURL?.deletingPathExtension().lastPathComponent ?? (MainBundle.nameSpaceName ?? "")
  2707. }
  2708. return "\(fileName)-Page \(pageIndex)"
  2709. }
  2710. func switchType(_ type: BotaType) {
  2711. if type == .Thumbnail {
  2712. self.leftView.segmentedControl.selectedSegment = 1
  2713. } else if type == .Outline {
  2714. self.leftView.segmentedControl.selectedSegment = 2
  2715. } else if type == .Annotation {
  2716. self.leftView.segmentedControl.selectedSegment = 3
  2717. } else if type == .snapshot {
  2718. self.leftView.segmentedControl.selectedSegment = 4
  2719. } else if type == .Search {
  2720. self.leftView.segmentedControl.selectedSegment = 0
  2721. }
  2722. }
  2723. func updateTableFont() {
  2724. let font = NSFont.systemFont(ofSize: KMPreference.shared.outlineFontSize.cgFloat)
  2725. self.tocOutlineView.font = font
  2726. self.noteOutlineView.font = font
  2727. self.findTableView.font = font
  2728. self.groupedFindTableView.font = font
  2729. }
  2730. }