feat: configure explicit state-dependent player keybindings
This commit is contained in:
+143
-17
@@ -11,18 +11,96 @@ struct ConfigurationChecks {
|
||||
if !condition { throw Failure(description: "FAIL: " + message) }
|
||||
}
|
||||
|
||||
static func requireLoadError(at url: URL, _ message: String) throws {
|
||||
static func requireLoadError(at url: URL, _ message: String, containing fragments: [String] = []) throws {
|
||||
do {
|
||||
_ = try PlayerConfiguration.load(from: url)
|
||||
} catch let caught {
|
||||
let diagnostic = caught.localizedDescription + " " + String(describing: caught)
|
||||
try require(diagnostic.contains(url.path), message + " identifies the actual config path")
|
||||
for fragment in fragments {
|
||||
try require(diagnostic.contains(fragment), message + " identifies '\(fragment)': " + diagnostic)
|
||||
}
|
||||
return
|
||||
}
|
||||
throw Failure(description: "FAIL: " + message + " was accepted")
|
||||
}
|
||||
|
||||
static func requireError(_ fragments: [String], _ operation: () throws -> Void) throws {
|
||||
do { try operation() }
|
||||
catch {
|
||||
for fragment in fragments {
|
||||
try require(error.localizedDescription.contains(fragment), "error identifies '\(fragment)': \(error)")
|
||||
}
|
||||
return
|
||||
}
|
||||
throw Failure(description: "FAIL: invalid binding was accepted")
|
||||
}
|
||||
|
||||
static func checkBindings() throws {
|
||||
let bindings = KeyBindings()
|
||||
let inversePairs: [(PlayerAction, PlayerAction)] = [
|
||||
(.play, .pause), (.mute, .unmute), (.enterFullscreen, .exitFullscreen),
|
||||
(.enterCompact, .exitCompact), (.enableAlwaysOnTop, .disableAlwaysOnTop),
|
||||
(.showBufferInfo, .hideBufferInfo), (.enableCompactSnapping, .disableCompactSnapping),
|
||||
(.enableAutomaticQuality, .disableAutomaticQuality)
|
||||
]
|
||||
for (first, second) in inversePairs {
|
||||
try require(first.inverse == second && second.inverse == first, "symmetric inverses")
|
||||
let overrides = try KeyBindings(overrides: [first.name: ["ctrl+alt+x"], second.name: ["ctrl+alt+x"]])
|
||||
try require(Set(overrides.actions(for: try KeyChord("ctrl+alt+x"))) == [first, second],
|
||||
"every known inverse pair may share a chord")
|
||||
}
|
||||
for height in [1, 144, 720, 1080, 2160, 4320] {
|
||||
let action = PlayerAction.quality(height)
|
||||
try require(PlayerAction(name: "quality_\(height)p") == action && action.name == "quality_\(height)p",
|
||||
"explicit positive quality height round-trips")
|
||||
try require(action.inverse == nil && !action.allowsRepeat && bindings.shortcuts(for: action).isEmpty,
|
||||
"quality actions default to unbound and non-repeating")
|
||||
}
|
||||
for name in ["", "Play", "toggle_play", "toggle_fullscreen", "captions", "enable_captions", "disable_captions",
|
||||
"quality_0p", "quality_-720p", "quality_0720p", "quality_720", "quality_720P", "quality_7.2p",
|
||||
"quality_999999999999999999999999999p"] {
|
||||
try require(PlayerAction(name: name) == nil, "unsupported action \(name) rejected")
|
||||
try requireError(name.isEmpty ? [] : [name]) { _ = try KeyBindings(overrides: [name: []]) }
|
||||
}
|
||||
let normalized = try KeyChord("SHIFT+CMD+Option+CTRL+A")
|
||||
try require(normalized == KeyChord(key: "a", modifiers: [.shift, .command, .option, .control]),
|
||||
"letter casing and modifier order normalize")
|
||||
try require(try KeyChord(normalized.description) == normalized, "canonical description round-trips")
|
||||
try require(try KeyChord("option+x") == KeyChord("alt+x"), "option alias normalizes")
|
||||
try require(try KeyChord("plus").key == "+" && KeyChord("cmd+plus").description == "cmd+plus",
|
||||
"named plus identifies the printable + key")
|
||||
for key in ["space", "escape", "return", "tab", "backspace", "delete", "left", "right", "up", "down",
|
||||
"home", "end", "pageup", "pagedown", ".", ",", "é"] + (1...35).map({ "f\($0)" }) {
|
||||
let chord = try KeyChord(key)
|
||||
try require(try KeyChord(chord.description) == chord, "\(key) parses and round-trips")
|
||||
}
|
||||
for chord in ["", "+", "cmd+", "+a", "cmd++a", "cmd+cmd+a", "alt+option+a", "super+a",
|
||||
"cmd+wat", "f0", "f36", "f01", " ", "\n", "a+b", "media_play", "cmd+tab", "cmd+shift+tab"] {
|
||||
try requireError(chord.isEmpty ? [] : [chord]) { _ = try KeyChord(chord) }
|
||||
}
|
||||
let merged = try KeyBindings(overrides: ["play": ["x", "ctrl+x"], "pause": ["x"], "mute": [],
|
||||
"quality_720p": ["alt+7"], "quality_default": ["alt+0"]])
|
||||
try require(merged.shortcuts(for: .play) == [KeyChord("x"), KeyChord("ctrl+x")], "array replaces, not appends")
|
||||
try require(merged.shortcuts(for: .mute).isEmpty && merged.actions(for: KeyChord("m")) == [.unmute],
|
||||
"empty array unbinds only its action")
|
||||
try require(merged.actions(for: KeyChord("k")).isEmpty && merged.shortcuts(for: .openFile) == bindings.shortcuts(for: .openFile),
|
||||
"replaced shortcuts disappear while omitted actions retain defaults")
|
||||
try require(merged.actions(for: KeyChord("alt+7")) == [.quality(720)]
|
||||
&& merged.actions(for: KeyChord("alt+0")) == [.qualityDefault], "quality shortcuts indexed")
|
||||
try require(bindings.actions(for: KeyChord("cmd+k")).isEmpty, "modifiers match exactly")
|
||||
try require(try KeyBindings(overrides: ["show_buffer_info": []]) == bindings, "unbinding an already-unbound action is unchanged")
|
||||
try requireError(["play", "k"]) { _ = try KeyBindings(overrides: ["play": ["k", "K"]]) }
|
||||
try requireError(["play", "ctrl+cmd+x"]) { _ = try KeyBindings(overrides: ["play": ["ctrl+cmd+x", "cmd+ctrl+X"]]) }
|
||||
try requireError(["play", "quit", "q"]) { _ = try KeyBindings(overrides: ["play": ["q"]]) }
|
||||
try requireError(["play", "mute", "x"]) { _ = try KeyBindings(overrides: ["play": ["x"], "mute": ["x"]]) }
|
||||
try requireError(["quality_720p", "quality_1080p", "alt+7"]) {
|
||||
_ = try KeyBindings(overrides: ["quality_720p": ["alt+7"], "quality_1080p": ["alt+7"]])
|
||||
}
|
||||
}
|
||||
|
||||
static func main() throws {
|
||||
try checkBindings()
|
||||
let files = FileManager.default
|
||||
let root = files.temporaryDirectory.appendingPathComponent("terebi.configuration-check.\(UUID().uuidString)")
|
||||
try files.createDirectory(at: root, withIntermediateDirectories: true)
|
||||
@@ -47,16 +125,18 @@ struct ConfigurationChecks {
|
||||
|
||||
try files.createDirectory(at: config.deletingLastPathComponent(), withIntermediateDirectories: true)
|
||||
let original = Data("""
|
||||
# Root keys can be quoted, with comments and whitespace.
|
||||
"show_buffer_info" = true # session display default
|
||||
'automatic_quality' = true
|
||||
snap_to_edges = false
|
||||
# Root and nested keys can be quoted, with comments and whitespace.
|
||||
'automatic_quality' = true
|
||||
[ui]
|
||||
"buffer_info" = true # session display default
|
||||
compact_snap_to_edges = false
|
||||
""".utf8)
|
||||
try original.write(to: config)
|
||||
try files.setAttributes([.posixPermissions: 0o444], ofItemAtPath: config.path)
|
||||
let configured = PlayerConfiguration(showBufferInfo: true, automaticQuality: true, snapToEdges: false)
|
||||
let environment = ["XDG_CONFIG_HOME": xdg.path]
|
||||
try require(try PlayerConfiguration.load(from: config) == configured,
|
||||
"TOML comments, whitespace, quoted root keys, and both boolean values load")
|
||||
"TOML comments, whitespace, quoted nested keys, and both boolean values load from a read-only file")
|
||||
var session = try PlayerConfiguration.loadForApplication(arguments: [], environment: environment, homeDirectory: home)
|
||||
try require(session == configured, "application startup automatically loads the XDG config")
|
||||
session.showBufferInfo = false
|
||||
@@ -76,21 +156,67 @@ struct ConfigurationChecks {
|
||||
try require(try PlayerConfiguration.loadForApplication(arguments: ["--config-file", partial.absoluteString],
|
||||
environment: environment, homeDirectory: home)
|
||||
== PlayerConfiguration(automaticQuality: true), "explicit file URL overrides the resolved XDG config")
|
||||
try require(try Data(contentsOf: config) == original, "read-only file bytes remain unchanged")
|
||||
let nested = root.appendingPathComponent("nested.toml")
|
||||
try Data("""
|
||||
[ui]
|
||||
buffer_info = true
|
||||
[keys]
|
||||
play = "x"
|
||||
pause = ["x", "ctrl+x"]
|
||||
mute = []
|
||||
quality_1080p = "alt+1"
|
||||
show_buffer_info = "alt+b"
|
||||
hide_buffer_info = "alt+b"
|
||||
""".utf8).write(to: nested)
|
||||
let nestedConfiguration = try PlayerConfiguration.load(from: nested)
|
||||
try require(nestedConfiguration.showBufferInfo && nestedConfiguration.snapToEdges && !nestedConfiguration.automaticQuality,
|
||||
"partial nested UI keeps omitted root and UI defaults")
|
||||
let expectedKeys = try KeyBindings(overrides: ["play": ["x"], "pause": ["x", "ctrl+x"], "mute": [],
|
||||
"quality_1080p": ["alt+1"], "show_buffer_info": ["alt+b"],
|
||||
"hide_buffer_info": ["alt+b"]])
|
||||
try require(nestedConfiguration.keys == expectedKeys, "heterogeneous string, array, and empty shortcuts decode")
|
||||
var keySession = nestedConfiguration
|
||||
keySession.keys = KeyBindings()
|
||||
try require(try PlayerConfiguration.load(from: nested).keys == expectedKeys && keySession.keys == defaults.keys,
|
||||
"key session overrides never persist")
|
||||
let emptySections = root.appendingPathComponent("empty-sections.toml")
|
||||
try Data("[ui]\n[keys]\n".utf8).write(to: emptySections)
|
||||
try require(try PlayerConfiguration.load(from: emptySections) == defaults, "empty nested sections use defaults")
|
||||
|
||||
let invalid: [(String, String)] = [
|
||||
("syntax", "show_buffer_info = [\n"),
|
||||
("duplicate", "show_buffer_info = true\n\"show_buffer_info\" = false\n"),
|
||||
("unknown", "unknown_setting = true\n"),
|
||||
("string", "show_buffer_info = \"true\"\n"),
|
||||
("integer", "automatic_quality = 1\n"),
|
||||
("array", "snap_to_edges = [true]\n"),
|
||||
("table", "[show_buffer_info]\nenabled = true\n")
|
||||
let invalid: [(String, String, [String])] = [
|
||||
("syntax", "automatic_quality = [\n", ["automatic_quality"]),
|
||||
("duplicate", "automatic_quality = true\n\"automatic_quality\" = false\n", ["automatic_quality"]),
|
||||
("unknown", "unknown_setting = true\n", ["unknown_setting"]),
|
||||
("legacy-buffer", "show_buffer_info = true\n", ["show_buffer_info"]),
|
||||
("legacy-snap", "snap_to_edges = false\n", ["snap_to_edges"]),
|
||||
("ui-unknown", "[ui]\nunknown_setting = true\n", ["ui.unknown_setting"]),
|
||||
("ui-legacy", "[ui]\nshow_buffer_info = true\n", ["ui.show_buffer_info"]),
|
||||
("string", "[ui]\nbuffer_info = \"true\"\n", []),
|
||||
("integer", "automatic_quality = 1\n", []),
|
||||
("array", "[ui]\ncompact_snap_to_edges = [true]\n", []),
|
||||
("table", "[automatic_quality]\nenabled = true\n", ["automatic_quality"]),
|
||||
("ui-scalar", "ui = true\n", ["ui"]),
|
||||
("keys-scalar", "keys = true\n", ["keys"]),
|
||||
("key-bool", "[keys]\nplay = true\n", ["keys.play"]),
|
||||
("key-int", "[keys]\nplay = 42\n", ["keys.play"]),
|
||||
("key-mixed-array", "[keys]\nplay = [\"x\", 42]\n", ["play"]),
|
||||
("key-table", "[keys.play]\nkey = \"x\"\n", ["keys.play"]),
|
||||
("key-unknown", "[keys]\nunknown_action = \"x\"\n", ["keys.unknown_action"]),
|
||||
("key-toggle", "[keys]\ntoggle_play = \"x\"\n", ["keys.toggle_play"]),
|
||||
("key-captions", "[keys]\nenable_captions = []\n", ["keys.enable_captions"]),
|
||||
("key-quality", "[keys]\nquality_0p = \"alt+0\"\n", ["keys.quality_0p"]),
|
||||
("key-chord", "[keys]\nplay = \"cmd++x\"\n", ["keys.play", "cmd++x"]),
|
||||
("key-duplicate", "[keys]\nplay = [\"x\", \"X\"]\n", ["keys.play", "duplicate", "X"]),
|
||||
("key-default-conflict", "[keys]\nplay = \"q\"\n", ["keys.play", "keys.quit", "q"]),
|
||||
("key-explicit-conflict", "[keys]\nplay = \"x\"\nmute = \"x\"\n", ["keys.play", "keys.mute", "x"]),
|
||||
("key-reserved", "[keys]\nplay = \"cmd+tab\"\n", ["keys.play", "cmd+tab", "unsupported"])
|
||||
]
|
||||
for (name, text) in invalid {
|
||||
for (name, text, fragments) in invalid {
|
||||
let url = root.appendingPathComponent(name + ".toml")
|
||||
let bytes = Data(text.utf8)
|
||||
try bytes.write(to: url)
|
||||
try requireLoadError(at: url, "invalid \(name) configuration")
|
||||
try requireLoadError(at: url, "invalid \(name) configuration", containing: fragments)
|
||||
try require(try Data(contentsOf: url) == bytes, "rejected \(name) config remains unchanged")
|
||||
}
|
||||
let invalidUTF8 = root.appendingPathComponent("invalid-utf8.toml")
|
||||
@@ -112,7 +238,7 @@ struct ConfigurationChecks {
|
||||
let badXDG = root.appendingPathComponent("bad-xdg")
|
||||
let badConfig = badXDG.appendingPathComponent("terebi/config.toml")
|
||||
try files.createDirectory(at: badConfig.deletingLastPathComponent(), withIntermediateDirectories: true)
|
||||
let malformedBytes = Data("snap_to_edges =\n".utf8)
|
||||
let malformedBytes = Data("[ui]\ncompact_snap_to_edges =\n".utf8)
|
||||
try malformedBytes.write(to: badConfig)
|
||||
try require(try PlayerConfiguration.loadForApplication(arguments: ["--no-config"],
|
||||
environment: ["XDG_CONFIG_HOME": badXDG.path], homeDirectory: home) == defaults,
|
||||
|
||||
Reference in New Issue
Block a user