Repository Public
1import SwiftUI23/// The native start page of a new tab.4struct MobileStartPage: View {5 @Bindable var model: BrowserModel6 let environment: MobileEnvironment7 let tab: MobileTab8 @Environment(\.horizontalSizeClass) private var sizeClass910 private var services: BrowserServices { environment.services }11 private var library: SyncedLibrary { services.library }12 private var columns: [GridItem] { [GridItem(.adaptive(minimum: sizeClass == .regular ? 104 : 80), spacing: 12, alignment: .top)] }1314 private var theme: AmbientTheme { environment.settings.startPage.ambient }1516 var body: some View {17 // Reading the revisions refreshes the page when sync or another scene changes the data.18 let _ = (services.libraryRevision, environment.dataRevision)19 ScrollView {20 VStack(spacing: 26) {21 VStack(spacing: 10) {22 Image("BrandMark").resizable().frame(width: 56, height: 56).accessibilityHidden(true)23 .shadow(color: Aura.Palette.violet.opacity(0.35), radius: 18)24 Text(tab.isPrivate ? L("Private Browsing") : "SP Browser")25 .font(.system(.largeTitle, design: .rounded).weight(.bold))26 .foregroundStyle(tab.isPrivate ? AnyShapeStyle(Aura.Palette.violet) : AnyShapeStyle(Aura.gradient))27 .accessibilityAddTraits(.isHeader)28 }29 .padding(.top, sizeClass == .regular ? 70 : 36)30 .auraAppear(0)3132 Button { model.isEditingAddress = true } label: {33 HStack(spacing: 10) {34 Image(systemName: "magnifyingglass").foregroundStyle(.secondary)35 Text(environment.settings.activeSearchEngine().isNative ? L("Search the web with SP Search") : L("Search or enter address"))36 .foregroundStyle(.secondary).lineLimit(1)37 Spacer()38 }39 .font(.system(size: 17)).padding(.horizontal, 18).frame(height: 52).frame(maxWidth: 640)40 .auraField(focused: false, radius: 26, tint: tab.isPrivate ? Aura.Palette.violet.opacity(0.5) : nil)41 .contentShape(Rectangle())42 }43 .buttonStyle(AuraPressStyle(scale: 0.98, haptic: .tap))44 .accessibilityIdentifier("start-search-field")45 .auraAppear(1)4647 if tab.isPrivate { privateNote.auraAppear(2) } else { sections }48 }49 .padding(.horizontal, 18).padding(.bottom, 40)50 .frame(maxWidth: 760)51 .frame(maxWidth: .infinity)52 }53 .scrollDismissesKeyboard(.immediately)54 // Obsidian and Cosmos only exist in a dark rendition: the content turns light-on-dark over them.55 .environment(\.colorScheme, theme.isAlwaysDark ? .dark : scheme)56 .background(AmbientBackground(theme: theme, isPrivate: tab.isPrivate))57 .accessibilityIdentifier("start-page")58 }5960 @Environment(\.colorScheme) private var scheme6162 private var privateNote: some View {63 VStack(alignment: .leading, spacing: 12) {64 note("checkmark.circle.fill", .green, L("Not kept on this device"),65 L("Pages you visit and what you search are not added to history. Cookies and site data live in memory only and are discarded when the last private tab closes."))66 note("checkmark.circle.fill", .green, L("Never synchronized"), L("Private tabs are not sent to SP Sync and never appear on your other devices."))67 note("exclamationmark.triangle.fill", .orange, L("Not invisible"),68 L("Websites, your employer or school, and your internet provider can still see your activity. Files you download and bookmarks you add are kept."))69 }70 .padding(16).auraSurface(.panel, radius: Aura.Radius.xl, tint: Aura.Palette.violet)71 .accessibilityIdentifier("private-note")72 }7374 private func note(_ symbol: String, _ color: Color, _ title: String, _ text: String) -> some View {75 HStack(alignment: .top, spacing: 12) {76 Image(systemName: symbol).foregroundStyle(color).font(.title3).accessibilityHidden(true)77 VStack(alignment: .leading, spacing: 2) {78 Text(title).font(.headline)79 Text(text).font(.subheadline).foregroundStyle(.secondary).fixedSize(horizontal: false, vertical: true)80 }81 }82 .accessibilityElement(children: .combine)83 }8485 @ViewBuilder private var sections: some View {86 // Tabs another device sent here: an explicit hand-off, offered before anything else.87 ForEach(services.pendingHandoffs) { item in handoffBanner(item).auraAppear(2) }88 let favorites = library.favorites()89 if !favorites.isEmpty {90 tileSection(L("Favorites"), items: favorites.map { ($0.title, $0.url) }, removable: .favorite).auraAppear(3)91 }92 let top = environment.history.topSites(limit: 8)93 if !top.isEmpty { tileSection(L("Frequently visited"), items: top.map { (title($0), $0.url) }, removable: .history).auraAppear(4) }94 let topURLs = Set(top.map(\.url))95 let recent = environment.history.recentPages(limit: 16).filter { !topURLs.contains($0.url) }.prefix(8)96 if !recent.isEmpty { rowSection(L("Recently visited"), symbol: "clock", items: recent.map { (title($0), $0.url) }).auraAppear(5) }97 if services.searchPreferences.keepSearchHistory {98 let searches = library.recentSearches(limit: 8)99 if !searches.isEmpty {100 VStack(alignment: .leading, spacing: 10) {101 header(L("Recent searches"))102 FlowLayout(spacing: 8) {103 ForEach(searches) { item in104 Button { tab.search(item.query) } label: {105 Label(item.query, systemImage: "magnifyingglass").font(.subheadline).lineLimit(1)106 .padding(.horizontal, 12).padding(.vertical, 8)107 .auraSurface(.panel, radius: 18)108 }109 .buttonStyle(AuraPressStyle(scale: 0.95, haptic: .tap))110 .contextMenu { Button(role: .destructive) { library.removeSearch(item); environment.dataChanged() } label: { Label(L("Remove from Search History"), systemImage: "trash") } }111 }112 }113 }114 .auraAppear(6)115 }116 }117 let devices = services.portalDevices118 if !devices.isEmpty {119 // Portal on the start page: opening a tab from here uses this (empty) tab.120 let host = startPortalHost121 VStack(alignment: .leading, spacing: 10) {122 HStack {123 header(L("Continue from another device"))124 Button(L("Portal")) { model.perform(.showPortal) }.font(.subheadline.weight(.medium)).accessibilityIdentifier("start-portal")125 }126 SyncStatusLine(host: host)127 ForEach(devices.prefix(3)) { device in PortalDeviceCard(device: device, host: host) }128 }129 .accessibilityIdentifier("start-other-devices")130 .auraAppear(7)131 }132 let bookmarks = environment.bookmarks.all().filter { !$0.isFolder && $0.url != nil }.prefix(6)133 if !bookmarks.isEmpty { rowSection(L("Bookmarks"), symbol: "bookmark", items: bookmarks.map { ($0.title, $0.url ?? "") }).auraAppear(8) }134 let unread = library.readingList().filter { !$0.isRead }.prefix(5)135 if !unread.isEmpty { rowSection(L("Reading list"), symbol: "eyeglasses", items: unread.map { ($0.title, $0.url) }).auraAppear(9) }136 if !model.recentlyClosed.isEmpty {137 VStack(alignment: .leading, spacing: 8) {138 header(L("Recently closed tabs"))139 VStack(spacing: 0) {140 ForEach(model.recentlyClosed.prefix(5)) { snapshot in141 Button { model.reopen(snapshot) } label: { rowLabel(snapshot.title.isEmpty ? (snapshot.url ?? "") : snapshot.title, snapshot.url ?? "", symbol: "arrow.uturn.backward") }142 .buttonStyle(AuraPressStyle(scale: 0.98))143 }144 }145 .padding(.horizontal, 12).padding(.vertical, 4)146 .auraSurface(.panel, radius: Aura.Radius.xl)147 }148 .auraAppear(10)149 }150 }151152 private var startPortalHost: PortalHost {153 var host = model.portalHost154 host.open = { [weak tab, weak model] remote, inNewTab in155 guard let url = URL(string: remote.url) else { return }156 if inNewTab || tab == nil { model?.newTab(url: url) } else { tab?.load(url) }157 }158 return host159 }160161 private func handoffBanner(_ item: HandoffItem) -> some View {162 VStack(alignment: .leading, spacing: 10) {163 HStack(spacing: 10) {164 Image(systemName: PortalDevice.symbol(for: item.fromPlatform)).font(.title2).foregroundStyle(Aura.gradient).accessibilityHidden(true)165 VStack(alignment: .leading, spacing: 1) {166 Text(L("Sent from %@", item.fromDeviceName)).font(.subheadline.weight(.semibold))167 Text(item.tabs.first.map { $0.title.isEmpty ? $0.url : $0.title } ?? "").font(.caption).foregroundStyle(.secondary).lineLimit(1)168 }169 Spacer(minLength: 0)170 }171 HStack {172 Button(L("Open %d Tabs", item.tabs.count)) { model.acceptHandoff(item) }.buttonStyle(.borderedProminent).accessibilityIdentifier("handoff-accept")173 Button(L("Dismiss")) { services.consumeHandoff(item) }.buttonStyle(.bordered)174 }175 }176 .padding(14)177 .frame(maxWidth: .infinity, alignment: .leading)178 .auraSurface(.floating, radius: Aura.Radius.xl, tint: Aura.Palette.violet, luminous: true)179 .accessibilityIdentifier("start-handoff")180 }181182 private func title(_ page: HistorySuggestion) -> String { page.title.isEmpty ? (URL(string: page.url)?.host ?? page.url) : page.title }183184 private func header(_ text: String) -> some View {185 Text(text).font(.headline).frame(maxWidth: .infinity, alignment: .leading).accessibilityAddTraits(.isHeader)186 }187188 private enum Removal { case favorite, history }189190 private func tileSection(_ titleText: String, items: [(String, String)], removable: Removal) -> some View {191 VStack(alignment: .leading, spacing: 12) {192 header(titleText)193 LazyVGrid(columns: columns, spacing: 12) {194 ForEach(Array(items.enumerated()), id: \.offset) { _, item in195 Button { if let url = URL(string: item.1) { tab.load(url) } } label: {196 VStack(spacing: 8) {197 SiteIcon(url: faviconURL(item.1), fallback: URL(string: item.1)?.host?.replacingOccurrences(of: "www.", with: "") ?? item.0, size: 30)198 Text(item.0).font(.caption).lineLimit(1).frame(maxWidth: .infinity)199 }200 .padding(.horizontal, 6).padding(.top, 14).padding(.bottom, 10)201 .frame(maxWidth: .infinity)202 .auraSurface(.panel, radius: Aura.Radius.xl, interactive: true)203 .contentShape(Rectangle())204 }205 .buttonStyle(AuraPressStyle(scale: 0.94, haptic: .tap))206 .accessibilityLabel(item.0).accessibilityAddTraits(.isLink)207 .contextMenu {208 Button { model.newTab(url: URL(string: item.1)) } label: { Label(L("Open in New Tab"), systemImage: "plus.square") }209 Button { PlatformActions.copy(item.1) } label: { Label(L("Copy Link"), systemImage: "doc.on.doc") }210 switch removable {211 case .favorite:212 Button(role: .destructive) { library.removeFavorite(url: item.1); environment.dataChanged() } label: { Label(L("Remove from Favorites"), systemImage: "star.slash") }213 case .history:214 if !library.isFavorite(item.1) { Button { library.addFavorite(title: item.0, url: item.1); environment.dataChanged() } label: { Label(L("Add to Favorites"), systemImage: "star") } }215 Button(role: .destructive) { environment.history.delete(url: item.1); environment.dataChanged() } label: { Label(L("Remove from History"), systemImage: "trash") }216 }217 }218 }219 }220 }221 }222223 /// The site's own icon, fetched from the site itself (no third-party favicon service).224 private func faviconURL(_ page: String) -> URL? {225 guard let url = URL(string: page), let host = url.host, url.scheme == "https" else { return nil }226 return URL(string: "https://\(host)/favicon.ico")227 }228229 private func rowSection(_ titleText: String, symbol: String, items: [(String, String)]) -> some View {230 VStack(alignment: .leading, spacing: 8) {231 header(titleText)232 VStack(spacing: 0) {233 ForEach(Array(items.enumerated()), id: \.offset) { _, item in linkRow(item.0, item.1, symbol: symbol) }234 }235 .padding(.horizontal, 12).padding(.vertical, 4)236 .auraSurface(.panel, radius: Aura.Radius.xl)237 }238 }239240 private func linkRow(_ titleText: String, _ url: String, symbol: String) -> some View {241 Button { if let target = URL(string: url) { tab.load(target) } } label: { rowLabel(titleText, url, symbol: symbol) }242 .buttonStyle(AuraPressStyle(scale: 0.98))243 .contextMenu {244 Button { model.newTab(url: URL(string: url)) } label: { Label(L("Open in New Tab"), systemImage: "plus.square") }245 Button { PlatformActions.copy(url) } label: { Label(L("Copy Link"), systemImage: "doc.on.doc") }246 }247 }248249 private func rowLabel(_ titleText: String, _ url: String, symbol: String) -> some View {250 HStack(spacing: 10) {251 Image(systemName: symbol).foregroundStyle(.secondary).frame(width: 22)252 VStack(alignment: .leading, spacing: 1) {253 Text(titleText.isEmpty ? url : titleText).lineLimit(1)254 Text(URL(string: url)?.host ?? "").font(.caption).foregroundStyle(.secondary).lineLimit(1)255 }256 Spacer(minLength: 0)257 }258 .padding(.vertical, 7).contentShape(Rectangle())259 }260}261