Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 21 additions & 0 deletions Sources/Stag/Utils/RecentColors.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import SwiftUI

/// Most-recently-used color list logic for the editor's swatch row. Previously
/// this de-dup/insert/cap sequence lived inline in `EditorView`; extracting it
/// makes the rule a pure, unit-testable function and trims the view's surface.
enum RecentColors {
/// Default number of swatches retained.
static let limit = 8

/// Returns `history` with `color` moved to the front (de-duplicated) and the
/// list capped at `limit` — most-recently-used first.
static func recording(_ color: Color, into history: [Color], limit: Int = limit) -> [Color] {
var out = history
out.removeAll { $0 == color }
out.insert(color, at: 0)
if out.count > limit {
out.removeLast(out.count - limit)
}
return out
}
}
9 changes: 1 addition & 8 deletions Sources/Stag/Views/Editor/EditorView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -1743,14 +1743,7 @@ struct EditorView: View {
// MARK: - Color History

private func recordColorUsage(_ color: Color) {
// Remove if already in history
colorHistory.removeAll { $0 == color }
// Add to beginning
colorHistory.insert(color, at: 0)
// Keep only last 8
if colorHistory.count > 8 {
colorHistory.removeLast()
}
colorHistory = RecentColors.recording(color, into: colorHistory)
}

// MARK: - Arrow Head Drawing
Expand Down
47 changes: 47 additions & 0 deletions Tests/StagTests/RecentColorsTests.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
import XCTest
import SwiftUI
@testable import Stag

/// MRU/dedup/cap logic for the editor's recent-color swatches.
final class RecentColorsTests: XCTestCase {

private let a = Color(red: 1, green: 0, blue: 0)
private let b = Color(red: 0, green: 1, blue: 0)
private let c = Color(red: 0, green: 0, blue: 1)

func testRecordingIntoEmpty() {
XCTAssertEqual(RecentColors.recording(a, into: []), [a])
}

func testNewColorGoesToFront() {
XCTAssertEqual(RecentColors.recording(c, into: [a, b]), [c, a, b])
}

func testExistingColorMovesToFrontWithoutDuplicating() {
XCTAssertEqual(RecentColors.recording(b, into: [a, b, c]), [b, a, c])
}

func testCapsAtLimitDroppingOldest() {
// Fill beyond the limit with distinct grays, newest recorded last.
var history: [Color] = []
for i in 0..<12 {
history = RecentColors.recording(Color(white: Double(i) / 12.0), into: history)
}
XCTAssertEqual(history.count, RecentColors.limit)
// The most recent (i = 11) is first; the oldest survivors dropped off the end.
XCTAssertEqual(history.first, Color(white: 11.0 / 12.0))
}

func testRecordingExistingAtCapacityKeepsCount() {
var history: [Color] = (0..<RecentColors.limit).map { Color(white: Double($0) / 10.0) }
let existing = history[3]
history = RecentColors.recording(existing, into: history)
XCTAssertEqual(history.count, RecentColors.limit)
XCTAssertEqual(history.first, existing)
}

func testCustomLimit() {
let out = RecentColors.recording(c, into: [a, b], limit: 2)
XCTAssertEqual(out, [c, a])
}
}
Loading