설명 없음

DashboardView.swift 178KB

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