iOS 0.6.0: milestone 5 (recording, MIDI, input bindings, themes, bundles, backups)
- Master recording (MasterSink ring + writer thread) and the sample recorder - CoreMIDI in/out: sound module, clock send/follow, virtual "Sizzle" ports; platform-free parser/module/clock in core (midi_test) - Hardware keyboard and gamepads via GameController; rebindable keyboard, gamepad and MIDI CC actions; per-parameter MIDI learn - Colour themes (.szt), project bundles (.szgz), backup & restore; ZIP in core with zlib (zip_test against jar and unzip) - Persistent app settings; audio pauses in the background unless sound module Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
co-authored by
Claude Opus 5.5
parent
435305e42a
commit
64e21e0bcc
@@ -0,0 +1,118 @@
|
||||
// SPDX-FileCopyrightText: 2026 Reactorcoremeltdown
|
||||
// SPDX-License-Identifier: GPL-3.0-or-later
|
||||
|
||||
import Foundation
|
||||
|
||||
/// Colour themes (Android's ThemeIo + ThemeLibrary): the two stock palettes in
|
||||
/// code, user themes as `SIZZLE-THEME 1` text in Documents/themes/<name>.szt, the
|
||||
/// same files the Android app imports and exports.
|
||||
@MainActor
|
||||
final class ThemeLibrary: ObservableObject {
|
||||
static let shared = ThemeLibrary()
|
||||
static let header = "SIZZLE-THEME 1"
|
||||
|
||||
@Published private(set) var names: [String] = []
|
||||
|
||||
private var dir: URL {
|
||||
let d = FileManager.default.urls(for: .documentDirectory, in: .userDomainMask)[0].appendingPathComponent("themes")
|
||||
try? FileManager.default.createDirectory(at: d, withIntermediateDirectories: true)
|
||||
return d
|
||||
}
|
||||
|
||||
private init() { refresh() }
|
||||
|
||||
/// Stock themes first, then the stored ones alphabetically.
|
||||
func refresh() {
|
||||
let files = (try? FileManager.default.contentsOfDirectory(atPath: dir.path)) ?? []
|
||||
let user = files.filter { $0.hasSuffix(".szt") }.map { String($0.dropLast(4)) }.sorted()
|
||||
names = Palette.stock.map(\.name) + user.filter { u in !Palette.stock.contains { $0.name == u } }
|
||||
}
|
||||
|
||||
func isStock(_ name: String) -> Bool { Palette.stock.contains { $0.name == name } }
|
||||
|
||||
func palette(named name: String) -> Palette? {
|
||||
if let p = Palette.stock.first(where: { $0.name == name }) { return p }
|
||||
guard let text = try? String(contentsOf: file(name), encoding: .utf8) else { return nil }
|
||||
return Self.parse(text)
|
||||
}
|
||||
|
||||
/// Apply a theme by name (unknown names fall back to the default) and remember it.
|
||||
func select(_ name: String) {
|
||||
let p = palette(named: name) ?? .amber
|
||||
Theme.current = p
|
||||
if AppSettings.shared.paletteName != p.name { AppSettings.shared.paletteName = p.name }
|
||||
}
|
||||
|
||||
/// Store and apply theme [text]; nil if it isn't a theme.
|
||||
func importText(_ text: String) -> Palette? {
|
||||
guard let p = Self.parse(text) else { return nil }
|
||||
try? Self.export(p).write(to: file(p.name), atomically: true, encoding: .utf8)
|
||||
refresh()
|
||||
select(p.name)
|
||||
return p
|
||||
}
|
||||
|
||||
func delete(_ name: String) {
|
||||
guard !isStock(name) else { return }
|
||||
try? FileManager.default.removeItem(at: file(name))
|
||||
refresh()
|
||||
if Theme.current.name == name { select(Palette.amber.name) }
|
||||
}
|
||||
|
||||
/// The current theme as a .szt in a temporary folder, for sharing.
|
||||
func exportFile() throws -> URL {
|
||||
let p = Theme.current
|
||||
let d = FileManager.default.temporaryDirectory.appendingPathComponent("ThemeExport", isDirectory: true)
|
||||
try? FileManager.default.removeItem(at: d)
|
||||
try FileManager.default.createDirectory(at: d, withIntermediateDirectories: true)
|
||||
let url = d.appendingPathComponent(Self.sanitize(p.name) + ".szt")
|
||||
try Self.export(p).write(to: url, atomically: true, encoding: .utf8)
|
||||
return url
|
||||
}
|
||||
|
||||
private func file(_ name: String) -> URL { dir.appendingPathComponent(Self.sanitize(name) + ".szt") }
|
||||
|
||||
/// Android's sanitize: anything but letters, digits, `._ -` becomes `_`.
|
||||
static func sanitize(_ name: String) -> String {
|
||||
let ok = Set("ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789._ -")
|
||||
let s = String(name.trimmingCharacters(in: .whitespaces).map { ok.contains($0) ? $0 : "_" })
|
||||
return s.isEmpty ? "theme" : s
|
||||
}
|
||||
|
||||
// MARK: ThemeIo
|
||||
|
||||
static func export(_ p: Palette) -> String {
|
||||
func hex(_ c: UInt32) -> String { String(format: "#%06X", c & 0xFFFFFF) }
|
||||
return [header, "NAME=\(p.name)", "background=\(hex(p.background))", "surface=\(hex(p.surface))",
|
||||
"grid=\(hex(p.grid))", "text=\(hex(p.text))", "textDim=\(hex(p.textDim))", "accent=\(hex(p.accent))",
|
||||
"beat=\(hex(p.beat))", "bar=\(hex(p.bar))", "playhead=\(hex(p.playhead))", "danger=\(hex(p.danger))"]
|
||||
.joined(separator: "\n") + "\n"
|
||||
}
|
||||
|
||||
/// Lenient about single colours (a bad one falls back to the default theme's),
|
||||
/// strict about the header and a name.
|
||||
static func parse(_ text: String) -> Palette? {
|
||||
var map: [String: String] = [:]
|
||||
var sawHeader = false
|
||||
for raw in text.components(separatedBy: .newlines) {
|
||||
let line = raw.trimmingCharacters(in: .whitespaces)
|
||||
if line.isEmpty { continue }
|
||||
if line == header { sawHeader = true; continue }
|
||||
if let eq = line.firstIndex(of: "="), eq > line.startIndex {
|
||||
map[line[..<eq].trimmingCharacters(in: .whitespaces)] = line[line.index(after: eq)...].trimmingCharacters(in: .whitespaces)
|
||||
}
|
||||
}
|
||||
guard sawHeader, let name = map["NAME"], !name.isEmpty else { return nil }
|
||||
let d = Palette.amber
|
||||
func c(_ k: String, _ fb: UInt32) -> UInt32 {
|
||||
guard var h = map[k] else { return fb }
|
||||
if h.hasPrefix("#") { h.removeFirst() }
|
||||
guard h.count == 6, let v = UInt32(h, radix: 16) else { return fb }
|
||||
return v
|
||||
}
|
||||
return Palette(name: name, background: c("background", d.background), surface: c("surface", d.surface),
|
||||
grid: c("grid", d.grid), text: c("text", d.text), textDim: c("textDim", d.textDim),
|
||||
accent: c("accent", d.accent), beat: c("beat", d.beat), bar: c("bar", d.bar),
|
||||
playhead: c("playhead", d.playhead), danger: c("danger", d.danger))
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user