Ingen beskrivning

DashboardView.swift 105KB

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