macOS: fix config form rendering

This commit is contained in:
Shadow
2026-01-16 16:19:42 -06:00
committed by Peter Steinberger
parent cc2d617ea6
commit 3ec221c70e
3 changed files with 89 additions and 83 deletions

View File

@@ -9,14 +9,14 @@ struct ConfigSchemaForm: View {
self.renderNode(schema, path: path) self.renderNode(schema, path: path)
} }
@ViewBuilder private func renderNode(_ schema: ConfigSchemaNode, path: ConfigPath) -> AnyView {
private func renderNode(_ schema: ConfigSchemaNode, path: ConfigPath) -> some View {
let value = store.configValue(at: path) let value = store.configValue(at: path)
let label = hintForPath(path, hints: store.configUiHints)?.label ?? schema.title let label = hintForPath(path, hints: store.configUiHints)?.label ?? schema.title
let help = hintForPath(path, hints: store.configUiHints)?.help ?? schema.description let help = hintForPath(path, hints: store.configUiHints)?.help ?? schema.description
switch schema.schemaType { switch schema.schemaType {
case "object": case "object":
return AnyView(
VStack(alignment: .leading, spacing: 12) { VStack(alignment: .leading, spacing: 12) {
if let label { if let label {
Text(label) Text(label)
@@ -43,24 +43,29 @@ struct ConfigSchemaForm: View {
self.renderAdditionalProperties(schema, path: path, value: value) self.renderAdditionalProperties(schema, path: path, value: value)
} }
} }
)
case "array": case "array":
self.renderArray(schema, path: path, value: value, label: label, help: help) return AnyView(self.renderArray(schema, path: path, value: value, label: label, help: help))
case "boolean": case "boolean":
return AnyView(
Toggle(isOn: self.boolBinding(path)) { Toggle(isOn: self.boolBinding(path)) {
if let label { Text(label) } else { Text("Enabled") } if let label { Text(label) } else { Text("Enabled") }
} }
.help(help ?? "") .help(help ?? "")
)
case "number", "integer": case "number", "integer":
self.renderNumberField(schema, path: path, label: label, help: help) return AnyView(self.renderNumberField(schema, path: path, label: label, help: help))
case "string": case "string":
self.renderStringField(schema, path: path, label: label, help: help) return AnyView(self.renderStringField(schema, path: path, label: label, help: help))
default: default:
return AnyView(
VStack(alignment: .leading, spacing: 6) { VStack(alignment: .leading, spacing: 6) {
if let label { Text(label).font(.callout.weight(.semibold)) } if let label { Text(label).font(.callout.weight(.semibold)) }
Text("Unsupported field type.") Text("Unsupported field type.")
.font(.caption) .font(.caption)
.foregroundStyle(.secondary) .foregroundStyle(.secondary)
} }
)
} }
} }
@@ -171,7 +176,7 @@ struct ConfigSchemaForm: View {
path: ConfigPath, path: ConfigPath,
value: Any?) -> some View value: Any?) -> some View
{ {
guard let additionalSchema = schema.additionalProperties else { return } if let additionalSchema = schema.additionalProperties {
let dict = value as? [String: Any] ?? [:] let dict = value as? [String: Any] ?? [:]
let reserved = Set(schema.properties.keys) let reserved = Set(schema.properties.keys)
let extras = dict.keys.filter { !reserved.contains($0) }.sorted() let extras = dict.keys.filter { !reserved.contains($0) }.sorted()
@@ -216,6 +221,7 @@ struct ConfigSchemaForm: View {
.controlSize(.small) .controlSize(.small)
} }
} }
}
private func stringBinding(_ path: ConfigPath) -> Binding<String> { private func stringBinding(_ path: ConfigPath) -> Binding<String> {
Binding( Binding(

View File

@@ -65,9 +65,9 @@ extension ChannelsStore {
} }
func updateConfigValue(path: ConfigPath, value: Any?) { func updateConfigValue(path: ConfigPath, value: Any?) {
var root = self.configDraft var root: Any = self.configDraft
setValue(&root, path: path, value: value) setValue(&root, path: path, value: value)
self.configDraft = root self.configDraft = root as? [String: Any] ?? self.configDraft
self.configDirty = true self.configDirty = true
} }
@@ -125,7 +125,7 @@ private func setValue(_ root: inout Any, path: ConfigPath, value: Any?) {
case .index(let index): case .index(let index):
var array = root as? [Any] ?? [] var array = root as? [Any] ?? []
if index >= array.count { if index >= array.count {
array.append(contentsOf: repeatElement(NSNull(), count: index - array.count + 1)) array.append(contentsOf: repeatElement(NSNull() as Any, count: index - array.count + 1))
} }
if path.count == 1 { if path.count == 1 {
if let value { if let value {

View File

@@ -900,7 +900,7 @@ extension DebugSettings {
} }
} }
private struct PlainSettingsGroupBoxStyle: GroupBoxStyle { struct PlainSettingsGroupBoxStyle: GroupBoxStyle {
func makeBody(configuration: Configuration) -> some View { func makeBody(configuration: Configuration) -> some View {
VStack(alignment: .leading, spacing: 10) { VStack(alignment: .leading, spacing: 10) {
configuration.label configuration.label