Files

40 lines
1.3 KiB
Swift

import Foundation
import HelderCore
/// What choosing a launcher row does.
public enum LauncherChoice: Equatable {
/// "Open new project…": the folder picker.
case chooseFolder
case open(path: String)
}
/// The launcher's rows and keyboard cursor: row 0 is "Open new project…", then the recent projects.
public struct LauncherList: Equatable {
public private(set) var recents: [RecentProject]
public private(set) var selection = 0
/// `excluding` drops the window's own project, so ⇧⌘O never offers the folder that is already open.
public init(recents: [RecentProject] = [], excluding root: String? = nil) {
self.recents = recents.filter { $0.path != root }
}
public var count: Int { recents.count + 1 }
/// Moves the cursor, clamped to the first and last row.
public mutating func move(by step: Int) {
selection = min(max(selection + step, 0), count - 1)
}
public mutating func select(_ index: Int) {
selection = min(max(index, 0), count - 1)
}
public func choice(at index: Int) -> LauncherChoice? {
if index == 0 { return .chooseFolder }
guard recents.indices.contains(index - 1) else { return nil }
return .open(path: recents[index - 1].path)
}
public var selectedChoice: LauncherChoice? { choice(at: selection) }
}