Brak opisu

DashboardView.swift 185KB

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