KMLeftSideViewController.swift 254 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992399339943995399639973998399940004001400240034004400540064007400840094010401140124013401440154016401740184019402040214022402340244025402640274028402940304031403240334034403540364037403840394040404140424043404440454046404740484049405040514052405340544055405640574058405940604061406240634064406540664067406840694070407140724073407440754076407740784079408040814082408340844085408640874088408940904091409240934094409540964097409840994100410141024103410441054106410741084109411041114112411341144115411641174118411941204121412241234124412541264127412841294130413141324133413441354136413741384139414041414142414341444145414641474148414941504151415241534154415541564157415841594160416141624163416441654166416741684169417041714172417341744175417641774178417941804181418241834184418541864187418841894190419141924193419441954196419741984199420042014202420342044205420642074208420942104211421242134214421542164217421842194220422142224223422442254226422742284229423042314232423342344235423642374238423942404241424242434244424542464247424842494250425142524253425442554256425742584259426042614262426342644265426642674268426942704271427242734274427542764277427842794280428142824283428442854286428742884289429042914292429342944295429642974298429943004301430243034304430543064307430843094310431143124313431443154316431743184319432043214322432343244325432643274328432943304331433243334334433543364337433843394340434143424343434443454346434743484349435043514352435343544355435643574358435943604361436243634364436543664367436843694370437143724373437443754376437743784379438043814382438343844385438643874388438943904391439243934394439543964397439843994400440144024403440444054406440744084409441044114412441344144415441644174418441944204421442244234424442544264427442844294430443144324433443444354436443744384439444044414442444344444445444644474448444944504451445244534454445544564457445844594460446144624463446444654466446744684469447044714472447344744475447644774478447944804481448244834484448544864487448844894490449144924493449444954496449744984499450045014502450345044505450645074508450945104511451245134514451545164517451845194520452145224523452445254526452745284529453045314532453345344535453645374538453945404541454245434544454545464547454845494550455145524553455445554556455745584559456045614562456345644565456645674568456945704571457245734574457545764577457845794580458145824583458445854586458745884589459045914592459345944595459645974598459946004601460246034604460546064607460846094610461146124613461446154616461746184619462046214622462346244625462646274628462946304631463246334634463546364637463846394640464146424643464446454646464746484649465046514652465346544655465646574658465946604661466246634664466546664667466846694670467146724673467446754676467746784679468046814682468346844685468646874688468946904691469246934694469546964697469846994700470147024703470447054706470747084709471047114712471347144715471647174718471947204721472247234724472547264727472847294730473147324733473447354736473747384739474047414742474347444745474647474748474947504751475247534754475547564757475847594760476147624763476447654766476747684769477047714772477347744775477647774778477947804781478247834784478547864787478847894790479147924793479447954796479747984799480048014802480348044805480648074808480948104811481248134814481548164817481848194820482148224823482448254826482748284829483048314832483348344835483648374838483948404841484248434844484548464847484848494850485148524853485448554856485748584859486048614862486348644865486648674868486948704871487248734874487548764877487848794880488148824883488448854886488748884889489048914892489348944895489648974898489949004901490249034904490549064907490849094910491149124913491449154916491749184919492049214922492349244925492649274928492949304931493249334934493549364937493849394940494149424943494449454946494749484949495049514952495349544955495649574958495949604961496249634964496549664967496849694970497149724973497449754976497749784979498049814982498349844985498649874988498949904991499249934994499549964997499849995000500150025003500450055006500750085009501050115012501350145015501650175018501950205021502250235024502550265027502850295030503150325033503450355036503750385039504050415042504350445045504650475048504950505051505250535054505550565057505850595060506150625063506450655066506750685069507050715072507350745075507650775078507950805081508250835084508550865087508850895090509150925093509450955096509750985099510051015102510351045105510651075108510951105111511251135114511551165117511851195120512151225123512451255126512751285129513051315132513351345135513651375138513951405141514251435144514551465147514851495150515151525153515451555156515751585159516051615162516351645165516651675168516951705171517251735174517551765177517851795180518151825183518451855186518751885189519051915192519351945195519651975198519952005201520252035204520552065207520852095210521152125213521452155216521752185219522052215222522352245225522652275228522952305231523252335234523552365237523852395240524152425243524452455246524752485249525052515252525352545255525652575258525952605261526252635264526552665267526852695270527152725273527452755276527752785279528052815282528352845285528652875288528952905291529252935294529552965297529852995300530153025303530453055306530753085309531053115312531353145315531653175318531953205321532253235324532553265327532853295330533153325333533453355336533753385339534053415342534353445345534653475348534953505351535253535354535553565357535853595360536153625363536453655366536753685369537053715372537353745375537653775378
  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. }
  17. class KMLeftSideViewController: KMSideViewController {
  18. var dataSource : [KMLeftMethodMode] = [KMLeftMethodMode]()
  19. var type : KMLeftMethodMode = KMLeftMethodMode()
  20. var isShowPanel : Bool = false
  21. var norImage : [String] = []
  22. var selectImage : [String] = []
  23. var mainVC: KMMainViewController?
  24. var selectPages: [Int]?
  25. open weak var delegate: KMLeftSideViewControllerDelegate?
  26. deinit {
  27. KMPrint("KMLeftSideViewController deinit.")
  28. NotificationCenter.default.removeObserver(self)
  29. }
  30. override var nibName: NSNib.Name? {
  31. return "LeftSideView"
  32. }
  33. convenience init(type : KMLeftMethodMode) {
  34. self.init()
  35. self.type = type
  36. }
  37. override func viewDidLoad() {
  38. super.viewDidLoad()
  39. self.reloadThumbnailSize()
  40. self.reloadSnapshotSize()
  41. DistributedNotificationCenter.default().addObserver(self, selector: #selector(_themeChanged), name: NSApplication.interfaceThemeChangedNotification, object: nil)
  42. self.isDisplayPageSize = UserDefaults.standard.bool(forKey: "kKMThumbnailDisplayPageSizeKey")
  43. }
  44. func showPanelView(show: Bool) {
  45. self.isShowPanel = show
  46. if show {
  47. self.leftView.segmentedControl.selectedSegment = 0
  48. } else {
  49. self.leftView.segmentedControl.selectedSegment = UInt8.max
  50. self.delegate?.controlStateChange?(self, show: false)
  51. }
  52. }
  53. func refreshMethodType(methodType: BotaType) {
  54. let newType = KMLeftMethodMode()
  55. var show = true
  56. if self.type.methodType != methodType {
  57. newType.methodType = methodType
  58. }
  59. if self.type.methodType == methodType {
  60. show = false
  61. } else if methodType == .None {
  62. show = false
  63. }
  64. self.type = newType;
  65. self.delegate?.controlStateChange?(self,show:show)
  66. }
  67. // MARK: - New
  68. /*
  69. NSArrayController *findArrayController;
  70. NSArrayController *groupedFindArrayController;
  71. }
  72. - (void)applySearchTableHeader:(NSString *)message;
  73. */
  74. @IBOutlet var segmentedControl: KMSegmentedControl!
  75. @IBOutlet var thumbnailTableView: KMThumbnailTableView!
  76. @IBOutlet var tocOutlineView: KMTocOutlineView!
  77. @IBOutlet var noteOutlineView: KMNoteOutlineView!
  78. @IBOutlet var findTableView: KMBotaTableView!
  79. @IBOutlet var groupedFindTableView: KMBotaTableView!
  80. @IBOutlet var snapshotTableView: KMBotaTableView!
  81. @IBOutlet weak var leftListView: NSView!
  82. @IBOutlet var searchViewController: KMBotaSearchViewController!
  83. @IBOutlet weak var toolButtonBox: NSBox!
  84. @IBOutlet weak var toolButtonBoxLayoutConstraint: NSLayoutConstraint!
  85. @IBOutlet weak var snapshotNormalView: NSView!
  86. @IBOutlet weak var snapshotLabel: NSTextField!
  87. @IBOutlet weak var snapshotNormalMoreButton: NSButton!
  88. @IBOutlet weak var snapshotNormalSearchButton: NSButton!
  89. @IBOutlet weak var snapshotNormalZoomOutButton: NSButton!
  90. @IBOutlet weak var snapshotNormalZoomInButton: NSButton!
  91. @IBOutlet weak var snapshotSearchZoomOutButton: NSButton!
  92. @IBOutlet weak var snapshotSearchZoomInButton: NSButton!
  93. @IBOutlet weak var snapshotSearchField: KMLeftSideViewSearchField!
  94. @IBOutlet weak var snapshotDoneButton: NSButton!
  95. @IBOutlet weak var outlineView: NSView!
  96. @IBOutlet weak var outlineMoreButton: NSButton!
  97. @IBOutlet weak var outlineAddButton: NSButton!
  98. @IBOutlet weak var outlineSearchButton: NSButton!
  99. @IBOutlet weak var outlineLabel: NSTextField!
  100. @IBOutlet weak var outlineSearchField: KMLeftSideViewSearchField!
  101. @IBOutlet weak var outlineDoneButton: NSButton!
  102. @IBOutlet weak var noteView: NSView!
  103. @IBOutlet weak var noteMoreButton: NSButton!
  104. @IBOutlet weak var noteFilterButton: NSButton!
  105. @IBOutlet weak var noteSearchButton: NSButton!
  106. @IBOutlet weak var noteSearchField: KMLeftSideViewSearchField!
  107. @IBOutlet weak var noteTitleLabel: NSTextField!
  108. @IBOutlet weak var noteHeaderView: NSView!
  109. @IBOutlet weak var sortTypeBox: KMBox!
  110. @IBOutlet weak var sortTypeLabel: NSTextField!
  111. @IBOutlet weak var noteSortButton: NSButton!
  112. @IBOutlet weak var noteDoneButton: NSButton!
  113. @IBOutlet weak var thumbnailView: NSView!
  114. @IBOutlet weak var thumbnailZoomOutButton: NSButton!
  115. @IBOutlet weak var thumbnailZoomInButton: NSButton!
  116. @IBOutlet weak var thumbnailTitleLabel: NSTextField!
  117. @IBOutlet weak var emptySearchBox: NSBox!
  118. @IBOutlet weak var emptySearchLabel: NSTextField!
  119. var filterButtonLayer: NSView?
  120. var moreButtonLayer: KMButtonLayer?
  121. var thumbnails: [KMThumbnail] = []
  122. var isDisplayPageSize = false
  123. var thumbnailCacheSize: CGFloat = 32 * 3
  124. var snapshotCacheSize: CGFloat = 32 * 3
  125. private var _findState: KMFindState = .none
  126. var findState: KMFindState {
  127. get {
  128. return self._findState
  129. }
  130. set {
  131. if self._findState != newValue {
  132. self._findState = newValue
  133. self.displayFindState()
  134. if self._findState == .content {
  135. self.search(self.searchField)
  136. } else if self.findState == .note {
  137. // self.searchNotes(self.searchField)
  138. self.searchNotes(nil)
  139. } else if self.findState == .snapshot {
  140. self.searchNotes(self.searchField)
  141. }
  142. }
  143. }
  144. }
  145. var searchResults : [KMSearchMode] = [] {
  146. didSet {
  147. self.updataLeftSideFindView()
  148. }
  149. }
  150. var groupSearchResults: [KMSearchMode] = []
  151. var findPaneState: KMFindPaneState = .singular
  152. var isSearchOutlineMode = false
  153. var isSearchSnapshotMode = false
  154. var outlineIgnoreCaseFlag = false
  155. var noteTypeDict: [String : Any] = [:]
  156. private let MIN_SIDE_PANE_WIDTH: CGFloat = 270
  157. private let LABEL_COLUMNID = "label"
  158. var foldType: KMFoldAllAnnotationType = .none
  159. private let KMLeftSideViewNoteSortTypeKey = "KMLeftSideViewNoteSortTypeKey"
  160. private let KMLeftSideViewAscendSortBoolKey = "KMLeftSideViewAscendSortBoolKey"
  161. private let SKDisableTableToolTipsKey = "SKDisableTableToolTips"
  162. var tocType: KMFoldType = .none
  163. var snapshots: [KMSnapshotModel] = []
  164. var searchSnapshots: [KMSnapshotModel] = []
  165. private var _noteSortType: KMNoteSortType = .none
  166. var noteSortType: KMNoteSortType {
  167. get {
  168. return self._noteSortType
  169. }
  170. set {
  171. self._noteSortType = newValue
  172. NotificationCenter.default.post(name: NSNotification.Name(rawValue: "KMAnnotationSortTypeKeyNotification"), object: self)
  173. }
  174. }
  175. var isAscendSort = false {
  176. didSet {
  177. NotificationCenter.default.post(name: NSNotification.Name(rawValue: "KMAnnotationSortTypeKeyNotification"), object: self)
  178. }
  179. }
  180. lazy var leftSideEmptyVC: KMLeftSideEmptyFileViewController = {
  181. let vc = KMLeftSideEmptyFileViewController(nibName: "KMLeftSideEmptyFileViewController", bundle: nil)
  182. vc.view.wantsLayer = true
  183. return vc
  184. }()
  185. var preThumbnailRow: Int = 0
  186. var leftMargin: CGFloat = 0
  187. lazy var leftView: KMBotaLeftView = {
  188. let view = KMBotaLeftView()
  189. return view
  190. }()
  191. private var _copysPages: [CPDFPage] = []
  192. var copyPages: [CPDFPage] {
  193. get {
  194. return self._copysPages
  195. }
  196. }
  197. private var _annotations: [KMBOTAAnnotationSection] = []
  198. private var _allAnnotations: [CPDFAnnotation] = []
  199. var allAnnotations: [CPDFAnnotation] {
  200. get {
  201. return self._allAnnotations
  202. }
  203. }
  204. private let kKMPDFViewOutlineDragDataType = NSPasteboard.PasteboardType("kKMPDFViewOutlineDragDataType")
  205. private let KPDFThumbnailDoucumentURLForDraggedTypes = NSPasteboard.PasteboardType("KPDFThumbnailDoucumentURLForDraggedTypes")
  206. var renamePDFOutline: CPDFOutline?
  207. var renamePDFOutlineTextField: NSTextField?
  208. var allFoldNotes: [CPDFAnnotation] = []
  209. var notes: [CPDFAnnotation] = []
  210. var canFoldNotes: [CPDFAnnotation] = []
  211. var isRenameNoteOutline = false
  212. var caseInsensitiveNoteSearch = false
  213. var noteSearchArray: [CPDFAnnotation] = []
  214. var noteSearchMode = false
  215. var mwcFlags: MwcFlags = MwcFlags()
  216. private var _dragPDFOutline: CPDFOutline?
  217. override func loadView() {
  218. super.loadView()
  219. self.view.wantsLayer = true
  220. self.view.layer?.backgroundColor = KMAppearance.Layout.l0Color().cgColor
  221. self.view.addSubview(self.leftView)
  222. self.leftView.frame = NSMakeRect(0, 0, 44, NSHeight(self.view.frame))
  223. self.leftView.autoresizingMask = [.height]
  224. self.leftView.wantsLayer = true
  225. self.leftView.layer?.backgroundColor = .white
  226. self.outlineView.wantsLayer = true
  227. self.outlineView.layer?.backgroundColor = KMAppearance.Layout.l0Color().cgColor
  228. self.noteView.wantsLayer = true
  229. self.noteView.layer?.backgroundColor = KMAppearance.Layout.l0Color().cgColor
  230. self.thumbnailView.wantsLayer = true
  231. self.thumbnailView.layer?.backgroundColor = KMAppearance.Layout.l0Color().cgColor
  232. self.snapshotNormalView.wantsLayer = true
  233. self.snapshotNormalView.layer?.backgroundColor = KMAppearance.Layout.l0Color().cgColor
  234. self.tocOutlineView.backgroundColor = KMAppearance.Layout.l0Color()
  235. self.noteOutlineView.backgroundColor = KMAppearance.Layout.l0Color()
  236. self.findTableView.backgroundColor = KMAppearance.Layout.l0Color()
  237. self.groupedFindTableView.backgroundColor = KMAppearance.Layout.l0Color()
  238. self.snapshotTableView.backgroundColor = KMAppearance.Layout.l0Color()
  239. self.thumbnailTableView.backgroundColor = KMAppearance.Layout.l0Color()
  240. self.leftListView.wantsLayer = true
  241. self.leftListView.layer?.backgroundColor = KMAppearance.Layout.l0Color().cgColor
  242. self.emptySearchLabel.stringValue = KMLocalizedString("No Results",nil)
  243. self.emptySearchLabel.textColor = KMAppearance.Layout.h0Color()
  244. self.emptySearchBox.isHidden = true
  245. self.thumbnailTitleLabel.stringValue = KMLocalizedString("Thumbnails", nil)
  246. self.thumbnailTitleLabel.textColor = KMAppearance.Layout.h0Color()
  247. self.thumbnailZoomInButton.action = #selector(thumbnailSizeScaling)
  248. self.thumbnailZoomInButton.target = self
  249. self.thumbnailZoomInButton.tag = 1
  250. self.thumbnailZoomOutButton.action = #selector(thumbnailSizeScaling)
  251. self.thumbnailZoomOutButton.target = self
  252. self.thumbnailZoomOutButton.tag = 0
  253. self.snapshotTableView.delegate = self
  254. self.snapshotTableView.dataSource = self
  255. self.snapshotTableView.menu = NSMenu()
  256. self.snapshotTableView.menu?.delegate = self
  257. self.snapshotTableView.doubleAction = #selector(toggleSelectedSnapshots)
  258. self.snapshotTableView.target = self
  259. self.snapshotTableView.backgroundColor = .clear
  260. // [snapshotTableView setDraggingSourceOperationMask:NSDragOperationEvery forLocal:NO];
  261. self.snapshotLabel.stringValue = KMLocalizedString("Snapshots", nil)
  262. self.snapshotLabel.textColor = KMAppearance.Layout.h0Color()
  263. self.snapshotNormalZoomInButton.action = #selector(thumbnailSizeScaling)
  264. self.snapshotNormalZoomInButton.target = self
  265. self.snapshotNormalZoomInButton.tag = 2
  266. self.snapshotNormalZoomOutButton.action = #selector(thumbnailSizeScaling)
  267. self.snapshotNormalZoomOutButton.target = self
  268. self.snapshotNormalZoomOutButton.tag = 3
  269. self.snapshotNormalSearchButton.toolTip = KMLocalizedString("Search", nil)
  270. self.snapshotNormalMoreButton.action = #selector(leftSideViewMoreButtonAction)
  271. self.snapshotNormalMoreButton.target = self
  272. self.snapshotNormalMoreButton.tag = 300
  273. self.snapshotSearchZoomInButton.action = #selector(thumbnailSizeScaling)
  274. self.snapshotSearchZoomInButton.target = self
  275. self.snapshotSearchZoomInButton.tag = 2
  276. self.snapshotSearchZoomOutButton.action = #selector(thumbnailSizeScaling)
  277. self.snapshotSearchZoomOutButton.target = self
  278. self.snapshotSearchZoomOutButton.tag = 3
  279. self.snapshotDoneButton.title = KMLocalizedString("Done", nil)
  280. self.snapshotDoneButton.toolTip = KMLocalizedString("Done", nil)
  281. self.snapshotDoneButton.setTitleColor(KMAppearance.Layout.w0Color())
  282. self.snapshotDoneButton.wantsLayer = true
  283. self.snapshotDoneButton.layer?.backgroundColor = KMAppearance.Interactive.a0Color().cgColor
  284. self.snapshotDoneButton.layer?.cornerRadius = 4.0
  285. self.snapshotDoneButton.action = #selector(leftSideViewDoneButtonAction)
  286. self.snapshotDoneButton.target = self
  287. self.snapshotDoneButton.tag = 312
  288. self.snapshotDoneButton.isHidden = true
  289. self.noteOutlineView.autoresizesOutlineColumn = false
  290. self.noteOutlineView.delegate = self
  291. self.noteOutlineView.dataSource = self
  292. self.noteOutlineView.botaDelegate = self
  293. self.noteOutlineView.botaDataSource = self
  294. self.noteOutlineView.noteDelegate = self
  295. self.noteOutlineView.menu = NSMenu()
  296. self.noteOutlineView.menu?.delegate = self
  297. // [noteOutlineView setTypeSelectHelper:[SKTypeSelectHelper typeSelectHelperWithMatchOption:SKSubstringMatch]];
  298. self.noteOutlineView.indentationPerLevel = 0
  299. // [noteOutlineView registerForDraggedTypes:[NSColor readableTypesForPasteboard:[NSPasteboard pasteboardWithName:NSDragPboard]]];
  300. self.noteOutlineView.registerForDraggedTypes(NSColor.readableTypes(for: NSPasteboard(name: .drag)))
  301. self.noteOutlineView.target = self
  302. self.noteOutlineView.doubleAction = #selector(selectSelectedNote)
  303. self.tocOutlineView.menu = NSMenu()
  304. self.tocOutlineView.menu?.delegate = self
  305. self.outlineSearchField.delegate = self
  306. self.snapshotSearchField.backgroundColor = KMAppearance.Layout.l_1Color()
  307. self.outlineSearchField.backgroundColor = KMAppearance.Layout.l_1Color()
  308. self.noteSearchField.backgroundColor = KMAppearance.Layout.l_1Color()
  309. self.snapshotSearchField.wantsLayer = true
  310. self.outlineSearchField.wantsLayer = true
  311. self.noteSearchField.wantsLayer = true
  312. self.snapshotSearchField.layer?.backgroundColor = KMAppearance.Layout.l_1Color().cgColor
  313. self.outlineSearchField.layer?.backgroundColor = KMAppearance.Layout.l_1Color().cgColor
  314. self.noteSearchField.layer?.backgroundColor = KMAppearance.Layout.l_1Color().cgColor
  315. self.snapshotSearchField.layer?.borderWidth = 1.0
  316. self.outlineSearchField.layer?.borderWidth = 1.0
  317. self.noteSearchField.layer?.borderWidth = 1.0
  318. self.snapshotSearchField.layer?.borderColor = KMAppearance.Interactive.a0Color().cgColor
  319. self.outlineSearchField.layer?.borderColor = KMAppearance.Interactive.a0Color().cgColor
  320. self.noteSearchField.layer?.borderColor = KMAppearance.Interactive.a0Color().cgColor
  321. // __block typeof(self) blockSelf = self;
  322. // _snapshotSearchField.changeCallBack = ^(NSString *changeContent) {
  323. // NSString *editContent = @"";
  324. // if (changeContent) {
  325. // editContent = changeContent;
  326. // }
  327. // [blockSelf.mainController searchFieldChangeAction:editContent];
  328. // };
  329. self.snapshotSearchField.isHidden = true
  330. self.snapshotSearchZoomOutButton.isHidden = true
  331. self.snapshotSearchZoomInButton.isHidden = true
  332. // _snapshotSearchField.endEditCallBack = ^(BOOL isEndEdit) {
  333. // if (isEndEdit/* && searchViewController.segmentedControl.segmentCount == 3*/) {
  334. // _snapshotSearchField.hidden = YES;
  335. // _snapshotSearchZoomOutButton.hidden = YES;
  336. // _snapshotSearchZoomInButton.hidden = YES;
  337. // _snapshotLabel.hidden = NO;
  338. // _snapshotNormalZoomOutButton.hidden = NO;
  339. // _snapshotNormalZoomInButton.hidden = NO;
  340. // }
  341. // };
  342. self.thumbnailZoomInButton.toolTip = KMLocalizedString("Zoom In", nil)
  343. self.snapshotNormalZoomInButton.toolTip = KMLocalizedString("Zoom In", nil)
  344. self.snapshotSearchZoomInButton.toolTip = KMLocalizedString("Zoom In", nil)
  345. self.thumbnailZoomOutButton.toolTip = KMLocalizedString("Zoom Out", nil)
  346. self.snapshotNormalZoomOutButton.toolTip = KMLocalizedString("Zoom Out", nil)
  347. self.snapshotSearchZoomOutButton.toolTip = KMLocalizedString("Zoom Out", nil)
  348. self.outlineLabel.stringValue = KMLocalizedString("Outline", nil);
  349. self.outlineLabel.textColor = KMAppearance.Layout.h0Color()
  350. self.outlineAddButton.toolTip = KMLocalizedString("Add Item", nil)
  351. self.outlineAddButton.action = #selector(outlineContextMenuItemClicked_AddEntry)
  352. self.outlineAddButton.target = self
  353. self.outlineSearchButton.toolTip = KMLocalizedString("Search", nil)
  354. self.outlineMoreButton.action = #selector(leftSideViewMoreButtonAction)
  355. self.outlineMoreButton.target = self
  356. self.outlineMoreButton.tag = 302
  357. self.outlineDoneButton.title = KMLocalizedString("Done", nil);
  358. self.outlineDoneButton.toolTip = KMLocalizedString("Done", nil);
  359. self.outlineDoneButton.setTitleColor(KMAppearance.Layout.w0Color())
  360. self.outlineDoneButton.wantsLayer = true
  361. self.outlineDoneButton.layer?.backgroundColor = KMAppearance.Interactive.a0Color().cgColor
  362. self.outlineDoneButton.layer?.cornerRadius = 4.0
  363. self.outlineDoneButton.action = #selector(leftSideViewDoneButtonAction)
  364. self.outlineDoneButton.target = self
  365. self.outlineDoneButton.tag = 310
  366. self.outlineDoneButton.isHidden = true
  367. self.outlineSearchField.isHidden = true
  368. // _outlineSearchField.endEditCallBack = ^(BOOL isEndEdit) {
  369. // if (isEndEdit) {
  370. // _outlineSearchField.hidden = YES;
  371. // _outlineLabel.hidden = NO;
  372. // _outlineSearchButton.hidden = NO;
  373. // }
  374. // };
  375. let menuOutline = NSMenu()
  376. menuOutline.addItem(title: KMLocalizedString("Ignore Case", "Menu item title"), action: #selector(toggleOutlineCaseInsensitiveSearch), target: self)
  377. (self.outlineSearchField.cell as? NSSearchFieldCell)?.searchMenuTemplate = menuOutline
  378. (self.outlineSearchField.cell as? NSSearchFieldCell)?.placeholderString = KMLocalizedString("Search Outline", nil)
  379. self.outlineSearchField.target = self
  380. let sud = UserDefaults.standard
  381. self.noteTitleLabel.stringValue = KMLocalizedString("Notes", nil);
  382. self.noteTitleLabel.textColor = KMAppearance.Layout.h0Color()
  383. self.noteMoreButton.action = #selector(leftSideViewMoreButtonAction)
  384. self.noteMoreButton.target = self
  385. self.noteMoreButton.tag = 304
  386. self.noteMoreButton.wantsLayer = true
  387. self.moreButtonLayer = KMButtonLayer()
  388. self.noteMoreButton.layer?.addSublayer(self.moreButtonLayer!)
  389. self.moreButtonLayer?.frame = CGRectMake(0, 0, CGRectGetWidth(self.noteMoreButton.bounds), CGRectGetHeight(self.noteMoreButton.bounds))
  390. self.moreButtonLayer?.layerType = .none
  391. self.moreButtonLayer?.isHidden = true
  392. self.noteFilterButton.action = #selector(noteFilterAction)
  393. self.noteFilterButton.target = self
  394. self.noteFilterButton.toolTip = KMLocalizedString("Sort", nil)
  395. self.noteFilterButton.wantsLayer = true
  396. self.filterButtonLayer = NSView()
  397. self.noteFilterButton.addSubview(self.filterButtonLayer!)
  398. self.filterButtonLayer?.frame = CGRectMake(14, 2, 8, 8)
  399. self.filterButtonLayer?.isHidden = true
  400. self.filterButtonLayer?.wantsLayer = true
  401. self.filterButtonLayer?.layer?.backgroundColor = KMAppearance.Interactive.a0Color().cgColor
  402. self.filterButtonLayer?.layer?.cornerRadius = 4.0
  403. self.noteDoneButton.title = KMLocalizedString("Done", nil)
  404. self.noteDoneButton.toolTip = KMLocalizedString("Done", nil)
  405. self.noteDoneButton.setTitleColor(KMAppearance.Layout.w0Color())
  406. self.noteDoneButton.wantsLayer = true
  407. self.noteDoneButton.layer?.backgroundColor = KMAppearance.Interactive.a0Color().cgColor
  408. self.noteDoneButton.layer?.cornerRadius = 4.0
  409. self.noteDoneButton.action = #selector(leftSideViewDoneButtonAction)
  410. self.noteDoneButton.target = self
  411. self.noteDoneButton.tag = 311
  412. self.noteDoneButton.isHidden = true
  413. self.noteSearchButton.toolTip = KMLocalizedString("Search", nil)
  414. self.noteSearchField.delegate = self
  415. self.noteSearchField.isHidden = true
  416. self.noteSearchField.endEditCallBack = { [unowned self] isEndEdit in
  417. if (isEndEdit) {
  418. self.noteSearchField.isHidden = true
  419. self.noteSearchButton.isHidden = false
  420. self.noteTitleLabel.isHidden = false
  421. }
  422. };
  423. self.noteHeaderView.wantsLayer = true
  424. self.noteHeaderView.layer?.backgroundColor = KMAppearance.Else.textTagColor().cgColor
  425. self.noteHeaderView.layer?.cornerRadius = 1.0
  426. let sortType = sud.integer(forKey: KMLeftSideViewNoteSortTypeKey)
  427. if (sortType == 1) {
  428. self.noteSortType = KMNoteSortType(rawValue: sortType)!
  429. if (self.noteSortType == .time) {
  430. self.sortTypeLabel.stringValue = KMLocalizedString("Time", nil)
  431. self.sortTypeBox.toolTip = KMLocalizedString("Time", nil)
  432. } else if (self._noteSortType == .page) {
  433. self.sortTypeLabel.stringValue = KMLocalizedString("Page", nil)
  434. self.sortTypeBox.toolTip = KMLocalizedString("Page", nil)
  435. }
  436. } else {
  437. self.noteSortType = .time
  438. self.sortTypeLabel.stringValue = KMLocalizedString("Time", nil)
  439. }
  440. self.sortTypeLabel.textColor = KMAppearance.Layout.h1Color()
  441. self.isAscendSort = sud.bool(forKey: KMLeftSideViewAscendSortBoolKey)
  442. if (self.isAscendSort) {
  443. self.noteSortButton.image = NSImage(named: KMImageNameBtnSidebarRankReverse)
  444. self.noteSortButton.toolTip = KMLocalizedString("ascending sort", nil)
  445. } else {
  446. self.noteSortButton.image = NSImage(named: KMImageNameBtnSidebarRankPositive)
  447. self.noteSortButton.toolTip = KMLocalizedString("descending sort", nil)
  448. }
  449. self.sortTypeBox.downCallback = { [unowned self] downEntered, mouseBox, _ in
  450. if (downEntered) {
  451. let menu = NSMenu()
  452. let timeItem = menu.addItem(title: KMLocalizedString("Time", nil), action: #selector(sortTypeAction), target: self)
  453. timeItem?.representedObject = self
  454. timeItem?.tag = 0
  455. let pageItem = menu.addItem(title: KMLocalizedString("Page", nil), action: #selector(sortTypeAction), target: self)
  456. pageItem?.representedObject = self
  457. timeItem?.tag = 1
  458. if (self.noteSortType == .time) {
  459. timeItem?.state = .on
  460. pageItem?.state = .off
  461. } else if (self.noteSortType == .page) {
  462. timeItem?.state = .off
  463. pageItem?.state = .on
  464. }
  465. menu.popUp(positioning: nil, at: CGPointMake(-10, 0), in: self.sortTypeBox)
  466. }
  467. }
  468. self.searchViewController.loadView()
  469. self.searchViewController.contentView = self.findTableView.enclosingScrollView
  470. self.searchField = self.searchViewController.searchField
  471. self.searchViewController.segmentedControl.setSegmentCount(2, with: 25)
  472. self.searchViewController.segmentedControl.setImage(NSImage(named: KMImageNameUXIconBtnSidebarListNor)!, for: 0)
  473. self.searchViewController.segmentedControl.setImage(NSImage(named: KMImageNameUXIconBtnSidebarPageNor)!, for: 1)
  474. self.searchViewController.segmentedControl.setToolTip(KMLocalizedString("Separate search results", nil), for: 0)
  475. self.searchViewController.segmentedControl.setToolTip(KMLocalizedString("Group search results by page", nil), for: 1)
  476. self.searchViewController.segmentedControl.isBackgroundHighlighted = true
  477. self.searchViewController.segmentedControl.selectedSegment = 0
  478. self.searchViewController.segmentedControl.block = { [unowned self] segIndex in
  479. if segIndex == 0 {
  480. self.findPaneState = .singular
  481. self.displayFindViewAnimating(false)
  482. } else {
  483. self.findPaneState = .grouped
  484. self.displayGroupedFindViewAnimating(false)
  485. }
  486. }
  487. self.leftView.segmentedControl.block = { [unowned self] segIndex in
  488. self.toolButtonBox.isHidden = false
  489. self.toolButtonBoxLayoutConstraint.constant = 40.0
  490. if (segIndex == 0) {
  491. if self.type.methodType == .Thumbnail {
  492. self.leftView.segmentedControl.selectedSegment = UInt8.max
  493. self.refreshMethodType(methodType: .None)
  494. return
  495. }
  496. self.refreshMethodType(methodType: .Thumbnail)
  497. DispatchQueue.main.async {
  498. self.toolButtonBox.contentView = self.thumbnailView
  499. self.displayThumbnailViewAnimating(false)
  500. }
  501. } else if (segIndex == 1) {
  502. if self.type.methodType == .Outline {
  503. self.leftView.segmentedControl.selectedSegment = UInt8.max
  504. self.refreshMethodType(methodType: .None)
  505. return
  506. }
  507. self.refreshMethodType(methodType: .Outline)
  508. DispatchQueue.main.async {
  509. self.toolButtonBox.contentView = self.outlineView
  510. self.displayTocViewAnimating(false)
  511. }
  512. } else if (segIndex == 2) {
  513. if self.type.methodType == .Annotation {
  514. self.leftView.segmentedControl.selectedSegment = UInt8.max
  515. self.refreshMethodType(methodType: .None)
  516. return
  517. }
  518. self.refreshMethodType(methodType: .Annotation)
  519. DispatchQueue.main.async {
  520. self.toolButtonBox.contentView = self.noteView
  521. self.displayNoteViewAnimating(false)
  522. }
  523. } else if (segIndex == 3) {
  524. self.toolButtonBox.contentView = self.snapshotNormalView
  525. self.updateSnapshotFilterPredicate()
  526. self.displaySnapshotViewAnimating(false)
  527. self.updataLeftSideSnapView()
  528. } else if (segIndex == 4) {
  529. if self.type.methodType == .Search {
  530. self.leftView.segmentedControl.selectedSegment = UInt8.max
  531. self.refreshMethodType(methodType: .None)
  532. return
  533. }
  534. self.refreshMethodType(methodType: .Search)
  535. DispatchQueue.main.async {
  536. self.toolButtonBox.isHidden = true
  537. self.toolButtonBoxLayoutConstraint.constant = 0
  538. self.displayFindViewAnimating(false)
  539. }
  540. }
  541. }
  542. // self.button.setHelp(KMLocalizedString("View Thumbnails", "Tool tip message"), for: KMLeftSidePaneState.thumbnail.rawValue)
  543. // self.button.setHelp(KMLocalizedString("View Outline", "Tool tip message"), for: KMLeftSidePaneState.outline.rawValue)
  544. // self.alternateButton.setHelp(KMLocalizedString("Separate search results", "Tool tip message"), for: KMFindPaneState.singular.rawValue)
  545. // self.alternateButton.setHelp(KMLocalizedString("Group search results by page", "Tool tip message"), for: KMFindPaneState.grouped.rawValue)
  546. let menu = NSMenu()
  547. menu.addItem(title: KMLocalizedString("Whole Words Only", "Menu item title"), action: #selector(toggleWholeWordSearch), target: self)
  548. menu.addItem(title: KMLocalizedString("Ignore Case", "Menu item title"), action: #selector(toggleCaseInsensitiveSearch), target: self)
  549. (self.searchField.cell as? NSSearchFieldCell)?.searchMenuTemplate = menu
  550. (self.searchField.cell as? NSSearchFieldCell)?.placeholderString = KMLocalizedString("Search PDF", "placeholder")
  551. self.searchField.action = #selector(search)
  552. self.searchField.target = self
  553. self.tocOutlineView.autoresizesOutlineColumn = false
  554. self.tocOutlineView.allowsMultipleSelection = true
  555. self.tocOutlineView.allowsEmptySelection = true
  556. self.tocOutlineView.delegate = self
  557. self.tocOutlineView.dataSource = self
  558. self.tocOutlineView.botaDelegate = self
  559. self.tocOutlineView.botaDataSource = self
  560. self.tocOutlineView.tocDelegate = self
  561. self.thumbnailTableView.delegate = self
  562. self.thumbnailTableView.dataSource = self
  563. self.thumbnailTableView.thumbDelegate = self
  564. self.thumbnailTableView.allowsMultipleSelection = true
  565. self.findTableView.delegate = self
  566. self.findTableView.dataSource = self
  567. self.findTableView.botaDelegate = self
  568. self.groupedFindTableView.delegate = self
  569. self.groupedFindTableView.dataSource = self
  570. self.groupedFindTableView.botaDelegate = self
  571. self.thumbnailTableView.menu?.delegate = self
  572. self.findTableView.menu?.delegate = self
  573. self.groupedFindTableView.menu?.delegate = self
  574. self.tocOutlineView.doubleAction = #selector(goToSelectedOutlineItem)
  575. self.tocOutlineView.target = self
  576. self.findTableView.doubleAction = #selector(goToSelectedFindResults)
  577. self.findTableView.target = self
  578. self.groupedFindTableView.doubleAction = #selector(goToSelectedFindResults)
  579. self.groupedFindTableView.target = self
  580. // [thumbnailTableView setTypeSelectHelper:[SKTypeSelectHelper typeSelectHelperWithMatchOption:SKFullStringMatch]];
  581. // //支持拖拽的文字类型
  582. // [thumbnailTableView registerForDraggedTypes:@[KPDFThumbnailLocalForDraggedTypes,NSFilenamesPboardType]];
  583. self.thumbnailTableView.registerForDraggedTypes([.localDraggedTypes, .fileURL,.string,.pdf])
  584. // self.thumbnailTableView.registerForDraggedTypes(NSFilePromiseReceiver.readableDraggedTypes.map { NSPasteboard.PasteboardType($0) })
  585. // self.thumbnailTableView.setDraggingSourceOperationMask([.copy, .delete], forLocal: true)
  586. // [tocOutlineView setTypeSelectHelper:[SKTypeSelectHelper typeSelectHelperWithMatchOption:SKSubstringMatch]];
  587. self.tocOutlineView.registerForDraggedTypes([kKMPDFViewOutlineDragDataType, .localDraggedTypes])
  588. // [[[findTableView tableColumnWithIdentifier:PAGE_COLUMNID] headerCell] setTitle:NSLocalizedString(@"Page", @"Table header title")];
  589. // [[[groupedFindTableView tableColumnWithIdentifier:PAGE_COLUMNID] headerCell] setTitle:NSLocalizedString(@"Page", @"Table header title")];
  590. // [[[groupedFindTableView tableColumnWithIdentifier:RELEVANCE_COLUMNID] dataCell] setEnabled:NO];
  591. self.thumbnailTableView.selectionHighlightStyle = .none
  592. // NSSortDescriptor *countDescriptor = [[[NSSortDescriptor alloc] initWithKey:SKGroupedSearchResultCountKey ascending:NO] autorelease];
  593. // [groupedFindArrayController setSortDescriptors:[NSArray arrayWithObjects:countDescriptor, nil]];
  594. //
  595. // self.thumbnailTableView.setDraggingSourceOperationMask(.every, forLocal: false)
  596. if UserDefaults.standard.bool(forKey: SKDisableTableToolTipsKey) == false {
  597. self.tocOutlineView.hasImageToolTips = true
  598. self.findTableView.hasImageToolTips = true
  599. self.groupedFindTableView.hasImageToolTips = true
  600. }
  601. self._updateViewColor()
  602. self.mwcFlags.wholeWordSearch = UserDefaults.standard.integer(forKey: SKWholeWordSearchKey)
  603. self.mwcFlags.caseInsensitiveSearch = UserDefaults.standard.integer(forKey: SKCaseInsensitiveSearchKey)
  604. }
  605. func displayThumbnailViewAnimating(_ animate: Bool) {
  606. self.replaceSideView(self.thumbnailTableView.enclosingScrollView!, animate: animate)
  607. var frame = self.thumbnailTableView.enclosingScrollView?.frame ?? .zero
  608. frame.origin.y = 0
  609. frame.origin.x = self.leftMargin
  610. frame.size.height = self.thumbnailTableView.enclosingScrollView?.superview?.frame.size.height ?? 0
  611. self.thumbnailTableView.enclosingScrollView?.frame = frame
  612. self.resetThumbnails()
  613. frame = self.noteOutlineView.enclosingScrollView?.frame ?? .zero
  614. frame.origin.y = 0
  615. frame.origin.x = self.leftMargin
  616. frame.size.height = self.noteOutlineView.enclosingScrollView?.superview?.frame.size.height ?? 0
  617. self.noteOutlineView.enclosingScrollView?.frame = frame
  618. frame = self.snapshotTableView.enclosingScrollView?.frame ?? .zero
  619. frame.origin.y = 0
  620. frame.origin.x = self.leftMargin
  621. frame.size.height = self.snapshotTableView.enclosingScrollView?.superview?.frame.size.height ?? 0
  622. self.snapshotTableView.enclosingScrollView?.frame = frame
  623. // [self updateThumbnailSelection];
  624. }
  625. func displayFindViewAnimating(_ animate: Bool) {
  626. self.replaceSideView(self.searchViewController.view, animate: animate)
  627. if (self.findState != .content) {
  628. self.findState = .content
  629. } else {
  630. self.displayFindState()
  631. }
  632. var frame = self.searchViewController.view.frame
  633. frame.origin.y = 0
  634. frame.size.height = self.searchViewController.view.superview?.frame.size.height ?? .zero
  635. self.searchViewController.view.frame = frame
  636. frame = self.noteOutlineView.enclosingScrollView?.frame ?? .zero
  637. frame.origin.y = 0
  638. frame.size.height = self.noteOutlineView.enclosingScrollView?.superview?.frame.size.height ?? 0
  639. self.noteOutlineView.enclosingScrollView?.frame = frame
  640. frame = self.snapshotTableView.enclosingScrollView?.frame ?? .zero
  641. frame.origin.y = 0
  642. frame.size.height = self.snapshotTableView.enclosingScrollView?.superview?.frame.size.height ?? 0
  643. self.snapshotTableView.enclosingScrollView?.frame = frame
  644. self.leftSideEmptyVC.emptySnapView.removeFromSuperview()
  645. self.updataLeftSideSnapView()
  646. }
  647. func displayFindState() {
  648. if (self.findState == .content) {
  649. self.displayFind()
  650. } else if (self.findState == .note) {
  651. self.displayNoteFind()
  652. } else if (self.findState == .snapshot) {
  653. self.displaySnapshotFind()
  654. }
  655. }
  656. func updataLeftSideSnapView() {
  657. if (snapshots.count < 1) {
  658. self.snapshotNormalSearchButton.isEnabled = false
  659. // leftSideController.snapshotSearchZoomOutButton.enabled = NO;
  660. self.snapshotNormalZoomOutButton.isEnabled = false
  661. // leftSideController.snapshotSearchZoomInButton.enabled = NO;
  662. self.snapshotNormalZoomInButton.isEnabled = false
  663. self.snapshotNormalMoreButton.isEnabled = false
  664. self.leftSideEmptyVC.deleteSnapBtn.isEnabled = false
  665. self.leftSideEmptyVC.exportSnapBtn.isEnabled = false
  666. self.leftSideEmptyVC.printSnapBtn.isEnabled = false
  667. self.snapshotTableView.usesAlternatingRowBackgroundColors = false
  668. let view = self.snapshotTableView.enclosingScrollView!
  669. let emptyVcSize = self.leftSideEmptyVC.emptySnapView.frame.size
  670. self.leftSideEmptyVC.emptySnapView.frame = NSMakeRect((view.frame.size.width-emptyVcSize.width)/2.0,(view.frame.size.height-emptyVcSize.height)/2.0, emptyVcSize.width, emptyVcSize.height);
  671. self.leftSideEmptyVC.emptySnapView.autoresizingMask = [.minXMargin, .maxXMargin, .minYMargin, .maxYMargin]
  672. self.snapshotTableView.enclosingScrollView?.documentView?.addSubview(self.leftSideEmptyVC.emptySnapView)
  673. } else {
  674. self.snapshotNormalSearchButton.isEnabled = true
  675. // leftSideController.snapshotSearchZoomOutButton.enabled = YES;
  676. self.snapshotNormalZoomOutButton.isEnabled = true
  677. // leftSideController.snapshotSearchZoomInButton.enabled = YES;
  678. self.snapshotNormalZoomInButton.isEnabled = true
  679. self.snapshotNormalMoreButton.isEnabled = true
  680. self.leftSideEmptyVC.emptySnapView.removeFromSuperview()
  681. self.leftSideEmptyVC.deleteSnapBtn.isEnabled = true
  682. self.leftSideEmptyVC.exportSnapBtn.isEnabled = true
  683. self.leftSideEmptyVC.printSnapBtn.isEnabled = true
  684. }
  685. }
  686. func updataLeftSideFindView() {
  687. if (self.findState != .content) {
  688. return
  689. }
  690. if (self.searchResults.count > 0) {
  691. self.searchViewController.emptyBox.isHidden = true
  692. self.searchViewController.searchResultsView.isHidden = false
  693. self.searchViewController.searchResultsLabel.stringValue = String(format: KMLocalizedString("%ld Results", "Message in search table header"), self.searchResults.count)
  694. } else {
  695. self.searchViewController.emptyBox.isHidden = false
  696. self.searchViewController.searchResultsView.isHidden = true
  697. }
  698. }
  699. func displayGroupedFindViewAnimating(_ animate: Bool) {
  700. self.replaceSideView(self.searchViewController.view , animate: animate)
  701. if (self.findState != .content) {
  702. self.findState = .content
  703. } else {
  704. self.displayFindState()
  705. }
  706. var frame = self.searchViewController.view.frame
  707. frame.origin.y = 0
  708. frame.size.height = self.searchViewController.view.superview?.frame.size.height ?? 0
  709. self.searchViewController.view.frame = frame
  710. frame = self.noteOutlineView.enclosingScrollView?.frame ?? .zero
  711. frame.origin.y = 0
  712. frame.size.height = self.noteOutlineView.enclosingScrollView?.superview?.frame.size.height ?? 0
  713. self.noteOutlineView.enclosingScrollView?.frame = frame
  714. frame = self.snapshotTableView.enclosingScrollView?.frame ?? .zero
  715. frame.origin.y = 0
  716. frame.size.height = self.snapshotTableView.enclosingScrollView?.superview?.frame.size.height ?? 0
  717. self.snapshotTableView.enclosingScrollView?.frame = frame
  718. self.updataLeftSideSnapView()
  719. }
  720. func displayNoteViewAnimating(_ animate: Bool) {
  721. self.reloadAnnotation()
  722. self.annotationSort(sortArray: [])
  723. self.searchViewController.contentView = nil
  724. self.replaceSideView(self.noteOutlineView.enclosingScrollView!, animate: animate)
  725. if (self.findState != .note) {
  726. self.findState = .note
  727. } else {
  728. self.displayFindState()
  729. }
  730. var frame = self.noteOutlineView.enclosingScrollView?.frame ?? .zero
  731. frame.origin.y = 0
  732. frame.size.height = self.noteOutlineView.enclosingScrollView?.superview?.frame.size.height ?? 0
  733. self.noteOutlineView.enclosingScrollView?.frame = frame
  734. frame = self.noteOutlineView.enclosingScrollView?.frame ?? .zero
  735. frame.origin.y = 0
  736. frame.size.height = self.noteOutlineView.enclosingScrollView?.superview?.frame.size.height ?? 0
  737. self.noteOutlineView.enclosingScrollView?.frame = frame
  738. frame = self.snapshotTableView.enclosingScrollView?.frame ?? .zero
  739. frame.origin.y = 0
  740. frame.size.height = self.snapshotTableView.enclosingScrollView?.superview?.frame.size.height ?? 0
  741. self.snapshotTableView.enclosingScrollView?.frame = frame
  742. let view = self.noteOutlineView.enclosingScrollView!
  743. let emptyVcSize = self.leftSideEmptyVC.emptyAnnotationView.frame.size
  744. self.leftSideEmptyVC.emptyAnnotationView.frame = NSMakeRect((view.frame.size.width-emptyVcSize.width)/2.0,(view.frame.size.height-emptyVcSize.height)/2.0, emptyVcSize.width, emptyVcSize.height)
  745. DispatchQueue.main.async {
  746. self.noteOutlineView.reloadData()
  747. }
  748. }
  749. func displaySnapshotViewAnimating(_ animate: Bool) {
  750. self.searchViewController.contentView = nil
  751. self.replaceSideView(self.snapshotTableView.enclosingScrollView!, animate: animate)
  752. if (self.findState != .snapshot) {
  753. self.findState = .snapshot
  754. } else {
  755. self.displayFindState()
  756. }
  757. var frame = self.snapshotTableView.enclosingScrollView?.frame ?? .zero
  758. frame.origin.y = 0
  759. frame.size.height = self.snapshotTableView.enclosingScrollView?.superview?.frame.size.height ?? 0
  760. self.snapshotTableView.enclosingScrollView?.frame = frame
  761. // frame = rightSideController.noteOutlineView.enclosingScrollView.frame;
  762. // frame.origin.y = 0;
  763. // frame.size.height = rightSideController.noteOutlineView.enclosingScrollView.superview.frame.size.height;
  764. // rightSideController.noteOutlineView.enclosingScrollView.frame = frame;
  765. frame = self.tocOutlineView.enclosingScrollView?.frame ?? .zero
  766. frame.origin.y = 0
  767. frame.size.height = self.tocOutlineView.enclosingScrollView?.superview?.frame.size.height ?? 0
  768. self.tocOutlineView.enclosingScrollView?.frame = frame
  769. // [self updateSnapshotsIfNeeded];
  770. Task { @MainActor in
  771. self.snapshotTableView.reloadData()
  772. }
  773. }
  774. func displayFind() {
  775. self.searchField = self.searchViewController.searchField
  776. let menu = NSMenu()
  777. _ = menu.addItem(title: KMLocalizedString("Whole Words Only", "Menu item title"), action: #selector(toggleWholeWordSearch), target: self)
  778. _ = menu.addItem(title: KMLocalizedString("Ignore Case", "Menu item title"), action: #selector(toggleCaseInsensitiveSearch), target: self)
  779. (self.searchViewController.searchField.cell as? NSSearchFieldCell)?.searchMenuTemplate = menu
  780. (self.searchViewController.searchField.cell as? NSSearchFieldCell)?.placeholderString = KMLocalizedString("Search PDF", "placeholder")
  781. self.searchViewController.searchField.action = #selector(search)
  782. self.searchViewController.searchField.target = self
  783. if (self.findPaneState == .singular) {
  784. self.searchViewController.contentView = self.findTableView.enclosingScrollView
  785. self.findTableView.wantsLayer = true
  786. self.findTableView.layer?.backgroundColor = NSColor.red.cgColor
  787. DispatchQueue.main.async {
  788. self.findTableView.reloadData()
  789. }
  790. } else if (self.findPaneState == .grouped) {
  791. self.searchViewController.contentView = self.groupedFindTableView.enclosingScrollView
  792. var array = KMSearchMode.sortSearchResult(results: self.searchResults)
  793. array.sort(){$0.datas.count > $1.datas.count}
  794. self.groupSearchResults = array
  795. self.groupedFindTableView.reloadData()
  796. }
  797. }
  798. func displayTocViewAnimating(_ animate: Bool) {
  799. self.replaceSideView(self.tocOutlineView.enclosingScrollView!, animate: animate)
  800. var frame = self.tocOutlineView.enclosingScrollView?.frame ?? .zero
  801. frame.origin.y = 0
  802. frame.size.height = self.tocOutlineView.enclosingScrollView?.superview?.frame.size.height ?? 0
  803. self.tocOutlineView.enclosingScrollView?.frame = frame
  804. frame = self.noteOutlineView.enclosingScrollView?.frame ?? .zero
  805. frame.origin.y = 0
  806. frame.size.height = self.noteOutlineView.enclosingScrollView?.superview?.frame.size.height ?? 0
  807. self.noteOutlineView.enclosingScrollView?.frame = frame
  808. frame = self.snapshotTableView.enclosingScrollView?.frame ?? .zero
  809. frame.origin.y = 0
  810. frame.size.height = self.snapshotTableView.enclosingScrollView?.superview?.frame.size.height ?? 0
  811. self.snapshotTableView.enclosingScrollView?.frame = frame
  812. let view = self.tocOutlineView.enclosingScrollView!
  813. let emptyVcSize = self.leftSideEmptyVC.emptyOutlineView.frame.size
  814. self.leftSideEmptyVC.emptyOutlineView.frame = NSMakeRect((view.frame.size.width-emptyVcSize.width)/2.0,(view.frame.size.height-emptyVcSize.height)/2.0, emptyVcSize.width, emptyVcSize.height);
  815. DispatchQueue.main.async {
  816. self.tocOutlineView.reloadData()
  817. self.updateOutlineSelection()
  818. }
  819. }
  820. override func requiresAlternateButton(forView aview: NSView?) -> Bool {
  821. return false
  822. }
  823. func displayNoteFind() {
  824. self.searchField = self.noteSearchField
  825. let menu = NSMenu()
  826. _ = menu.addItem(title: KMLocalizedString("Ignore Case", "Menu item title"), action: #selector(toggleCaseInsensitiveNoteSearch), target: self)
  827. (self.noteSearchField.cell as? NSSearchFieldCell)?.searchMenuTemplate = menu
  828. (self.noteSearchField.cell as? NSSearchFieldCell)?.placeholderString = KMLocalizedString("Search Notes", "placeholder")
  829. self.noteSearchField.action = #selector(searchNotes)
  830. self.noteSearchField.target = self
  831. }
  832. func displaySnapshotFind() {
  833. self.searchField = self.snapshotSearchField;
  834. let menu = NSMenu()
  835. _ = menu.addItem(title: KMLocalizedString("Ignore Case", "Menu item title"), action: #selector(toggleCaseInsensitiveNoteSearch), target: self)
  836. (self.snapshotSearchField.cell as? NSSearchFieldCell)?.searchMenuTemplate = menu
  837. (self.snapshotSearchField.cell as? NSSearchFieldCell)?.placeholderString = KMLocalizedString("Search Snapshots", "placeholder")
  838. self.snapshotSearchField.action = #selector(searchNotes)
  839. self.snapshotSearchField.target = self
  840. self.snapshotSearchField.delegate = self
  841. }
  842. /*
  843. - (void)applySearchTableHeader:(NSString *)message {
  844. [[[findTableView tableColumnWithIdentifier:RESULTS_COLUMNID] headerCell] setStringValue:message];
  845. [[findTableView headerView] setNeedsDisplay:YES];
  846. [[[groupedFindTableView tableColumnWithIdentifier:RELEVANCE_COLUMNID] headerCell] setStringValue:message];
  847. [[groupedFindTableView headerView] setNeedsDisplay:YES];
  848. }
  849. */
  850. @IBAction func search(_ sender: NSSearchField) {
  851. self.delegate?.searchAction?(searchString: sender.stringValue, isCase: self.mwcFlags.caseInsensitiveSearch == 1)
  852. }
  853. @IBAction func snapshotNormalSearchButtonAction(_ sender: NSButton) {
  854. self.snapshotSearchField.isHidden = false
  855. // _snapshotSearchZoomOutButton.hidden = YES;
  856. self.snapshotSearchZoomInButton.isHidden = true
  857. self.snapshotNormalSearchButton.isHidden = true
  858. self.snapshotDoneButton.isHidden = false
  859. self.snapshotLabel.isHidden = true
  860. self.snapshotNormalZoomOutButton.isHidden = true
  861. self.snapshotNormalZoomInButton.isHidden = true
  862. self.snapshotSearchField.becomeFirstResponder()
  863. }
  864. func resetThumbnails() {
  865. // [self willChangeValueForKey:THUMBNAILS_KEY];
  866. // self.thumbnailCacheSize = 160
  867. self.thumbnails.removeAll()
  868. let pageLabels = self.listView.document.pageLabels()
  869. if (pageLabels.isEmpty == false) {
  870. let isLocked = self.listView.document.isLocked
  871. let firstPage = self.listView.document.page(at: 0)
  872. let emptyPage = CPDFPage()
  873. let firstFrame = firstPage?.bounds(for: .cropBox) ?? .zero
  874. let firstFrame2 = firstPage?.bounds(for: .mediaBox) ?? .zero
  875. emptyPage.setBounds(firstFrame, for: .cropBox)
  876. emptyPage.setBounds(firstFrame2, for: .mediaBox)
  877. emptyPage.rotation = firstPage?.rotation ?? 0
  878. let pageImage = firstPage!.thumbnail(of: NSMakeSize(self.thumbnailCacheSize, self.thumbnailCacheSize))
  879. // NSImage * = [emptyPage thumbnailWithSize:thumbnailCacheSize forBox:[pdfView displayBox]];
  880. var rect: NSRect = .zero
  881. rect.size = pageImage?.size ?? .zero
  882. let width = 1.2 * fmin(NSWidth(rect), NSHeight(rect))
  883. rect = NSInsetRect(rect, 0.5 * (NSWidth(rect) - width), 0.5 * (NSHeight(rect) - width));
  884. pageImage?.lockFocus()
  885. // NSImage(named: NSImage.applicationIconName)?.draw(in: rect, from: .zero, operation: .sourceOver, fraction: 0.5)
  886. if (isLocked) {
  887. NSWorkspace.shared.icon(forFileType: NSFileTypeForHFSTypeCode(OSType(kLockedBadgeIcon))).draw(in: rect, from: .zero, operation: .sourceOver, fraction: 0.5)
  888. // [[[NSWorkspace sharedWorkspace] iconForFileType:NSFileTypeForHFSTypeCode(kLockedBadgeIcon)] drawInRect:rect fromRect:NSZeroRect operation:NSCompositingOperationSourceOver fraction:0.5];
  889. }
  890. pageImage?.unlockFocus()
  891. for (i, label) in pageLabels.enumerated() {
  892. let firstPage = self.listView.document.page(at: UInt(i))
  893. let size = NSMakeSize(self.thumbnailCacheSize, self.thumbnailCacheSize)
  894. // let pageImage = firstPage!.thumbnail(of: size)
  895. let thumbnail = KMThumbnail(image: nil, label: label, pageIndex: i)
  896. // thumbnail.delegate = self
  897. thumbnail.dirty = true
  898. self.thumbnails.append(thumbnail)
  899. }
  900. }
  901. // [self didChangeValueForKey:THUMBNAILS_KEY];
  902. // [self allThumbnailsNeedUpdate];
  903. DispatchQueue.main.async {
  904. self.thumbnailTableView.reloadData()
  905. }
  906. }
  907. @IBAction @objc func sortTypeAction(_ sender: NSMenuItem) {
  908. let item = sender
  909. let tag = item.tag
  910. if (item.state == .on) {
  911. item.state = .off
  912. } else {
  913. item.state = .on
  914. }
  915. if (tag == 0) {
  916. self.noteSortType = .page
  917. self.sortTypeLabel.stringValue = KMLocalizedString("Page", nil)
  918. self.sortTypeBox.toolTip = KMLocalizedString("Page", nil)
  919. } else if (tag == 1) {
  920. self.noteSortType = .time
  921. self.sortTypeLabel.stringValue = KMLocalizedString("Time", nil)
  922. self.sortTypeBox.toolTip = KMLocalizedString("Time", nil)
  923. }
  924. UserDefaults.standard.set(self.noteSortType.rawValue, forKey: KMLeftSideViewNoteSortTypeKey)
  925. UserDefaults.standard.synchronize()
  926. self.annotationSort(sortArray: [])
  927. }
  928. @IBAction func noteSortAction(_ sender: AnyObject?) {
  929. if (self.isAscendSort) {
  930. self.isAscendSort = false
  931. self.noteSortButton.image = NSImage(named: KMImageNameBtnSidebarRankPositive)
  932. self.noteSortButton.toolTip = KMLocalizedString("descending sort", nil)
  933. } else {
  934. self.isAscendSort = true
  935. self.noteSortButton.image = NSImage(named: KMImageNameBtnSidebarRankReverse)
  936. self.noteSortButton.toolTip = KMLocalizedString("ascending sort", nil)
  937. }
  938. UserDefaults.standard.setValue(self.isAscendSort, forKey: KMLeftSideViewAscendSortBoolKey)
  939. UserDefaults.standard.synchronize()
  940. self.annotationSort(sortArray: [])
  941. }
  942. @IBAction func noteSearchAction(_ sender: NSButton) {
  943. self.noteSearchField.isHidden = false
  944. self.noteTitleLabel.isHidden = true
  945. self.noteSearchButton.isHidden = true
  946. self.noteDoneButton.isHidden = false
  947. self.noteFilterButton.isHidden = true
  948. self.noteMoreButton.isHidden = true
  949. self.noteSearchField.becomeFirstResponder()
  950. }
  951. @IBAction func leftSideViewDoneButtonAction(_ sender: AnyObject?) {
  952. let button = sender as? NSButton
  953. let tag = button?.tag ?? 0
  954. if (tag == 310) {
  955. self.outlineSearchField.isHidden = true
  956. self.outlineDoneButton.isHidden = true
  957. self.outlineLabel.isHidden = false
  958. self.outlineSearchButton.isHidden = false
  959. self.outlineMoreButton.isHidden = false
  960. self.outlineAddButton.isHidden = false
  961. } else if (tag == 311) {
  962. self.noteSearchField.isHidden = true
  963. self.noteTitleLabel.isHidden = false
  964. self.noteSearchButton.isHidden = false
  965. self.noteDoneButton.isHidden = true
  966. self.noteFilterButton.isHidden = false
  967. self.noteMoreButton.isHidden = false
  968. } else if (tag == 312) {
  969. self.snapshotSearchField.isHidden = true
  970. // leftSideController.snapshotSearchZoomOutButton.hidden = YES;
  971. // leftSideController.snapshotSearchZoomInButton.hidden = YES;
  972. self.snapshotNormalSearchButton.isHidden = false
  973. self.snapshotDoneButton.isHidden = true
  974. self.snapshotLabel.isHidden = false
  975. self.snapshotNormalZoomOutButton.isHidden = false
  976. self.snapshotNormalZoomInButton.isHidden = false
  977. }
  978. }
  979. @IBAction func leftSideViewMoreButtonAction(_ sender: AnyObject?) {
  980. let button = sender as? NSButton
  981. let tag = button?.tag ?? 0
  982. if (tag == 300) {
  983. var selectedRow = 0
  984. if (self.snapshotTableView.selectedRow >= 0) {
  985. selectedRow = self.snapshotTableView.selectedRow;
  986. } else {
  987. return
  988. }
  989. let model = self.snapshots[selectedRow]
  990. let controller = model.windowC
  991. let menu = NSMenu()
  992. let itemExport = menu.addItem(title: KMLocalizedString("Export", "Menu item title"), action: nil, target: self)
  993. let subMenu = NSMenu()
  994. var t = subMenu.addItem(title: KMLocalizedString("PNG", "Menu item title"), action: #selector(menuItemClick_ExportPNG), target: self)
  995. t?.representedObject = controller
  996. t = subMenu.addItem(title: KMLocalizedString("JPG", "Menu item title"), action: #selector(menuItemClick_ExportJPG), target: self)
  997. t?.representedObject = controller
  998. t = subMenu.addItem(title: KMLocalizedString("PDF", "Menu item title"), action: #selector(menuItemClick_ExportPDF), target: self)
  999. t?.representedObject = controller
  1000. itemExport?.submenu = subMenu
  1001. let itemPrint = menu.addItem(title: KMLocalizedString("Print", "Menu item title"), action: #selector(menuItemClick_Print), target: self)
  1002. itemPrint?.representedObject = controller
  1003. menu.addItem(.separator())
  1004. let itemSelectAll = menu.addItem(title: KMLocalizedString("Select All", "Menu item title"), action: #selector(menuItemClick_SelectAll), target: self)
  1005. itemSelectAll?.representedObject = controller
  1006. menu.addItem(.separator())
  1007. let itemDeleteAllSnapshot = menu.addItem(title: KMLocalizedString("Delete All Snapshots", "Menu item title"), action: #selector(deleteAllSnapshot), target: self)
  1008. itemDeleteAllSnapshot?.representedObject = controller
  1009. //
  1010. NSMenu.popUpContextMenu(menu, with: NSApp.currentEvent!, for: sender as! NSButton)
  1011. } else if (tag == 302) {
  1012. let menu = NSMenu()
  1013. let expandAllCommentsItem = menu.addItem(title: KMLocalizedString("Expand All", nil), action: #selector(toc_expandAllComments), target: self)
  1014. expandAllCommentsItem?.representedObject = self.tocOutlineView
  1015. let foldAllCommentsItem = menu.addItem(title: KMLocalizedString("Collapse All", nil), action: #selector(toc_foldAllComments), target: self)
  1016. expandAllCommentsItem?.representedObject = self.tocOutlineView
  1017. let item = self.listView.document.outlineRoot()
  1018. var num = 0
  1019. for i in 0 ..< Int(item?.numberOfChildren ?? 0) {
  1020. let outline = item?.child(at: UInt(i))
  1021. if self.tocOutlineView.isItemExpanded(outline) {
  1022. num += 1
  1023. }
  1024. }
  1025. if let cnt = item?.numberOfChildren, cnt > 0 && num == 0 {
  1026. self.tocType = .fold
  1027. } else if let cnt = item?.numberOfChildren, cnt > 0 && num == cnt {
  1028. self.tocType = .unfold
  1029. } else {
  1030. self.tocType = .none
  1031. }
  1032. if (self.tocType == .unfold) {
  1033. expandAllCommentsItem?.state = .on
  1034. foldAllCommentsItem?.state = .off
  1035. } else if (self.tocType == .fold) {
  1036. expandAllCommentsItem?.state = .off
  1037. foldAllCommentsItem?.state = .on
  1038. } else {
  1039. expandAllCommentsItem?.state = .off
  1040. foldAllCommentsItem?.state = .off
  1041. }
  1042. let removeEntryItem = menu.addItem(title: KMLocalizedString("Remove All Outlines", nil), action: #selector(leftSideEmptyAnnotationClick_DeleteOutline), target: self)
  1043. removeEntryItem?.representedObject = self.tocOutlineView
  1044. NSMenu.popUpContextMenu(menu, with: NSApp.currentEvent!, for: sender as! NSButton)
  1045. } else if (tag == 304) {
  1046. let menu = NSMenu()
  1047. let object = KMPopupMenuObject()
  1048. object.menuTag = 1001
  1049. menu.delegate = object
  1050. object.enterControllerCallback = { [weak self] isEnter in
  1051. if (isEnter) {
  1052. self?.moreButtonLayer?.isHidden = false
  1053. } else {
  1054. self?.moreButtonLayer?.isHidden = true
  1055. }
  1056. }
  1057. let expandAllCommentsItem = menu.addItem(title: KMLocalizedString("Expand All", nil), action: #selector(note_expandAllComments), target: self)
  1058. expandAllCommentsItem?.representedObject = self.noteOutlineView
  1059. let foldAllCommentsItem = menu.addItem(title: KMLocalizedString("Collapse All", nil), action: #selector(note_foldAllComments), target: self)
  1060. foldAllCommentsItem?.representedObject = self.noteOutlineView
  1061. if (self.foldType == .unfold) {
  1062. expandAllCommentsItem?.state = .on
  1063. foldAllCommentsItem?.state = .off
  1064. } else if (self.foldType == .fold) {
  1065. expandAllCommentsItem?.state = .off
  1066. foldAllCommentsItem?.state = .on
  1067. } else {
  1068. expandAllCommentsItem?.state = .off
  1069. foldAllCommentsItem?.state = .off
  1070. }
  1071. let showAnnotationItem = menu.addItem(title: KMLocalizedString("Show Note", nil), action: nil, target: self)
  1072. let subMenu = NSMenu()
  1073. var t = subMenu.addItem(title: KMLocalizedString("Page", nil), action: #selector(noteShowNoteAction), target: self)
  1074. let pageKey = self.noteTypeDict["kKMNoteFilterAnnotationPageKey"] as? Bool ?? false
  1075. if pageKey {
  1076. t?.state = .off
  1077. } else {
  1078. t?.state = .on
  1079. }
  1080. t?.representedObject = self.noteOutlineView
  1081. t?.tag = 101
  1082. t = subMenu.addItem(title: KMLocalizedString("Time", nil) , action: #selector(noteShowNoteAction), target: self)
  1083. let timeKey = self.noteTypeDict["kKMNoteFilterAnnotationTimeKey"] as? Bool ?? false
  1084. if timeKey {
  1085. t?.state = .off
  1086. } else {
  1087. t?.state = .on
  1088. }
  1089. t?.representedObject = self.noteOutlineView
  1090. t?.tag = 102
  1091. t = subMenu.addItem(title: KMLocalizedString("Author", nil) , action: #selector(noteShowNoteAction), target: self)
  1092. let authorKey = self.noteTypeDict["kKMNoteFilterAnnotationAutherKey"] as? Bool ?? false
  1093. if authorKey {
  1094. t?.state = .off
  1095. } else {
  1096. t?.state = .on
  1097. }
  1098. t?.representedObject = self.noteOutlineView
  1099. t?.tag = 103
  1100. showAnnotationItem?.submenu = subMenu
  1101. menu.addItem(.separator())
  1102. let exportAnnotationsItem = menu.addItem(title: NSLocalizedString("Export Annotations…", tableName: "MainMenu", comment: ""), action: nil, target: self)
  1103. let subMenu2 = NSMenu()
  1104. var t2 = subMenu2.addItem(title: NSLocalizedString("PDF", tableName: "MainMenu", comment: "Menu item title"), action: #selector(exportAnnotationNotes), target: self)
  1105. t2?.representedObject = self.noteOutlineView
  1106. t2?.tag = 0
  1107. t2 = subMenu2.addItem(title: NSLocalizedString("PDF Bundle", tableName: "MainMenu", comment: "Menu item title"), action: #selector(exportAnnotationNotes), target: self)
  1108. t2?.representedObject = self.noteOutlineView
  1109. t2?.tag = 1
  1110. t2 = subMenu2.addItem(title: NSLocalizedString("PDF Reader Pro Edition Notes", tableName: "MainMenu", comment: "Menu item title"), action: #selector(exportAnnotationNotes), target: self)
  1111. t2?.representedObject = self.noteOutlineView
  1112. t2?.tag = 2
  1113. t2 = subMenu2.addItem(title: NSLocalizedString("Notes as Text", tableName: "MainMenu", comment: "Menu item title"), action: #selector(exportAnnotationNotes), target: self)
  1114. t2?.representedObject = self.noteOutlineView
  1115. t2?.tag = 3
  1116. t2 = subMenu2.addItem(title: NSLocalizedString("Notes as RTF", tableName: "MainMenu", comment: "Menu item title"), action: #selector(exportAnnotationNotes), target: self)
  1117. t2?.representedObject = self.noteOutlineView
  1118. t2?.tag = 4
  1119. t2 = subMenu2.addItem(title: NSLocalizedString("Notes as RTFD", tableName: "MainMenu", comment: "Menu item title"), action: #selector(exportAnnotationNotes), target: self)
  1120. t2?.representedObject = self.noteOutlineView
  1121. t2?.tag = 5
  1122. t2 = subMenu2.addItem(title: NSLocalizedString("Notes as FDF", tableName: "MainMenu", comment: "Menu item title"), action: #selector(exportAnnotationNotes), target: self)
  1123. t2?.representedObject = self.noteOutlineView
  1124. t2?.tag = 6
  1125. exportAnnotationsItem?.submenu = subMenu2
  1126. menu.addItem(.separator())
  1127. let removeAllAnnotationsItem = menu.addItem(title: NSLocalizedString("Remove All Annotations", tableName: "MainMenu", comment: ""), action: #selector(leftSideEmptyAnnotationClick_DeleteAnnotation), target: self)
  1128. removeAllAnnotationsItem?.representedObject = self.noteOutlineView
  1129. NSMenu.popUpContextMenu(menu, with: NSApp.currentEvent!, for: button!, with: nil)
  1130. }
  1131. }
  1132. @IBAction func noteFilterAction(_ sender: AnyObject?) {
  1133. let button = sender as? NSButton
  1134. let menu = NSMenu()
  1135. let filterViewController = KMNoteOutlineFilterViewController()
  1136. filterViewController.listView = self.listView
  1137. filterViewController.view.layer?.backgroundColor = .clear
  1138. var notes = NSMutableArray()
  1139. // for section in self._annotations {
  1140. // if section.annotations?.count != 0 {
  1141. // for item in section.annotations! {
  1142. // notes.add(item.annotation!)
  1143. // }
  1144. // }
  1145. // }
  1146. filterViewController.setNotesArray(self.allAnnotations as NSArray)
  1147. filterViewController.applyFilterCallback = { [weak self] typeArr, colorArr, authorArr, isEmpty in
  1148. menu.cancelTracking()
  1149. if (isEmpty) {
  1150. self?.filterButtonLayer?.isHidden = true
  1151. } else {
  1152. self?.filterButtonLayer?.isHidden = false
  1153. }
  1154. self?.annotationSort(sortArray: [])
  1155. }
  1156. filterViewController.cancelCallback = { isCancel in
  1157. if (isCancel) {
  1158. menu.cancelTracking()
  1159. }
  1160. }
  1161. let item = menu.addItem(withTitle: "", action: nil, keyEquivalent: "")
  1162. item.target = self
  1163. item.representedObject = filterViewController
  1164. item.view = filterViewController.view
  1165. menu.popUp(positioning: nil, at: CGPointMake(-130, 30), in: button)
  1166. }
  1167. @IBAction func outlineNormalSearchButtonAction(_ sender: NSButton) {
  1168. self.outlineSearchField.isHidden = false
  1169. self.outlineDoneButton.isHidden = false
  1170. self.outlineLabel.isHidden = true
  1171. self.outlineSearchButton.isHidden = true
  1172. self.outlineMoreButton.isHidden = true
  1173. self.outlineAddButton.isHidden = true
  1174. self.outlineSearchField.becomeFirstResponder()
  1175. }
  1176. @IBAction func toc_expandAllComments(_ sender: AnyObject?) {
  1177. if (self.tocType == .unfold) {
  1178. return
  1179. }
  1180. self.tocType = .unfold
  1181. self.tocOutlineView.reloadData()
  1182. self.tocOutlineView.expandItem(nil, expandChildren: true)
  1183. }
  1184. @IBAction func toc_foldAllComments(_ sender: AnyObject?) {
  1185. if (self.tocType == .fold) {
  1186. return
  1187. }
  1188. self.tocType = .fold
  1189. self.tocOutlineView.reloadData()
  1190. self.tocOutlineView.collapseItem(nil, collapseChildren: true)
  1191. }
  1192. @objc func leftSideEmptyAnnotationClick_DeleteOutline(_ sender: AnyObject?) {
  1193. let alert = NSAlert()
  1194. alert.alertStyle = .critical
  1195. alert.messageText = ""
  1196. alert.informativeText = KMLocalizedString("This will permanently remove all outlines. Are you sure to continue?", nil)
  1197. alert.addButton(withTitle: KMLocalizedString("Yes", nil))
  1198. alert.addButton(withTitle: KMLocalizedString("No", nil))
  1199. let response = alert.runModal()
  1200. if response == .alertFirstButtonReturn {
  1201. if let item = self.listView.document.outlineRoot() {
  1202. self.removeAllOutline(item)
  1203. }
  1204. }
  1205. }
  1206. @IBAction func thumbnailSizeScaling(_ sender: NSButton) {
  1207. let sud = UserDefaults.standard
  1208. let tag = sender.tag
  1209. if (tag == 0 || tag == 1) {
  1210. var scaling = sud.float(forKey: "KMThumbnailSizeScalingKey")
  1211. if (scaling <= 0) {
  1212. scaling = 1
  1213. }
  1214. if (tag == 0) { // thumbnail Zoom In
  1215. scaling += 0.1
  1216. if scaling >= 2.2 {
  1217. return
  1218. }
  1219. } else if (tag == 1) { // thumbnail Zoom Out
  1220. scaling -= 0.1
  1221. if scaling <= 0.4 {
  1222. return
  1223. }
  1224. }
  1225. sud.setValue(scaling, forKey: "KMThumbnailSizeScalingKey")
  1226. let selectRow = self.thumbnailTableView.selectedRow
  1227. self.thumbnailTableView.reloadData()
  1228. self.thumbnailTableView.selectRowIndexes(IndexSet(integer: selectRow), byExtendingSelection: false)
  1229. } else if (tag == 2 || tag == 3) {
  1230. var scaling = sud.float(forKey: "KMSnapshotSizeScalingKey")
  1231. if (scaling <= 0) {
  1232. scaling = 1
  1233. }
  1234. if (tag == 2) { // snapshot Zoom In
  1235. scaling += 0.1
  1236. } else if (tag == 3) { // snapshot Zoom Out
  1237. scaling -= 0.1
  1238. }
  1239. sud.setValue(scaling, forKey: "KMSnapshotSizeScalingKey")
  1240. let selectRow = self.snapshotTableView.selectedRow
  1241. self.snapshotTableView.reloadData()
  1242. self.snapshotTableView.selectRowIndexes(IndexSet(integer: selectRow), byExtendingSelection: false)
  1243. }
  1244. }
  1245. func reloadAnnotation() {
  1246. if self.listView != nil {
  1247. var dataArray: [KMBOTAAnnotationSection] = []
  1248. var annotationArray: [CPDFAnnotation] = []
  1249. var allAnnotation: [CPDFAnnotation] = []
  1250. for i in 0 ..< self.listView.document.pageCount {
  1251. var annotationItemArray: [KMBOTAAnnotationItem] = []
  1252. let page = self.listView.document.page(at: i)
  1253. let types = ["Highlight","Underline","Strikeout","Freehand","FreeText","Note","Square","Circle","Line","Stamp","Arrow","Image","Redact","Sign"]
  1254. var pageAnnotations: [CPDFAnnotation] = KMOCToolClass.filterAnnotation(annotations: page!.annotations,types: types) as! [CPDFAnnotation]
  1255. //添加签名注释
  1256. for annotation in page!.annotations {
  1257. if annotation.isKind(of: CPDFSignatureAnnotation.self) {
  1258. pageAnnotations.append(annotation)
  1259. }
  1260. }
  1261. for annotation in pageAnnotations {
  1262. if annotation.annotationShouldDisplay() == false {
  1263. pageAnnotations.removeObject(annotation)
  1264. }
  1265. }
  1266. //转换所有annotation类型
  1267. let section = KMBOTAAnnotationSection()
  1268. for annotation in pageAnnotations {
  1269. let item = KMBOTAAnnotationItem()
  1270. item.section = section
  1271. item.annotation = annotation
  1272. item.index = Int(annotation.page.pageIndex())
  1273. annotationItemArray.append(item)
  1274. allAnnotation.append(annotation)
  1275. }
  1276. if annotationItemArray.count != 0 {
  1277. section.annotations = annotationItemArray
  1278. section.page = page
  1279. section.isItemExpanded = true
  1280. dataArray.append(section)
  1281. }
  1282. //添加所有annotation 用于筛选
  1283. annotationArray += pageAnnotations
  1284. }
  1285. if self.noteSortType == .page {
  1286. /// 根据id进行排序(升序)
  1287. if self.isAscendSort {
  1288. dataArray.sort { $0.page!.pageIndex() > $1.page!.pageIndex()}
  1289. } else {
  1290. dataArray.sort { $0.page!.pageIndex() <= $1.page!.pageIndex()}
  1291. }
  1292. self._annotations = dataArray
  1293. } else if self.noteSortType == .time {
  1294. var datas: [KMBOTAAnnotationSection] = []
  1295. /// 根据id进行排序(升序)
  1296. if self.isAscendSort {
  1297. allAnnotation.sort {
  1298. if $0.modificationDate() == nil {
  1299. return false
  1300. }
  1301. if $1.modificationDate() == nil {
  1302. return false
  1303. }
  1304. return $0.modificationDate() > $1.modificationDate()
  1305. }
  1306. } else {
  1307. allAnnotation.sort {
  1308. if $0.modificationDate() == nil {
  1309. return false
  1310. }
  1311. if $1.modificationDate() == nil {
  1312. return false
  1313. }
  1314. return $0.modificationDate() <= $1.modificationDate()
  1315. }
  1316. }
  1317. for anno in allAnnotation {
  1318. let section = KMBOTAAnnotationSection()
  1319. let item = KMBOTAAnnotationItem()
  1320. item.section = section
  1321. item.annotation = anno
  1322. item.index = Int(anno.pageIndex())
  1323. section.annotations = [item]
  1324. section.page = anno.page
  1325. section.isItemExpanded = true
  1326. datas.append(section)
  1327. }
  1328. self._annotations = datas
  1329. }
  1330. //转换对象,用于数据显示
  1331. // self._annotations = dataArray
  1332. self._allAnnotations = annotationArray
  1333. // if self.annotations.count < 1 {
  1334. // self.filtrateButton.isEnabled = false
  1335. // } else {
  1336. // self.filtrateButton.isEnabled = true
  1337. // }
  1338. }
  1339. }
  1340. func annotationSort(sortArray:[[Any]]) {
  1341. if self.listView != nil {
  1342. var typeArr: [Any] = []
  1343. var colorArr: [Any] = []
  1344. var authorArr: [Any] = []
  1345. let sud = UserDefaults.standard
  1346. let typeData = sud.object(forKey: "KMNoteOutlineFilterSelectArray_Type" + self.listView.document.documentURL.path) as? Data
  1347. // let typeData = sud.object(forKey: "KMNoteOutlineFilterSelectArray_Type") as? Data
  1348. if typeData != nil {
  1349. typeArr = NSKeyedUnarchiver.unarchiveObject(with: typeData!) as! [Any]
  1350. }
  1351. let colorData = sud.object(forKey: "KMNoteOutlineFilterSelectArray_Color" + self.listView.document.documentURL.path) as? Data
  1352. // let colorData = sud.object(forKey: "KMNoteOutlineFilterSelectArray_Color") as? Data
  1353. if colorData != nil {
  1354. colorArr = NSKeyedUnarchiver.unarchiveObject(with: colorData!) as! [Any]
  1355. }
  1356. let authorData = sud.object(forKey: "KMNoteOutlineFilterSelectArray_Author" + self.listView.document.documentURL.path) as? Data
  1357. // let authorData = sud.object(forKey: "KMNoteOutlineFilterSelectArray_Author") as? Data
  1358. if authorData != nil {
  1359. authorArr = NSKeyedUnarchiver.unarchiveObject(with: authorData!) as! [Any]
  1360. }
  1361. var colorMutableArray = NSMutableArray()
  1362. var typeMutableArray = NSMutableArray()
  1363. var authorMutableArray = NSMutableArray()
  1364. for annotation in self.allAnnotations {
  1365. if annotation.isKind(of: CPDFInkAnnotation.self) == false &&
  1366. annotation.isKind(of: CPDFTextWidgetAnnotation.self) == false &&
  1367. annotation.isKind(of: CPDFButtonWidgetAnnotation.self) == false &&
  1368. annotation.isKind(of: CPDFChoiceWidgetAnnotation.self) == false
  1369. // ![annotation.widgetFieldType isEqualToString:PDFAnnotationWidgetSubtypeSignature] &&
  1370. // ![annotation.widgetFieldType isEqualToString:PDFAnnotationWidgetSubtypeText] &&
  1371. // ![annotation.widgetFieldType isEqualToString:PDFAnnotationWidgetSubtypeButton] &&
  1372. // ![annotation.widgetFieldType isEqualToString:PDFAnnotationWidgetSubtypeChoice])
  1373. {
  1374. let noteColor = annotation.color ?? .clear
  1375. let noteType = annotation.type ?? ""
  1376. let authorString = annotation.userName() ?? ""
  1377. if (noteColor != nil) {
  1378. if (colorMutableArray.count > 0) {
  1379. if colorMutableArray.contains(noteColor) == false {
  1380. colorMutableArray.add(noteColor)
  1381. }
  1382. } else {
  1383. colorMutableArray.add(noteColor)
  1384. }
  1385. }
  1386. if noteType.isEmpty == false {
  1387. if (typeMutableArray.count > 0) {
  1388. if typeMutableArray.contains(noteType) == false {
  1389. typeMutableArray.add(noteType)
  1390. }
  1391. } else {
  1392. typeMutableArray.add(noteType)
  1393. }
  1394. }
  1395. if authorString.isEmpty == false {
  1396. if (authorMutableArray.count > 0) {
  1397. if authorMutableArray.contains(authorString) == false {
  1398. authorMutableArray.add(authorString)
  1399. }
  1400. } else {
  1401. authorMutableArray.add(authorString)
  1402. }
  1403. }
  1404. }
  1405. }
  1406. // var temporaryArr1 = NSMutableArray()
  1407. // var temporaryArr2 = NSMutableArray()
  1408. // var temporaryArr3 = NSMutableArray()
  1409. // if typeArr.isEmpty == false {
  1410. // for type in typeArr {
  1411. // if typeMutableArray.contains(type) == false {
  1412. // temporaryArr1.add(type)
  1413. // }
  1414. // }
  1415. // }
  1416. // if colorArr.isEmpty == false {
  1417. // for color in colorArr {
  1418. // if colorMutableArray.contains(color) == false {
  1419. // temporaryArr2.add(type)
  1420. // }
  1421. // }
  1422. // }
  1423. // if authorArr.isEmpty == false {
  1424. // for author in authorArr {
  1425. // if authorMutableArray.contains(author) == false {
  1426. // temporaryArr3.add(type)
  1427. // }
  1428. // }
  1429. // }
  1430. // if (temporaryArr1.count > 0) {
  1431. // for type in temporaryArr1 {
  1432. // guard let typeS = type as? String else {
  1433. // continue
  1434. // }
  1435. //// [typeArr removeObject:type];
  1436. //
  1437. // var flag = -1
  1438. // for (i, data) in typeArr.enumerated() {
  1439. // if data as! String == typeS {
  1440. // flag = i
  1441. // }
  1442. // }
  1443. // if flag != -1 {
  1444. // typeArr.remove(at: flag)
  1445. // }
  1446. // }
  1447. // }
  1448. // if (temporaryArr2.count > 0) {
  1449. // for (NSColor *color in temporaryArr2) {
  1450. // [colorArr removeObject:color];
  1451. // }
  1452. // }
  1453. // for color in temporaryArr2 {
  1454. // guard let colorT = color as? NSColor else {
  1455. // continue
  1456. // }
  1457. //// [typeArr removeObject:type];
  1458. //
  1459. // var flag = -1
  1460. // for (i, data) in colorArr.enumerated() {
  1461. // if data as! NSColor == colorT {
  1462. // flag = i
  1463. // }
  1464. // }
  1465. // if flag != -1 {
  1466. // colorArr.remove(at: flag)
  1467. // }
  1468. // }
  1469. // if (temporaryArr3.count > 0) {
  1470. // for (NSColor *author in temporaryArr3) {
  1471. // [authorArr removeObject:author];
  1472. // }
  1473. // }
  1474. // for author in temporaryArr3 {
  1475. // guard let authorS = author as? String else {
  1476. // continue
  1477. // }
  1478. //// [typeArr removeObject:type];
  1479. //
  1480. // var flag = -1
  1481. // for (i, data) in authorArr.enumerated() {
  1482. // if data as! String == authorS {
  1483. // flag = i
  1484. // }
  1485. // }
  1486. // if flag != -1 {
  1487. // authorArr.remove(at: flag)
  1488. // }
  1489. // }
  1490. // let data1 = NSKeyedArchiver.archivedData(withRootObject: NSArray(array: typeArr))
  1491. // let data2 = NSKeyedArchiver.archivedData(withRootObject: NSArray(array: colorArr))
  1492. // let data3 = NSKeyedArchiver.archivedData(withRootObject: NSArray(array: authorArr))
  1493. // let sud = UserDefaults.standard
  1494. // sud.set(data1, forKey: "KMNoteOutlineFilterSelectArray_Type")
  1495. // sud.set(data2, forKey: "KMNoteOutlineFilterSelectArray_Color")
  1496. // sud.set(data3, forKey: "KMNoteOutlineFilterSelectArray_Author")
  1497. // sud.synchronize()
  1498. if typeArr.count == 0 && colorArr.count == 0 && authorArr.count == 0 {
  1499. // self.filtrateButton.image = NSImage(named: "KMImageNameAnnotationsFiltrate")
  1500. self.reloadAnnotation()
  1501. } else {
  1502. // self.filtrateButton.image = NSImage(named: "icon_annotation_screening_select")
  1503. var dataArray: [KMBOTAAnnotationSection] = []
  1504. var allAnnotation: [CPDFAnnotation] = []
  1505. for i in 0 ..< self.listView.document.pageCount {
  1506. var annotationItemArray: [KMBOTAAnnotationItem] = []
  1507. let page = self.listView.document.page(at: i)
  1508. if page!.annotations.count > 0 {
  1509. var filterAnnotations: [CPDFAnnotation] = page!.annotations
  1510. if typeArr.count > 0 {
  1511. filterAnnotations = (KMOCToolClass.filterAnnotation(annotations: filterAnnotations, types: typeArr) as! [CPDFAnnotation])
  1512. }
  1513. if (colorArr.count > 0) {
  1514. filterAnnotations = (KMOCToolClass.filterAnnotation(annotations: filterAnnotations,colors: colorArr) as! [CPDFAnnotation])
  1515. }
  1516. if (authorArr.count > 0) {
  1517. filterAnnotations = (KMOCToolClass.filterAnnotation(annotations: filterAnnotations,authors: authorArr) as! [CPDFAnnotation])
  1518. }
  1519. let section = KMBOTAAnnotationSection()
  1520. for annotation in filterAnnotations {
  1521. let item = KMBOTAAnnotationItem()
  1522. item.section = section
  1523. item.annotation = annotation
  1524. item.index = Int(page!.pageIndex())
  1525. annotationItemArray.append(item)
  1526. allAnnotation.append(annotation)
  1527. }
  1528. if annotationItemArray.count != 0 {
  1529. section.annotations = annotationItemArray
  1530. section.page = page
  1531. section.isItemExpanded = true
  1532. dataArray.append(section)
  1533. }
  1534. }
  1535. }
  1536. if self.noteSortType == .page {
  1537. /// 根据id进行排序(升序)
  1538. if self.isAscendSort {
  1539. dataArray.sort { $0.page!.pageIndex() > $1.page!.pageIndex()}
  1540. } else {
  1541. dataArray.sort { $0.page!.pageIndex() <= $1.page!.pageIndex()}
  1542. }
  1543. self._annotations = dataArray
  1544. } else if self.noteSortType == .time {
  1545. var datas: [KMBOTAAnnotationSection] = []
  1546. /// 根据id进行排序(升序)
  1547. if self.isAscendSort {
  1548. allAnnotation.sort { $0.modificationDate() > $1.modificationDate() }
  1549. } else {
  1550. allAnnotation.sort { $0.modificationDate() <= $1.modificationDate() }
  1551. }
  1552. for anno in allAnnotation {
  1553. let section = KMBOTAAnnotationSection()
  1554. let item = KMBOTAAnnotationItem()
  1555. item.section = section
  1556. item.annotation = anno
  1557. item.index = Int(anno.pageIndex())
  1558. section.annotations = [item]
  1559. section.page = anno.page
  1560. section.isItemExpanded = true
  1561. datas.append(section)
  1562. }
  1563. self._annotations = datas
  1564. }
  1565. }
  1566. Task { @MainActor in
  1567. self.noteOutlineView.reloadData()
  1568. }
  1569. }
  1570. }
  1571. func showSearchOutlineBlankState(_ toShowState: Bool) {
  1572. if (toShowState) {
  1573. self.leftSideEmptyVC.outlineSearchView.frame = CGRectMake((self.tocOutlineView.enclosingScrollView!.documentView!.frame.size.width - self.leftSideEmptyVC.outlineSearchView.bounds.size.width)/2.0, (self.tocOutlineView.enclosingScrollView!.documentView!.frame.size.height - self.leftSideEmptyVC.outlineSearchView.bounds.size.height)/2.0, self.leftSideEmptyVC.outlineSearchView.bounds.size.width, self.leftSideEmptyVC.outlineSearchView.bounds.size.height);
  1574. self.tocOutlineView.enclosingScrollView?.documentView?.addSubview(self.leftSideEmptyVC.outlineSearchView)
  1575. self.leftSideEmptyVC.outlineSearchView.autoresizingMask = [.minXMargin, .maxXMargin, .minYMargin, .maxYMargin]
  1576. } else {
  1577. self.leftSideEmptyVC.outlineSearchView.removeFromSuperview()
  1578. }
  1579. }
  1580. // func allRangeOfRoughString(_ roughString: String, searchString: String) -> [NSRange] {
  1581. //
  1582. // }
  1583. func removeAllOutline(_ outline: CPDFOutline) {
  1584. self.listView.document.setNewOutlineRoot()
  1585. for i in 0 ..< self.listView.document.pageCount {
  1586. self.listView.document.removeBookmark(forPageIndex: i)
  1587. }
  1588. self.listView.layoutDocumentView()
  1589. DispatchQueue.main.async {
  1590. self.tocOutlineView.reloadData()
  1591. }
  1592. }
  1593. func updateNoteFilterPredicate() {
  1594. //注释筛选
  1595. // [rightSideController.noteArrayController setFilterPredicate:[noteTypeSheetController filterPredicateForSearchString:[rightSideController.searchField stringValue] caseInsensitive:mwcFlags.caseInsensitiveNoteSearch]];
  1596. let stringValue = self.noteSearchField.stringValue
  1597. // NSPredicate *predicate = [noteTypeSheetController filterPredicateForSearchString:[rightSideController.searchField stringValue] caseInsensitive:mwcFlags.caseInsensitiveNoteSearch];
  1598. // [self loadAnnotationSortData:[NSArray arrayWithObjects:predicate, nil]];
  1599. self.noteSearchArray.removeAll()
  1600. if stringValue.isEmpty {
  1601. self.noteSearchArray = self.allAnnotations
  1602. } else {
  1603. for note in self.allAnnotations {
  1604. var noteString = KMBOTAAnnotationTool.fetchContentLabelString(annotation: note)
  1605. if let anno = note as? CPDFMarkupAnnotation {
  1606. noteString = anno.markupContent()
  1607. }
  1608. if noteString.contains(stringValue) {
  1609. self.noteSearchArray.append(note)
  1610. }
  1611. }
  1612. }
  1613. Task { @MainActor in
  1614. self.noteOutlineView.reloadData()
  1615. }
  1616. }
  1617. @objc func selectSelectedNote(_ sender: AnyObject?) {
  1618. if self.listView.hideNotes == false {
  1619. let selectedNotes = self.selectedNotes()
  1620. if selectedNotes.count == 1 {
  1621. let annotation = selectedNotes.last!
  1622. self.listView.go(to: annotation.bounds, on: annotation.page, animated: true)
  1623. // [pdfView scrollAnnotationToVisible:annotation];
  1624. // [pdfView setActiveAnnotation:annotation];
  1625. self.listView.updateActiveAnnotations([annotation])
  1626. self.listView.setNeedsDisplayAnnotationViewForVisiblePages()
  1627. // }
  1628. }
  1629. // NSInteger column = [sender clickedColumn];
  1630. // if (column != -1) {
  1631. // NSString *colID = [[[sender tableColumns] objectAtIndex:column] identifier];
  1632. //
  1633. // if ([colID isEqualToString:@"color"]){
  1634. // for (PDFAnnotation *annotation in self.pdfView.activeAnnotations) {
  1635. // if (![annotation isKindOfClass:[PDFAnnotationChoiceWidget class]] &&
  1636. // ![annotation isKindOfClass:[PDFAnnotationButtonWidget class]] &&
  1637. // ![annotation isKindOfClass:[PDFAnnotationTextWidget class]]) {
  1638. // [[NSColorPanel sharedColorPanel] orderFront:nil];
  1639. // break;
  1640. // }
  1641. //
  1642. // }
  1643. // }
  1644. // }
  1645. }
  1646. }
  1647. func selectedNotes() -> [CPDFAnnotation] {
  1648. var selectedNotes: [CPDFAnnotation] = []
  1649. let rowIndexes = self.noteOutlineView.selectedRowIndexes
  1650. for row in rowIndexes {
  1651. let item = self.noteOutlineView.item(atRow: row)
  1652. if item is KMBOTAAnnotationItem {
  1653. if let anno = (item as! KMBOTAAnnotationItem).annotation {
  1654. // if anno.type == nil {
  1655. // item = [(SKNoteText *)item note];
  1656. // }
  1657. if selectedNotes.contains(anno) == false {
  1658. selectedNotes.append(anno)
  1659. }
  1660. }
  1661. }
  1662. }
  1663. return selectedNotes
  1664. }
  1665. }
  1666. // MARK: - Note
  1667. extension KMLeftSideViewController {
  1668. func loadAnnotationSortData(_ morePredicates: NSArray) {
  1669. var isLink = false
  1670. var typeMutableArr = NSMutableArray()
  1671. if self.allAnnotations.count > 0 {
  1672. for annotation in self.allAnnotations {
  1673. if annotation is CPDFLinkAnnotation || annotation is CPDFTextWidgetAnnotation || annotation is CPDFButtonWidgetAnnotation || annotation is CPDFChoiceWidgetAnnotation {
  1674. // [annotation.widgetFieldType isEqualToString:PDFAnnotationWidgetSubtypeSignature] ||
  1675. // [annotation.widgetFieldType isEqualToString:PDFAnnotationWidgetSubtypeText] ||
  1676. // [annotation.widgetFieldType isEqualToString:PDFAnnotationWidgetSubtypeButton] ||
  1677. // [annotation.widgetFieldType isEqualToString:PDFAnnotationWidgetSubtypeChoice]) {
  1678. isLink = true
  1679. } else {
  1680. if typeMutableArr.contains(annotation.type) == false {
  1681. typeMutableArr.add(annotation.type)
  1682. }
  1683. }
  1684. }
  1685. }
  1686. var colorMutableArray = NSMutableArray()
  1687. var typeMutableArray = NSMutableArray()
  1688. var authorMutableArray = NSMutableArray()
  1689. for annotation in self.allAnnotations {
  1690. if annotation is CPDFLinkAnnotation || annotation is CPDFTextWidgetAnnotation || annotation is CPDFButtonWidgetAnnotation || annotation is CPDFChoiceWidgetAnnotation {
  1691. // [annotation.widgetFieldType isEqualToString:PDFAnnotationWidgetSubtypeSignature] ||
  1692. // [annotation.widgetFieldType isEqualToString:PDFAnnotationWidgetSubtypeText] ||
  1693. // [annotation.widgetFieldType isEqualToString:PDFAnnotationWidgetSubtypeButton] ||
  1694. // [annotation.widgetFieldType isEqualToString:PDFAnnotationWidgetSubtypeChoice]) {
  1695. let noteColor = annotation.color ?? .clear
  1696. let noteType = annotation.type ?? ""
  1697. let authorString = annotation.userName() ?? ""
  1698. if (noteColor != nil) {
  1699. if (colorMutableArray.count > 0) {
  1700. if colorMutableArray.contains(noteColor) == false {
  1701. colorMutableArray.add(noteColor)
  1702. }
  1703. } else {
  1704. colorMutableArray.add(noteColor)
  1705. }
  1706. }
  1707. if noteType.isEmpty == false {
  1708. if typeMutableArray.count > 0 {
  1709. if typeMutableArray.contains(noteType) == false {
  1710. typeMutableArray.add(noteType)
  1711. }
  1712. } else {
  1713. typeMutableArray.add(noteType)
  1714. }
  1715. }
  1716. if authorString.isEmpty == false {
  1717. if authorString.count > 0 {
  1718. if authorMutableArray.contains(authorString) == false {
  1719. authorMutableArray.add(authorString)
  1720. }
  1721. } else {
  1722. authorMutableArray.add(authorString)
  1723. }
  1724. }
  1725. } else {
  1726. if typeMutableArr.contains(annotation.type) == false {
  1727. typeMutableArr.add(annotation.type)
  1728. }
  1729. }
  1730. }
  1731. let sud = UserDefaults.standard
  1732. var typeArr = NSMutableArray()
  1733. if let typeData = sud.object(forKey: "KMNoteOutlineFilterSelectArray_Type") as? Data {
  1734. if let data = NSKeyedUnarchiver.unarchiveObject(with: typeData) as? NSArray {
  1735. typeArr = NSMutableArray(array: data)
  1736. }
  1737. }
  1738. var colorArr = NSMutableArray()
  1739. if let colorData = sud.object(forKey: "KMNoteOutlineFilterSelectArray_Color") as? Data {
  1740. if let data = NSKeyedUnarchiver.unarchiveObject(with: colorData) as? NSArray {
  1741. colorArr = NSMutableArray(array: data)
  1742. }
  1743. }
  1744. var authorArr = NSMutableArray()
  1745. if let authorData = sud.object(forKey: "KMNoteOutlineFilterSelectArray_Author") as? Data {
  1746. if let data = NSKeyedUnarchiver.unarchiveObject(with: authorData) as? NSArray {
  1747. authorArr = NSMutableArray(array: data)
  1748. }
  1749. }
  1750. var temporaryArr1 = NSMutableArray()
  1751. var temporaryArr2 = NSMutableArray()
  1752. var temporaryArr3 = NSMutableArray()
  1753. if (typeArr.count > 0) {
  1754. for type in typeArr {
  1755. if typeMutableArray.contains(type) == false {
  1756. temporaryArr1.add(type)
  1757. }
  1758. }
  1759. }
  1760. if (colorArr.count > 0) {
  1761. for color in colorArr {
  1762. if colorMutableArray.contains(color) == false {
  1763. temporaryArr2.add(color)
  1764. }
  1765. }
  1766. }
  1767. if (authorArr.count > 0) {
  1768. for author in authorArr {
  1769. if authorMutableArray.contains(author) == false {
  1770. temporaryArr3.add(author)
  1771. }
  1772. }
  1773. }
  1774. if (temporaryArr1.count > 0) {
  1775. for type in temporaryArr1 {
  1776. typeArr.remove(type)
  1777. }
  1778. }
  1779. if (temporaryArr2.count > 0) {
  1780. for color in temporaryArr2 {
  1781. colorArr.remove(color)
  1782. }
  1783. }
  1784. if (temporaryArr3.count > 0) {
  1785. for author in temporaryArr3 {
  1786. authorArr.remove(author)
  1787. }
  1788. }
  1789. var predicateMutableArr = NSMutableArray()
  1790. // predicateMutableArr.add
  1791. }
  1792. }
  1793. // MARK: - KMBotaTableViewDelegate
  1794. extension KMLeftSideViewController: KMBotaTableViewDelegate {
  1795. func tableView(_ aTableView: NSTableView, canCopyRowsWithIndexes rowIndexes: IndexSet) -> Bool {
  1796. if aTableView.isEqual(to: self.thumbnailTableView) || aTableView.isEqual(to: self.findTableView) || aTableView.isEqual(to: self.groupedFindTableView) {
  1797. return rowIndexes.count > 0
  1798. }
  1799. return false
  1800. }
  1801. func tableView(_ aTableView: NSTableView, canPasteFromPasteboard pboard: NSPasteboard) -> Bool {
  1802. if aTableView.isEqual(to: self.thumbnailTableView) {
  1803. if self._copysPages.count > 0 {
  1804. return true
  1805. }
  1806. }
  1807. return false
  1808. }
  1809. func tableView(_ aTableView: NSTableView, canDeleteRowsWithIndexes rowIndexes: IndexSet) -> Bool {
  1810. // if ([tv isEqual:rightSideController.snapshotTableView]) {
  1811. // return [rowIndexes count] > 0;
  1812. // } else
  1813. if aTableView.isEqual(to: self.thumbnailTableView) {
  1814. if self.listView.document.pageCount <= 1 {
  1815. return false
  1816. }
  1817. return true
  1818. }
  1819. return false
  1820. }
  1821. func tableView(_ aTableView: NSTableView, pasteFromPasteboard pboard: NSPasteboard?) {
  1822. if IAPProductsManager.default().isAvailableAllFunction() == false {
  1823. KMPurchaseCompareWindowController.sharedInstance().showWindow(nil)
  1824. return
  1825. }
  1826. if aTableView.isEqual(to: self.thumbnailTableView) {
  1827. if self._copysPages.count > 0 {
  1828. let index = self.listView.document.index(for: self.listView.currentPage()) + 1
  1829. if (index == NSNotFound) {
  1830. return
  1831. }
  1832. for page in self._copysPages.reversed() {
  1833. // if let newPage = page.copy() as? CPDFPage {
  1834. self.listView.document.insertPageObject(page, at: index)
  1835. // }
  1836. self.listView.layoutDocumentView()
  1837. // [pageLabels setArray:[[pdfView document] pageLabels]];
  1838. self.resetThumbnails()
  1839. let pageIndex = min(index, self.listView.document.pageCount-1)
  1840. self.listView.go(toPageIndex: Int(pageIndex), animated: true)
  1841. (self.listView.undoManager?.prepare(withInvocationTarget: self) as AnyObject).tableView(self.thumbnailTableView, deleteRowsWithIndexes: IndexSet(integer: IndexSet.Element(index)))
  1842. }
  1843. }
  1844. }
  1845. }
  1846. func tableViewMoveLeft(_ aTableView: NSTableView) {
  1847. if aTableView.isEqual(to: self.findTableView) || aTableView.isEqual(to: self.groupedFindTableView) {
  1848. // [self updateFindResultHighlightsForDirection:NSSelectingPrevious];
  1849. }
  1850. }
  1851. func tableViewMoveRight(_ aTableView: NSTableView) {
  1852. if aTableView.isEqual(to: self.findTableView) || aTableView.isEqual(to: self.groupedFindTableView) {
  1853. // [self updateFindResultHighlightsForDirection:NSSelectingNext];
  1854. }
  1855. }
  1856. func tableView(_ aTableView: NSTableView, imageContextForRow rowIndex: Int) -> AnyObject? {
  1857. if aTableView.isEqual(to: self.findTableView) {
  1858. if rowIndex >= self.searchResults.count {
  1859. return nil
  1860. }
  1861. let model = self.searchResults[rowIndex]
  1862. let selection = model.selection
  1863. let point = NSPoint(x: NSWidth(selection.bounds) * 0.5, y: NSHeight(selection.bounds) * 0.5)
  1864. return CPDFDestination(document: self.listView.document, pageIndex: Int(selection.page.pageIndex()), at: point, zoom: self.listView.scaleFactor)
  1865. } else if aTableView.isEqual(to: self.groupedFindTableView) {
  1866. if rowIndex >= self.groupSearchResults.count {
  1867. return nil
  1868. }
  1869. // let model = self.groupSearchResults[rowIndex]
  1870. // let selection = model.selection
  1871. // let point = NSPoint(x: NSWidth(selection.bounds) * 0.5, y: NSHeight(selection.bounds) * 0.5)
  1872. // return CPDFDestination(document: self.listView.document, pageIndex: Int(selection.page.pageIndex()), at: point, zoom: self.listView.scaleFactor)
  1873. }
  1874. return nil
  1875. }
  1876. /*
  1877. - (NSArray *)tableView:(NSTableView *)tv typeSelectHelperSelectionStrings:(SKTypeSelectHelper *)typeSelectHelper {
  1878. if ([tv isEqual:leftSideController.thumbnailTableView]) {
  1879. return pageLabels;
  1880. }
  1881. return nil;
  1882. }
  1883. - (void)tableView:(NSTableView *)tv typeSelectHelper:(SKTypeSelectHelper *)typeSelectHelper didFailToFindMatchForSearchString:(NSString *)searchString {
  1884. if ([tv isEqual:leftSideController.thumbnailTableView]) {
  1885. [statusBar setLeftStringValue:[NSString stringWithFormat:NSLocalizedString(@"No match: \"%@\"", @"Status message"), searchString]];
  1886. }
  1887. }
  1888. - (void)tableView:(NSTableView *)tv typeSelectHelper:(SKTypeSelectHelper *)typeSelectHelper updateSearchString:(NSString *)searchString {
  1889. if ([tv isEqual:leftSideController.thumbnailTableView]) {
  1890. if (searchString.length > 0)
  1891. [statusBar setLeftStringValue:[NSString stringWithFormat:NSLocalizedString(@"Go to page: %@", @"Status message"), searchString]];
  1892. else
  1893. [self updateLeftStatus];
  1894. }
  1895. }
  1896. */
  1897. }
  1898. // MARK: - KMThumbnailTableViewDelegate
  1899. extension KMLeftSideViewController: KMThumbnailTableViewDelegate {
  1900. func tableView(_ tableView: NSTableView, highlightLevelForRow row: Int) -> UInt {
  1901. if tableView.isEqual(to: self.thumbnailTableView) {
  1902. // NSUInteger i, iMax = [lastViewedPages count];
  1903. // for (i = 0; i < iMax; i++) {
  1904. // if (row == (NSInteger)[lastViewedPages pointerAtIndex:i])
  1905. // return i;
  1906. // }
  1907. }
  1908. return UInt.max
  1909. }
  1910. func tableView(_ tableView: NSTableView, commandSelectRow rowIndex: Int) -> Bool {
  1911. if tableView.isEqual(to: self.thumbnailTableView) {
  1912. // NSRect rect = [[[pdfView document] pageAtIndex:row] boundsForBox:kPDFDisplayBoxCropBox];
  1913. //
  1914. // rect.origin.y = NSMidY(rect) - 0.5 * SNAPSHOT_HEIGHT;
  1915. // rect.size.height = SNAPSHOT_HEIGHT;
  1916. // [self showSnapshotAtPageNumber:row forRect:rect scaleFactor:[pdfView scaleFactor] autoFits:NO];
  1917. let thumbailTabCell = tableView.view(atColumn: 0, row: rowIndex, makeIfNecessary: true) as? KMThumbnailTableviewCell
  1918. thumbailTabCell?.isSelectCell = true
  1919. var rowIndexSet = IndexSet()
  1920. for i in self.thumbnailTableView.selectedRowIndexes {
  1921. rowIndexSet.insert(i)
  1922. }
  1923. rowIndexSet.insert(rowIndex)
  1924. self.thumbnailTableView.selectRowIndexes(rowIndexSet, byExtendingSelection: true)
  1925. return true
  1926. }
  1927. return false
  1928. }
  1929. func tableView(_ tableView: NSTableView, shiftSelectRow rowIndex: Int) -> Bool {
  1930. if tableView.isEqual(to: self.thumbnailTableView) {
  1931. if (self.thumbnailTableView.selectedRowIndexes.count == 0) {
  1932. let thumbailTabCell = tableView.view(atColumn: 0, row: rowIndex, makeIfNecessary: true) as? KMThumbnailTableviewCell
  1933. thumbailTabCell?.isSelectCell = true
  1934. var rowIndexSet = IndexSet()
  1935. self.thumbnailTableView.selectRowIndexes(rowIndexSet, byExtendingSelection: true)
  1936. return true
  1937. } else if (self.thumbnailTableView.selectedRowIndexes.count == 1 && self.thumbnailTableView.selectedRowIndexes.first == rowIndex) {
  1938. return false
  1939. } else {
  1940. var fristIndex = self.thumbnailTableView.selectedRowIndexes.first ?? Int.min
  1941. var lastIndex = self.thumbnailTableView.selectedRowIndexes.last ?? Int.max
  1942. for idx in self.thumbnailTableView.selectedRowIndexes {
  1943. // [leftSideController.thumbnailTableView.selectedRowIndexes enumerateIndexesUsingBlock:^(NSUInteger idx, BOOL * _Nonnull stop) {
  1944. if(idx < fristIndex) {
  1945. fristIndex = idx
  1946. }
  1947. if(idx > lastIndex) {
  1948. lastIndex = idx
  1949. }
  1950. }
  1951. if(rowIndex < fristIndex) {
  1952. fristIndex = rowIndex
  1953. } else if (rowIndex > lastIndex) {
  1954. lastIndex = rowIndex
  1955. }
  1956. var rowIndexSet = IndexSet()
  1957. for i in fristIndex ... lastIndex {
  1958. rowIndexSet.insert(i)
  1959. }
  1960. self.thumbnailTableView.selectRowIndexes(rowIndexSet, byExtendingSelection: true)
  1961. return true
  1962. }
  1963. }
  1964. return false
  1965. }
  1966. }
  1967. // MARK: - 扩展
  1968. extension KMLeftSideViewController {
  1969. public func selectType(_ type: BotaType) {
  1970. // 更新 type
  1971. var show = true
  1972. if (self.type.methodType == .None) {
  1973. show = true
  1974. } else {
  1975. if (self.type.methodType == type) {
  1976. show = false
  1977. } else {
  1978. show = true
  1979. }
  1980. }
  1981. var index: Int = NSNotFound
  1982. switch type {
  1983. case .None:
  1984. index = NSNotFound
  1985. break
  1986. case .Thumbnail:
  1987. index = 0
  1988. break
  1989. case .Outline:
  1990. index = 1
  1991. break
  1992. case .BookMark:
  1993. index = 2
  1994. break
  1995. case .Annotation:
  1996. index = 3
  1997. break
  1998. case .Search:
  1999. index = 4
  2000. break
  2001. case .From:
  2002. index = 5
  2003. break
  2004. case .Signature:
  2005. index = 6
  2006. break
  2007. case .snapshot:
  2008. index = 7
  2009. break
  2010. }
  2011. self.type = self.getMethodMode(show ? type : .None)
  2012. // 更新按钮状态
  2013. // self.updateViewButtonState()
  2014. // 将事件传递出去
  2015. self.delegate?.controlStateChange?(self,show:show)
  2016. if (index != NSNotFound) {
  2017. // 记录当前选中
  2018. UserDefaults.standard.set(index, forKey: "KMBOTASelectedIndexKey")
  2019. UserDefaults.standard.synchronize()
  2020. }
  2021. }
  2022. private func getMethodMode(_ type: BotaType) -> KMLeftMethodMode {
  2023. let mode = KMLeftMethodMode()
  2024. mode.methodType = type
  2025. switch type {
  2026. case .None:
  2027. mode.methodName = ""
  2028. break
  2029. case .Thumbnail:
  2030. mode.methodName = thumbnailMethodKey
  2031. break
  2032. case .Outline:
  2033. mode.methodName = outlineMethodKey
  2034. break
  2035. case .BookMark:
  2036. mode.methodName = bookMarkMethodKey
  2037. break
  2038. case .Annotation:
  2039. mode.methodName = anntationMethodKey
  2040. break
  2041. case .Search:
  2042. mode.methodName = searchMethodKey
  2043. break
  2044. case .From:
  2045. mode.methodName = formMethodKey
  2046. break
  2047. case .Signature:
  2048. mode.methodName = signatureMethodKey
  2049. break
  2050. case .snapshot:
  2051. mode.methodName = snapshotMethodKey
  2052. }
  2053. return mode
  2054. }
  2055. }
  2056. //MARK: Cache
  2057. extension KMLeftSideViewController {
  2058. func clearAnnotationFilterData() {
  2059. if let _key = self.listView?.document?.documentURL?.path {
  2060. let userDefaults = UserDefaults.standard
  2061. let typeData = try?NSKeyedArchiver.archivedData(withRootObject: [Any](), requiringSecureCoding: false)
  2062. userDefaults.set(typeData, forKey: "KMNoteOutlineFilterSelectArray_Type" + _key)
  2063. let colorData = try?NSKeyedArchiver.archivedData(withRootObject: [Any](), requiringSecureCoding: false)
  2064. userDefaults.set(colorData, forKey: "KMNoteOutlineFilterSelectArray_Color" + _key)
  2065. let authorData = try?NSKeyedArchiver.archivedData(withRootObject: [Any](), requiringSecureCoding: false)
  2066. userDefaults.set(authorData, forKey: "KMNoteOutlineFilterSelectArray_Author" + _key)
  2067. userDefaults.synchronize()
  2068. }
  2069. }
  2070. func clearNotification() {
  2071. }
  2072. }
  2073. // MARK: - Analytics (埋点)
  2074. extension KMLeftSideViewController {
  2075. func trackEvent(type: BotaType) -> Void {
  2076. if (type == .Thumbnail) {
  2077. KMAnalytics.trackEvent(eventName: "Btn_LeftSideBar_Thumbnail", parameters: [
  2078. KMAnalytics.Parameter.categoryKey : KMAnalytics.Category.leftSideBar,
  2079. KMAnalytics.Parameter.labelKey : KMAnalytics.Label.leftSideBar_Btn], platform: .AppCenter, appTarget: .all)
  2080. } else if (type == .Outline) {
  2081. KMAnalytics.trackEvent(eventName: "Btn_LeftSideBar_Outline", parameters: [
  2082. KMAnalytics.Parameter.categoryKey : KMAnalytics.Category.leftSideBar,
  2083. KMAnalytics.Parameter.labelKey : KMAnalytics.Label.leftSideBar_Btn], platform: .AppCenter, appTarget: .all)
  2084. } else if (type == .BookMark) {
  2085. KMAnalytics.trackEvent(eventName: "Btn_LeftSideBar_BookMark", parameters: [
  2086. KMAnalytics.Parameter.categoryKey : KMAnalytics.Category.leftSideBar,
  2087. KMAnalytics.Parameter.labelKey : KMAnalytics.Label.leftSideBar_Btn], platform: .AppCenter, appTarget: .all)
  2088. } else if (type == .Annotation) {
  2089. KMAnalytics.trackEvent(eventName: "Btn_LeftSideBar_Annotation", parameters: [
  2090. KMAnalytics.Parameter.categoryKey : KMAnalytics.Category.leftSideBar,
  2091. KMAnalytics.Parameter.labelKey : KMAnalytics.Label.leftSideBar_Btn], platform: .AppCenter, appTarget: .all)
  2092. } else if (type == .Search) {
  2093. KMAnalytics.trackEvent(eventName: "Btn_LeftSideBar_Search", parameters: [
  2094. KMAnalytics.Parameter.categoryKey : KMAnalytics.Category.leftSideBar,
  2095. KMAnalytics.Parameter.labelKey : KMAnalytics.Label.leftSideBar_Btn], platform: .AppCenter, appTarget: .all)
  2096. }
  2097. }
  2098. }
  2099. // MARK: - Private Methods
  2100. extension KMLeftSideViewController {
  2101. @objc private func _themeChanged(_ notification: NSNotification) {
  2102. DispatchQueue.main.asyncAfter(deadline: .now()+0.3) {
  2103. self._updateViewColor()
  2104. }
  2105. }
  2106. private func _updateViewColor() {
  2107. if(KMAppearance.isDarkMode()){
  2108. self.leftListView.layer?.backgroundColor = NSColor(red: 0.149, green: 0.157, blue: 0.169, alpha: 1).cgColor
  2109. self.snapshotNormalView.layer?.backgroundColor = NSColor(red: 0.149, green: 0.157, blue: 0.169, alpha: 1).cgColor
  2110. self.thumbnailView.layer?.backgroundColor = NSColor(red: 0.149, green: 0.157, blue: 0.169, alpha: 1).cgColor
  2111. self.noteView.layer?.backgroundColor = NSColor(red: 0.149, green: 0.157, blue: 0.169, alpha: 1).cgColor
  2112. self.outlineView.layer?.backgroundColor = NSColor(red: 0.149, green: 0.157, blue: 0.169, alpha: 1).cgColor
  2113. self.view.layer?.backgroundColor = NSColor(red: 0.149, green: 0.157, blue: 0.169, alpha: 1).cgColor
  2114. self.thumbnailTableView.backgroundColor = NSColor(red: 0.149, green: 0.157, blue: 0.169, alpha: 1)
  2115. self.groupedFindTableView.backgroundColor = NSColor(red: 0.149, green: 0.157, blue: 0.169, alpha: 1)
  2116. self.tocOutlineView.backgroundColor = NSColor(red: 0.149, green: 0.157, blue: 0.169, alpha: 1)
  2117. self.findTableView.backgroundColor = NSColor(red: 0.149, green: 0.157, blue: 0.169, alpha: 1)
  2118. self.snapshotSearchField.layer?.backgroundColor = NSColor(red: 0.224, green: 0.235, blue: 0.243, alpha: 1).cgColor
  2119. self.outlineSearchField.layer?.backgroundColor = NSColor(red: 0.224, green: 0.235, blue: 0.243, alpha: 1).cgColor
  2120. self.noteSearchField.layer?.backgroundColor = NSColor(red: 0.224, green: 0.235, blue: 0.243, alpha: 1).cgColor
  2121. self.segmentedControl.layer?.backgroundColor = NSColor(red: 0.224, green: 0.235, blue: 0.243, alpha: 1).cgColor
  2122. self.snapshotSearchField.backgroundColor = NSColor(red: 0.224, green: 0.235, blue: 0.243, alpha: 1)
  2123. self.outlineSearchField.backgroundColor = NSColor(red: 0.224, green: 0.235, blue: 0.243, alpha: 1)
  2124. self.noteSearchField.backgroundColor = NSColor(red: 0.224, green: 0.235, blue: 0.243, alpha: 1)
  2125. } else {
  2126. self.leftListView.layer?.backgroundColor = NSColor(red: 0.988, green: 0.992, blue: 1, alpha: 1).cgColor
  2127. self.snapshotNormalView.layer?.backgroundColor = NSColor(red: 0.988, green: 0.992, blue: 1, alpha: 1).cgColor
  2128. self.thumbnailView.layer?.backgroundColor = NSColor(red: 0.988, green: 0.992, blue: 1, alpha: 1).cgColor
  2129. self.noteView.layer?.backgroundColor = NSColor(red: 0.988, green: 0.992, blue: 1, alpha: 1).cgColor
  2130. self.outlineView.layer?.backgroundColor = NSColor(red: 0.988, green: 0.992, blue: 1, alpha: 1).cgColor
  2131. self.view.layer?.backgroundColor = NSColor(red: 0.988, green: 0.992, blue: 1, alpha: 1).cgColor
  2132. self.thumbnailTableView.backgroundColor = NSColor(red: 0.988, green: 0.992, blue: 1.000, alpha: 1)
  2133. self.groupedFindTableView.backgroundColor = NSColor(red: 0.988, green: 0.992, blue: 1.000, alpha: 1)
  2134. self.tocOutlineView.backgroundColor = NSColor(red: 0.988, green: 0.992, blue: 1.000, alpha: 1)
  2135. self.findTableView.backgroundColor = NSColor(red: 0.988, green: 0.992, blue: 1.000, alpha: 1)
  2136. self.snapshotSearchField.layer?.backgroundColor = NSColor(red: 0.922, green: 0.925, blue: 0.941, alpha: 1).cgColor
  2137. self.outlineSearchField.layer?.backgroundColor = NSColor(red: 0.922, green: 0.925, blue: 0.941, alpha: 1).cgColor
  2138. self.noteSearchField.layer?.backgroundColor = NSColor(red: 0.922, green: 0.925, blue: 0.941, alpha: 1).cgColor
  2139. self.segmentedControl.layer?.backgroundColor = NSColor(red: 0.922, green: 0.925, blue: 0.941, alpha: 1).cgColor
  2140. self.snapshotSearchField.backgroundColor = NSColor(red: 0.922, green: 0.925, blue: 0.941, alpha: 1)
  2141. self.outlineSearchField.backgroundColor = NSColor(red: 0.922, green: 0.925, blue: 0.941, alpha: 1)
  2142. self.noteSearchField.backgroundColor = NSColor(red: 0.922, green: 0.925, blue: 0.941, alpha: 1)
  2143. }
  2144. }
  2145. private func _hasContainString(_ searchString: String, rootOutline outline: CPDFOutline) -> Bool {
  2146. var label = outline.label ?? ""
  2147. var searchLabel = searchString
  2148. if self.outlineIgnoreCaseFlag {
  2149. label = label.lowercased()
  2150. searchLabel = searchLabel.lowercased()
  2151. }
  2152. if label.contains(searchLabel) {
  2153. // if ([outline.label rangeOfString:searchString options:self.outlineIgnoreCaseFlag?NSCaseInsensitiveSearch:0].location != NSNotFound){
  2154. return true
  2155. } else {
  2156. var subHas = false
  2157. for i in 0 ..< outline.numberOfChildren {
  2158. if let subOutline = outline.child(at: i) {
  2159. subHas = self._hasContainString(searchString, rootOutline: subOutline)
  2160. } else {
  2161. continue
  2162. }
  2163. if (subHas) {
  2164. break
  2165. }
  2166. }
  2167. return subHas
  2168. }
  2169. return false
  2170. }
  2171. }
  2172. // MARK: - NSTableViewDelegate, NSTableViewDataSource
  2173. extension KMLeftSideViewController: NSTableViewDelegate, NSTableViewDataSource {
  2174. func numberOfRows(in tableView: NSTableView) -> Int {
  2175. if tableView.isEqual(to: self.thumbnailTableView) {
  2176. return self.thumbnails.count
  2177. } else if tableView.isEqual(to: self.findTableView) {
  2178. return self.searchResults.count
  2179. } else if tableView.isEqual(to: self.groupedFindTableView) {
  2180. return self.groupSearchResults.count
  2181. } else if tableView.isEqual(to: self.snapshotTableView) {
  2182. if self.isSearchSnapshotMode {
  2183. return self.searchSnapshots.count
  2184. }
  2185. return self.snapshots.count
  2186. }
  2187. return 0
  2188. }
  2189. func tableView(_ tableView: NSTableView, viewFor tableColumn: NSTableColumn?, row: Int) -> NSView? {
  2190. if tableView.isEqual(to: self.thumbnailTableView) {
  2191. let cell = tableView.makeView(withIdentifier: NSUserInterfaceItemIdentifier(rawValue: "KMThumbnailTableviewCell"), owner: self) as! KMThumbnailTableviewCell
  2192. let thumbnail = self.thumbnails[row]
  2193. cell.pageNumLabel.stringValue = thumbnail.label
  2194. // cell.thumImage.image = thumbnail.image
  2195. cell.pageView.page = self.listView.document.page(at: UInt(row))
  2196. if let _image = thumbnail.image {
  2197. let multiplierHToW = _image.size.height / (_image.size.width == 0 ? 1 : _image.size.width)
  2198. let multiplierWToH = _image.size.width / (_image.size.height == 0 ? 1 : _image.size.height)
  2199. if (_image.size.height > _image.size.width) {
  2200. NSLayoutConstraint.deactivate([cell.imageAspectRatioLayout])
  2201. // cell.imageAspectRatioLayout = NSLayoutConstraint(item: cell.thumImage, attribute: .height, relatedBy: .equal, toItem: cell.thumImage, attribute: .width, multiplier: multiplierHToW, constant: 0)
  2202. // NSLayoutConstraint.activate([cell.imageAspectRatioLayout])
  2203. } else {
  2204. NSLayoutConstraint.deactivate([cell.imageAspectRatioLayout])
  2205. // cell.imageAspectRatioLayout = NSLayoutConstraint(item: cell.thumImage, attribute: .width, relatedBy: .equal, toItem: cell.thumImage, attribute: .height, multiplier: multiplierWToH, constant: 0)
  2206. // NSLayoutConstraint.activate([cell.imageAspectRatioLayout])
  2207. }
  2208. }
  2209. if (self.isDisplayPageSize) {
  2210. cell.sizeLabel.isHidden = false
  2211. //获取Page的真实尺寸
  2212. let page = self.listView.document.page(at: UInt(row))
  2213. let rect = page?.bounds(for: .cropBox) ?? .zero
  2214. let w = KMPageSizeTool.conversion(withUnit: "mm", value: (CGRectGetWidth(rect)/595 * 210))
  2215. let h = KMPageSizeTool.conversion(withUnit: "mm", value: (CGRectGetHeight(rect)/842 * 297))
  2216. if let data = page?.rotation, data == 90 || data == 270 {
  2217. cell.sizeLabel.stringValue = String(format: "%.f × %.f %@", h.stringToCGFloat(), w.stringToCGFloat(), KMLocalizedString("mm", nil))
  2218. } else {
  2219. cell.sizeLabel.stringValue = String(format: "%.f × %.f %@", w.stringToCGFloat(), h.stringToCGFloat(), KMLocalizedString("mm", nil))
  2220. }
  2221. } else {
  2222. cell.sizeLabel.isHidden = true
  2223. }
  2224. cell.sizeTopConstant.constant = cell.sizeLabel.isHidden ? -cell.sizeLabel.frame.size.height : 0
  2225. if(self.thumbnailTableView.selectedRowIndexes.contains(row)) {
  2226. cell.isSelectCell = true
  2227. } else {
  2228. cell.isSelectCell = false
  2229. }
  2230. return cell
  2231. } else if (tableView.isEqual(to: self.findTableView)) {
  2232. let cell = tableView.makeView(withIdentifier: NSUserInterfaceItemIdentifier(rawValue: "KMFindTableviewCell"), owner: self) as! KMFindTableviewCell
  2233. let selection = searchResults[row]
  2234. if let data = tableColumn?.identifier.rawValue, data == "results" {
  2235. cell.resultLabel.attributedStringValue = selection.attributedString
  2236. cell.resultLabel.textColor = KMAppearance.Layout.h0Color()
  2237. } else if let data = tableColumn?.identifier.rawValue, data == "page" {
  2238. cell.resultLabel.stringValue = "\(Int(selection.selection.page?.pageIndex() ?? 0) + 1)"
  2239. cell.resultLabel.textColor = KMAppearance.Layout.h2Color()
  2240. }
  2241. return cell
  2242. } else if tableView.isEqual(to: self.snapshotTableView) {
  2243. let cell = tableView.makeView(withIdentifier: NSUserInterfaceItemIdentifier(rawValue: "KMSnapshotTableViewCell"), owner: self) as! KMSnapshotTableViewCell
  2244. var snapshot: KMSnapshotModel?
  2245. if self.isSearchSnapshotMode {
  2246. snapshot = self.searchSnapshots[row]
  2247. } else {
  2248. snapshot = self.snapshots[row]
  2249. }
  2250. cell.snapshotImage.image = snapshot?.windowC?.thumbnail
  2251. cell.snapshotLabel.stringValue = snapshot?.windowC?.pageLabel ?? ""
  2252. if let data = snapshot?.windowC?.hasWindow, data {
  2253. cell.snapshotImageView.isHidden = false
  2254. } else {
  2255. cell.snapshotImageView.isHidden = true
  2256. }
  2257. cell.isSelectCell = snapshot?.isSelected ?? false
  2258. if (row == tableView.selectedRow) {
  2259. cell.snapshotImageView.image = NSImage(named: KMImageNameUXIconSidebarSnapshotWindowSel)
  2260. } else {
  2261. cell.snapshotImageView.image = NSImage(named: KMImageNameUXIconSidebarSnapshotWindowNor)
  2262. }
  2263. return cell
  2264. }
  2265. return nil
  2266. }
  2267. func tableView(_ tableView: NSTableView, heightOfRow row: Int) -> CGFloat {
  2268. if tableView.isEqual(to: self.thumbnailTableView) {
  2269. let scaling = UserDefaults.standard.float(forKey: "KMThumbnailSizeScalingKey")
  2270. // let thumbnailSize = self.thumbnails[row].size
  2271. let thumbnailSize = NSMakeSize(self.thumbnailCacheSize, self.thumbnailCacheSize)
  2272. let newScaling: CGFloat = scaling.cgFloat + 0.1
  2273. let newThumbnailHeight = thumbnailSize.width * newScaling
  2274. if (newThumbnailHeight > MIN_SIDE_PANE_WIDTH) {
  2275. self.thumbnailZoomOutButton.isEnabled = false
  2276. } else {
  2277. self.thumbnailZoomOutButton.isEnabled = true
  2278. }
  2279. if ((scaling - 0.1) < 0.3) {
  2280. self.thumbnailZoomInButton.isEnabled = false
  2281. } else {
  2282. self.thumbnailZoomInButton.isEnabled = true
  2283. }
  2284. var labelHeight = 0.0
  2285. if (self.isDisplayPageSize) {
  2286. labelHeight = 56.0
  2287. } else {
  2288. labelHeight = 41.5
  2289. }
  2290. let cellHeight = thumbnailSize.height + labelHeight
  2291. var thumbSize: NSSize = .zero
  2292. if (scaling != nil && scaling > 0) {
  2293. thumbSize = NSMakeSize(thumbnailSize.width * scaling.cgFloat, cellHeight * scaling.cgFloat)
  2294. } else {
  2295. thumbSize = NSMakeSize(thumbnailSize.width, cellHeight)
  2296. }
  2297. return thumbSize.height
  2298. // NSSize cellSize = NSMakeSize([[tv tableColumnWithIdentifier:IMAGE_COLUMNID] width], fmin(thumbSize.height, roundedThumbnailSize));
  2299. // if (thumbSize.height < [tv rowHeight])
  2300. // return [tv rowHeight];
  2301. // else if (thumbSize.width / thumbSize.height < cellSize.width / cellSize.height)
  2302. // return cellSize.height;
  2303. // else
  2304. // return fmax([tv rowHeight], fmin(cellSize.width, thumbSize.width) * thumbSize.height / thumbSize.width);
  2305. } else if tableView.isEqual(to: self.snapshotTableView) {
  2306. let scaling = UserDefaults.standard.float(forKey: "KMSnapshotSizeScalingKey")
  2307. // NSSize snapshotSize = [[[[rightSideController.snapshotArrayController arrangedObjects] objectAtIndex:row] thumbnail] size];
  2308. let snapshotSize = CGSizeMake(120, 63)
  2309. var newScaling = scaling + 0.1
  2310. let newSnapshotHeight = snapshotSize.width * newScaling.cgFloat;
  2311. if (newSnapshotHeight > MIN_SIDE_PANE_WIDTH) {
  2312. self.snapshotNormalZoomInButton.isEnabled = false
  2313. } else {
  2314. self.snapshotNormalZoomInButton.isEnabled = true
  2315. }
  2316. if ((scaling - 0.1) < 0.3 || (newSnapshotHeight < 150.0)) {
  2317. self.snapshotNormalZoomOutButton.isEnabled = false
  2318. } else {
  2319. self.snapshotNormalZoomOutButton.isEnabled = true
  2320. }
  2321. let cellHeight = snapshotSize.height + 24.0
  2322. var thumbSize: NSSize = .zero
  2323. if (scaling > 0) {
  2324. thumbSize = NSMakeSize(snapshotSize.width * scaling.cgFloat, cellHeight * scaling.cgFloat)
  2325. } else {
  2326. thumbSize = NSMakeSize(snapshotSize.width, cellHeight)
  2327. }
  2328. return thumbSize.height
  2329. } else if (tableView.isEqual(to: self.findTableView)) {
  2330. return 40.0
  2331. } else if tableView.isEqual(to: self.groupedFindTableView) {
  2332. return 16
  2333. }
  2334. return tableView.rowHeight
  2335. }
  2336. func tableView(_ tableView: NSTableView, rowViewForRow row: Int) -> NSTableRowView? {
  2337. if (tableView.isEqual(to: self.findTableView)) {
  2338. let rowView = KMBotaTableRowView()
  2339. return rowView
  2340. } else if tableView.isEqual(to: self.groupedFindTableView) {
  2341. let rowView = KMGroupFindTableRowView()
  2342. rowView.totalNumber = self.groupSearchResults.first?.datas.count ?? 0
  2343. let model = self.groupSearchResults[row]
  2344. rowView.number = model.datas.count
  2345. rowView.pageIndex = Int(model.selectionPageIndex) + 1
  2346. return rowView
  2347. }
  2348. return nil
  2349. }
  2350. func tableViewSelectionIsChanging(_ notification: Notification) {
  2351. }
  2352. func tableViewSelectionDidChange(_ notification: Notification) {
  2353. // if ([[aNotification object] isEqual:leftSideController.findTableView] || [[aNotification object] isEqual:leftSideController.groupedFindTableView]) {
  2354. // [self updateFindResultHighlightsForDirection:NSDirectSelection];
  2355. //
  2356. // if ([self interactionMode] == SKPresentationMode && [[NSUserDefaults standardUserDefaults] boolForKey:SKAutoHidePresentationContentsKey])
  2357. // [self hideLeftSideWindow];
  2358. // } else
  2359. if self.findTableView.isEqual(to: notification.object) {
  2360. let row = self.findTableView.selectedRow
  2361. if row >= 0 {
  2362. let model = self.searchResults[row]
  2363. if model.selection != nil {
  2364. self.listView.go(to: model.selection, animated: true)
  2365. self.listView.setHighlightedSelection(model.selection, animated: true)
  2366. self.listView.setNeedsDisplayAnnotationViewForVisiblePages()
  2367. }
  2368. }
  2369. } else if self.thumbnailTableView.isEqual(to: notification.object) {
  2370. // if (mwcFlags.updatingThumbnailSelection == 0) {
  2371. let row = self.thumbnailTableView.selectedRow
  2372. let curPage = self.listView.document.index(for: self.listView.currentPage())
  2373. if (row != -1 && row != curPage) {
  2374. // [pdfView goToPage:[[pdfView document] pageAtIndex:row]];
  2375. self.listView.go(toPageIndex: row, animated: true)
  2376. }
  2377. // if ([self interactionMode] == SKPresentationMode && [[NSUserDefaults standardUserDefaults] boolForKey:SKAutoHidePresentationContentsKey])
  2378. // [self hideLeftSideWindow];
  2379. // thumbnailSelectCount = row;
  2380. // var rowIndexSet = IndexSet()
  2381. // for i in 0 ..< self.thumbnailTableView.numberOfRows {
  2382. // rowIndexSet.insert(i)
  2383. // }
  2384. // self.thumbnailTableView.reloadData(forRowIndexes: rowIndexSet, columnIndexes: IndexSet(integer: 0))
  2385. // }
  2386. let view = self.thumbnailTableView.view(atColumn: 0, row: self.preThumbnailRow, makeIfNecessary: false) as? KMThumbnailTableviewCell
  2387. view?.isSelectCell = false
  2388. let view2 = self.thumbnailTableView.view(atColumn: 0, row: row, makeIfNecessary: false) as? KMThumbnailTableviewCell
  2389. view2?.isSelectCell = true
  2390. self.preThumbnailRow = row
  2391. }
  2392. // else if ([[aNotification object] isEqual:rightSideController.snapshotTableView]) {
  2393. // NSInteger row = [rightSideController.snapshotTableView selectedRow];
  2394. // [selectCellList removeAllObjects];
  2395. // [selectCellList addObject:[NSString stringWithFormat:@"%ld",row]];
  2396. // if (row != -1) {
  2397. // SKSnapshotWindowController *controller = [[rightSideController.snapshotArrayController arrangedObjects] objectAtIndex:row];
  2398. // if ([[controller window] isVisible])
  2399. // [[controller window] orderFront:self];
  2400. // }
  2401. // NSMutableIndexSet *rowIndexSet = [[[NSMutableIndexSet alloc] init] autorelease];
  2402. // for (NSInteger i = 0; i < rightSideController.snapshotTableView.numberOfRows; i ++) {
  2403. // [rowIndexSet addIndex:i];
  2404. // }
  2405. // NSMutableIndexSet *columnIndexSet = [[[NSMutableIndexSet alloc] init] autorelease];
  2406. // [columnIndexSet addIndex:0];
  2407. // [rightSideController.snapshotTableView reloadDataForRowIndexes:rowIndexSet columnIndexes:columnIndexSet];
  2408. //
  2409. // }
  2410. }
  2411. func tableView(_ tableView: NSTableView, validateDrop info: NSDraggingInfo, proposedRow row: Int, proposedDropOperation dropOperation: NSTableView.DropOperation) -> NSDragOperation {
  2412. if dropOperation == .on || tableView.isEqual(to: self.thumbnailTableView) == false {
  2413. return NSDragOperation(rawValue: 0)
  2414. }
  2415. let pboard = info.draggingPasteboard
  2416. if (pboard.availableType(from: [.localDraggedTypes]) != nil) {
  2417. return .move
  2418. } else if (pboard.availableType(from: [.fileURL]) != nil) && tableView.isEqual(to: self.thumbnailTableView) {
  2419. guard let pbItems = pboard.pasteboardItems else {
  2420. return NSDragOperation(rawValue: 0)
  2421. }
  2422. var hasValidFile = false
  2423. for item in pbItems {
  2424. guard let data = item.string(forType: .fileURL), let _url = URL(string: data) else {
  2425. continue
  2426. }
  2427. let type = _url.pathExtension.lowercased()
  2428. if type == "pdf" || KMImageToPDFMethod.supportedImageTypes().contains(type) {
  2429. hasValidFile = true
  2430. }
  2431. }
  2432. if (!hasValidFile) {
  2433. return NSDragOperation(rawValue: 0)
  2434. } else {
  2435. return .move
  2436. }
  2437. }
  2438. return NSDragOperation(rawValue: 0)
  2439. }
  2440. func tableView(_ tableView: NSTableView, acceptDrop info: NSDraggingInfo, row: Int, dropOperation: NSTableView.DropOperation) -> Bool {
  2441. var result = false
  2442. if tableView.isEqual(to: self.thumbnailTableView) == false {
  2443. return result
  2444. }
  2445. let pasteboard = info.draggingPasteboard
  2446. if (pasteboard.availableType(from: [.localDraggedTypes]) != nil) {
  2447. result = true
  2448. let rowData = pasteboard.data(forType: .localDraggedTypes)
  2449. let rowIndexes = NSKeyedUnarchiver.unarchiveObject(with: rowData!) as? IndexSet ?? IndexSet()
  2450. let urlData = pasteboard.data(forType: KPDFThumbnailDoucumentURLForDraggedTypes)
  2451. let url = NSKeyedUnarchiver.unarchiveObject(with: urlData!) as? String
  2452. if url != nil && url != self.listView.document.documentURL.absoluteString {
  2453. if (pasteboard.availableType(from: [.fileURL]) != nil) {
  2454. // if ([pasteboard availableTypeFromArray:[NSArray arrayWithObject:NSFilenamesPboardType]]){
  2455. // NSArray *fileNames = [pasteboard propertyListForType:NSFilenamesPboardType];
  2456. var fileNames = pasteboard.propertyList(forType: .fileURL) as? [String] ?? []
  2457. if (fileNames.count == 1) {
  2458. let path = fileNames.first ?? ""
  2459. let pathExtension = path.pathExtension.lowercased()
  2460. if pathExtension == "pdf" {
  2461. var index = row
  2462. var insertIndexSet = IndexSet()
  2463. var pdf = CPDFDocument(url: URL(fileURLWithPath: path))
  2464. if let data = pdf?.isEncrypted, data {
  2465. KMBaseWindowController.checkPassword(url: URL(fileURLWithPath: path)) { success, resultPassword in
  2466. if success && resultPassword.isEmpty == false {
  2467. for i in 0 ..< (pdf?.pageCount ?? 0) {
  2468. let page = pdf?.page(at: i).copy() as? CPDFPage
  2469. self.listView.document.insertPageObject(page!, at: UInt(index))
  2470. insertIndexSet.insert(index)
  2471. index += 1
  2472. }
  2473. self.insertPages(insertIndexSet, pageAt: index-1)
  2474. }
  2475. }
  2476. } else {
  2477. for i in 0 ..< (pdf?.pageCount ?? 0) {
  2478. // PDFPage *page = [[pdf pageAtIndex:i] copy];
  2479. var page = pdf?.page(at: i)
  2480. self.listView.document.insertPageObject(page, at: UInt(index))
  2481. insertIndexSet.insert(index)
  2482. index += 1
  2483. }
  2484. self.insertPages(insertIndexSet, pageAt: index-1)
  2485. }
  2486. }
  2487. return true
  2488. }
  2489. }
  2490. }
  2491. var pageIndex = 0
  2492. var pages: [CPDFPage] = []
  2493. for idx in rowIndexes {
  2494. let page = self.listView.document.page(at: UInt(idx))?.copy() as? CPDFPage
  2495. pages.append(page!)
  2496. }
  2497. var toPage: CPDFPage?
  2498. if row < self.listView.document.pageCount {
  2499. toPage = self.listView.document.page(at: UInt(row))
  2500. }
  2501. var toPageIndex = 0
  2502. if (toPage != nil) {
  2503. toPageIndex = Int(self.listView.document.index(for: toPage))
  2504. } else {
  2505. toPageIndex = Int(self.listView.document.pageCount)
  2506. }
  2507. for i in 0 ..< pages.count {
  2508. var page = pages[i]
  2509. self.listView.document.insertPageObject(page, at: UInt(toPageIndex+i))
  2510. pageIndex += 1
  2511. self.listView.go(toPageIndex: toPageIndex+i, animated: false)
  2512. }
  2513. self.listView.layoutDocumentView()
  2514. var deletepages: [CPDFPage] = []
  2515. for idx in rowIndexes {
  2516. if idx < toPageIndex {
  2517. if((idx+pageIndex) < self.listView.document.pageCount) {
  2518. var page = self.listView.document.page(at: UInt(idx))
  2519. deletepages.append(page!)
  2520. self.listView.document.removePage(at: IndexSet(integer: idx))
  2521. }
  2522. } else {
  2523. if((idx+pageIndex) < self.listView.document.pageCount) {
  2524. let page = self.listView.document.page(at: UInt(idx+pageIndex))
  2525. deletepages.append(page!)
  2526. self.listView.document.removePage(at: IndexSet(integer: idx+pageIndex))
  2527. }
  2528. }
  2529. }
  2530. // [[[[self document] undoManager] prepareWithInvocationTarget:self] undoClick];
  2531. // [self.pageLabels setArray:[[self.pdfView document] pageLabels]];
  2532. self.listView.layoutDocumentView()
  2533. self.resetThumbnails()
  2534. } else if pasteboard.availableType(from: [.fileURL]) != nil && tableView.isEqual(to: self.thumbnailTableView) {
  2535. if IAPProductsManager.default().isAvailableAllFunction() == false {
  2536. KMPurchaseCompareWindowController.sharedInstance().showWindow(nil)
  2537. return false
  2538. }
  2539. var fileNames = pasteboard.propertyList(forType: .fileURL)
  2540. var insertIndexSet = IndexSet()
  2541. var index = row
  2542. // if (fileNames.count == 1) {
  2543. if fileNames != nil {
  2544. // var path = fileNames.first as? String ?? ""
  2545. var path = fileNames as? String ?? ""
  2546. let url = URL(string: path)
  2547. let pathExtension = url?.pathExtension.lowercased() ?? ""
  2548. if pathExtension == "pdf" {
  2549. let pdf = CPDFDocument(url: url!)
  2550. if let data = pdf?.isEncrypted, data {
  2551. // KMDecryptWindowController *vc = [[KMDecryptWindowController alloc] init];
  2552. KMBaseWindowController.checkPassword(url: url!) { success, resultPassword in
  2553. if success && resultPassword.isEmpty == false {
  2554. pdf?.unlock(withPassword: resultPassword)
  2555. for i in 0 ..< (pdf?.pageCount ?? 0) {
  2556. // let page = pdf?.page(at: i).copy() as? CPDFPage
  2557. let page = pdf?.page(at: i)
  2558. self.listView.document.insertPageObject(page!, at: UInt(index))
  2559. insertIndexSet.insert(index)
  2560. index += 1
  2561. }
  2562. self.insertPages(insertIndexSet, pageAt: index-1)
  2563. }
  2564. }
  2565. } else {
  2566. for i in 0 ..< (pdf?.pageCount ?? 0) {
  2567. // let page = pdf?.page(at: i).copy() as? CPDFPage
  2568. let page = pdf?.page(at: i)
  2569. self.listView.document.insertPageObject(page!, at: UInt(index))
  2570. insertIndexSet.insert(index)
  2571. index += 1
  2572. }
  2573. self.insertPages(insertIndexSet, pageAt: index-1)
  2574. }
  2575. } else if KMImageToPDFMethod.supportedImageTypes().contains(pathExtension) {
  2576. let image = NSImage(contentsOfFile: url?.path ?? "")
  2577. // PDFPage * page = [[[PDFPage alloc] initWithImage:image] autorelease];
  2578. // [pdfView.document insertPage:page atIndex:index];
  2579. _ = self.listView.document.km_insert(image: image!, at: UInt(index))
  2580. insertIndexSet.insert(index)
  2581. self.insertPages(insertIndexSet, pageAt: index-1)
  2582. }
  2583. result = true
  2584. } else {
  2585. result = false
  2586. }
  2587. }
  2588. return result == false
  2589. }
  2590. func tableView(_ tableView: NSTableView, writeRowsWith rowIndexes: IndexSet, to pboard: NSPasteboard) -> Bool {
  2591. if tableView.isEqual(to: self.thumbnailTableView) {
  2592. let idx = rowIndexes.first ?? NSNotFound
  2593. if (idx != NSNotFound && self.listView.document.isLocked == false) {
  2594. let page = self.listView.document.page(at: UInt(idx))
  2595. var fileExt: String?
  2596. let tiffData = page?.PDFListViewTIFFData(for: page?.bounds(for: self.listView.displayBox) ?? .zero)
  2597. if self.listView.document.allowsPrinting {
  2598. // NSData *pdfData = [page dataRepresentation];
  2599. fileExt = "pdf"
  2600. // filenames
  2601. pboard.declareTypes([.pdf, .tiff, .fileURL, .filePromise, .localDraggedTypes, KPDFThumbnailDoucumentURLForDraggedTypes], owner: self)
  2602. // let newDoc = CPDFDocument()
  2603. // newDoc?.insertPageObject(page, at: 0)
  2604. // let data = newDoc?.dataRepresentation()
  2605. // [pboard setData:pdfData forType:NSPasteboardTypePDF];
  2606. pboard.setData(tiffData, forType: .pdf)
  2607. // NSData *zNSIndexSetData = [NSKeyedArchiver archivedDataWithRootObject:rowIndexes];
  2608. let zNSIndexSetData = NSKeyedArchiver.archivedData(withRootObject: rowIndexes)
  2609. pboard.setData(zNSIndexSetData, forType: .localDraggedTypes)
  2610. let documentURL = NSKeyedArchiver.archivedData(withRootObject: self.listView.document.documentURL?.absoluteString)
  2611. // NSString *docmentName = [[[pdfView.document.documentURL path] lastPathComponent] stringByDeletingPathExtension];
  2612. var docmentName = self.listView.document.documentURL.deletingPathExtension().lastPathComponent
  2613. // __block NSMutableString *pagesName = nil;
  2614. var pagesName = ""
  2615. if (rowIndexes.count > 1) {
  2616. pagesName = " pages"
  2617. } else {
  2618. pagesName = " page"
  2619. }
  2620. let tFileName = String(format: "%@ %@", pagesName, self.fileNameWithSelectedPages(rowIndexes))
  2621. let pdf = CPDFDocument()
  2622. for idx in rowIndexes {
  2623. // var copyPage = self.listView.document.page(at: UInt(idx)).copy() as? CPDFPage
  2624. var copyPage = self.listView.document.page(at: UInt(idx))
  2625. pdf?.insertPageObject(copyPage, at: pdf?.pageCount ?? 0)
  2626. }
  2627. let paths = NSSearchPathForDirectoriesInDomains(.cachesDirectory, .userDomainMask, true)
  2628. var cachesDir = paths.first ?? ""
  2629. docmentName = String(format: "%@%@", docmentName, tFileName)
  2630. if (docmentName.count > 50) {
  2631. docmentName = docmentName.substring(to: 50)
  2632. }
  2633. // cachesDir = [[cachesDir stringByAppendingPathComponent:docmentName] stringByAppendingPathExtension:@"pdf"];
  2634. cachesDir = "\(cachesDir)/\(docmentName).pdf"
  2635. let success = pdf?.write(toFile: cachesDir) ?? false
  2636. if (success) {
  2637. // [pboard setPropertyList:@[cachesDir] forType:NSFilenamesPboardType];
  2638. pboard.setPropertyList([cachesDir], forType: .fileURL)
  2639. } else{
  2640. // [pboard setPropertyList:@[@""] forType:NSFilenamesPboardType];
  2641. pboard.setPropertyList([], forType: .fileURL)
  2642. }
  2643. pboard.setData(documentURL, forType: KPDFThumbnailDoucumentURLForDraggedTypes)
  2644. } else {
  2645. fileExt = "tiff"
  2646. pboard.declareTypes([.tiff, .filePromise], owner: self)
  2647. }
  2648. pboard.setData(tiffData, forType: .tiff)
  2649. pboard.setPropertyList([fileExt], forType: .filePromise)
  2650. return true
  2651. }
  2652. }
  2653. // else if ([tv isEqual:rightSideController.snapshotTableView]) {
  2654. // NSUInteger idx = [rowIndexes firstIndex];
  2655. // if (idx != NSNotFound) {
  2656. // SKSnapshotWindowController *snapshot = [self objectInSnapshotsAtIndex:idx];
  2657. // [pboard declareTypes:[NSArray arrayWithObjects:NSPasteboardTypeTIFF, NSFilesPromisePboardType, nil] owner:self];
  2658. // [pboard setData:[[snapshot thumbnailWithSize:0.0] TIFFRepresentation] forType:NSPasteboardTypeTIFF];
  2659. // [pboard setPropertyList:[NSArray arrayWithObject:@"tiff"] forType:NSFilesPromisePboardType];
  2660. // return YES;
  2661. // }
  2662. // }
  2663. return false
  2664. }
  2665. @objc dynamic func tableView(_ aTableView: NSTableView, deleteRowsWithIndexes rowIndexes: IndexSet) {
  2666. if IAPProductsManager.default().isAvailableAllFunction() == false {
  2667. KMPurchaseCompareWindowController.sharedInstance().showWindow(nil)
  2668. return
  2669. }
  2670. // if ([tv isEqual:rightSideController.snapshotTableView]) {
  2671. // NSArray *controllers = [[rightSideController.snapshotArrayController arrangedObjects] objectsAtIndexes:rowIndexes];
  2672. // [controllers makeObjectsPerformSelector:@selector(close)];
  2673. // } else
  2674. if aTableView.isEqual(to: self.thumbnailTableView) {
  2675. for idx in rowIndexes {
  2676. if idx >= self.listView.document.pageCount {
  2677. return
  2678. }
  2679. if let page = self.listView.document.page(at: UInt(idx)) {
  2680. for anno in page.annotations {
  2681. page.removeAnnotation(anno)
  2682. }
  2683. (self.listView.undoManager?.prepare(withInvocationTarget: self) as AnyObject).insertPage(page, pageAt: idx)
  2684. self.listView.document.removePage(at: IndexSet(integer: idx))
  2685. }
  2686. }
  2687. self.listView.layoutDocumentView()
  2688. // [pageLabels setArray:[[pdfView document] pageLabels]];
  2689. self.resetThumbnails()
  2690. let idx = rowIndexes.first ?? -1
  2691. let index = min(idx, Int(self.listView.document.pageCount)-1)
  2692. self.listView.go(toPageIndex: index, animated: false)
  2693. }
  2694. }
  2695. func tableView(_ aTableView: NSTableView, copyRowsWithIndexes rowIndexes: IndexSet) {
  2696. if IAPProductsManager.default().isAvailableAllFunction() == false {
  2697. KMPurchaseCompareWindowController.sharedInstance().showWindow(nil)
  2698. return
  2699. }
  2700. if aTableView.isEqual(to: self.thumbnailTableView) {
  2701. self._copysPages.removeAll()
  2702. if self.listView.document.isLocked == false {
  2703. for idx in rowIndexes {
  2704. if let page = self.listView.document.page(at: UInt(idx))?.copy() as? CPDFPage {
  2705. self._copysPages.append(page)
  2706. }
  2707. }
  2708. // NSData *tiffData = [page TIFFDataForRect:[page boundsForBox:[pdfView displayBox]]];
  2709. // NSPasteboard *pboard = [NSPasteboard generalPasteboard];
  2710. // NSPasteboardItem *pboardItem = [[[NSPasteboardItem alloc] init] autorelease];
  2711. // if ([[pdfView document] allowsPrinting])
  2712. // [pboardItem setData:[page dataRepresentation] forType:NSPasteboardTypePDF];
  2713. // [pboardItem setData:tiffData forType:NSPasteboardTypeTIFF];
  2714. // [pboard clearContents];
  2715. // [pboard writeObjects:[NSArray arrayWithObjects:pboardItem, nil]];
  2716. }
  2717. }
  2718. // else if ([tv isEqual:leftSideController.findTableView]) {
  2719. // NSMutableString *string = [NSMutableString string];
  2720. // [rowIndexes enumerateIndexesUsingBlock:^(NSUInteger idx, BOOL *stop) {
  2721. // PDFSelection *match = [searchResults objectAtIndex:idx];
  2722. // [string appendString:@"* "];
  2723. // [string appendFormat:NSLocalizedString(@"Page %@", @""), [match firstPageLabel]];
  2724. // [string appendFormat:@": %@\n", [[match contextString] string]];
  2725. // }];
  2726. // NSPasteboard *pboard = [NSPasteboard generalPasteboard];
  2727. // [pboard clearContents];
  2728. // [pboard writeObjects:[NSArray arrayWithObjects:string, nil]];
  2729. // } else if ([tv isEqual:leftSideController.groupedFindTableView]) {
  2730. // NSMutableString *string = [NSMutableString string];
  2731. // [rowIndexes enumerateIndexesUsingBlock:^(NSUInteger idx, BOOL *stop) {
  2732. // SKGroupedSearchResult *result = [groupedSearchResults objectAtIndex:idx];
  2733. // NSArray *matches = [result matches];
  2734. // [string appendString:@"* "];
  2735. // [string appendFormat:NSLocalizedString(@"Page %@", @""), [[result page] displayLabel]];
  2736. // [string appendString:@": "];
  2737. // [string appendFormat:NSLocalizedString(@"%ld Results", @""), (long)[matches count]];
  2738. // [string appendFormat:@":\n\t%@\n", [[matches valueForKeyPath:@"contextString.string"] componentsJoinedByString:@"\n\t"]];
  2739. // }];
  2740. // NSPasteboard *pboard = [NSPasteboard generalPasteboard];
  2741. // [pboard clearContents];
  2742. // [pboard writeObjects:[NSArray arrayWithObjects:string, nil]];
  2743. // }
  2744. }
  2745. /*
  2746. #pragma mark dragging
  2747. - (NSArray *)tableView:(NSTableView *)tv namesOfPromisedFilesDroppedAtDestination:(NSURL *)dropDestination forDraggedRowsWithIndexes:(NSIndexSet *)rowIndexes {
  2748. if ([tv isEqual:leftSideController.thumbnailTableView]) {
  2749. __block NSMutableArray *fileURLArray = [NSMutableArray array];
  2750. if (rowIndexes.count > 1) {
  2751. NSString *docmentName = @"";
  2752. NSString * tFileName = [NSString stringWithFormat:@"%@",[self fileNameWithSelectedPages:rowIndexes]];
  2753. PDFDocument *pdf = [[[PDFDocument alloc] init] autorelease];
  2754. [rowIndexes enumerateIndexesUsingBlock:^(NSUInteger idx, BOOL * _Nonnull stop) {
  2755. if (idx != NSNotFound && [[pdfView document] isLocked] == NO) {
  2756. PDFPage *copyPage = [[[pdfView document] pageAtIndex:idx] copy];
  2757. [pdf insertPage:copyPage atIndex:pdf.pageCount];
  2758. [copyPage release];
  2759. }
  2760. }];
  2761. NSURL *fileURL = [[[dropDestination URLByAppendingPathComponent:tFileName] URLByAppendingPathExtension:@"pdf"] uniqueFileURL];
  2762. docmentName = fileURL.path;
  2763. BOOL success = [pdf writeToFile:docmentName];
  2764. if(success) {
  2765. [fileURLArray addObject:[fileURL lastPathComponent]];
  2766. }
  2767. } else {
  2768. PDFPage *page = [[pdfView document] pageAtIndex:rowIndexes.firstIndex];
  2769. NSURL *fileURL = [dropDestination URLByAppendingPathComponent:[self draggedFileNameForPage:page]];
  2770. NSString *pathExt = nil;
  2771. NSData *data = nil;
  2772. if ([[pdfView document] allowsPrinting]) {
  2773. pathExt = @"pdf";
  2774. data = [page dataRepresentation];
  2775. } else {
  2776. pathExt = @"tiff";
  2777. data = [page TIFFDataForRect:[page boundsForBox:[pdfView displayBox]]];
  2778. }
  2779. fileURL = [[fileURL URLByAppendingPathExtension:pathExt] uniqueFileURL];
  2780. if ([data writeToURL:fileURL atomically:YES]) {
  2781. [fileURLArray addObject:[fileURL lastPathComponent]];
  2782. }
  2783. }
  2784. return fileURLArray;
  2785. } else if ([tv isEqual:rightSideController.snapshotTableView]) {
  2786. NSUInteger idx = [rowIndexes firstIndex];
  2787. if (idx != NSNotFound) {
  2788. SKSnapshotWindowController *snapshot = [self objectInSnapshotsAtIndex:idx];
  2789. PDFPage *page = [[pdfView document] pageAtIndex:[snapshot pageIndex]];
  2790. NSURL *fileURL = [[dropDestination URLByAppendingPathComponent:[self draggedFileNameForPage:page]] URLByAppendingPathExtension:@"tiff"];
  2791. fileURL = [fileURL uniqueFileURL];
  2792. if ([[[snapshot thumbnailWithSize:0.0] TIFFRepresentation] writeToURL:fileURL atomically:YES])
  2793. return [NSArray arrayWithObjects:[fileURL lastPathComponent], nil];
  2794. }
  2795. }
  2796. return [NSArray array];
  2797. }
  2798. - (void)tableView:(NSTableView *)tv sortDescriptorsDidChange:(NSArray *)oldDescriptors {
  2799. if ([tv isEqual:leftSideController.groupedFindTableView]) {
  2800. [leftSideController.groupedFindArrayController setSortDescriptors:[tv sortDescriptors]];
  2801. }
  2802. }
  2803. #pragma mark NSTableView delegate protocol
  2804. - (void)tableViewColumnDidResize:(NSNotification *)aNotification {
  2805. if ([[[[aNotification userInfo] objectForKey:@"NSTableColumn"] identifier] isEqualToString:IMAGE_COLUMNID]) {
  2806. if ([[aNotification object] isEqual:leftSideController.thumbnailTableView]) {
  2807. [leftSideController.thumbnailTableView noteHeightOfRowsWithIndexesChanged:[NSIndexSet indexSetWithIndexesInRange:NSMakeRange(0, [leftSideController.thumbnailTableView numberOfRows])]];
  2808. } else if ([[aNotification object] isEqual:rightSideController.snapshotTableView]) {
  2809. [rightSideController.snapshotTableView noteHeightOfRowsWithIndexesChanged:[NSIndexSet indexSetWithIndexesInRange:NSMakeRange(0, [rightSideController.snapshotTableView numberOfRows])]];
  2810. }
  2811. }
  2812. }
  2813. - (void)tableView:(NSTableView *)tv shareRowsWithIndexes:(NSIndexSet *)rowIndexes {
  2814. if ([tv isEqual:leftSideController.thumbnailTableView]) {
  2815. NSUInteger idx = [rowIndexes firstIndex];
  2816. if (idx != NSNotFound) {
  2817. PDFPage *page = [[pdfView document] pageAtIndex:idx];
  2818. NSString *fileName = [[[pdfView document] documentURL] lastPathComponent];
  2819. NSString *folderPath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0];
  2820. NSString *filePath = [folderPath stringByAppendingPathComponent:[NSString stringWithFormat:@"Untitled"]];
  2821. filePath = [filePath stringByAppendingPathExtension:[fileName pathExtension]];
  2822. [self fileWithPage:page atPath:filePath];
  2823. if (rint(NSAppKitVersionNumber) < NSAppKitVersionNumber10_8) {
  2824. [KMMailHelper sendFileWithPaths:[NSArray arrayWithObject:filePath]];
  2825. } else {
  2826. NSSharingService *service = [NSSharingService sharingServiceNamed:NSSharingServiceNameComposeEmail];
  2827. [service performWithItems:[NSArray arrayWithObject:[NSURL fileURLWithPath:filePath]]];
  2828. }
  2829. }
  2830. }
  2831. }
  2832. - (void)fileWithPage:(PDFPage *)page atPath:(NSString *)filePath {
  2833. NSData *data = [page dataRepresentation];
  2834. PDFDocument *document = [[PDFDocument alloc] initWithData:data];
  2835. [[NSFileManager defaultManager] removeItemAtPath:filePath error:nil];
  2836. BOOL success = [document writeToURL:[NSURL fileURLWithPath:filePath]];
  2837. if (success) {
  2838. NSWorkspace *workspace = [NSWorkspace sharedWorkspace];
  2839. NSURL *url = [NSURL fileURLWithPath:filePath];
  2840. [workspace activateFileViewerSelectingURLs:[NSArray arrayWithObject:url]];
  2841. }
  2842. [document release];
  2843. */
  2844. }
  2845. // MARK: - NSOutlineViewDelegate, NSOutlineViewDataSource
  2846. /*
  2847. func outlineView(_ outlineView: NSOutlineView, child index: Int, ofItem item: Any?) -> Any {
  2848. if item is KMBOTAAnnotationSection {
  2849. let section = item as? KMBOTAAnnotationSection
  2850. return section!.annotations?[index] as Any
  2851. } else if item is KMBOTAAnnotationItem {
  2852. return item as Any
  2853. } else {
  2854. var tempArray: [KMBOTAAnnotationSection] = []
  2855. for temp in self.data {
  2856. if temp.annotations?.count != 0 {
  2857. tempArray.append(temp)
  2858. }
  2859. }
  2860. let section = tempArray[index]
  2861. return section as Any
  2862. }
  2863. }
  2864. func outlineView(_ outlineView: NSOutlineView, viewFor tableColumn: NSTableColumn?, item: Any) -> NSView? {
  2865. if item is KMBOTAAnnotationItem {
  2866. let cell : KMAnnotationOutlineCellView = KMAnnotationOutlineCellView.init()
  2867. cell.delegate = self
  2868. cell.model = item as? KMBOTAAnnotationItem
  2869. return cell
  2870. } else if item is KMBOTAAnnotationSection {
  2871. let section = item as? KMBOTAAnnotationSection
  2872. let cell : KMAnnotationOutlineSectionView = KMAnnotationOutlineSectionView.init()
  2873. cell.model = section
  2874. return cell
  2875. }
  2876. return NSTableCellView()
  2877. }
  2878. func outlineView(_ outlineView: NSOutlineView, rowViewForItem item: Any) -> NSTableRowView? {
  2879. let rowView = KMAnnotationOutlineRowView()
  2880. if item is KMBOTAAnnotationItem {
  2881. rowView.model = (item as? KMBOTAAnnotationItem)
  2882. } else if item is KMBOTAAnnotationSection {
  2883. rowView.section = (item as? KMBOTAAnnotationSection)
  2884. }
  2885. rowView.mouseDownAction = { [unowned self] view, event in
  2886. if rowView.section != nil {
  2887. let expanded = outlineView.isItemExpanded(outlineView.item(atRow: outlineView.selectedRow))
  2888. if expanded {
  2889. outlineView.collapseItem(outlineView.item(atRow: outlineView.selectedRow), collapseChildren: true)
  2890. outlineView.reloadItem(outlineView.item(atRow: outlineView.selectedRow))
  2891. } else {
  2892. outlineView.expandItem(outlineView.item(atRow: outlineView.selectedRow), expandChildren: true)
  2893. outlineView.reloadItem(outlineView.item(atRow: outlineView.selectedRow))
  2894. }
  2895. } else if rowView.model != nil {
  2896. self.didSelectItem(view: rowView, event: event)
  2897. }
  2898. }
  2899. rowView.rightMouseDownAction = { [unowned self] view, event in
  2900. if rowView.section != nil {
  2901. } else if rowView.model != nil {
  2902. if !KMOCToolClass.arrayContains(array: self.selectItems, annotation: item) ||
  2903. self.selectItems.count == 1 {
  2904. self.selectItem(item: item as! KMBOTAAnnotationItem)
  2905. }
  2906. DispatchQueue.main.async {
  2907. self.delegate?.annotationOutlineView(self, rightMouseDownDidSelectView: view, evnet: event)
  2908. }
  2909. }
  2910. }
  2911. rowView.hoverCallback = { [unowned self] (mouseEntered, mouseBox) in
  2912. self.outlineView.enumerateAvailableRowViews { view, row in
  2913. if view is KMAnnotationOutlineRowView {
  2914. (view as? KMAnnotationOutlineRowView)?.model?.hover = false
  2915. (view as? KMAnnotationOutlineRowView)?.reloadData()
  2916. }
  2917. }
  2918. if mouseEntered {
  2919. rowView.model?.hover = true
  2920. } else {
  2921. rowView.model?.hover = false
  2922. }
  2923. }
  2924. return rowView
  2925. }
  2926. func outlineView(_ outlineView: NSOutlineView, heightOfRowByItem item: Any) -> CGFloat {
  2927. if item is KMBOTAAnnotationItem {
  2928. return KMBOTAAnnotationTool.fetchCellHeight(annotation: (item as? KMBOTAAnnotationItem)!.annotation!, maxSize: CGSize(width: self.maxWidth - 16, height: 1000))
  2929. } else if item is KMBOTAAnnotationSection {
  2930. return 40
  2931. } else {
  2932. return 30
  2933. }
  2934. }
  2935. func outlineViewSelectionDidChange(_ notification: Notification) {
  2936. if self.outlineView.selectedRow == -1 {
  2937. self.cancelSelect()
  2938. }
  2939. }
  2940. func outlineView(_ outlineView: NSOutlineView, isItemExpandable item: Any) -> Bool {
  2941. if item is KMBOTAAnnotationItem {
  2942. return false
  2943. } else if item is KMBOTAAnnotationSection {
  2944. let section = item as? KMBOTAAnnotationSection
  2945. return section!.annotations?.count ?? 0 > 0
  2946. }
  2947. return false
  2948. }
  2949. func outlineView(_ outlineView: NSOutlineView, shouldExpandItem item: Any) -> Bool {
  2950. if let item = item as? KMBOTAAnnotationSection {
  2951. if !item.isItemExpanded {
  2952. item.isItemExpanded = true
  2953. outlineView.animator().expandItem(item, expandChildren: true)
  2954. return false
  2955. }
  2956. }
  2957. return true
  2958. }
  2959. func outlineView(_ outlineView: NSOutlineView, shouldCollapseItem item: Any) -> Bool {
  2960. if let item = item as? KMBOTAAnnotationSection {
  2961. if item.isItemExpanded {
  2962. item.isItemExpanded = false
  2963. outlineView.animator().collapseItem(item, collapseChildren: true)
  2964. return false
  2965. }
  2966. }
  2967. return true
  2968. }
  2969. */
  2970. extension KMLeftSideViewController: NSOutlineViewDelegate, NSOutlineViewDataSource {
  2971. func outlineView(_ outlineView: NSOutlineView, numberOfChildrenOfItem item: Any?) -> Int {
  2972. if outlineView.isEqual(to: self.tocOutlineView) {
  2973. let isLocked = self.listView.document?.isLocked ?? true
  2974. if isLocked { // 文档不存在 或 已加锁
  2975. return 0
  2976. }
  2977. if item == nil { // 第一层
  2978. // 获取根
  2979. guard let outline = self.listView.document.outlineRoot() else {
  2980. let view = self.tocOutlineView.enclosingScrollView!
  2981. let emptyVcSize = self.leftSideEmptyVC.emptyOutlineView.frame.size
  2982. //
  2983. self.leftSideEmptyVC.emptyOutlineView.frame = NSMakeRect((view.frame.size.width-emptyVcSize.width)/2.0,(view.frame.size.height-emptyVcSize.height)/2.0, emptyVcSize.width, emptyVcSize.height)
  2984. self.leftSideEmptyVC.emptyOutlineView.autoresizingMask = [.minXMargin, .maxXMargin, .minYMargin, .maxYMargin]
  2985. self.tocOutlineView.enclosingScrollView?.documentView?.addSubview(self.leftSideEmptyVC.emptyOutlineView)
  2986. self.leftSideEmptyVC.deleteOutlineBtn.isEnabled = false
  2987. return 0
  2988. }
  2989. if outline.numberOfChildren == 0 {
  2990. // return 0
  2991. }
  2992. if self.isSearchOutlineMode { // 是否为搜索模块
  2993. if self._hasContainString(self.outlineSearchField.stringValue, rootOutline: outline) {
  2994. self.showSearchOutlineBlankState(false)
  2995. } else {
  2996. self.showSearchOutlineBlankState(true)
  2997. return 0
  2998. }
  2999. } else {
  3000. if outline.numberOfChildren > 0 { // 有数据
  3001. self.leftSideEmptyVC.emptyOutlineView.removeFromSuperview()
  3002. self.leftSideEmptyVC.deleteOutlineBtn.isEnabled = true
  3003. } else { // 没有数据
  3004. let view = self.tocOutlineView.enclosingScrollView!
  3005. let emptyVcSize = self.leftSideEmptyVC.emptyOutlineView.frame.size
  3006. //
  3007. self.leftSideEmptyVC.emptyOutlineView.frame = NSMakeRect((view.frame.size.width-emptyVcSize.width)/2.0,(view.frame.size.height-emptyVcSize.height)/2.0, emptyVcSize.width, emptyVcSize.height)
  3008. self.leftSideEmptyVC.emptyOutlineView.autoresizingMask = [.minXMargin, .maxXMargin, .minYMargin, .maxYMargin]
  3009. self.tocOutlineView.enclosingScrollView?.documentView?.addSubview(self.leftSideEmptyVC.emptyOutlineView)
  3010. self.leftSideEmptyVC.deleteOutlineBtn.isEnabled = false
  3011. return 0
  3012. }
  3013. }
  3014. // 搜索按钮
  3015. if outline.numberOfChildren > 0 {
  3016. self.outlineSearchButton.isEnabled = true
  3017. } else {
  3018. self.outlineSearchButton.isEnabled = false
  3019. }
  3020. if (self.isSearchOutlineMode) {
  3021. var num = 0
  3022. for i in 0 ..< outline.numberOfChildren {
  3023. if let child = outline.child(at: i) {
  3024. if self._hasContainString(self.outlineSearchField.stringValue, rootOutline: child) {
  3025. num += 1
  3026. }
  3027. }
  3028. }
  3029. return num
  3030. } else {
  3031. let array = self.listView.document.bookmarks() ?? [CPDFBookmark]()
  3032. var bookmarkNum = 0
  3033. if array.isEmpty == false {
  3034. bookmarkNum = 1
  3035. }
  3036. return Int(outline.numberOfChildren) + bookmarkNum
  3037. }
  3038. } else { // 第二层 +
  3039. if self.isSearchOutlineMode {
  3040. if let data = item as? String, data == "Bookmarks" { // 书签group
  3041. return 0
  3042. } else if item is CPDFOutline { // 大纲
  3043. let child = item as! CPDFOutline
  3044. if child.numberOfChildren == 0 {
  3045. return 0
  3046. }
  3047. var num = 0
  3048. for i in 0 ..< child.numberOfChildren {
  3049. if let _child = child.child(at: i) {
  3050. if self._hasContainString(self.outlineSearchField.stringValue, rootOutline: _child) {
  3051. num += 1
  3052. }
  3053. }
  3054. }
  3055. return num
  3056. } else if item is CPDFBookmark { // 书签
  3057. return 0
  3058. }
  3059. } else {
  3060. if let data = item as? String, data == "Bookmarks" { // 书签group
  3061. return (self.listView.document?.bookmarks().count) ?? 0
  3062. } else if item is CPDFOutline { // 大纲
  3063. return Int((item as? CPDFOutline)?.numberOfChildren ?? 0)
  3064. } else if item is CPDFBookmark { // 书签
  3065. return 0
  3066. }
  3067. }
  3068. }
  3069. } else if outlineView.isEqual(to: self.noteOutlineView) {
  3070. if self.noteSearchMode {
  3071. return self.noteSearchArray.count
  3072. }
  3073. var count = 0
  3074. for section in self._annotations {
  3075. if section.annotations?.count != 0 {
  3076. count += section.annotations!.count
  3077. }
  3078. }
  3079. // if (item == nil){
  3080. // NSInteger count = [[rightSideController.noteArrayController arrangedObjects] count];
  3081. if (count < 1) {
  3082. // if (notes.count < 1) {
  3083. self.noteSearchButton.isEnabled = false
  3084. self.noteFilterButton.isEnabled = false
  3085. // }
  3086. self.noteOutlineView.usesAlternatingRowBackgroundColors = false
  3087. let view = self.noteOutlineView.enclosingScrollView!
  3088. var emptyVcSize = self.leftSideEmptyVC.emptyAnnotationView.frame.size
  3089. self.leftSideEmptyVC.emptyAnnotationView.frame = NSMakeRect((view.frame.size.width-emptyVcSize.width)/2.0,(view.frame.size.height-emptyVcSize.height)/2.0, emptyVcSize.width, emptyVcSize.height)
  3090. self.leftSideEmptyVC.emptyAnnotationView.autoresizingMask = [.minXMargin, .maxXMargin, .minYMargin, .maxYMargin]
  3091. self.noteOutlineView.enclosingScrollView?.documentView?.addSubview(self.leftSideEmptyVC.emptyAnnotationView)
  3092. self.leftSideEmptyVC.exportAnnotationBtn.isEnabled = false
  3093. self.leftSideEmptyVC.deleteAnnotationBtn.isEnabled = false
  3094. //
  3095. if (self.leftView.segmentedControl.selectedSegment == KMSelectedSegmentType.annotation.rawValue) {
  3096. self.noteHeaderView.isHidden = true
  3097. self.toolButtonBoxLayoutConstraint.constant = 40.0
  3098. }
  3099. } else {
  3100. self.noteSearchButton.isEnabled = true
  3101. self.noteFilterButton.isEnabled = true
  3102. self.noteOutlineView.usesAlternatingRowBackgroundColors = false
  3103. self.leftSideEmptyVC.emptyAnnotationView.removeFromSuperview()
  3104. self.leftSideEmptyVC.exportAnnotationBtn.isEnabled = true
  3105. self.leftSideEmptyVC.deleteAnnotationBtn.isEnabled = true
  3106. //
  3107. if (self.leftView.segmentedControl.selectedSegment == KMSelectedSegmentType.annotation.rawValue) {
  3108. self.noteHeaderView.isHidden = false
  3109. self.toolButtonBoxLayoutConstraint.constant = 64.0
  3110. }
  3111. }
  3112. return count
  3113. // } else {
  3114. // return [item hasNoteText];
  3115. // }
  3116. }
  3117. return 0
  3118. }
  3119. func outlineView(_ outlineView: NSOutlineView, child index: Int, ofItem item: Any?) -> Any {
  3120. if outlineView.isEqual(to: self.tocOutlineView) {
  3121. let isLocked = self.listView.document?.isLocked ?? true
  3122. if isLocked { // 文档不存在 或 已加锁
  3123. return ""
  3124. }
  3125. let array = self.listView.document.bookmarks() ?? [CPDFBookmark]()
  3126. var bookmarkNum = 0
  3127. if array.isEmpty == false {
  3128. bookmarkNum = 1
  3129. }
  3130. if item == nil {
  3131. if self.isSearchOutlineMode {
  3132. guard let outline = self.listView.document.outlineRoot() else {
  3133. return ""
  3134. }
  3135. if outline.numberOfChildren == 0 {
  3136. return ""
  3137. }
  3138. var array: [CPDFOutline] = []
  3139. for i in 0 ..< outline.numberOfChildren {
  3140. if let child = outline.child(at: i) {
  3141. if self._hasContainString(self.outlineSearchField.stringValue, rootOutline: child) {
  3142. array.append(child)
  3143. }
  3144. }
  3145. }
  3146. if index < array.count {
  3147. return array[index]
  3148. }
  3149. return ""
  3150. } else {
  3151. if index == 0 && bookmarkNum == 1 {
  3152. return "Bookmarks"
  3153. } else {
  3154. var _index = bookmarkNum == 1 ? index-1 : index
  3155. let outline = self.listView.document.outlineRoot()
  3156. var obj = outline?.child(at: UInt(_index))
  3157. return obj as Any
  3158. }
  3159. }
  3160. } else {
  3161. if let data = item as? String, data == "Bookmarks" {
  3162. return array[index]
  3163. } else if item is CPDFOutline {
  3164. return (item as! CPDFOutline).child(at: UInt(index))
  3165. } else if item is CPDFBookmark {
  3166. return ""
  3167. }
  3168. }
  3169. } else if outlineView.isEqual(to: self.noteOutlineView) {
  3170. // if (item == nil)
  3171. // {
  3172. // return [[rightSideController.noteArrayController arrangedObjects] objectAtIndex:anIndex];
  3173. // // return [self.notes objectAtIndex:anIndex];
  3174. // }
  3175. // else
  3176. // return [item noteText];
  3177. if self.noteSearchMode {
  3178. return self.noteSearchArray[index]
  3179. }
  3180. var tempArray: [KMBOTAAnnotationItem] = []
  3181. for secion in self._annotations {
  3182. if secion.annotations?.count != 0 {
  3183. for _item in secion.annotations! {
  3184. tempArray.append(_item)
  3185. }
  3186. }
  3187. }
  3188. return tempArray[index] as Any
  3189. }
  3190. return item
  3191. }
  3192. func outlineView(_ outlineView: NSOutlineView, objectValueFor tableColumn: NSTableColumn?, byItem item: Any?) -> Any? {
  3193. if outlineView.isEqual(to: self.tocOutlineView) {
  3194. // let tcID = tableColumn?.identifier.rawValue ?? ""
  3195. // var ol = item as? CPDFOutline
  3196. // if(tcID == LABEL_COLUMNID) {
  3197. // if (self.isSearchOutlineMode) {
  3198. //// NSString *roughString = [[ol label] stringByCollapsingWhitespaceAndNewlinesAndRemovingSurroundingWhitespaceAndNewlines]? : @"";
  3199. //// NSArray *arr = [self allRangeOfRoughString:roughString searchString:self.leftSideController.outlineSearchField.stringValue];
  3200. //// NSMutableAttributedString *attributeString = [[[NSMutableAttributedString alloc] initWithString:roughString] autorelease];
  3201. //// for (NSUInteger i = 0; i <arr.count ; i++) {
  3202. //// NSValue * rangeValue = arr[i];
  3203. //// NSRange range = rangeValue.rangeValue;
  3204. //// range.location += i * self.leftSideController.outlineSearchField.stringValue.length;
  3205. //// [attributeString addAttribute:NSFontAttributeName value:[NSFont boldSystemFontOfSize:13] range:range];
  3206. //// }
  3207. //// [attributeString addAttribute:NSForegroundColorAttributeName value:[KMAppearance KMColor_Layout_H0] range:NSMakeRange(0, roughString.length)];
  3208. //// return attributeString;
  3209. // } else {
  3210. // var attributedString = NSMutableAttributedString()
  3211. //// attributedString
  3212. // attributedString.append(.init(string: ol?.label ?? "", attributes: [.foregroundColor : KMAppearance.Layout.h0Color()]))
  3213. //// NSString *roughString = [[ol label] stringByCollapsingWhitespaceAndNewlinesAndRemovingSurroundingWhitespaceAndNewlines]? : @"";
  3214. //// NSDictionary *dictAttr1 = @{NSForegroundColorAttributeName:[KMAppearance KMColor_Layout_H0]};
  3215. //// NSAttributedString *attr1 = [[NSAttributedString alloc]initWithString:roughString attributes:dictAttr1];
  3216. //// [attributedString appendAttributedString:attr1];
  3217. //
  3218. // return attributedString
  3219. // }
  3220. //
  3221. // }
  3222. // else if([tcID isEqualToString:PAGE_COLUMNID]) {
  3223. // NSMutableAttributedString *attributedString = [[[NSMutableAttributedString alloc]init] autorelease];
  3224. // NSString *roughString = [ol pageLabel]? : @"";
  3225. // NSDictionary *dictAttr1 = @{NSForegroundColorAttributeName:[KMAppearance KMColor_Layout_H0]};
  3226. // NSAttributedString *attr1 = [[NSAttributedString alloc]initWithString:roughString attributes:dictAttr1];
  3227. // [attributedString appendAttributedString:attr1];
  3228. //
  3229. // return attributedString;
  3230. // }
  3231. }
  3232. // else if ([ov isEqual:rightSideController.noteOutlineView]) {
  3233. // NSString *tcID = [tableColumn identifier];
  3234. // PDFAnnotation *note = item;
  3235. // if (tableColumn == nil || [tcID isEqualToString:NOTE_COLUMNID])
  3236. // return [note objectValue];
  3237. // else if([tcID isEqualToString:TYPE_COLUMNID]){
  3238. // NSString *noteType = [note type];
  3239. // if ([note isKindOfClass:[PDFAnnotationButtonWidget class]]) {
  3240. // PDFAnnotationButtonWidget *buttonWidget = (PDFAnnotationButtonWidget *)note;
  3241. // if (buttonWidget.controlType == kPDFWidgetRadioButtonControl) {
  3242. // noteType = SKAnnotationFormRadioButtonKey;
  3243. // } else if (buttonWidget.controlType == kPDFWidgetCheckBoxControl){
  3244. // noteType = SKAnnotationFormCheckBoxKey;
  3245. // } else if (buttonWidget.controlType == kPDFWidgetPushButtonControl){
  3246. // noteType = SKAnnotationFormActionButtonKey;
  3247. // }
  3248. // } else if ([note isKindOfClass:[PDFAnnotationTextWidget class]]){
  3249. // noteType = SKAnnotationFormTextFieldKey;
  3250. // } else if ([note isKindOfClass:[PDFAnnotationChoiceWidget class]]){
  3251. // PDFAnnotationChoiceWidget *choiceWidget = (PDFAnnotationChoiceWidget *)note;
  3252. // if (choiceWidget.isListChoice) {
  3253. // noteType = SKAnnotationFormListMenuKey;
  3254. // } else {
  3255. // noteType = SKAnnotationFormComboBoxKey;
  3256. // }
  3257. //
  3258. // }
  3259. // return [NSDictionary dictionaryWithObjectsAndKeys:[NSNumber numberWithBool:note == [pdfView activeAnnotation]], SKAnnotationTypeImageCellActiveKey, noteType, SKAnnotationTypeImageCellTypeKey,[note color],SKAnnotationTypeImageCellColorKey, nil];
  3260. // }
  3261. // else if([tcID isEqualToString:COLOR_COLUMNID])
  3262. // return [note type] ? [note color] : nil;
  3263. // else if([tcID isEqualToString:PAGE_COLUMNID])
  3264. // return [[note page] displayLabel];
  3265. // else if([tcID isEqualToString:AUTHOR_COLUMNID])
  3266. // return [note type] ? [note userName] : nil;
  3267. // else if([tcID isEqualToString:DATE_COLUMNID])
  3268. // return [note type] ? [note modificationDate] : nil;
  3269. // }
  3270. return nil
  3271. }
  3272. func outlineView(_ outlineView: NSOutlineView, viewFor tableColumn: NSTableColumn?, item: Any) -> NSView? {
  3273. if outlineView.isEqual(to: self.tocOutlineView) {
  3274. let cell = outlineView.makeView(withIdentifier: NSUserInterfaceItemIdentifier(rawValue: "KMTocTableCellView"), owner: self) as! KMTocTableCellView
  3275. let tcID = tableColumn?.identifier.rawValue
  3276. var title = ""
  3277. var pageLabel = ""
  3278. if let data = item as? String, data == "Bookmarks" {
  3279. title = NSLocalizedString("Bookmarks", comment: "")
  3280. } else if item is CPDFOutline {
  3281. title = (item as! CPDFOutline).label
  3282. pageLabel = "\(((item as! CPDFOutline).destination?.pageIndex ?? 0) + 1)"
  3283. } else if item is CPDFBookmark {
  3284. title = (item as! CPDFBookmark).label
  3285. pageLabel = "\((item as! CPDFBookmark).pageIndex + 1)"
  3286. }
  3287. if tcID == LABEL_COLUMNID {
  3288. if (self.isSearchOutlineMode) {
  3289. // NSString *roughString = [[ol label] stringByCollapsingWhitespaceAndNewlinesAndRemovingSurroundingWhitespaceAndNewlines];
  3290. let roughString = title
  3291. // NSArray *arr = [self allRangeOfRoughString:roughString searchString:self.leftSideController.outlineSearchField.stringValue];
  3292. // NSMutableAttributedString *attributeString = [[[NSMutableAttributedString alloc] initWithString:roughString] autorelease];
  3293. //
  3294. // for (NSUInteger i = 0; i <arr.count ; i++) {
  3295. // NSValue * rangeValue = arr[i];
  3296. // NSRange range = rangeValue.rangeValue;
  3297. // range.location += i * self.leftSideController.outlineSearchField.stringValue.length;
  3298. // [attributeString addAttribute:NSFontAttributeName value:[NSFont boldSystemFontOfSize:13] range:range];
  3299. // }
  3300. // cell.tocLabel.attributedStringValue = attributeString;
  3301. cell.tocLabel.stringValue = title
  3302. } else {
  3303. cell.tocLabel.stringValue = title
  3304. }
  3305. cell.pageLabel.stringValue = pageLabel
  3306. }
  3307. // else if([tcID isEqualToString:PAGE_COLUMNID]) {
  3308. // cell.pageLabel.stringValue = [ol pageLabel];
  3309. // }
  3310. return cell
  3311. } else if outlineView.isEqual(to: self.noteOutlineView) {
  3312. let model = item as? KMBOTAAnnotationItem
  3313. var note: CPDFAnnotation!
  3314. if self.noteSearchMode {
  3315. note = item as! CPDFAnnotation
  3316. } else {
  3317. note = (item as! KMBOTAAnnotationItem).annotation!
  3318. }
  3319. let cell = outlineView.makeView(withIdentifier: NSUserInterfaceItemIdentifier(rawValue: "KMNoteTableViewCell"), owner: self) as! KMNoteTableViewCell
  3320. cell.cellNote = note
  3321. let noteColor = note.color
  3322. var noteType = note.type ?? ""
  3323. var noteString = KMBOTAAnnotationTool.fetchContentLabelString(annotation: note)
  3324. // NSString *pageString = [[note page] displayLabel] ? : @"";
  3325. var pageString = "\((note.page?.pageIndex() ?? 0) + 1)"
  3326. let formatter = DateFormatter()
  3327. formatter.dateFormat = "hh:mm"
  3328. var dateString = ""
  3329. if let date = note.modificationDate() {
  3330. dateString = formatter.string(from: date)
  3331. }
  3332. var authorString = note.userName() ?? ""
  3333. // NSString *noteTextString = [note noteText].string ? : @"";
  3334. var noteTextString = note.string() ?? ""
  3335. //
  3336. // if([noteType isEqualToString:@"Redact"]) {
  3337. // noteString = @"Redact";
  3338. // }
  3339. //
  3340. let timeKey = self.noteTypeDict["kKMNoteFilterAnnotationTimeKey"] as? Bool
  3341. if timeKey == nil || timeKey == false {
  3342. // if (date != nil) {
  3343. cell.timeLabel.stringValue = dateString
  3344. //
  3345. // } else {
  3346. // cell.timeLabel.stringValue = ""
  3347. // }
  3348. cell.timeLabel.isHidden = false
  3349. } else {
  3350. cell.timeLabel.isHidden = true
  3351. }
  3352. let pageKey = self.noteTypeDict["kKMNoteFilterAnnotationPageKey"] as? Bool
  3353. if pageKey == nil || pageKey == false {
  3354. let labelsize = cell.pageLabel.stringValue.boundingRect(with: CGSizeMake(CGFloat(MAXFLOAT),CGRectGetHeight(cell.pageLabel.bounds)), options: [.usesLineFragmentOrigin, .usesFontLeading], attributes: [.font : NSFont.systemFont(ofSize: 12)])
  3355. cell.pageLabelWidthConstraint.constant = labelsize.size.width + 5
  3356. cell.pageLabel.stringValue = pageString
  3357. cell.pageLabel.isHidden = false
  3358. } else {
  3359. cell.pageLabel.isHidden = true
  3360. }
  3361. let autherKey = self.noteTypeDict["kKMNoteFilterAnnotationAutherKey"] as? Bool
  3362. if pageKey == nil || pageKey == false {
  3363. cell.autherLabel.stringValue = authorString
  3364. cell.autherLabel.isHidden = false
  3365. } else {
  3366. cell.autherLabel.isHidden = true
  3367. }
  3368. let imageView = KMNoteTypeImageView()
  3369. if note.isKind(of: CPDFStampAnnotation.self) {
  3370. let annotation_stamp = note as! CPDFStampAnnotation
  3371. if (annotation_stamp.stampImage() != nil) {
  3372. noteType = SKNStampString
  3373. }
  3374. } else if note.isKind(of: CPDFLineAnnotation.self) {
  3375. let annotation_line = note as! CPDFLineAnnotation
  3376. if annotation_line.startLineStyle == .none && annotation_line.endLineStyle == .none {
  3377. noteType = SKNLine_NoneString
  3378. } else if annotation_line.startLineStyle == .none && annotation_line.endLineStyle == .openArrow {
  3379. noteType = SKNLine_OpenArrowString
  3380. } else {
  3381. noteType = SKNLine_NoneString
  3382. }
  3383. } else if note.isKind(of: CPDFButtonWidgetAnnotation.self) {
  3384. let buttonWidget = note as! CPDFButtonWidgetAnnotation
  3385. if buttonWidget.controlType() == .radioButtonControl {
  3386. noteType = KMAnnotationFormRadioButtonKey
  3387. } else if buttonWidget.controlType() == .checkBoxControl {
  3388. noteType = KMAnnotationFormCheckBoxKey
  3389. } else if buttonWidget.controlType() == .pushButtonControl {
  3390. noteType = KMAnnotationFormActionButtonKey
  3391. }
  3392. } else if note.isKind(of: CPDFTextWidgetAnnotation.self) {
  3393. noteType = KMAnnotationFormTextFieldKey
  3394. } else if note.isKind(of: CPDFChoiceWidgetAnnotation.self) {
  3395. let choiceWidget = note as! CPDFChoiceWidgetAnnotation
  3396. if choiceWidget.isListChoice {
  3397. noteType = KMAnnotationFormListMenuKey
  3398. } else {
  3399. noteType = KMAnnotationFormComboBoxKey
  3400. }
  3401. } else if note.isKind(of: KMTableAnnotation.self) {
  3402. noteType = "Ink_Table"
  3403. } else if note.isKind(of: KMSelfSignAnnotation.self) {
  3404. let selfSignNote = note as! KMSelfSignAnnotation
  3405. if selfSignNote.annotationType == .signFalse {
  3406. noteType = "KMSelfSignTypeFalseActionButtonKey"
  3407. } else if (selfSignNote.annotationType == .signature) {
  3408. noteType = "KMSelfSignTypeTureActionButtonKey"
  3409. } else if (selfSignNote.annotationType == .signCircle) {
  3410. noteType = "KMSelfSignTypeCircleActionButtonKey"
  3411. } else if (selfSignNote.annotationType == .signLine) {
  3412. noteType = "KMSelfSignTypeLineActionButtonKey"
  3413. } else if (selfSignNote.annotationType == .signDot) {
  3414. noteType = "KMSelfSignTypeDotActionButtonKey"
  3415. } else if (selfSignNote.annotationType == .signText) {
  3416. noteType = "KMSelfSignTypeTextActionButtonKey"
  3417. }
  3418. } else if note.isKind(of: CPDFSquareAnnotation.self) {
  3419. noteType = SKNSquareString
  3420. } else if note.isKind(of: CPDFTextAnnotation.self) {
  3421. noteType = SKNNoteString
  3422. } else if note.isKind(of: CPDFFreeTextAnnotation.self) {
  3423. noteType = SKNFreeTextString
  3424. } else if note.isKind(of: CPDFCircleAnnotation.self) {
  3425. noteType = SKNCircleString
  3426. } else if note.isKind(of: CPDFSignatureAnnotation.self) {
  3427. noteType = SKNSignatureString
  3428. } else if note.isKind(of: CPDFInkAnnotation.self) {
  3429. noteType = SKNInkString
  3430. } else if note.isKind(of: CPDFMarkupAnnotation.self) {
  3431. let anno = note as! CPDFMarkupAnnotation
  3432. if anno.markupType() == .highlight {
  3433. noteType = SKNHighlightString
  3434. } else if anno.markupType() == .underline {
  3435. noteType = SKNUnderlineString
  3436. } else if anno.markupType() == .strikeOut {
  3437. noteType = SKNStrikeOutString
  3438. }
  3439. }
  3440. cell.typeImageView.image = imageView.noteTypeImage(withType: noteType, color: noteColor ?? .red)
  3441. cell.typeImageView.isHidden = false
  3442. if note.isKind(of: CPDFMarkupAnnotation.self) {
  3443. cell.isFold = true
  3444. }
  3445. cell.noteContentBox.isHidden = true
  3446. cell.noteImageView.isHidden = true
  3447. cell.foldButton.isHidden = true
  3448. cell.annotationContentLabel.isHidden = false
  3449. cell.noteContentLabel.stringValue = noteString
  3450. cell.contentView.isHidden = false
  3451. cell.contentViewHidden(false)
  3452. if note.isKind(of: CPDFMarkupAnnotation.self) {
  3453. let markup = note as! CPDFMarkupAnnotation
  3454. var contentString = KMBOTAAnnotationTool.fetchText(text: markup.markupContent() ?? "")
  3455. contentString = contentString.replacingOccurrences(of: "\r", with: "")
  3456. contentString = contentString.replacingOccurrences(of: "\n", with: "")
  3457. cell.noteContentLabel.stringValue = contentString
  3458. if(contentString.isEmpty == false) {
  3459. cell.foldButton.isHidden = false
  3460. }
  3461. var attributeStr = NSMutableAttributedString(string: contentString)
  3462. if (markup.markupType() == .highlight) {
  3463. attributeStr.addAttribute(.backgroundColor, value: noteColor as Any, range: NSMakeRange(0, contentString.count))
  3464. } else if (markup.markupType() == .strikeOut) {
  3465. attributeStr.addAttribute(.strikethroughStyle, value: NSUnderlineStyle.single.rawValue, range: NSMakeRange(0, contentString.count))
  3466. attributeStr.addAttribute(.strikethroughColor, value: noteColor as Any, range: NSMakeRange(0, contentString.count))
  3467. } else if (markup.markupType() == .underline) {
  3468. attributeStr.addAttribute(.underlineStyle, value: NSUnderlineStyle.single.rawValue, range: NSMakeRange(0, contentString.count))
  3469. attributeStr.addAttribute(.underlineColor, value: noteColor as Any, range: NSMakeRange(0, contentString.count))
  3470. }
  3471. cell.annotationContentLabel.attributedStringValue = attributeStr
  3472. if let data = model?.foldType, data == .unfold {
  3473. cell.isFold = false
  3474. } else if let data = model?.foldType, data == .fold {
  3475. cell.isFold = true
  3476. } else {
  3477. if self.allFoldNotes.isEmpty == false && self.allFoldNotes.contains(note) {
  3478. cell.isFold = false
  3479. } else {
  3480. cell.isFold = true
  3481. }
  3482. }
  3483. // noteString.isEmpty == false &&
  3484. if (contentString.isEmpty == false) {
  3485. cell.contentViewHidden(false)
  3486. } else {
  3487. cell.contentViewHidden(true)
  3488. }
  3489. } else if note.isKind(of: CPDFLineAnnotation.self) || noteType == SKNSquareString || noteType == SKNCircleString || noteType == SKNInkString {
  3490. cell.annotationContentLabel.stringValue = noteString
  3491. if (noteString.isEmpty == false) {
  3492. cell.contentViewHidden(false)
  3493. } else {
  3494. cell.contentViewHidden(true)
  3495. }
  3496. } else if note.isKind(of: CPDFStampAnnotation.self) {
  3497. if note.isKind(of: KMSelfSignAnnotation.self) {
  3498. let newAnnotation = note as! KMSelfSignAnnotation
  3499. let type = newAnnotation.annotationType
  3500. var returnString = ""
  3501. if (type == .signFalse) {
  3502. returnString = KMLocalizedString("X", nil)
  3503. } else if (type == .signature) {
  3504. returnString = KMLocalizedString("Check mark", nil)
  3505. } else if (type == .signCircle) {
  3506. returnString = KMLocalizedString("Circle", nil)
  3507. } else if (type == .signLine) {
  3508. returnString = KMLocalizedString("Line", nil)
  3509. } else if (type == .signDot) {
  3510. returnString = KMLocalizedString("Dot", nil)
  3511. } else if (type == .signText) {
  3512. returnString = KMLocalizedString("Text", nil)
  3513. }
  3514. cell.annotationContentLabel.stringValue = returnString
  3515. } else {
  3516. cell.annotationContentLabel.isHidden = true
  3517. cell.noteImageView.isHidden = false
  3518. let anno = note as! CPDFStampAnnotation
  3519. cell.noteImageView.image = anno.stampImage()
  3520. }
  3521. } else if note.isKind(of: CPDFTextAnnotation.self) {
  3522. cell.foldButton.isHidden = false
  3523. if noteString.isEmpty {
  3524. cell.annotationContentLabel.stringValue = noteTextString
  3525. }else{
  3526. cell.annotationContentLabel.stringValue = noteString
  3527. cell.noteContentLabel.stringValue = noteTextString
  3528. }
  3529. if (self.foldType == .unfold) {
  3530. cell.isFold = false
  3531. } else if (self.foldType == .fold) {
  3532. cell.isFold = true
  3533. } else {
  3534. if self.allFoldNotes.isEmpty == false && self.allFoldNotes.contains(note) {
  3535. cell.isFold = false
  3536. } else {
  3537. cell.isFold = true
  3538. }
  3539. }
  3540. if (noteString.isEmpty == false || noteTextString.isEmpty == false) {
  3541. cell.contentView.isHidden = false
  3542. } else {
  3543. cell.contentView.isHidden = true
  3544. cell.contentViewHidden(true)
  3545. }
  3546. // noteString.isEmpty == false &&
  3547. if (noteTextString.isEmpty == false) {
  3548. cell.foldButton.isHidden = false
  3549. }else{
  3550. cell.foldButton.isHidden = true
  3551. }
  3552. } else {
  3553. cell.annotationContentLabel.stringValue = noteString
  3554. cell.imageViewHeightConstraint.constant = cell.contentView.frame.size.height
  3555. if (noteString.isEmpty == false) {
  3556. cell.contentViewHidden(false)
  3557. } else {
  3558. cell.contentViewHidden(true)
  3559. }
  3560. }
  3561. cell.autherLayoutConstraint.constant = cell.autherLabel.isHidden ? -(cell.autherLabel.bounds.size.width) + 10.0 : 10.0
  3562. cell.typeImageViewLayoutConstraint.constant = cell.typeImageView.isHidden ? -(cell.typeImageView.bounds.size.width) : 0.0
  3563. cell.contentBoxLayoutConstraint.constant = cell.noteContentBox.isHidden ? -(cell.noteContentBox.bounds.size.height+8.0) : 8.0
  3564. if note.isKind(of: CPDFStampAnnotation.self) && note.isKind(of: KMSelfSignAnnotation.self) == false {
  3565. } else {
  3566. if note.isKind(of: CPDFMarkupAnnotation.self) {
  3567. if (!cell.isFold) {
  3568. self.allFoldNotes.append(note)
  3569. } else {
  3570. cell.imageViewHeightConstraint.constant = 18.0 + 8
  3571. }
  3572. } else {
  3573. cell.imageViewHeightConstraint.constant = 18.0 + 8
  3574. }
  3575. }
  3576. cell.isUnFoldNote = { [unowned self] cellNote, isUnfold in
  3577. model?.foldType = isUnfold ? .unfold : .fold
  3578. // let COLUMN_INDENTATION: CGFloat = 16
  3579. // if let _cell = tableColumn?.dataCell as? NSCell {
  3580. // _cell.objectValue = cell.annotationContentLabel.attributedStringValue
  3581. // let bound = NSMakeRect(0.0, 0.0, fmax(10.0, NSWidth(outlineView.frame) - COLUMN_INDENTATION - outlineView.indentationPerLevel-40), CGFLOAT_MAX)
  3582. // let height = _cell.cellSize(forBounds: bound).height
  3583. // KMPrint(height)
  3584. // }
  3585. if cellNote is CPDFMarkupAnnotation {
  3586. // let content = cell.annotationContentLabel.attributedStringValue
  3587. // let bound = content.string.boundingRect(with: NSMakeSize(150, CGFLOAT_MAX), options: [.usesFontLeading, .usesLineFragmentOrigin], attributes: nil)
  3588. // KMPrint(bound)
  3589. // 154 34 273
  3590. // 273 - 188 = 85 noteContentHeightConstraint maltlineLabelLayoutConstraint
  3591. model?.foldH = isUnfold ? 30 : (cell.noteContentHeightConstraint.constant + cell.maltlineLabelLayoutConstraint.constant + 85)
  3592. }
  3593. if (isUnfold) {
  3594. if (self.allFoldNotes.contains(note)) {
  3595. self.allFoldNotes.removeObject(note)
  3596. }
  3597. if (self.allFoldNotes.count == 0) {
  3598. self.foldType = .fold
  3599. } else {
  3600. self.foldType = .none
  3601. }
  3602. } else {
  3603. if self.allFoldNotes.contains(note) == false {
  3604. self.allFoldNotes.append(note)
  3605. }
  3606. // if (rightSideController.allFoldNotes.count == rightSideController.canFoldNotes.count) {
  3607. self.foldType = .unfold
  3608. // } else {
  3609. // self.foldType = KMFoldAllAnnotationType_None;
  3610. // }
  3611. }
  3612. }
  3613. return cell
  3614. }
  3615. return nil
  3616. }
  3617. func outlineView(_ outlineView: NSOutlineView, heightOfRowByItem item: Any) -> CGFloat {
  3618. if outlineView.isEqual(self.noteOutlineView) {
  3619. if item is KMBOTAAnnotationItem {
  3620. let model = item as! KMBOTAAnnotationItem
  3621. if model.foldType == .fold {
  3622. return model.foldH
  3623. }
  3624. return KMBOTAAnnotationTool.fetchCellHeight(annotation: (item as? KMBOTAAnnotationItem)!.annotation!, maxSize: CGSize(width: 260+40 - 16, height: 1000))
  3625. }
  3626. return 30
  3627. } else if outlineView.isEqual(self.tocOutlineView) {
  3628. if item is CPDFOutline {
  3629. let tempItem = item as! CPDFOutline
  3630. let string: NSString = tempItem.label as NSString
  3631. let paragraphStyle = NSMutableParagraphStyle()
  3632. paragraphStyle.lineHeightMultiple = 1.32
  3633. paragraphStyle.alignment = .left
  3634. let attributes = [NSAttributedString.Key.paragraphStyle: paragraphStyle,
  3635. NSAttributedString.Key.font : NSFont.SFProTextRegularFont(14.0)]
  3636. let size = string.boundingRect(with: CGSizeMake(outlineView.frame.size.width - 30, 200), options: NSString.DrawingOptions(rawValue: 3), attributes: attributes)
  3637. return max(40, size.height + 16)
  3638. }
  3639. return 40
  3640. }
  3641. return outlineView.rowHeight
  3642. }
  3643. func outlineView(_ outlineView: NSOutlineView, isItemExpandable item: Any) -> Bool {
  3644. if outlineView.isEqual(self.tocOutlineView) {
  3645. // var _item: CPDFOutline?
  3646. // if item == nil && self.listView.document.isLocked == false {
  3647. // _item = self.listView.document.outlineRoot()
  3648. // }
  3649. if let data = item as? String, data == "Bookmarks" {
  3650. return true
  3651. } else if item is CPDFOutline {
  3652. return ((item as! CPDFOutline).numberOfChildren ?? 0) != 0
  3653. } else if item is CPDFBookmark {
  3654. return false
  3655. }
  3656. if (self.isSearchOutlineMode) {
  3657. // return [self subOutLineContainString:self.leftSideController.outlineSearchField.stringValue rootOutline:(PDFOutline *)item];
  3658. } else {
  3659. // return ((_item?.numberOfChildren ?? 0) != 0)
  3660. }
  3661. } else if outlineView.isEqual(to: self.noteOutlineView) {
  3662. // return [item hasNoteText];
  3663. return false
  3664. }
  3665. return false
  3666. }
  3667. func outlineView(_ outlineView: NSOutlineView, rowViewForItem item: Any) -> NSTableRowView? {
  3668. if outlineView.isEqual(self.tocOutlineView) {
  3669. let itemView = KMBotaTableRowView()
  3670. return itemView
  3671. } else if outlineView.isEqual(self.noteOutlineView) {
  3672. let itemView = KMBotaTableRowView()
  3673. return itemView;
  3674. }
  3675. return nil
  3676. }
  3677. func outlineViewSelectionDidChange(_ notification: Notification) {
  3678. if self.tocOutlineView.isEqual(to: notification.object) {
  3679. // if ([[notification object] isEqual:leftSideController.tocOutlineView] && (mwcFlags.updatingOutlineSelection == 0)){
  3680. // mwcFlags.updatingOutlineSelection = 1;
  3681. self.goToSelectedOutlineItem(nil)
  3682. // mwcFlags.updatingOutlineSelection = 0;
  3683. // if ([self interactionMode] == SKPresentationMode && [[NSUserDefaults standardUserDefaults] boolForKey:SKAutoHidePresentationContentsKey])
  3684. // [self hideLeftSideWindow];
  3685. }
  3686. }
  3687. func outlineViewItemDidExpand(_ notification: Notification) {
  3688. if self.tocOutlineView.isEqual(to: notification.object) {
  3689. self.updateOutlineSelection()
  3690. }
  3691. }
  3692. func outlineViewItemDidCollapse(_ notification: Notification) {
  3693. if self.tocOutlineView.isEqual(to: notification.object) {
  3694. self.updateOutlineSelection()
  3695. }
  3696. }
  3697. func outlineView(_ outlineView: NSOutlineView, validateDrop info: NSDraggingInfo, proposedItem item: Any?, proposedChildIndex index: Int) -> NSDragOperation {
  3698. var dragOp = NSDragOperation(rawValue: 0)
  3699. if outlineView.isEqual(to: self.noteOutlineView) {
  3700. let pboard = info.draggingPasteboard
  3701. if pboard.canReadObject(forClasses: [NSColor.self], options: [:]) && index == NSOutlineViewDropOnItemIndex {
  3702. // if ([pboard canReadObjectForClasses:[NSArray arrayWithObject:[NSColor class]] options:[NSDictionary dictionary]] &&
  3703. // anIndex == NSOutlineViewDropOnItemIndex && [(PDFAnnotation *)item type] != nil)
  3704. // dragOp = NSDragOperationEvery;
  3705. if let note = item as? CPDFAnnotation, note.type != nil {
  3706. dragOp = .every
  3707. }
  3708. }
  3709. } else if outlineView.isEqual(to: self.tocOutlineView) {
  3710. if (index == -1) {
  3711. dragOp = NSDragOperation(rawValue: 0)
  3712. } else {
  3713. dragOp = .move
  3714. }
  3715. }
  3716. return dragOp
  3717. }
  3718. func outlineView(_ outlineView: NSOutlineView, writeItems items: [Any], to pasteboard: NSPasteboard) -> Bool {
  3719. if outlineView.isEqual(to: self.tocOutlineView) {
  3720. if (self.tocOutlineView.selectedRowIndexes.count > 1) {
  3721. return false
  3722. }
  3723. let tIndex = IndexSet(integer: self.tocOutlineView.clickedRow)
  3724. self.tocOutlineView.deselectRow(tIndex.first ?? 0)
  3725. self._dragPDFOutline = items.first as? CPDFOutline
  3726. let set = IndexSet(integer: 0)
  3727. let zNSIndexSetData = NSKeyedArchiver.archivedData(withRootObject: set)
  3728. // [pasteboard declareTypes:[NSArray arrayWithObject:kKMPDFViewOutlineDragDataType] owner:self];
  3729. pasteboard.declareTypes([.localDraggedTypes], owner: self)
  3730. pasteboard.setData(zNSIndexSetData, forType: .localDraggedTypes)
  3731. return true
  3732. }
  3733. return false
  3734. }
  3735. func outlineView(_ outlineView: NSOutlineView, acceptDrop info: NSDraggingInfo, item: Any?, childIndex index: Int) -> Bool {
  3736. if outlineView.isEqual(to: self.noteOutlineView) {
  3737. let pboard = info.draggingPasteboard
  3738. if pboard.canReadObject(forClasses: [NSColor.self], options: [:]) {
  3739. // [item setColor:[NSColor colorFromPasteboard:pboard] alternate:isAlt updateDefaults:isShift];
  3740. let isShift = NSEvent.modifierFlags.contains(.shift)
  3741. let isAlt = NSEvent.modifierFlags.contains(.option)
  3742. guard let note = item as? CPDFAnnotation else {
  3743. NSSound.beep()
  3744. return false
  3745. }
  3746. if let color = NSColor(from: pboard) {
  3747. note.setColor(color, alternate: isAlt, updateDefaults: isShift)
  3748. return true
  3749. }
  3750. return false
  3751. }
  3752. } else if outlineView.isEqual(to: self.tocOutlineView) {
  3753. if (index < 0) {
  3754. return false
  3755. }
  3756. // guard let outline = item as? CPDFOutline else {
  3757. // return false
  3758. // }
  3759. let outline = item as? CPDFOutline
  3760. guard let dragPDFOL = self._dragPDFOutline else {
  3761. return false
  3762. }
  3763. //root,drag item to root
  3764. if ((outline?.parent == nil)) {
  3765. //fetch root
  3766. var root = dragPDFOL
  3767. while (root.parent != nil) {
  3768. root = root.parent
  3769. }
  3770. if dragPDFOL.parent.isEqual(to: root) {
  3771. if dragPDFOL.index > index {
  3772. self.dragPDFOutline(self._dragPDFOutline, toIndex: index, newParentOutline: root)
  3773. } else {
  3774. self.dragPDFOutline(self._dragPDFOutline, toIndex: index-1, newParentOutline: root)
  3775. }
  3776. } else {
  3777. self.dragPDFOutline(self._dragPDFOutline, toIndex: index, newParentOutline: root)
  3778. }
  3779. } else {
  3780. //在同一个层级内移动
  3781. if dragPDFOL.parent.isEqual(to: item) {
  3782. if (dragPDFOL.index > 0) {
  3783. if dragPDFOL.index > index {
  3784. self.dragPDFOutline(self._dragPDFOutline, toIndex: index, newParentOutline: outline)
  3785. }else{
  3786. self.dragPDFOutline(self._dragPDFOutline, toIndex: index-1, newParentOutline: outline)
  3787. }
  3788. } else {
  3789. return false
  3790. }
  3791. } else {
  3792. var tOutlline: CPDFOutline? = outline
  3793. var isContains = false
  3794. while (tOutlline != nil) {
  3795. if tOutlline!.isEqual(to: self._dragPDFOutline) {
  3796. isContains = true
  3797. break
  3798. }
  3799. tOutlline = tOutlline?.parent
  3800. }
  3801. if (!isContains) {
  3802. self.dragPDFOutline(self._dragPDFOutline, toIndex: index, newParentOutline: outline)
  3803. }
  3804. }
  3805. }
  3806. return true
  3807. }
  3808. return false
  3809. }
  3810. /*
  3811. #pragma mark NSOutlineView datasource protocol
  3812. - (void)outlineView:(NSOutlineView *)ov setObjectValue:(id)object forTableColumn:(NSTableColumn *)tableColumn byItem:(id)item{
  3813. if ([ov isEqual:rightSideController.noteOutlineView]) {
  3814. PDFAnnotation *note = item;
  3815. if ([note type]) {
  3816. if ([[tableColumn identifier] isEqualToString:NOTE_COLUMNID]) {
  3817. if ([(object ?: @"") isEqualToString:([note string] ?: @"")] == NO)
  3818. [note setString:object];
  3819. } else if ([[tableColumn identifier] isEqualToString:AUTHOR_COLUMNID]) {
  3820. if ([(object ?: @"") isEqualToString:([note userName] ?: @"")] == NO)
  3821. [note setUserName:object];
  3822. }
  3823. }
  3824. }
  3825. }
  3826. #pragma mark NSOutlineView delegate protocol
  3827. - (NSCell *)outlineView:(NSOutlineView *)ov dataCellForTableColumn:(NSTableColumn *)tableColumn item:(id)item {
  3828. if ([ov isEqual:rightSideController.noteOutlineView] && tableColumn == nil && [(PDFAnnotation *)item type] == nil) {
  3829. return [[ov tableColumnWithIdentifier:NOTE_COLUMNID] dataCellForRow:[ov rowForItem:item]];
  3830. }
  3831. return [tableColumn dataCellForRow:[ov rowForItem:item]];
  3832. }
  3833. - (void)outlineView:(NSOutlineView *)ov willDisplayOutlineCell:(id)cell forTableColumn:(NSTableColumn *)tableColumn item:(id)item {
  3834. if ([ov isEqual:leftSideController.tocOutlineView] &&
  3835. [ov selectionHighlightStyle] == NSTableViewSelectionHighlightStyleRegular &&
  3836. [ov isRowSelected:[ov rowForItem:item]]) {
  3837. [cell setBackgroundStyle:NSBackgroundStyleLowered];
  3838. }
  3839. }
  3840. - (BOOL)outlineView:(NSOutlineView *)ov shouldEditTableColumn:(NSTableColumn *)tableColumn item:(id)item{
  3841. if ([ov isEqual:rightSideController.noteOutlineView]) {
  3842. if (tableColumn == nil) {
  3843. if ([pdfView hideNotes] == NO && [[(SKNoteText *)item note] isNote]) {
  3844. PDFAnnotation *annotation = [(SKNoteText *)item note];
  3845. [pdfView scrollAnnotationToVisible:annotation];
  3846. [pdfView setActiveAnnotation:annotation];
  3847. [self showNote:annotation];
  3848. SKNoteWindowController *noteController = (SKNoteWindowController *)[self windowControllerForNote:annotation];
  3849. [[noteController window] makeFirstResponder:[noteController textView]];
  3850. [[noteController textView] selectAll:nil];
  3851. }
  3852. return NO;
  3853. } else if ([[tableColumn identifier] isEqualToString:NOTE_COLUMNID] || [[tableColumn identifier] isEqualToString:AUTHOR_COLUMNID]) {
  3854. return YES;
  3855. }
  3856. }
  3857. return NO;
  3858. }
  3859. - (void)outlineView:(NSOutlineView *)ov didClickTableColumn:(NSTableColumn *)tableColumn {
  3860. if ([ov isEqual:rightSideController.noteOutlineView]) {
  3861. NSTableColumn *oldTableColumn = [ov highlightedTableColumn];
  3862. NSTableColumn *newTableColumn = ([NSEvent modifierFlags] & NSEventModifierFlagCommand) ? nil : tableColumn;
  3863. NSMutableArray *sortDescriptors = nil;
  3864. BOOL ascending = YES;
  3865. if ([oldTableColumn isEqual:newTableColumn]) {
  3866. sortDescriptors = [[[rightSideController.noteArrayController sortDescriptors] mutableCopy] autorelease];
  3867. [sortDescriptors replaceObjectAtIndex:0 withObject:[[sortDescriptors firstObject] reversedSortDescriptor]];
  3868. ascending = [[sortDescriptors firstObject] ascending];
  3869. } else {
  3870. NSString *tcID = [newTableColumn identifier];
  3871. NSSortDescriptor *pageIndexSortDescriptor = [[[NSSortDescriptor alloc] initWithKey:SKNPDFAnnotationPageIndexKey ascending:ascending] autorelease];
  3872. NSSortDescriptor *boundsSortDescriptor = [[[NSSortDescriptor alloc] initWithKey:SKPDFAnnotationBoundsOrderKey ascending:ascending selector:@selector(compare:)] autorelease];
  3873. sortDescriptors = [NSMutableArray arrayWithObjects:pageIndexSortDescriptor, boundsSortDescriptor, nil];
  3874. if ([tcID isEqualToString:TYPE_COLUMNID]) {
  3875. [sortDescriptors insertObject:[[[NSSortDescriptor alloc] initWithKey:SKNPDFAnnotationTypeKey ascending:YES selector:@selector(noteTypeCompare:)] autorelease] atIndex:0];
  3876. } else if ([tcID isEqualToString:COLOR_COLUMNID]) {
  3877. [sortDescriptors insertObject:[[[NSSortDescriptor alloc] initWithKey:SKNPDFAnnotationColorKey ascending:YES selector:@selector(colorCompare:)] autorelease] atIndex:0];
  3878. } else if ([tcID isEqualToString:NOTE_COLUMNID]) {
  3879. [sortDescriptors insertObject:[[[NSSortDescriptor alloc] initWithKey:SKNPDFAnnotationStringKey ascending:YES selector:@selector(localizedCaseInsensitiveNumericCompare:)] autorelease] atIndex:0];
  3880. } else if ([tcID isEqualToString:AUTHOR_COLUMNID]) {
  3881. [sortDescriptors insertObject:[[[NSSortDescriptor alloc] initWithKey:SKNPDFAnnotationUserNameKey ascending:YES selector:@selector(localizedCaseInsensitiveNumericCompare:)] autorelease] atIndex:0];
  3882. } else if ([tcID isEqualToString:DATE_COLUMNID]) {
  3883. [sortDescriptors insertObject:[[[NSSortDescriptor alloc] initWithKey:SKNPDFAnnotationModificationDateKey ascending:YES] autorelease] atIndex:0];
  3884. }
  3885. if (oldTableColumn)
  3886. [ov setIndicatorImage:nil inTableColumn:oldTableColumn];
  3887. [ov setHighlightedTableColumn:newTableColumn];
  3888. }
  3889. [rightSideController.noteArrayController setSortDescriptors:sortDescriptors];
  3890. if (newTableColumn)
  3891. [ov setIndicatorImage:[NSImage imageNamed:ascending ? @"NSAscendingSortIndicator" : @"NSDescendingSortIndicator"]
  3892. inTableColumn:newTableColumn];
  3893. [ov reloadData];
  3894. }
  3895. }
  3896. - (NSString *)outlineView:(NSOutlineView *)ov toolTipForCell:(NSCell *)cell rect:(NSRectPointer)rect tableColumn:(NSTableColumn *)tableColumn item:(id)item mouseLocation:(NSPoint)mouseLocation {
  3897. if ([ov isEqual:rightSideController.noteOutlineView] &&
  3898. (tableColumn == nil || [[tableColumn identifier] isEqualToString:NOTE_COLUMNID])) {
  3899. return [item string];
  3900. }
  3901. return @"";
  3902. }
  3903. - (void)outlineViewColumnDidResize:(NSNotification *)notification{
  3904. if (mwcFlags.autoResizeNoteRows &&
  3905. [[notification object] isEqual:rightSideController.noteOutlineView] &&
  3906. [[[[notification userInfo] objectForKey:@"NSTableColumn"] identifier] isEqualToString:NOTE_COLUMNID] &&
  3907. [(SKScrollView *)[[notification object] enclosingScrollView] isResizingSubviews] == NO) {
  3908. [rowHeights removeAllFloats];
  3909. [rightSideController.noteOutlineView noteHeightOfRowsWithIndexesChanged:[NSIndexSet indexSetWithIndexesInRange:NSMakeRange(0, [rightSideController.noteOutlineView numberOfRows])]];
  3910. }
  3911. }
  3912. - (void)updateSelectRowHeight{
  3913. CGFloat rowHeight = 0;
  3914. PDFOutline *outline = [leftSideController.tocOutlineView itemAtRow:leftSideController.tocOutlineView.selectedRow];
  3915. if (!outline){
  3916. return;
  3917. }
  3918. NSMutableAttributedString *attributedString = [[[NSMutableAttributedString alloc]init] autorelease];
  3919. NSDictionary *dictAttr1 = @{NSForegroundColorAttributeName:[KMAppearance KMColor_Layout_H0]};
  3920. NSAttributedString *attr1 = [[NSAttributedString alloc]initWithString:outline.label attributes:dictAttr1];
  3921. [attributedString appendAttributedString:attr1];
  3922. NSInteger *row = [leftSideController.tocOutlineView selectedRow];
  3923. NSTableCellView *viewS = [leftSideController.tocOutlineView viewAtColumn:0 row:row makeIfNecessary:YES];
  3924. NSTableColumn *tableColumn = [leftSideController.tocOutlineView tableColumnWithIdentifier:LABEL_COLUMNID];
  3925. // id cell = [tableColumn dataCell];
  3926. id cell = [tableColumn dataCellForRow:row];
  3927. [cell setObjectValue:attributedString];
  3928. CGFloat w = leftSideController.view.frame.size.width - 86;//[tableColumn width] > 260 ? [tableColumn width] : 260;
  3929. NSInteger num = [self getNum:outline];
  3930. CGFloat gap = [leftSideController.tocOutlineView indentationPerLevel];
  3931. rowHeight = [cell cellSizeForBounds:NSMakeRect(0.0, 0.0, w - (num > 0?16:0) - gap*num, CGFLOAT_MAX)].height;
  3932. rowHeight = fmax(rowHeight, [leftSideController.tocOutlineView rowHeight]) + 25;
  3933. [rowHeights setFloat:rowHeight forKey:outline];
  3934. if (@available(macOS 10.13, *)) {
  3935. } else {
  3936. rowHeight = 40.0;
  3937. }
  3938. CGRect fram = viewS.frame;
  3939. viewS.frame = CGRectMake(fram.origin.x, fram.origin.y, fram.size.width, rowHeight);
  3940. [leftSideController.tocOutlineView reloadData];
  3941. }
  3942. - (NSInteger)getNum:(PDFOutline *)ol{
  3943. NSInteger num = 0;
  3944. PDFOutline *outLine = [ol parent];
  3945. do {
  3946. outLine = [outLine parent];
  3947. if (outLine){
  3948. num ++;
  3949. }
  3950. } while (outLine);
  3951. return num;
  3952. }
  3953. - (void)sizeOutlineViewToContents:(NSOutlineView*) outlineView;
  3954. {
  3955. NSInteger rowCount = [outlineView numberOfRows];
  3956. for (NSInteger i = 0; i < rowCount; i++){
  3957. CGFloat rowHeight = 0;
  3958. PDFOutline *outline = [leftSideController.tocOutlineView itemAtRow:i];
  3959. if (!outline){
  3960. continue;
  3961. }
  3962. NSMutableAttributedString *attributedString = [[[NSMutableAttributedString alloc]init] autorelease];
  3963. NSDictionary *dictAttr1 = @{NSForegroundColorAttributeName:[KMAppearance KMColor_Layout_H0]};
  3964. NSAttributedString *attr1 = [[NSAttributedString alloc]initWithString:outline.label attributes:dictAttr1];
  3965. [attributedString appendAttributedString:attr1];
  3966. // NSTableCellView *viewS = [leftSideController.tocOutlineView viewAtColumn:0 row:i makeIfNecessary:YES];
  3967. NSTableColumn *tableColumn = [leftSideController.tocOutlineView tableColumnWithIdentifier:LABEL_COLUMNID];
  3968. // id cell = [tableColumn dataCell];
  3969. id cell = [tableColumn dataCellForRow:i];
  3970. [cell setObjectValue:attributedString];
  3971. CGFloat w = leftSideController.view.frame.size.width - 86;//[tableColumn width] > 260 ? [tableColumn width] : 260;
  3972. NSInteger num = [self getNum:outline];
  3973. CGFloat gap = [leftSideController.tocOutlineView indentationPerLevel];
  3974. rowHeight = [cell cellSizeForBounds:NSMakeRect(0.0, 0.0, w - (num > 0?16:0) - gap*num, CGFLOAT_MAX)].height;
  3975. rowHeight = fmax(rowHeight, [leftSideController.tocOutlineView rowHeight]) + 25;
  3976. [rowHeights setFloat:rowHeight forKey:outline];
  3977. if (@available(macOS 10.13, *)) {
  3978. } else {
  3979. rowHeight = 40.0;
  3980. }
  3981. // CGRect fram = viewS.frame;
  3982. // viewS.frame = CGRectMake(fram.origin.x, fram.origin.y, fram.size.width, rowHeight);
  3983. }
  3984. [leftSideController.tocOutlineView reloadData];
  3985. }
  3986. */
  3987. func noteItems(_ items: NSArray) -> NSArray {
  3988. var noteItems = NSMutableArray()
  3989. for item in items {
  3990. guard let anno = (item as? KMBOTAAnnotationItem)?.annotation else {
  3991. continue
  3992. }
  3993. if anno.type == nil {
  3994. // item = [(SKNoteText *)item note];
  3995. }
  3996. if noteItems.contains(anno) == false {
  3997. noteItems.add(anno)
  3998. }
  3999. }
  4000. return noteItems
  4001. }
  4002. /*
  4003. - (NSArray *)outlineView:(NSOutlineView *)ov typeSelectHelperSelectionStrings:(SKTypeSelectHelper *)typeSelectHelper {
  4004. if ([ov isEqual:rightSideController.noteOutlineView]) {
  4005. NSInteger i, count = [rightSideController.noteOutlineView numberOfRows];
  4006. NSMutableArray *texts = [NSMutableArray arrayWithCapacity:count];
  4007. for (i = 0; i < count; i++) {
  4008. id item = [rightSideController.noteOutlineView itemAtRow:i];
  4009. NSString *string = [item string];
  4010. [texts addObject:string ?: @""];
  4011. }
  4012. return texts;
  4013. } else if ([ov isEqual:leftSideController.tocOutlineView]) {
  4014. NSInteger i, count = [leftSideController.tocOutlineView numberOfRows];
  4015. NSMutableArray *array = [NSMutableArray arrayWithCapacity:count];
  4016. for (i = 0; i < count; i++)
  4017. [array addObject:[[(PDFOutline *)[leftSideController.tocOutlineView itemAtRow:i] label] lossyStringUsingEncoding:NSASCIIStringEncoding]];
  4018. return array;
  4019. }
  4020. return nil;
  4021. }
  4022. - (void)outlineView:(NSOutlineView *)ov typeSelectHelper:(SKTypeSelectHelper *)typeSelectHelper didFailToFindMatchForSearchString:(NSString *)searchString {
  4023. if ([ov isEqual:rightSideController.noteOutlineView]) {
  4024. [statusBar setRightStringValue:[NSString stringWithFormat:NSLocalizedString(@"No match: \"%@\"", @"Status message"), searchString]];
  4025. } else if ([ov isEqual:leftSideController.tocOutlineView]) {
  4026. [statusBar setLeftStringValue:[NSString stringWithFormat:NSLocalizedString(@"No match: \"%@\"", @"Status message"), searchString]];
  4027. }
  4028. }
  4029. - (void)outlineView:(NSOutlineView *)ov typeSelectHelper:(SKTypeSelectHelper *)typeSelectHelper updateSearchString:(NSString *)searchString {
  4030. if ([typeSelectHelper isEqual:[leftSideController.thumbnailTableView typeSelectHelper]] || [typeSelectHelper isEqual:[pdfView typeSelectHelper]]) {
  4031. if (searchString)
  4032. [statusBar setLeftStringValue:[NSString stringWithFormat:NSLocalizedString(@"Go to page: %@", @"Status message"), searchString]];
  4033. else
  4034. [self updateLeftStatus];
  4035. } else if ([typeSelectHelper isEqual:[rightSideController.noteOutlineView typeSelectHelper]]) {
  4036. if (searchString)
  4037. [statusBar setRightStringValue:[NSString stringWithFormat:NSLocalizedString(@"Finding note: \"%@\"", @"Status message"), searchString]];
  4038. else
  4039. [self updateRightStatus];
  4040. } else if ([typeSelectHelper isEqual:[leftSideController.tocOutlineView typeSelectHelper]]) {
  4041. if (searchString)
  4042. [statusBar setLeftStringValue:[NSString stringWithFormat:NSLocalizedString(@"Finding: \"%@\"", @"Status message"), searchString]];
  4043. else
  4044. [self updateLeftStatus];
  4045. }
  4046. }
  4047. */
  4048. }
  4049. // MARK: - KMCustomOutlineViewDelegate, KMCustomOutlineViewDataSource
  4050. extension KMLeftSideViewController: KMCustomOutlineViewDelegate, KMCustomOutlineViewDataSource {
  4051. func outlineView(_ anOutlineView: NSOutlineView, canDeleteItems items: [Any]) -> Bool {
  4052. if anOutlineView.isEqual(to: self.noteOutlineView) {
  4053. return self.listView.hideNotes == false && items.count > 0
  4054. } else if anOutlineView.isEqual(to: self.tocOutlineView) {
  4055. return items.count > 0
  4056. }
  4057. return false
  4058. }
  4059. func outlineView(_ anOutlineView: NSOutlineView, deleteItems items: [Any]) {
  4060. if anOutlineView.isEqual(to: self.noteOutlineView) {
  4061. if (items.isEmpty) {
  4062. return
  4063. }
  4064. for item in self.noteItems(items as NSArray) {
  4065. guard let anno = item as? CPDFAnnotation else {
  4066. continue
  4067. }
  4068. self.listView.remove(anno)
  4069. }
  4070. self.listView.undoManager?.setActionName(KMLocalizedString("Remove Note", "Undo action name"))
  4071. self.reloadAnnotation()
  4072. self.annotationSort(sortArray: [])
  4073. self.noteOutlineView.reloadData()
  4074. } else if anOutlineView.isEqual(to: self.tocOutlineView) {
  4075. self.outlineContextMenuItemClicked_RemoveEntry(nil)
  4076. }
  4077. }
  4078. func outlineView(_ anOutlineView: NSOutlineView, canCopyItems items: [Any]) -> Bool {
  4079. if anOutlineView.isEqual(to: self.noteOutlineView) {
  4080. if (items.count == 1) {
  4081. // PDFAnnotation *annotation = [[self noteItems:items] lastObject];
  4082. // if ([annotation isKindOfClass:[PDFAnnotationStamp class]] ||
  4083. // [annotation isKindOfClass:[PDFAnnotationLink class]]) {
  4084. // return NO;
  4085. // }
  4086. }
  4087. return items.count > 0
  4088. }
  4089. return false
  4090. }
  4091. func outlineView(_ anOutlineView: NSOutlineView, copyItems items: [Any]) {
  4092. if anOutlineView.isEqual(to: self.noteOutlineView) && items.isEmpty == false {
  4093. let pboard = NSPasteboard.general
  4094. var copiedItems: [Any] = []
  4095. var attrString = NSMutableAttributedString()
  4096. var isAttributed = false
  4097. var item: AnyObject?
  4098. // for (item in [self noteItems:items]) {
  4099. // if ([item isMovable])
  4100. // [copiedItems addObject:item];
  4101. // }
  4102. // for (item in items) {
  4103. // if ([attrString length])
  4104. // [attrString replaceCharactersInRange:NSMakeRange([attrString length], 0) withString:@"\n\n"];
  4105. // if ([(PDFAnnotation *)item type] == nil && [[(SKNoteText *)item note] isNote]) {
  4106. // [attrString appendAttributedString:[(SKNoteText *)item text]];
  4107. // isAttributed = YES;
  4108. // } else {
  4109. // [attrString replaceCharactersInRange:NSMakeRange([attrString length], 0) withString:[item string] ?: @""];
  4110. // }
  4111. // }
  4112. //
  4113. // [pboard clearContents];
  4114. // if (isAttributed)
  4115. // [pboard writeObjects:[NSArray arrayWithObjects:attrString, nil]];
  4116. // else
  4117. // [pboard writeObjects:[NSArray arrayWithObjects:[attrString string], nil]];
  4118. // if ([copiedItems count] > 0)
  4119. // [pboard writeObjects:copiedItems];
  4120. }
  4121. }
  4122. }
  4123. // MARK: - KMTocOutlineViewDelegate
  4124. extension KMLeftSideViewController: KMTocOutlineViewDelegate {
  4125. // func outlineView(_ anOutlineView: NSOutlineView, highlightLevelForRow row: Int) -> Int {
  4126. // if ([ov isEqual:leftSideController.tocOutlineView]) {
  4127. // NSInteger numRows = [ov numberOfRows];
  4128. // NSUInteger firstPage = [[[ov itemAtRow:row] page] pageIndex];
  4129. // NSUInteger lastPage = row + 1 < numRows ? [[[ov itemAtRow:row + 1] page] pageIndex] : [[self pdfDocument] pageCount];
  4130. // NSRange range = NSMakeRange(firstPage, MAX(1LU, lastPage - firstPage));
  4131. // NSUInteger i, iMax = [lastViewedPages count];
  4132. // for (i = 0; i < iMax; i++) {
  4133. // if (NSLocationInRange((NSUInteger)[lastViewedPages pointerAtIndex:i], range))
  4134. // return i;
  4135. // }
  4136. // }
  4137. // return NSNotFound;
  4138. // }
  4139. func outlineView(_ anOutlineView: NSOutlineView, imageContextForItem item: Any?) -> AnyObject? {
  4140. if anOutlineView.isEqual(to: self.tocOutlineView) {
  4141. if item == nil {
  4142. return true as AnyObject
  4143. }
  4144. if item is CPDFOutline {
  4145. return (item as! CPDFOutline).destination
  4146. }
  4147. }
  4148. return nil
  4149. }
  4150. }
  4151. extension KMLeftSideViewController: KMNoteOutlineViewDelegate {
  4152. func outlineView(_ anOutlineView: NSOutlineView, canResizeRowByItem item: AnyObject?) -> Bool? {
  4153. if anOutlineView.isEqual(to: self.noteOutlineView) {
  4154. return true
  4155. }
  4156. return false
  4157. }
  4158. func outlineView(_ anOutlineView: NSOutlineView, setHeight newHeight: CGFloat, ofRowByItem item: AnyObject?) {
  4159. // [rowHeights setFloat:newHeight forKey:item];
  4160. }
  4161. func outlineView(_ anOutlineView: NSOutlineView, didChangeHiddenOfTableColumn aTableColumn: NSTableColumn) {
  4162. // if (mwcFlags.autoResizeNoteRows &&
  4163. // [ov isEqual:rightSideController.noteOutlineView] &&
  4164. // [[tableColumn identifier] isEqualToString:NOTE_COLUMNID]) {
  4165. // [rowHeights removeAllFloats];
  4166. // [rightSideController.noteOutlineView noteHeightOfRowsWithIndexesChanged:[NSIndexSet indexSetWithIndexesInRange:NSMakeRange(0, [rightSideController.noteOutlineView numberOfRows])]];
  4167. // }
  4168. }
  4169. func outlineViewCommandKeyPressedDuringNavigation(_ anOutlineView: NSOutlineView) {
  4170. // PDFAnnotation *annotation = [[self selectedNotes] lastObject];
  4171. // if (annotation) {
  4172. // [pdfView scrollAnnotationToVisible:annotation];
  4173. // [pdfView setActiveAnnotation:annotation];
  4174. // }
  4175. }
  4176. }
  4177. // MARK: - Menu Item Actions
  4178. extension KMLeftSideViewController {
  4179. @objc func cutPage(_ sender: AnyObject?) {
  4180. if IAPProductsManager.default().isAvailableAllFunction() == false {
  4181. KMPurchaseCompareWindowController.sharedInstance().showWindow(nil)
  4182. return
  4183. }
  4184. self._tableView(self.thumbnailTableView, cutRowsWithIndexes: self.thumbnailTableView.selectedRowIndexes)
  4185. }
  4186. @objc func copyPage(_ sender: AnyObject?) {
  4187. self.tableView(self.thumbnailTableView, copyRowsWithIndexes: self.thumbnailTableView.selectedRowIndexes)
  4188. }
  4189. @objc func pastePage(_ sender: AnyObject?) {
  4190. self.tableView(self.thumbnailTableView, pasteFromPasteboard: nil)
  4191. }
  4192. @objc func deletePage(_ sender: AnyObject?) {
  4193. if IAPProductsManager.default().isAvailableAllFunction() == false {
  4194. KMPurchaseCompareWindowController.sharedInstance().showWindow(nil)
  4195. return
  4196. }
  4197. self.tableView(self.thumbnailTableView, deleteRowsWithIndexes: self.thumbnailTableView.selectedRowIndexes)
  4198. }
  4199. @objc func rotatePageMenuAction(_ sender: AnyObject?) {
  4200. if IAPProductsManager.default().isAvailableAllFunction() == false {
  4201. KMPurchaseCompareWindowController.sharedInstance().showWindow(nil)
  4202. return
  4203. }
  4204. self.tableView(self.thumbnailTableView, rotateRowsWithIndexes: self.thumbnailTableView.selectedRowIndexes as NSIndexSet)
  4205. }
  4206. @objc func quickInsert(_ sender: AnyObject?) {
  4207. if IAPProductsManager.default().isAvailableAllFunction() == false {
  4208. KMPurchaseCompareWindowController.sharedInstance().showWindow(nil)
  4209. return
  4210. }
  4211. let idx = self.thumbnailTableView.selectedRowIndexes.first ?? NSNotFound
  4212. if idx == NSNotFound || idx >= self.listView.document.pageCount {
  4213. return
  4214. }
  4215. let result = self.listView.insertPage(KMNormalBlankSize, at: idx+1)
  4216. if result == false {
  4217. return
  4218. }
  4219. var selectedIndexSet = IndexSet()
  4220. selectedIndexSet.insert(idx+1)
  4221. self.insertPages(selectedIndexSet, pageAt: idx)
  4222. }
  4223. @objc func insert(_ sender: AnyObject?) {
  4224. if IAPProductsManager.default().isAvailableAllFunction() == false {
  4225. KMPurchaseCompareWindowController.sharedInstance().showWindow(nil)
  4226. return
  4227. }
  4228. guard let document = self.listView.document else {
  4229. return
  4230. }
  4231. let idx = self.thumbnailTableView.selectedRowIndexes.first ?? NSNotFound
  4232. if idx == NSNotFound || idx >= document.pageCount {
  4233. return
  4234. }
  4235. if document.allowsCopying == false || document.allowsPrinting == false {
  4236. Task {
  4237. _ = await KMAlertTool.runModel(message: KMLocalizedString("This is a secured document. Editing is not permitted.", nil))
  4238. }
  4239. return
  4240. }
  4241. let insertVC = KMPDFEditInsertBlankPageWindow(document: document)
  4242. insertVC.insertLocation = 3
  4243. insertVC.currentPage = idx + 1
  4244. insertVC.callback = { [weak self] pdfDoc, _, pages, insertI in
  4245. if let _pages = pages {
  4246. // mm 单位的大小
  4247. guard let _winC = self?.kmCurrentWindowC as? KMPDFEditInsertBlankPageWindow else {
  4248. self?.km_endSheet()
  4249. return
  4250. }
  4251. var pageSize = _winC.pageSize
  4252. let direction = _winC.pageRotation == 0 ? 0 : 1
  4253. if (direction == 0) { // 纵向
  4254. if (pageSize.width > pageSize.height) { // 需要交换
  4255. let tmp = pageSize.width
  4256. pageSize.width = pageSize.height
  4257. pageSize.height = tmp
  4258. } else {
  4259. // no things.
  4260. }
  4261. } else { // 横向
  4262. if (pageSize.width > pageSize.height) {
  4263. // no things.
  4264. } else { // 需要交换
  4265. let tmp = pageSize.width
  4266. pageSize.width = pageSize.height
  4267. pageSize.height = tmp
  4268. }
  4269. }
  4270. /// 插入位置
  4271. let document = CPDFDocument()
  4272. document?.insertPage(pageSize, at: 0)
  4273. if let page: CPDFPage = (document?.page(at: 0)) {
  4274. self?.insertPage(page, pageAt: insertI)
  4275. }
  4276. }
  4277. self?.km_quick_endSheet()
  4278. }
  4279. self.km_beginSheet(windowC: insertVC)
  4280. }
  4281. @objc func insertPDF(_ sender: AnyObject?) {
  4282. if IAPProductsManager.default().isAvailableAllFunction() == false {
  4283. KMPurchaseCompareWindowController.sharedInstance().showWindow(nil)
  4284. return
  4285. }
  4286. guard let document = self.listView.document else {
  4287. return
  4288. }
  4289. let idx = self.thumbnailTableView.selectedRowIndexes.first ?? NSNotFound
  4290. if idx == NSNotFound || idx >= document.pageCount {
  4291. return
  4292. }
  4293. if document.allowsCopying == false || document.allowsPrinting == false {
  4294. Task {
  4295. _ = await KMAlertTool.runModel(message: KMLocalizedString("This is a secured document. Editing is not permitted.", nil))
  4296. }
  4297. return
  4298. }
  4299. let panel = NSOpenPanel()
  4300. panel.allowedFileTypes = ["pdf"]
  4301. panel.beginSheetModal(for: self.view.window!) { response in
  4302. if response == .cancel {
  4303. return
  4304. }
  4305. for fileURL in panel.urls {
  4306. let pdfDoc = CPDFDocument(url: fileURL)
  4307. if let data = pdfDoc?.isLocked, data {
  4308. DispatchQueue.main.asyncAfter(deadline: .now() + 0.5) {
  4309. let com = PasswordWindowController(windowNibName: "PasswordWindowController")
  4310. com.fileURL = fileURL
  4311. self.km_beginSheet(windowC: com)
  4312. com.closeCallBack = { [unowned self] password in
  4313. self.km_quick_endSheet()
  4314. // }
  4315. if (password.isEmpty == false) {
  4316. let insertVC = KMPDFEditInsertPageWindow(document: document, path: fileURL, password: password)
  4317. insertVC.insertLocation = 3
  4318. insertVC.currentPage = idx + 1
  4319. self.km_beginSheet(windowC: insertVC)
  4320. insertVC.callback = { [weak self] pdfDoc, pwd, pages, insertIdx in
  4321. var indexs = IndexSet()
  4322. guard let _winC = self?.kmCurrentWindowC as? KMPDFEditInsertPageWindow, _winC.insertDocument != nil else {
  4323. self?.km_quick_endSheet()
  4324. return
  4325. }
  4326. let doc = _winC.insertDocument!
  4327. let fileAttribute = _winC.fileAttribute
  4328. var insertIndex = insertIdx
  4329. var insertPages: [CPDFPage] = []
  4330. for number in fileAttribute.fetchSelectPages() {
  4331. if let page = doc.page(at: UInt(number-1)) {
  4332. insertPages.append(page)
  4333. indexs.insert(insertIndex)
  4334. insertIndex += 1
  4335. }
  4336. }
  4337. for (i, page) in insertPages.enumerated() {
  4338. self?.listView.document.insertPageObject(page, at: UInt(insertIdx + i))
  4339. }
  4340. self?.insertPages(indexs, pageAt: insertIdx)
  4341. self?.km_quick_endSheet()
  4342. }
  4343. }
  4344. }
  4345. }
  4346. } else {
  4347. let insertVC = KMPDFEditInsertPageWindow(document: document, path: fileURL)
  4348. insertVC.insertLocation = 3
  4349. insertVC.currentPage = idx + 1
  4350. self.km_beginSheet(windowC: insertVC)
  4351. insertVC.callback = { [weak self] pdfDoc, pwd, pages, insertIdx in
  4352. var indexs = IndexSet()
  4353. guard let _winC = self?.kmCurrentWindowC as? KMPDFEditInsertPageWindow, _winC.insertDocument != nil else {
  4354. self?.km_quick_endSheet()
  4355. return
  4356. }
  4357. let doc = _winC.insertDocument!
  4358. let fileAttribute = _winC.fileAttribute
  4359. var insertIndex = insertIdx
  4360. var insertPages: [CPDFPage] = []
  4361. for number in fileAttribute.fetchSelectPages() {
  4362. if let page = doc.page(at: UInt(number-1)) {
  4363. insertPages.append(page)
  4364. indexs.insert(insertIndex)
  4365. insertIndex += 1
  4366. }
  4367. }
  4368. for (i, page) in insertPages.enumerated() {
  4369. self?.listView.document.insertPageObject(page, at: UInt(insertIdx + i))
  4370. }
  4371. self?.insertPages(indexs, pageAt: insertIdx)
  4372. self?.km_quick_endSheet()
  4373. }
  4374. }
  4375. }
  4376. }
  4377. }
  4378. @objc func extractPage(_ sender: AnyObject?) {
  4379. if IAPProductsManager.default().isAvailableAllFunction() == false {
  4380. KMPurchaseCompareWindowController.sharedInstance().showWindow(nil)
  4381. return
  4382. }
  4383. self.tableView(self.thumbnailTableView, extractRowsWithIndexes: self.thumbnailTableView.selectedRowIndexes)
  4384. }
  4385. @objc func pageEdit(_ sender: AnyObject?) {
  4386. self.delegate?.controller?(controller: self, itemClick: nil, itemKey: .pageEdit, params: nil)
  4387. }
  4388. @objc func displayPageSize(_ sender: AnyObject?) {
  4389. if IAPProductsManager.default().isAvailableAllFunction() == false {
  4390. KMPurchaseCompareWindowController.sharedInstance().showWindow(nil)
  4391. return
  4392. }
  4393. self.isDisplayPageSize = !self.isDisplayPageSize;
  4394. UserDefaults.standard.setValue(self.isDisplayPageSize, forKey: "kKMThumbnailDisplayPageSizeKey")
  4395. UserDefaults.standard.synchronize()
  4396. Task { @MainActor in
  4397. self.thumbnailTableView.reloadData()
  4398. }
  4399. }
  4400. @objc func sharePage(_ sender: AnyObject?) {
  4401. if IAPProductsManager.default().isAvailableAllFunction() == false {
  4402. KMPurchaseCompareWindowController.sharedInstance().showWindow(nil)
  4403. return
  4404. }
  4405. guard let document = self.listView.document else {
  4406. return
  4407. }
  4408. var pages = NSMutableArray()
  4409. for idx in self.thumbnailTableView.selectedRowIndexes {
  4410. if (idx < document.pageCount) {
  4411. if let page = self.listView.document.page(at: UInt(idx)) {
  4412. pages.add(page)
  4413. }
  4414. }
  4415. }
  4416. let pdf = CPDFDocument()
  4417. for page in pages {
  4418. // PDFPage *copyPage = [[page copy] autorelease];
  4419. if let _page = page as? CPDFPage {
  4420. pdf?.insertPageObject(_page, at: pdf?.pageCount ?? 0)
  4421. }
  4422. }
  4423. var fileName = ""
  4424. // NSString * = [self.pdfDocument.documentURL.lastPathComponent stringByDeletingPathExtension];
  4425. var documentFileName = document.documentURL?.deletingPathExtension().lastPathComponent ?? ""
  4426. var tName = self.fileNameWithSelectedPages(self.thumbnailTableView.selectedRowIndexes)
  4427. if (tName.count > 50) {
  4428. tName = tName.substring(to: 50)
  4429. }
  4430. if (pages.count > 1) {
  4431. fileName = String(format: "%@ pages %@", documentFileName, tName)
  4432. } else {
  4433. fileName = String(format: "%@ page %@", documentFileName, tName)
  4434. }
  4435. let paths = NSSearchPathForDirectoriesInDomains(.cachesDirectory, .userDomainMask, true)
  4436. var cachesDir = paths.first ?? ""
  4437. cachesDir = "\(cachesDir)/\(fileName).pdf"
  4438. var array = NSMutableArray()
  4439. let url = URL(fileURLWithPath: cachesDir)
  4440. array.add(url)
  4441. let vc = KMProgressWindowController()
  4442. // // [NSApp beginSheet:[vc window]
  4443. // // modalForWindow:self.window
  4444. // // modalDelegate:nil
  4445. // // didEndSelector:nil
  4446. // // contextInfo:NULL];
  4447. self.view.window?.beginSheet(vc.window!, completionHandler: { [unowned self] returnCode in
  4448. NSApp.endSheet(self.view.window!, returnCode: returnCode.rawValue)
  4449. })
  4450. DispatchQueue.global().async {
  4451. let sucess = pdf?.write(toFile: cachesDir) ?? false
  4452. if (sucess) {
  4453. DispatchQueue.main.async {
  4454. // [[sender representedObject] performWithItems:array];
  4455. let represent : NSSharingService = (sender as! NSMenuItem).representedObject as! NSSharingService
  4456. represent.perform(withItems: [url])
  4457. NSApp.endSheet(vc.window!)
  4458. vc.close()
  4459. }
  4460. } else {
  4461. NSApp.endSheet(vc.window!)
  4462. vc.close()
  4463. }
  4464. }
  4465. // [vc release];
  4466. }
  4467. @objc func toggleCaseInsensitiveNoteSearch(_ sender: AnyObject?) {
  4468. KMPrint("KMLeftSideViewController-toggleCaseInsensitiveNoteSearch...")
  4469. }
  4470. @objc func searchNotes(_ sender: AnyObject?) {
  4471. self.noteSearchMode = false
  4472. if self.findState == .note {
  4473. if self.noteSearchField.isEqual(to: sender) {
  4474. self.noteSearchMode = true
  4475. }
  4476. self.updateNoteFilterPredicate()
  4477. } else {
  4478. self.updateSnapshotFilterPredicate()
  4479. }
  4480. let textfield = sender as? NSSearchField
  4481. if let data = textfield?.stringValue.isEmpty, data == false {
  4482. let findPboard = NSPasteboard(name: .find)
  4483. findPboard.clearContents()
  4484. // findPboard.writeObjects([textfield!.stringValue])
  4485. }
  4486. }
  4487. @objc func toggleSelectedSnapshots(_ sender: AnyObject?) {
  4488. let indexs = self.snapshotTableView.selectedRowIndexes
  4489. if indexs.isEmpty {
  4490. return
  4491. }
  4492. let model = self.snapshots[indexs.last!]
  4493. let windowC = model.windowC
  4494. if let data = windowC?.window?.isVisible, data {
  4495. windowC?.miniaturize()
  4496. } else {
  4497. windowC?.deminiaturize()
  4498. }
  4499. var rowIndexSet = IndexSet()
  4500. let row = self.snapshotTableView.selectedRow
  4501. if row >= 0 && row < self.snapshots.count {
  4502. rowIndexSet.insert(row)
  4503. }
  4504. var columnIndexSet = IndexSet()
  4505. columnIndexSet.insert(0)
  4506. self.snapshotTableView.reloadData(forRowIndexes: rowIndexSet, columnIndexes: columnIndexSet)
  4507. }
  4508. private func _tableView(_ tv: NSTableView, cutRowsWithIndexes rowIndexes: IndexSet) {
  4509. if tv.isEqual(to: self.thumbnailTableView) {
  4510. self._copysPages.removeAll()
  4511. for idx in rowIndexes {
  4512. if (idx != NSNotFound) {
  4513. if let page = self.listView.document.page(at: UInt(idx))?.copy() as? CPDFPage {
  4514. self._copysPages.append(page)
  4515. }
  4516. }
  4517. }
  4518. self.tableView(tv, deleteRowsWithIndexes: rowIndexes)
  4519. }
  4520. }
  4521. // MARK: - Snapshot
  4522. @objc func menuItemClick_ExportPNG(_ sender: AnyObject?) {
  4523. guard let snapshot = (sender as? NSMenuItem)?.representedObject as? KMSnapshotWindowController else {
  4524. return
  4525. }
  4526. let image = snapshot.thumbnailWithSize(0)
  4527. NSPanel.savePanel_data_success(self.view.window!, imageData: image?.pngData(), allowedTypes: ["png"]) { url in
  4528. NSWorkspace.shared.selectFile(url.path, inFileViewerRootedAtPath: "")
  4529. }
  4530. }
  4531. @objc func menuItemClick_ExportJPG(_ sender: AnyObject?) {
  4532. guard let snapshot = (sender as? NSMenuItem)?.representedObject as? KMSnapshotWindowController else {
  4533. return
  4534. }
  4535. let image = snapshot.thumbnailWithSize(0)
  4536. NSPanel.savePanel_data_success(self.view.window!, imageData: image?.jpgData(), allowedTypes: ["jpg"]) { url in
  4537. NSWorkspace.shared.selectFile(url.path, inFileViewerRootedAtPath: "")
  4538. }
  4539. }
  4540. @objc func menuItemClick_ExportPDF(_ sender: AnyObject?) {
  4541. guard let snapshot = (sender as? NSMenuItem)?.representedObject as? KMSnapshotWindowController else {
  4542. return
  4543. }
  4544. if let image = snapshot.thumbnailWithSize(0) {
  4545. let document = CPDFDocument()
  4546. _ = document?.km_insert(image: image, at: 0)
  4547. NSPanel.savePanel_pdf_success(self.view.window!, document: document) { url in
  4548. NSWorkspace.shared.selectFile(url.path, inFileViewerRootedAtPath: "")
  4549. }
  4550. }
  4551. }
  4552. @objc func menuItemClick_Print(_ sender: AnyObject?) {
  4553. guard let snapshot = (sender as? NSMenuItem)?.representedObject as? KMSnapshotWindowController else {
  4554. return
  4555. }
  4556. if let image = snapshot.thumbnailWithSize(0) {
  4557. self.delegate?.controller?(controller: self, itemClick: nil, itemKey: .print, params: image)
  4558. }
  4559. }
  4560. @objc func menuItemClick_SelectAll(_ sender: AnyObject?) {
  4561. let selected = self.snapshotListIsAllSelected()
  4562. for model in self.snapshots {
  4563. model.isSelected = !selected
  4564. }
  4565. Task { @MainActor in
  4566. self.snapshotTableView.reloadData()
  4567. }
  4568. }
  4569. @objc func deleteAllSnapshot(_ sender: AnyObject?) {
  4570. for model in self.snapshots {
  4571. model.windowC?.close()
  4572. }
  4573. }
  4574. @objc func hideSnapshot(_ sender: AnyObject?) {
  4575. guard let snapshot = (sender as? NSMenuItem)?.representedObject as? KMSnapshotWindowController else {
  4576. return
  4577. }
  4578. if let data = snapshot.window?.isVisible, data {
  4579. snapshot.miniaturize()
  4580. }
  4581. }
  4582. @objc func showSnapshot(_ sender: AnyObject?) {
  4583. guard let snapshot = (sender as? NSMenuItem)?.representedObject as? KMSnapshotWindowController else {
  4584. return
  4585. }
  4586. if let data = snapshot.window?.isVisible, data {
  4587. snapshot.window?.orderFront(nil)
  4588. } else {
  4589. snapshot.deminiaturize()
  4590. }
  4591. }
  4592. @objc func deleteSnapshot(_ sender: AnyObject?) {
  4593. guard let snapshot = (sender as? NSMenuItem)?.representedObject as? KMSnapshotWindowController else {
  4594. return
  4595. }
  4596. snapshot.close()
  4597. }
  4598. @objc func menuItemClick_Copy(_ sender: AnyObject?) {
  4599. guard let snapshot = (sender as? NSMenuItem)?.representedObject as? KMSnapshotWindowController else {
  4600. return
  4601. }
  4602. let image = snapshot.thumbnailWithSize(0)
  4603. if let tiffData = image?.tiffRepresentation {
  4604. let pasteboardItem = NSPasteboardItem()
  4605. pasteboardItem.setData(tiffData, forType: .tiff)
  4606. let pboard = NSPasteboard.general
  4607. pboard.clearContents()
  4608. pboard.writeObjects([pasteboardItem])
  4609. }
  4610. }
  4611. func updateSnapshotFilterPredicate() {
  4612. let searchString = self.snapshotSearchField.stringValue
  4613. self.searchSnapshots.removeAll()
  4614. if self.findState == .snapshot && searchString.isEmpty == false {
  4615. self.searchSnapshots = self.snapshots.filter({ model in
  4616. let data = model.windowC?.string.contains(searchString) ?? false
  4617. return data
  4618. })
  4619. }
  4620. Task { @MainActor in
  4621. self.updataLeftSideSnapView()
  4622. self.snapshotTableView.reloadData()
  4623. }
  4624. }
  4625. func snapshotListIsAllSelected() -> Bool {
  4626. if self.snapshots.isEmpty {
  4627. return false
  4628. }
  4629. for model in self.snapshots {
  4630. if model.isSelected == false {
  4631. return false
  4632. }
  4633. }
  4634. return true
  4635. }
  4636. }
  4637. // MARK: - Undo & Redo
  4638. extension KMLeftSideViewController {
  4639. @objc dynamic func tableView(_ tv: NSTableView, rotateRowsWithIndexes rowIndexes: NSIndexSet) {
  4640. if tv.isEqual(to: self.thumbnailTableView) {
  4641. for idx in rowIndexes {
  4642. if (idx != NSNotFound) {
  4643. let page = self.listView.document.page(at: UInt(idx))
  4644. (self.listView.undoManager?.prepare(withInvocationTarget: self) as AnyObject).rotatePage(page, pageAt: idx)
  4645. page?.rightRotate()
  4646. self.listView.layoutDocumentView()
  4647. self.resetThumbnails()
  4648. // NSInteger pageIndex = MIN(idx, [[pdfView document] pageCount]-1);
  4649. // [pdfView goToPage:[[pdfView document] pageAtIndex:pageIndex]];
  4650. }
  4651. self.thumbnailTableView.selectRowIndexes(rowIndexes as IndexSet, byExtendingSelection: true)
  4652. }
  4653. }
  4654. }
  4655. @objc dynamic func rotatePage(_ page: CPDFPage?, pageAt index: Int) {
  4656. (self.listView.undoManager?.prepare(withInvocationTarget: self) as AnyObject).tableView(self.thumbnailTableView, rotateRowsWithIndexes: NSIndexSet(index: index))
  4657. page?.leftRotate()
  4658. self.listView.layoutDocumentView()
  4659. self.resetThumbnails()
  4660. }
  4661. @objc dynamic func insertPage(_ page: CPDFPage, pageAt index: Int) {
  4662. (self.listView.undoManager?.prepare(withInvocationTarget: self) as AnyObject).tableView(self.thumbnailTableView, deleteRowsWithIndexes: IndexSet(integer: index))
  4663. self.listView.document.insertPageObject(page, at: UInt(index))
  4664. self.listView.layoutDocumentView()
  4665. // [pageLabels setArray:[[pdfView document] pageLabels]];
  4666. self.resetThumbnails()
  4667. let pageIndex = min(index, Int((self.listView.document?.pageCount ?? 0))-1)
  4668. self.listView.go(toPageIndex: pageIndex, animated: false)
  4669. }
  4670. @objc dynamic func insertPages(_ selectedIndexSet: IndexSet, pageAt index: Int) {
  4671. (self.listView.undoManager?.prepare(withInvocationTarget: self) as AnyObject).deletePages(selectedIndexSet, pageAt: index)
  4672. self.listView.layoutDocumentView()
  4673. // [pageLabels setArray:[[pdfView document] pageLabels]];
  4674. self.resetThumbnails()
  4675. if let pageIndex = selectedIndexSet.first {
  4676. self.listView.go(toPageIndex: pageIndex, animated: false)
  4677. }
  4678. }
  4679. @objc dynamic func deletePages(_ selectedIndexSet: IndexSet, pageAt index: Int) {
  4680. (self.listView.undoManager?.prepare(withInvocationTarget: self) as AnyObject).insertPages(selectedIndexSet, pageAt: index)
  4681. for idx in selectedIndexSet {
  4682. if idx < self.listView.document.pageCount {
  4683. self.listView.document.removePage(at: UInt(idx))
  4684. }
  4685. }
  4686. self.listView.layoutDocumentView()
  4687. // [pageLabels setArray:[[pdfView document] pageLabels]];
  4688. self.resetThumbnails()
  4689. let pageIndex = min(index, Int(self.listView.document.pageCount)-1)
  4690. self.listView.go(toPageIndex: pageIndex, animated: false)
  4691. }
  4692. }
  4693. // MARK: - Other
  4694. extension KMLeftSideViewController {
  4695. @objc func goToSelectedOutlineItem(_ sender: AnyObject?) {
  4696. let outlineItem = self.tocOutlineView.item(atRow: self.tocOutlineView.selectedRow)
  4697. let outline = self.tocOutlineView
  4698. if let cnt = outline?.selectedRowIndexes.count, cnt == 1 {
  4699. if outlineItem is CPDFOutline {
  4700. let outline = (outlineItem as! CPDFOutline)
  4701. if let des = outline.destination {
  4702. self.listView.go(to: des)
  4703. } else if let action = outline.action {
  4704. self.listView.perform(action)
  4705. }
  4706. } else if outlineItem is CPDFBookmark {
  4707. let bookmark = outlineItem as! CPDFBookmark
  4708. self.listView.go(toPageIndex: bookmark.pageIndex, animated: true)
  4709. }
  4710. }
  4711. }
  4712. @objc func goToSelectedFindResults(_ sender: AnyObject?) {
  4713. KMPrint("KMLeftSideViewController-goToSelectedFindResults...")
  4714. }
  4715. func tableView(_ tv: NSTableView, extractRowsWithIndexes rowIndexes: IndexSet) {
  4716. if tv.isEqual(to: self.thumbnailTableView) {
  4717. guard let document = self.listView.document else {
  4718. return
  4719. }
  4720. var pages = NSMutableArray()
  4721. for idx in self.thumbnailTableView.selectedRowIndexes {
  4722. if (idx < document.pageCount) {
  4723. if let page = self.listView.document.page(at: UInt(idx)) {
  4724. pages.add(page)
  4725. }
  4726. }
  4727. }
  4728. let fileName = document.getFileNameAccordingSelctPages(pages as! [CPDFPage])
  4729. let saveAccessCtr = KMSavePanelAccessoryController()
  4730. let outputSavePanel = NSSavePanel()
  4731. outputSavePanel.allowedFileTypes = ["pdf"]
  4732. outputSavePanel.accessoryView = saveAccessCtr.view
  4733. outputSavePanel.nameFieldStringValue = fileName
  4734. outputSavePanel.beginSheetModal(for: self.view.window!) { result in
  4735. if (result == .OK) {
  4736. DispatchQueue.main.asyncAfter(deadline: .now()+0.5) {
  4737. let vc = KMProgressWindowController()
  4738. self.view.window?.beginSheet(vc.window!)
  4739. let saveFilePath = outputSavePanel.url?.path
  4740. DispatchQueue.global().async {
  4741. let pdf = CPDFDocument()
  4742. let success = pdf?.extractAsOneDocument(withPages: pages as! [CPDFPage], savePath: saveFilePath) ?? false
  4743. DispatchQueue.main.async {
  4744. if (success) {
  4745. if (saveAccessCtr.openAutomaticButton.state == .on) {
  4746. NSDocumentController.shared.km_safe_openDocument(withContentsOf: outputSavePanel.url!, display: true) { _, _, _ in
  4747. }
  4748. } else {
  4749. KMTools.viewFile(at: saveFilePath!)
  4750. }
  4751. }
  4752. NSApp.endSheet(vc.window!)
  4753. vc.close()
  4754. }
  4755. }
  4756. }
  4757. }
  4758. }
  4759. }
  4760. }
  4761. func fileNameWithSelectedPages(_ itemIndexes: IndexSet) -> String {
  4762. var pagesName = ""
  4763. if (itemIndexes.count > 1) {
  4764. pagesName.append(" pages")
  4765. } else {
  4766. pagesName.append("page")
  4767. }
  4768. let docmentName = self.listView.document?.documentURL.deletingPathExtension().lastPathComponent ?? ""
  4769. var fileName = ""
  4770. if (itemIndexes.count > 0) {
  4771. if (itemIndexes.count == 1) {
  4772. let idx = itemIndexes.first! + 1
  4773. let tFileName = String(format: "%@ %@", pagesName, "\(idx)")
  4774. return String(format: "%@%@", docmentName, tFileName)
  4775. }
  4776. var sortIndex = IndexSet()
  4777. for idx in itemIndexes {
  4778. let _idx = idx + 1
  4779. sortIndex.insert(_idx)
  4780. }
  4781. // NSSortDescriptor * sort = [[NSSortDescriptor alloc] initWithKey:nil ascending:YES];
  4782. // NSArray *sortDesc = @[sort];
  4783. // NSArray *sortArray = [sortIndex sortedArrayUsingDescriptors:sortDesc];
  4784. let sortArray = sortIndex.sorted()
  4785. var a = 0
  4786. var b = 0
  4787. for num in sortArray {
  4788. // for (NSNumber *num in sortArray) {
  4789. if (fileName.isEmpty == false) {
  4790. if (num == b+1) {
  4791. b = num
  4792. if (num == sortArray.last) {
  4793. fileName = "\(fileName)\(a)-\(b)"
  4794. }
  4795. } else {
  4796. if (a == b) {
  4797. fileName = "\(fileName)\(a),"
  4798. } else {
  4799. fileName = "\(fileName)\(a)-\(b),"
  4800. }
  4801. b = num
  4802. a = b
  4803. if (num == sortArray.last) {
  4804. fileName = "\(fileName)\(a)"
  4805. }
  4806. }
  4807. } else {
  4808. fileName = ""
  4809. b = num
  4810. a = b
  4811. }
  4812. }
  4813. let tFileName = String(format: "%@ %@", pagesName,fileName)
  4814. return String(format: "%@%@", docmentName,tFileName)
  4815. }
  4816. return ""
  4817. }
  4818. func switchType(_ type: BotaType) {
  4819. if type == .Thumbnail {
  4820. self.leftView.segmentedControl.selectedSegment = 0
  4821. } else if type == .Outline {
  4822. self.leftView.segmentedControl.selectedSegment = 1
  4823. } else if type == .Annotation {
  4824. self.leftView.segmentedControl.selectedSegment = 2
  4825. } else if type == .snapshot {
  4826. self.leftView.segmentedControl.selectedSegment = 3
  4827. } else if type == .Search {
  4828. self.leftView.segmentedControl.selectedSegment = 4
  4829. }
  4830. }
  4831. var TINY_SIZE: Float {
  4832. get {
  4833. return 32.0
  4834. }
  4835. }
  4836. var SMALL_SIZE: Float {
  4837. get {
  4838. return 64.0
  4839. }
  4840. }
  4841. var LARGE_SIZE: Float {
  4842. get {
  4843. return 128.0
  4844. }
  4845. }
  4846. var HUGE_SIZE: Float {
  4847. get {
  4848. return 256.0
  4849. }
  4850. }
  4851. var FUDGE_SIZE: Float {
  4852. get {
  4853. return 0.1
  4854. }
  4855. }
  4856. func updateTableFont() {
  4857. let font = NSFont.systemFont(ofSize: KMPreference.shared.outlineFontSize.cgFloat)
  4858. self.tocOutlineView.font = font
  4859. self.noteOutlineView.font = font
  4860. self.findTableView.font = font
  4861. self.groupedFindTableView.font = font
  4862. }
  4863. }