Нема описа

DashboardView.swift 147KB

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