SPB Git

Repository Public License

NewTabPage.swift 7.1 KB 164 lines swift

1import LunarDesign2import NavigatorCore3import Persistence4import SwiftUI56/// The native page of an empty tab: the mark, a search field, favourites and recent pages. No feed, no ads.7struct NewTabPage: View {8    @Environment(\.lunarTheme) private var theme9    @Environment(\.accessibilityReduceMotion) private var reduceMotion10    @Environment(\.horizontalSizeClass) private var sizeClass11    @Environment(BrowserStore.self) private var store12    let window: WindowModel13    var bottomInset: CGFloat = 01415    @State private var query = ""16    @State private var favorites: [Bookmark] = []17    @State private var recents: [HistoryPage] = []1819    private var isCompact: Bool {20        #if os(iOS)21        sizeClass == .compact22        #else23        false24        #endif25    }2627    var body: some View {28        ZStack {29            BackdropView(density: store.settings.newTabStarfield ? 1.1 : 0,30                         isAnimated: store.settings.newTabStarfield && !reduceMotion)31            ScrollView {32                VStack(spacing: LunarSpacing.lg) {33                    hero34                    searchField35                    if store.settings.newTabShowsFavorites, !favorites.isEmpty { favoritesGrid }36                    if store.settings.newTabShowsRecents, !recents.isEmpty { recentList }37                }38                .frame(maxWidth: 720)39                .padding(.horizontal, LunarSpacing.lg)40                .padding(.top, isCompact ? LunarSpacing.xl : 56)41                .padding(.bottom, LunarSpacing.xl + bottomInset)42                .frame(maxWidth: .infinity)43            }44            .scrollIndicators(.never)45            #if os(iOS)46            .scrollDismissesKeyboard(.interactively)47            #endif48        }49        .task(id: "\(store.bookmarksRevision)#\(store.historyRevision)#\(window.dataProfileID)") { await load() }50        .accessibilityElement(children: .contain)51        .accessibilityIdentifier("newTabPage")52    }5354    private var hero: some View {55        VStack(spacing: LunarSpacing.sm) {56            ZStack {57                LunarOrbitRings(count: 2).frame(width: isCompact ? 190 : 250, height: isCompact ? 190 : 250).opacity(0.7)58                LunarMarkView(size: isCompact ? 96 : 124)59            }60            .accessibilityHidden(true)61            Text(verbatim: "ORIX Navigator").lunarFont(.display).foregroundStyle(theme.textPrimary)62                .accessibilityAddTraits(.isHeader)63            Text(Self.greeting()).lunarFont(.callout).foregroundStyle(theme.textSecondary.mixed(with: theme.textPrimary, 0.5))64        }65    }6667    private var searchField: some View {68        LunarSearchField(text: $query, prompt: L("Search or enter an address"), leading: .symbol("magnifyingglass")) {69            #if os(macOS)70            LunarShortcut("⌘L")71            #endif72        }73        .onSubmit {74            let text = query75            query = ""76            window.open(input: text)77        }78        #if os(iOS)79        .textInputAutocapitalization(.never)80        .autocorrectionDisabled()81        .keyboardType(.webSearch)82        .submitLabel(.go)83        #endif84        .frame(maxWidth: 600)85        .padding(LunarSpacing.unit)86        .lunarGlass(.capsule, style: .chrome, elevation: .floating)87        .accessibilityIdentifier("newTab.search")88    }8990    private var favoritesGrid: some View {91        let columns = [GridItem(.adaptive(minimum: isCompact ? 72 : 96, maximum: 120), spacing: LunarSpacing.md)]92        return LazyVGrid(columns: columns, spacing: LunarSpacing.md) {93            ForEach(favorites.prefix(isCompact ? 8 : 10)) { bookmark in94                Button { if let url = bookmark.url { window.navigate(to: url) } } label: {95                    VStack(spacing: LunarSpacing.xs) {96                        FaviconView(page: bookmark.url, title: bookmark.title, size: 34, profile: window.dataProfileID)97                            .frame(width: 66, height: 66)98                            .background { LunarShape.sm.fill(theme.elevatedSurface.withAlpha(theme.isDark ? 0.7 : 0.9)) }99                            .lunarHairline(.sm)100                            .lunarShadow(.raised)101                        Text(bookmark.title).lunarFont(.caption).foregroundStyle(theme.textSecondary).lineLimit(1)102                    }103                    .frame(maxWidth: .infinity)104                    .contentShape(Rectangle())105                }106                .buttonStyle(LunarPressStyle())107                .accessibilityLabel(bookmark.title)108                .accessibilityHint(L("Opens this favourite"))109                .accessibilityIdentifier("newTab.favorite")110            }111        }112        .frame(maxWidth: 640)113    }114115    private var recentList: some View {116        VStack(alignment: .leading, spacing: LunarSpacing.unit) {117            Text(L("Recent pages")).textCase(.uppercase).lunarFont(.overline).foregroundStyle(theme.textSecondary)118                .padding(.horizontal, LunarSpacing.sm)119                .accessibilityAddTraits(.isHeader)120            LunarCard(padding: LunarSpacing.unit, shape: .sm) {121                VStack(spacing: 0) {122                    ForEach(recents.prefix(6), id: \.url) { page in123                        Button { window.navigate(to: page.url) } label: {124                            LunarHoverRow {125                                HStack(spacing: LunarSpacing.sm) {126                                    FaviconView(page: page.url, title: page.title.isEmpty ? URLDisplay.host(page.url) : page.title,127                                                size: 20, profile: window.dataProfileID)128                                    VStack(alignment: .leading, spacing: 1) {129                                        Text(page.title.isEmpty ? URLDisplay.short(page.url) : page.title)130                                            .lunarFont(.bodyEmphasized).foregroundStyle(theme.textPrimary).lineLimit(1)131                                        Text(URLDisplay.short(page.url)).lunarFont(.caption).foregroundStyle(theme.textSecondary)132                                            .lineLimit(1).truncationMode(.middle)133                                    }134                                    Spacer(minLength: 0)135                                }136                                .padding(.horizontal, LunarSpacing.sm)137                                .frame(minHeight: 48)138                            }139                        }140                        .buttonStyle(LunarPressStyle())141                        .accessibilityElement(children: .combine)142                        .accessibilityIdentifier("newTab.recent")143                    }144                }145            }146        }147        .frame(maxWidth: 600)148    }149150    private func load() async {151        let profile = window.dataProfileID152        favorites = (try? await store.database.bookmarks.favorites(profile: profile)) ?? []153        recents = (try? await store.database.history.topPages(profile: profile, limit: 6)) ?? []154    }155156    static func greeting(now: Date = .now, calendar: Calendar = .current) -> String {157        switch calendar.component(.hour, from: now) {158        case 5..<12: L("Good morning.")159        case 12..<18: L("Good afternoon.")160        default: L("Good evening.")161        }162    }163}164
Files main
Menu

Theme