Açıklama Yok

DashboardView.swift 114KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487
  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. /// Subtitle on the welcome hero: readable blue-gray aligned with brand.
  21. static let welcomeSubtitleText = NSColor(srgbRed: 52 / 255, green: 92 / 255, blue: 142 / 255, alpha: 1)
  22. static let selectionFill = NSColor(srgbRed: 37 / 255, green: 87 / 255, blue: 167 / 255, alpha: 0.12)
  23. static let cardBackground = NSColor(srgbRed: 1, green: 1, blue: 1, alpha: 1)
  24. static let toggleBackground = NSColor(srgbRed: 232 / 255, green: 232 / 255, blue: 232 / 255, alpha: 1)
  25. static let primaryText = NSColor(srgbRed: 45 / 255, green: 45 / 255, blue: 45 / 255, alpha: 1)
  26. static let secondaryText = NSColor(srgbRed: 118 / 255, green: 118 / 255, blue: 118 / 255, alpha: 1)
  27. static let tertiaryText = NSColor(srgbRed: 118 / 255, green: 118 / 255, blue: 118 / 255, alpha: 1)
  28. static let border = NSColor(srgbRed: 212 / 255, green: 210 / 255, blue: 208 / 255, alpha: 1)
  29. /// Job search bar outer stroke (charcoal).
  30. static let searchBarBorder = NSColor(srgbRed: 58 / 255, green: 58 / 255, blue: 58 / 255, alpha: 1)
  31. /// Search bar border on hover (brand-tinted, matches focus affordance elsewhere).
  32. static let searchBarBorderHover = NSColor(srgbRed: 37 / 255, green: 87 / 255, blue: 167 / 255, alpha: 0.45)
  33. static let proCardFill = NSColor(srgbRed: 239 / 255, green: 244 / 255, blue: 252 / 255, alpha: 1)
  34. static let proCardBorder = NSColor(srgbRed: 212 / 255, green: 210 / 255, blue: 208 / 255, alpha: 1)
  35. static let proAccent = NSColor(srgbRed: 37 / 255, green: 87 / 255, blue: 167 / 255, alpha: 1)
  36. static let proCTABackground = NSColor(srgbRed: 37 / 255, green: 87 / 255, blue: 167 / 255, alpha: 1)
  37. static let proCTAText = NSColor(srgbRed: 1, green: 1, blue: 1, alpha: 1)
  38. /// Slightly lighter blue for the top of the search-bar “Find jobs” pill (reads less flat than a solid fill).
  39. static let findJobsCTAHighlight = NSColor(srgbRed: 54 / 255, green: 110 / 255, blue: 198 / 255, alpha: 1)
  40. /// Hover states: darker brand blue, deeper gradient top, stronger tints, and subtle neutral fills used across CTAs, toggles, and the sidebar.
  41. static let brandBlueHover = NSColor(srgbRed: 28 / 255, green: 70 / 255, blue: 140 / 255, alpha: 1)
  42. static let findJobsCTAHighlightHover = NSColor(srgbRed: 44 / 255, green: 94 / 255, blue: 178 / 255, alpha: 1)
  43. static let selectionFillHover = NSColor(srgbRed: 37 / 255, green: 87 / 255, blue: 167 / 255, alpha: 0.2)
  44. static let neutralHoverFill = NSColor(srgbRed: 240 / 255, green: 240 / 255, blue: 240 / 255, alpha: 1)
  45. static let sidebarRowHoverFill = NSColor(srgbRed: 0, green: 0, blue: 0, alpha: 0.04)
  46. static let settingsPageBackground = NSColor(srgbRed: 1, green: 1, blue: 1, alpha: 1)
  47. static let settingsGroupBackground = NSColor(srgbRed: 1, green: 1, blue: 1, alpha: 1)
  48. static let settingsIconBackground = NSColor(srgbRed: 239 / 255, green: 244 / 255, blue: 252 / 255, alpha: 1)
  49. static let settingsDivider = NSColor(srgbRed: 228 / 255, green: 228 / 255, blue: 228 / 255, alpha: 1)
  50. }
  51. /// Multiline `NSTextField` often ignores `alignment` for wrapped runs; explicit paragraph alignment matches the title.
  52. private static func jobListingDescriptionAttributedString(_ plain: String) -> NSAttributedString {
  53. let paragraph = NSMutableParagraphStyle()
  54. paragraph.alignment = .left
  55. paragraph.lineBreakMode = .byWordWrapping
  56. paragraph.baseWritingDirection = .leftToRight
  57. let font = NSFont.systemFont(ofSize: 13, weight: .regular)
  58. return NSAttributedString(string: plain, attributes: [
  59. .font: font,
  60. .foregroundColor: Theme.secondaryText,
  61. .paragraphStyle: paragraph
  62. ])
  63. }
  64. private let contentStack = NSStackView()
  65. private let chromeContainer = NSView()
  66. private let sidebar = NSStackView()
  67. private let mainHost = NSView()
  68. private let mainOverlay = NSStackView()
  69. private let greetingLabel = NSTextField(labelWithString: "")
  70. private let subtitleLabel = NSTextField(labelWithString: "")
  71. private let searchBarShadowHost = NSView()
  72. private let searchCard = HoverableView()
  73. private let jobSearchIcon = NSImageView()
  74. private let jobKeywordsField = NSTextField()
  75. private let findJobsButton = HoverableButton()
  76. private let findJobsCTAHost = NSView()
  77. private let findJobsCTAChrome = HoverableView()
  78. private var findJobsCTAGradientLayer: CAGradientLayer?
  79. private let chatStatusStack = NSStackView()
  80. private let chatStatusSymbolContainer = NSView()
  81. private let chatStatusIcon = NSImageView()
  82. private let chatStatusLoadingIndicator = NSProgressIndicator()
  83. private let chatStatusLabel = NSTextField(labelWithString: "Opening the vault...")
  84. private let chatScrollView = NSScrollView()
  85. private let chatDocumentView = JobListingsDocumentView()
  86. private let chatStack = NSStackView()
  87. /// Shown when a sidebar item other than Home is selected.
  88. private let nonHomeHost = NSView()
  89. private let nonHomeGenericContainer = NSView()
  90. private let nonHomeTitleLabel = NSTextField(labelWithString: "")
  91. private let nonHomeSubtitleLabel = NSTextField(wrappingLabelWithString: "")
  92. private let savedJobsPageContainer = NSView()
  93. private let savedJobsPageTitleLabel = NSTextField(labelWithString: "Saved Jobs")
  94. private let savedJobsPageSubtitleLabel = NSTextField(wrappingLabelWithString: "")
  95. private let savedJobsScrollView = NSScrollView()
  96. private let savedJobsDocumentView = JobListingsDocumentView()
  97. private let savedJobsStack = NSStackView()
  98. private let settingsPageContainer = NSView()
  99. private let themeControl = NSSegmentedControl(labels: ["System", "Light", "Dark"], trackingMode: .selectOne, target: nil, action: nil)
  100. private var currentSidebarItems: [SidebarItem] = []
  101. private var selectedSidebarIndex: Int = 0
  102. /// 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.
  103. private var lastSearchResults: [JobListing] = []
  104. /// "Show more jobs" row under the latest assistant message that listed jobs; removed when a newer listing block replaces it.
  105. private var trailingLoadMoreJobsRow: NSView?
  106. private weak var trailingLoadMoreJobsButton: HoverableButton?
  107. /// Most recently saved jobs appear first; persisted across launches.
  108. private var savedJobOrder: [JobListing] = []
  109. private var chatMessages: [ChatMessage] = []
  110. private var isAwaitingResponse = false
  111. private let jobSearchService = OpenAIJobSearchService()
  112. /// Upper bound sent to the model per request (was fixed at 8 in the prompt). Clamped when calling the API.
  113. private static let jobsPerSearchDefault = 15
  114. private static let jobsPerSearchMin = 1
  115. private static let jobsPerSearchMaxCap = 25
  116. private static func clampedJobsPerRequest(_ requested: Int = jobsPerSearchDefault) -> Int {
  117. min(jobsPerSearchMaxCap, max(jobsPerSearchMin, requested))
  118. }
  119. private static let chatStatusSparklePulseKey = "chatStatusSparklePulse"
  120. private static let welcomeSubtitleBreathKey = "welcomeSubtitleBreath"
  121. override init(frame frameRect: NSRect) {
  122. super.init(frame: frameRect)
  123. setupLayout()
  124. }
  125. required init?(coder: NSCoder) {
  126. super.init(coder: coder)
  127. setupLayout()
  128. }
  129. override func layout() {
  130. super.layout()
  131. updateSearchBarShadowPath()
  132. findJobsCTAGradientLayer?.frame = findJobsCTAChrome.bounds
  133. updateFindJobsCTAShadowPath()
  134. updateJobListingDescriptionWidths()
  135. updateChatBubbleWidths()
  136. }
  137. func render(_ data: DashboardData) {
  138. greetingLabel.stringValue = "Welcome"
  139. subtitleLabel.stringValue = data.subtitle
  140. currentSidebarItems = data.sidebarItems
  141. if selectedSidebarIndex >= currentSidebarItems.count {
  142. selectedSidebarIndex = max(0, currentSidebarItems.count - 1)
  143. }
  144. configureSidebar()
  145. savedJobOrder = Self.normalizedSavedJobs(SavedJobsStore.load())
  146. resetChatState()
  147. updateMainContentVisibility()
  148. }
  149. private func setupLayout() {
  150. wantsLayer = true
  151. layer?.backgroundColor = Theme.pageBackground.cgColor
  152. contentStack.orientation = .horizontal
  153. contentStack.spacing = 10
  154. contentStack.distribution = .fill
  155. contentStack.translatesAutoresizingMaskIntoConstraints = false
  156. contentStack.alignment = .height
  157. // Tighter chrome insets so panels sit closer to the window edges (especially leading / top under the title bar).
  158. contentStack.edgeInsets = NSEdgeInsets(top: 10, left: 12, bottom: 20, right: 20)
  159. chromeContainer.translatesAutoresizingMaskIntoConstraints = false
  160. chromeContainer.wantsLayer = true
  161. chromeContainer.layer?.backgroundColor = Theme.chromeBackground.cgColor
  162. chromeContainer.layer?.cornerRadius = 0
  163. addSubview(chromeContainer)
  164. chromeContainer.addSubview(contentStack)
  165. sidebar.orientation = .vertical
  166. sidebar.spacing = 10
  167. sidebar.distribution = .fill
  168. sidebar.alignment = .leading
  169. sidebar.wantsLayer = true
  170. sidebar.layer?.backgroundColor = Theme.sidebarBackground.cgColor
  171. sidebar.layer?.cornerRadius = 16
  172. sidebar.edgeInsets = NSEdgeInsets(top: 16, left: 12, bottom: 16, right: 12)
  173. sidebar.translatesAutoresizingMaskIntoConstraints = false
  174. mainHost.translatesAutoresizingMaskIntoConstraints = false
  175. mainHost.wantsLayer = true
  176. mainHost.layer?.backgroundColor = Theme.mainHostBackground.cgColor
  177. mainHost.layer?.cornerRadius = 16
  178. mainHost.layer?.masksToBounds = true
  179. sidebar.setContentHuggingPriority(.required, for: .horizontal)
  180. mainHost.setContentHuggingPriority(.defaultLow, for: .horizontal)
  181. mainHost.addSubview(mainOverlay)
  182. configureNonHomePlaceholder()
  183. mainHost.addSubview(nonHomeHost)
  184. mainOverlay.orientation = .vertical
  185. mainOverlay.spacing = 0
  186. mainOverlay.alignment = .centerX
  187. mainOverlay.distribution = .fill
  188. mainOverlay.translatesAutoresizingMaskIntoConstraints = false
  189. mainOverlay.setContentHuggingPriority(.defaultLow, for: .vertical)
  190. greetingLabel.font = .systemFont(ofSize: 32, weight: .bold)
  191. greetingLabel.textColor = Theme.brandBlue
  192. greetingLabel.alignment = .center
  193. greetingLabel.maximumNumberOfLines = 1
  194. subtitleLabel.font = .systemFont(ofSize: 15, weight: .regular)
  195. subtitleLabel.textColor = Theme.welcomeSubtitleText
  196. subtitleLabel.alignment = .center
  197. subtitleLabel.maximumNumberOfLines = 2
  198. subtitleLabel.wantsLayer = true
  199. let topInset = NSView()
  200. topInset.translatesAutoresizingMaskIntoConstraints = false
  201. topInset.heightAnchor.constraint(equalToConstant: 18).isActive = true
  202. configureSearchBar()
  203. configureChatViews()
  204. let titleBlock = NSStackView(views: [greetingLabel, subtitleLabel])
  205. titleBlock.orientation = .vertical
  206. titleBlock.spacing = 10
  207. titleBlock.alignment = .centerX
  208. let midSpacer = NSView()
  209. midSpacer.translatesAutoresizingMaskIntoConstraints = false
  210. midSpacer.heightAnchor.constraint(equalToConstant: 18).isActive = true
  211. let chatTopSpacer = NSView()
  212. chatTopSpacer.translatesAutoresizingMaskIntoConstraints = false
  213. chatTopSpacer.heightAnchor.constraint(equalToConstant: 14).isActive = true
  214. let chatBottomSpacer = NSView()
  215. chatBottomSpacer.translatesAutoresizingMaskIntoConstraints = false
  216. chatBottomSpacer.heightAnchor.constraint(equalToConstant: 14).isActive = true
  217. mainOverlay.addArrangedSubview(topInset)
  218. mainOverlay.addArrangedSubview(titleBlock)
  219. mainOverlay.addArrangedSubview(midSpacer)
  220. mainOverlay.addArrangedSubview(chatStatusStack)
  221. mainOverlay.addArrangedSubview(chatTopSpacer)
  222. mainOverlay.addArrangedSubview(chatScrollView)
  223. mainOverlay.addArrangedSubview(chatBottomSpacer)
  224. mainOverlay.addArrangedSubview(searchBarShadowHost)
  225. contentStack.addArrangedSubview(sidebar)
  226. contentStack.addArrangedSubview(mainHost)
  227. NSLayoutConstraint.activate([
  228. chromeContainer.leadingAnchor.constraint(equalTo: leadingAnchor),
  229. chromeContainer.trailingAnchor.constraint(equalTo: trailingAnchor),
  230. chromeContainer.topAnchor.constraint(equalTo: topAnchor),
  231. chromeContainer.bottomAnchor.constraint(equalTo: bottomAnchor),
  232. contentStack.leadingAnchor.constraint(equalTo: chromeContainer.leadingAnchor),
  233. contentStack.trailingAnchor.constraint(equalTo: chromeContainer.trailingAnchor),
  234. contentStack.topAnchor.constraint(equalTo: safeAreaLayoutGuide.topAnchor),
  235. contentStack.bottomAnchor.constraint(equalTo: chromeContainer.bottomAnchor),
  236. sidebar.widthAnchor.constraint(equalToConstant: 218),
  237. mainHost.widthAnchor.constraint(greaterThanOrEqualToConstant: 720),
  238. mainOverlay.leadingAnchor.constraint(equalTo: mainHost.leadingAnchor),
  239. mainOverlay.trailingAnchor.constraint(equalTo: mainHost.trailingAnchor),
  240. mainOverlay.topAnchor.constraint(equalTo: mainHost.topAnchor),
  241. mainOverlay.bottomAnchor.constraint(equalTo: mainHost.bottomAnchor, constant: -24),
  242. nonHomeHost.leadingAnchor.constraint(equalTo: mainHost.leadingAnchor),
  243. nonHomeHost.trailingAnchor.constraint(equalTo: mainHost.trailingAnchor),
  244. nonHomeHost.topAnchor.constraint(equalTo: mainHost.topAnchor),
  245. nonHomeHost.bottomAnchor.constraint(equalTo: mainHost.bottomAnchor, constant: -24),
  246. searchBarShadowHost.widthAnchor.constraint(equalTo: mainOverlay.widthAnchor, multiplier: 0.92),
  247. chatStatusStack.widthAnchor.constraint(equalTo: mainOverlay.widthAnchor, multiplier: 0.92),
  248. chatScrollView.widthAnchor.constraint(equalTo: mainOverlay.widthAnchor, multiplier: 0.92),
  249. greetingLabel.leadingAnchor.constraint(equalTo: mainOverlay.leadingAnchor, constant: 16),
  250. greetingLabel.trailingAnchor.constraint(equalTo: mainOverlay.trailingAnchor, constant: -16),
  251. subtitleLabel.leadingAnchor.constraint(equalTo: greetingLabel.leadingAnchor),
  252. subtitleLabel.trailingAnchor.constraint(equalTo: greetingLabel.trailingAnchor)
  253. ])
  254. }
  255. private func configureChatViews() {
  256. chatStatusStack.orientation = .vertical
  257. chatStatusStack.spacing = 6
  258. chatStatusStack.alignment = .centerX
  259. chatStatusStack.translatesAutoresizingMaskIntoConstraints = false
  260. chatStatusSymbolContainer.translatesAutoresizingMaskIntoConstraints = false
  261. chatStatusIcon.translatesAutoresizingMaskIntoConstraints = false
  262. chatStatusIcon.wantsLayer = true
  263. chatStatusIcon.symbolConfiguration = NSImage.SymbolConfiguration(pointSize: 36, weight: .regular)
  264. chatStatusIcon.image = NSImage(systemSymbolName: "sparkles", accessibilityDescription: "Assistant status")
  265. chatStatusIcon.contentTintColor = Theme.brandBlue
  266. chatStatusLoadingIndicator.translatesAutoresizingMaskIntoConstraints = false
  267. chatStatusLoadingIndicator.style = .spinning
  268. chatStatusLoadingIndicator.isIndeterminate = true
  269. chatStatusLoadingIndicator.isDisplayedWhenStopped = false
  270. chatStatusLoadingIndicator.controlSize = .regular
  271. chatStatusLoadingIndicator.isHidden = true
  272. chatStatusLoadingIndicator.setAccessibilityRole(.progressIndicator)
  273. chatStatusLoadingIndicator.setAccessibilityLabel("Searching for jobs")
  274. chatStatusSymbolContainer.addSubview(chatStatusIcon)
  275. chatStatusSymbolContainer.addSubview(chatStatusLoadingIndicator)
  276. NSLayoutConstraint.activate([
  277. chatStatusSymbolContainer.widthAnchor.constraint(equalToConstant: 40),
  278. chatStatusSymbolContainer.heightAnchor.constraint(equalToConstant: 40),
  279. chatStatusIcon.centerXAnchor.constraint(equalTo: chatStatusSymbolContainer.centerXAnchor),
  280. chatStatusIcon.centerYAnchor.constraint(equalTo: chatStatusSymbolContainer.centerYAnchor),
  281. chatStatusLoadingIndicator.centerXAnchor.constraint(equalTo: chatStatusSymbolContainer.centerXAnchor),
  282. chatStatusLoadingIndicator.centerYAnchor.constraint(equalTo: chatStatusSymbolContainer.centerYAnchor),
  283. chatStatusLoadingIndicator.widthAnchor.constraint(equalToConstant: 32),
  284. chatStatusLoadingIndicator.heightAnchor.constraint(equalToConstant: 32)
  285. ])
  286. chatStatusLabel.font = .systemFont(ofSize: 20, weight: .semibold)
  287. chatStatusLabel.textColor = Theme.primaryText
  288. chatStatusLabel.alignment = .center
  289. chatStatusLabel.maximumNumberOfLines = 1
  290. chatStatusStack.addArrangedSubview(chatStatusSymbolContainer)
  291. chatStatusStack.addArrangedSubview(chatStatusLabel)
  292. syncChatStatusLoadingIndicator(forStatusText: chatStatusLabel.stringValue)
  293. chatDocumentView.translatesAutoresizingMaskIntoConstraints = false
  294. chatStack.orientation = .vertical
  295. chatStack.spacing = 18
  296. chatStack.alignment = .width
  297. chatStack.distribution = .fill
  298. chatStack.translatesAutoresizingMaskIntoConstraints = false
  299. chatDocumentView.addSubview(chatStack)
  300. NSLayoutConstraint.activate([
  301. chatStack.leadingAnchor.constraint(equalTo: chatDocumentView.leadingAnchor, constant: 4),
  302. chatStack.trailingAnchor.constraint(equalTo: chatDocumentView.trailingAnchor, constant: -4),
  303. chatStack.topAnchor.constraint(equalTo: chatDocumentView.topAnchor, constant: 8),
  304. chatStack.bottomAnchor.constraint(equalTo: chatDocumentView.bottomAnchor, constant: -8)
  305. ])
  306. chatScrollView.translatesAutoresizingMaskIntoConstraints = false
  307. chatScrollView.hasVerticalScroller = true
  308. chatScrollView.hasHorizontalScroller = false
  309. // Legacy reserves a dedicated track to the right of the clip view so the thumb never sits on top of cards/buttons.
  310. chatScrollView.scrollerStyle = .legacy
  311. chatScrollView.autohidesScrollers = true
  312. chatScrollView.drawsBackground = false
  313. chatScrollView.borderType = .noBorder
  314. chatScrollView.documentView = chatDocumentView
  315. chatScrollView.setContentHuggingPriority(.defaultLow, for: .vertical)
  316. chatScrollView.setContentCompressionResistancePriority(.defaultLow, for: .vertical)
  317. chatScrollView.heightAnchor.constraint(greaterThanOrEqualToConstant: 320).isActive = true
  318. // 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.
  319. NSLayoutConstraint.activate([
  320. chatDocumentView.topAnchor.constraint(equalTo: chatScrollView.contentView.topAnchor),
  321. chatDocumentView.leadingAnchor.constraint(equalTo: chatScrollView.contentView.leadingAnchor),
  322. chatDocumentView.widthAnchor.constraint(equalTo: chatScrollView.contentView.widthAnchor)
  323. ])
  324. }
  325. private func setChatStatusLabel(_ text: String) {
  326. chatStatusLabel.stringValue = text
  327. syncChatStatusLoadingIndicator(forStatusText: text)
  328. syncChatStatusSparkleAnimation()
  329. }
  330. private func syncChatStatusLoadingIndicator(forStatusText text: String) {
  331. let loading = (text == "Thinking...")
  332. if loading {
  333. chatStatusIcon.isHidden = true
  334. chatStatusLoadingIndicator.isHidden = false
  335. chatStatusLoadingIndicator.startAnimation(nil)
  336. } else {
  337. chatStatusLoadingIndicator.stopAnimation(nil)
  338. chatStatusIcon.isHidden = false
  339. }
  340. }
  341. private func isWelcomeHeroVisible() -> Bool {
  342. !mainOverlay.isHidden
  343. }
  344. private var prefersReducedMotion: Bool {
  345. NSWorkspace.shared.accessibilityDisplayShouldReduceMotion
  346. }
  347. private func shouldAnimateChatStatusSparkles(for statusText: String) -> Bool {
  348. statusText == "Ask me to find jobs"
  349. || statusText == "Ask for another role, company, or skill match"
  350. }
  351. private func syncChatStatusSparkleAnimation() {
  352. guard !prefersReducedMotion else {
  353. stopChatStatusSparkleAnimation()
  354. return
  355. }
  356. guard isWelcomeHeroVisible(), shouldAnimateChatStatusSparkles(for: chatStatusLabel.stringValue) else {
  357. stopChatStatusSparkleAnimation()
  358. return
  359. }
  360. guard let layer = chatStatusIcon.layer, layer.animation(forKey: Self.chatStatusSparklePulseKey) == nil else { return }
  361. let scale = CABasicAnimation(keyPath: "transform.scale")
  362. scale.fromValue = 1.0
  363. scale.toValue = 1.1
  364. scale.duration = 1.25
  365. scale.autoreverses = true
  366. scale.repeatCount = .greatestFiniteMagnitude
  367. scale.timingFunction = CAMediaTimingFunction(name: .easeInEaseOut)
  368. layer.add(scale, forKey: Self.chatStatusSparklePulseKey)
  369. }
  370. private func stopChatStatusSparkleAnimation() {
  371. chatStatusIcon.layer?.removeAnimation(forKey: Self.chatStatusSparklePulseKey)
  372. chatStatusIcon.layer?.transform = CATransform3DIdentity
  373. }
  374. private func syncWelcomeSubtitleBreathingAnimation() {
  375. guard !prefersReducedMotion else {
  376. stopWelcomeSubtitleBreathingAnimation()
  377. return
  378. }
  379. guard isWelcomeHeroVisible(), !subtitleLabel.stringValue.isEmpty else {
  380. stopWelcomeSubtitleBreathingAnimation()
  381. return
  382. }
  383. guard let layer = subtitleLabel.layer, layer.animation(forKey: Self.welcomeSubtitleBreathKey) == nil else { return }
  384. let pulse = CABasicAnimation(keyPath: "opacity")
  385. pulse.fromValue = 1.0
  386. pulse.toValue = 0.86
  387. pulse.duration = 2.4
  388. pulse.autoreverses = true
  389. pulse.repeatCount = .greatestFiniteMagnitude
  390. pulse.timingFunction = CAMediaTimingFunction(name: .easeInEaseOut)
  391. layer.add(pulse, forKey: Self.welcomeSubtitleBreathKey)
  392. }
  393. private func stopWelcomeSubtitleBreathingAnimation() {
  394. subtitleLabel.layer?.removeAnimation(forKey: Self.welcomeSubtitleBreathKey)
  395. subtitleLabel.layer?.opacity = 1
  396. }
  397. private func updateJobListingDescriptionWidths() {
  398. updateDescriptionColumnWidths(in: savedJobsStack, containerWidth: savedJobsDocumentView.bounds.width)
  399. walkChatJobStacks { stack in
  400. updateDescriptionColumnWidths(in: stack, containerWidth: stack.bounds.width)
  401. }
  402. }
  403. private func walkChatJobStacks(_ visitor: (ChatJobsStackView) -> Void) {
  404. func walk(_ view: NSView) {
  405. if let stack = view as? ChatJobsStackView {
  406. visitor(stack)
  407. }
  408. for sub in view.subviews { walk(sub) }
  409. }
  410. walk(chatStack)
  411. }
  412. /// 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.
  413. private func updateChatBubbleWidths() {
  414. func walk(_ view: NSView) {
  415. if let label = view as? ChatBubbleLabel,
  416. let bubble = label.superview, bubble.bounds.width > 1 {
  417. let target = max(40, bubble.bounds.width - 28)
  418. if abs(label.preferredMaxLayoutWidth - target) > 0.5 {
  419. label.preferredMaxLayoutWidth = target
  420. label.invalidateIntrinsicContentSize()
  421. }
  422. }
  423. for sub in view.subviews { walk(sub) }
  424. }
  425. walk(chatStack)
  426. }
  427. private func updateDescriptionColumnWidths(in stack: NSStackView, containerWidth: CGFloat) {
  428. guard containerWidth > 1 else { return }
  429. // 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.
  430. let contentHorizontalInset: CGFloat = 32
  431. var didChange = false
  432. for card in stack.arrangedSubviews {
  433. guard let desc = card.viewWithTag(502) as? NSTextField else { continue }
  434. let cardWidth = card.bounds.width > 1 ? card.bounds.width : containerWidth
  435. let fallbackColumn = max(1, cardWidth - contentHorizontalInset)
  436. let columnWidth: CGFloat
  437. if desc.bounds.width > 1 {
  438. columnWidth = desc.bounds.width
  439. } else if let column = desc.superview, column.bounds.width > 1 {
  440. columnWidth = column.bounds.width
  441. } else {
  442. columnWidth = fallbackColumn
  443. }
  444. if abs(desc.preferredMaxLayoutWidth - columnWidth) > 0.5 {
  445. desc.preferredMaxLayoutWidth = columnWidth
  446. desc.invalidateIntrinsicContentSize()
  447. didChange = true
  448. }
  449. }
  450. if didChange {
  451. stack.needsLayout = true
  452. }
  453. }
  454. private static func normalizedSavedJobs(_ jobs: [JobListing]) -> [JobListing] {
  455. var seen = Set<JobListing>()
  456. var out: [JobListing] = []
  457. for job in jobs where seen.insert(job).inserted {
  458. out.append(job)
  459. }
  460. return out
  461. }
  462. private func isJobSaved(_ job: JobListing) -> Bool {
  463. savedJobOrder.contains(job)
  464. }
  465. private func persistSavedJobs() {
  466. SavedJobsStore.save(savedJobOrder)
  467. }
  468. private func applySavedState(_ saved: Bool, for job: JobListing) {
  469. if saved {
  470. savedJobOrder.removeAll { $0 == job }
  471. savedJobOrder.insert(job, at: 0)
  472. } else {
  473. savedJobOrder.removeAll { $0 == job }
  474. }
  475. persistSavedJobs()
  476. }
  477. private func makeJobListingCard(_ job: JobListing, context: JobListingCardContext) -> NSView {
  478. let card = NSView()
  479. card.translatesAutoresizingMaskIntoConstraints = false
  480. card.wantsLayer = true
  481. card.layer?.backgroundColor = Theme.cardBackground.cgColor
  482. card.layer?.cornerRadius = 12
  483. card.layer?.borderWidth = 1
  484. card.layer?.borderColor = Theme.border.cgColor
  485. card.layer?.masksToBounds = true
  486. let titleField = NSTextField(labelWithString: job.title)
  487. titleField.font = .systemFont(ofSize: 16, weight: .semibold)
  488. titleField.textColor = Theme.primaryText
  489. titleField.maximumNumberOfLines = 2
  490. titleField.lineBreakMode = .byWordWrapping
  491. titleField.alignment = .left
  492. titleField.translatesAutoresizingMaskIntoConstraints = false
  493. let descriptionField = NSTextField(wrappingLabelWithString: job.description)
  494. descriptionField.font = .systemFont(ofSize: 13, weight: .regular)
  495. descriptionField.textColor = Theme.secondaryText
  496. descriptionField.maximumNumberOfLines = 0
  497. descriptionField.alignment = .left
  498. descriptionField.lineBreakMode = .byWordWrapping
  499. descriptionField.baseWritingDirection = .leftToRight
  500. descriptionField.attributedStringValue = Self.jobListingDescriptionAttributedString(job.description)
  501. if let cell = descriptionField.cell as? NSTextFieldCell {
  502. cell.alignment = .left
  503. cell.wraps = true
  504. }
  505. descriptionField.setContentHuggingPriority(.defaultLow, for: .horizontal)
  506. descriptionField.setContentCompressionResistancePriority(.defaultLow, for: .horizontal)
  507. descriptionField.tag = 502
  508. descriptionField.translatesAutoresizingMaskIntoConstraints = false
  509. let applyButton = JobPayloadButton(title: "Apply", target: self, action: #selector(didTapJobApply(_:)))
  510. applyButton.jobPayload = job
  511. applyButton.cardContext = context
  512. applyButton.isBordered = false
  513. applyButton.bezelStyle = .rounded
  514. applyButton.font = .systemFont(ofSize: 13, weight: .semibold)
  515. applyButton.wantsLayer = true
  516. applyButton.layer?.cornerRadius = 6
  517. applyButton.layer?.backgroundColor = Theme.brandBlue.cgColor
  518. applyButton.contentTintColor = Theme.proCTAText
  519. applyButton.focusRingType = .none
  520. applyButton.pointerCursor = true
  521. applyButton.hoverHandler = { [weak applyButton] hovering in
  522. applyButton?.layer?.backgroundColor = (hovering ? Theme.brandBlueHover : Theme.brandBlue).cgColor
  523. }
  524. applyButton.setContentHuggingPriority(.required, for: .horizontal)
  525. applyButton.setContentCompressionResistancePriority(.required, for: .horizontal)
  526. let savedOn = isJobSaved(job)
  527. let savedButton = JobPayloadButton(title: savedOn ? "Saved" : "Save", target: self, action: #selector(didTapJobSaved(_:)))
  528. savedButton.jobPayload = job
  529. savedButton.cardContext = context
  530. savedButton.setButtonType(.toggle)
  531. savedButton.isBordered = false
  532. savedButton.bezelStyle = .rounded
  533. savedButton.font = .systemFont(ofSize: 13, weight: .semibold)
  534. savedButton.focusRingType = .none
  535. savedButton.state = savedOn ? .on : .off
  536. savedButton.pointerCursor = true
  537. savedButton.hoverHandler = { [weak self, weak savedButton] _ in
  538. guard let savedButton = savedButton else { return }
  539. self?.styleJobSavedButton(savedButton)
  540. }
  541. styleJobSavedButton(savedButton)
  542. savedButton.setContentHuggingPriority(.required, for: .horizontal)
  543. savedButton.setContentCompressionResistancePriority(.required, for: .horizontal)
  544. let dismissButton = JobPayloadButton()
  545. dismissButton.jobPayload = job
  546. dismissButton.cardContext = context
  547. dismissButton.image = NSImage(systemSymbolName: "xmark", accessibilityDescription: "Dismiss")
  548. dismissButton.imagePosition = .imageOnly
  549. dismissButton.imageScaling = .scaleProportionallyDown
  550. dismissButton.symbolConfiguration = NSImage.SymbolConfiguration(pointSize: 11, weight: .semibold)
  551. dismissButton.isBordered = false
  552. dismissButton.bezelStyle = .rounded
  553. dismissButton.contentTintColor = Theme.secondaryText
  554. dismissButton.target = self
  555. dismissButton.action = #selector(didTapJobDismiss(_:))
  556. dismissButton.toolTip = context == .savedJobsPage ? "Remove from saved" : "Dismiss"
  557. dismissButton.focusRingType = .none
  558. dismissButton.wantsLayer = true
  559. dismissButton.layer?.cornerRadius = 14
  560. dismissButton.layer?.backgroundColor = NSColor.clear.cgColor
  561. dismissButton.pointerCursor = true
  562. dismissButton.hoverHandler = { [weak dismissButton] hovering in
  563. dismissButton?.layer?.backgroundColor = (hovering ? Theme.neutralHoverFill : NSColor.clear).cgColor
  564. dismissButton?.contentTintColor = hovering ? Theme.primaryText : Theme.secondaryText
  565. }
  566. dismissButton.setContentHuggingPriority(.required, for: .horizontal)
  567. let buttonRow = NSStackView(views: [applyButton, savedButton, dismissButton])
  568. buttonRow.orientation = .horizontal
  569. buttonRow.spacing = 8
  570. buttonRow.alignment = .centerY
  571. buttonRow.translatesAutoresizingMaskIntoConstraints = false
  572. buttonRow.setContentHuggingPriority(.required, for: .horizontal)
  573. buttonRow.setContentCompressionResistancePriority(.required, for: .horizontal)
  574. // Title hugs the leading edge; a low–hugging-priority spacer absorbs remaining width so buttons stay trailing.
  575. titleField.setContentHuggingPriority(.defaultHigh, for: .horizontal)
  576. titleField.setContentCompressionResistancePriority(.defaultLow, for: .horizontal)
  577. let titleRowSpacer = NSView()
  578. titleRowSpacer.translatesAutoresizingMaskIntoConstraints = false
  579. titleRowSpacer.setContentHuggingPriority(NSLayoutConstraint.Priority(1), for: .horizontal)
  580. titleRowSpacer.setContentCompressionResistancePriority(NSLayoutConstraint.Priority(1), for: .horizontal)
  581. let titleAndActionsRow = NSStackView(views: [titleField, titleRowSpacer, buttonRow])
  582. titleAndActionsRow.orientation = .horizontal
  583. titleAndActionsRow.spacing = 0
  584. titleAndActionsRow.setCustomSpacing(14, after: titleRowSpacer)
  585. titleAndActionsRow.alignment = .centerY
  586. titleAndActionsRow.distribution = .fill
  587. titleAndActionsRow.userInterfaceLayoutDirection = .leftToRight
  588. titleAndActionsRow.translatesAutoresizingMaskIntoConstraints = false
  589. let contentColumn = NSStackView(views: [titleAndActionsRow, descriptionField])
  590. contentColumn.orientation = .vertical
  591. contentColumn.spacing = 6
  592. contentColumn.alignment = .width
  593. contentColumn.translatesAutoresizingMaskIntoConstraints = false
  594. card.addSubview(contentColumn)
  595. NSLayoutConstraint.activate([
  596. contentColumn.leadingAnchor.constraint(equalTo: card.leadingAnchor, constant: 16),
  597. contentColumn.trailingAnchor.constraint(equalTo: card.trailingAnchor, constant: -16),
  598. contentColumn.topAnchor.constraint(equalTo: card.topAnchor, constant: 14),
  599. contentColumn.bottomAnchor.constraint(equalTo: card.bottomAnchor, constant: -14),
  600. applyButton.widthAnchor.constraint(greaterThanOrEqualToConstant: 72),
  601. applyButton.heightAnchor.constraint(equalToConstant: 28),
  602. savedButton.widthAnchor.constraint(greaterThanOrEqualToConstant: 72),
  603. savedButton.heightAnchor.constraint(equalToConstant: 28),
  604. dismissButton.widthAnchor.constraint(equalToConstant: 28),
  605. dismissButton.heightAnchor.constraint(equalToConstant: 28),
  606. descriptionField.widthAnchor.constraint(equalTo: contentColumn.widthAnchor)
  607. ])
  608. return card
  609. }
  610. private func styleJobSavedButton(_ button: NSButton) {
  611. button.wantsLayer = true
  612. button.layer?.cornerRadius = 6
  613. let on = button.state == .on
  614. let hovering = (button as? HoverableButton)?.isHovering ?? false
  615. if on {
  616. button.layer?.backgroundColor = (hovering ? Theme.selectionFillHover : Theme.selectionFill).cgColor
  617. button.layer?.borderWidth = 1
  618. button.layer?.borderColor = Theme.brandBlue.cgColor
  619. button.contentTintColor = Theme.brandBlue
  620. } else {
  621. button.layer?.backgroundColor = (hovering ? Theme.neutralHoverFill : Theme.cardBackground).cgColor
  622. button.layer?.borderWidth = 1
  623. button.layer?.borderColor = Theme.border.cgColor
  624. button.contentTintColor = Theme.primaryText
  625. }
  626. }
  627. @objc private func didTapJobApply(_ sender: NSButton) {
  628. guard let job = (sender as? JobPayloadButton)?.jobPayload else { return }
  629. if let rawURL = job.url, let url = URL(string: rawURL), !rawURL.isEmpty {
  630. NSWorkspace.shared.open(url)
  631. return
  632. }
  633. let allowed = CharacterSet.urlQueryAllowed
  634. let q = job.title.addingPercentEncoding(withAllowedCharacters: allowed) ?? ""
  635. guard let url = URL(string: "https://www.indeed.com/jobs?q=\(q)") else { return }
  636. NSWorkspace.shared.open(url)
  637. }
  638. @objc private func didTapJobSaved(_ sender: NSButton) {
  639. guard let job = (sender as? JobPayloadButton)?.jobPayload else { return }
  640. let willSave = !isJobSaved(job)
  641. applySavedState(willSave, for: job)
  642. sender.state = willSave ? .on : .off
  643. sender.title = willSave ? "Saved" : "Save"
  644. styleJobSavedButton(sender)
  645. if isSavedJobsSidebarIndex(selectedSidebarIndex) {
  646. reloadSavedJobsListings()
  647. }
  648. }
  649. @objc private func didTapJobDismiss(_ sender: NSButton) {
  650. guard let button = sender as? JobPayloadButton, let job = button.jobPayload else { return }
  651. switch button.cardContext {
  652. case .homeSearchResults:
  653. removeJobCardFromChat(originating: button, job: job)
  654. case .savedJobsPage:
  655. applySavedState(false, for: job)
  656. reloadSavedJobsListings()
  657. }
  658. }
  659. /// 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.
  660. private func removeJobCardFromChat(originating button: NSView, job: JobListing) {
  661. var node: NSView? = button
  662. var card: NSView?
  663. var stack: ChatJobsStackView?
  664. while let v = node {
  665. if let parent = v.superview as? ChatJobsStackView {
  666. card = v
  667. stack = parent
  668. break
  669. }
  670. node = v.superview
  671. }
  672. guard let card, let stack else { return }
  673. stack.removeArrangedSubview(card)
  674. card.removeFromSuperview()
  675. lastSearchResults.removeAll { $0 == job }
  676. }
  677. private func configureSearchBar() {
  678. let pillCorner: CGFloat = 27
  679. let barHeight: CGFloat = 54
  680. searchBarShadowHost.translatesAutoresizingMaskIntoConstraints = false
  681. searchBarShadowHost.wantsLayer = true
  682. searchBarShadowHost.layer?.masksToBounds = false
  683. searchBarShadowHost.layer?.shadowColor = NSColor.black.withAlphaComponent(0.18).cgColor
  684. searchBarShadowHost.layer?.shadowOffset = CGSize(width: 0, height: 2)
  685. searchBarShadowHost.layer?.shadowRadius = 10
  686. searchBarShadowHost.layer?.shadowOpacity = 1
  687. searchBarShadowHost.setContentHuggingPriority(.defaultHigh, for: .vertical)
  688. searchCard.translatesAutoresizingMaskIntoConstraints = false
  689. searchCard.wantsLayer = true
  690. searchCard.layer?.backgroundColor = Theme.cardBackground.cgColor
  691. searchCard.layer?.cornerRadius = pillCorner
  692. searchCard.layer?.borderWidth = 1
  693. searchCard.layer?.borderColor = Theme.searchBarBorder.cgColor
  694. searchCard.layer?.masksToBounds = true
  695. searchCard.hoverHandler = { [weak self] hovering in
  696. guard let self else { return }
  697. CATransaction.begin()
  698. CATransaction.setAnimationDuration(0.15)
  699. self.searchCard.layer?.backgroundColor = (hovering ? Theme.neutralHoverFill : Theme.cardBackground).cgColor
  700. self.searchCard.layer?.borderColor = (hovering ? Theme.searchBarBorderHover : Theme.searchBarBorder).cgColor
  701. self.searchBarShadowHost.layer?.shadowColor = NSColor.black.withAlphaComponent(hovering ? 0.24 : 0.18).cgColor
  702. self.searchBarShadowHost.layer?.shadowRadius = hovering ? 12 : 10
  703. CATransaction.commit()
  704. }
  705. searchBarShadowHost.addSubview(searchCard)
  706. func configureField(_ field: NSTextField, placeholder: String) {
  707. field.translatesAutoresizingMaskIntoConstraints = false
  708. field.isBordered = false
  709. field.drawsBackground = false
  710. field.focusRingType = .none
  711. field.font = .systemFont(ofSize: 14, weight: .regular)
  712. field.textColor = Theme.primaryText
  713. field.delegate = self
  714. field.placeholderAttributedString = NSAttributedString(
  715. string: placeholder,
  716. attributes: [
  717. .foregroundColor: Theme.secondaryText,
  718. .font: NSFont.systemFont(ofSize: 14, weight: .regular)
  719. ]
  720. )
  721. field.cell?.usesSingleLineMode = true
  722. field.cell?.wraps = false
  723. field.cell?.isScrollable = true
  724. field.target = self
  725. field.action = #selector(didSubmitSearch)
  726. }
  727. jobSearchIcon.translatesAutoresizingMaskIntoConstraints = false
  728. jobSearchIcon.symbolConfiguration = NSImage.SymbolConfiguration(pointSize: 15, weight: .medium)
  729. jobSearchIcon.image = NSImage(systemSymbolName: "magnifyingglass", accessibilityDescription: "Job search")
  730. jobSearchIcon.contentTintColor = Theme.primaryText
  731. configureField(jobKeywordsField, placeholder: "Ask for roles, skills, salary, or job descriptions...")
  732. let ctaHeight: CGFloat = 42
  733. let ctaCorner = ctaHeight / 2
  734. findJobsCTAHost.translatesAutoresizingMaskIntoConstraints = false
  735. findJobsCTAHost.wantsLayer = true
  736. findJobsCTAHost.layer?.masksToBounds = false
  737. findJobsCTAHost.layer?.shadowColor = NSColor.black.cgColor
  738. findJobsCTAHost.layer?.shadowOpacity = 0.16
  739. findJobsCTAHost.layer?.shadowOffset = CGSize(width: 0, height: 2)
  740. findJobsCTAHost.layer?.shadowRadius = 6
  741. findJobsCTAChrome.translatesAutoresizingMaskIntoConstraints = false
  742. findJobsCTAChrome.wantsLayer = true
  743. findJobsCTAChrome.layer?.masksToBounds = true
  744. findJobsCTAChrome.layer?.cornerRadius = ctaCorner
  745. if #available(macOS 11.0, *) {
  746. findJobsCTAChrome.layer?.cornerCurve = .continuous
  747. }
  748. let gradient = CAGradientLayer()
  749. gradient.colors = [Theme.findJobsCTAHighlight.cgColor, Theme.brandBlue.cgColor]
  750. gradient.startPoint = CGPoint(x: 0.5, y: 1)
  751. gradient.endPoint = CGPoint(x: 0.5, y: 0)
  752. findJobsCTAChrome.layer?.addSublayer(gradient)
  753. findJobsCTAGradientLayer = gradient
  754. // 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.
  755. findJobsCTAChrome.pointerCursor = true
  756. findJobsCTAChrome.hoverHandler = { [weak self] hovering in
  757. guard let layer = self?.findJobsCTAGradientLayer else { return }
  758. CATransaction.begin()
  759. CATransaction.setAnimationDuration(0.15)
  760. layer.colors = hovering
  761. ? [Theme.findJobsCTAHighlightHover.cgColor, Theme.brandBlueHover.cgColor]
  762. : [Theme.findJobsCTAHighlight.cgColor, Theme.brandBlue.cgColor]
  763. CATransaction.commit()
  764. }
  765. findJobsButton.translatesAutoresizingMaskIntoConstraints = false
  766. findJobsButton.title = ""
  767. findJobsButton.attributedTitle = NSAttributedString(
  768. string: "Send",
  769. attributes: [
  770. .font: NSFont.systemFont(ofSize: 14, weight: .semibold),
  771. .foregroundColor: Theme.proCTAText,
  772. .kern: 0.35
  773. ]
  774. )
  775. findJobsButton.isBordered = false
  776. findJobsButton.bezelStyle = .rounded
  777. findJobsButton.wantsLayer = true
  778. findJobsButton.layer?.backgroundColor = NSColor.clear.cgColor
  779. findJobsButton.focusRingType = .none
  780. findJobsButton.target = self
  781. findJobsButton.action = #selector(didSubmitSearch)
  782. findJobsButton.setContentHuggingPriority(.required, for: .horizontal)
  783. findJobsButton.setContentCompressionResistancePriority(.required, for: .horizontal)
  784. findJobsCTAHost.addSubview(findJobsCTAChrome)
  785. findJobsCTAHost.addSubview(findJobsButton)
  786. NSLayoutConstraint.activate([
  787. findJobsCTAChrome.leadingAnchor.constraint(equalTo: findJobsCTAHost.leadingAnchor),
  788. findJobsCTAChrome.trailingAnchor.constraint(equalTo: findJobsCTAHost.trailingAnchor),
  789. findJobsCTAChrome.topAnchor.constraint(equalTo: findJobsCTAHost.topAnchor),
  790. findJobsCTAChrome.bottomAnchor.constraint(equalTo: findJobsCTAHost.bottomAnchor),
  791. findJobsButton.leadingAnchor.constraint(equalTo: findJobsCTAHost.leadingAnchor, constant: 14),
  792. findJobsButton.trailingAnchor.constraint(equalTo: findJobsCTAHost.trailingAnchor, constant: -14),
  793. findJobsButton.topAnchor.constraint(equalTo: findJobsCTAHost.topAnchor),
  794. findJobsButton.bottomAnchor.constraint(equalTo: findJobsCTAHost.bottomAnchor)
  795. ])
  796. let keywordsStack = NSStackView(views: [jobSearchIcon, jobKeywordsField])
  797. keywordsStack.orientation = .horizontal
  798. keywordsStack.spacing = 10
  799. keywordsStack.alignment = .centerY
  800. keywordsStack.translatesAutoresizingMaskIntoConstraints = false
  801. keywordsStack.edgeInsets = NSEdgeInsets(top: 0, left: 18, bottom: 0, right: 10)
  802. keywordsStack.setContentHuggingPriority(.defaultLow, for: .horizontal)
  803. let row = NSStackView(views: [keywordsStack, findJobsCTAHost])
  804. row.orientation = .horizontal
  805. row.spacing = 0
  806. row.alignment = .centerY
  807. row.distribution = .fill
  808. row.translatesAutoresizingMaskIntoConstraints = false
  809. row.edgeInsets = NSEdgeInsets(top: 0, left: 0, bottom: 0, right: 7)
  810. searchCard.addSubview(row)
  811. NSLayoutConstraint.activate([
  812. searchCard.leadingAnchor.constraint(equalTo: searchBarShadowHost.leadingAnchor),
  813. searchCard.trailingAnchor.constraint(equalTo: searchBarShadowHost.trailingAnchor),
  814. searchCard.topAnchor.constraint(equalTo: searchBarShadowHost.topAnchor),
  815. searchCard.bottomAnchor.constraint(equalTo: searchBarShadowHost.bottomAnchor),
  816. searchBarShadowHost.heightAnchor.constraint(equalToConstant: barHeight),
  817. row.leadingAnchor.constraint(equalTo: searchCard.leadingAnchor),
  818. row.trailingAnchor.constraint(equalTo: searchCard.trailingAnchor),
  819. row.topAnchor.constraint(equalTo: searchCard.topAnchor),
  820. row.bottomAnchor.constraint(equalTo: searchCard.bottomAnchor),
  821. jobSearchIcon.widthAnchor.constraint(equalToConstant: 18),
  822. jobSearchIcon.heightAnchor.constraint(equalToConstant: 18),
  823. findJobsCTAHost.heightAnchor.constraint(equalToConstant: ctaHeight),
  824. findJobsCTAHost.widthAnchor.constraint(greaterThanOrEqualToConstant: 112)
  825. ])
  826. searchCard.hoverHandler = nil
  827. }
  828. private func updateFindJobsCTAShadowPath() {
  829. guard findJobsCTAHost.bounds.width > 0, findJobsCTAHost.bounds.height > 0 else { return }
  830. let r = findJobsCTAHost.bounds
  831. let radius = min(r.height / 2, r.width / 2)
  832. findJobsCTAHost.layer?.shadowPath = CGPath(
  833. roundedRect: r,
  834. cornerWidth: radius,
  835. cornerHeight: radius,
  836. transform: nil
  837. )
  838. }
  839. private func configureNonHomePlaceholder() {
  840. nonHomeHost.translatesAutoresizingMaskIntoConstraints = false
  841. nonHomeHost.wantsLayer = true
  842. nonHomeHost.layer?.backgroundColor = Theme.mainHostBackground.cgColor
  843. nonHomeHost.isHidden = true
  844. nonHomeGenericContainer.translatesAutoresizingMaskIntoConstraints = false
  845. savedJobsPageContainer.translatesAutoresizingMaskIntoConstraints = false
  846. settingsPageContainer.translatesAutoresizingMaskIntoConstraints = false
  847. nonHomeHost.addSubview(nonHomeGenericContainer)
  848. nonHomeHost.addSubview(savedJobsPageContainer)
  849. nonHomeHost.addSubview(settingsPageContainer)
  850. NSLayoutConstraint.activate([
  851. nonHomeGenericContainer.leadingAnchor.constraint(equalTo: nonHomeHost.leadingAnchor),
  852. nonHomeGenericContainer.trailingAnchor.constraint(equalTo: nonHomeHost.trailingAnchor),
  853. nonHomeGenericContainer.topAnchor.constraint(equalTo: nonHomeHost.topAnchor),
  854. nonHomeGenericContainer.bottomAnchor.constraint(equalTo: nonHomeHost.bottomAnchor),
  855. savedJobsPageContainer.leadingAnchor.constraint(equalTo: nonHomeHost.leadingAnchor),
  856. savedJobsPageContainer.trailingAnchor.constraint(equalTo: nonHomeHost.trailingAnchor),
  857. savedJobsPageContainer.topAnchor.constraint(equalTo: nonHomeHost.topAnchor),
  858. savedJobsPageContainer.bottomAnchor.constraint(equalTo: nonHomeHost.bottomAnchor),
  859. settingsPageContainer.leadingAnchor.constraint(equalTo: nonHomeHost.leadingAnchor),
  860. settingsPageContainer.trailingAnchor.constraint(equalTo: nonHomeHost.trailingAnchor),
  861. settingsPageContainer.topAnchor.constraint(equalTo: nonHomeHost.topAnchor),
  862. settingsPageContainer.bottomAnchor.constraint(equalTo: nonHomeHost.bottomAnchor)
  863. ])
  864. nonHomeTitleLabel.font = .systemFont(ofSize: 22, weight: .bold)
  865. nonHomeTitleLabel.textColor = Theme.primaryText
  866. nonHomeTitleLabel.alignment = .center
  867. nonHomeTitleLabel.maximumNumberOfLines = 1
  868. nonHomeSubtitleLabel.font = .systemFont(ofSize: 14, weight: .regular)
  869. nonHomeSubtitleLabel.textColor = Theme.secondaryText
  870. nonHomeSubtitleLabel.alignment = .center
  871. nonHomeSubtitleLabel.maximumNumberOfLines = 0
  872. nonHomeSubtitleLabel.stringValue = "This area is not available in the preview build. Use Home to search jobs."
  873. let genericStack = NSStackView(views: [nonHomeTitleLabel, nonHomeSubtitleLabel])
  874. genericStack.orientation = .vertical
  875. genericStack.spacing = 10
  876. genericStack.alignment = .centerX
  877. genericStack.translatesAutoresizingMaskIntoConstraints = false
  878. nonHomeGenericContainer.addSubview(genericStack)
  879. NSLayoutConstraint.activate([
  880. genericStack.centerXAnchor.constraint(equalTo: nonHomeGenericContainer.centerXAnchor),
  881. genericStack.centerYAnchor.constraint(equalTo: nonHomeGenericContainer.centerYAnchor),
  882. genericStack.leadingAnchor.constraint(greaterThanOrEqualTo: nonHomeGenericContainer.leadingAnchor, constant: 32),
  883. genericStack.trailingAnchor.constraint(lessThanOrEqualTo: nonHomeGenericContainer.trailingAnchor, constant: -32),
  884. nonHomeSubtitleLabel.widthAnchor.constraint(lessThanOrEqualToConstant: 420)
  885. ])
  886. savedJobsPageTitleLabel.font = .systemFont(ofSize: 22, weight: .bold)
  887. savedJobsPageTitleLabel.textColor = Theme.primaryText
  888. savedJobsPageTitleLabel.alignment = .left
  889. savedJobsPageTitleLabel.maximumNumberOfLines = 1
  890. savedJobsPageSubtitleLabel.font = .systemFont(ofSize: 14, weight: .regular)
  891. savedJobsPageSubtitleLabel.textColor = Theme.secondaryText
  892. savedJobsPageSubtitleLabel.alignment = .left
  893. savedJobsPageSubtitleLabel.maximumNumberOfLines = 0
  894. savedJobsDocumentView.translatesAutoresizingMaskIntoConstraints = false
  895. savedJobsStack.orientation = .vertical
  896. savedJobsStack.spacing = 14
  897. savedJobsStack.alignment = .leading
  898. savedJobsStack.distribution = .fill
  899. savedJobsStack.translatesAutoresizingMaskIntoConstraints = false
  900. savedJobsStack.setContentHuggingPriority(.defaultHigh, for: .vertical)
  901. savedJobsStack.setHuggingPriority(.defaultLow, for: .horizontal)
  902. savedJobsDocumentView.addSubview(savedJobsStack)
  903. NSLayoutConstraint.activate([
  904. savedJobsStack.leadingAnchor.constraint(equalTo: savedJobsDocumentView.leadingAnchor),
  905. savedJobsStack.trailingAnchor.constraint(equalTo: savedJobsDocumentView.trailingAnchor),
  906. savedJobsStack.topAnchor.constraint(equalTo: savedJobsDocumentView.topAnchor),
  907. savedJobsStack.bottomAnchor.constraint(equalTo: savedJobsDocumentView.bottomAnchor)
  908. ])
  909. savedJobsScrollView.translatesAutoresizingMaskIntoConstraints = false
  910. savedJobsScrollView.hasVerticalScroller = true
  911. savedJobsScrollView.hasHorizontalScroller = false
  912. savedJobsScrollView.scrollerStyle = .legacy
  913. savedJobsScrollView.autohidesScrollers = true
  914. savedJobsScrollView.drawsBackground = false
  915. savedJobsScrollView.borderType = .noBorder
  916. savedJobsScrollView.documentView = savedJobsDocumentView
  917. savedJobsScrollView.setContentHuggingPriority(.defaultLow, for: .vertical)
  918. savedJobsScrollView.setContentCompressionResistancePriority(.defaultLow, for: .vertical)
  919. let savedHeaderStack = NSStackView(views: [savedJobsPageTitleLabel, savedJobsPageSubtitleLabel])
  920. savedHeaderStack.orientation = .vertical
  921. savedHeaderStack.spacing = 6
  922. savedHeaderStack.alignment = .leading
  923. savedHeaderStack.translatesAutoresizingMaskIntoConstraints = false
  924. let savedOuterStack = NSStackView(views: [savedHeaderStack, savedJobsScrollView])
  925. savedOuterStack.orientation = .vertical
  926. savedOuterStack.spacing = 16
  927. // Leading alignment plus explicit column width keeps the title and subtitle on the same edge as the cards.
  928. savedOuterStack.alignment = .leading
  929. savedOuterStack.translatesAutoresizingMaskIntoConstraints = false
  930. savedJobsPageContainer.userInterfaceLayoutDirection = .leftToRight
  931. savedJobsPageContainer.addSubview(savedOuterStack)
  932. NSLayoutConstraint.activate([
  933. savedOuterStack.leadingAnchor.constraint(equalTo: savedJobsPageContainer.leadingAnchor, constant: 32),
  934. savedOuterStack.trailingAnchor.constraint(equalTo: savedJobsPageContainer.trailingAnchor, constant: -32),
  935. savedOuterStack.topAnchor.constraint(equalTo: savedJobsPageContainer.topAnchor, constant: 8),
  936. savedOuterStack.bottomAnchor.constraint(equalTo: savedJobsPageContainer.bottomAnchor),
  937. savedHeaderStack.widthAnchor.constraint(equalTo: savedOuterStack.widthAnchor),
  938. savedJobsScrollView.widthAnchor.constraint(equalTo: savedOuterStack.widthAnchor),
  939. savedJobsDocumentView.topAnchor.constraint(equalTo: savedJobsScrollView.contentView.topAnchor),
  940. savedJobsDocumentView.leadingAnchor.constraint(equalTo: savedJobsScrollView.contentView.leadingAnchor),
  941. savedJobsDocumentView.widthAnchor.constraint(equalTo: savedJobsScrollView.contentView.widthAnchor)
  942. ])
  943. configureSettingsPage()
  944. }
  945. private func configureSettingsPage() {
  946. settingsPageContainer.wantsLayer = true
  947. settingsPageContainer.layer?.backgroundColor = Theme.settingsPageBackground.cgColor
  948. settingsPageContainer.isHidden = true
  949. let contentStack = NSStackView()
  950. contentStack.orientation = .vertical
  951. contentStack.spacing = 26
  952. contentStack.alignment = .leading
  953. contentStack.translatesAutoresizingMaskIntoConstraints = false
  954. let settingsSection = makeSettingsSection(rows: [
  955. makeSettingsRow(title: "Share App", systemImage: "square.and.arrow.up", accessory: nil),
  956. makeSettingsRow(title: "Theme", systemImage: "circle.lefthalf.filled", accessory: makeThemeControl()),
  957. makeSettingsRow(title: "More Apps", systemImage: "square.grid.2x2", accessory: nil)
  958. ])
  959. let aboutTitle = NSTextField(labelWithString: "About")
  960. aboutTitle.font = .systemFont(ofSize: 12, weight: .semibold)
  961. aboutTitle.textColor = Theme.secondaryText
  962. aboutTitle.alignment = .left
  963. let aboutSection = makeSettingsSection(rows: [
  964. makeSettingsRow(title: "Support", systemImage: "questionmark.circle", accessory: nil),
  965. makeSettingsRow(title: "Terms of Use", systemImage: "doc.text", accessory: nil),
  966. makeSettingsRow(title: "Privacy Policy", systemImage: "shield", accessory: nil)
  967. ])
  968. let aboutStack = NSStackView(views: [aboutTitle, aboutSection])
  969. aboutStack.orientation = .vertical
  970. aboutStack.spacing = 14
  971. aboutStack.alignment = .leading
  972. aboutStack.translatesAutoresizingMaskIntoConstraints = false
  973. contentStack.addArrangedSubview(settingsSection)
  974. contentStack.addArrangedSubview(aboutStack)
  975. settingsPageContainer.addSubview(contentStack)
  976. NSLayoutConstraint.activate([
  977. contentStack.leadingAnchor.constraint(equalTo: settingsPageContainer.leadingAnchor, constant: 42),
  978. contentStack.trailingAnchor.constraint(lessThanOrEqualTo: settingsPageContainer.trailingAnchor, constant: -42),
  979. contentStack.topAnchor.constraint(equalTo: settingsPageContainer.topAnchor, constant: 48),
  980. settingsSection.widthAnchor.constraint(equalTo: contentStack.widthAnchor),
  981. aboutStack.widthAnchor.constraint(equalTo: contentStack.widthAnchor),
  982. aboutSection.widthAnchor.constraint(equalTo: aboutStack.widthAnchor),
  983. contentStack.widthAnchor.constraint(equalTo: settingsPageContainer.widthAnchor, constant: -84)
  984. ])
  985. }
  986. private func makeThemeControl() -> NSSegmentedControl {
  987. themeControl.target = self
  988. themeControl.action = #selector(didChangeThemeSelection(_:))
  989. themeControl.selectedSegment = 0
  990. themeControl.segmentStyle = .rounded
  991. themeControl.controlSize = .large
  992. themeControl.font = .systemFont(ofSize: 13, weight: .semibold)
  993. themeControl.translatesAutoresizingMaskIntoConstraints = false
  994. themeControl.widthAnchor.constraint(equalToConstant: 204).isActive = true
  995. themeControl.heightAnchor.constraint(equalToConstant: 30).isActive = true
  996. return themeControl
  997. }
  998. private func makeSettingsSection(rows: [NSView]) -> NSView {
  999. let section = NSStackView()
  1000. section.orientation = .vertical
  1001. section.spacing = 0
  1002. section.alignment = .leading
  1003. section.translatesAutoresizingMaskIntoConstraints = false
  1004. section.wantsLayer = true
  1005. section.layer?.backgroundColor = Theme.settingsGroupBackground.cgColor
  1006. section.layer?.cornerRadius = 14
  1007. section.layer?.borderWidth = 1
  1008. section.layer?.borderColor = Theme.border.cgColor
  1009. section.layer?.masksToBounds = true
  1010. for (index, row) in rows.enumerated() {
  1011. section.addArrangedSubview(row)
  1012. row.widthAnchor.constraint(equalTo: section.widthAnchor).isActive = true
  1013. if index < rows.count - 1 {
  1014. let divider = NSView()
  1015. divider.translatesAutoresizingMaskIntoConstraints = false
  1016. divider.wantsLayer = true
  1017. divider.layer?.backgroundColor = Theme.settingsDivider.cgColor
  1018. section.addArrangedSubview(divider)
  1019. NSLayoutConstraint.activate([
  1020. divider.heightAnchor.constraint(equalToConstant: 1),
  1021. divider.leadingAnchor.constraint(equalTo: section.leadingAnchor),
  1022. divider.trailingAnchor.constraint(equalTo: section.trailingAnchor)
  1023. ])
  1024. }
  1025. }
  1026. return section
  1027. }
  1028. private func makeSettingsRow(title: String, systemImage: String, accessory: NSView?) -> NSView {
  1029. let row = NSView()
  1030. row.translatesAutoresizingMaskIntoConstraints = false
  1031. row.wantsLayer = true
  1032. let iconTile = NSView()
  1033. iconTile.translatesAutoresizingMaskIntoConstraints = false
  1034. iconTile.wantsLayer = true
  1035. iconTile.layer?.backgroundColor = Theme.settingsIconBackground.cgColor
  1036. iconTile.layer?.cornerRadius = 9
  1037. let icon = NSImageView()
  1038. icon.translatesAutoresizingMaskIntoConstraints = false
  1039. icon.symbolConfiguration = NSImage.SymbolConfiguration(pointSize: 13, weight: .medium)
  1040. icon.image = NSImage(systemSymbolName: systemImage, accessibilityDescription: title)
  1041. icon.contentTintColor = Theme.brandBlue
  1042. let titleLabel = NSTextField(labelWithString: title)
  1043. titleLabel.font = .systemFont(ofSize: 14, weight: .medium)
  1044. titleLabel.textColor = Theme.secondaryText
  1045. titleLabel.alignment = .left
  1046. let rowStack = NSStackView()
  1047. rowStack.orientation = .horizontal
  1048. rowStack.spacing = 16
  1049. rowStack.alignment = .centerY
  1050. rowStack.translatesAutoresizingMaskIntoConstraints = false
  1051. let spacer = NSView()
  1052. spacer.translatesAutoresizingMaskIntoConstraints = false
  1053. spacer.setContentHuggingPriority(NSLayoutConstraint.Priority(1), for: .horizontal)
  1054. iconTile.addSubview(icon)
  1055. rowStack.addArrangedSubview(iconTile)
  1056. rowStack.addArrangedSubview(titleLabel)
  1057. rowStack.addArrangedSubview(spacer)
  1058. if let accessory {
  1059. rowStack.addArrangedSubview(accessory)
  1060. }
  1061. row.addSubview(rowStack)
  1062. NSLayoutConstraint.activate([
  1063. row.heightAnchor.constraint(equalToConstant: 68),
  1064. iconTile.widthAnchor.constraint(equalToConstant: 38),
  1065. iconTile.heightAnchor.constraint(equalToConstant: 38),
  1066. icon.centerXAnchor.constraint(equalTo: iconTile.centerXAnchor),
  1067. icon.centerYAnchor.constraint(equalTo: iconTile.centerYAnchor),
  1068. icon.widthAnchor.constraint(equalToConstant: 20),
  1069. icon.heightAnchor.constraint(equalToConstant: 20),
  1070. rowStack.leadingAnchor.constraint(equalTo: row.leadingAnchor, constant: 16),
  1071. rowStack.trailingAnchor.constraint(equalTo: row.trailingAnchor, constant: -16),
  1072. rowStack.topAnchor.constraint(equalTo: row.topAnchor),
  1073. rowStack.bottomAnchor.constraint(equalTo: row.bottomAnchor)
  1074. ])
  1075. return row
  1076. }
  1077. private func reloadSavedJobsListings() {
  1078. savedJobsStack.arrangedSubviews.forEach {
  1079. savedJobsStack.removeArrangedSubview($0)
  1080. $0.removeFromSuperview()
  1081. }
  1082. if savedJobOrder.isEmpty {
  1083. savedJobsPageSubtitleLabel.stringValue = "Save jobs from Home to see them here."
  1084. let empty = NSTextField(wrappingLabelWithString: "No saved jobs yet. Search on Home, then tap Save on a listing.")
  1085. empty.font = .systemFont(ofSize: 14, weight: .regular)
  1086. empty.textColor = Theme.secondaryText
  1087. empty.alignment = .left
  1088. empty.maximumNumberOfLines = 0
  1089. empty.translatesAutoresizingMaskIntoConstraints = false
  1090. savedJobsStack.addArrangedSubview(empty)
  1091. empty.widthAnchor.constraint(equalTo: savedJobsStack.widthAnchor).isActive = true
  1092. return
  1093. }
  1094. savedJobsPageSubtitleLabel.stringValue = "\(savedJobOrder.count) saved \(savedJobOrder.count == 1 ? "position" : "positions")"
  1095. for job in savedJobOrder {
  1096. let card = makeJobListingCard(job, context: .savedJobsPage)
  1097. savedJobsStack.addArrangedSubview(card)
  1098. card.widthAnchor.constraint(equalTo: savedJobsStack.widthAnchor).isActive = true
  1099. }
  1100. }
  1101. private func isSavedJobsSidebarIndex(_ index: Int) -> Bool {
  1102. guard index >= 0, index < currentSidebarItems.count else { return false }
  1103. return currentSidebarItems[index].title == "Saved Jobs"
  1104. }
  1105. private func isHomeSidebarIndex(_ index: Int) -> Bool {
  1106. guard index >= 0, index < currentSidebarItems.count else { return false }
  1107. return currentSidebarItems[index].title == "Home"
  1108. }
  1109. private func isSettingsSidebarIndex(_ index: Int) -> Bool {
  1110. guard index >= 0, index < currentSidebarItems.count else { return false }
  1111. return currentSidebarItems[index].title == "Settings"
  1112. }
  1113. private func updateMainContentVisibility() {
  1114. let home = isHomeSidebarIndex(selectedSidebarIndex)
  1115. let savedJobs = isSavedJobsSidebarIndex(selectedSidebarIndex)
  1116. let settings = isSettingsSidebarIndex(selectedSidebarIndex)
  1117. mainOverlay.isHidden = !home
  1118. nonHomeHost.isHidden = home
  1119. nonHomeGenericContainer.isHidden = savedJobs || settings
  1120. savedJobsPageContainer.isHidden = !savedJobs
  1121. settingsPageContainer.isHidden = !settings
  1122. if !home, selectedSidebarIndex < currentSidebarItems.count {
  1123. if savedJobs {
  1124. reloadSavedJobsListings()
  1125. } else if settings {
  1126. window?.makeFirstResponder(nil)
  1127. } else {
  1128. nonHomeTitleLabel.stringValue = currentSidebarItems[selectedSidebarIndex].title
  1129. }
  1130. }
  1131. if home {
  1132. syncWelcomeSubtitleBreathingAnimation()
  1133. syncChatStatusSparkleAnimation()
  1134. } else {
  1135. stopWelcomeSubtitleBreathingAnimation()
  1136. stopChatStatusSparkleAnimation()
  1137. }
  1138. }
  1139. /// Restores the main job-search experience: cleared query and a fresh chat history.
  1140. private func applyHomeState() {
  1141. jobKeywordsField.stringValue = ""
  1142. resetChatState()
  1143. window?.makeFirstResponder(nil)
  1144. }
  1145. private func updateSearchBarShadowPath() {
  1146. guard searchBarShadowHost.bounds.width > 0, searchBarShadowHost.bounds.height > 0 else { return }
  1147. let r = searchBarShadowHost.bounds
  1148. let radius = min(r.height / 2, 27)
  1149. searchBarShadowHost.layer?.shadowPath = CGPath(
  1150. roundedRect: r,
  1151. cornerWidth: radius,
  1152. cornerHeight: radius,
  1153. transform: nil
  1154. )
  1155. }
  1156. @objc private func didSubmitSearch() {
  1157. let prompt = jobKeywordsField.stringValue.trimmingCharacters(in: .whitespacesAndNewlines)
  1158. guard !prompt.isEmpty, !isAwaitingResponse else { return }
  1159. let isContinuation = isContinuationPrompt(prompt)
  1160. let effectiveQuery = resolvedSearchQuery(for: prompt)
  1161. appendChatBubble(text: prompt, isUser: true)
  1162. chatMessages.append(ChatMessage(role: "user", content: prompt))
  1163. jobKeywordsField.stringValue = ""
  1164. startJobSearchRequest(effectiveQuery: effectiveQuery, isContinuation: isContinuation)
  1165. window?.makeFirstResponder(nil)
  1166. }
  1167. @objc private func didTapLoadMoreJobs() {
  1168. let prompt = "Show more jobs"
  1169. guard !isAwaitingResponse, isContinuationPrompt(prompt) else { return }
  1170. if anchorUserJobQuery(excludingLatestUserMessage: prompt) == nil { return }
  1171. appendChatBubble(text: prompt, isUser: true)
  1172. chatMessages.append(ChatMessage(role: "user", content: prompt))
  1173. let effectiveQuery = resolvedSearchQuery(for: prompt)
  1174. startJobSearchRequest(effectiveQuery: effectiveQuery, isContinuation: true)
  1175. }
  1176. private func startJobSearchRequest(effectiveQuery: String, isContinuation: Bool) {
  1177. isAwaitingResponse = true
  1178. setChatStatusLabel("Thinking...")
  1179. setInputEnabled(false)
  1180. let contextMessages = chatMessages
  1181. let maxJobs = Self.clampedJobsPerRequest()
  1182. jobSearchService.searchJobs(query: effectiveQuery, conversation: contextMessages, maxJobs: maxJobs) { [weak self] result in
  1183. DispatchQueue.main.async {
  1184. guard let self else { return }
  1185. self.isAwaitingResponse = false
  1186. self.setInputEnabled(true)
  1187. switch result {
  1188. case .success(let output):
  1189. let normalizedJobs = self.normalizedJobs(output.jobs)
  1190. let freshJobs: [JobListing]
  1191. if isContinuation {
  1192. // Continuations append only the *new* matches; previous cards already live in their own assistant message above.
  1193. let alreadySeen = Set(self.lastSearchResults)
  1194. freshJobs = normalizedJobs.filter { !alreadySeen.contains($0) }
  1195. } else {
  1196. freshJobs = normalizedJobs
  1197. }
  1198. self.lastSearchResults.append(contentsOf: freshJobs)
  1199. let reply = self.makeAssistantSearchReply(
  1200. query: effectiveQuery,
  1201. newJobsCount: freshJobs.count,
  1202. isContinuation: isContinuation
  1203. )
  1204. self.chatMessages.append(ChatMessage(role: "assistant", content: reply))
  1205. self.appendChatBubble(text: reply, isUser: false, jobs: freshJobs)
  1206. self.setChatStatusLabel("Ask for another role, company, or skill match")
  1207. case .failure(let error):
  1208. self.appendChatBubble(text: error.localizedDescription, isUser: false)
  1209. if error is URLError {
  1210. self.setChatStatusLabel("Could not reach API. Try again.")
  1211. } else {
  1212. self.setChatStatusLabel("Search did not finish. Try again.")
  1213. }
  1214. }
  1215. }
  1216. }
  1217. }
  1218. private func normalizedJobs(_ jobs: [JobListing]) -> [JobListing] {
  1219. let trimmed = jobs.map {
  1220. JobListing(
  1221. title: $0.title.trimmingCharacters(in: .whitespacesAndNewlines),
  1222. description: $0.description.trimmingCharacters(in: .whitespacesAndNewlines),
  1223. url: $0.url?.trimmingCharacters(in: .whitespacesAndNewlines)
  1224. )
  1225. }
  1226. return trimmed.filter { !$0.title.isEmpty && !$0.description.isEmpty }
  1227. }
  1228. private func makeAssistantSearchReply(query: String, newJobsCount: Int, isContinuation: Bool) -> String {
  1229. if newJobsCount == 0 {
  1230. if isContinuation {
  1231. return "I couldn't find new matches for \u{201C}\(query)\u{201D}. Try a different angle or a more specific keyword."
  1232. }
  1233. return "No jobs found for \u{201C}\(query)\u{201D}. Try another title, skill, company, or location."
  1234. }
  1235. let plural = newJobsCount == 1 ? "match" : "matches"
  1236. if isContinuation {
  1237. return "Here are \(newJobsCount) more \(plural) for \u{201C}\(query)\u{201D}."
  1238. }
  1239. return "Found \(newJobsCount) \(plural) for \u{201C}\(query)\u{201D}. Tap Apply to open the listing or Save to revisit later."
  1240. }
  1241. private func resolvedSearchQuery(for prompt: String) -> String {
  1242. let anchor = anchorUserJobQuery(excludingLatestUserMessage: prompt)
  1243. if isContinuationPrompt(prompt), !isRefinementPrompt(prompt) {
  1244. if let anchor { return anchor }
  1245. return prompt
  1246. }
  1247. if isRefinementPrompt(prompt), let anchor {
  1248. return "\(anchor). User follow-up (apply on top of the same search topic): \(prompt)"
  1249. }
  1250. return prompt
  1251. }
  1252. /// First prior user message that looks like an original job query (skips short continuations and refinements so follow-ups keep a stable topic anchor).
  1253. private func anchorUserJobQuery(excludingLatestUserMessage latest: String) -> String? {
  1254. let prior = Array(chatMessages.dropLast())
  1255. for message in prior.reversed() where message.role == "user" {
  1256. let candidate = message.content.trimmingCharacters(in: .whitespacesAndNewlines)
  1257. guard !candidate.isEmpty, candidate != latest else { continue }
  1258. if isContinuationPrompt(candidate) { continue }
  1259. if isRefinementPrompt(candidate) { continue }
  1260. return candidate
  1261. }
  1262. return nil
  1263. }
  1264. private func isContinuationPrompt(_ prompt: String) -> Bool {
  1265. let normalized = prompt.trimmingCharacters(in: .whitespacesAndNewlines).lowercased()
  1266. let continuationPhrases: Set<String> = [
  1267. "more",
  1268. "show more",
  1269. "more jobs",
  1270. "more results",
  1271. "do more searches",
  1272. "more searches",
  1273. "search more",
  1274. "continue",
  1275. "next"
  1276. ]
  1277. if continuationPhrases.contains(normalized) {
  1278. return true
  1279. }
  1280. return normalized.contains("more search") || normalized.contains("more job")
  1281. }
  1282. /// Follow-ups that narrow, re-rank, or re-frame results rather than starting a brand-new role search.
  1283. /// 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.
  1284. private func isRefinementPrompt(_ prompt: String) -> Bool {
  1285. let n = prompt.trimmingCharacters(in: .whitespacesAndNewlines).lowercased()
  1286. if n.isEmpty { return false }
  1287. let strongPhrases = [
  1288. "higher pay", "high pay", "better pay", "more pay", "top pay", "best pay",
  1289. "higher salary", "better salary", "more salary", "pay rate", "hourly rate",
  1290. "paid more", "paying more", "earn more", "better paid", "paying better",
  1291. "work from home", "in office", "in-office", "on-site only", "remote only",
  1292. "hybrid only", "onsite only", "visa sponsorship", "h1b",
  1293. "entry level", "entry-level", "mid level", "mid-level", "full time", "full-time",
  1294. "part time", "part-time",
  1295. "closer to", "nearer", "different city", "different state", "relocate",
  1296. "filter", "only show", "just show", "exclude", "without", "sort by", "rank by",
  1297. "cheaper", "lower pay", "less travel",
  1298. "better benefits", "equity", "bonus", "overtime",
  1299. "get me the jobs", "show me the jobs", "give me the jobs", "narrow", "refine"
  1300. ]
  1301. if strongPhrases.contains(where: { n.contains($0) }) { return true }
  1302. if n.hasPrefix("only ") || n.hasPrefix("just ") { return true }
  1303. guard !lastSearchResults.isEmpty, n.count <= 52 else { return false }
  1304. let softAfterResults = [
  1305. "remote", "hybrid", "onsite", "on-site", "senior", "junior", "staff", "lead",
  1306. "principal", "intern", "contract", "location"
  1307. ]
  1308. return softAfterResults.contains(where: { n.contains($0) })
  1309. }
  1310. func controlTextDidBeginEditing(_ obj: Notification) {
  1311. applySearchFieldInsertionPoint(obj.object)
  1312. if (obj.object as? NSTextField) === jobKeywordsField {
  1313. setChatStatusLabel("Opening the vault...")
  1314. }
  1315. }
  1316. func controlTextDidChange(_ obj: Notification) {
  1317. applySearchFieldInsertionPoint(obj.object)
  1318. }
  1319. func control(_ control: NSControl, textView: NSTextView, doCommandBy commandSelector: Selector) -> Bool {
  1320. guard control === jobKeywordsField, commandSelector == #selector(NSResponder.insertNewline(_:)) else {
  1321. return false
  1322. }
  1323. didSubmitSearch()
  1324. return true
  1325. }
  1326. private func applySearchFieldInsertionPoint(_ object: Any?) {
  1327. guard let field = object as? NSTextField,
  1328. field === jobKeywordsField,
  1329. let textView = field.window?.fieldEditor(true, for: field) as? NSTextView else { return }
  1330. textView.insertionPointColor = Theme.primaryText
  1331. }
  1332. private func resetChatState() {
  1333. trailingLoadMoreJobsRow = nil
  1334. trailingLoadMoreJobsButton = nil
  1335. chatMessages.removeAll()
  1336. lastSearchResults.removeAll()
  1337. chatStack.arrangedSubviews.forEach {
  1338. chatStack.removeArrangedSubview($0)
  1339. $0.removeFromSuperview()
  1340. }
  1341. let welcome = "Tell me what role you want and I will return job descriptions, key skills, and a quick fit summary."
  1342. chatMessages.append(ChatMessage(role: "assistant", content: welcome))
  1343. appendChatBubble(text: welcome, isUser: false)
  1344. setChatStatusLabel("Ask me to find jobs")
  1345. }
  1346. private func appendChatBubble(text: String, isUser: Bool, jobs: [JobListing]? = nil) {
  1347. let host = NSView()
  1348. host.translatesAutoresizingMaskIntoConstraints = false
  1349. if isUser {
  1350. installUserBubble(text: text, into: host)
  1351. } else {
  1352. installAssistantBubble(text: text, jobs: jobs, into: host)
  1353. }
  1354. chatStack.addArrangedSubview(host)
  1355. host.widthAnchor.constraint(equalTo: chatStack.widthAnchor).isActive = true
  1356. if prefersReducedMotion {
  1357. host.alphaValue = 1
  1358. } else {
  1359. host.alphaValue = 0
  1360. }
  1361. DispatchQueue.main.async { [weak self] in
  1362. guard let self else { return }
  1363. if self.prefersReducedMotion {
  1364. self.updateChatBubbleWidths()
  1365. self.scrollChatToBottom()
  1366. return
  1367. }
  1368. NSAnimationContext.runAnimationGroup { ctx in
  1369. ctx.duration = 0.3
  1370. ctx.timingFunction = CAMediaTimingFunction(name: .easeOut)
  1371. host.animator().alphaValue = 1
  1372. }
  1373. self.updateChatBubbleWidths()
  1374. self.scrollChatToBottom()
  1375. }
  1376. }
  1377. private func installUserBubble(text: String, into host: NSView) {
  1378. let bubble = makeChatBubbleContainer(text: text, isUser: true)
  1379. host.addSubview(bubble)
  1380. NSLayoutConstraint.activate([
  1381. bubble.topAnchor.constraint(equalTo: host.topAnchor),
  1382. bubble.bottomAnchor.constraint(equalTo: host.bottomAnchor),
  1383. bubble.trailingAnchor.constraint(equalTo: host.trailingAnchor),
  1384. bubble.leadingAnchor.constraint(greaterThanOrEqualTo: host.leadingAnchor, constant: 64),
  1385. bubble.widthAnchor.constraint(lessThanOrEqualTo: host.widthAnchor, multiplier: 0.78)
  1386. ])
  1387. }
  1388. private func installAssistantBubble(text: String, jobs: [JobListing]?, into host: NSView) {
  1389. let avatar = makeAssistantAvatarView()
  1390. let nameLabel = NSTextField(labelWithString: "AI Job Finder")
  1391. nameLabel.font = .systemFont(ofSize: 11, weight: .semibold)
  1392. nameLabel.textColor = Theme.secondaryText
  1393. nameLabel.translatesAutoresizingMaskIntoConstraints = false
  1394. let bubble = makeChatBubbleContainer(text: text, isUser: false)
  1395. let column = NSStackView(views: [nameLabel, bubble])
  1396. column.orientation = .vertical
  1397. column.spacing = 6
  1398. column.alignment = .width
  1399. column.translatesAutoresizingMaskIntoConstraints = false
  1400. if let jobs, !jobs.isEmpty {
  1401. trailingLoadMoreJobsRow?.removeFromSuperview()
  1402. trailingLoadMoreJobsRow = nil
  1403. trailingLoadMoreJobsButton = nil
  1404. let jobsStack = makeChatJobsStackView(jobs: jobs)
  1405. column.addArrangedSubview(jobsStack)
  1406. jobsStack.widthAnchor.constraint(equalTo: column.widthAnchor).isActive = true
  1407. let moreRow = makeLoadMoreJobsRowView()
  1408. column.addArrangedSubview(moreRow)
  1409. moreRow.widthAnchor.constraint(equalTo: column.widthAnchor).isActive = true
  1410. trailingLoadMoreJobsRow = moreRow
  1411. }
  1412. host.addSubview(avatar)
  1413. host.addSubview(column)
  1414. NSLayoutConstraint.activate([
  1415. avatar.leadingAnchor.constraint(equalTo: host.leadingAnchor),
  1416. avatar.topAnchor.constraint(equalTo: host.topAnchor),
  1417. avatar.widthAnchor.constraint(equalToConstant: 36),
  1418. avatar.heightAnchor.constraint(equalToConstant: 36),
  1419. column.leadingAnchor.constraint(equalTo: avatar.trailingAnchor, constant: 12),
  1420. column.trailingAnchor.constraint(equalTo: host.trailingAnchor),
  1421. column.topAnchor.constraint(equalTo: host.topAnchor),
  1422. column.bottomAnchor.constraint(equalTo: host.bottomAnchor)
  1423. ])
  1424. }
  1425. private func makeChatBubbleContainer(text: String, isUser: Bool) -> NSView {
  1426. let container = NSView()
  1427. container.translatesAutoresizingMaskIntoConstraints = false
  1428. container.wantsLayer = true
  1429. container.layer?.cornerRadius = 14
  1430. if #available(macOS 11.0, *) {
  1431. container.layer?.cornerCurve = .continuous
  1432. }
  1433. container.layer?.masksToBounds = true
  1434. if isUser {
  1435. container.layer?.backgroundColor = Theme.brandBlue.cgColor
  1436. } else {
  1437. container.layer?.backgroundColor = Theme.chromeBackground.cgColor
  1438. container.layer?.borderWidth = 1
  1439. container.layer?.borderColor = Theme.border.cgColor
  1440. }
  1441. let label = ChatBubbleLabel(wrappingLabelWithString: text)
  1442. label.font = .systemFont(ofSize: 13.5, weight: .regular)
  1443. label.textColor = isUser ? .white : Theme.primaryText
  1444. label.maximumNumberOfLines = 0
  1445. label.lineBreakMode = .byWordWrapping
  1446. label.alignment = .left
  1447. label.translatesAutoresizingMaskIntoConstraints = false
  1448. label.setContentHuggingPriority(.defaultLow, for: .horizontal)
  1449. label.setContentCompressionResistancePriority(.defaultLow, for: .horizontal)
  1450. container.addSubview(label)
  1451. NSLayoutConstraint.activate([
  1452. label.leadingAnchor.constraint(equalTo: container.leadingAnchor, constant: 14),
  1453. label.trailingAnchor.constraint(equalTo: container.trailingAnchor, constant: -14),
  1454. label.topAnchor.constraint(equalTo: container.topAnchor, constant: 10),
  1455. label.bottomAnchor.constraint(equalTo: container.bottomAnchor, constant: -10)
  1456. ])
  1457. return container
  1458. }
  1459. private func makeAssistantAvatarView() -> NSView {
  1460. let view = NSView()
  1461. view.translatesAutoresizingMaskIntoConstraints = false
  1462. view.wantsLayer = true
  1463. view.layer?.cornerRadius = 18
  1464. if #available(macOS 11.0, *) {
  1465. view.layer?.cornerCurve = .continuous
  1466. }
  1467. view.layer?.backgroundColor = Theme.settingsIconBackground.cgColor
  1468. view.layer?.borderWidth = 1
  1469. view.layer?.borderColor = Theme.proCardBorder.cgColor
  1470. let icon = NSImageView()
  1471. icon.translatesAutoresizingMaskIntoConstraints = false
  1472. icon.symbolConfiguration = NSImage.SymbolConfiguration(pointSize: 15, weight: .semibold)
  1473. icon.image = NSImage(systemSymbolName: "sparkles", accessibilityDescription: "AI Job Finder")
  1474. icon.contentTintColor = Theme.brandBlue
  1475. view.addSubview(icon)
  1476. NSLayoutConstraint.activate([
  1477. icon.centerXAnchor.constraint(equalTo: view.centerXAnchor),
  1478. icon.centerYAnchor.constraint(equalTo: view.centerYAnchor)
  1479. ])
  1480. return view
  1481. }
  1482. private func makeLoadMoreJobsRowView() -> NSView {
  1483. let row = NSView()
  1484. row.translatesAutoresizingMaskIntoConstraints = false
  1485. let button = HoverableButton()
  1486. button.pointerCursor = true
  1487. button.title = "Show more jobs"
  1488. button.font = .systemFont(ofSize: 12, weight: .semibold)
  1489. button.bezelStyle = .rounded
  1490. button.controlSize = .regular
  1491. button.contentTintColor = Theme.brandBlue
  1492. button.target = self
  1493. button.action = #selector(didTapLoadMoreJobs)
  1494. button.translatesAutoresizingMaskIntoConstraints = false
  1495. trailingLoadMoreJobsButton = button
  1496. row.addSubview(button)
  1497. NSLayoutConstraint.activate([
  1498. button.leadingAnchor.constraint(equalTo: row.leadingAnchor),
  1499. button.topAnchor.constraint(equalTo: row.topAnchor, constant: 2),
  1500. button.bottomAnchor.constraint(equalTo: row.bottomAnchor, constant: -2)
  1501. ])
  1502. return row
  1503. }
  1504. private func makeChatJobsStackView(jobs: [JobListing]) -> ChatJobsStackView {
  1505. let stack = ChatJobsStackView()
  1506. stack.orientation = .vertical
  1507. stack.spacing = 10
  1508. stack.alignment = .width
  1509. stack.distribution = .fill
  1510. stack.translatesAutoresizingMaskIntoConstraints = false
  1511. for job in jobs {
  1512. let card = makeJobListingCard(job, context: .homeSearchResults)
  1513. stack.addArrangedSubview(card)
  1514. card.widthAnchor.constraint(equalTo: stack.widthAnchor).isActive = true
  1515. }
  1516. return stack
  1517. }
  1518. private func scrollChatToBottom() {
  1519. let maxY = max(0, chatDocumentView.bounds.height - chatScrollView.contentView.bounds.height)
  1520. chatScrollView.contentView.scroll(to: NSPoint(x: 0, y: maxY))
  1521. chatScrollView.reflectScrolledClipView(chatScrollView.contentView)
  1522. }
  1523. private func setInputEnabled(_ enabled: Bool) {
  1524. jobKeywordsField.isEnabled = enabled
  1525. findJobsButton.isEnabled = enabled
  1526. findJobsButton.alphaValue = enabled ? 1 : 0.65
  1527. trailingLoadMoreJobsButton?.isEnabled = enabled
  1528. trailingLoadMoreJobsButton?.alphaValue = enabled ? 1 : 0.65
  1529. }
  1530. private func configureSidebar() {
  1531. let items = currentSidebarItems
  1532. sidebar.arrangedSubviews.forEach {
  1533. sidebar.removeArrangedSubview($0)
  1534. $0.removeFromSuperview()
  1535. }
  1536. let brand = NSTextField(labelWithString: "Indeed AI\nJob Finder")
  1537. brand.font = .systemFont(ofSize: 18, weight: .bold)
  1538. brand.textColor = Theme.brandBlue
  1539. brand.alignment = .left
  1540. brand.maximumNumberOfLines = 2
  1541. // Tight multiline height in the sidebar stack (zero width makes intrinsic height unreliable).
  1542. brand.preferredMaxLayoutWidth = 194
  1543. sidebar.addArrangedSubview(brand)
  1544. sidebar.setCustomSpacing(10, after: brand)
  1545. items.enumerated().forEach { index, item in
  1546. let isSelected = index == selectedSidebarIndex
  1547. let rowHost = SidebarNavRowView { [weak self] in
  1548. self?.selectSidebarItem(at: index)
  1549. }
  1550. rowHost.translatesAutoresizingMaskIntoConstraints = false
  1551. rowHost.wantsLayer = true
  1552. rowHost.layer?.cornerRadius = 8
  1553. rowHost.restingBackgroundColor = isSelected ? Theme.selectionFill : nil
  1554. rowHost.hoverBackgroundColor = isSelected ? Theme.selectionFillHover : Theme.sidebarRowHoverFill
  1555. rowHost.setAccessibilityLabel(item.title)
  1556. rowHost.setAccessibilityRole(.button)
  1557. rowHost.setAccessibilitySelected(isSelected)
  1558. let row = NSStackView()
  1559. row.orientation = .horizontal
  1560. row.spacing = 8
  1561. row.alignment = .centerY
  1562. row.translatesAutoresizingMaskIntoConstraints = false
  1563. let icon = NSImageView()
  1564. icon.symbolConfiguration = NSImage.SymbolConfiguration(pointSize: 13, weight: .medium)
  1565. icon.image = NSImage(systemSymbolName: item.systemImage, accessibilityDescription: item.title)
  1566. icon.contentTintColor = isSelected ? Theme.brandBlue : Theme.secondaryText
  1567. let text = NSTextField(labelWithString: item.title)
  1568. text.font = .systemFont(ofSize: 14, weight: .medium)
  1569. text.textColor = isSelected ? Theme.brandBlue : Theme.secondaryText
  1570. text.refusesFirstResponder = true
  1571. row.addArrangedSubview(icon)
  1572. row.addArrangedSubview(text)
  1573. if let badge = item.badge {
  1574. let badgeField = NSTextField(labelWithString: badge)
  1575. badgeField.font = .systemFont(ofSize: 11, weight: .semibold)
  1576. badgeField.textColor = Theme.primaryText
  1577. badgeField.wantsLayer = true
  1578. badgeField.layer?.backgroundColor = Theme.toggleBackground.cgColor
  1579. badgeField.layer?.cornerRadius = 8
  1580. badgeField.alignment = .center
  1581. badgeField.maximumNumberOfLines = 1
  1582. badgeField.lineBreakMode = .byClipping
  1583. badgeField.refusesFirstResponder = true
  1584. badgeField.translatesAutoresizingMaskIntoConstraints = false
  1585. badgeField.widthAnchor.constraint(equalToConstant: 42).isActive = true
  1586. row.addArrangedSubview(NSView())
  1587. row.addArrangedSubview(badgeField)
  1588. }
  1589. rowHost.addSubview(row)
  1590. NSLayoutConstraint.activate([
  1591. row.leadingAnchor.constraint(equalTo: rowHost.leadingAnchor, constant: 10),
  1592. row.trailingAnchor.constraint(equalTo: rowHost.trailingAnchor, constant: -10),
  1593. row.topAnchor.constraint(equalTo: rowHost.topAnchor, constant: 8),
  1594. row.bottomAnchor.constraint(equalTo: rowHost.bottomAnchor, constant: -8)
  1595. ])
  1596. rowHost.setContentHuggingPriority(.defaultLow, for: .horizontal)
  1597. sidebar.addArrangedSubview(rowHost)
  1598. let sidebarHorizontalInset = sidebar.edgeInsets.left + sidebar.edgeInsets.right
  1599. rowHost.widthAnchor.constraint(equalTo: sidebar.widthAnchor, constant: -sidebarHorizontalInset).isActive = true
  1600. }
  1601. let sidebarBottomSpacer = NSView()
  1602. sidebarBottomSpacer.translatesAutoresizingMaskIntoConstraints = false
  1603. sidebarBottomSpacer.setContentHuggingPriority(.defaultLow, for: .vertical)
  1604. sidebarBottomSpacer.setContentCompressionResistancePriority(.defaultLow, for: .vertical)
  1605. sidebar.addArrangedSubview(sidebarBottomSpacer)
  1606. let upgradeCard = NSView()
  1607. upgradeCard.translatesAutoresizingMaskIntoConstraints = false
  1608. upgradeCard.wantsLayer = true
  1609. upgradeCard.layer?.backgroundColor = Theme.proCardFill.cgColor
  1610. upgradeCard.layer?.cornerRadius = 14
  1611. upgradeCard.layer?.borderWidth = 1
  1612. upgradeCard.layer?.borderColor = Theme.proCardBorder.cgColor
  1613. upgradeCard.layer?.masksToBounds = true
  1614. let accentBar = NSView()
  1615. accentBar.translatesAutoresizingMaskIntoConstraints = false
  1616. accentBar.wantsLayer = true
  1617. accentBar.layer?.backgroundColor = Theme.proAccent.cgColor
  1618. let inner = NSStackView()
  1619. inner.translatesAutoresizingMaskIntoConstraints = false
  1620. inner.orientation = .vertical
  1621. inner.spacing = 10
  1622. inner.alignment = .centerX
  1623. let proIcon = NSImageView()
  1624. proIcon.translatesAutoresizingMaskIntoConstraints = false
  1625. proIcon.symbolConfiguration = NSImage.SymbolConfiguration(pointSize: 13, weight: .semibold)
  1626. proIcon.image = NSImage(systemSymbolName: "sparkles", accessibilityDescription: nil)
  1627. proIcon.contentTintColor = Theme.proAccent
  1628. let proEyebrow = NSTextField(labelWithString: "Premium")
  1629. proEyebrow.font = .systemFont(ofSize: 11, weight: .heavy)
  1630. proEyebrow.textColor = Theme.proAccent
  1631. proEyebrow.alignment = .center
  1632. let eyebrowRow = NSStackView(views: [proIcon, proEyebrow])
  1633. eyebrowRow.orientation = .horizontal
  1634. eyebrowRow.spacing = 6
  1635. eyebrowRow.alignment = .centerY
  1636. let headline = NSTextField(labelWithString: "Upgrade to Pro")
  1637. headline.font = .systemFont(ofSize: 16, weight: .bold)
  1638. headline.textColor = Theme.primaryText
  1639. headline.alignment = .center
  1640. let upgradeDescription = NSTextField(wrappingLabelWithString: "Unlimited AI matches, smart alerts, and interview prep—all in one place.")
  1641. upgradeDescription.font = .systemFont(ofSize: 12, weight: .regular)
  1642. upgradeDescription.textColor = Theme.secondaryText
  1643. upgradeDescription.alignment = .center
  1644. // Sidebar content width is the fixed sidebar width minus horizontal edge insets; card must stay within that band.
  1645. let cardWidth: CGFloat = 186
  1646. let innerContentWidth = cardWidth - 28
  1647. upgradeDescription.preferredMaxLayoutWidth = innerContentWidth
  1648. let upgradeButton = HoverableButton(title: "Upgrade to Pro", target: self, action: #selector(didTapUpgradeToPro))
  1649. upgradeButton.isBordered = false
  1650. upgradeButton.bezelStyle = .rounded
  1651. upgradeButton.font = .systemFont(ofSize: 13, weight: .bold)
  1652. upgradeButton.contentTintColor = Theme.proCTAText
  1653. upgradeButton.alignment = .center
  1654. upgradeButton.wantsLayer = true
  1655. upgradeButton.layer?.backgroundColor = Theme.proCTABackground.cgColor
  1656. upgradeButton.layer?.cornerRadius = 20
  1657. upgradeButton.translatesAutoresizingMaskIntoConstraints = false
  1658. upgradeButton.heightAnchor.constraint(equalToConstant: 40).isActive = true
  1659. upgradeButton.pointerCursor = true
  1660. upgradeButton.hoverHandler = { [weak upgradeButton] hovering in
  1661. upgradeButton?.layer?.backgroundColor = (hovering ? Theme.brandBlueHover : Theme.proCTABackground).cgColor
  1662. }
  1663. inner.addArrangedSubview(eyebrowRow)
  1664. inner.addArrangedSubview(headline)
  1665. inner.addArrangedSubview(upgradeDescription)
  1666. inner.addArrangedSubview(upgradeButton)
  1667. upgradeCard.addSubview(accentBar)
  1668. upgradeCard.addSubview(inner)
  1669. NSLayoutConstraint.activate([
  1670. upgradeCard.widthAnchor.constraint(equalToConstant: cardWidth),
  1671. accentBar.topAnchor.constraint(equalTo: upgradeCard.topAnchor),
  1672. accentBar.leadingAnchor.constraint(equalTo: upgradeCard.leadingAnchor),
  1673. accentBar.trailingAnchor.constraint(equalTo: upgradeCard.trailingAnchor),
  1674. accentBar.heightAnchor.constraint(equalToConstant: 2),
  1675. inner.leadingAnchor.constraint(equalTo: upgradeCard.leadingAnchor, constant: 14),
  1676. inner.trailingAnchor.constraint(equalTo: upgradeCard.trailingAnchor, constant: -14),
  1677. inner.topAnchor.constraint(equalTo: accentBar.bottomAnchor, constant: 12),
  1678. inner.bottomAnchor.constraint(equalTo: upgradeCard.bottomAnchor, constant: -14),
  1679. upgradeButton.widthAnchor.constraint(equalTo: inner.widthAnchor)
  1680. ])
  1681. sidebar.addArrangedSubview(upgradeCard)
  1682. }
  1683. @objc private func didTapUpgradeToPro() {
  1684. guard let url = URL(string: "https://www.indeed.com") else { return }
  1685. NSWorkspace.shared.open(url)
  1686. }
  1687. @objc private func didChangeThemeSelection(_ sender: NSSegmentedControl) {
  1688. switch sender.selectedSegment {
  1689. case 1:
  1690. NSApp.appearance = NSAppearance(named: .aqua)
  1691. case 2:
  1692. NSApp.appearance = NSAppearance(named: .darkAqua)
  1693. default:
  1694. NSApp.appearance = nil
  1695. }
  1696. }
  1697. private func selectSidebarItem(at index: Int) {
  1698. guard index >= 0, index < currentSidebarItems.count else { return }
  1699. let selectingHome = isHomeSidebarIndex(index)
  1700. if index == selectedSidebarIndex {
  1701. if selectingHome {
  1702. applyHomeState()
  1703. }
  1704. return
  1705. }
  1706. selectedSidebarIndex = index
  1707. configureSidebar()
  1708. updateMainContentVisibility()
  1709. if selectingHome {
  1710. applyHomeState()
  1711. }
  1712. }
  1713. }
  1714. private struct ChatMessage: Codable {
  1715. let role: String
  1716. let content: String
  1717. }
  1718. private final class OpenAIJobSearchService {
  1719. private let endpoint = URL(string: "https://api.openai.com/v1/responses")!
  1720. private let session = URLSession(configuration: .ephemeral)
  1721. func searchJobs(query: String, conversation: [ChatMessage], maxJobs: Int, completion: @escaping (Result<JobSearchOutput, Error>) -> Void) {
  1722. let jobLimit = max(1, min(maxJobs, 25))
  1723. let apiKey = OpenAIConfiguration.apiKey
  1724. guard OpenAIConfiguration.hasAPIKey else {
  1725. completion(.failure(NSError(
  1726. domain: "OpenAIJobSearchService",
  1727. code: 1,
  1728. userInfo: [NSLocalizedDescriptionKey: "Missing API key. Set OPENAI_API_KEY in Xcode Build Settings."]
  1729. )))
  1730. return
  1731. }
  1732. var request = URLRequest(url: endpoint)
  1733. request.httpMethod = "POST"
  1734. request.setValue("application/json", forHTTPHeaderField: "Content-Type")
  1735. request.setValue("Bearer \(apiKey)", forHTTPHeaderField: "Authorization")
  1736. request.timeoutInterval = 45
  1737. let recentContext = conversation.suffix(8)
  1738. .map { "\($0.role.uppercased()): \($0.content)" }
  1739. .joined(separator: "\n")
  1740. let contextBlock: String
  1741. if recentContext.isEmpty {
  1742. contextBlock = "No prior conversation context."
  1743. } else {
  1744. contextBlock = recentContext
  1745. }
  1746. let instructions = """
  1747. 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).
  1748. Conversation context:
  1749. \(contextBlock)
  1750. Latest user query: "\(query)"
  1751. Use web search to find currently posted jobs that satisfy this query. 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.
  1752. CRITICAL OUTPUT RULES:
  1753. - Reply with NOTHING except one JSON object. No markdown, no code fences, no prose before or after, no labels like "Here is the JSON".
  1754. - The JSON must match exactly this shape (lowercase key "jobs"):
  1755. {"jobs":[{"title":"...","description":"...","url":"https://..."}]}
  1756. - If you find no suitable listings, return {"jobs":[]} — still valid JSON only.
  1757. Return up to \(jobLimit) jobs in the jobs array (fewer only if the web has no additional distinct matches). Do not repeat titles or URLs already implied in the conversation context above.
  1758. Keep each description to one sentence; prefer real listing URLs.
  1759. """
  1760. let payload = OpenAIResponsesRequest(
  1761. model: "gpt-4o-mini",
  1762. input: instructions,
  1763. tools: [OpenAIResponsesTool(type: "web_search_preview")]
  1764. )
  1765. do {
  1766. request.httpBody = try JSONEncoder().encode(payload)
  1767. } catch {
  1768. completion(.failure(error))
  1769. return
  1770. }
  1771. session.dataTask(with: request) { data, response, error in
  1772. if let error {
  1773. completion(.failure(error))
  1774. return
  1775. }
  1776. guard let data else {
  1777. completion(.failure(NSError(
  1778. domain: "OpenAIJobSearchService",
  1779. code: 2,
  1780. userInfo: [NSLocalizedDescriptionKey: "API returned an empty response."]
  1781. )))
  1782. return
  1783. }
  1784. if let http = response as? HTTPURLResponse, !(200...299).contains(http.statusCode) {
  1785. if let apiError = try? JSONDecoder().decode(OpenAIAPIErrorResponse.self, from: data) {
  1786. completion(.failure(NSError(
  1787. domain: "OpenAIJobSearchService",
  1788. code: http.statusCode,
  1789. userInfo: [NSLocalizedDescriptionKey: apiError.error.message]
  1790. )))
  1791. } else {
  1792. completion(.failure(NSError(
  1793. domain: "OpenAIJobSearchService",
  1794. code: http.statusCode,
  1795. userInfo: [NSLocalizedDescriptionKey: "Job search request failed with status \(http.statusCode)."]
  1796. )))
  1797. }
  1798. return
  1799. }
  1800. do {
  1801. let modelText = try Self.extractModelTextFromResponsesBody(data)
  1802. let trimmed = modelText.trimmingCharacters(in: .whitespacesAndNewlines)
  1803. guard !trimmed.isEmpty else {
  1804. throw NSError(
  1805. domain: "OpenAIJobSearchService",
  1806. code: 4,
  1807. userInfo: [NSLocalizedDescriptionKey: "The API returned an empty text payload."]
  1808. )
  1809. }
  1810. let jobs = try Self.parseJobListings(fromModelText: trimmed)
  1811. completion(.success(JobSearchOutput(jobs: jobs)))
  1812. } catch {
  1813. completion(.failure(error))
  1814. }
  1815. }.resume()
  1816. }
  1817. /// 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).
  1818. private static func extractModelTextFromResponsesBody(_ data: Data) throws -> String {
  1819. let rootObject: Any
  1820. do {
  1821. rootObject = try JSONSerialization.jsonObject(with: data, options: [])
  1822. } catch {
  1823. throw NSError(
  1824. domain: "OpenAIJobSearchService",
  1825. code: 5,
  1826. userInfo: [NSLocalizedDescriptionKey: "The job search service returned data that was not valid JSON."]
  1827. )
  1828. }
  1829. guard let root = rootObject as? [String: Any] else {
  1830. throw NSError(
  1831. domain: "OpenAIJobSearchService",
  1832. code: 5,
  1833. userInfo: [NSLocalizedDescriptionKey: "Unexpected response shape from the job search service."]
  1834. )
  1835. }
  1836. if let status = root["status"] as? String {
  1837. if status == "failed" {
  1838. let message = (root["error"] as? [String: Any])?["message"] as? String ?? "The search request failed."
  1839. throw NSError(domain: "OpenAIJobSearchService", code: 7, userInfo: [NSLocalizedDescriptionKey: message])
  1840. }
  1841. if status == "incomplete",
  1842. let details = root["incomplete_details"] as? [String: Any],
  1843. let reason = details["reason"] as? String {
  1844. throw NSError(
  1845. domain: "OpenAIJobSearchService",
  1846. code: 8,
  1847. userInfo: [NSLocalizedDescriptionKey: "Search stopped early (\(reason)). Try a simpler query or try again."]
  1848. )
  1849. }
  1850. }
  1851. if let direct = root["output_text"] as? String {
  1852. let trimmed = direct.trimmingCharacters(in: .whitespacesAndNewlines)
  1853. if !trimmed.isEmpty { return trimmed }
  1854. }
  1855. guard let output = root["output"] as? [Any] else {
  1856. throw NSError(
  1857. domain: "OpenAIJobSearchService",
  1858. code: 9,
  1859. userInfo: [NSLocalizedDescriptionKey: "The search service returned no assistant text. Try again in a moment."]
  1860. )
  1861. }
  1862. var segments: [String] = []
  1863. for case let item as [String: Any] in output where (item["type"] as? String) == "message" {
  1864. collectOutputTextSegments(fromOutputItem: item, into: &segments)
  1865. }
  1866. if segments.isEmpty {
  1867. for case let item as [String: Any] in output {
  1868. collectOutputTextSegments(fromOutputItem: item, into: &segments)
  1869. }
  1870. }
  1871. let combined = segments.joined(separator: "\n").trimmingCharacters(in: .whitespacesAndNewlines)
  1872. if !combined.isEmpty {
  1873. return combined
  1874. }
  1875. throw NSError(
  1876. domain: "OpenAIJobSearchService",
  1877. code: 9,
  1878. userInfo: [NSLocalizedDescriptionKey: "The model did not return readable job-search text. Try again."]
  1879. )
  1880. }
  1881. private static func collectOutputTextSegments(fromOutputItem item: [String: Any], into segments: inout [String]) {
  1882. guard let content = item["content"] as? [Any] else { return }
  1883. for case let part as [String: Any] in content {
  1884. guard (part["type"] as? String) == "output_text" else { continue }
  1885. if let s = part["text"] as? String {
  1886. segments.append(s)
  1887. } else if let nested = part["text"] as? [String: Any], let value = nested["value"] as? String {
  1888. segments.append(value)
  1889. }
  1890. }
  1891. }
  1892. private static func parseJobListings(fromModelText text: String) throws -> [JobListing] {
  1893. let jsonString = extractJobJSONObjectString(from: text) ?? extractJSONObject(from: text)
  1894. let jsonData = Data(jsonString.utf8)
  1895. if let payload = try? JSONDecoder().decode(JobSearchResultsPayload.self, from: jsonData) {
  1896. return payload.jobs
  1897. }
  1898. if let listings = try? JSONDecoder().decode([JobListing].self, from: jsonData) {
  1899. return listings
  1900. }
  1901. if let obj = try? JSONSerialization.jsonObject(with: jsonData, options: []) {
  1902. if let dict = obj as? [String: Any], let jobs = jobListings(fromFlexibleJSONObject: dict) {
  1903. return jobs
  1904. }
  1905. if let arr = obj as? [[String: Any]], let jobs = jobListings(fromFlexibleJobArray: arr) {
  1906. return jobs
  1907. }
  1908. }
  1909. throw NSError(
  1910. domain: "OpenAIJobSearchService",
  1911. code: 10,
  1912. userInfo: [NSLocalizedDescriptionKey: "The assistant reply did not include job listings in the expected JSON format. Try your search again."]
  1913. )
  1914. }
  1915. private static func jobListings(fromFlexibleJSONObject dict: [String: Any]) -> [JobListing]? {
  1916. for (key, value) in dict {
  1917. guard key.caseInsensitiveCompare("jobs") == .orderedSame, let arr = value as? [[String: Any]] else { continue }
  1918. if let jobs = jobListings(fromFlexibleJobArray: arr) { return jobs }
  1919. }
  1920. for wrapKey in ["data", "result", "results", "payload"] {
  1921. if let inner = dict[wrapKey] as? [String: Any], let nested = jobListings(fromFlexibleJSONObject: inner) {
  1922. return nested
  1923. }
  1924. }
  1925. return nil
  1926. }
  1927. private static func jobListings(fromFlexibleJobArray jobs: [[String: Any]]) -> [JobListing]? {
  1928. var out: [JobListing] = []
  1929. for item in jobs {
  1930. guard let title = firstString(valuesForKeys: ["title", "job_title", "name", "position"], in: item)?.trimmingCharacters(in: .whitespacesAndNewlines),
  1931. !title.isEmpty,
  1932. let desc = firstString(valuesForKeys: ["description", "snippet", "summary", "desc"], in: item)?.trimmingCharacters(in: .whitespacesAndNewlines),
  1933. !desc.isEmpty else { continue }
  1934. let urlRaw = firstString(valuesForKeys: ["url", "link", "apply_url", "job_url"], in: item)?.trimmingCharacters(in: .whitespacesAndNewlines)
  1935. let url: String? = (urlRaw?.isEmpty == true) ? nil : urlRaw
  1936. out.append(JobListing(title: title, description: desc, url: url))
  1937. }
  1938. return out.isEmpty ? nil : out
  1939. }
  1940. private static func firstString(valuesForKeys keys: [String], in dict: [String: Any]) -> String? {
  1941. for wanted in keys {
  1942. for (dk, dv) in dict {
  1943. guard dk.caseInsensitiveCompare(wanted) == .orderedSame, let s = dv as? String else { continue }
  1944. return s
  1945. }
  1946. }
  1947. return nil
  1948. }
  1949. private static func stripMarkdownCodeFence(_ text: String) -> String {
  1950. var s = text.trimmingCharacters(in: .whitespacesAndNewlines)
  1951. guard s.hasPrefix("```") else { return s }
  1952. s.removeFirst(3)
  1953. if s.lowercased().hasPrefix("json") {
  1954. s.removeFirst(4)
  1955. }
  1956. s = s.trimmingCharacters(in: .whitespacesAndNewlines)
  1957. if let fence = s.range(of: "```", options: .backwards) {
  1958. s = String(s[..<fence.lowerBound])
  1959. }
  1960. return s.trimmingCharacters(in: .whitespacesAndNewlines)
  1961. }
  1962. private static func balancedJSONObject(from openBrace: String.Index, in s: String) -> String? {
  1963. var depth = 0
  1964. var inString = false
  1965. var escaped = false
  1966. var i = openBrace
  1967. while i < s.endIndex {
  1968. let ch = s[i]
  1969. if inString {
  1970. if escaped {
  1971. escaped = false
  1972. } else if ch == "\\" {
  1973. escaped = true
  1974. } else if ch == "\"" {
  1975. inString = false
  1976. }
  1977. } else {
  1978. switch ch {
  1979. case "\"":
  1980. inString = true
  1981. case "{":
  1982. depth += 1
  1983. case "}":
  1984. depth -= 1
  1985. if depth == 0 {
  1986. return String(s[openBrace...i])
  1987. }
  1988. default:
  1989. break
  1990. }
  1991. }
  1992. i = s.index(after: i)
  1993. }
  1994. return nil
  1995. }
  1996. /// Prefers the JSON object that contains a `"jobs"` key so prose before/after the payload does not confuse the decoder.
  1997. private static func extractJobJSONObjectString(from text: String) -> String? {
  1998. let s = stripMarkdownCodeFence(text)
  1999. guard let jobsRange = s.range(of: "\"jobs\"", options: .caseInsensitive) else { return nil }
  2000. let head = s[..<jobsRange.lowerBound]
  2001. guard let open = head.lastIndex(of: "{") else { return nil }
  2002. return balancedJSONObject(from: open, in: s)
  2003. }
  2004. private static func extractJSONObject(from text: String) -> String {
  2005. if let extracted = extractJobJSONObjectString(from: text) {
  2006. return extracted
  2007. }
  2008. let stripped = stripMarkdownCodeFence(text)
  2009. if let first = stripped.firstIndex(of: "{"), let balanced = balancedJSONObject(from: first, in: stripped) {
  2010. return balanced
  2011. }
  2012. if let range = text.range(of: "\\{[\\s\\S]*\\}", options: .regularExpression) {
  2013. return String(text[range])
  2014. }
  2015. return text
  2016. }
  2017. }
  2018. private struct OpenAIResponsesRequest: Codable {
  2019. let model: String
  2020. let input: String
  2021. let tools: [OpenAIResponsesTool]
  2022. }
  2023. private struct OpenAIResponsesTool: Codable {
  2024. let type: String
  2025. }
  2026. private struct JobSearchResultsPayload: Codable {
  2027. let jobs: [JobListing]
  2028. }
  2029. private struct JobSearchOutput {
  2030. let jobs: [JobListing]
  2031. }
  2032. private struct OpenAIAPIErrorResponse: Codable {
  2033. let error: APIError
  2034. struct APIError: Codable {
  2035. let message: String
  2036. }
  2037. }
  2038. /// `NSButton` that carries a `JobListing` for card actions (`representedObject` is unavailable on `NSButton` in this target).
  2039. private final class JobPayloadButton: HoverableButton {
  2040. var jobPayload: JobListing?
  2041. var cardContext: JobListingCardContext = .homeSearchResults
  2042. }
  2043. /// `NSButton` with a tracking area that reports hover transitions and (optionally) swaps in a pointing-hand cursor while hovered.
  2044. private class HoverableButton: NSButton {
  2045. var hoverHandler: ((Bool) -> Void)?
  2046. var pointerCursor: Bool = false
  2047. private(set) var isHovering: Bool = false
  2048. private var trackingArea: NSTrackingArea?
  2049. private var didPushCursor: Bool = false
  2050. override func updateTrackingAreas() {
  2051. super.updateTrackingAreas()
  2052. if let area = trackingArea { removeTrackingArea(area) }
  2053. let area = NSTrackingArea(
  2054. rect: bounds,
  2055. options: [.activeInKeyWindow, .mouseEnteredAndExited, .inVisibleRect],
  2056. owner: self,
  2057. userInfo: nil
  2058. )
  2059. addTrackingArea(area)
  2060. trackingArea = area
  2061. }
  2062. override func mouseEntered(with event: NSEvent) {
  2063. super.mouseEntered(with: event)
  2064. isHovering = true
  2065. hoverHandler?(true)
  2066. if pointerCursor, !didPushCursor {
  2067. NSCursor.pointingHand.push()
  2068. didPushCursor = true
  2069. }
  2070. }
  2071. override func mouseExited(with event: NSEvent) {
  2072. super.mouseExited(with: event)
  2073. isHovering = false
  2074. hoverHandler?(false)
  2075. if didPushCursor {
  2076. NSCursor.pop()
  2077. didPushCursor = false
  2078. }
  2079. }
  2080. override func viewWillMove(toWindow newWindow: NSWindow?) {
  2081. super.viewWillMove(toWindow: newWindow)
  2082. // Guard against an unbalanced cursor stack if the button is removed mid-hover (e.g. job card replaced after a search).
  2083. if newWindow == nil, didPushCursor {
  2084. NSCursor.pop()
  2085. didPushCursor = false
  2086. isHovering = false
  2087. }
  2088. }
  2089. }
  2090. /// `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.
  2091. private class HoverableView: NSView {
  2092. var hoverHandler: ((Bool) -> Void)?
  2093. var pointerCursor: Bool = false
  2094. private(set) var isHovering: Bool = false
  2095. private var trackingArea: NSTrackingArea?
  2096. private var didPushCursor: Bool = false
  2097. override func updateTrackingAreas() {
  2098. super.updateTrackingAreas()
  2099. if let area = trackingArea { removeTrackingArea(area) }
  2100. let area = NSTrackingArea(
  2101. rect: bounds,
  2102. options: [.activeInKeyWindow, .mouseEnteredAndExited, .inVisibleRect],
  2103. owner: self,
  2104. userInfo: nil
  2105. )
  2106. addTrackingArea(area)
  2107. trackingArea = area
  2108. }
  2109. override func mouseEntered(with event: NSEvent) {
  2110. super.mouseEntered(with: event)
  2111. isHovering = true
  2112. hoverHandler?(true)
  2113. if pointerCursor, !didPushCursor {
  2114. NSCursor.pointingHand.push()
  2115. didPushCursor = true
  2116. }
  2117. }
  2118. override func mouseExited(with event: NSEvent) {
  2119. super.mouseExited(with: event)
  2120. isHovering = false
  2121. hoverHandler?(false)
  2122. if didPushCursor {
  2123. NSCursor.pop()
  2124. didPushCursor = false
  2125. }
  2126. }
  2127. override func viewWillMove(toWindow newWindow: NSWindow?) {
  2128. super.viewWillMove(toWindow: newWindow)
  2129. if newWindow == nil, didPushCursor {
  2130. NSCursor.pop()
  2131. didPushCursor = false
  2132. isHovering = false
  2133. }
  2134. }
  2135. }
  2136. /// 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).
  2137. private final class JobListingsDocumentView: NSView {
  2138. override var isFlipped: Bool { true }
  2139. }
  2140. /// 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.
  2141. private final class ChatJobsStackView: NSStackView {}
  2142. /// 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.
  2143. private final class ChatBubbleLabel: NSTextField {}
  2144. /// 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.
  2145. private final class SidebarNavRowView: NSView {
  2146. private let onSelect: () -> Void
  2147. var restingBackgroundColor: NSColor? {
  2148. didSet { applyBackground() }
  2149. }
  2150. var hoverBackgroundColor: NSColor?
  2151. private var isHovering: Bool = false
  2152. private var didPushCursor: Bool = false
  2153. init(onSelect: @escaping () -> Void) {
  2154. self.onSelect = onSelect
  2155. super.init(frame: .zero)
  2156. }
  2157. @available(*, unavailable)
  2158. required init?(coder: NSCoder) {
  2159. fatalError("init(coder:) has not been implemented")
  2160. }
  2161. override func hitTest(_ point: NSPoint) -> NSView? {
  2162. guard let superview else { return super.hitTest(point) }
  2163. let local = convert(point, from: superview)
  2164. return bounds.contains(local) ? self : nil
  2165. }
  2166. override func mouseDown(with event: NSEvent) {
  2167. onSelect()
  2168. }
  2169. override func updateTrackingAreas() {
  2170. super.updateTrackingAreas()
  2171. trackingAreas.forEach { removeTrackingArea($0) }
  2172. addTrackingArea(NSTrackingArea(
  2173. rect: bounds,
  2174. options: [.activeInKeyWindow, .mouseEnteredAndExited, .inVisibleRect],
  2175. owner: self,
  2176. userInfo: nil
  2177. ))
  2178. }
  2179. override func mouseEntered(with event: NSEvent) {
  2180. super.mouseEntered(with: event)
  2181. isHovering = true
  2182. applyBackground()
  2183. if !didPushCursor {
  2184. NSCursor.pointingHand.push()
  2185. didPushCursor = true
  2186. }
  2187. }
  2188. override func mouseExited(with event: NSEvent) {
  2189. super.mouseExited(with: event)
  2190. isHovering = false
  2191. applyBackground()
  2192. if didPushCursor {
  2193. NSCursor.pop()
  2194. didPushCursor = false
  2195. }
  2196. }
  2197. override func viewWillMove(toWindow newWindow: NSWindow?) {
  2198. super.viewWillMove(toWindow: newWindow)
  2199. if newWindow == nil, didPushCursor {
  2200. NSCursor.pop()
  2201. didPushCursor = false
  2202. isHovering = false
  2203. }
  2204. }
  2205. private func applyBackground() {
  2206. let color = isHovering ? (hoverBackgroundColor ?? restingBackgroundColor) : restingBackgroundColor
  2207. layer?.backgroundColor = color?.cgColor
  2208. }
  2209. }