diff --git a/Documentation/Reference/SettingsKit/README.md b/Documentation/Reference/SettingsKit/README.md index 2efdb80..fd6c3bd 100644 --- a/Documentation/Reference/SettingsKit/README.md +++ b/Documentation/Reference/SettingsKit/README.md @@ -25,4 +25,4 @@ - [Scene](extensions/Scene.md) - [String](extensions/String.md) -This file was generated by [SourceDocs](https://github.com/eneko/SourceDocs) +This file was generated by [SourceDocs](https://github.com/eneko/SourceDocs) \ No newline at end of file diff --git a/Documentation/Reference/SettingsKit/extensions/ButtonStyle.md b/Documentation/Reference/SettingsKit/extensions/ButtonStyle.md index aa69d69..2eb32cf 100644 --- a/Documentation/Reference/SettingsKit/extensions/ButtonStyle.md +++ b/Documentation/Reference/SettingsKit/extensions/ButtonStyle.md @@ -31,4 +31,4 @@ Button { | Name | Description | | ---- | ----------- | -| isSelected | Whether the image is selected. | +| isSelected | Whether the image is selected. | \ No newline at end of file diff --git a/Documentation/Reference/SettingsKit/structs/SettingsTab.md b/Documentation/Reference/SettingsKit/structs/SettingsTab.md index 7f55064..d08a7d8 100644 --- a/Documentation/Reference/SettingsKit/structs/SettingsTab.md +++ b/Documentation/Reference/SettingsKit/structs/SettingsTab.md @@ -154,6 +154,22 @@ An initializer for a custom settings tav. | id | The identifier. | | content | The content of the custom settings tab. | +### `listContent(subtab:)` + +```swift +private func listContent(subtab: SettingsSubtab) -> some View +``` + +A row in the sidebar list. +- Parameter subtab: The subtab of the row. +- Returns: The row. + +#### Parameters + +| Name | Description | +| ---- | ----------- | +| subtab | The subtab of the row. | + ### `updateSubtabSelection(ids:)` ```swift diff --git a/Sources/SettingsKit/Model/Data/SettingsTab.swift b/Sources/SettingsKit/Model/Data/SettingsTab.swift index f3ace59..92b28ad 100755 --- a/Sources/SettingsKit/Model/Data/SettingsTab.swift +++ b/Sources/SettingsKit/Model/Data/SettingsTab.swift @@ -64,19 +64,26 @@ public struct SettingsTab: Identifiable, View { /// The list in the tab's sidebar. private var sidebarList: some View { - List( - contentWithoutNoSelectionSubtabs, - selection: (model.selectedSubtabs[id] ?? .init()).binding { newValue in - model.selectedSubtabs[id] = newValue - } - ) { subtab in + Group { if #available(macOS 13, *) { - subtab.label - .tag(subtab.id) - .listRowSeparator(.hidden) + let notOptional = model.selectedSubtabs[id] ?? "" + List( + contentWithoutNoSelectionSubtabs, + selection: notOptional.binding { newValue in + model.selectedSubtabs[id] = newValue + } + ) { subtab in + listContent(subtab: subtab) + } } else { - subtab.label - .tag(subtab.id) + List( + contentWithoutNoSelectionSubtabs, + selection: model.selectedSubtabs[id].binding { newValue in + model.selectedSubtabs[id] = newValue + } + ) { subtab in + listContent(subtab: subtab) + } } } .onChange(of: model.selectedSubtabs[id]) { newValue in @@ -129,21 +136,39 @@ public struct SettingsTab: Identifiable, View { self.init(.new(label: label), id: id, content: content) } + /// A row in the sidebar list. + /// - Parameter subtab: The subtab of the row. + /// - Returns: The row. + @ViewBuilder + private func listContent(subtab: SettingsSubtab) -> some View { + if #available(macOS 13, *) { + subtab.label + .tag(subtab.id) + .listRowSeparator(.hidden) + } else { + subtab.label + .tag(subtab.id) + } + } + /// Update the selection of the subtab. /// - Parameter ids: The identifiers of the subtabs. private func updateSubtabSelection(ids: [String]) { - if ids.contains(model.selectedSubtabs[id] ?? ""), let last = ids.last { - model.selectedSubtabs[id] = last - return - } else { + if let first = ids.first(where: { id in + !content.contains { $0.id == id } + }) { + model.selectedSubtabs[id] = first + } else if content.count > ids.count { let index = contentWithoutNoSelectionSubtabs.firstIndex { $0.id == model.selectedSubtabs[id] } - if let before = ids[safe: (index ?? 0) - 1] { - model.selectedSubtabs[id] = before - } else if let after = ids[safe: index ?? ids.count] { + if let after = ids[safe: index ?? ids.count] { model.selectedSubtabs[id] = after + } else if let before = ids[safe: (index ?? 0) - 1] { + model.selectedSubtabs[id] = before } else { model.selectedSubtabs[id] = ids.last ?? "" } + } else if !ids.contains(model.selectedSubtabs[id] ?? ""), let last = ids.last { + model.selectedSubtabs[id] = last } }