|
| 1 | +import AppKit |
| 2 | +import SwiftUI |
| 3 | + |
| 4 | +/// Presentation for one banner phase, derived separately from the view so the |
| 5 | +/// copy and available actions stay unit-testable. |
| 6 | +struct BrowserProfileImportBannerContent: Equatable { |
| 7 | + enum Badge: Equatable { |
| 8 | + case globe |
| 9 | + case progress |
| 10 | + case success |
| 11 | + case failure |
| 12 | + } |
| 13 | + |
| 14 | + enum Action: Equatable { |
| 15 | + case importProfiles([BrowserSystemProfile]) |
| 16 | + case retry |
| 17 | + case none |
| 18 | + } |
| 19 | + |
| 20 | + let title: String |
| 21 | + let subtitle: String |
| 22 | + let badge: Badge |
| 23 | + let action: Action |
| 24 | + |
| 25 | + static func content(for phase: BrowserProfileImportModel.Phase) -> BrowserProfileImportBannerContent? { |
| 26 | + switch phase { |
| 27 | + case .hidden: |
| 28 | + return nil |
| 29 | + case let .offering(status): |
| 30 | + let profiles = status.importableProfiles |
| 31 | + let browsers = Self.browserList(for: profiles) |
| 32 | + return BrowserProfileImportBannerContent( |
| 33 | + title: String(localized: "Use your browser logins"), |
| 34 | + subtitle: String(localized: """ |
| 35 | + Copy cookies from \(browsers) into an isolated agent profile. \ |
| 36 | + Passwords are never touched. |
| 37 | + """), |
| 38 | + badge: .globe, |
| 39 | + action: .importProfiles(profiles)) |
| 40 | + case let .importing(profile, target): |
| 41 | + return BrowserProfileImportBannerContent( |
| 42 | + title: String(localized: "Importing browser cookies…"), |
| 43 | + subtitle: String(localized: """ |
| 44 | + Copying \(profile.displayName) into “\(target)”. Touch ID may be required. |
| 45 | + """), |
| 46 | + badge: .progress, |
| 47 | + action: .none) |
| 48 | + case let .imported(result): |
| 49 | + return BrowserProfileImportBannerContent( |
| 50 | + title: String(localized: "Browser logins imported"), |
| 51 | + subtitle: String(localized: """ |
| 52 | + \(result.cookies.imported) of \(result.cookies.total) cookies copied into \ |
| 53 | + “\(result.into)” — now the default profile for agent browsing. |
| 54 | + """), |
| 55 | + badge: .success, |
| 56 | + action: .none) |
| 57 | + case let .failed(message, _): |
| 58 | + return BrowserProfileImportBannerContent( |
| 59 | + title: String(localized: "Browser import failed"), |
| 60 | + subtitle: message, |
| 61 | + badge: .failure, |
| 62 | + action: .retry) |
| 63 | + } |
| 64 | + } |
| 65 | + |
| 66 | + static func browserList(for profiles: [BrowserSystemProfile]) -> String { |
| 67 | + var names: [String] = [] |
| 68 | + for profile in profiles { |
| 69 | + let name = profile.browserDisplayName |
| 70 | + if !names.contains(name) { |
| 71 | + names.append(name) |
| 72 | + } |
| 73 | + } |
| 74 | + switch names.count { |
| 75 | + case 0: |
| 76 | + return String(localized: "your browser") |
| 77 | + case 1: |
| 78 | + return names[0] |
| 79 | + default: |
| 80 | + return ListFormatter.localizedString(byJoining: names) |
| 81 | + } |
| 82 | + } |
| 83 | +} |
| 84 | + |
| 85 | +/// Floating, dismissible import offer shown over the dashboard — the inline |
| 86 | +/// replacement for the old modal browser-login alert. |
| 87 | +struct BrowserProfileImportBannerView: View { |
| 88 | + let model: BrowserProfileImportModel |
| 89 | + |
| 90 | + var body: some View { |
| 91 | + if let content = BrowserProfileImportBannerContent.content(for: self.model.phase) { |
| 92 | + self.card(content) |
| 93 | + .transition(.move(edge: .top).combined(with: .opacity)) |
| 94 | + } |
| 95 | + } |
| 96 | + |
| 97 | + private func card(_ content: BrowserProfileImportBannerContent) -> some View { |
| 98 | + HStack(alignment: .center, spacing: 12) { |
| 99 | + BannerBadgeIcon(badge: content.badge) |
| 100 | + VStack(alignment: .leading, spacing: 2) { |
| 101 | + Text(content.title) |
| 102 | + .font(.system(size: 13, weight: .semibold)) |
| 103 | + Text(content.subtitle) |
| 104 | + .font(.system(size: 11)) |
| 105 | + .foregroundStyle(.secondary) |
| 106 | + .fixedSize(horizontal: false, vertical: true) |
| 107 | + } |
| 108 | + Spacer(minLength: 12) |
| 109 | + self.trailingControls(for: content) |
| 110 | + self.dismissButton(for: content) |
| 111 | + } |
| 112 | + .padding(.vertical, 10) |
| 113 | + .padding(.leading, 12) |
| 114 | + .padding(.trailing, 10) |
| 115 | + .frame(maxWidth: .infinity, alignment: .leading) |
| 116 | + .background(.regularMaterial, in: RoundedRectangle(cornerRadius: 12, style: .continuous)) |
| 117 | + .overlay( |
| 118 | + RoundedRectangle(cornerRadius: 12, style: .continuous) |
| 119 | + .strokeBorder(Color(nsColor: .separatorColor), lineWidth: 1)) |
| 120 | + .shadow(color: .black.opacity(0.18), radius: 14, y: 6) |
| 121 | + .accessibilityElement(children: .contain) |
| 122 | + .accessibilityLabel("\(content.title). \(content.subtitle)") |
| 123 | + } |
| 124 | + |
| 125 | + @ViewBuilder |
| 126 | + private func trailingControls(for content: BrowserProfileImportBannerContent) -> some View { |
| 127 | + switch content.action { |
| 128 | + case let .importProfiles(profiles): |
| 129 | + if profiles.count == 1, let profile = profiles.first { |
| 130 | + Button("Import") { |
| 131 | + Task { await self.model.importProfile(profile) } |
| 132 | + } |
| 133 | + .buttonStyle(.borderedProminent) |
| 134 | + .controlSize(.small) |
| 135 | + } else { |
| 136 | + Menu("Import") { |
| 137 | + ForEach(profiles, id: \.menuID) { profile in |
| 138 | + Button(profile.displayName) { |
| 139 | + Task { await self.model.importProfile(profile) } |
| 140 | + } |
| 141 | + } |
| 142 | + } |
| 143 | + .menuStyle(.button) |
| 144 | + .buttonStyle(.borderedProminent) |
| 145 | + .controlSize(.small) |
| 146 | + .fixedSize() |
| 147 | + } |
| 148 | + case .retry: |
| 149 | + Button("Retry") { |
| 150 | + self.model.retry() |
| 151 | + } |
| 152 | + .buttonStyle(.bordered) |
| 153 | + .controlSize(.small) |
| 154 | + case .none: |
| 155 | + if content.badge == .progress { |
| 156 | + ProgressView() |
| 157 | + .controlSize(.small) |
| 158 | + } |
| 159 | + } |
| 160 | + } |
| 161 | + |
| 162 | + private func dismissButton(for content: BrowserProfileImportBannerContent) -> some View { |
| 163 | + Button { |
| 164 | + self.model.dismiss() |
| 165 | + } label: { |
| 166 | + Image(systemName: "xmark") |
| 167 | + .font(.system(size: 10, weight: .semibold)) |
| 168 | + .foregroundStyle(.secondary) |
| 169 | + .frame(width: 20, height: 20) |
| 170 | + .contentShape(Circle()) |
| 171 | + } |
| 172 | + .buttonStyle(.plain) |
| 173 | + .disabled(content.badge == .progress) |
| 174 | + .opacity(content.badge == .progress ? 0.4 : 1) |
| 175 | + .help("Dismiss") |
| 176 | + .accessibilityLabel("Dismiss") |
| 177 | + } |
| 178 | +} |
| 179 | + |
| 180 | +extension BrowserProfileImportBannerView { |
| 181 | + /// Wraps the dashboard web view in a pane that floats the shared import |
| 182 | + /// banner on top; the banner renders empty (zero height) until the model |
| 183 | + /// has an offer or outcome to show. |
| 184 | + @MainActor |
| 185 | + static func makeDashboardPane(webView: NSView) -> NSView { |
| 186 | + let container = NSView() |
| 187 | + webView.translatesAutoresizingMaskIntoConstraints = false |
| 188 | + container.addSubview(webView) |
| 189 | + let banner = NSHostingView(rootView: BrowserProfileImportBannerView(model: .shared)) |
| 190 | + banner.translatesAutoresizingMaskIntoConstraints = false |
| 191 | + container.addSubview(banner) |
| 192 | + // Preferred card width; the required side insets win on narrow panes. |
| 193 | + let bannerWidth = banner.widthAnchor.constraint(equalToConstant: 560) |
| 194 | + bannerWidth.priority = .defaultHigh |
| 195 | + NSLayoutConstraint.activate([ |
| 196 | + webView.leadingAnchor.constraint(equalTo: container.leadingAnchor), |
| 197 | + webView.trailingAnchor.constraint(equalTo: container.trailingAnchor), |
| 198 | + webView.topAnchor.constraint(equalTo: container.topAnchor), |
| 199 | + webView.bottomAnchor.constraint(equalTo: container.bottomAnchor), |
| 200 | + // Float just below the 50px titlebar clearance the native chrome |
| 201 | + // CSS reserves so the card never collides with window controls. |
| 202 | + banner.topAnchor.constraint(equalTo: container.topAnchor, constant: 58), |
| 203 | + banner.centerXAnchor.constraint(equalTo: container.centerXAnchor), |
| 204 | + banner.leadingAnchor.constraint(greaterThanOrEqualTo: container.leadingAnchor, constant: 16), |
| 205 | + bannerWidth, |
| 206 | + ]) |
| 207 | + return container |
| 208 | + } |
| 209 | +} |
| 210 | + |
| 211 | +/// App icon with a status badge in the corner — mirrors the familiar |
| 212 | +/// "import from browser" banner iconography without shipping third-party logos. |
| 213 | +private struct BannerBadgeIcon: View { |
| 214 | + let badge: BrowserProfileImportBannerContent.Badge |
| 215 | + |
| 216 | + var body: some View { |
| 217 | + Image(nsImage: NSApp.applicationIconImage ?? NSImage()) |
| 218 | + .resizable() |
| 219 | + .frame(width: 34, height: 34) |
| 220 | + .overlay(alignment: .bottomTrailing) { |
| 221 | + self.badgeSymbol |
| 222 | + .frame(width: 16, height: 16) |
| 223 | + .background(Circle().fill(.background)) |
| 224 | + .offset(x: 4, y: 4) |
| 225 | + } |
| 226 | + .padding(.trailing, 2) |
| 227 | + } |
| 228 | + |
| 229 | + @ViewBuilder |
| 230 | + private var badgeSymbol: some View { |
| 231 | + switch self.badge { |
| 232 | + case .globe, .progress: |
| 233 | + Image(systemName: "globe") |
| 234 | + .font(.system(size: 11, weight: .semibold)) |
| 235 | + .foregroundStyle(Color.accentColor) |
| 236 | + case .success: |
| 237 | + Image(systemName: "checkmark.circle.fill") |
| 238 | + .font(.system(size: 13, weight: .semibold)) |
| 239 | + .foregroundStyle(.green) |
| 240 | + case .failure: |
| 241 | + Image(systemName: "exclamationmark.triangle.fill") |
| 242 | + .font(.system(size: 11, weight: .semibold)) |
| 243 | + .foregroundStyle(.orange) |
| 244 | + } |
| 245 | + } |
| 246 | +} |
0 commit comments