Skip to content

Commit

Permalink
Improve the subtab selection behavior
Browse files Browse the repository at this point in the history
  • Loading branch information
david-swift committed Aug 1, 2023
1 parent 76b7449 commit 917c80f
Show file tree
Hide file tree
Showing 4 changed files with 61 additions and 20 deletions.
2 changes: 1 addition & 1 deletion Documentation/Reference/SettingsKit/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Original file line number Diff line number Diff line change
Expand Up @@ -31,4 +31,4 @@ Button {

| Name | Description |
| ---- | ----------- |
| isSelected | Whether the image is selected. |
| isSelected | Whether the image is selected. |
16 changes: 16 additions & 0 deletions Documentation/Reference/SettingsKit/structs/SettingsTab.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
61 changes: 43 additions & 18 deletions Sources/SettingsKit/Model/Data/SettingsTab.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
}
}

Expand Down

0 comments on commit 917c80f

Please sign in to comment.