SPB Git

Repository Public

SidebarView.swift 7.4 KB 178 lines swift

1// ORIX Docs2// File: App/Sources/Sidebar/SidebarView.swift3// Description: The document sidebar: a compact tab strip over the outline, page thumbnails, find and replace, and review panels,4//              plus the small controls those panels share.5//6// Author: Simon-Pierre Boucher7// Contact: contact@spboucher.ai8import SwiftUI9import DocModel1011struct SidebarView: View {12    @Bindable var session: DocumentSession1314    init(session: DocumentSession) { self.session = session }1516    var body: some View {17        VStack(spacing: 0) {18            SidebarTabStrip(selection: $session.sidebarTab)19            Rectangle().fill(ORIX.Palette.hairline).frame(height: 1)20            Group {21                switch session.sidebarTab {22                case .outline: OutlinePanel(session: session)23                case .pages: PagesPanel(session: session)24                case .search: FindPanel(session: session)25                case .review: ReviewPanel(session: session)26                }27            }28            .frame(maxWidth: .infinity, maxHeight: .infinity, alignment: .top)29        }30        .background(ORIX.Palette.chrome)31        .accessibilityIdentifier("sidebar")32    }33}3435/// Four equal segments; the selected one carries its title so the strip stays readable at sidebar width.36private struct SidebarTabStrip: View {37    @Binding var selection: SidebarTab3839    var body: some View {40        HStack(spacing: 2) {41            ForEach(SidebarTab.allCases) { tab in42                let isOn = selection == tab43                Button { selection = tab } label: {44                    HStack(spacing: 5) {45                        Image(systemName: tab.symbol).font(.system(size: 11.5, weight: .medium))46                        if isOn { Text(tab.title).font(.system(size: 11.5, weight: .semibold)).lineLimit(1).fixedSize() }47                    }48                    .frame(maxWidth: isOn ? .infinity : 34, minHeight: 24)49                    .foregroundStyle(isOn ? ORIX.Palette.accent : ORIX.Palette.textSecondary)50                    .background(RoundedRectangle(cornerRadius: ORIX.Radius.s).fill(isOn ? ORIX.Palette.accent.opacity(0.14) : Color.clear))51                    .contentShape(Rectangle())52                }53                .buttonStyle(.plain)54                .help(Text(tab.title))55                .accessibilityLabel(Text(tab.title))56                .accessibilityAddTraits(isOn ? .isSelected : [])57                .accessibilityIdentifier("sidebar-tab-\(tab.rawValue)")58            }59        }60        .padding(.horizontal, ORIX.Space.s)61        .frame(height: 34)62        .animation(ORIX.Motion.quick, value: selection)63    }64}6566// MARK: - Shared controls6768/// Uppercase section title with an optional trailing detail (a count, a state).69struct SidebarSectionHeader<Trailing: View>: View {70    let title: LocalizedStringKey71    @ViewBuilder var trailing: Trailing7273    var body: some View {74        HStack(spacing: ORIX.Space.s) {75            Text(title).font(ORIX.Font.sectionTitle).textCase(.uppercase).kerning(0.6).foregroundStyle(ORIX.Palette.textTertiary)76                .accessibilityAddTraits(.isHeader)77            Spacer(minLength: 0)78            trailing79        }80        .font(ORIX.Font.caption)81        .foregroundStyle(ORIX.Palette.textTertiary)82    }83}8485extension SidebarSectionHeader where Trailing == EmptyView {86    init(title: LocalizedStringKey) { self.init(title: title) { EmptyView() } }87}8889/// The field chrome used by every sidebar text field: surface fill, hairline, accent ring when focused.90struct SidebarFieldChrome: ViewModifier {91    var focused: Bool92    var invalid = false93    func body(content: Content) -> some View {94        content95            .textFieldStyle(.plain)96            .font(ORIX.Font.body)97            .padding(.horizontal, 7).padding(.vertical, 5)98            .background(RoundedRectangle(cornerRadius: ORIX.Radius.s).fill(ORIX.Palette.surface))99            .overlay(RoundedRectangle(cornerRadius: ORIX.Radius.s).strokeBorder(invalid ? ORIX.Palette.critical : (focused ? ORIX.Palette.accent : ORIX.Palette.hairline)))100    }101}102103/// A small icon button for list rows (accept, reject, resolve, delete).104struct SidebarIconButton: View {105    let symbol: String106    let label: LocalizedStringKey107    var tint: Color = ORIX.Palette.textSecondary108    let action: () -> Void109    @State private var hover = false110111    var body: some View {112        Button(action: action) {113            Image(systemName: symbol)114                .font(.system(size: 10.5, weight: .semibold))115                .frame(width: 22, height: 20)116                .foregroundStyle(tint)117                .background(RoundedRectangle(cornerRadius: ORIX.Radius.s).fill(hover ? tint.opacity(0.16) : ORIX.Palette.text.opacity(0.05)))118                .contentShape(Rectangle())119        }120        .buttonStyle(.plain)121        .onHover { hover = $0 }122        .help(label)123        .accessibilityLabel(Text(label))124    }125}126127enum SidebarFormat {128    /// "12 Nov, 14:02" in the user's locale.129    static func date(_ date: Date) -> String {130        date.formatted(.dateTime.day().month(.abbreviated).hour().minute())131    }132133    /// One line of text for a list row: line breaks and object placeholders become spaces, long text is cut on a character boundary.134    static func excerpt(_ text: String, limit: Int = 140) -> String {135        var s = text.replacingOccurrences(of: "\u{FFFC}", with: " ").replacingOccurrences(of: "\u{2028}", with: " ")136            .replacingOccurrences(of: "\n", with: " ").replacingOccurrences(of: "\t", with: " ")137            .trimmingCharacters(in: .whitespaces)138        if s.count > limit { s = String(s.prefix(limit)).trimmingCharacters(in: .whitespaces) + "…" }139        return s140    }141142    static func story(_ story: StoryID, cell: Bool) -> String {143        switch story {144        case .body: return cell ? String(localized: "Table") : String(localized: "Body")145        case .header: return String(localized: "Header")146        case .footer: return String(localized: "Footer")147        case .note: return String(localized: "Note")148        }149    }150}151152extension DocumentSession {153    /// Formatted number of the page that shows `offset` of a paragraph; nil for text that repeats on every page or is not laid out.154    func pageLabel(story: StoryID, path: ParaPath, offset: Int) -> String? {155        switch story {156        case .header, .footer: return nil157        case .body, .note: break158        }159        let fragments = layout.fragments(story: story, path: path)160        guard !fragments.isEmpty else { return nil }161        var page = fragments[0].page162        for f in fragments {163            guard let first = f.fragment.lines.first, let last = f.fragment.lines.last else { continue }164            let lo = f.fragment.layout.lines[first].range.lowerBound, hi = f.fragment.layout.lines[last].range.upperBound165            if offset >= lo && offset <= hi { page = f.page; break }166        }167        guard layout.pages.indices.contains(page) else { return nil }168        let text = layout.pages[page].numberText169        return text.isEmpty ? String(page + 1) : text170    }171172    /// Selects a range and scrolls to it without taking keyboard focus away from the sidebar.173    func showRange(story: StoryID, path: ParaPath, range: Range<Int>) {174        if story != .body, let frag = layout.fragments(story: story, path: path).first { activePage = frag.page }175        select(Selection(story: story, anchor: Position(path: path, offset: range.lowerBound), head: Position(path: path, offset: range.upperBound)))176    }177}178
Files main
Menu

Theme