Geen omschrijving

DashboardView.swift 174KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851
  1. //
  2. // DashboardView.swift
  3. // App for Indeed
  4. //
  5. import Cocoa
  6. import QuartzCore
  7. import Security
  8. private enum JobListingCardContext {
  9. case homeSearchResults
  10. case savedJobsPage
  11. }
  12. private enum PremiumSheetLayout {
  13. /// Grow the sheet past the host content rect on each side to hide compositing hairlines.
  14. static let overscanPerEdge: CGFloat = 2
  15. /// Additional growth on the top edge only (pt).
  16. static let overscanExtraTop: CGFloat = 0.5
  17. }
  18. final class DashboardView: NSView, NSTextFieldDelegate, NSSharingServicePickerDelegate, NSSharingServiceDelegate {
  19. /// Indeed.com-inspired neutrals and brand blue; values follow light / dark / system appearance.
  20. private enum Theme {
  21. static var brandBlue: NSColor { AppDashboardTheme.brandBlue }
  22. static var pageBackground: NSColor { AppDashboardTheme.pageBackground }
  23. static var chromeBackground: NSColor { AppDashboardTheme.chromeBackground }
  24. static var sidebarBackground: NSColor { AppDashboardTheme.sidebarBackground }
  25. static var mainHostBackground: NSColor { AppDashboardTheme.mainHostBackground }
  26. static var welcomeHeroHeadingBlue: NSColor { AppDashboardTheme.welcomeHeroHeadingBlue }
  27. static var welcomeHeroSubtitleText: NSColor { AppDashboardTheme.welcomeHeroSubtitleText }
  28. static var welcomeHeroIconWell: NSColor { AppDashboardTheme.welcomeHeroIconWell }
  29. static var welcomeHeroWaveTint: NSColor { AppDashboardTheme.welcomeHeroWaveTint }
  30. static var welcomeSubtitleText: NSColor { AppDashboardTheme.welcomeSubtitleText }
  31. static var selectionFill: NSColor { AppDashboardTheme.selectionFill }
  32. static var cardBackground: NSColor { AppDashboardTheme.cardBackground }
  33. static var toggleBackground: NSColor { AppDashboardTheme.toggleBackground }
  34. static var primaryText: NSColor { AppDashboardTheme.primaryText }
  35. static var secondaryText: NSColor { AppDashboardTheme.secondaryText }
  36. static var tertiaryText: NSColor { AppDashboardTheme.tertiaryText }
  37. static var border: NSColor { AppDashboardTheme.border }
  38. static var searchBarBorder: NSColor { AppDashboardTheme.searchBarBorder }
  39. static var searchBarBorderHover: NSColor { AppDashboardTheme.searchBarBorderHover }
  40. static var proCardFill: NSColor { AppDashboardTheme.proCardFill }
  41. static var proCardBorder: NSColor { AppDashboardTheme.proCardBorder }
  42. static var proAccent: NSColor { AppDashboardTheme.proAccent }
  43. static var proCTABackground: NSColor { AppDashboardTheme.proCTABackground }
  44. static var proCTAText: NSColor { AppDashboardTheme.proCTAText }
  45. static var brandBlueHover: NSColor { AppDashboardTheme.brandBlueHover }
  46. static var selectionFillHover: NSColor { AppDashboardTheme.selectionFillHover }
  47. static var neutralHoverFill: NSColor { AppDashboardTheme.neutralHoverFill }
  48. static var sidebarRowHoverFill: NSColor { AppDashboardTheme.sidebarRowHoverFill }
  49. static var settingsPageBackground: NSColor { AppDashboardTheme.settingsPageBackground }
  50. static var settingsGroupBackground: NSColor { AppDashboardTheme.settingsGroupBackground }
  51. static var settingsIconBackground: NSColor { AppDashboardTheme.settingsIconBackground }
  52. static var settingsDivider: NSColor { AppDashboardTheme.settingsDivider }
  53. }
  54. /// Multiline `NSTextField` often ignores `alignment` for wrapped runs; explicit paragraph alignment matches the title.
  55. private static func jobListingDescriptionAttributedString(_ plain: String) -> NSAttributedString {
  56. let paragraph = NSMutableParagraphStyle()
  57. paragraph.alignment = .left
  58. paragraph.lineBreakMode = .byWordWrapping
  59. paragraph.baseWritingDirection = .leftToRight
  60. let font = NSFont.systemFont(ofSize: 13, weight: .regular)
  61. return NSAttributedString(string: plain, attributes: [
  62. .font: font,
  63. .foregroundColor: Theme.secondaryText,
  64. .paragraphStyle: paragraph
  65. ])
  66. }
  67. /// Horizontal row for sidebar + main; plain view + constraints keep both panels top/bottom aligned (stack view height alignment was inconsistent).
  68. private let panelsRow = NSView()
  69. private let chromeContainer = NSView()
  70. private let sidebar = NSStackView()
  71. private let mainHost = NSView()
  72. private let mainOverlay = NSStackView()
  73. private let greetingLabel = NSTextField(labelWithString: "")
  74. private let subtitleLabel = NSTextField(labelWithString: "")
  75. private let searchBarShadowHost = NSView()
  76. private let searchCard = HoverableView()
  77. private let jobSearchIcon = NSImageView()
  78. private let jobKeywordsField = NSTextField()
  79. private let findJobsButton = HoverableButton()
  80. private let findJobsCTAPill = HoverableView()
  81. private let sendIconView = NSImageView()
  82. private let sendLabel = NSTextField(labelWithString: "Send")
  83. private let sendContentStack = NSStackView()
  84. private let findJobsCTAHost = NSView()
  85. private let welcomeHeroHost = NSView()
  86. private let welcomeHeroBackgroundView = WelcomeHeroBackgroundView()
  87. private let welcomeLogoWell = NSView()
  88. private let welcomeLogoView = IndeedLogoView(displayHeight: 40, variant: .compact)
  89. private let featureCardsRow = NSStackView()
  90. private enum FeatureShortcut: Int { case role = 0, company = 1, skill = 2 }
  91. private let clearChatButton = NSButton(title: "Clear chat", target: nil, action: nil)
  92. private let chatScrollView = NSScrollView()
  93. private let chatDocumentView = JobListingsDocumentView()
  94. private let chatStack = NSStackView()
  95. /// Shown when a sidebar item other than Home is selected.
  96. private let nonHomeHost = NSView()
  97. /// Full-bleed Indeed apply / listing web view inside the main panel (same window as the dashboard).
  98. private let indeedJobBrowserHost = NSView()
  99. private let nonHomeGenericContainer = NSView()
  100. private let nonHomeTitleLabel = NSTextField(labelWithString: "")
  101. private let nonHomeSubtitleLabel = NSTextField(wrappingLabelWithString: "")
  102. private let savedJobsPageContainer = NSView()
  103. private let savedJobsPageTitleLabel = NSTextField(labelWithString: "Saved Jobs")
  104. private let savedJobsPageSubtitleLabel = NSTextField(wrappingLabelWithString: "")
  105. private let savedJobsScrollView = NSScrollView()
  106. private let savedJobsDocumentView = JobListingsDocumentView()
  107. private let savedJobsStack = NSStackView()
  108. private let settingsPageContainer = NSView()
  109. private weak var appearanceModeSegment: NSSegmentedControl?
  110. private let cvMakerPageContainer = NSView()
  111. private lazy var cvMakerPageView: CVMakerPageView = {
  112. CVMakerPageView()
  113. }()
  114. private let profilePageContainer = NSView()
  115. private lazy var profilesListPageView: ProfilesListPageView = {
  116. ProfilesListPageView()
  117. }()
  118. private lazy var myProfilePageView: MyProfilePageView = {
  119. MyProfilePageView()
  120. }()
  121. /// When true, `myProfilePageView` is visible instead of the profiles list.
  122. private var isProfileEditorPresented = false
  123. /// When true, the merged CV preview is visible instead of the profiles list or editor.
  124. private var isCVDocumentPreviewPresented = false
  125. /// Exact template chosen in CV Maker until the user leaves Profile or starts a new CV Maker hand-off (avoids re-resolving by id and picking a different row).
  126. private var pendingCVTemplate: CVTemplate?
  127. private let cvFilledPreviewPageView = CVFilledPreviewPageView()
  128. private var currentSidebarItems: [SidebarItem] = []
  129. private var selectedSidebarIndex: Int = 0
  130. /// When true, the **Indeed** sidebar row is highlighted instead of `selectedSidebarIndex`.
  131. private var isIndeedSidebarSelected = false
  132. /// All jobs that have been shown in the current chat session, oldest first. Used to deduplicate continuation searches so "show more" doesn't re-display the same listings.
  133. private var lastSearchResults: [JobListing] = []
  134. /// "Show more jobs" row under the latest assistant message that listed jobs; removed when a newer listing block replaces it.
  135. private var trailingLoadMoreJobsRow: NSView?
  136. private weak var trailingLoadMoreJobsButton: HoverableButton?
  137. /// Most recently saved jobs appear first; persisted across launches.
  138. private var savedJobOrder: [JobListing] = []
  139. private var chatMessages: [ChatMessage] = []
  140. private var isAwaitingResponse = false
  141. /// Shown under the latest user message while a job search request is in flight.
  142. private var chatThinkingRowHost: NSView?
  143. private let jobSearchService = OpenAIJobSearchService()
  144. private var premiumPlansWindowController: PremiumPlansWindowController?
  145. private var indeedJobBrowserViewController: IndeedJobBrowserViewController?
  146. private var isIndeedJobBrowserPresented = false
  147. private weak var sidebarUpgradeCard: NSView?
  148. private weak var sidebarUpgradeHeadline: NSTextField?
  149. private weak var sidebarUpgradeDescription: NSTextField?
  150. private weak var sidebarUpgradeButton: HoverableButton?
  151. private var subscriptionObserver: NSObjectProtocol?
  152. private var appearanceObserver: NSObjectProtocol?
  153. /// Retains the system share picker until the user picks a destination or dismisses the menu.
  154. private var appSharePicker: NSSharingServicePicker?
  155. /// Upper bound sent to the model per request (was fixed at 8 in the prompt). Clamped when calling the API.
  156. private static let jobsPerSearchDefault = 15
  157. private static let jobsPerSearchMin = 1
  158. private static let jobsPerSearchMaxCap = 25
  159. private static func clampedJobsPerRequest(_ requested: Int = jobsPerSearchDefault) -> Int {
  160. min(jobsPerSearchMaxCap, max(jobsPerSearchMin, requested))
  161. }
  162. override init(frame frameRect: NSRect) {
  163. super.init(frame: frameRect)
  164. setupLayout()
  165. registerAppearanceObservers()
  166. }
  167. required init?(coder: NSCoder) {
  168. super.init(coder: coder)
  169. setupLayout()
  170. registerAppearanceObservers()
  171. }
  172. deinit {
  173. if let subscriptionObserver {
  174. NotificationCenter.default.removeObserver(subscriptionObserver)
  175. }
  176. if let appearanceObserver {
  177. NotificationCenter.default.removeObserver(appearanceObserver)
  178. }
  179. }
  180. override func viewDidChangeEffectiveAppearance() {
  181. super.viewDidChangeEffectiveAppearance()
  182. applyCurrentAppearance()
  183. }
  184. private func registerAppearanceObservers() {
  185. appearanceObserver = NotificationCenter.default.addObserver(
  186. forName: AppAppearanceManager.didChangeNotification,
  187. object: nil,
  188. queue: .main
  189. ) { [weak self] _ in
  190. self?.applyCurrentAppearance()
  191. }
  192. }
  193. override func viewDidMoveToWindow() {
  194. super.viewDidMoveToWindow()
  195. guard window != nil else { return }
  196. Task { @MainActor in
  197. await SubscriptionStore.shared.refreshEntitlements(deep: true)
  198. self.applyProSubscriptionToSidebar()
  199. }
  200. }
  201. override func layout() {
  202. super.layout()
  203. updateSearchBarShadowPath()
  204. updateFindJobsCTAShadowPath()
  205. updateJobListingDescriptionWidths()
  206. updateChatBubbleWidths()
  207. }
  208. func render(_ data: DashboardData) {
  209. dismissIndeedJobBrowserEmbedded()
  210. greetingLabel.stringValue = "Welcome"
  211. subtitleLabel.stringValue = data.subtitle
  212. currentSidebarItems = data.sidebarItems
  213. if selectedSidebarIndex >= currentSidebarItems.count {
  214. selectedSidebarIndex = max(0, currentSidebarItems.count - 1)
  215. }
  216. configureSidebar()
  217. savedJobOrder = Self.normalizedSavedJobs(SavedJobsStore.load())
  218. resetChatState()
  219. updateMainContentVisibility()
  220. applyCurrentAppearance()
  221. }
  222. private func applyCurrentAppearance() {
  223. window?.backgroundColor = AppAppearanceManager.shared.windowChromeColor
  224. layer?.backgroundColor = Theme.chromeBackground.cgColor
  225. chromeContainer.layer?.backgroundColor = Theme.chromeBackground.cgColor
  226. sidebar.layer?.backgroundColor = Theme.sidebarBackground.cgColor
  227. mainHost.layer?.backgroundColor = Theme.mainHostBackground.cgColor
  228. indeedJobBrowserHost.layer?.backgroundColor = Theme.mainHostBackground.cgColor
  229. nonHomeHost.layer?.backgroundColor = Theme.mainHostBackground.cgColor
  230. cvMakerPageContainer.layer?.backgroundColor = Theme.mainHostBackground.cgColor
  231. profilePageContainer.layer?.backgroundColor = Theme.mainHostBackground.cgColor
  232. settingsPageContainer.layer?.backgroundColor = Theme.settingsPageBackground.cgColor
  233. greetingLabel.textColor = Theme.welcomeHeroHeadingBlue
  234. subtitleLabel.textColor = Theme.welcomeHeroSubtitleText
  235. welcomeHeroBackgroundView.waveTint = Theme.welcomeHeroWaveTint
  236. welcomeLogoWell.layer?.backgroundColor = Theme.welcomeHeroIconWell.cgColor
  237. nonHomeTitleLabel.textColor = Theme.primaryText
  238. nonHomeSubtitleLabel.textColor = Theme.secondaryText
  239. savedJobsPageTitleLabel.textColor = Theme.primaryText
  240. savedJobsPageSubtitleLabel.textColor = Theme.secondaryText
  241. let searchHovering = searchCard.isHovering
  242. searchCard.layer?.backgroundColor = (searchHovering ? Theme.neutralHoverFill : Theme.cardBackground).cgColor
  243. searchCard.layer?.borderColor = (searchHovering ? Theme.searchBarBorderHover : Theme.searchBarBorder).cgColor
  244. jobKeywordsField.textColor = Theme.primaryText
  245. jobKeywordsField.placeholderAttributedString = NSAttributedString(
  246. string: "Ask for roles, skills, salary, or job descriptions...",
  247. attributes: [
  248. .foregroundColor: Theme.secondaryText,
  249. .font: NSFont.systemFont(ofSize: 14, weight: .regular)
  250. ]
  251. )
  252. jobSearchIcon.contentTintColor = Theme.brandBlue
  253. let ctaHovering = findJobsButton.isHovering
  254. findJobsCTAPill.layer?.backgroundColor = (ctaHovering ? Theme.brandBlueHover : Theme.brandBlue).cgColor
  255. sendIconView.contentTintColor = Theme.proCTAText
  256. sendLabel.textColor = Theme.proCTAText
  257. appearanceModeSegment?.selectedSegment = AppAppearanceManager.shared.mode.segmentIndex
  258. cvMakerPageView.applyCurrentAppearance()
  259. profilesListPageView.applyCurrentAppearance()
  260. myProfilePageView.applyCurrentAppearance()
  261. cvFilledPreviewPageView.applyCurrentAppearance()
  262. refreshSettingsPageAppearance(in: settingsPageContainer)
  263. rebuildFeatureShortcutCards()
  264. configureSidebar()
  265. reloadSavedJobsListings()
  266. rebuildChatUI()
  267. applyProSubscriptionToSidebar()
  268. needsLayout = true
  269. }
  270. private func refreshSettingsPageAppearance(in root: NSView) {
  271. for case let field as NSTextField in root.subviewsRecursive() where !field.isEditable {
  272. switch field.font?.pointSize {
  273. case 12, 14:
  274. field.textColor = Theme.secondaryText
  275. default:
  276. break
  277. }
  278. }
  279. for case let stack as NSStackView in root.subviewsRecursive() where stack.orientation == .vertical && stack.layer?.cornerRadius == 14 {
  280. stack.layer?.backgroundColor = Theme.settingsGroupBackground.cgColor
  281. stack.layer?.borderColor = Theme.border.cgColor
  282. for case let divider as NSView in stack.arrangedSubviews where divider.bounds.height <= 1.5 && divider.wantsLayer {
  283. divider.layer?.backgroundColor = Theme.settingsDivider.cgColor
  284. }
  285. }
  286. for case let tile as NSView in root.subviewsRecursive() where tile.layer?.cornerRadius == 9 && tile.bounds.width == 38 {
  287. tile.layer?.backgroundColor = Theme.settingsIconBackground.cgColor
  288. for case let icon as NSImageView in tile.subviews {
  289. icon.contentTintColor = Theme.brandBlue
  290. }
  291. }
  292. }
  293. private func rebuildFeatureShortcutCards() {
  294. let selectedIndex = featureCardsRow.arrangedSubviews.firstIndex {
  295. ($0 as? FeatureShortcutCardView)?.isSelected == true
  296. }
  297. featureCardsRow.arrangedSubviews.forEach {
  298. featureCardsRow.removeArrangedSubview($0)
  299. $0.removeFromSuperview()
  300. }
  301. configureFeatureShortcutCards()
  302. if let selectedIndex, let shortcut = FeatureShortcut(rawValue: selectedIndex) {
  303. selectFeatureShortcut(shortcut)
  304. }
  305. }
  306. private func setupLayout() {
  307. wantsLayer = true
  308. // Match chrome so the outer margin (inset chrome container) is grey, not an extra white ring.
  309. layer?.backgroundColor = Theme.chromeBackground.cgColor
  310. panelsRow.translatesAutoresizingMaskIntoConstraints = false
  311. chromeContainer.translatesAutoresizingMaskIntoConstraints = false
  312. chromeContainer.wantsLayer = true
  313. chromeContainer.layer?.backgroundColor = Theme.chromeBackground.cgColor
  314. chromeContainer.layer?.cornerRadius = 18
  315. chromeContainer.layer?.masksToBounds = true
  316. addSubview(chromeContainer)
  317. chromeContainer.addSubview(panelsRow)
  318. sidebar.orientation = .vertical
  319. sidebar.spacing = 10
  320. sidebar.distribution = .fill
  321. sidebar.alignment = .leading
  322. sidebar.wantsLayer = true
  323. sidebar.layer?.backgroundColor = Theme.sidebarBackground.cgColor
  324. sidebar.layer?.cornerRadius = 16
  325. sidebar.edgeInsets = NSEdgeInsets(top: 20, left: 6, bottom: 6, right: 6)
  326. sidebar.translatesAutoresizingMaskIntoConstraints = false
  327. mainHost.translatesAutoresizingMaskIntoConstraints = false
  328. mainHost.wantsLayer = true
  329. mainHost.layer?.backgroundColor = Theme.mainHostBackground.cgColor
  330. mainHost.layer?.cornerRadius = 16
  331. mainHost.layer?.masksToBounds = true
  332. sidebar.setContentHuggingPriority(.required, for: .horizontal)
  333. mainHost.setContentHuggingPriority(.defaultLow, for: .horizontal)
  334. mainHost.addSubview(mainOverlay)
  335. configureNonHomePlaceholder()
  336. mainHost.addSubview(nonHomeHost)
  337. indeedJobBrowserHost.translatesAutoresizingMaskIntoConstraints = false
  338. indeedJobBrowserHost.wantsLayer = true
  339. indeedJobBrowserHost.layer?.backgroundColor = Theme.mainHostBackground.cgColor
  340. indeedJobBrowserHost.isHidden = true
  341. mainHost.addSubview(indeedJobBrowserHost)
  342. mainOverlay.orientation = .vertical
  343. mainOverlay.spacing = 0
  344. mainOverlay.alignment = .centerX
  345. mainOverlay.distribution = .fill
  346. mainOverlay.translatesAutoresizingMaskIntoConstraints = false
  347. mainOverlay.setContentHuggingPriority(.defaultLow, for: .vertical)
  348. greetingLabel.font = .systemFont(ofSize: 28, weight: .bold)
  349. greetingLabel.textColor = Theme.welcomeHeroHeadingBlue
  350. greetingLabel.alignment = .center
  351. greetingLabel.maximumNumberOfLines = 1
  352. subtitleLabel.font = .systemFont(ofSize: 14, weight: .regular)
  353. subtitleLabel.textColor = Theme.welcomeHeroSubtitleText
  354. subtitleLabel.alignment = .center
  355. subtitleLabel.maximumNumberOfLines = 2
  356. let topInset = NSView()
  357. topInset.translatesAutoresizingMaskIntoConstraints = false
  358. topInset.heightAnchor.constraint(equalToConstant: 12).isActive = true
  359. configureSearchBar()
  360. configureChatViews()
  361. welcomeHeroHost.translatesAutoresizingMaskIntoConstraints = false
  362. welcomeHeroBackgroundView.translatesAutoresizingMaskIntoConstraints = false
  363. welcomeHeroBackgroundView.waveTint = Theme.welcomeHeroWaveTint
  364. welcomeLogoWell.translatesAutoresizingMaskIntoConstraints = false
  365. welcomeLogoWell.wantsLayer = true
  366. welcomeLogoWell.layer?.backgroundColor = Theme.welcomeHeroIconWell.cgColor
  367. welcomeLogoWell.layer?.cornerRadius = 28
  368. if #available(macOS 11.0, *) {
  369. welcomeLogoWell.layer?.cornerCurve = .continuous
  370. }
  371. welcomeLogoView.translatesAutoresizingMaskIntoConstraints = false
  372. welcomeLogoWell.addSubview(welcomeLogoView)
  373. let welcomeHeroContent = NSStackView(views: [welcomeLogoWell, greetingLabel, subtitleLabel])
  374. welcomeHeroContent.orientation = .vertical
  375. welcomeHeroContent.spacing = 8
  376. welcomeHeroContent.alignment = .centerX
  377. welcomeHeroContent.translatesAutoresizingMaskIntoConstraints = false
  378. welcomeHeroHost.addSubview(welcomeHeroBackgroundView)
  379. welcomeHeroHost.addSubview(welcomeHeroContent)
  380. welcomeHeroHost.setContentHuggingPriority(.defaultHigh, for: .vertical)
  381. NSLayoutConstraint.activate([
  382. welcomeHeroBackgroundView.leadingAnchor.constraint(equalTo: welcomeHeroHost.leadingAnchor),
  383. welcomeHeroBackgroundView.trailingAnchor.constraint(equalTo: welcomeHeroHost.trailingAnchor),
  384. welcomeHeroBackgroundView.topAnchor.constraint(equalTo: welcomeHeroHost.topAnchor),
  385. welcomeHeroBackgroundView.bottomAnchor.constraint(equalTo: welcomeHeroHost.bottomAnchor),
  386. welcomeHeroContent.centerXAnchor.constraint(equalTo: welcomeHeroHost.centerXAnchor),
  387. welcomeHeroContent.leadingAnchor.constraint(greaterThanOrEqualTo: welcomeHeroHost.leadingAnchor, constant: 16),
  388. welcomeHeroContent.trailingAnchor.constraint(lessThanOrEqualTo: welcomeHeroHost.trailingAnchor, constant: -16),
  389. welcomeHeroContent.topAnchor.constraint(equalTo: welcomeHeroHost.topAnchor, constant: 4),
  390. welcomeHeroContent.bottomAnchor.constraint(equalTo: welcomeHeroHost.bottomAnchor, constant: -2),
  391. welcomeLogoWell.widthAnchor.constraint(equalToConstant: 56),
  392. welcomeLogoWell.heightAnchor.constraint(equalToConstant: 56),
  393. welcomeLogoView.centerXAnchor.constraint(equalTo: welcomeLogoWell.centerXAnchor),
  394. welcomeLogoView.centerYAnchor.constraint(equalTo: welcomeLogoWell.centerYAnchor)
  395. ])
  396. configureFeatureShortcutCards()
  397. featureCardsRow.setContentHuggingPriority(.defaultHigh, for: .vertical)
  398. let heroCardsSpacer = NSView()
  399. heroCardsSpacer.translatesAutoresizingMaskIntoConstraints = false
  400. heroCardsSpacer.heightAnchor.constraint(equalToConstant: 14).isActive = true
  401. let midSpacer = NSView()
  402. midSpacer.translatesAutoresizingMaskIntoConstraints = false
  403. midSpacer.heightAnchor.constraint(equalToConstant: 6).isActive = true
  404. let chatTopSpacer = NSView()
  405. chatTopSpacer.translatesAutoresizingMaskIntoConstraints = false
  406. chatTopSpacer.heightAnchor.constraint(equalToConstant: 8).isActive = true
  407. let chatBottomSpacer = NSView()
  408. chatBottomSpacer.translatesAutoresizingMaskIntoConstraints = false
  409. chatBottomSpacer.heightAnchor.constraint(equalToConstant: 8).isActive = true
  410. mainOverlay.addArrangedSubview(topInset)
  411. mainOverlay.addArrangedSubview(welcomeHeroHost)
  412. mainOverlay.addArrangedSubview(heroCardsSpacer)
  413. mainOverlay.addArrangedSubview(featureCardsRow)
  414. mainOverlay.addArrangedSubview(midSpacer)
  415. mainOverlay.addArrangedSubview(chatTopSpacer)
  416. let chatHeaderRow = NSStackView()
  417. chatHeaderRow.orientation = .horizontal
  418. chatHeaderRow.spacing = 8
  419. chatHeaderRow.alignment = .centerY
  420. chatHeaderRow.distribution = .fill
  421. chatHeaderRow.translatesAutoresizingMaskIntoConstraints = false
  422. let chatHeaderLeadingSpacer = NSView()
  423. chatHeaderLeadingSpacer.translatesAutoresizingMaskIntoConstraints = false
  424. chatHeaderLeadingSpacer.setContentHuggingPriority(.defaultLow, for: .horizontal)
  425. clearChatButton.translatesAutoresizingMaskIntoConstraints = false
  426. clearChatButton.bezelStyle = .rounded
  427. clearChatButton.font = .systemFont(ofSize: 12, weight: .medium)
  428. clearChatButton.target = self
  429. clearChatButton.action = #selector(didTapClearChat)
  430. clearChatButton.toolTip = "Remove all messages and start a new conversation"
  431. clearChatButton.setContentHuggingPriority(.required, for: .horizontal)
  432. chatHeaderRow.addArrangedSubview(chatHeaderLeadingSpacer)
  433. chatHeaderRow.addArrangedSubview(clearChatButton)
  434. mainOverlay.addArrangedSubview(chatHeaderRow)
  435. mainOverlay.addArrangedSubview(chatScrollView)
  436. mainOverlay.addArrangedSubview(chatBottomSpacer)
  437. mainOverlay.addArrangedSubview(searchBarShadowHost)
  438. panelsRow.addSubview(sidebar)
  439. panelsRow.addSubview(mainHost)
  440. NSLayoutConstraint.activate([
  441. chromeContainer.leadingAnchor.constraint(equalTo: leadingAnchor, constant: 6),
  442. chromeContainer.trailingAnchor.constraint(equalTo: trailingAnchor, constant: -6),
  443. chromeContainer.topAnchor.constraint(equalTo: topAnchor, constant: 6),
  444. chromeContainer.bottomAnchor.constraint(equalTo: bottomAnchor, constant: -6),
  445. panelsRow.leadingAnchor.constraint(equalTo: chromeContainer.leadingAnchor, constant: 6),
  446. panelsRow.trailingAnchor.constraint(equalTo: chromeContainer.trailingAnchor, constant: -8),
  447. panelsRow.topAnchor.constraint(equalTo: safeAreaLayoutGuide.topAnchor, constant: 2),
  448. panelsRow.bottomAnchor.constraint(equalTo: chromeContainer.bottomAnchor, constant: -8),
  449. sidebar.leadingAnchor.constraint(equalTo: panelsRow.leadingAnchor),
  450. sidebar.topAnchor.constraint(equalTo: panelsRow.topAnchor),
  451. sidebar.bottomAnchor.constraint(equalTo: panelsRow.bottomAnchor),
  452. sidebar.widthAnchor.constraint(equalToConstant: 218),
  453. mainHost.leadingAnchor.constraint(equalTo: sidebar.trailingAnchor, constant: 6),
  454. mainHost.trailingAnchor.constraint(equalTo: panelsRow.trailingAnchor),
  455. mainHost.topAnchor.constraint(equalTo: panelsRow.topAnchor),
  456. mainHost.bottomAnchor.constraint(equalTo: panelsRow.bottomAnchor),
  457. mainHost.widthAnchor.constraint(greaterThanOrEqualToConstant: 720),
  458. mainOverlay.leadingAnchor.constraint(equalTo: mainHost.leadingAnchor),
  459. mainOverlay.trailingAnchor.constraint(equalTo: mainHost.trailingAnchor),
  460. mainOverlay.topAnchor.constraint(equalTo: mainHost.topAnchor),
  461. mainOverlay.bottomAnchor.constraint(equalTo: mainHost.bottomAnchor, constant: -24),
  462. nonHomeHost.leadingAnchor.constraint(equalTo: mainHost.leadingAnchor),
  463. nonHomeHost.trailingAnchor.constraint(equalTo: mainHost.trailingAnchor),
  464. nonHomeHost.topAnchor.constraint(equalTo: mainHost.topAnchor),
  465. nonHomeHost.bottomAnchor.constraint(equalTo: mainHost.bottomAnchor, constant: -24),
  466. indeedJobBrowserHost.leadingAnchor.constraint(equalTo: mainHost.leadingAnchor),
  467. indeedJobBrowserHost.trailingAnchor.constraint(equalTo: mainHost.trailingAnchor),
  468. indeedJobBrowserHost.topAnchor.constraint(equalTo: mainHost.topAnchor),
  469. indeedJobBrowserHost.bottomAnchor.constraint(equalTo: mainHost.bottomAnchor, constant: -24),
  470. searchBarShadowHost.widthAnchor.constraint(equalTo: mainOverlay.widthAnchor, multiplier: 0.92),
  471. featureCardsRow.widthAnchor.constraint(equalTo: mainOverlay.widthAnchor, multiplier: 0.92),
  472. chatHeaderRow.widthAnchor.constraint(equalTo: mainOverlay.widthAnchor, multiplier: 0.92),
  473. chatScrollView.widthAnchor.constraint(equalTo: mainOverlay.widthAnchor, multiplier: 0.92),
  474. welcomeHeroHost.leadingAnchor.constraint(equalTo: mainOverlay.leadingAnchor),
  475. welcomeHeroHost.trailingAnchor.constraint(equalTo: mainOverlay.trailingAnchor),
  476. greetingLabel.leadingAnchor.constraint(equalTo: mainOverlay.leadingAnchor, constant: 16),
  477. greetingLabel.trailingAnchor.constraint(equalTo: mainOverlay.trailingAnchor, constant: -16),
  478. subtitleLabel.leadingAnchor.constraint(equalTo: greetingLabel.leadingAnchor),
  479. subtitleLabel.trailingAnchor.constraint(equalTo: greetingLabel.trailingAnchor),
  480. welcomeHeroContent.widthAnchor.constraint(equalTo: mainOverlay.widthAnchor, constant: -32)
  481. ])
  482. registerSubscriptionObserverOnce()
  483. }
  484. private func registerSubscriptionObserverOnce() {
  485. guard subscriptionObserver == nil else { return }
  486. subscriptionObserver = NotificationCenter.default.addObserver(
  487. forName: .subscriptionStatusDidChange,
  488. object: nil,
  489. queue: .main
  490. ) { [weak self] _ in
  491. self?.applyProSubscriptionToSidebar()
  492. }
  493. }
  494. private func applyProSubscriptionToSidebar() {
  495. let active = SubscriptionStore.shared.isProActive
  496. sidebarUpgradeCard?.isHidden = false
  497. guard let headline = sidebarUpgradeHeadline,
  498. let upgradeDescription = sidebarUpgradeDescription,
  499. let upgradeButton = sidebarUpgradeButton else { return }
  500. let descriptionWidth: CGFloat = 158
  501. if active {
  502. headline.stringValue = "You're on Pro"
  503. upgradeDescription.stringValue = "Manage billing, renewals, and plans in Premium."
  504. upgradeDescription.preferredMaxLayoutWidth = descriptionWidth
  505. upgradeButton.title = "Manage Subscription"
  506. } else {
  507. headline.stringValue = "Upgrade to Pro"
  508. upgradeDescription.stringValue = "Unlimited AI matches, smart alerts, and interview prep—all in one place."
  509. upgradeDescription.preferredMaxLayoutWidth = descriptionWidth
  510. upgradeButton.title = "Try Pro"
  511. }
  512. }
  513. /// Returns `false` and presents the paywall when the user does not have an active Pro subscription.
  514. @discardableResult
  515. private func ensureProAccess() -> Bool {
  516. guard SubscriptionStore.shared.isProActive else {
  517. presentPremiumPlansSheet()
  518. return false
  519. }
  520. return true
  521. }
  522. private func presentPremiumPlansSheet() {
  523. guard let hostWindow = window else { return }
  524. if premiumPlansWindowController == nil {
  525. premiumPlansWindowController = PremiumPlansWindowController()
  526. }
  527. guard let paywallWindow = premiumPlansWindowController?.window else { return }
  528. if hostWindow.attachedSheet === paywallWindow {
  529. return
  530. }
  531. paywallWindow.styleMask = [.borderless, .closable, .resizable]
  532. paywallWindow.isOpaque = true
  533. paywallWindow.backgroundColor = PremiumPlansWindowController.paywallSheetBackground
  534. let hostContentRect = hostWindow.contentRect(forFrameRect: hostWindow.frame)
  535. let overscan = PremiumSheetLayout.overscanPerEdge
  536. var expandedContentRect = hostContentRect.insetBy(dx: -overscan, dy: -overscan)
  537. expandedContentRect.size.height += PremiumSheetLayout.overscanExtraTop
  538. let paywallFrame = paywallWindow.frameRect(forContentRect: expandedContentRect)
  539. paywallWindow.setFrame(paywallFrame, display: false)
  540. let lockedSize = paywallWindow.frame.size
  541. paywallWindow.minSize = lockedSize
  542. paywallWindow.maxSize = lockedSize
  543. hostWindow.beginSheet(paywallWindow)
  544. }
  545. private func configureFeatureShortcutCards() {
  546. featureCardsRow.orientation = .horizontal
  547. featureCardsRow.spacing = 16
  548. featureCardsRow.distribution = .fillEqually
  549. featureCardsRow.alignment = .top
  550. featureCardsRow.translatesAutoresizingMaskIntoConstraints = false
  551. let specs: [(symbol: String, title: String, subtitle: String, action: Selector)] = [
  552. ("briefcase", "Role", "Explore similar or better job roles", #selector(didTapFeatureRole)),
  553. ("building.2", "Company", "Find opportunities at other companies", #selector(didTapFeatureCompany)),
  554. ("chevron.left.forwardslash.chevron.right", "Skill", "Match jobs that fit your skills", #selector(didTapFeatureSkill))
  555. ]
  556. for spec in specs {
  557. let card = FeatureShortcutCardView(
  558. symbolName: spec.symbol,
  559. title: spec.title,
  560. subtitle: spec.subtitle,
  561. target: self,
  562. action: spec.action
  563. )
  564. featureCardsRow.addArrangedSubview(card)
  565. }
  566. }
  567. private func configureChatViews() {
  568. chatDocumentView.translatesAutoresizingMaskIntoConstraints = false
  569. chatStack.orientation = .vertical
  570. chatStack.spacing = 18
  571. chatStack.alignment = .width
  572. chatStack.distribution = .fill
  573. chatStack.translatesAutoresizingMaskIntoConstraints = false
  574. chatDocumentView.addSubview(chatStack)
  575. NSLayoutConstraint.activate([
  576. chatStack.leadingAnchor.constraint(equalTo: chatDocumentView.leadingAnchor, constant: 4),
  577. chatStack.trailingAnchor.constraint(equalTo: chatDocumentView.trailingAnchor, constant: -4),
  578. chatStack.topAnchor.constraint(equalTo: chatDocumentView.topAnchor, constant: 8),
  579. chatStack.bottomAnchor.constraint(equalTo: chatDocumentView.bottomAnchor, constant: -8)
  580. ])
  581. chatScrollView.translatesAutoresizingMaskIntoConstraints = false
  582. chatScrollView.hasVerticalScroller = true
  583. chatScrollView.hasHorizontalScroller = false
  584. // Legacy reserves a dedicated track to the right of the clip view so the thumb never sits on top of cards/buttons.
  585. chatScrollView.scrollerStyle = .legacy
  586. chatScrollView.autohidesScrollers = true
  587. chatScrollView.drawsBackground = false
  588. chatScrollView.borderType = .noBorder
  589. chatScrollView.documentView = chatDocumentView
  590. chatScrollView.setContentHuggingPriority(.defaultLow, for: .vertical)
  591. chatScrollView.setContentCompressionResistancePriority(.defaultLow, for: .vertical)
  592. // Match Saved Jobs: pin document width to the clip view so cards and bubbles track window width instead of sticking to a narrow intrinsic width.
  593. NSLayoutConstraint.activate([
  594. chatDocumentView.topAnchor.constraint(equalTo: chatScrollView.contentView.topAnchor),
  595. chatDocumentView.leadingAnchor.constraint(equalTo: chatScrollView.contentView.leadingAnchor),
  596. chatDocumentView.widthAnchor.constraint(equalTo: chatScrollView.contentView.widthAnchor)
  597. ])
  598. }
  599. private var prefersReducedMotion: Bool {
  600. NSWorkspace.shared.accessibilityDisplayShouldReduceMotion
  601. }
  602. private func updateJobListingDescriptionWidths() {
  603. updateDescriptionColumnWidths(in: savedJobsStack, containerWidth: savedJobsDocumentView.bounds.width)
  604. walkChatJobStacks { stack in
  605. updateDescriptionColumnWidths(in: stack, containerWidth: stack.bounds.width)
  606. }
  607. }
  608. private func walkChatJobStacks(_ visitor: (ChatJobsStackView) -> Void) {
  609. func walk(_ view: NSView) {
  610. if let stack = view as? ChatJobsStackView {
  611. visitor(stack)
  612. }
  613. for sub in view.subviews { walk(sub) }
  614. }
  615. walk(chatStack)
  616. }
  617. /// Chat bubble text fields are wrapping labels whose `preferredMaxLayoutWidth` needs to track the available row width so long strings reflow correctly when the window resizes.
  618. private func updateChatBubbleWidths() {
  619. func walk(_ view: NSView) {
  620. if let label = view as? ChatBubbleLabel,
  621. let bubble = label.superview, bubble.bounds.width > 1 {
  622. let target = max(40, bubble.bounds.width - 28)
  623. if abs(label.preferredMaxLayoutWidth - target) > 0.5 {
  624. label.preferredMaxLayoutWidth = target
  625. label.invalidateIntrinsicContentSize()
  626. }
  627. }
  628. for sub in view.subviews { walk(sub) }
  629. }
  630. walk(chatStack)
  631. }
  632. private func updateDescriptionColumnWidths(in stack: NSStackView, containerWidth: CGFloat) {
  633. guard containerWidth > 1 else { return }
  634. // Matches `contentColumn` insets on the card (16 leading + 16 trailing). The description spans the full row below the buttons, so never subtract a “button strip” here — a too-narrow `preferredMaxLayoutWidth` inside a wider field makes wrapped `NSTextField` text lay out like a trailing-aligned band after resizes.
  635. let contentHorizontalInset: CGFloat = 32
  636. var didChange = false
  637. for card in stack.arrangedSubviews {
  638. guard let desc = card.viewWithTag(502) as? NSTextField else { continue }
  639. let cardWidth = card.bounds.width > 1 ? card.bounds.width : containerWidth
  640. let fallbackColumn = max(1, cardWidth - contentHorizontalInset)
  641. let columnWidth: CGFloat
  642. if desc.bounds.width > 1 {
  643. columnWidth = desc.bounds.width
  644. } else if let column = desc.superview, column.bounds.width > 1 {
  645. columnWidth = column.bounds.width
  646. } else {
  647. columnWidth = fallbackColumn
  648. }
  649. if abs(desc.preferredMaxLayoutWidth - columnWidth) > 0.5 {
  650. desc.preferredMaxLayoutWidth = columnWidth
  651. desc.invalidateIntrinsicContentSize()
  652. didChange = true
  653. }
  654. }
  655. if didChange {
  656. stack.needsLayout = true
  657. }
  658. }
  659. private static func normalizedSavedJobs(_ jobs: [JobListing]) -> [JobListing] {
  660. var seen = Set<JobListing>()
  661. var out: [JobListing] = []
  662. for job in jobs where seen.insert(job).inserted {
  663. out.append(job)
  664. }
  665. return out
  666. }
  667. private func isJobSaved(_ job: JobListing) -> Bool {
  668. savedJobOrder.contains(job)
  669. }
  670. private func persistSavedJobs() {
  671. SavedJobsStore.save(savedJobOrder)
  672. }
  673. private func applySavedState(_ saved: Bool, for job: JobListing) {
  674. if saved {
  675. savedJobOrder.removeAll { $0 == job }
  676. savedJobOrder.insert(job, at: 0)
  677. } else {
  678. savedJobOrder.removeAll { $0 == job }
  679. }
  680. persistSavedJobs()
  681. }
  682. private func jobListingHostSubtitle(_ job: JobListing) -> String {
  683. guard let raw = job.url, let url = URL(string: raw), let host = url.host?.lowercased() else {
  684. return "Indeed"
  685. }
  686. if host.hasPrefix("www.") {
  687. return String(host.dropFirst(4))
  688. }
  689. return host
  690. }
  691. private func jobListingCategorySymbol(for job: JobListing) -> String {
  692. let blob = (job.title + " " + job.description).lowercased()
  693. if blob.contains("machine learning") || blob.contains("deep learning") || blob.contains(" ml ") {
  694. return "brain.head.profile"
  695. }
  696. if blob.contains("audio") || blob.contains(" sound ") || blob.contains("dsp") {
  697. return "waveform"
  698. }
  699. if blob.contains("ios") || blob.contains("swift") || blob.contains("mobile") {
  700. return "iphone"
  701. }
  702. if blob.contains("design") || blob.contains(" ux") || blob.contains("figma") {
  703. return "paintpalette.fill"
  704. }
  705. if blob.contains("data ") || blob.contains("analytics") {
  706. return "chart.bar.fill"
  707. }
  708. if blob.contains("ai") || blob.contains("llm") || blob.contains("nlp") {
  709. return "cpu"
  710. }
  711. return "briefcase.fill"
  712. }
  713. private func makeJobListingCard(_ job: JobListing, context: JobListingCardContext) -> NSView {
  714. let card = NSView()
  715. card.translatesAutoresizingMaskIntoConstraints = false
  716. card.wantsLayer = true
  717. card.layer?.backgroundColor = Theme.cardBackground.cgColor
  718. card.layer?.cornerRadius = 14
  719. card.layer?.borderWidth = 1
  720. card.layer?.borderColor = Theme.border.cgColor
  721. card.layer?.masksToBounds = true
  722. let iconBox = NSView()
  723. iconBox.translatesAutoresizingMaskIntoConstraints = false
  724. iconBox.wantsLayer = true
  725. iconBox.layer?.backgroundColor = Theme.brandBlue.cgColor
  726. iconBox.layer?.cornerRadius = 12
  727. if #available(macOS 11.0, *) {
  728. iconBox.layer?.cornerCurve = .continuous
  729. }
  730. let categoryIcon = NSImageView()
  731. categoryIcon.translatesAutoresizingMaskIntoConstraints = false
  732. categoryIcon.symbolConfiguration = NSImage.SymbolConfiguration(pointSize: 22, weight: .medium)
  733. categoryIcon.image = NSImage(systemSymbolName: jobListingCategorySymbol(for: job), accessibilityDescription: nil)
  734. categoryIcon.contentTintColor = .white
  735. iconBox.addSubview(categoryIcon)
  736. let titleField = NSTextField(labelWithString: job.title)
  737. titleField.font = .systemFont(ofSize: 16, weight: .semibold)
  738. titleField.textColor = Theme.brandBlue
  739. titleField.maximumNumberOfLines = 2
  740. titleField.lineBreakMode = .byWordWrapping
  741. titleField.alignment = .left
  742. titleField.setContentCompressionResistancePriority(.defaultLow, for: .horizontal)
  743. titleField.translatesAutoresizingMaskIntoConstraints = false
  744. let buildingIcon = NSImageView()
  745. buildingIcon.translatesAutoresizingMaskIntoConstraints = false
  746. buildingIcon.symbolConfiguration = NSImage.SymbolConfiguration(pointSize: 12, weight: .medium)
  747. buildingIcon.image = NSImage(systemSymbolName: "building.2.fill", accessibilityDescription: nil)
  748. buildingIcon.contentTintColor = Theme.welcomeSubtitleText
  749. let companyLabel = NSTextField(labelWithString: jobListingHostSubtitle(job))
  750. companyLabel.font = .systemFont(ofSize: 12, weight: .medium)
  751. companyLabel.textColor = Theme.welcomeSubtitleText
  752. companyLabel.maximumNumberOfLines = 1
  753. companyLabel.lineBreakMode = .byTruncatingTail
  754. companyLabel.translatesAutoresizingMaskIntoConstraints = false
  755. let companyRow = NSStackView(views: [buildingIcon, companyLabel])
  756. companyRow.orientation = .horizontal
  757. companyRow.spacing = 5
  758. companyRow.alignment = .centerY
  759. companyRow.translatesAutoresizingMaskIntoConstraints = false
  760. let descriptionField = NSTextField(wrappingLabelWithString: job.description)
  761. descriptionField.font = .systemFont(ofSize: 13, weight: .regular)
  762. descriptionField.textColor = Theme.secondaryText
  763. descriptionField.maximumNumberOfLines = 2
  764. descriptionField.lineBreakMode = .byWordWrapping
  765. descriptionField.alignment = .left
  766. descriptionField.baseWritingDirection = .leftToRight
  767. descriptionField.attributedStringValue = Self.jobListingDescriptionAttributedString(job.description)
  768. if let cell = descriptionField.cell as? NSTextFieldCell {
  769. cell.alignment = .left
  770. cell.wraps = true
  771. }
  772. descriptionField.setContentHuggingPriority(.defaultLow, for: .horizontal)
  773. descriptionField.setContentCompressionResistancePriority(.defaultLow, for: .horizontal)
  774. descriptionField.tag = 502
  775. descriptionField.translatesAutoresizingMaskIntoConstraints = false
  776. let applyButton = JobPayloadButton(title: "Apply", target: self, action: #selector(didTapJobApply(_:)))
  777. applyButton.jobPayload = job
  778. applyButton.cardContext = context
  779. applyButton.isBordered = false
  780. applyButton.bezelStyle = .rounded
  781. applyButton.font = .systemFont(ofSize: 13, weight: .semibold)
  782. applyButton.wantsLayer = true
  783. applyButton.layer?.cornerRadius = 8
  784. applyButton.layer?.backgroundColor = Theme.brandBlue.cgColor
  785. applyButton.contentTintColor = Theme.proCTAText
  786. applyButton.focusRingType = .none
  787. applyButton.pointerCursor = true
  788. applyButton.hoverHandler = { [weak applyButton] hovering in
  789. applyButton?.layer?.backgroundColor = (hovering ? Theme.brandBlueHover : Theme.brandBlue).cgColor
  790. }
  791. applyButton.setContentHuggingPriority(.required, for: .horizontal)
  792. applyButton.setContentCompressionResistancePriority(.required, for: .horizontal)
  793. let savedOn = isJobSaved(job)
  794. let savedButton = SaveJobPayloadButton(title: savedOn ? "Saved" : "Save", target: self, action: #selector(didTapJobSaved(_:)))
  795. savedButton.jobPayload = job
  796. savedButton.cardContext = context
  797. savedButton.setButtonType(.toggle)
  798. savedButton.isBordered = false
  799. savedButton.bezelStyle = .rounded
  800. savedButton.font = .systemFont(ofSize: 13, weight: .semibold)
  801. savedButton.image = NSImage(systemSymbolName: savedOn ? "heart.fill" : "heart", accessibilityDescription: nil)
  802. savedButton.imagePosition = .imageLeading
  803. savedButton.symbolConfiguration = NSImage.SymbolConfiguration(pointSize: 11, weight: .semibold)
  804. savedButton.focusRingType = .none
  805. savedButton.state = savedOn ? .on : .off
  806. savedButton.pointerCursor = true
  807. savedButton.hoverHandler = { [weak self, weak savedButton] _ in
  808. guard let savedButton = savedButton else { return }
  809. self?.styleJobSavedButton(savedButton)
  810. }
  811. styleJobSavedButton(savedButton)
  812. savedButton.setContentHuggingPriority(.required, for: .horizontal)
  813. savedButton.setContentCompressionResistancePriority(.required, for: .horizontal)
  814. let dismissButton = JobPayloadButton()
  815. dismissButton.jobPayload = job
  816. dismissButton.cardContext = context
  817. dismissButton.image = NSImage(systemSymbolName: "xmark", accessibilityDescription: "Dismiss")
  818. dismissButton.imagePosition = .imageOnly
  819. dismissButton.imageScaling = .scaleProportionallyDown
  820. dismissButton.symbolConfiguration = NSImage.SymbolConfiguration(pointSize: 12, weight: .semibold)
  821. dismissButton.isBordered = false
  822. dismissButton.bezelStyle = .rounded
  823. dismissButton.contentTintColor = Theme.secondaryText
  824. dismissButton.target = self
  825. dismissButton.action = #selector(didTapJobDismiss(_:))
  826. dismissButton.toolTip = context == .savedJobsPage ? "Remove from saved" : "Dismiss"
  827. dismissButton.focusRingType = .none
  828. dismissButton.wantsLayer = true
  829. dismissButton.layer?.cornerRadius = 8
  830. dismissButton.layer?.backgroundColor = NSColor.clear.cgColor
  831. dismissButton.pointerCursor = true
  832. dismissButton.hoverHandler = { [weak dismissButton] hovering in
  833. dismissButton?.layer?.backgroundColor = (hovering ? Theme.neutralHoverFill : NSColor.clear).cgColor
  834. dismissButton?.contentTintColor = hovering ? Theme.primaryText : Theme.secondaryText
  835. }
  836. dismissButton.setContentHuggingPriority(.required, for: .horizontal)
  837. let buttonRow = NSStackView(views: [applyButton, savedButton, dismissButton])
  838. buttonRow.orientation = .horizontal
  839. buttonRow.spacing = 8
  840. buttonRow.alignment = .top
  841. buttonRow.translatesAutoresizingMaskIntoConstraints = false
  842. buttonRow.setContentHuggingPriority(.required, for: .horizontal)
  843. buttonRow.setContentCompressionResistancePriority(.required, for: .horizontal)
  844. buttonRow.setContentHuggingPriority(.required, for: .vertical)
  845. buttonRow.setContentCompressionResistancePriority(.required, for: .vertical)
  846. applyButton.setContentCompressionResistancePriority(.required, for: .horizontal)
  847. savedButton.setContentCompressionResistancePriority(.required, for: .horizontal)
  848. dismissButton.setContentCompressionResistancePriority(.required, for: .horizontal)
  849. let middleColumn = NSStackView(views: [titleField, companyRow, descriptionField])
  850. middleColumn.orientation = .vertical
  851. middleColumn.spacing = 5
  852. middleColumn.alignment = .leading
  853. middleColumn.translatesAutoresizingMaskIntoConstraints = false
  854. middleColumn.setContentHuggingPriority(.defaultLow, for: .horizontal)
  855. middleColumn.setContentCompressionResistancePriority(.defaultLow, for: .horizontal)
  856. let contentRow = NSStackView(views: [iconBox, middleColumn])
  857. contentRow.orientation = .horizontal
  858. contentRow.spacing = 14
  859. contentRow.alignment = .top
  860. contentRow.distribution = .fill
  861. contentRow.translatesAutoresizingMaskIntoConstraints = false
  862. card.addSubview(contentRow)
  863. card.addSubview(buttonRow)
  864. let actionCornerInset: CGFloat = 8
  865. let contentToActionsGap: CGFloat = 12
  866. let bodyTrailingInset: CGFloat = 16
  867. NSLayoutConstraint.activate([
  868. contentRow.leadingAnchor.constraint(equalTo: card.leadingAnchor, constant: 16),
  869. contentRow.trailingAnchor.constraint(equalTo: card.trailingAnchor, constant: -bodyTrailingInset),
  870. contentRow.topAnchor.constraint(equalTo: card.topAnchor, constant: 14),
  871. contentRow.bottomAnchor.constraint(equalTo: card.bottomAnchor, constant: -14),
  872. buttonRow.topAnchor.constraint(equalTo: card.topAnchor, constant: actionCornerInset),
  873. buttonRow.trailingAnchor.constraint(equalTo: card.trailingAnchor, constant: -actionCornerInset),
  874. middleColumn.trailingAnchor.constraint(lessThanOrEqualTo: buttonRow.leadingAnchor, constant: -contentToActionsGap),
  875. iconBox.widthAnchor.constraint(equalToConstant: 58),
  876. iconBox.heightAnchor.constraint(equalToConstant: 58),
  877. categoryIcon.centerXAnchor.constraint(equalTo: iconBox.centerXAnchor),
  878. categoryIcon.centerYAnchor.constraint(equalTo: iconBox.centerYAnchor),
  879. buildingIcon.widthAnchor.constraint(equalToConstant: 14),
  880. buildingIcon.heightAnchor.constraint(equalToConstant: 14),
  881. applyButton.widthAnchor.constraint(greaterThanOrEqualToConstant: 76),
  882. applyButton.heightAnchor.constraint(equalToConstant: 32),
  883. savedButton.widthAnchor.constraint(greaterThanOrEqualToConstant: 84),
  884. savedButton.heightAnchor.constraint(equalToConstant: 32),
  885. dismissButton.widthAnchor.constraint(equalToConstant: 32),
  886. dismissButton.heightAnchor.constraint(equalToConstant: 32),
  887. descriptionField.widthAnchor.constraint(equalTo: middleColumn.widthAnchor)
  888. ])
  889. return card
  890. }
  891. private func styleJobSavedButton(_ button: NSButton) {
  892. button.wantsLayer = true
  893. button.layer?.cornerRadius = 10
  894. let hovering = (button as? HoverableButton)?.isHovering ?? false
  895. // Reference: white surface, soft blue outline, brand blue icon + label (no tinted fill on hover).
  896. button.layer?.backgroundColor = Theme.cardBackground.cgColor
  897. button.layer?.borderWidth = 1
  898. button.layer?.borderColor = (hovering ? Theme.searchBarBorderHover : Theme.searchBarBorder).cgColor
  899. button.contentTintColor = Theme.brandBlue
  900. }
  901. @objc private func didTapJobApply(_ sender: NSButton) {
  902. guard let job = (sender as? JobPayloadButton)?.jobPayload else { return }
  903. presentIndeedJobBrowser(url: Self.resolvedIndeedApplyURL(for: job))
  904. }
  905. /// Apply always loads a live Indeed page. Single-job URLs (`/viewjob`, `/pagead`, `/rc/clk`, …) are often expired or incorrectly synthesized by the model and show Indeed’s 404; we only trust **search** URLs whose path is `/jobs`. Otherwise we open a fresh `/jobs?q=<title>` (same regional host when the model provided one).
  906. private static func resolvedIndeedApplyURL(for job: JobListing) -> URL {
  907. let title = job.title.trimmingCharacters(in: .whitespacesAndNewlines)
  908. let raw = job.url?.trimmingCharacters(in: .whitespacesAndNewlines) ?? ""
  909. var preferredHost: String?
  910. if !raw.isEmpty, let components = URLComponents(string: raw), let host = components.host {
  911. let lower = host.lowercased()
  912. if isIndeedApplyHost(lower) {
  913. preferredHost = host
  914. let pathLower = components.path.lowercased()
  915. let isJobsSearchPath = pathLower == "/jobs" || pathLower.hasSuffix("/jobs")
  916. if isJobsSearchPath, let url = components.url {
  917. return url
  918. }
  919. }
  920. }
  921. return indeedJobsSearchURL(title: title, preferredHost: preferredHost)
  922. }
  923. private static func indeedJobsSearchURL(title: String, preferredHost: String?) -> URL {
  924. let allowed = CharacterSet.urlQueryAllowed
  925. let q = title.addingPercentEncoding(withAllowedCharacters: allowed) ?? ""
  926. let host: String
  927. if let preferredHost, isIndeedApplyHost(preferredHost.lowercased()) {
  928. host = preferredHost
  929. } else {
  930. host = "www.indeed.com"
  931. }
  932. if let url = URL(string: "https://\(host)/jobs?q=\(q)") {
  933. return url
  934. }
  935. return URL(string: "https://www.indeed.com/jobs?q=\(q)")!
  936. }
  937. private static func isIndeedApplyHost(_ host: String) -> Bool {
  938. if host == "indeed.com" { return true }
  939. if host.hasPrefix("indeed.") { return true }
  940. return host.contains(".indeed.")
  941. }
  942. private static let indeedBrowseHomeURL = URL(string: "https://www.indeed.com/")!
  943. private static let externalIndeedAppURLSchemes = ["indeed://", "indeedjobs://"]
  944. private func selectIndeedSidebar() {
  945. isIndeedSidebarSelected = true
  946. if !isIndeedJobBrowserPresented {
  947. openIndeedFromSidebar()
  948. }
  949. configureSidebar()
  950. updateMainContentVisibility()
  951. }
  952. /// Opens the installed Indeed app when a handler is registered; otherwise loads Indeed in the embedded browser.
  953. private func openIndeedFromSidebar() {
  954. for scheme in Self.externalIndeedAppURLSchemes {
  955. guard let url = URL(string: scheme) else { continue }
  956. if NSWorkspace.shared.urlForApplication(toOpen: url) != nil {
  957. NSWorkspace.shared.open(url)
  958. return
  959. }
  960. }
  961. presentIndeedJobBrowser(url: Self.indeedBrowseHomeURL)
  962. }
  963. private func presentIndeedJobBrowser(url: URL) {
  964. guard let parentVC = hostingViewController else { return }
  965. if indeedJobBrowserViewController == nil {
  966. let vc = IndeedJobBrowserViewController()
  967. vc.onDismissEmbedded = { [weak self] in
  968. self?.dismissIndeedJobBrowserEmbedded()
  969. }
  970. vc.embed(in: indeedJobBrowserHost, parent: parentVC)
  971. indeedJobBrowserViewController = vc
  972. }
  973. indeedJobBrowserViewController?.loadPage(url)
  974. isIndeedJobBrowserPresented = true
  975. updateMainContentVisibility()
  976. }
  977. private func dismissIndeedJobBrowserEmbedded() {
  978. guard isIndeedJobBrowserPresented else { return }
  979. isIndeedJobBrowserPresented = false
  980. if isIndeedSidebarSelected {
  981. isIndeedSidebarSelected = false
  982. configureSidebar()
  983. }
  984. updateMainContentVisibility()
  985. }
  986. private var hostingViewController: NSViewController? {
  987. var responder: NSResponder? = self
  988. while let current = responder {
  989. if let viewController = current as? NSViewController {
  990. return viewController
  991. }
  992. responder = current.nextResponder
  993. }
  994. return nil
  995. }
  996. @objc private func didTapJobSaved(_ sender: NSButton) {
  997. guard let job = (sender as? JobPayloadButton)?.jobPayload else { return }
  998. let willSave = !isJobSaved(job)
  999. applySavedState(willSave, for: job)
  1000. sender.state = willSave ? .on : .off
  1001. sender.title = willSave ? "Saved" : "Save"
  1002. sender.image = NSImage(systemSymbolName: willSave ? "heart.fill" : "heart", accessibilityDescription: nil)
  1003. styleJobSavedButton(sender)
  1004. if isSavedJobsSidebarIndex(selectedSidebarIndex) {
  1005. reloadSavedJobsListings()
  1006. }
  1007. }
  1008. @objc private func didTapJobDismiss(_ sender: NSButton) {
  1009. guard let button = sender as? JobPayloadButton, let job = button.jobPayload else { return }
  1010. switch button.cardContext {
  1011. case .homeSearchResults:
  1012. removeJobCardFromChat(originating: button, job: job)
  1013. case .savedJobsPage:
  1014. applySavedState(false, for: job)
  1015. reloadSavedJobsListings()
  1016. }
  1017. }
  1018. /// Walks up from a dismiss button until it finds the enclosing chat job stack, then removes only the card that owns the button. Other chat history (older searches, the assistant summary text) is untouched.
  1019. private func removeJobCardFromChat(originating button: NSView, job: JobListing) {
  1020. var node: NSView? = button
  1021. var card: NSView?
  1022. var stack: ChatJobsStackView?
  1023. while let v = node {
  1024. if let parent = v.superview as? ChatJobsStackView {
  1025. card = v
  1026. stack = parent
  1027. break
  1028. }
  1029. node = v.superview
  1030. }
  1031. guard let card, let stack else { return }
  1032. stack.removeArrangedSubview(card)
  1033. card.removeFromSuperview()
  1034. lastSearchResults.removeAll { $0 == job }
  1035. }
  1036. private func configureSearchBar() {
  1037. let pillCorner: CGFloat = 27
  1038. let barHeight: CGFloat = 54
  1039. searchBarShadowHost.translatesAutoresizingMaskIntoConstraints = false
  1040. searchBarShadowHost.wantsLayer = true
  1041. searchBarShadowHost.layer?.masksToBounds = false
  1042. searchBarShadowHost.layer?.shadowColor = NSColor.black.withAlphaComponent(0.18).cgColor
  1043. searchBarShadowHost.layer?.shadowOffset = CGSize(width: 0, height: 2)
  1044. searchBarShadowHost.layer?.shadowRadius = 10
  1045. searchBarShadowHost.layer?.shadowOpacity = 1
  1046. searchBarShadowHost.setContentHuggingPriority(.defaultHigh, for: .vertical)
  1047. searchCard.translatesAutoresizingMaskIntoConstraints = false
  1048. searchCard.wantsLayer = true
  1049. searchCard.layer?.backgroundColor = Theme.cardBackground.cgColor
  1050. searchCard.layer?.cornerRadius = pillCorner
  1051. searchCard.layer?.borderWidth = 1
  1052. searchCard.layer?.borderColor = Theme.searchBarBorder.cgColor
  1053. searchCard.layer?.masksToBounds = true
  1054. searchCard.hoverHandler = { [weak self] hovering in
  1055. guard let self else { return }
  1056. CATransaction.begin()
  1057. CATransaction.setAnimationDuration(0.15)
  1058. self.searchCard.layer?.backgroundColor = (hovering ? Theme.neutralHoverFill : Theme.cardBackground).cgColor
  1059. self.searchCard.layer?.borderColor = (hovering ? Theme.searchBarBorderHover : Theme.searchBarBorder).cgColor
  1060. self.searchBarShadowHost.layer?.shadowColor = NSColor.black.withAlphaComponent(hovering ? 0.24 : 0.18).cgColor
  1061. self.searchBarShadowHost.layer?.shadowRadius = hovering ? 12 : 10
  1062. CATransaction.commit()
  1063. }
  1064. searchBarShadowHost.addSubview(searchCard)
  1065. func configureField(_ field: NSTextField, placeholder: String) {
  1066. field.translatesAutoresizingMaskIntoConstraints = false
  1067. field.isBordered = false
  1068. field.drawsBackground = false
  1069. field.focusRingType = .none
  1070. field.font = .systemFont(ofSize: 14, weight: .regular)
  1071. field.textColor = Theme.primaryText
  1072. field.delegate = self
  1073. field.placeholderAttributedString = NSAttributedString(
  1074. string: placeholder,
  1075. attributes: [
  1076. .foregroundColor: Theme.secondaryText,
  1077. .font: NSFont.systemFont(ofSize: 14, weight: .regular)
  1078. ]
  1079. )
  1080. field.cell?.usesSingleLineMode = true
  1081. field.cell?.wraps = false
  1082. field.cell?.isScrollable = true
  1083. field.target = self
  1084. field.action = #selector(didSubmitSearch)
  1085. }
  1086. jobSearchIcon.translatesAutoresizingMaskIntoConstraints = false
  1087. jobSearchIcon.symbolConfiguration = NSImage.SymbolConfiguration(pointSize: 16, weight: .semibold)
  1088. jobSearchIcon.image = NSImage(systemSymbolName: "sparkles", accessibilityDescription: "Ask AI")
  1089. jobSearchIcon.contentTintColor = Theme.brandBlue
  1090. configureField(jobKeywordsField, placeholder: "Ask for roles, skills, salary, or job descriptions...")
  1091. let ctaHeight: CGFloat = 42
  1092. let ctaCorner = ctaHeight / 2
  1093. findJobsCTAHost.translatesAutoresizingMaskIntoConstraints = false
  1094. findJobsCTAHost.wantsLayer = true
  1095. findJobsCTAHost.layer?.masksToBounds = false
  1096. findJobsCTAHost.layer?.shadowColor = NSColor.black.cgColor
  1097. findJobsCTAHost.layer?.shadowOpacity = 0.16
  1098. findJobsCTAHost.layer?.shadowOffset = CGSize(width: 0, height: 2)
  1099. findJobsCTAHost.layer?.shadowRadius = 6
  1100. let sendContentPadding: CGFloat = 16
  1101. findJobsCTAPill.translatesAutoresizingMaskIntoConstraints = false
  1102. findJobsCTAPill.wantsLayer = true
  1103. findJobsCTAPill.layer?.cornerRadius = ctaCorner
  1104. if #available(macOS 11.0, *) {
  1105. findJobsCTAPill.layer?.cornerCurve = .continuous
  1106. }
  1107. findJobsCTAPill.layer?.masksToBounds = true
  1108. findJobsCTAPill.layer?.backgroundColor = Theme.brandBlue.cgColor
  1109. sendIconView.translatesAutoresizingMaskIntoConstraints = false
  1110. sendIconView.symbolConfiguration = NSImage.SymbolConfiguration(pointSize: 11, weight: .semibold)
  1111. sendIconView.image = NSImage(systemSymbolName: "paperplane.fill", accessibilityDescription: nil)
  1112. sendIconView.contentTintColor = Theme.proCTAText
  1113. sendLabel.font = .systemFont(ofSize: 14, weight: .semibold)
  1114. sendLabel.textColor = Theme.proCTAText
  1115. sendLabel.alignment = .center
  1116. sendContentStack.orientation = .horizontal
  1117. sendContentStack.spacing = 6
  1118. sendContentStack.alignment = .centerY
  1119. sendContentStack.translatesAutoresizingMaskIntoConstraints = false
  1120. sendContentStack.addArrangedSubview(sendIconView)
  1121. sendContentStack.addArrangedSubview(sendLabel)
  1122. findJobsButton.translatesAutoresizingMaskIntoConstraints = false
  1123. findJobsButton.title = ""
  1124. findJobsButton.isBordered = false
  1125. findJobsButton.bezelStyle = .inline
  1126. findJobsButton.wantsLayer = true
  1127. findJobsButton.layer?.backgroundColor = NSColor.clear.cgColor
  1128. findJobsButton.focusRingType = .none
  1129. findJobsButton.pointerCursor = true
  1130. findJobsButton.target = self
  1131. findJobsButton.action = #selector(didSubmitSearch)
  1132. findJobsButton.setAccessibilityLabel("Send")
  1133. findJobsButton.hoverHandler = { [weak self] hovering in
  1134. self?.findJobsCTAPill.layer?.backgroundColor = (hovering ? Theme.brandBlueHover : Theme.brandBlue).cgColor
  1135. }
  1136. findJobsCTAHost.addSubview(findJobsCTAPill)
  1137. findJobsCTAHost.addSubview(sendContentStack)
  1138. findJobsCTAHost.addSubview(findJobsButton)
  1139. findJobsCTAHost.setContentHuggingPriority(.required, for: .horizontal)
  1140. findJobsCTAHost.setContentCompressionResistancePriority(.required, for: .horizontal)
  1141. NSLayoutConstraint.activate([
  1142. findJobsCTAPill.leadingAnchor.constraint(equalTo: findJobsCTAHost.leadingAnchor),
  1143. findJobsCTAPill.trailingAnchor.constraint(equalTo: findJobsCTAHost.trailingAnchor),
  1144. findJobsCTAPill.topAnchor.constraint(equalTo: findJobsCTAHost.topAnchor),
  1145. findJobsCTAPill.bottomAnchor.constraint(equalTo: findJobsCTAHost.bottomAnchor),
  1146. sendContentStack.centerXAnchor.constraint(equalTo: findJobsCTAPill.centerXAnchor),
  1147. sendContentStack.centerYAnchor.constraint(equalTo: findJobsCTAPill.centerYAnchor),
  1148. sendContentStack.leadingAnchor.constraint(greaterThanOrEqualTo: findJobsCTAPill.leadingAnchor, constant: sendContentPadding),
  1149. sendContentStack.trailingAnchor.constraint(lessThanOrEqualTo: findJobsCTAPill.trailingAnchor, constant: -sendContentPadding),
  1150. sendIconView.widthAnchor.constraint(equalToConstant: 14),
  1151. sendIconView.heightAnchor.constraint(equalToConstant: 14),
  1152. findJobsButton.leadingAnchor.constraint(equalTo: findJobsCTAHost.leadingAnchor),
  1153. findJobsButton.trailingAnchor.constraint(equalTo: findJobsCTAHost.trailingAnchor),
  1154. findJobsButton.topAnchor.constraint(equalTo: findJobsCTAHost.topAnchor),
  1155. findJobsButton.bottomAnchor.constraint(equalTo: findJobsCTAHost.bottomAnchor)
  1156. ])
  1157. let keywordsStack = NSStackView(views: [jobSearchIcon, jobKeywordsField])
  1158. keywordsStack.orientation = .horizontal
  1159. keywordsStack.spacing = 10
  1160. keywordsStack.alignment = .centerY
  1161. keywordsStack.translatesAutoresizingMaskIntoConstraints = false
  1162. keywordsStack.edgeInsets = NSEdgeInsets(top: 0, left: 18, bottom: 0, right: 10)
  1163. keywordsStack.setContentHuggingPriority(.defaultLow, for: .horizontal)
  1164. let row = NSStackView(views: [keywordsStack, findJobsCTAHost])
  1165. row.orientation = .horizontal
  1166. row.spacing = 10
  1167. row.alignment = .centerY
  1168. row.distribution = .fill
  1169. row.translatesAutoresizingMaskIntoConstraints = false
  1170. row.edgeInsets = NSEdgeInsets(top: 0, left: 0, bottom: 0, right: 7)
  1171. searchCard.addSubview(row)
  1172. NSLayoutConstraint.activate([
  1173. searchCard.leadingAnchor.constraint(equalTo: searchBarShadowHost.leadingAnchor),
  1174. searchCard.trailingAnchor.constraint(equalTo: searchBarShadowHost.trailingAnchor),
  1175. searchCard.topAnchor.constraint(equalTo: searchBarShadowHost.topAnchor),
  1176. searchCard.bottomAnchor.constraint(equalTo: searchBarShadowHost.bottomAnchor),
  1177. searchBarShadowHost.heightAnchor.constraint(equalToConstant: barHeight),
  1178. row.leadingAnchor.constraint(equalTo: searchCard.leadingAnchor),
  1179. row.trailingAnchor.constraint(equalTo: searchCard.trailingAnchor),
  1180. row.topAnchor.constraint(equalTo: searchCard.topAnchor),
  1181. row.bottomAnchor.constraint(equalTo: searchCard.bottomAnchor),
  1182. jobSearchIcon.widthAnchor.constraint(equalToConstant: 18),
  1183. jobSearchIcon.heightAnchor.constraint(equalToConstant: 18),
  1184. findJobsCTAHost.heightAnchor.constraint(equalToConstant: ctaHeight),
  1185. findJobsCTAHost.widthAnchor.constraint(greaterThanOrEqualTo: sendContentStack.widthAnchor, constant: sendContentPadding * 2)
  1186. ])
  1187. searchCard.hoverHandler = nil
  1188. }
  1189. private func updateFindJobsCTAShadowPath() {
  1190. guard findJobsCTAHost.bounds.width > 0, findJobsCTAHost.bounds.height > 0 else { return }
  1191. let r = findJobsCTAHost.bounds
  1192. let radius = min(r.height / 2, r.width / 2)
  1193. findJobsCTAHost.layer?.shadowPath = CGPath(
  1194. roundedRect: r,
  1195. cornerWidth: radius,
  1196. cornerHeight: radius,
  1197. transform: nil
  1198. )
  1199. }
  1200. private func configureNonHomePlaceholder() {
  1201. nonHomeHost.translatesAutoresizingMaskIntoConstraints = false
  1202. nonHomeHost.wantsLayer = true
  1203. nonHomeHost.layer?.backgroundColor = Theme.mainHostBackground.cgColor
  1204. nonHomeHost.isHidden = true
  1205. nonHomeHost.userInterfaceLayoutDirection = .leftToRight
  1206. nonHomeGenericContainer.translatesAutoresizingMaskIntoConstraints = false
  1207. savedJobsPageContainer.translatesAutoresizingMaskIntoConstraints = false
  1208. settingsPageContainer.translatesAutoresizingMaskIntoConstraints = false
  1209. cvMakerPageContainer.translatesAutoresizingMaskIntoConstraints = false
  1210. profilePageContainer.translatesAutoresizingMaskIntoConstraints = false
  1211. nonHomeHost.addSubview(nonHomeGenericContainer)
  1212. nonHomeHost.addSubview(savedJobsPageContainer)
  1213. nonHomeHost.addSubview(settingsPageContainer)
  1214. nonHomeHost.addSubview(cvMakerPageContainer)
  1215. nonHomeHost.addSubview(profilePageContainer)
  1216. NSLayoutConstraint.activate([
  1217. nonHomeGenericContainer.leadingAnchor.constraint(equalTo: nonHomeHost.leadingAnchor),
  1218. nonHomeGenericContainer.trailingAnchor.constraint(equalTo: nonHomeHost.trailingAnchor),
  1219. nonHomeGenericContainer.topAnchor.constraint(equalTo: nonHomeHost.topAnchor),
  1220. nonHomeGenericContainer.bottomAnchor.constraint(equalTo: nonHomeHost.bottomAnchor),
  1221. savedJobsPageContainer.leadingAnchor.constraint(equalTo: nonHomeHost.leadingAnchor),
  1222. savedJobsPageContainer.trailingAnchor.constraint(equalTo: nonHomeHost.trailingAnchor),
  1223. savedJobsPageContainer.topAnchor.constraint(equalTo: nonHomeHost.topAnchor),
  1224. savedJobsPageContainer.bottomAnchor.constraint(equalTo: nonHomeHost.bottomAnchor),
  1225. settingsPageContainer.leadingAnchor.constraint(equalTo: nonHomeHost.leadingAnchor),
  1226. settingsPageContainer.trailingAnchor.constraint(equalTo: nonHomeHost.trailingAnchor),
  1227. settingsPageContainer.topAnchor.constraint(equalTo: nonHomeHost.topAnchor),
  1228. settingsPageContainer.bottomAnchor.constraint(equalTo: nonHomeHost.bottomAnchor),
  1229. cvMakerPageContainer.leadingAnchor.constraint(equalTo: nonHomeHost.leadingAnchor),
  1230. cvMakerPageContainer.trailingAnchor.constraint(equalTo: nonHomeHost.trailingAnchor),
  1231. cvMakerPageContainer.topAnchor.constraint(equalTo: nonHomeHost.topAnchor),
  1232. cvMakerPageContainer.bottomAnchor.constraint(equalTo: nonHomeHost.bottomAnchor),
  1233. profilePageContainer.leftAnchor.constraint(equalTo: nonHomeHost.leftAnchor),
  1234. profilePageContainer.rightAnchor.constraint(equalTo: nonHomeHost.rightAnchor),
  1235. profilePageContainer.topAnchor.constraint(equalTo: nonHomeHost.topAnchor),
  1236. profilePageContainer.bottomAnchor.constraint(equalTo: nonHomeHost.bottomAnchor)
  1237. ])
  1238. nonHomeTitleLabel.font = .systemFont(ofSize: 22, weight: .bold)
  1239. nonHomeTitleLabel.textColor = Theme.primaryText
  1240. nonHomeTitleLabel.alignment = .center
  1241. nonHomeTitleLabel.maximumNumberOfLines = 1
  1242. nonHomeSubtitleLabel.font = .systemFont(ofSize: 14, weight: .regular)
  1243. nonHomeSubtitleLabel.textColor = Theme.secondaryText
  1244. nonHomeSubtitleLabel.alignment = .center
  1245. nonHomeSubtitleLabel.maximumNumberOfLines = 0
  1246. nonHomeSubtitleLabel.stringValue = "This area is not available in the preview build. Use Home to search jobs."
  1247. let genericStack = NSStackView(views: [nonHomeTitleLabel, nonHomeSubtitleLabel])
  1248. genericStack.orientation = .vertical
  1249. genericStack.spacing = 10
  1250. genericStack.alignment = .centerX
  1251. genericStack.translatesAutoresizingMaskIntoConstraints = false
  1252. nonHomeGenericContainer.addSubview(genericStack)
  1253. NSLayoutConstraint.activate([
  1254. genericStack.centerXAnchor.constraint(equalTo: nonHomeGenericContainer.centerXAnchor),
  1255. genericStack.centerYAnchor.constraint(equalTo: nonHomeGenericContainer.centerYAnchor),
  1256. genericStack.leadingAnchor.constraint(greaterThanOrEqualTo: nonHomeGenericContainer.leadingAnchor, constant: 32),
  1257. genericStack.trailingAnchor.constraint(lessThanOrEqualTo: nonHomeGenericContainer.trailingAnchor, constant: -32),
  1258. nonHomeSubtitleLabel.widthAnchor.constraint(lessThanOrEqualToConstant: 420)
  1259. ])
  1260. savedJobsPageTitleLabel.font = .systemFont(ofSize: 22, weight: .bold)
  1261. savedJobsPageTitleLabel.textColor = Theme.primaryText
  1262. savedJobsPageTitleLabel.alignment = .left
  1263. savedJobsPageTitleLabel.maximumNumberOfLines = 1
  1264. savedJobsPageSubtitleLabel.font = .systemFont(ofSize: 14, weight: .regular)
  1265. savedJobsPageSubtitleLabel.textColor = Theme.secondaryText
  1266. savedJobsPageSubtitleLabel.alignment = .left
  1267. savedJobsPageSubtitleLabel.maximumNumberOfLines = 0
  1268. savedJobsDocumentView.translatesAutoresizingMaskIntoConstraints = false
  1269. savedJobsStack.orientation = .vertical
  1270. savedJobsStack.spacing = 14
  1271. savedJobsStack.alignment = .leading
  1272. savedJobsStack.distribution = .fill
  1273. savedJobsStack.translatesAutoresizingMaskIntoConstraints = false
  1274. savedJobsStack.setContentHuggingPriority(.defaultHigh, for: .vertical)
  1275. savedJobsStack.setHuggingPriority(.defaultLow, for: .horizontal)
  1276. savedJobsDocumentView.addSubview(savedJobsStack)
  1277. NSLayoutConstraint.activate([
  1278. savedJobsStack.leadingAnchor.constraint(equalTo: savedJobsDocumentView.leadingAnchor),
  1279. savedJobsStack.trailingAnchor.constraint(equalTo: savedJobsDocumentView.trailingAnchor),
  1280. savedJobsStack.topAnchor.constraint(equalTo: savedJobsDocumentView.topAnchor),
  1281. savedJobsStack.bottomAnchor.constraint(equalTo: savedJobsDocumentView.bottomAnchor)
  1282. ])
  1283. savedJobsScrollView.translatesAutoresizingMaskIntoConstraints = false
  1284. savedJobsScrollView.hasVerticalScroller = true
  1285. savedJobsScrollView.hasHorizontalScroller = false
  1286. savedJobsScrollView.scrollerStyle = .legacy
  1287. savedJobsScrollView.autohidesScrollers = true
  1288. savedJobsScrollView.drawsBackground = false
  1289. savedJobsScrollView.borderType = .noBorder
  1290. savedJobsScrollView.documentView = savedJobsDocumentView
  1291. savedJobsScrollView.setContentHuggingPriority(.defaultLow, for: .vertical)
  1292. savedJobsScrollView.setContentCompressionResistancePriority(.defaultLow, for: .vertical)
  1293. let savedHeaderStack = NSStackView(views: [savedJobsPageTitleLabel, savedJobsPageSubtitleLabel])
  1294. savedHeaderStack.orientation = .vertical
  1295. savedHeaderStack.spacing = 6
  1296. savedHeaderStack.alignment = .leading
  1297. savedHeaderStack.translatesAutoresizingMaskIntoConstraints = false
  1298. let savedOuterStack = NSStackView(views: [savedHeaderStack, savedJobsScrollView])
  1299. savedOuterStack.orientation = .vertical
  1300. savedOuterStack.spacing = 16
  1301. // Leading alignment plus explicit column width keeps the title and subtitle on the same edge as the cards.
  1302. savedOuterStack.alignment = .leading
  1303. savedOuterStack.translatesAutoresizingMaskIntoConstraints = false
  1304. savedJobsPageContainer.userInterfaceLayoutDirection = .leftToRight
  1305. savedJobsPageContainer.addSubview(savedOuterStack)
  1306. NSLayoutConstraint.activate([
  1307. savedOuterStack.leadingAnchor.constraint(equalTo: savedJobsPageContainer.leadingAnchor, constant: 32),
  1308. savedOuterStack.trailingAnchor.constraint(equalTo: savedJobsPageContainer.trailingAnchor, constant: -32),
  1309. savedOuterStack.topAnchor.constraint(equalTo: savedJobsPageContainer.topAnchor, constant: 8),
  1310. savedOuterStack.bottomAnchor.constraint(equalTo: savedJobsPageContainer.bottomAnchor),
  1311. savedHeaderStack.widthAnchor.constraint(equalTo: savedOuterStack.widthAnchor),
  1312. savedJobsScrollView.widthAnchor.constraint(equalTo: savedOuterStack.widthAnchor),
  1313. savedJobsDocumentView.topAnchor.constraint(equalTo: savedJobsScrollView.contentView.topAnchor),
  1314. savedJobsDocumentView.leadingAnchor.constraint(equalTo: savedJobsScrollView.contentView.leadingAnchor),
  1315. savedJobsDocumentView.widthAnchor.constraint(equalTo: savedJobsScrollView.contentView.widthAnchor)
  1316. ])
  1317. configureSettingsPage()
  1318. configureCVMakerPage()
  1319. configureProfilePage()
  1320. }
  1321. private func configureCVMakerPage() {
  1322. cvMakerPageContainer.wantsLayer = true
  1323. cvMakerPageContainer.layer?.backgroundColor = Theme.mainHostBackground.cgColor
  1324. cvMakerPageContainer.isHidden = true
  1325. cvMakerPageView.translatesAutoresizingMaskIntoConstraints = false
  1326. cvMakerPageContainer.addSubview(cvMakerPageView)
  1327. NSLayoutConstraint.activate([
  1328. cvMakerPageView.leadingAnchor.constraint(equalTo: cvMakerPageContainer.leadingAnchor),
  1329. cvMakerPageView.trailingAnchor.constraint(equalTo: cvMakerPageContainer.trailingAnchor),
  1330. cvMakerPageView.topAnchor.constraint(equalTo: cvMakerPageContainer.topAnchor),
  1331. cvMakerPageView.bottomAnchor.constraint(equalTo: cvMakerPageContainer.bottomAnchor)
  1332. ])
  1333. cvMakerPageView.onContinueToProfileSelection = { [weak self] template in
  1334. guard let self, self.ensureProAccess() else { return }
  1335. self.pendingCVTemplate = template
  1336. self.profilesListPageView.setPendingCVTemplateDisplayName(template.name)
  1337. self.selectProfileSidebarForCVMakerFlow()
  1338. }
  1339. }
  1340. /// Switches the main panel to **Profile** so the user can pick a saved CV profile after choosing a template in CV Maker.
  1341. private func selectProfileSidebarForCVMakerFlow() {
  1342. guard let index = currentSidebarItems.firstIndex(where: { $0.title == "Profile" }) else { return }
  1343. selectSidebarItem(at: index)
  1344. }
  1345. private func configureProfilePage() {
  1346. profilePageContainer.wantsLayer = true
  1347. profilePageContainer.layer?.backgroundColor = Theme.mainHostBackground.cgColor
  1348. profilePageContainer.isHidden = true
  1349. profilePageContainer.userInterfaceLayoutDirection = .leftToRight
  1350. profilesListPageView.translatesAutoresizingMaskIntoConstraints = false
  1351. myProfilePageView.translatesAutoresizingMaskIntoConstraints = false
  1352. cvFilledPreviewPageView.translatesAutoresizingMaskIntoConstraints = false
  1353. profilePageContainer.addSubview(profilesListPageView)
  1354. profilePageContainer.addSubview(myProfilePageView)
  1355. profilePageContainer.addSubview(cvFilledPreviewPageView)
  1356. NSLayoutConstraint.activate([
  1357. profilesListPageView.leftAnchor.constraint(equalTo: profilePageContainer.leftAnchor),
  1358. profilesListPageView.rightAnchor.constraint(equalTo: profilePageContainer.rightAnchor),
  1359. profilesListPageView.topAnchor.constraint(equalTo: profilePageContainer.topAnchor),
  1360. profilesListPageView.bottomAnchor.constraint(equalTo: profilePageContainer.bottomAnchor),
  1361. myProfilePageView.leftAnchor.constraint(equalTo: profilePageContainer.leftAnchor),
  1362. myProfilePageView.rightAnchor.constraint(equalTo: profilePageContainer.rightAnchor),
  1363. myProfilePageView.topAnchor.constraint(equalTo: profilePageContainer.topAnchor),
  1364. myProfilePageView.bottomAnchor.constraint(equalTo: profilePageContainer.bottomAnchor),
  1365. cvFilledPreviewPageView.leftAnchor.constraint(equalTo: profilePageContainer.leftAnchor),
  1366. cvFilledPreviewPageView.rightAnchor.constraint(equalTo: profilePageContainer.rightAnchor),
  1367. cvFilledPreviewPageView.topAnchor.constraint(equalTo: profilePageContainer.topAnchor),
  1368. cvFilledPreviewPageView.bottomAnchor.constraint(equalTo: profilePageContainer.bottomAnchor)
  1369. ])
  1370. profilesListPageView.onAddProfile = { [weak self] in
  1371. self?.presentProfileEditor(existingID: nil)
  1372. }
  1373. profilesListPageView.onEditProfile = { [weak self] id in
  1374. self?.presentProfileEditor(existingID: id)
  1375. }
  1376. profilesListPageView.onDeleteProfile = { [weak self] id in
  1377. self?.confirmDeleteProfile(id: id)
  1378. }
  1379. profilesListPageView.onBuildCVWithProfile = { [weak self] profileID in
  1380. guard let self,
  1381. let template = self.pendingCVTemplate,
  1382. let profile = SavedProfilesStore.profile(id: profileID) else { return }
  1383. self.presentCVDocumentPreview(profile: profile, template: template)
  1384. }
  1385. cvFilledPreviewPageView.onDismiss = { [weak self] in
  1386. self?.dismissCVDocumentPreview()
  1387. }
  1388. myProfilePageView.onDismiss = { [weak self] in
  1389. self?.dismissProfileEditor()
  1390. }
  1391. isProfileEditorPresented = false
  1392. isCVDocumentPreviewPresented = false
  1393. profilesListPageView.isHidden = false
  1394. myProfilePageView.isHidden = true
  1395. cvFilledPreviewPageView.isHidden = true
  1396. profilesListPageView.reloadFromStore()
  1397. }
  1398. private func presentCVDocumentPreview(profile: SavedProfile, template: CVTemplate) {
  1399. guard ensureProAccess() else { return }
  1400. isCVDocumentPreviewPresented = true
  1401. cvFilledPreviewPageView.configure(profile: profile, template: template)
  1402. cvFilledPreviewPageView.isHidden = false
  1403. profilesListPageView.isHidden = true
  1404. myProfilePageView.isHidden = true
  1405. }
  1406. private func dismissCVDocumentPreview() {
  1407. isCVDocumentPreviewPresented = false
  1408. cvFilledPreviewPageView.isHidden = true
  1409. profilesListPageView.reloadFromStore()
  1410. profilesListPageView.isHidden = false
  1411. myProfilePageView.isHidden = true
  1412. }
  1413. private func presentProfileEditor(existingID: UUID?) {
  1414. guard ensureProAccess() else { return }
  1415. if isCVDocumentPreviewPresented {
  1416. dismissCVDocumentPreview()
  1417. }
  1418. isProfileEditorPresented = true
  1419. if let id = existingID, let profile = SavedProfilesStore.profile(id: id) {
  1420. myProfilePageView.loadSavedProfile(profile)
  1421. } else {
  1422. myProfilePageView.prepareNewProfile()
  1423. }
  1424. profilesListPageView.isHidden = true
  1425. myProfilePageView.isHidden = false
  1426. }
  1427. private func dismissProfileEditor() {
  1428. isProfileEditorPresented = false
  1429. profilesListPageView.reloadFromStore()
  1430. profilesListPageView.isHidden = false
  1431. myProfilePageView.isHidden = true
  1432. }
  1433. private func confirmDeleteProfile(id: UUID) {
  1434. let displayName = SavedProfilesStore.profile(id: id)?.profileDisplayName ?? ""
  1435. let alert = NSAlert()
  1436. alert.messageText = "Delete this profile?"
  1437. alert.informativeText = displayName.isEmpty
  1438. ? "This profile will be removed from this Mac."
  1439. : "“\(displayName)” will be removed from this Mac."
  1440. alert.alertStyle = .warning
  1441. alert.addButton(withTitle: "Cancel")
  1442. alert.addButton(withTitle: "Delete")
  1443. guard let window = window else {
  1444. let response = alert.runModal()
  1445. if response == .alertSecondButtonReturn {
  1446. SavedProfilesStore.delete(id: id)
  1447. profilesListPageView.reloadFromStore()
  1448. }
  1449. return
  1450. }
  1451. alert.beginSheetModal(for: window) { [weak self] response in
  1452. guard let self else { return }
  1453. if response == .alertSecondButtonReturn {
  1454. SavedProfilesStore.delete(id: id)
  1455. if self.isProfileEditorPresented {
  1456. self.dismissProfileEditor()
  1457. } else {
  1458. self.profilesListPageView.reloadFromStore()
  1459. }
  1460. }
  1461. }
  1462. }
  1463. private func configureSettingsPage() {
  1464. settingsPageContainer.wantsLayer = true
  1465. settingsPageContainer.layer?.backgroundColor = Theme.settingsPageBackground.cgColor
  1466. settingsPageContainer.isHidden = true
  1467. let contentStack = NSStackView()
  1468. contentStack.orientation = .vertical
  1469. contentStack.spacing = 26
  1470. contentStack.alignment = .leading
  1471. contentStack.translatesAutoresizingMaskIntoConstraints = false
  1472. let appearanceTitle = NSTextField(labelWithString: "Appearance")
  1473. appearanceTitle.font = .systemFont(ofSize: 12, weight: .semibold)
  1474. appearanceTitle.textColor = Theme.secondaryText
  1475. appearanceTitle.alignment = .left
  1476. let themeSegment = makeAppearanceModeSegment()
  1477. appearanceModeSegment = themeSegment
  1478. let appearanceSection = makeSettingsSection(rows: [
  1479. makeSettingsRow(title: "Theme", systemImage: "circle.lefthalf.filled", accessory: themeSegment, tapAction: nil)
  1480. ])
  1481. let appearanceStack = NSStackView(views: [appearanceTitle, appearanceSection])
  1482. appearanceStack.orientation = .vertical
  1483. appearanceStack.spacing = 14
  1484. appearanceStack.alignment = .leading
  1485. appearanceStack.translatesAutoresizingMaskIntoConstraints = false
  1486. let settingsSection = makeSettingsSection(rows: [
  1487. makeSettingsRow(title: "Share App", systemImage: "square.and.arrow.up", accessory: nil, tapAction: #selector(didTapShareApp)),
  1488. makeSettingsRow(title: "More Apps", systemImage: "square.grid.2x2", accessory: nil, tapAction: #selector(didTapMoreApps))
  1489. ])
  1490. let aboutTitle = NSTextField(labelWithString: "About")
  1491. aboutTitle.font = .systemFont(ofSize: 12, weight: .semibold)
  1492. aboutTitle.textColor = Theme.secondaryText
  1493. aboutTitle.alignment = .left
  1494. let aboutSection = makeSettingsSection(rows: [
  1495. makeSettingsRow(title: "Website", systemImage: "globe", accessory: nil, tapAction: #selector(didTapWebsite)),
  1496. makeSettingsRow(title: "Support", systemImage: "questionmark.circle", accessory: nil, tapAction: #selector(didTapSupport)),
  1497. makeSettingsRow(title: "Terms of Use", systemImage: "doc.text", accessory: nil, tapAction: #selector(didTapTermsOfUse)),
  1498. makeSettingsRow(title: "Privacy Policy", systemImage: "shield", accessory: nil, tapAction: #selector(didTapPrivacyPolicy))
  1499. ])
  1500. let aboutStack = NSStackView(views: [aboutTitle, aboutSection])
  1501. aboutStack.orientation = .vertical
  1502. aboutStack.spacing = 14
  1503. aboutStack.alignment = .leading
  1504. aboutStack.translatesAutoresizingMaskIntoConstraints = false
  1505. contentStack.addArrangedSubview(appearanceStack)
  1506. contentStack.addArrangedSubview(settingsSection)
  1507. contentStack.addArrangedSubview(aboutStack)
  1508. settingsPageContainer.addSubview(contentStack)
  1509. NSLayoutConstraint.activate([
  1510. contentStack.leadingAnchor.constraint(equalTo: settingsPageContainer.leadingAnchor, constant: 42),
  1511. contentStack.trailingAnchor.constraint(lessThanOrEqualTo: settingsPageContainer.trailingAnchor, constant: -42),
  1512. contentStack.topAnchor.constraint(equalTo: settingsPageContainer.topAnchor, constant: 48),
  1513. appearanceStack.widthAnchor.constraint(equalTo: contentStack.widthAnchor),
  1514. appearanceSection.widthAnchor.constraint(equalTo: appearanceStack.widthAnchor),
  1515. settingsSection.widthAnchor.constraint(equalTo: contentStack.widthAnchor),
  1516. aboutStack.widthAnchor.constraint(equalTo: contentStack.widthAnchor),
  1517. aboutSection.widthAnchor.constraint(equalTo: aboutStack.widthAnchor),
  1518. contentStack.widthAnchor.constraint(equalTo: settingsPageContainer.widthAnchor, constant: -84)
  1519. ])
  1520. }
  1521. private func makeAppearanceModeSegment() -> NSSegmentedControl {
  1522. let segment = NSSegmentedControl(
  1523. labels: ["System", "Light", "Dark"],
  1524. trackingMode: .selectOne,
  1525. target: self,
  1526. action: #selector(appearanceModeChanged(_:))
  1527. )
  1528. segment.translatesAutoresizingMaskIntoConstraints = false
  1529. segment.segmentStyle = .automatic
  1530. segment.selectedSegment = AppAppearanceManager.shared.mode.segmentIndex
  1531. segment.setContentHuggingPriority(.required, for: .horizontal)
  1532. segment.setContentCompressionResistancePriority(.required, for: .horizontal)
  1533. return segment
  1534. }
  1535. @objc private func appearanceModeChanged(_ sender: NSSegmentedControl) {
  1536. guard let mode = AppAppearanceManager.Mode(segmentIndex: sender.selectedSegment) else { return }
  1537. AppAppearanceManager.shared.mode = mode
  1538. }
  1539. private func makeSettingsSection(rows: [NSView]) -> NSView {
  1540. let section = NSStackView()
  1541. section.orientation = .vertical
  1542. section.spacing = 0
  1543. section.alignment = .leading
  1544. section.translatesAutoresizingMaskIntoConstraints = false
  1545. section.wantsLayer = true
  1546. section.layer?.backgroundColor = Theme.settingsGroupBackground.cgColor
  1547. section.layer?.cornerRadius = 14
  1548. section.layer?.borderWidth = 1
  1549. section.layer?.borderColor = Theme.border.cgColor
  1550. section.layer?.masksToBounds = true
  1551. for (index, row) in rows.enumerated() {
  1552. section.addArrangedSubview(row)
  1553. row.widthAnchor.constraint(equalTo: section.widthAnchor).isActive = true
  1554. if index < rows.count - 1 {
  1555. let divider = NSView()
  1556. divider.translatesAutoresizingMaskIntoConstraints = false
  1557. divider.wantsLayer = true
  1558. divider.layer?.backgroundColor = Theme.settingsDivider.cgColor
  1559. section.addArrangedSubview(divider)
  1560. NSLayoutConstraint.activate([
  1561. divider.heightAnchor.constraint(equalToConstant: 1),
  1562. divider.leadingAnchor.constraint(equalTo: section.leadingAnchor),
  1563. divider.trailingAnchor.constraint(equalTo: section.trailingAnchor)
  1564. ])
  1565. }
  1566. }
  1567. return section
  1568. }
  1569. private func makeSettingsRow(title: String, systemImage: String, accessory: NSView?, tapAction: Selector? = nil) -> NSView {
  1570. let row = NSView()
  1571. row.translatesAutoresizingMaskIntoConstraints = false
  1572. row.wantsLayer = true
  1573. let iconTile = NSView()
  1574. iconTile.translatesAutoresizingMaskIntoConstraints = false
  1575. iconTile.wantsLayer = true
  1576. iconTile.layer?.backgroundColor = Theme.settingsIconBackground.cgColor
  1577. iconTile.layer?.cornerRadius = 9
  1578. let icon = NSImageView()
  1579. icon.translatesAutoresizingMaskIntoConstraints = false
  1580. icon.symbolConfiguration = NSImage.SymbolConfiguration(pointSize: 13, weight: .medium)
  1581. icon.image = NSImage(systemSymbolName: systemImage, accessibilityDescription: title)
  1582. icon.contentTintColor = Theme.brandBlue
  1583. let titleLabel = NSTextField(labelWithString: title)
  1584. titleLabel.font = .systemFont(ofSize: 14, weight: .medium)
  1585. titleLabel.textColor = Theme.secondaryText
  1586. titleLabel.alignment = .left
  1587. let rowStack = NSStackView()
  1588. rowStack.orientation = .horizontal
  1589. rowStack.spacing = 16
  1590. rowStack.alignment = .centerY
  1591. rowStack.translatesAutoresizingMaskIntoConstraints = false
  1592. let spacer = NSView()
  1593. spacer.translatesAutoresizingMaskIntoConstraints = false
  1594. spacer.setContentHuggingPriority(NSLayoutConstraint.Priority(1), for: .horizontal)
  1595. iconTile.addSubview(icon)
  1596. rowStack.addArrangedSubview(iconTile)
  1597. rowStack.addArrangedSubview(titleLabel)
  1598. rowStack.addArrangedSubview(spacer)
  1599. if let accessory {
  1600. rowStack.addArrangedSubview(accessory)
  1601. }
  1602. row.addSubview(rowStack)
  1603. NSLayoutConstraint.activate([
  1604. row.heightAnchor.constraint(equalToConstant: 68),
  1605. iconTile.widthAnchor.constraint(equalToConstant: 38),
  1606. iconTile.heightAnchor.constraint(equalToConstant: 38),
  1607. icon.centerXAnchor.constraint(equalTo: iconTile.centerXAnchor),
  1608. icon.centerYAnchor.constraint(equalTo: iconTile.centerYAnchor),
  1609. icon.widthAnchor.constraint(equalToConstant: 20),
  1610. icon.heightAnchor.constraint(equalToConstant: 20),
  1611. rowStack.leadingAnchor.constraint(equalTo: row.leadingAnchor, constant: 16),
  1612. rowStack.trailingAnchor.constraint(equalTo: row.trailingAnchor, constant: -16),
  1613. rowStack.topAnchor.constraint(equalTo: row.topAnchor),
  1614. rowStack.bottomAnchor.constraint(equalTo: row.bottomAnchor)
  1615. ])
  1616. if let tapAction {
  1617. let rowButton = NSButton(title: "", target: self, action: tapAction)
  1618. rowButton.translatesAutoresizingMaskIntoConstraints = false
  1619. rowButton.isBordered = false
  1620. rowButton.bezelStyle = .regularSquare
  1621. rowButton.setButtonType(.momentaryChange)
  1622. rowButton.focusRingType = .none
  1623. rowButton.wantsLayer = true
  1624. rowButton.layer?.backgroundColor = .clear
  1625. row.addSubview(rowButton)
  1626. NSLayoutConstraint.activate([
  1627. rowButton.leadingAnchor.constraint(equalTo: row.leadingAnchor),
  1628. rowButton.trailingAnchor.constraint(equalTo: row.trailingAnchor),
  1629. rowButton.topAnchor.constraint(equalTo: row.topAnchor),
  1630. rowButton.bottomAnchor.constraint(equalTo: row.bottomAnchor)
  1631. ])
  1632. }
  1633. return row
  1634. }
  1635. private func reloadSavedJobsListings() {
  1636. savedJobsStack.arrangedSubviews.forEach {
  1637. savedJobsStack.removeArrangedSubview($0)
  1638. $0.removeFromSuperview()
  1639. }
  1640. if savedJobOrder.isEmpty {
  1641. savedJobsPageSubtitleLabel.stringValue = "Save jobs from Home to see them here."
  1642. let empty = NSTextField(wrappingLabelWithString: "No saved jobs yet. Search on Home, then tap Save on a listing.")
  1643. empty.font = .systemFont(ofSize: 14, weight: .regular)
  1644. empty.textColor = Theme.secondaryText
  1645. empty.alignment = .left
  1646. empty.maximumNumberOfLines = 0
  1647. empty.translatesAutoresizingMaskIntoConstraints = false
  1648. savedJobsStack.addArrangedSubview(empty)
  1649. empty.widthAnchor.constraint(equalTo: savedJobsStack.widthAnchor).isActive = true
  1650. return
  1651. }
  1652. savedJobsPageSubtitleLabel.stringValue = "\(savedJobOrder.count) saved \(savedJobOrder.count == 1 ? "position" : "positions")"
  1653. for job in savedJobOrder {
  1654. let card = makeJobListingCard(job, context: .savedJobsPage)
  1655. savedJobsStack.addArrangedSubview(card)
  1656. card.widthAnchor.constraint(equalTo: savedJobsStack.widthAnchor).isActive = true
  1657. }
  1658. }
  1659. private func isSavedJobsSidebarIndex(_ index: Int) -> Bool {
  1660. guard index >= 0, index < currentSidebarItems.count else { return false }
  1661. return currentSidebarItems[index].title == "Saved Jobs"
  1662. }
  1663. private func isHomeSidebarIndex(_ index: Int) -> Bool {
  1664. guard index >= 0, index < currentSidebarItems.count else { return false }
  1665. return currentSidebarItems[index].title == "Home"
  1666. }
  1667. private func isSettingsSidebarIndex(_ index: Int) -> Bool {
  1668. guard index >= 0, index < currentSidebarItems.count else { return false }
  1669. return currentSidebarItems[index].title == "Settings"
  1670. }
  1671. private func isCVMakerSidebarIndex(_ index: Int) -> Bool {
  1672. guard index >= 0, index < currentSidebarItems.count else { return false }
  1673. return currentSidebarItems[index].title == "CV Maker"
  1674. }
  1675. private func isProfileSidebarIndex(_ index: Int) -> Bool {
  1676. guard index >= 0, index < currentSidebarItems.count else { return false }
  1677. return currentSidebarItems[index].title == "Profile"
  1678. }
  1679. private func updateMainContentVisibility() {
  1680. if isIndeedJobBrowserPresented {
  1681. mainOverlay.isHidden = true
  1682. nonHomeHost.isHidden = true
  1683. indeedJobBrowserHost.isHidden = false
  1684. return
  1685. }
  1686. indeedJobBrowserHost.isHidden = true
  1687. let home = isHomeSidebarIndex(selectedSidebarIndex)
  1688. let savedJobs = isSavedJobsSidebarIndex(selectedSidebarIndex)
  1689. let settings = isSettingsSidebarIndex(selectedSidebarIndex)
  1690. let cvMaker = isCVMakerSidebarIndex(selectedSidebarIndex)
  1691. let profile = isProfileSidebarIndex(selectedSidebarIndex)
  1692. mainOverlay.isHidden = !home
  1693. nonHomeHost.isHidden = home
  1694. nonHomeGenericContainer.isHidden = savedJobs || settings || cvMaker || profile
  1695. savedJobsPageContainer.isHidden = !savedJobs
  1696. settingsPageContainer.isHidden = !settings
  1697. cvMakerPageContainer.isHidden = !cvMaker
  1698. profilePageContainer.isHidden = !profile
  1699. if !profile {
  1700. isProfileEditorPresented = false
  1701. isCVDocumentPreviewPresented = false
  1702. pendingCVTemplate = nil
  1703. profilesListPageView.setPendingCVTemplateDisplayName(nil)
  1704. cvFilledPreviewPageView.isHidden = true
  1705. profilesListPageView.isHidden = false
  1706. myProfilePageView.isHidden = true
  1707. }
  1708. if profile, !isProfileEditorPresented {
  1709. if isCVDocumentPreviewPresented {
  1710. profilesListPageView.isHidden = true
  1711. myProfilePageView.isHidden = true
  1712. cvFilledPreviewPageView.isHidden = false
  1713. } else {
  1714. profilesListPageView.reloadFromStore()
  1715. profilesListPageView.isHidden = false
  1716. myProfilePageView.isHidden = true
  1717. cvFilledPreviewPageView.isHidden = true
  1718. }
  1719. }
  1720. if !home, selectedSidebarIndex < currentSidebarItems.count {
  1721. if savedJobs {
  1722. reloadSavedJobsListings()
  1723. } else if settings || cvMaker || profile {
  1724. window?.makeFirstResponder(nil)
  1725. } else {
  1726. nonHomeTitleLabel.stringValue = currentSidebarItems[selectedSidebarIndex].title
  1727. }
  1728. }
  1729. }
  1730. /// Restores the main job-search field when returning to Home; chat history is kept until the user chooses **Clear chat**.
  1731. private func applyHomeState() {
  1732. jobKeywordsField.stringValue = ""
  1733. window?.makeFirstResponder(nil)
  1734. }
  1735. @objc private func didTapClearChat() {
  1736. guard !isAwaitingResponse else { return }
  1737. resetChatState()
  1738. window?.makeFirstResponder(nil)
  1739. }
  1740. private func updateSearchBarShadowPath() {
  1741. guard searchBarShadowHost.bounds.width > 0, searchBarShadowHost.bounds.height > 0 else { return }
  1742. let r = searchBarShadowHost.bounds
  1743. let radius = min(r.height / 2, 27)
  1744. searchBarShadowHost.layer?.shadowPath = CGPath(
  1745. roundedRect: r,
  1746. cornerWidth: radius,
  1747. cornerHeight: radius,
  1748. transform: nil
  1749. )
  1750. }
  1751. @objc private func didSubmitSearch() {
  1752. guard ensureProAccess() else { return }
  1753. let prompt = jobKeywordsField.stringValue.trimmingCharacters(in: .whitespacesAndNewlines)
  1754. guard !prompt.isEmpty, !isAwaitingResponse else { return }
  1755. let isContinuation = isContinuationPrompt(prompt)
  1756. let effectiveQuery = resolvedSearchQuery(for: prompt)
  1757. appendChatBubble(text: prompt, isUser: true)
  1758. chatMessages.append(ChatMessage(role: "user", content: prompt))
  1759. jobKeywordsField.stringValue = ""
  1760. startJobSearchRequest(effectiveQuery: effectiveQuery, isContinuation: isContinuation)
  1761. window?.makeFirstResponder(nil)
  1762. }
  1763. @objc private func didTapFeatureRole() {
  1764. selectFeatureShortcut(.role)
  1765. focusSearchField(seed: "Find roles similar to: ")
  1766. }
  1767. @objc private func didTapFeatureCompany() {
  1768. selectFeatureShortcut(.company)
  1769. focusSearchField(seed: "Find jobs at company: ")
  1770. }
  1771. @objc private func didTapFeatureSkill() {
  1772. selectFeatureShortcut(.skill)
  1773. focusSearchField(seed: "Find jobs that require skill: ")
  1774. }
  1775. private func selectFeatureShortcut(_ shortcut: FeatureShortcut) {
  1776. for (index, view) in featureCardsRow.arrangedSubviews.enumerated() {
  1777. guard let card = view as? FeatureShortcutCardView else { continue }
  1778. card.isSelected = (index == shortcut.rawValue)
  1779. }
  1780. }
  1781. @objc private func didTapShareApp(_ sender: NSButton) {
  1782. presentAppShareMenu(anchoredTo: sender)
  1783. }
  1784. /// Shows the macOS share menu (Mail, Messages, AirDrop, Copy Link, etc.) with the app link.
  1785. private func presentAppShareMenu(anchoredTo sender: NSButton) {
  1786. guard let row = sender.superview else { return }
  1787. let items = AppMarketingLinks.shareItems
  1788. guard !items.isEmpty else { return }
  1789. let picker = NSSharingServicePicker(items: items)
  1790. picker.delegate = self
  1791. appSharePicker = picker
  1792. // Match `makeSettingsRow` layout: 16pt leading inset + 38pt icon tile — anchor the
  1793. // popover beside the share icon, not the horizontal center of the full-width row.
  1794. let iconTileInset: CGFloat = 16
  1795. let iconTileSize: CGFloat = 38
  1796. let anchorRect = NSRect(
  1797. x: iconTileInset,
  1798. y: row.bounds.minY + 6,
  1799. width: iconTileSize,
  1800. height: max(row.bounds.height - 12, 1)
  1801. )
  1802. picker.show(relativeTo: anchorRect, of: row, preferredEdge: .minY)
  1803. }
  1804. func sharingServicePicker(_ sharingServicePicker: NSSharingServicePicker, delegateFor sharingService: NSSharingService) -> Any? {
  1805. self
  1806. }
  1807. func sharingServicePicker(_ sharingServicePicker: NSSharingServicePicker, didChoose sharingService: NSSharingService?) {
  1808. appSharePicker = nil
  1809. }
  1810. func sharingService(_ sharingService: NSSharingService, willShareItems items: [Any]) -> [Any] {
  1811. if sharingService == NSSharingService(named: .composeEmail) {
  1812. sharingService.subject = AppMarketingLinks.shareEmailSubject
  1813. }
  1814. return items
  1815. }
  1816. @objc private func didTapMoreApps() {
  1817. guard let url = AppMarketingLinks.developerAppsURL else {
  1818. presentAppMarketingConfigurationAlert(feature: "More Apps")
  1819. return
  1820. }
  1821. NSWorkspace.shared.open(url)
  1822. }
  1823. private func presentAppMarketingConfigurationAlert(feature: String) {
  1824. let alert = NSAlert()
  1825. alert.messageText = "\(feature) isn’t available yet"
  1826. alert.informativeText = """
  1827. Add your Mac App Store IDs in the target’s build settings:
  1828. • AppStoreAppID — numeric app ID from App Store Connect
  1829. • AppStoreDeveloperID — numeric developer ID (for your other apps page)
  1830. """
  1831. alert.alertStyle = .informational
  1832. alert.addButton(withTitle: "OK")
  1833. if let window {
  1834. alert.beginSheetModal(for: window)
  1835. } else {
  1836. alert.runModal()
  1837. }
  1838. }
  1839. @objc private func didTapWebsite() {
  1840. AppLegalURLs.openInSafari(AppLegalURLs.marketingHome)
  1841. }
  1842. @objc private func didTapSupport() {
  1843. AppLegalURLs.openInSafari(AppLegalURLs.support)
  1844. }
  1845. @objc private func didTapTermsOfUse() {
  1846. AppLegalURLs.openInSafari(AppLegalURLs.termsOfUse)
  1847. }
  1848. @objc private func didTapPrivacyPolicy() {
  1849. AppLegalURLs.openInSafari(AppLegalURLs.privacyPolicy)
  1850. }
  1851. private func focusSearchField(seed: String) {
  1852. guard ensureProAccess() else { return }
  1853. jobKeywordsField.stringValue = seed
  1854. window?.makeFirstResponder(jobKeywordsField)
  1855. if let editor = jobKeywordsField.window?.fieldEditor(true, for: jobKeywordsField) as? NSTextView {
  1856. editor.moveToEndOfDocument(nil)
  1857. }
  1858. }
  1859. @objc private func didTapLoadMoreJobs() {
  1860. guard ensureProAccess() else { return }
  1861. let prompt = "Show more jobs"
  1862. guard !isAwaitingResponse, isContinuationPrompt(prompt) else { return }
  1863. if anchorUserJobQuery(excludingLatestUserMessage: prompt) == nil { return }
  1864. appendChatBubble(text: prompt, isUser: true)
  1865. chatMessages.append(ChatMessage(role: "user", content: prompt))
  1866. let effectiveQuery = resolvedSearchQuery(for: prompt)
  1867. startJobSearchRequest(effectiveQuery: effectiveQuery, isContinuation: true)
  1868. }
  1869. private func startJobSearchRequest(effectiveQuery: String, isContinuation: Bool) {
  1870. isAwaitingResponse = true
  1871. addInlineChatThinkingRow()
  1872. setInputEnabled(false)
  1873. let contextMessages = chatMessages
  1874. let maxJobs = Self.clampedJobsPerRequest()
  1875. jobSearchService.searchJobs(query: effectiveQuery, conversation: contextMessages, maxJobs: maxJobs) { [weak self] result in
  1876. DispatchQueue.main.async {
  1877. guard let self else { return }
  1878. self.removeInlineChatThinkingRow()
  1879. self.isAwaitingResponse = false
  1880. self.setInputEnabled(true)
  1881. switch result {
  1882. case .success(let output):
  1883. let normalizedJobs = self.normalizedJobs(output.jobs)
  1884. let freshJobs: [JobListing]
  1885. if isContinuation {
  1886. // Continuations append only the *new* matches; previous cards already live in their own assistant message above.
  1887. let alreadySeen = Set(self.lastSearchResults)
  1888. freshJobs = normalizedJobs.filter { !alreadySeen.contains($0) }
  1889. } else {
  1890. freshJobs = normalizedJobs
  1891. }
  1892. self.lastSearchResults.append(contentsOf: freshJobs)
  1893. let reply = self.makeAssistantSearchReply(
  1894. query: effectiveQuery,
  1895. newJobsCount: freshJobs.count,
  1896. isContinuation: isContinuation
  1897. )
  1898. self.chatMessages.append(ChatMessage(role: "assistant", content: reply, attachedJobs: freshJobs.isEmpty ? nil : freshJobs))
  1899. self.appendChatBubble(text: reply, isUser: false, jobs: freshJobs)
  1900. case .failure(let error):
  1901. self.appendChatBubble(text: error.localizedDescription, isUser: false)
  1902. }
  1903. }
  1904. }
  1905. }
  1906. private func normalizedJobs(_ jobs: [JobListing]) -> [JobListing] {
  1907. let trimmed = jobs.map {
  1908. JobListing(
  1909. title: $0.title.trimmingCharacters(in: .whitespacesAndNewlines),
  1910. description: $0.description.trimmingCharacters(in: .whitespacesAndNewlines),
  1911. url: $0.url?.trimmingCharacters(in: .whitespacesAndNewlines)
  1912. )
  1913. }
  1914. return trimmed.filter { !$0.title.isEmpty && !$0.description.isEmpty }
  1915. }
  1916. private func makeAssistantSearchReply(query: String, newJobsCount: Int, isContinuation: Bool) -> String {
  1917. if newJobsCount == 0 {
  1918. if isContinuation {
  1919. return "I couldn't find new matches for \u{201C}\(query)\u{201D}. Try a different angle or a more specific keyword."
  1920. }
  1921. return "No jobs found for \u{201C}\(query)\u{201D}. Try another title, skill, company, or location."
  1922. }
  1923. let plural = newJobsCount == 1 ? "match" : "matches"
  1924. if isContinuation {
  1925. return "Here are \(newJobsCount) more \(plural) for \u{201C}\(query)\u{201D}."
  1926. }
  1927. return "Found \(newJobsCount) \(plural) for \u{201C}\(query)\u{201D}. Tap Apply to open the listing or Save to revisit later."
  1928. }
  1929. private func resolvedSearchQuery(for prompt: String) -> String {
  1930. let anchor = anchorUserJobQuery(excludingLatestUserMessage: prompt)
  1931. if isContinuationPrompt(prompt), !isRefinementPrompt(prompt) {
  1932. if let anchor { return anchor }
  1933. return prompt
  1934. }
  1935. if isRefinementPrompt(prompt), let anchor {
  1936. return "\(anchor). User follow-up (apply on top of the same search topic): \(prompt)"
  1937. }
  1938. return prompt
  1939. }
  1940. /// First prior user message that looks like an original job query (skips short continuations and refinements so follow-ups keep a stable topic anchor).
  1941. private func anchorUserJobQuery(excludingLatestUserMessage latest: String) -> String? {
  1942. let prior = Array(chatMessages.dropLast())
  1943. for message in prior.reversed() where message.role == "user" {
  1944. let candidate = message.content.trimmingCharacters(in: .whitespacesAndNewlines)
  1945. guard !candidate.isEmpty, candidate != latest else { continue }
  1946. if isContinuationPrompt(candidate) { continue }
  1947. if isRefinementPrompt(candidate) { continue }
  1948. return candidate
  1949. }
  1950. return nil
  1951. }
  1952. private func isContinuationPrompt(_ prompt: String) -> Bool {
  1953. let normalized = prompt.trimmingCharacters(in: .whitespacesAndNewlines).lowercased()
  1954. let continuationPhrases: Set<String> = [
  1955. "more",
  1956. "show more",
  1957. "more jobs",
  1958. "more results",
  1959. "do more searches",
  1960. "more searches",
  1961. "search more",
  1962. "continue",
  1963. "next"
  1964. ]
  1965. if continuationPhrases.contains(normalized) {
  1966. return true
  1967. }
  1968. return normalized.contains("more search") || normalized.contains("more job")
  1969. }
  1970. /// Follow-ups that narrow, re-rank, or re-frame results rather than starting a brand-new role search.
  1971. /// Strong phrases always count. Single-word cues (e.g. "remote") only count after we already showed results, so first searches like "Senior iOS remote" stay anchored as primary queries.
  1972. private func isRefinementPrompt(_ prompt: String) -> Bool {
  1973. let n = prompt.trimmingCharacters(in: .whitespacesAndNewlines).lowercased()
  1974. if n.isEmpty { return false }
  1975. let strongPhrases = [
  1976. "higher pay", "high pay", "better pay", "more pay", "top pay", "best pay",
  1977. "higher salary", "better salary", "more salary", "pay rate", "hourly rate",
  1978. "paid more", "paying more", "earn more", "better paid", "paying better",
  1979. "work from home", "in office", "in-office", "on-site only", "remote only",
  1980. "hybrid only", "onsite only", "visa sponsorship", "h1b",
  1981. "entry level", "entry-level", "mid level", "mid-level", "full time", "full-time",
  1982. "part time", "part-time",
  1983. "closer to", "nearer", "different city", "different state", "relocate",
  1984. "filter", "only show", "just show", "exclude", "without", "sort by", "rank by",
  1985. "cheaper", "lower pay", "less travel",
  1986. "better benefits", "equity", "bonus", "overtime",
  1987. "get me the jobs", "show me the jobs", "give me the jobs", "narrow", "refine"
  1988. ]
  1989. if strongPhrases.contains(where: { n.contains($0) }) { return true }
  1990. if n.hasPrefix("only ") || n.hasPrefix("just ") { return true }
  1991. guard !lastSearchResults.isEmpty, n.count <= 52 else { return false }
  1992. let softAfterResults = [
  1993. "remote", "hybrid", "onsite", "on-site", "senior", "junior", "staff", "lead",
  1994. "principal", "intern", "contract", "location"
  1995. ]
  1996. return softAfterResults.contains(where: { n.contains($0) })
  1997. }
  1998. func controlTextDidBeginEditing(_ obj: Notification) {
  1999. applySearchFieldInsertionPoint(obj.object)
  2000. }
  2001. func controlTextDidChange(_ obj: Notification) {
  2002. applySearchFieldInsertionPoint(obj.object)
  2003. }
  2004. func control(_ control: NSControl, textView: NSTextView, doCommandBy commandSelector: Selector) -> Bool {
  2005. guard control === jobKeywordsField, commandSelector == #selector(NSResponder.insertNewline(_:)) else {
  2006. return false
  2007. }
  2008. didSubmitSearch()
  2009. return true
  2010. }
  2011. private func applySearchFieldInsertionPoint(_ object: Any?) {
  2012. guard let field = object as? NSTextField,
  2013. field === jobKeywordsField,
  2014. let textView = field.window?.fieldEditor(true, for: field) as? NSTextView else { return }
  2015. textView.insertionPointColor = Theme.primaryText
  2016. }
  2017. private func resetChatState() {
  2018. removeInlineChatThinkingRow()
  2019. trailingLoadMoreJobsRow = nil
  2020. trailingLoadMoreJobsButton = nil
  2021. chatMessages.removeAll()
  2022. lastSearchResults.removeAll()
  2023. clearChatStack()
  2024. let welcome = "Tell me what role you want and I will return job descriptions, key skills, and a quick fit summary."
  2025. chatMessages.append(ChatMessage(role: "assistant", content: welcome))
  2026. appendChatBubble(text: welcome, isUser: false)
  2027. }
  2028. private func clearChatStack() {
  2029. chatStack.arrangedSubviews.forEach {
  2030. chatStack.removeArrangedSubview($0)
  2031. $0.removeFromSuperview()
  2032. }
  2033. }
  2034. private func rebuildChatUI() {
  2035. guard !chatMessages.isEmpty else { return }
  2036. removeInlineChatThinkingRow()
  2037. trailingLoadMoreJobsRow = nil
  2038. trailingLoadMoreJobsButton = nil
  2039. clearChatStack()
  2040. for message in chatMessages {
  2041. let isUser = message.role == "user"
  2042. appendChatBubble(text: message.content, isUser: isUser, jobs: message.attachedJobs)
  2043. }
  2044. for host in chatStack.arrangedSubviews {
  2045. host.alphaValue = 1
  2046. }
  2047. updateChatBubbleWidths()
  2048. }
  2049. private func appendChatBubble(text: String, isUser: Bool, jobs: [JobListing]? = nil) {
  2050. let host = NSView()
  2051. host.translatesAutoresizingMaskIntoConstraints = false
  2052. if isUser {
  2053. installUserBubble(text: text, into: host)
  2054. } else {
  2055. installAssistantBubble(text: text, jobs: jobs, into: host)
  2056. }
  2057. chatStack.addArrangedSubview(host)
  2058. host.widthAnchor.constraint(equalTo: chatStack.widthAnchor).isActive = true
  2059. if prefersReducedMotion {
  2060. host.alphaValue = 1
  2061. } else {
  2062. host.alphaValue = 0
  2063. }
  2064. DispatchQueue.main.async { [weak self] in
  2065. guard let self else { return }
  2066. let scroll: () -> Void = {
  2067. if isUser {
  2068. self.scrollChatToBottom()
  2069. } else {
  2070. self.scrollChatToShowTopOfView(host)
  2071. }
  2072. }
  2073. if self.prefersReducedMotion {
  2074. self.updateChatBubbleWidths()
  2075. scroll()
  2076. return
  2077. }
  2078. NSAnimationContext.runAnimationGroup { ctx in
  2079. ctx.duration = 0.3
  2080. ctx.timingFunction = CAMediaTimingFunction(name: .easeOut)
  2081. host.animator().alphaValue = 1
  2082. }
  2083. self.updateChatBubbleWidths()
  2084. scroll()
  2085. }
  2086. }
  2087. private func installUserBubble(text: String, into host: NSView) {
  2088. let bubble = makeChatBubbleContainer(text: text, isUser: true)
  2089. host.addSubview(bubble)
  2090. NSLayoutConstraint.activate([
  2091. bubble.topAnchor.constraint(equalTo: host.topAnchor),
  2092. bubble.bottomAnchor.constraint(equalTo: host.bottomAnchor),
  2093. bubble.trailingAnchor.constraint(equalTo: host.trailingAnchor),
  2094. bubble.leadingAnchor.constraint(greaterThanOrEqualTo: host.leadingAnchor, constant: 64),
  2095. bubble.widthAnchor.constraint(lessThanOrEqualTo: host.widthAnchor, multiplier: 0.78)
  2096. ])
  2097. }
  2098. private func installAssistantBubble(text: String, jobs: [JobListing]?, into host: NSView) {
  2099. let avatar = makeAssistantAvatarView()
  2100. let nameLabel = NSTextField(labelWithString: AppMarketingLinks.appDisplayName)
  2101. nameLabel.font = .systemFont(ofSize: 11, weight: .semibold)
  2102. nameLabel.textColor = Theme.secondaryText
  2103. nameLabel.alignment = .left
  2104. nameLabel.translatesAutoresizingMaskIntoConstraints = false
  2105. let bubble = makeChatBubbleContainer(text: text, isUser: false)
  2106. let column = NSStackView(views: [nameLabel, bubble])
  2107. column.orientation = .vertical
  2108. column.spacing = 6
  2109. // Leading keeps the assistant label, text bubble, and job cards hugging the left (after the avatar); `.width` was letting narrow intrinsic widths sit on the trailing side so AI read like a second “user” column.
  2110. column.alignment = .leading
  2111. column.translatesAutoresizingMaskIntoConstraints = false
  2112. if let jobs, !jobs.isEmpty {
  2113. trailingLoadMoreJobsRow?.removeFromSuperview()
  2114. trailingLoadMoreJobsRow = nil
  2115. trailingLoadMoreJobsButton = nil
  2116. let jobsStack = makeChatJobsStackView(jobs: jobs)
  2117. column.addArrangedSubview(jobsStack)
  2118. jobsStack.widthAnchor.constraint(equalTo: column.widthAnchor).isActive = true
  2119. let moreRow = makeLoadMoreJobsRowView()
  2120. column.addArrangedSubview(moreRow)
  2121. moreRow.widthAnchor.constraint(equalTo: column.widthAnchor).isActive = true
  2122. trailingLoadMoreJobsRow = moreRow
  2123. }
  2124. host.addSubview(avatar)
  2125. host.addSubview(column)
  2126. host.userInterfaceLayoutDirection = .leftToRight
  2127. NSLayoutConstraint.activate([
  2128. avatar.leadingAnchor.constraint(equalTo: host.leadingAnchor),
  2129. avatar.topAnchor.constraint(equalTo: host.topAnchor),
  2130. avatar.widthAnchor.constraint(equalToConstant: 36),
  2131. avatar.heightAnchor.constraint(equalToConstant: 36),
  2132. column.leadingAnchor.constraint(equalTo: avatar.trailingAnchor, constant: 12),
  2133. column.trailingAnchor.constraint(equalTo: host.trailingAnchor),
  2134. column.topAnchor.constraint(equalTo: host.topAnchor),
  2135. column.bottomAnchor.constraint(equalTo: host.bottomAnchor),
  2136. bubble.widthAnchor.constraint(lessThanOrEqualTo: host.widthAnchor, multiplier: 0.78)
  2137. ])
  2138. }
  2139. private func makeChatBubbleContainer(text: String, isUser: Bool) -> NSView {
  2140. let container = NSView()
  2141. container.translatesAutoresizingMaskIntoConstraints = false
  2142. container.wantsLayer = true
  2143. container.layer?.cornerRadius = 14
  2144. if #available(macOS 11.0, *) {
  2145. container.layer?.cornerCurve = .continuous
  2146. }
  2147. container.layer?.masksToBounds = true
  2148. if isUser {
  2149. container.layer?.backgroundColor = Theme.brandBlue.cgColor
  2150. } else {
  2151. container.layer?.backgroundColor = Theme.chromeBackground.cgColor
  2152. container.layer?.borderWidth = 1
  2153. container.layer?.borderColor = Theme.border.cgColor
  2154. }
  2155. let label = ChatBubbleLabel(wrappingLabelWithString: text)
  2156. label.font = .systemFont(ofSize: 13.5, weight: .regular)
  2157. label.textColor = isUser ? .white : Theme.primaryText
  2158. label.maximumNumberOfLines = 0
  2159. label.lineBreakMode = .byWordWrapping
  2160. label.alignment = .left
  2161. label.translatesAutoresizingMaskIntoConstraints = false
  2162. label.setContentHuggingPriority(.defaultLow, for: .horizontal)
  2163. label.setContentCompressionResistancePriority(.defaultLow, for: .horizontal)
  2164. container.addSubview(label)
  2165. NSLayoutConstraint.activate([
  2166. label.leadingAnchor.constraint(equalTo: container.leadingAnchor, constant: 14),
  2167. label.trailingAnchor.constraint(equalTo: container.trailingAnchor, constant: -14),
  2168. label.topAnchor.constraint(equalTo: container.topAnchor, constant: 10),
  2169. label.bottomAnchor.constraint(equalTo: container.bottomAnchor, constant: -10)
  2170. ])
  2171. return container
  2172. }
  2173. private func makeAssistantAvatarView() -> NSView {
  2174. let view = NSView()
  2175. view.translatesAutoresizingMaskIntoConstraints = false
  2176. view.wantsLayer = true
  2177. view.layer?.cornerRadius = 18
  2178. if #available(macOS 11.0, *) {
  2179. view.layer?.cornerCurve = .continuous
  2180. }
  2181. view.layer?.backgroundColor = Theme.settingsIconBackground.cgColor
  2182. view.layer?.borderWidth = 1
  2183. view.layer?.borderColor = Theme.proCardBorder.cgColor
  2184. let icon = NSImageView()
  2185. icon.translatesAutoresizingMaskIntoConstraints = false
  2186. icon.symbolConfiguration = NSImage.SymbolConfiguration(pointSize: 15, weight: .semibold)
  2187. icon.image = NSImage(systemSymbolName: "sparkles", accessibilityDescription: AppMarketingLinks.appDisplayName)
  2188. icon.contentTintColor = Theme.brandBlue
  2189. view.addSubview(icon)
  2190. NSLayoutConstraint.activate([
  2191. icon.centerXAnchor.constraint(equalTo: view.centerXAnchor),
  2192. icon.centerYAnchor.constraint(equalTo: view.centerYAnchor)
  2193. ])
  2194. return view
  2195. }
  2196. private func makeLoadMoreJobsRowView() -> NSView {
  2197. let row = NSView()
  2198. row.translatesAutoresizingMaskIntoConstraints = false
  2199. let button = HoverableButton()
  2200. button.pointerCursor = true
  2201. button.title = "Show more jobs"
  2202. button.font = .systemFont(ofSize: 12, weight: .semibold)
  2203. button.bezelStyle = .rounded
  2204. button.controlSize = .regular
  2205. button.contentTintColor = Theme.brandBlue
  2206. button.target = self
  2207. button.action = #selector(didTapLoadMoreJobs)
  2208. button.translatesAutoresizingMaskIntoConstraints = false
  2209. trailingLoadMoreJobsButton = button
  2210. row.addSubview(button)
  2211. NSLayoutConstraint.activate([
  2212. button.leadingAnchor.constraint(equalTo: row.leadingAnchor),
  2213. button.topAnchor.constraint(equalTo: row.topAnchor, constant: 2),
  2214. button.bottomAnchor.constraint(equalTo: row.bottomAnchor, constant: -2)
  2215. ])
  2216. return row
  2217. }
  2218. private func makeChatJobsStackView(jobs: [JobListing]) -> ChatJobsStackView {
  2219. let stack = ChatJobsStackView()
  2220. stack.orientation = .vertical
  2221. stack.spacing = 10
  2222. stack.alignment = .width
  2223. stack.distribution = .fill
  2224. stack.translatesAutoresizingMaskIntoConstraints = false
  2225. for job in jobs {
  2226. let card = makeJobListingCard(job, context: .homeSearchResults)
  2227. stack.addArrangedSubview(card)
  2228. card.widthAnchor.constraint(equalTo: stack.widthAnchor).isActive = true
  2229. }
  2230. return stack
  2231. }
  2232. private func scrollChatToBottom() {
  2233. let maxY = max(0, chatDocumentView.bounds.height - chatScrollView.contentView.bounds.height)
  2234. chatScrollView.contentView.scroll(to: NSPoint(x: 0, y: maxY))
  2235. chatScrollView.reflectScrolledClipView(chatScrollView.contentView)
  2236. }
  2237. /// Scrolls so the top of `view` sits at the top of the chat clip (flipped document coords). Long assistant replies stay readable from the first line instead of jumping to the end.
  2238. private func scrollChatToShowTopOfView(_ view: NSView) {
  2239. chatDocumentView.layoutSubtreeIfNeeded()
  2240. view.layoutSubtreeIfNeeded()
  2241. let doc = chatDocumentView
  2242. let visibleHeight = chatScrollView.contentView.bounds.height
  2243. let docHeight = doc.bounds.height
  2244. guard docHeight > 0, visibleHeight > 0 else {
  2245. scrollChatToBottom()
  2246. return
  2247. }
  2248. let rectInDoc = view.convert(view.bounds, to: doc)
  2249. let maxY = max(0, docHeight - visibleHeight)
  2250. let targetY = min(max(0, rectInDoc.minY), maxY)
  2251. chatScrollView.contentView.scroll(to: NSPoint(x: 0, y: targetY))
  2252. chatScrollView.reflectScrolledClipView(chatScrollView.contentView)
  2253. }
  2254. private func addInlineChatThinkingRow() {
  2255. removeInlineChatThinkingRow()
  2256. let host = NSView()
  2257. host.translatesAutoresizingMaskIntoConstraints = false
  2258. let indicator = ChatThinkingIndicatorView(compact: false)
  2259. host.addSubview(indicator)
  2260. NSLayoutConstraint.activate([
  2261. indicator.leadingAnchor.constraint(equalTo: host.leadingAnchor, constant: 8),
  2262. indicator.topAnchor.constraint(equalTo: host.topAnchor),
  2263. indicator.bottomAnchor.constraint(equalTo: host.bottomAnchor, constant: -2)
  2264. ])
  2265. chatThinkingRowHost = host
  2266. chatStack.addArrangedSubview(host)
  2267. host.widthAnchor.constraint(equalTo: chatStack.widthAnchor).isActive = true
  2268. indicator.startAnimatingIfNeeded()
  2269. DispatchQueue.main.async { [weak self] in
  2270. self?.updateChatBubbleWidths()
  2271. self?.scrollChatToBottom()
  2272. }
  2273. }
  2274. private func removeInlineChatThinkingRow() {
  2275. guard let host = chatThinkingRowHost else { return }
  2276. for sub in host.subviews {
  2277. (sub as? ChatThinkingIndicatorView)?.stopAnimating()
  2278. }
  2279. chatStack.removeArrangedSubview(host)
  2280. host.removeFromSuperview()
  2281. chatThinkingRowHost = nil
  2282. }
  2283. private func setInputEnabled(_ enabled: Bool) {
  2284. jobKeywordsField.isEnabled = enabled
  2285. findJobsButton.isEnabled = enabled
  2286. findJobsCTAHost.alphaValue = enabled ? 1 : 0.65
  2287. clearChatButton.isEnabled = enabled
  2288. clearChatButton.alphaValue = enabled ? 1 : 0.65
  2289. trailingLoadMoreJobsButton?.isEnabled = enabled
  2290. trailingLoadMoreJobsButton?.alphaValue = enabled ? 1 : 0.65
  2291. }
  2292. private func addIndeedSidebarLaunchRow() {
  2293. let isSelected = isIndeedSidebarSelected
  2294. let rowHost = SidebarNavRowView { [weak self] in
  2295. self?.selectIndeedSidebar()
  2296. }
  2297. rowHost.translatesAutoresizingMaskIntoConstraints = false
  2298. rowHost.wantsLayer = true
  2299. rowHost.layer?.cornerRadius = 8
  2300. rowHost.restingBackgroundColor = isSelected ? Theme.selectionFill : nil
  2301. rowHost.hoverBackgroundColor = isSelected ? Theme.selectionFillHover : Theme.sidebarRowHoverFill
  2302. rowHost.setAccessibilityLabel("Indeed")
  2303. rowHost.setAccessibilityRole(.button)
  2304. rowHost.setAccessibilitySelected(isSelected)
  2305. rowHost.setAccessibilityHelp("Open Indeed to search and apply for jobs")
  2306. let row = NSStackView()
  2307. row.orientation = .horizontal
  2308. row.spacing = 8
  2309. row.alignment = .centerY
  2310. row.translatesAutoresizingMaskIntoConstraints = false
  2311. let logo = IndeedLogoView(displayHeight: 18, variant: .compact)
  2312. logo.translatesAutoresizingMaskIntoConstraints = false
  2313. let text = NSTextField(labelWithString: "Indeed")
  2314. text.font = .systemFont(ofSize: 14, weight: .medium)
  2315. text.textColor = isSelected ? Theme.brandBlue : Theme.secondaryText
  2316. text.refusesFirstResponder = true
  2317. row.addArrangedSubview(logo)
  2318. row.addArrangedSubview(text)
  2319. rowHost.addSubview(row)
  2320. NSLayoutConstraint.activate([
  2321. row.leadingAnchor.constraint(equalTo: rowHost.leadingAnchor, constant: 10),
  2322. row.trailingAnchor.constraint(equalTo: rowHost.trailingAnchor, constant: -10),
  2323. row.topAnchor.constraint(equalTo: rowHost.topAnchor, constant: 8),
  2324. row.bottomAnchor.constraint(equalTo: rowHost.bottomAnchor, constant: -8)
  2325. ])
  2326. rowHost.setContentHuggingPriority(.defaultLow, for: .horizontal)
  2327. sidebar.addArrangedSubview(rowHost)
  2328. let sidebarHorizontalInset = sidebar.edgeInsets.left + sidebar.edgeInsets.right
  2329. rowHost.widthAnchor.constraint(equalTo: sidebar.widthAnchor, constant: -sidebarHorizontalInset).isActive = true
  2330. sidebar.setCustomSpacing(10, after: rowHost)
  2331. }
  2332. private func configureSidebar() {
  2333. let items = currentSidebarItems
  2334. sidebar.arrangedSubviews.forEach {
  2335. sidebar.removeArrangedSubview($0)
  2336. $0.removeFromSuperview()
  2337. }
  2338. let logo = IndeedLogoView(displayHeight: 34, variant: .compact)
  2339. logo.translatesAutoresizingMaskIntoConstraints = false
  2340. let brand = NSTextField(labelWithString: AppMarketingLinks.appDisplayName)
  2341. brand.font = .systemFont(ofSize: 14, weight: .semibold)
  2342. brand.textColor = Theme.brandBlue
  2343. brand.alignment = .left
  2344. brand.maximumNumberOfLines = 2
  2345. brand.preferredMaxLayoutWidth = 194
  2346. let brandHeader = NSStackView(views: [logo, brand])
  2347. brandHeader.orientation = .vertical
  2348. brandHeader.alignment = .leading
  2349. brandHeader.spacing = 6
  2350. brandHeader.translatesAutoresizingMaskIntoConstraints = false
  2351. sidebar.addArrangedSubview(brandHeader)
  2352. sidebar.setCustomSpacing(22, after: brandHeader)
  2353. items.enumerated().forEach { index, item in
  2354. let isSelected = index == selectedSidebarIndex && !isIndeedSidebarSelected
  2355. let rowHost = SidebarNavRowView { [weak self] in
  2356. self?.selectSidebarItem(at: index)
  2357. }
  2358. rowHost.translatesAutoresizingMaskIntoConstraints = false
  2359. rowHost.wantsLayer = true
  2360. rowHost.layer?.cornerRadius = 8
  2361. rowHost.restingBackgroundColor = isSelected ? Theme.selectionFill : nil
  2362. rowHost.hoverBackgroundColor = isSelected ? Theme.selectionFillHover : Theme.sidebarRowHoverFill
  2363. rowHost.setAccessibilityLabel(item.title)
  2364. rowHost.setAccessibilityRole(.button)
  2365. rowHost.setAccessibilitySelected(isSelected)
  2366. let row = NSStackView()
  2367. row.orientation = .horizontal
  2368. row.spacing = 8
  2369. row.alignment = .centerY
  2370. row.translatesAutoresizingMaskIntoConstraints = false
  2371. let icon = NSImageView()
  2372. icon.symbolConfiguration = NSImage.SymbolConfiguration(pointSize: 13, weight: .medium)
  2373. icon.image = NSImage(systemSymbolName: item.systemImage, accessibilityDescription: item.title)
  2374. icon.contentTintColor = isSelected ? Theme.brandBlue : Theme.secondaryText
  2375. let text = NSTextField(labelWithString: item.title)
  2376. text.font = .systemFont(ofSize: 14, weight: .medium)
  2377. text.textColor = isSelected ? Theme.brandBlue : Theme.secondaryText
  2378. text.refusesFirstResponder = true
  2379. row.addArrangedSubview(icon)
  2380. row.addArrangedSubview(text)
  2381. if let badge = item.badge {
  2382. let badgeField = NSTextField(labelWithString: badge)
  2383. badgeField.font = .systemFont(ofSize: 11, weight: .semibold)
  2384. badgeField.textColor = Theme.primaryText
  2385. badgeField.wantsLayer = true
  2386. badgeField.layer?.backgroundColor = Theme.toggleBackground.cgColor
  2387. badgeField.layer?.cornerRadius = 8
  2388. badgeField.alignment = .center
  2389. badgeField.maximumNumberOfLines = 1
  2390. badgeField.lineBreakMode = .byClipping
  2391. badgeField.refusesFirstResponder = true
  2392. badgeField.translatesAutoresizingMaskIntoConstraints = false
  2393. badgeField.widthAnchor.constraint(equalToConstant: 42).isActive = true
  2394. row.addArrangedSubview(NSView())
  2395. row.addArrangedSubview(badgeField)
  2396. }
  2397. rowHost.addSubview(row)
  2398. NSLayoutConstraint.activate([
  2399. row.leadingAnchor.constraint(equalTo: rowHost.leadingAnchor, constant: 10),
  2400. row.trailingAnchor.constraint(equalTo: rowHost.trailingAnchor, constant: -10),
  2401. row.topAnchor.constraint(equalTo: rowHost.topAnchor, constant: 8),
  2402. row.bottomAnchor.constraint(equalTo: rowHost.bottomAnchor, constant: -8)
  2403. ])
  2404. rowHost.setContentHuggingPriority(.defaultLow, for: .horizontal)
  2405. sidebar.addArrangedSubview(rowHost)
  2406. let sidebarHorizontalInset = sidebar.edgeInsets.left + sidebar.edgeInsets.right
  2407. rowHost.widthAnchor.constraint(equalTo: sidebar.widthAnchor, constant: -sidebarHorizontalInset).isActive = true
  2408. if item.title == "Home" {
  2409. addIndeedSidebarLaunchRow()
  2410. }
  2411. }
  2412. let sidebarBottomSpacer = NSView()
  2413. sidebarBottomSpacer.translatesAutoresizingMaskIntoConstraints = false
  2414. sidebarBottomSpacer.setContentHuggingPriority(.defaultLow, for: .vertical)
  2415. sidebarBottomSpacer.setContentCompressionResistancePriority(.defaultLow, for: .vertical)
  2416. sidebar.addArrangedSubview(sidebarBottomSpacer)
  2417. let upgradeCard = NSView()
  2418. upgradeCard.translatesAutoresizingMaskIntoConstraints = false
  2419. upgradeCard.wantsLayer = true
  2420. upgradeCard.layer?.backgroundColor = Theme.proCardFill.cgColor
  2421. upgradeCard.layer?.cornerRadius = 14
  2422. upgradeCard.layer?.borderWidth = 1
  2423. upgradeCard.layer?.borderColor = Theme.proCardBorder.cgColor
  2424. upgradeCard.layer?.masksToBounds = true
  2425. let accentBar = NSView()
  2426. accentBar.translatesAutoresizingMaskIntoConstraints = false
  2427. accentBar.wantsLayer = true
  2428. accentBar.layer?.backgroundColor = Theme.proAccent.cgColor
  2429. let inner = NSStackView()
  2430. inner.translatesAutoresizingMaskIntoConstraints = false
  2431. inner.orientation = .vertical
  2432. inner.spacing = 10
  2433. inner.alignment = .centerX
  2434. let proIcon = NSImageView()
  2435. proIcon.translatesAutoresizingMaskIntoConstraints = false
  2436. proIcon.symbolConfiguration = NSImage.SymbolConfiguration(pointSize: 13, weight: .semibold)
  2437. proIcon.image = NSImage(systemSymbolName: "sparkles", accessibilityDescription: nil)
  2438. proIcon.contentTintColor = Theme.proAccent
  2439. let proEyebrow = NSTextField(labelWithString: "Premium")
  2440. proEyebrow.font = .systemFont(ofSize: 11, weight: .heavy)
  2441. proEyebrow.textColor = Theme.proAccent
  2442. proEyebrow.alignment = .center
  2443. let eyebrowRow = NSStackView(views: [proIcon, proEyebrow])
  2444. eyebrowRow.orientation = .horizontal
  2445. eyebrowRow.spacing = 6
  2446. eyebrowRow.alignment = .centerY
  2447. let headline = NSTextField(labelWithString: "Upgrade to Pro")
  2448. headline.font = .systemFont(ofSize: 16, weight: .bold)
  2449. headline.textColor = Theme.primaryText
  2450. headline.alignment = .center
  2451. let upgradeDescription = NSTextField(wrappingLabelWithString: "Unlimited AI matches, smart alerts, and interview prep—all in one place.")
  2452. upgradeDescription.font = .systemFont(ofSize: 12, weight: .regular)
  2453. upgradeDescription.textColor = Theme.secondaryText
  2454. upgradeDescription.alignment = .center
  2455. // Sidebar content width is the fixed sidebar width minus horizontal edge insets; card must stay within that band.
  2456. let cardWidth: CGFloat = 186
  2457. let innerContentWidth = cardWidth - 28
  2458. upgradeDescription.preferredMaxLayoutWidth = innerContentWidth
  2459. let upgradeButton = HoverableButton(title: "Try Pro", target: self, action: #selector(didTapUpgradeToPro))
  2460. upgradeButton.isBordered = false
  2461. upgradeButton.bezelStyle = .rounded
  2462. upgradeButton.font = .systemFont(ofSize: 13, weight: .bold)
  2463. upgradeButton.contentTintColor = Theme.proCTAText
  2464. upgradeButton.alignment = .center
  2465. upgradeButton.wantsLayer = true
  2466. upgradeButton.layer?.backgroundColor = Theme.proCTABackground.cgColor
  2467. upgradeButton.layer?.cornerRadius = 8
  2468. upgradeButton.translatesAutoresizingMaskIntoConstraints = false
  2469. upgradeButton.heightAnchor.constraint(equalToConstant: 32).isActive = true
  2470. upgradeButton.pointerCursor = true
  2471. upgradeButton.hoverHandler = { [weak upgradeButton] hovering in
  2472. upgradeButton?.layer?.backgroundColor = (hovering ? Theme.brandBlueHover : Theme.proCTABackground).cgColor
  2473. }
  2474. inner.addArrangedSubview(eyebrowRow)
  2475. inner.addArrangedSubview(headline)
  2476. inner.addArrangedSubview(upgradeDescription)
  2477. inner.addArrangedSubview(upgradeButton)
  2478. upgradeCard.addSubview(accentBar)
  2479. upgradeCard.addSubview(inner)
  2480. NSLayoutConstraint.activate([
  2481. upgradeCard.widthAnchor.constraint(equalToConstant: cardWidth),
  2482. accentBar.topAnchor.constraint(equalTo: upgradeCard.topAnchor),
  2483. accentBar.leadingAnchor.constraint(equalTo: upgradeCard.leadingAnchor),
  2484. accentBar.trailingAnchor.constraint(equalTo: upgradeCard.trailingAnchor),
  2485. accentBar.heightAnchor.constraint(equalToConstant: 2),
  2486. inner.leadingAnchor.constraint(equalTo: upgradeCard.leadingAnchor, constant: 14),
  2487. inner.trailingAnchor.constraint(equalTo: upgradeCard.trailingAnchor, constant: -14),
  2488. inner.topAnchor.constraint(equalTo: accentBar.bottomAnchor, constant: 12),
  2489. inner.bottomAnchor.constraint(equalTo: upgradeCard.bottomAnchor, constant: -14),
  2490. upgradeButton.widthAnchor.constraint(equalTo: inner.widthAnchor)
  2491. ])
  2492. let upgradeCardHost = NSView()
  2493. upgradeCardHost.translatesAutoresizingMaskIntoConstraints = false
  2494. upgradeCardHost.addSubview(upgradeCard)
  2495. NSLayoutConstraint.activate([
  2496. upgradeCard.centerXAnchor.constraint(equalTo: upgradeCardHost.centerXAnchor),
  2497. upgradeCard.topAnchor.constraint(equalTo: upgradeCardHost.topAnchor),
  2498. upgradeCard.bottomAnchor.constraint(equalTo: upgradeCardHost.bottomAnchor),
  2499. upgradeCard.leadingAnchor.constraint(greaterThanOrEqualTo: upgradeCardHost.leadingAnchor),
  2500. upgradeCard.trailingAnchor.constraint(lessThanOrEqualTo: upgradeCardHost.trailingAnchor)
  2501. ])
  2502. sidebar.addArrangedSubview(upgradeCardHost)
  2503. let upgradeCardHorizontalInset = sidebar.edgeInsets.left + sidebar.edgeInsets.right
  2504. upgradeCardHost.widthAnchor.constraint(equalTo: sidebar.widthAnchor, constant: -upgradeCardHorizontalInset).isActive = true
  2505. sidebarUpgradeCard = upgradeCard
  2506. sidebarUpgradeHeadline = headline
  2507. sidebarUpgradeDescription = upgradeDescription
  2508. sidebarUpgradeButton = upgradeButton
  2509. applyProSubscriptionToSidebar()
  2510. }
  2511. @objc private func didTapUpgradeToPro() {
  2512. Task { @MainActor in
  2513. await SubscriptionStore.shared.refreshEntitlements(deep: true)
  2514. applyProSubscriptionToSidebar()
  2515. presentPremiumPlansSheet()
  2516. }
  2517. }
  2518. private func selectSidebarItem(at index: Int) {
  2519. let leavingIndeedSelection = isIndeedSidebarSelected
  2520. isIndeedSidebarSelected = false
  2521. dismissIndeedJobBrowserEmbedded()
  2522. guard index >= 0, index < currentSidebarItems.count else { return }
  2523. let selectingHome = isHomeSidebarIndex(index)
  2524. if index == selectedSidebarIndex, !leavingIndeedSelection {
  2525. if selectingHome {
  2526. applyHomeState()
  2527. }
  2528. return
  2529. }
  2530. selectedSidebarIndex = index
  2531. configureSidebar()
  2532. updateMainContentVisibility()
  2533. if selectingHome {
  2534. applyHomeState()
  2535. }
  2536. }
  2537. }
  2538. private struct ChatMessage: Codable {
  2539. let role: String
  2540. let content: String
  2541. /// Job cards shown under this assistant message (not sent to the API).
  2542. var attachedJobs: [JobListing]?
  2543. enum CodingKeys: String, CodingKey {
  2544. case role
  2545. case content
  2546. }
  2547. init(role: String, content: String, attachedJobs: [JobListing]? = nil) {
  2548. self.role = role
  2549. self.content = content
  2550. self.attachedJobs = attachedJobs
  2551. }
  2552. }
  2553. private extension NSView {
  2554. func subviewsRecursive() -> [NSView] {
  2555. var result: [NSView] = []
  2556. var stack: [NSView] = [self]
  2557. while let view = stack.popLast() {
  2558. result.append(view)
  2559. stack.append(contentsOf: view.subviews)
  2560. }
  2561. return result
  2562. }
  2563. }
  2564. private final class OpenAIJobSearchService {
  2565. private let endpoint = URL(string: "https://api.openai.com/v1/responses")!
  2566. private let session = URLSession(configuration: .ephemeral)
  2567. func searchJobs(query: String, conversation: [ChatMessage], maxJobs: Int, completion: @escaping (Result<JobSearchOutput, Error>) -> Void) {
  2568. let jobLimit = max(1, min(maxJobs, 25))
  2569. let apiKey = OpenAIConfiguration.apiKey
  2570. guard OpenAIConfiguration.hasAPIKey else {
  2571. completion(.failure(NSError(
  2572. domain: "OpenAIJobSearchService",
  2573. code: 1,
  2574. userInfo: [NSLocalizedDescriptionKey: "Missing API key. Set OPENAI_API_KEY in Xcode Build Settings."]
  2575. )))
  2576. return
  2577. }
  2578. var request = URLRequest(url: endpoint)
  2579. request.httpMethod = "POST"
  2580. request.setValue("application/json", forHTTPHeaderField: "Content-Type")
  2581. request.setValue("Bearer \(apiKey)", forHTTPHeaderField: "Authorization")
  2582. request.timeoutInterval = 45
  2583. let recentContext = conversation.suffix(8)
  2584. .map { "\($0.role.uppercased()): \($0.content)" }
  2585. .joined(separator: "\n")
  2586. let contextBlock: String
  2587. if recentContext.isEmpty {
  2588. contextBlock = "No prior conversation context."
  2589. } else {
  2590. contextBlock = recentContext
  2591. }
  2592. let developerInstructions = """
  2593. You are the job-search backend for an Indeed-focused desktop app. Always use web search, but only to discover roles that are listed on Indeed (indeed.com and regional Indeed sites such as indeed.co.uk, ca.indeed.com, etc.).
  2594. Do not include jobs sourced only from LinkedIn, Glassdoor, company career pages (unless the same opening is clearly on Indeed with an Indeed URL), Google Jobs aggregates, or other job boards.
  2595. Your final assistant message must be JSON that strictly matches the configured response schema (one object with a "jobs" array). Do not add markdown, code fences, or conversational prose outside that JSON.
  2596. Each job entry needs a title, a single-sentence description, and a "url" string. Prefer a stable Indeed **search results** URL on the correct regional domain (path `/jobs` with a `q=` query built from the listing title, company, and location—e.g. `https://www.indeed.com/jobs?q=…&l=…`). Never invent or guess job keys (`jk=`), click IDs, or viewjob URLs you did not copy exactly from live search results—wrong permalinks show 404 inside the app. Use an empty string for "url" only when you cannot construct any Indeed URL (omit the listing if it is not on Indeed).
  2597. Return at most \(jobLimit) distinct listings. If the conversation context already lists jobs, do not repeat the same titles or URLs when the user asks for more—it is fine to return fewer than \(jobLimit) new results.
  2598. Full sentences such as "looking for an AI developer job" are still job queries: always populate "jobs" from Indeed-oriented web search rather than answering with chatty text alone.
  2599. """
  2600. let userInput = """
  2601. Continue this same job-search conversation. Use prior context when useful. The line "Latest user query" is the primary task; earlier USER/ASSISTANT lines are context (previous role, location, or results).
  2602. Conversation context:
  2603. \(contextBlock)
  2604. Latest user query: "\(query)"
  2605. If the user refines pay, seniority, work location, or similar, run a new Indeed-focused search that applies those constraints to the same career topic as in the context.
  2606. """
  2607. let payload = OpenAIJobSearchAPIRequest.jobSearchPayload(
  2608. model: "gpt-4o-mini",
  2609. instructions: developerInstructions,
  2610. input: userInput,
  2611. tools: [OpenAIResponsesTool(type: "web_search_preview")],
  2612. jobLimit: jobLimit
  2613. )
  2614. do {
  2615. request.httpBody = try JSONEncoder().encode(payload)
  2616. } catch {
  2617. completion(.failure(error))
  2618. return
  2619. }
  2620. session.dataTask(with: request) { data, response, error in
  2621. if let error {
  2622. completion(.failure(error))
  2623. return
  2624. }
  2625. guard let data else {
  2626. completion(.failure(NSError(
  2627. domain: "OpenAIJobSearchService",
  2628. code: 2,
  2629. userInfo: [NSLocalizedDescriptionKey: "API returned an empty response."]
  2630. )))
  2631. return
  2632. }
  2633. if let http = response as? HTTPURLResponse, !(200...299).contains(http.statusCode) {
  2634. if let apiError = try? JSONDecoder().decode(OpenAIAPIErrorResponse.self, from: data) {
  2635. completion(.failure(NSError(
  2636. domain: "OpenAIJobSearchService",
  2637. code: http.statusCode,
  2638. userInfo: [NSLocalizedDescriptionKey: apiError.error.message]
  2639. )))
  2640. } else {
  2641. completion(.failure(NSError(
  2642. domain: "OpenAIJobSearchService",
  2643. code: http.statusCode,
  2644. userInfo: [NSLocalizedDescriptionKey: "Job search request failed with status \(http.statusCode)."]
  2645. )))
  2646. }
  2647. return
  2648. }
  2649. do {
  2650. let modelText = try Self.extractModelTextFromResponsesBody(data)
  2651. let trimmed = modelText.trimmingCharacters(in: .whitespacesAndNewlines)
  2652. guard !trimmed.isEmpty else {
  2653. throw NSError(
  2654. domain: "OpenAIJobSearchService",
  2655. code: 4,
  2656. userInfo: [NSLocalizedDescriptionKey: "The API returned an empty text payload."]
  2657. )
  2658. }
  2659. let jobs = try Self.parseJobListings(fromModelText: trimmed)
  2660. .filter(Self.jobListingUsesIndeedOrEmptyURL)
  2661. .map(Self.normalizedJobListing)
  2662. completion(.success(JobSearchOutput(jobs: jobs)))
  2663. } catch {
  2664. completion(.failure(error))
  2665. }
  2666. }.resume()
  2667. }
  2668. /// Walks the `/v1/responses` JSON without strict Codable so tool calls, refusals, and future output item types do not break decoding. Collects only `output_text` segments from assistant `message` items (and any other output items that expose a `content` array).
  2669. private static func extractModelTextFromResponsesBody(_ data: Data) throws -> String {
  2670. let rootObject: Any
  2671. do {
  2672. rootObject = try JSONSerialization.jsonObject(with: data, options: [])
  2673. } catch {
  2674. throw NSError(
  2675. domain: "OpenAIJobSearchService",
  2676. code: 5,
  2677. userInfo: [NSLocalizedDescriptionKey: "The job search service returned data that was not valid JSON."]
  2678. )
  2679. }
  2680. guard let root = rootObject as? [String: Any] else {
  2681. throw NSError(
  2682. domain: "OpenAIJobSearchService",
  2683. code: 5,
  2684. userInfo: [NSLocalizedDescriptionKey: "Unexpected response shape from the job search service."]
  2685. )
  2686. }
  2687. if let status = root["status"] as? String {
  2688. if status == "failed" {
  2689. let message = (root["error"] as? [String: Any])?["message"] as? String ?? "The search request failed."
  2690. throw NSError(domain: "OpenAIJobSearchService", code: 7, userInfo: [NSLocalizedDescriptionKey: message])
  2691. }
  2692. if status == "incomplete",
  2693. let details = root["incomplete_details"] as? [String: Any],
  2694. let reason = details["reason"] as? String {
  2695. throw NSError(
  2696. domain: "OpenAIJobSearchService",
  2697. code: 8,
  2698. userInfo: [NSLocalizedDescriptionKey: "Search stopped early (\(reason)). Try a simpler query or try again."]
  2699. )
  2700. }
  2701. }
  2702. if let direct = root["output_text"] as? String {
  2703. let trimmed = direct.trimmingCharacters(in: .whitespacesAndNewlines)
  2704. if !trimmed.isEmpty { return trimmed }
  2705. }
  2706. guard let output = root["output"] as? [Any] else {
  2707. throw NSError(
  2708. domain: "OpenAIJobSearchService",
  2709. code: 9,
  2710. userInfo: [NSLocalizedDescriptionKey: "The search service returned no assistant text. Try again in a moment."]
  2711. )
  2712. }
  2713. var segments: [String] = []
  2714. for case let item as [String: Any] in output where (item["type"] as? String) == "message" {
  2715. collectOutputTextSegments(fromOutputItem: item, into: &segments)
  2716. }
  2717. if segments.isEmpty {
  2718. for case let item as [String: Any] in output {
  2719. collectOutputTextSegments(fromOutputItem: item, into: &segments)
  2720. }
  2721. }
  2722. let combined = segments.joined(separator: "\n").trimmingCharacters(in: .whitespacesAndNewlines)
  2723. if !combined.isEmpty {
  2724. return combined
  2725. }
  2726. throw NSError(
  2727. domain: "OpenAIJobSearchService",
  2728. code: 9,
  2729. userInfo: [NSLocalizedDescriptionKey: "The model did not return readable job-search text. Try again."]
  2730. )
  2731. }
  2732. private static func collectOutputTextSegments(fromOutputItem item: [String: Any], into segments: inout [String]) {
  2733. guard let content = item["content"] as? [Any] else { return }
  2734. for case let part as [String: Any] in content {
  2735. guard (part["type"] as? String) == "output_text" else { continue }
  2736. if let s = part["text"] as? String {
  2737. segments.append(s)
  2738. } else if let nested = part["text"] as? [String: Any], let value = nested["value"] as? String {
  2739. segments.append(value)
  2740. }
  2741. }
  2742. }
  2743. private static func normalizedJobListing(_ job: JobListing) -> JobListing {
  2744. let trimmedURL = job.url?.trimmingCharacters(in: .whitespacesAndNewlines) ?? ""
  2745. if trimmedURL.isEmpty {
  2746. return JobListing(title: job.title, description: job.description, url: nil)
  2747. }
  2748. return JobListing(title: job.title, description: job.description, url: trimmedURL)
  2749. }
  2750. /// Drops listings whose `url` points at non-Indeed sites (e.g. LinkedIn) when the model ignores instructions.
  2751. private static func jobListingUsesIndeedOrEmptyURL(_ job: JobListing) -> Bool {
  2752. let trimmed = job.url?.trimmingCharacters(in: .whitespacesAndNewlines) ?? ""
  2753. if trimmed.isEmpty { return true }
  2754. return isIndeedJobURL(trimmed)
  2755. }
  2756. /// Host looks like an official Indeed property (`indeed.com`, `www.indeed.co.uk`, `ca.indeed.com`, …), not `notindeed.com`.
  2757. private static func isIndeedJobURL(_ string: String) -> Bool {
  2758. guard let host = URL(string: string)?.host?.lowercased() else { return false }
  2759. if host == "indeed.com" { return true }
  2760. if host.hasPrefix("indeed.") { return true }
  2761. return host.contains(".indeed.")
  2762. }
  2763. private static func parseJobListings(fromModelText text: String) throws -> [JobListing] {
  2764. let stripped = text.trimmingCharacters(in: .whitespacesAndNewlines)
  2765. if stripped.hasPrefix("{"), let directData = stripped.data(using: .utf8),
  2766. let payload = try? JSONDecoder().decode(JobSearchResultsPayload.self, from: directData) {
  2767. return payload.jobs
  2768. }
  2769. let jsonString = extractJobJSONObjectString(from: text) ?? extractJSONObject(from: text)
  2770. let jsonData = Data(jsonString.utf8)
  2771. if let payload = try? JSONDecoder().decode(JobSearchResultsPayload.self, from: jsonData) {
  2772. return payload.jobs
  2773. }
  2774. if let listings = try? JSONDecoder().decode([JobListing].self, from: jsonData) {
  2775. return listings
  2776. }
  2777. if let obj = try? JSONSerialization.jsonObject(with: jsonData, options: []) {
  2778. if let dict = obj as? [String: Any], let jobs = jobListings(fromFlexibleJSONObject: dict) {
  2779. return jobs
  2780. }
  2781. if let arr = obj as? [[String: Any]], let jobs = jobListings(fromFlexibleJobArray: arr) {
  2782. return jobs
  2783. }
  2784. }
  2785. throw NSError(
  2786. domain: "OpenAIJobSearchService",
  2787. code: 10,
  2788. userInfo: [NSLocalizedDescriptionKey: "The assistant reply did not include job listings in the expected JSON format. Try your search again."]
  2789. )
  2790. }
  2791. private static func jobListings(fromFlexibleJSONObject dict: [String: Any]) -> [JobListing]? {
  2792. for (key, value) in dict {
  2793. guard key.caseInsensitiveCompare("jobs") == .orderedSame, let arr = value as? [[String: Any]] else { continue }
  2794. if let jobs = jobListings(fromFlexibleJobArray: arr) { return jobs }
  2795. }
  2796. for wrapKey in ["data", "result", "results", "payload"] {
  2797. if let inner = dict[wrapKey] as? [String: Any], let nested = jobListings(fromFlexibleJSONObject: inner) {
  2798. return nested
  2799. }
  2800. }
  2801. return nil
  2802. }
  2803. private static func jobListings(fromFlexibleJobArray jobs: [[String: Any]]) -> [JobListing]? {
  2804. var out: [JobListing] = []
  2805. for item in jobs {
  2806. guard let title = firstString(valuesForKeys: ["title", "job_title", "name", "position"], in: item)?.trimmingCharacters(in: .whitespacesAndNewlines),
  2807. !title.isEmpty,
  2808. let desc = firstString(valuesForKeys: ["description", "snippet", "summary", "desc"], in: item)?.trimmingCharacters(in: .whitespacesAndNewlines),
  2809. !desc.isEmpty else { continue }
  2810. let urlRaw = firstString(valuesForKeys: ["url", "link", "apply_url", "job_url"], in: item)?.trimmingCharacters(in: .whitespacesAndNewlines)
  2811. let url: String? = (urlRaw?.isEmpty == true) ? nil : urlRaw
  2812. out.append(JobListing(title: title, description: desc, url: url))
  2813. }
  2814. return out.isEmpty ? nil : out
  2815. }
  2816. private static func firstString(valuesForKeys keys: [String], in dict: [String: Any]) -> String? {
  2817. for wanted in keys {
  2818. for (dk, dv) in dict {
  2819. guard dk.caseInsensitiveCompare(wanted) == .orderedSame, let s = dv as? String else { continue }
  2820. return s
  2821. }
  2822. }
  2823. return nil
  2824. }
  2825. private static func stripMarkdownCodeFence(_ text: String) -> String {
  2826. var s = text.trimmingCharacters(in: .whitespacesAndNewlines)
  2827. guard s.hasPrefix("```") else { return s }
  2828. s.removeFirst(3)
  2829. if s.lowercased().hasPrefix("json") {
  2830. s.removeFirst(4)
  2831. }
  2832. s = s.trimmingCharacters(in: .whitespacesAndNewlines)
  2833. if let fence = s.range(of: "```", options: .backwards) {
  2834. s = String(s[..<fence.lowerBound])
  2835. }
  2836. return s.trimmingCharacters(in: .whitespacesAndNewlines)
  2837. }
  2838. private static func balancedJSONObject(from openBrace: String.Index, in s: String) -> String? {
  2839. var depth = 0
  2840. var inString = false
  2841. var escaped = false
  2842. var i = openBrace
  2843. while i < s.endIndex {
  2844. let ch = s[i]
  2845. if inString {
  2846. if escaped {
  2847. escaped = false
  2848. } else if ch == "\\" {
  2849. escaped = true
  2850. } else if ch == "\"" {
  2851. inString = false
  2852. }
  2853. } else {
  2854. switch ch {
  2855. case "\"":
  2856. inString = true
  2857. case "{":
  2858. depth += 1
  2859. case "}":
  2860. depth -= 1
  2861. if depth == 0 {
  2862. return String(s[openBrace...i])
  2863. }
  2864. default:
  2865. break
  2866. }
  2867. }
  2868. i = s.index(after: i)
  2869. }
  2870. return nil
  2871. }
  2872. /// Prefers the JSON object that contains a `"jobs"` key so prose before/after the payload does not confuse the decoder.
  2873. private static func extractJobJSONObjectString(from text: String) -> String? {
  2874. let s = stripMarkdownCodeFence(text)
  2875. guard let jobsRange = s.range(of: "\"jobs\"", options: .caseInsensitive) else { return nil }
  2876. let head = s[..<jobsRange.lowerBound]
  2877. guard let open = head.lastIndex(of: "{") else { return nil }
  2878. return balancedJSONObject(from: open, in: s)
  2879. }
  2880. private static func extractJSONObject(from text: String) -> String {
  2881. if let extracted = extractJobJSONObjectString(from: text) {
  2882. return extracted
  2883. }
  2884. let stripped = stripMarkdownCodeFence(text)
  2885. if let first = stripped.firstIndex(of: "{"), let balanced = balancedJSONObject(from: first, in: stripped) {
  2886. return balanced
  2887. }
  2888. if let range = text.range(of: "\\{[\\s\\S]*\\}", options: .regularExpression) {
  2889. return String(text[range])
  2890. }
  2891. return text
  2892. }
  2893. }
  2894. /// Responses API request with structured JSON output so web-search replies cannot omit the `jobs` schema.
  2895. private struct OpenAIJobSearchAPIRequest: Encodable {
  2896. let model: String
  2897. let instructions: String
  2898. let input: String
  2899. let tools: [OpenAIResponsesTool]
  2900. let text: OpenAITextOutputConfig
  2901. static func jobSearchPayload(
  2902. model: String,
  2903. instructions: String,
  2904. input: String,
  2905. tools: [OpenAIResponsesTool],
  2906. jobLimit: Int
  2907. ) -> OpenAIJobSearchAPIRequest {
  2908. let itemProperties = OpenAIJobSearchJobItemProperties(
  2909. title: OpenAIJSONSchemaStringField(type: "string", description: "Job title as shown on the Indeed listing."),
  2910. description: OpenAIJSONSchemaStringField(type: "string", description: "One concise sentence summarizing the role from the Indeed posting."),
  2911. url: OpenAIJSONSchemaStringField(type: "string", description: "Indeed search URL (https, path /jobs, query q=…) on indeed.com or the correct regional Indeed host; never fabricate viewjob/jk links. Empty string only if no Indeed URL exists—never use LinkedIn, Glassdoor, or other boards.")
  2912. )
  2913. let itemSchema = OpenAIJobSearchJobItemSchema(
  2914. type: "object",
  2915. properties: itemProperties,
  2916. required: ["title", "description", "url"],
  2917. additionalProperties: false
  2918. )
  2919. let jobsProperty = OpenAIJobSearchJobsArrayProperty(
  2920. type: "array",
  2921. description: "Up to \(jobLimit) jobs found on Indeed via web search; use an empty array if none are found. Do not include listings that only exist off Indeed.",
  2922. items: itemSchema
  2923. )
  2924. let rootProperties = OpenAIJobSearchRootProperties(jobs: jobsProperty)
  2925. let rootSchema = OpenAIJobSearchRootSchema(
  2926. type: "object",
  2927. properties: rootProperties,
  2928. required: ["jobs"],
  2929. additionalProperties: false
  2930. )
  2931. let format = OpenAIJobSearchResponseJSONSchemaFormat(
  2932. type: "json_schema",
  2933. name: "job_search_results",
  2934. strict: true,
  2935. schema: rootSchema
  2936. )
  2937. return OpenAIJobSearchAPIRequest(
  2938. model: model,
  2939. instructions: instructions,
  2940. input: input,
  2941. tools: tools,
  2942. text: OpenAITextOutputConfig(format: format)
  2943. )
  2944. }
  2945. }
  2946. private struct OpenAITextOutputConfig: Encodable {
  2947. let format: OpenAIJobSearchResponseJSONSchemaFormat
  2948. }
  2949. private struct OpenAIJobSearchResponseJSONSchemaFormat: Encodable {
  2950. let type: String
  2951. let name: String
  2952. let strict: Bool
  2953. let schema: OpenAIJobSearchRootSchema
  2954. }
  2955. private struct OpenAIJobSearchRootSchema: Encodable {
  2956. let type: String
  2957. let properties: OpenAIJobSearchRootProperties
  2958. let required: [String]
  2959. let additionalProperties: Bool
  2960. }
  2961. private struct OpenAIJobSearchRootProperties: Encodable {
  2962. let jobs: OpenAIJobSearchJobsArrayProperty
  2963. }
  2964. private struct OpenAIJobSearchJobsArrayProperty: Encodable {
  2965. let type: String
  2966. let description: String
  2967. let items: OpenAIJobSearchJobItemSchema
  2968. }
  2969. private struct OpenAIJobSearchJobItemSchema: Encodable {
  2970. let type: String
  2971. let properties: OpenAIJobSearchJobItemProperties
  2972. let required: [String]
  2973. let additionalProperties: Bool
  2974. }
  2975. private struct OpenAIJobSearchJobItemProperties: Encodable {
  2976. let title: OpenAIJSONSchemaStringField
  2977. let description: OpenAIJSONSchemaStringField
  2978. let url: OpenAIJSONSchemaStringField
  2979. }
  2980. private struct OpenAIJSONSchemaStringField: Encodable {
  2981. let type: String
  2982. let description: String
  2983. }
  2984. private struct OpenAIResponsesTool: Codable {
  2985. let type: String
  2986. }
  2987. private struct JobSearchResultsPayload: Codable {
  2988. let jobs: [JobListing]
  2989. }
  2990. private struct JobSearchOutput {
  2991. let jobs: [JobListing]
  2992. }
  2993. private struct OpenAIAPIErrorResponse: Codable {
  2994. let error: APIError
  2995. struct APIError: Codable {
  2996. let message: String
  2997. }
  2998. }
  2999. /// Decorative waves and faint sparkles behind the welcome hero (reference layout).
  3000. private final class WelcomeHeroBackgroundView: NSView {
  3001. /// Stroke color for side waves (pastel blue).
  3002. var waveTint = NSColor(srgbRed: 186 / 255, green: 210 / 255, blue: 253 / 255, alpha: 1)
  3003. override var isFlipped: Bool { true }
  3004. override func draw(_ dirtyRect: NSRect) {
  3005. NSColor.clear.setFill()
  3006. bounds.fill()
  3007. guard bounds.width > 24, bounds.height > 24 else { return }
  3008. drawSideWaves(in: bounds, isLeft: true)
  3009. drawSideWaves(in: bounds, isLeft: false)
  3010. drawAmbientSparkles(in: bounds)
  3011. }
  3012. private func drawSideWaves(in bounds: NSRect, isLeft: Bool) {
  3013. for i in 0..<9 {
  3014. let path = NSBezierPath()
  3015. path.lineWidth = 1
  3016. path.lineCapStyle = .round
  3017. let phase = CGFloat(i) * 0.88
  3018. let base = CGFloat(i + 1) * 11 + 4
  3019. var first = true
  3020. for y in stride(from: CGFloat(0), through: bounds.height, by: 2.8) {
  3021. let wobble = sin(y * 0.048 + phase) * (4 + CGFloat(i % 5))
  3022. let x = isLeft ? (base + wobble) : (bounds.width - base - wobble)
  3023. let point = NSPoint(x: x, y: y)
  3024. if first {
  3025. path.move(to: point)
  3026. first = false
  3027. } else {
  3028. path.line(to: point)
  3029. }
  3030. }
  3031. let fade = 1 - CGFloat(i) / 10
  3032. waveTint.withAlphaComponent((0.09 + CGFloat(i % 3) * 0.022) * fade).setStroke()
  3033. path.stroke()
  3034. }
  3035. }
  3036. private func drawAmbientSparkles(in bounds: NSRect) {
  3037. let accent = NSColor(srgbRed: 0, green: 82 / 255, blue: 204 / 255, alpha: 1)
  3038. let specs: [(CGFloat, CGFloat, CGFloat, CGFloat)] = [
  3039. (0.06, 0.14, 9, 0.28),
  3040. (0.94, 0.12, 12, 0.34),
  3041. (0.18, 0.42, 5, 0.18),
  3042. (0.86, 0.44, 6, 0.2),
  3043. (0.5, 0.06, 7, 0.15)
  3044. ]
  3045. for (nx, ny, size, a) in specs {
  3046. let center = NSPoint(x: bounds.width * nx, y: bounds.height * ny)
  3047. fillFourPointStar(center: center, radius: size, color: accent.withAlphaComponent(a))
  3048. }
  3049. }
  3050. private func fillFourPointStar(center: NSPoint, radius: CGFloat, color: NSColor) {
  3051. let path = NSBezierPath()
  3052. for i in 0..<4 {
  3053. let angle = CGFloat(i) * .pi / 2 - .pi / 2
  3054. let x = center.x + cos(angle) * radius
  3055. let y = center.y + sin(angle) * radius
  3056. let point = NSPoint(x: x, y: y)
  3057. if i == 0 {
  3058. path.move(to: point)
  3059. } else {
  3060. path.line(to: point)
  3061. }
  3062. }
  3063. path.close()
  3064. color.setFill()
  3065. path.fill()
  3066. }
  3067. }
  3068. /// Home welcome row: three tappable shortcuts that seed the main search field (reference: white cards, pastel icon well, arrow at bottom trailing).
  3069. private final class FeatureShortcutCardView: NSView {
  3070. private static let cardCornerRadius: CGFloat = 14
  3071. var isSelected = false { didSet { updateSelectionAppearance() } }
  3072. private weak var titleField: NSTextField?
  3073. private weak var subtitleField: NSTextField?
  3074. private weak var iconHost: NSView?
  3075. private weak var iconView: NSImageView?
  3076. private weak var chevronView: NSImageView?
  3077. private weak var actionTarget: AnyObject?
  3078. private var actionSelector: Selector
  3079. init(symbolName: String, title: String, subtitle: String, target: AnyObject?, action: Selector) {
  3080. self.actionTarget = target
  3081. self.actionSelector = action
  3082. super.init(frame: .zero)
  3083. translatesAutoresizingMaskIntoConstraints = false
  3084. wantsLayer = true
  3085. layer?.cornerRadius = Self.cardCornerRadius
  3086. if #available(macOS 11.0, *) {
  3087. layer?.cornerCurve = .continuous
  3088. }
  3089. layer?.backgroundColor = AppDashboardTheme.featureCardBackground.cgColor
  3090. layer?.masksToBounds = false
  3091. layer?.shadowColor = NSColor.black.withAlphaComponent(AppDashboardTheme.isDark ? 0.35 : 0.06).cgColor
  3092. layer?.shadowOffset = CGSize(width: 0, height: 2)
  3093. layer?.shadowRadius = 12
  3094. layer?.shadowOpacity = 1
  3095. updateSelectionAppearance()
  3096. let iconSize: CGFloat = 40
  3097. let iconHost = NSView()
  3098. self.iconHost = iconHost
  3099. iconHost.translatesAutoresizingMaskIntoConstraints = false
  3100. iconHost.wantsLayer = true
  3101. iconHost.layer?.cornerRadius = iconSize / 2
  3102. let icon = NSImageView()
  3103. self.iconView = icon
  3104. icon.translatesAutoresizingMaskIntoConstraints = false
  3105. icon.symbolConfiguration = NSImage.SymbolConfiguration(pointSize: 16, weight: .regular)
  3106. icon.image = NSImage(systemSymbolName: symbolName, accessibilityDescription: nil)
  3107. iconHost.addSubview(icon)
  3108. let titleField = NSTextField(wrappingLabelWithString: title)
  3109. self.titleField = titleField
  3110. titleField.font = .systemFont(ofSize: 15, weight: .bold)
  3111. titleField.maximumNumberOfLines = 1
  3112. titleField.isEditable = false
  3113. titleField.isBordered = false
  3114. titleField.drawsBackground = false
  3115. titleField.alignment = .left
  3116. let subtitleField = NSTextField(wrappingLabelWithString: subtitle)
  3117. self.subtitleField = subtitleField
  3118. subtitleField.font = .systemFont(ofSize: 12, weight: .regular)
  3119. subtitleField.maximumNumberOfLines = 2
  3120. subtitleField.isEditable = false
  3121. subtitleField.isBordered = false
  3122. subtitleField.drawsBackground = false
  3123. subtitleField.alignment = .left
  3124. subtitleField.lineBreakMode = .byWordWrapping
  3125. subtitleField.setContentHuggingPriority(.defaultLow, for: .horizontal)
  3126. subtitleField.setContentCompressionResistancePriority(.defaultLow, for: .horizontal)
  3127. let chevron = NSImageView()
  3128. self.chevronView = chevron
  3129. chevron.translatesAutoresizingMaskIntoConstraints = false
  3130. chevron.symbolConfiguration = NSImage.SymbolConfiguration(pointSize: 12, weight: .semibold)
  3131. chevron.image = NSImage(systemSymbolName: "arrow.right", accessibilityDescription: nil)
  3132. chevron.setContentHuggingPriority(.required, for: .horizontal)
  3133. chevron.setContentCompressionResistancePriority(.required, for: .horizontal)
  3134. let subtitleRow = NSStackView(views: [subtitleField, chevron])
  3135. subtitleRow.orientation = .horizontal
  3136. subtitleRow.spacing = 10
  3137. subtitleRow.alignment = .bottom
  3138. subtitleRow.distribution = .fill
  3139. subtitleRow.translatesAutoresizingMaskIntoConstraints = false
  3140. let textColumn = NSStackView(views: [titleField, subtitleRow])
  3141. textColumn.orientation = .vertical
  3142. textColumn.spacing = 6
  3143. textColumn.alignment = .leading
  3144. textColumn.translatesAutoresizingMaskIntoConstraints = false
  3145. textColumn.setContentHuggingPriority(.defaultLow, for: .horizontal)
  3146. textColumn.setContentCompressionResistancePriority(.defaultLow, for: .horizontal)
  3147. iconHost.setContentHuggingPriority(.required, for: .horizontal)
  3148. iconHost.setContentCompressionResistancePriority(.required, for: .horizontal)
  3149. let row = NSStackView(views: [iconHost, textColumn])
  3150. row.orientation = .horizontal
  3151. row.spacing = 16
  3152. row.alignment = .centerY
  3153. row.distribution = .fill
  3154. row.translatesAutoresizingMaskIntoConstraints = false
  3155. addSubview(row)
  3156. let inset: CGFloat = 16
  3157. NSLayoutConstraint.activate([
  3158. row.leadingAnchor.constraint(equalTo: leadingAnchor, constant: inset),
  3159. row.trailingAnchor.constraint(equalTo: trailingAnchor, constant: -inset),
  3160. row.topAnchor.constraint(equalTo: topAnchor, constant: inset),
  3161. row.bottomAnchor.constraint(equalTo: bottomAnchor, constant: -inset),
  3162. iconHost.widthAnchor.constraint(equalToConstant: iconSize),
  3163. iconHost.heightAnchor.constraint(equalToConstant: iconSize),
  3164. icon.centerXAnchor.constraint(equalTo: iconHost.centerXAnchor),
  3165. icon.centerYAnchor.constraint(equalTo: iconHost.centerYAnchor)
  3166. ])
  3167. setAccessibilityElement(true)
  3168. setAccessibilityRole(.button)
  3169. setAccessibilityLabel("\(title). \(subtitle)")
  3170. applyCardAppearance()
  3171. }
  3172. func applyCardAppearance() {
  3173. layer?.backgroundColor = AppDashboardTheme.featureCardBackground.cgColor
  3174. layer?.shadowColor = NSColor.black.withAlphaComponent(AppDashboardTheme.isDark ? 0.35 : 0.06).cgColor
  3175. iconHost?.layer?.backgroundColor = AppDashboardTheme.featureIconWell.cgColor
  3176. let accent = AppDashboardTheme.featurePrimaryBlue
  3177. iconView?.contentTintColor = accent
  3178. titleField?.textColor = accent
  3179. subtitleField?.textColor = AppDashboardTheme.featureSecondaryText
  3180. chevronView?.contentTintColor = accent
  3181. updateSelectionAppearance()
  3182. }
  3183. override func viewDidChangeEffectiveAppearance() {
  3184. super.viewDidChangeEffectiveAppearance()
  3185. applyCardAppearance()
  3186. }
  3187. private func updateSelectionAppearance() {
  3188. guard let layer else { return }
  3189. let accent = AppDashboardTheme.featurePrimaryBlue
  3190. if isSelected {
  3191. layer.borderWidth = 2
  3192. layer.borderColor = accent.cgColor
  3193. layer.shadowOpacity = AppDashboardTheme.isDark ? 0.2 : 0.1
  3194. } else {
  3195. layer.borderWidth = 1
  3196. layer.borderColor = AppDashboardTheme.featureCardBorder.cgColor
  3197. layer.shadowOpacity = 1
  3198. }
  3199. setAccessibilitySelected(isSelected)
  3200. }
  3201. @available(*, unavailable)
  3202. required init?(coder: NSCoder) {
  3203. fatalError("init(coder:) has not been implemented")
  3204. }
  3205. override func layout() {
  3206. super.layout()
  3207. guard let layer = layer, bounds.width > 0, bounds.height > 0 else { return }
  3208. let r = bounds
  3209. let cr = Self.cardCornerRadius
  3210. layer.shadowPath = CGPath(roundedRect: r, cornerWidth: cr, cornerHeight: cr, transform: nil)
  3211. }
  3212. override func mouseDown(with event: NSEvent) {
  3213. if let target = actionTarget {
  3214. _ = target.perform(actionSelector, with: nil)
  3215. }
  3216. }
  3217. override func resetCursorRects() {
  3218. super.resetCursorRects()
  3219. addCursorRect(bounds, cursor: .pointingHand)
  3220. }
  3221. }
  3222. /// `NSButton` that carries a `JobListing` for card actions (`representedObject` is unavailable on `NSButton` in this target).
  3223. private class JobPayloadButton: HoverableButton {
  3224. var jobPayload: JobListing?
  3225. var cardContext: JobListingCardContext = .homeSearchResults
  3226. }
  3227. /// Insets image + title so the Save pill matches the reference (balanced padding, not flush to the stroke).
  3228. private final class SaveJobButtonCell: NSButtonCell {
  3229. private let horizontalInset: CGFloat = 10
  3230. private let verticalInset: CGFloat = 3
  3231. private let imageTitleGap: CGFloat = 5
  3232. override func imageRect(forBounds rect: NSRect) -> NSRect {
  3233. super.imageRect(forBounds: rect.insetBy(dx: horizontalInset, dy: verticalInset))
  3234. }
  3235. override func titleRect(forBounds rect: NSRect) -> NSRect {
  3236. let padded = rect.insetBy(dx: horizontalInset, dy: verticalInset)
  3237. var t = super.titleRect(forBounds: padded)
  3238. t.origin.x += imageTitleGap
  3239. t.size.width = max(0, t.size.width - imageTitleGap)
  3240. return t
  3241. }
  3242. }
  3243. private final class SaveJobPayloadButton: JobPayloadButton {
  3244. override class var cellClass: AnyClass? {
  3245. get { SaveJobButtonCell.self }
  3246. set { }
  3247. }
  3248. }
  3249. /// `NSButton` with a tracking area that reports hover transitions and (optionally) swaps in a pointing-hand cursor while hovered.
  3250. private class HoverableButton: NSButton {
  3251. var hoverHandler: ((Bool) -> Void)?
  3252. var pointerCursor: Bool = false
  3253. private(set) var isHovering: Bool = false
  3254. private var trackingArea: NSTrackingArea?
  3255. private var didPushCursor: Bool = false
  3256. override func updateTrackingAreas() {
  3257. super.updateTrackingAreas()
  3258. if let area = trackingArea { removeTrackingArea(area) }
  3259. let area = NSTrackingArea(
  3260. rect: bounds,
  3261. options: [.activeInKeyWindow, .mouseEnteredAndExited, .inVisibleRect],
  3262. owner: self,
  3263. userInfo: nil
  3264. )
  3265. addTrackingArea(area)
  3266. trackingArea = area
  3267. }
  3268. override func mouseEntered(with event: NSEvent) {
  3269. super.mouseEntered(with: event)
  3270. isHovering = true
  3271. hoverHandler?(true)
  3272. if pointerCursor, !didPushCursor {
  3273. NSCursor.pointingHand.push()
  3274. didPushCursor = true
  3275. }
  3276. }
  3277. override func mouseExited(with event: NSEvent) {
  3278. super.mouseExited(with: event)
  3279. isHovering = false
  3280. hoverHandler?(false)
  3281. if didPushCursor {
  3282. NSCursor.pop()
  3283. didPushCursor = false
  3284. }
  3285. }
  3286. override func viewWillMove(toWindow newWindow: NSWindow?) {
  3287. super.viewWillMove(toWindow: newWindow)
  3288. // Guard against an unbalanced cursor stack if the button is removed mid-hover (e.g. job card replaced after a search).
  3289. if newWindow == nil, didPushCursor {
  3290. NSCursor.pop()
  3291. didPushCursor = false
  3292. isHovering = false
  3293. }
  3294. }
  3295. }
  3296. /// `NSView` companion to `HoverableButton`: emits hover transitions and can manage a pointing-hand cursor. Used to track hover over composite controls like the gradient "Find jobs" pill.
  3297. private class HoverableView: NSView {
  3298. var hoverHandler: ((Bool) -> Void)?
  3299. var pointerCursor: Bool = false
  3300. private(set) var isHovering: Bool = false
  3301. private var trackingArea: NSTrackingArea?
  3302. private var didPushCursor: Bool = false
  3303. override func updateTrackingAreas() {
  3304. super.updateTrackingAreas()
  3305. if let area = trackingArea { removeTrackingArea(area) }
  3306. let area = NSTrackingArea(
  3307. rect: bounds,
  3308. options: [.activeInKeyWindow, .mouseEnteredAndExited, .inVisibleRect],
  3309. owner: self,
  3310. userInfo: nil
  3311. )
  3312. addTrackingArea(area)
  3313. trackingArea = area
  3314. }
  3315. override func mouseEntered(with event: NSEvent) {
  3316. super.mouseEntered(with: event)
  3317. isHovering = true
  3318. hoverHandler?(true)
  3319. if pointerCursor, !didPushCursor {
  3320. NSCursor.pointingHand.push()
  3321. didPushCursor = true
  3322. }
  3323. }
  3324. override func mouseExited(with event: NSEvent) {
  3325. super.mouseExited(with: event)
  3326. isHovering = false
  3327. hoverHandler?(false)
  3328. if didPushCursor {
  3329. NSCursor.pop()
  3330. didPushCursor = false
  3331. }
  3332. }
  3333. override func viewWillMove(toWindow newWindow: NSWindow?) {
  3334. super.viewWillMove(toWindow: newWindow)
  3335. if newWindow == nil, didPushCursor {
  3336. NSCursor.pop()
  3337. didPushCursor = false
  3338. isHovering = false
  3339. }
  3340. }
  3341. }
  3342. /// Document view for the job list `NSScrollView`; flipped coordinates keep short result sets aligned to the top of the clip (avoids a large empty band above the cards on macOS).
  3343. private final class JobListingsDocumentView: NSView {
  3344. override var isFlipped: Bool { true }
  3345. }
  3346. /// Marker subclass for the per-assistant-message job stack embedded inside a chat bubble. `NSView.tag` is read-only on macOS, so a typed subclass is the cleanest way to identify these stacks during dismiss and layout passes.
  3347. private final class ChatJobsStackView: NSStackView {}
  3348. /// Marker subclass for the wrapping label inside a chat bubble. Lets the layout pass find each bubble label to update its `preferredMaxLayoutWidth` when the chat width changes.
  3349. private final class ChatBubbleLabel: NSTextField {}
  3350. /// Captures clicks for the full sidebar pill so icon, label, and padding behave as one tab. Manages its own hover background so non-selected rows highlight subtly on hover without disturbing the selected-row fill.
  3351. private final class SidebarNavRowView: NSView {
  3352. private let onSelect: () -> Void
  3353. var restingBackgroundColor: NSColor? {
  3354. didSet { applyBackground() }
  3355. }
  3356. var hoverBackgroundColor: NSColor?
  3357. private var isHovering: Bool = false
  3358. private var didPushCursor: Bool = false
  3359. init(onSelect: @escaping () -> Void) {
  3360. self.onSelect = onSelect
  3361. super.init(frame: .zero)
  3362. }
  3363. @available(*, unavailable)
  3364. required init?(coder: NSCoder) {
  3365. fatalError("init(coder:) has not been implemented")
  3366. }
  3367. override func hitTest(_ point: NSPoint) -> NSView? {
  3368. guard let superview else { return super.hitTest(point) }
  3369. let local = convert(point, from: superview)
  3370. return bounds.contains(local) ? self : nil
  3371. }
  3372. override func mouseDown(with event: NSEvent) {
  3373. onSelect()
  3374. }
  3375. override func updateTrackingAreas() {
  3376. super.updateTrackingAreas()
  3377. trackingAreas.forEach { removeTrackingArea($0) }
  3378. addTrackingArea(NSTrackingArea(
  3379. rect: bounds,
  3380. options: [.activeInKeyWindow, .mouseEnteredAndExited, .inVisibleRect],
  3381. owner: self,
  3382. userInfo: nil
  3383. ))
  3384. }
  3385. override func mouseEntered(with event: NSEvent) {
  3386. super.mouseEntered(with: event)
  3387. isHovering = true
  3388. applyBackground()
  3389. if !didPushCursor {
  3390. NSCursor.pointingHand.push()
  3391. didPushCursor = true
  3392. }
  3393. }
  3394. override func mouseExited(with event: NSEvent) {
  3395. super.mouseExited(with: event)
  3396. isHovering = false
  3397. applyBackground()
  3398. if didPushCursor {
  3399. NSCursor.pop()
  3400. didPushCursor = false
  3401. }
  3402. }
  3403. override func viewWillMove(toWindow newWindow: NSWindow?) {
  3404. super.viewWillMove(toWindow: newWindow)
  3405. if newWindow == nil, didPushCursor {
  3406. NSCursor.pop()
  3407. didPushCursor = false
  3408. isHovering = false
  3409. }
  3410. }
  3411. private func applyBackground() {
  3412. let color = isHovering ? (hoverBackgroundColor ?? restingBackgroundColor) : restingBackgroundColor
  3413. layer?.backgroundColor = color?.cgColor
  3414. }
  3415. }