SPB Git

Repository Public License

TabOperations.swift 3.6 KB 66 lines swift

1import Foundation2import NavigatorCore34/// Pure tab-organisation rules shared by `OrbitService` (persisted windows) and by in-memory private windows.5public enum TabOperations {6    /// A copy of `tab` to place right after it: new identity, same page, history and settings, not pinned7    /// (a duplicate of a favourite is an ordinary tab), dormant until the UI loads it.8    public static func duplicate(_ tab: Tab, now: Date) -> Tab {9        Tab(windowID: tab.windowID, spaceID: tab.spaceID, parentID: tab.isPinned ? nil : tab.parentID,10            groupID: tab.isPinned ? nil : tab.groupID, url: tab.url, title: tab.title, faviconURL: tab.faviconURL,11            isPinned: false, isMuted: tab.isMuted, order: tab.order + 1, lifecycle: .dormant,12            sessionState: tab.sessionState, zoom: tab.zoom, userAgentMode: tab.userAgentMode,13            isReaderActive: false, createdAt: now, lastActiveAt: now)14    }1516    /// `ids` with `moved` inserted at `index` (clamped; `nil` = end), removed from its previous position.17    public static func inserting(_ moved: TabID, into ids: [TabID], at index: Int?) -> [TabID] {18        var result = ids.filter { $0 != moved }19        result.insert(moved, at: min(max(index ?? result.count, 0), result.count))20        return result21    }2223    /// Position of a new tab among the unpinned tabs of a window (`order` = display order of the open tabs).24    public enum NewTabPosition: Sendable, Equatable {25        case end26        /// After `tab` and after the children it already has (a link opened in the background).27        case after(TabID)28    }2930    public static func insertionIndex(for position: NewTabPosition, in ordered: [Tab]) -> Int {31        switch position {32        case .end: return ordered.count33        case .after(let anchor):34            guard let index = ordered.firstIndex(where: { $0.id == anchor }) else { return ordered.count }35            var last = index36            // Skip the anchor's existing descendants so siblings appear in opening order.37            var family: Set<TabID> = [anchor]38            for position in (index + 1)..<ordered.count {39                guard let parent = ordered[position].parentID, family.contains(parent) else { break }40                family.insert(ordered[position].id)41                last = position42            }43            return last + 144        }45    }4647    /// The tab to select after `closing` goes away: the next tab in display order, else the previous one.48    /// `nil` when it was the last tab. If `closing` was not the selection, the selection does not change.49    public static func selectionAfterClosing(_ closing: TabID, selected: TabID?, order: [TabID]) -> TabID? {50        if let selected, selected != closing, order.contains(selected) { return selected }51        guard let index = order.firstIndex(of: closing) else { return order.first }52        if index + 1 < order.count { return order[index + 1] }53        return index > 0 ? order[index - 1] : nil54    }5556    /// Trims a user-entered name: surrounding white space removed, line breaks and control characters replaced57    /// by spaces, at most `limit` characters. `nil` when nothing printable is left.58    public static func cleanedName(_ name: String, limit: Int = 60) -> String? {59        let flattened = String(name.unicodeScalars.map { scalar -> Character in60            CharacterSet.controlCharacters.contains(scalar) || CharacterSet.newlines.contains(scalar) ? " " : Character(scalar)61        })62        let trimmed = flattened.trimmingCharacters(in: .whitespacesAndNewlines)63        return trimmed.isEmpty ? nil : String(trimmed.prefix(limit)).trimmingCharacters(in: .whitespaces)64    }65}66
Files main
Menu

Theme