SPB Git

Repository Public

TabStrip.swift 7.5 KB 155 lines swift

1import SwiftUI23/// Context menu shared by the horizontal strip and the vertical list.4struct TabMenu: View {5    let tab: BrowserTab6    let state: BrowserWindowState78    var body: some View {9        Button(L("New Tab After")) { state.newTab(select: true, after: tab); state.focusAddressBar() }10        Button(CommandID.duplicateTab.title) { state.duplicate(tab) }.disabled(tab.url == nil)11        Button(tab.isPinned ? L("Unpin Tab") : L("Pin Tab")) { state.togglePin(tab) }12        if tab.id != state.selectedTabID {13            Button(L("Open in Split View")) { state.openInSplit(tab) }14            Button(L("Suspend")) { tab.suspend() }.disabled(tab.webView == nil)15        }16        Divider()17        Menu(L("Move to Group")) {18            ForEach(state.groups) { group in19                Button(group.name) { state.setGroup(group.id, for: tab) }20            }21            if !state.groups.isEmpty { Divider() }22            Button(L("New Group")) { state.createGroup(name: "", with: tab) }23            if tab.groupID != nil { Button(L("Remove from Group")) { state.setGroup(nil, for: tab) } }24        }25        if state.workspaces.count > 1 {26            Menu(L("Move to Workspace")) {27                ForEach(state.workspaces.filter { $0.id != tab.workspaceID }) { workspace in28                    Button(workspace.name) {29                        tab.workspaceID = workspace.id30                        tab.groupID = nil31                        if state.selectedTabID == tab.id {32                            if let next = state.visibleTabs.first { state.select(next) } else { state.newTab(select: true) }33                        }34                    }35                }36            }37        }38        if !state.isPrivate, state.tabs.count > 1 {39            Button(CommandID.moveTabToNewWindow.title) { state.select(tab); state.perform(.moveTabToNewWindow) }40        }41        if tab.url != nil {42            let isFavorite = state.activeWorkspace?.favorites.contains { $0.url == tab.url?.absoluteString } ?? false43            Button(isFavorite ? L("Remove from Workspace Favorites") : L("Add to Workspace Favorites")) { state.toggleFavorite(for: tab) }44        }45        Divider()46        Button(CommandID.closeTab.title) { state.close(tab) }47        Button(CommandID.closeOtherTabs.title) { state.closeOthers(keeping: tab) }.disabled(state.visibleTabs.count < 2)48    }49}5051/// Small preview shown when the pointer rests on a tab.52struct TabPreview: View {53    let tab: BrowserTab5455    var body: some View {56        VStack(alignment: .leading, spacing: 6) {57            if let thumbnail = tab.thumbnail {58                Image(nsImage: thumbnail).resizable().aspectRatio(contentMode: .fill)59                    .frame(width: 260, height: 150, alignment: .top).clipShape(RoundedRectangle(cornerRadius: Theme.radiusS))60            }61            Text(tab.displayTitle).font(.callout.weight(.medium)).lineLimit(2)62            if let url = tab.url {63                Text(url.host ?? url.absoluteString).font(.caption).foregroundStyle(.secondary).lineLimit(1)64            }65            if tab.isSuspended { Label(L("Suspended to save memory"), systemImage: "moon.zzz").font(.caption).foregroundStyle(.secondary) }66        }67        .padding(10).frame(width: 280, alignment: .leading)68    }69}7071struct TabStripView: View {72    let state: BrowserWindowState73    let environment: AppEnvironment74    @Namespace private var selection75    @Environment(\.auraPolicy) private var policy7677    var body: some View {78        HStack(spacing: 4) {79            ScrollViewReader { proxy in80                ScrollView(.horizontal, showsIndicators: false) {81                    HStack(spacing: 3) {82                        ForEach(state.visibleTabs) { tab in83                            TabChip(tab: tab, state: state, namespace: selection).id(tab.id)84                                // Tab Morphing in the strip: a new chip grows in place, a closed one contracts into the bar.85                                .transition(AuraTransition.tabChip(policy))86                        }87                    }88                    .padding(.horizontal, 8)89                    .animation(AuraMotion.animation(.spring, policy: policy), value: state.visibleTabs.map(\.id))90                    .animation(AuraMotion.animation(.spring, policy: policy), value: state.selectedTabID)91                }92                .onChange(of: state.selectedTabID) { _, id in if let id { withAnimation(AuraMotion.animation(.standard, policy: policy)) { proxy.scrollTo(id) } } }93            }94            IconButton(symbol: "sparkle", label: CommandID.showConstellation.title, isActive: state.auraOverlay == .constellation, size: 26) { state.perform(.showConstellation) }95                .accessibilityIdentifier("constellation-button")96            IconButton(symbol: "plus", label: CommandID.newTab.title, size: 26) { state.perform(.newTab) }97                .padding(.trailing, 8)98        }99        .padding(.bottom, 5)100        .dropDestination(for: String.self) { items, _ in101            guard let id = items.first.flatMap(UUID.init(uuidString:)) else { return false }102            state.moveTab(id, before: nil)103            return true104        }105        .accessibilityElement(children: .contain)106        .accessibilityLabel(L("Tabs"))107    }108}109110struct TabChip: View {111    let tab: BrowserTab112    let state: BrowserWindowState113    var namespace: Namespace.ID?114    @State private var hovering = false115    @State private var showsPreview = false116    @State private var isTargeted = false117118    private var isSelected: Bool { state.selectedTabID == tab.id }119    private var group: TabGroup? { state.group(id: tab.groupID) }120121    var body: some View {122        AuraTab(title: tab.displayTitle, isSelected: isSelected, isPinned: tab.isPinned, accent: group.map { Theme.color($0.colorIndex) },123                badge: state.splitTabID == tab.id ? "rectangle.split.2x1" : nil, namespace: namespace,124                closeLabel: L("Close %@", tab.displayTitle), onClose: { state.close(tab) }) {125            if tab.isLoading {126                AuraProgressIndicator(progress: tab.progress, style: .ring, thickness: 1.8).frame(width: 13, height: 13)127            } else {128                Favicon(image: tab.favicon, fallback: tab.displayTitle).opacity(tab.isSuspended ? 0.45 : 1)129            }130        }131        .overlay(alignment: .leading) { if isTargeted { Capsule().fill(Theme.accent).frame(width: 2.5).offset(x: -3) } }132        .onTapGesture { state.select(tab) }133        .onHover { hovering = $0 }134        .task(id: hovering) {135            guard hovering, !isSelected else { showsPreview = false; return }136            try? await Task.sleep(for: .milliseconds(700))137            if !Task.isCancelled { showsPreview = true }138        }139        .popover(isPresented: $showsPreview, arrowEdge: .bottom) { TabPreview(tab: tab) }140        .contextMenu { TabMenu(tab: tab, state: state) }141        .draggable(tab.id.uuidString)142        .dropDestination(for: String.self) { items, _ in143            guard let id = items.first.flatMap(UUID.init(uuidString:)) else { return false }144            state.moveTab(id, before: tab.id)145            return true146        } isTargeted: { isTargeted = $0 }147        .help(tab.isPinned ? tab.displayTitle : (group.map { L("Group: %@", $0.name) } ?? ""))148        .accessibilityElement(children: .ignore)149        .accessibilityLabel(tab.displayTitle + (tab.isPinned ? ", " + L("pinned") : ""))150        .accessibilityAddTraits(isSelected ? [.isButton, .isSelected] : .isButton)151        .accessibilityAction { state.select(tab) }152        .accessibilityAction(named: L("Close Tab")) { state.close(tab) }153    }154}155
Files main
Menu

Theme