Açıklama Yok

DashboardView.swift 175KB

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