Skip to content

Commit

Permalink
Remove the keyboard shortcut functions, improve the subtab selection …
Browse files Browse the repository at this point in the history
…and update the docs
  • Loading branch information
david-swift committed Jul 31, 2023
1 parent 5b670db commit 76b7449
Show file tree
Hide file tree
Showing 19 changed files with 129 additions and 143 deletions.
3 changes: 1 addition & 2 deletions Documentation/Reference/SettingsKit/README.md
100755 → 100644
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

## Structs

- [SettingsCommands](structs/SettingsCommands.md)
- [SettingsImageButtonStyle](structs/SettingsImageButtonStyle.md)
- [SettingsKitScene](structs/SettingsKitScene.md)
- [SettingsSubtab](structs/SettingsSubtab.md)
Expand All @@ -26,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)
Empty file modified Documentation/Reference/SettingsKit/classes/SettingsModel.md
100755 → 100644
Empty file.
4 changes: 4 additions & 0 deletions Documentation/Reference/SettingsKit/enums/SettingsAction.md
100755 → 100644
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,10 @@ public static func showSettings(tab: String? = nil, subtab: String? = nil)
```

Show the settings window.

This function does no longer work in macOS 14 or higher.
Please use the `SettingsLink` view for adding a button for opening the settings.

- Parameters:
- tab: The identifier of the new tab selection.
- subtab: The identifier of the new subtab selection.
Expand Down
Empty file modified Documentation/Reference/SettingsKit/enums/TabType.md
100755 → 100644
Empty file.
2 changes: 1 addition & 1 deletion Documentation/Reference/SettingsKit/extensions/ButtonStyle.md
100755 → 100644
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. |
Empty file modified Documentation/Reference/SettingsKit/extensions/CGFloat.md
100755 → 100644
Empty file.
Empty file modified Documentation/Reference/SettingsKit/extensions/Double.md
100755 → 100644
Empty file.
Empty file.
Empty file modified Documentation/Reference/SettingsKit/extensions/Scene.md
100755 → 100644
Empty file.
Empty file modified Documentation/Reference/SettingsKit/extensions/String.md
100755 → 100644
Empty file.
102 changes: 0 additions & 102 deletions Documentation/Reference/SettingsKit/structs/SettingsCommands.md

This file was deleted.

Empty file.
Empty file modified Documentation/Reference/SettingsKit/structs/SettingsKitScene.md
100755 → 100644
Empty file.
11 changes: 1 addition & 10 deletions Documentation/Reference/SettingsKit/structs/SettingsSubtab.md
100755 → 100644
Original file line number Diff line number Diff line change
Expand Up @@ -97,13 +97,4 @@ An initializer for a custom settings subtab.
| ---- | ----------- |
| label | The label of the custom settings subtab. |
| id | The identifier. |
| content | The content of the custom settings subtab. |

### `shortcut()`

```swift
public func shortcut() -> Self
```

Adds the settings shortcut to the subtab and returns the new one.
- Returns: The new subtab with the shortcut.
| content | The content of the custom settings subtab. |
94 changes: 89 additions & 5 deletions Documentation/Reference/SettingsKit/structs/SettingsTab.md
100755 → 100644
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,30 @@ public var sidebarActions: [ToolbarGroup]

The sidebar actions view.

### `windowWidth`

```swift
public var windowWidth: CGFloat? = .settingsWidth
```

The settings window's width.

### `windowHeight`

```swift
public var windowHeight: CGFloat? = .settingsHeight
```

The settings window's height.

### `contentWithoutNoSelectionSubtabs`

```swift
private var contentWithoutNoSelectionSubtabs: [SettingsSubtab]
```

The tab's content, but without the subtabs with the ``TabType.noSelection`` type.

### `body`

```swift
Expand Down Expand Up @@ -130,14 +154,20 @@ An initializer for a custom settings tav.
| id | The identifier. |
| content | The content of the custom settings tab. |

### `shortcut()`
### `updateSubtabSelection(ids:)`

```swift
public func shortcut() -> Self
private func updateSubtabSelection(ids: [String])
```

Adds the settings shortcut to the first subtab.
- Returns: The new tab with the shortcut.
Update the selection of the subtab.
- Parameter ids: The identifiers of the subtabs.

#### Parameters

| Name | Description |
| ---- | ----------- |
| ids | The identifiers of the subtabs. |

### `actions(content:)`

Expand Down Expand Up @@ -195,4 +225,58 @@ The standard set of actions with an add button, a remove button and optionally a
| ---- | ----------- |
| add | The action that is called when the add button is pressed. |
| remove | The action that is called when the remove button is pressed, giving the the selected subtab’s index. |
| options | The action that is called when the options button is pressed. If it is nil, there is no options button. |
| options | The action that is called when the options button is pressed. If it is nil, there is no options button. |

### `frame(width:height:)`

```swift
public func frame(width: CGFloat? = nil, height: CGFloat? = nil) -> Self
```

Set the window's width and height when this tab is open.
This is being ignored if there is more than one subtab or if there are settings actions.
- Parameters:
- width: The width. If nil, the window uses the content's width.
- height: The height. If nil, the window uses the content's height.
- Returns: The settings tab with the new window size.

#### Parameters

| Name | Description |
| ---- | ----------- |
| width | The width. If nil, the window uses the content’s width. |
| height | The height. If nil, the window uses the content’s height. |

### `width(_:)`

```swift
public func width(_ width: CGFloat? = nil) -> Self
```

Set the window's width when this tab is open without affecting the height.
This is being ignored if there is more than one subtab or if there are settings actions.
- Parameter width: The width. If nil, the window uses the content's width.
- Returns: The settings tab with the new window size.

#### Parameters

| Name | Description |
| ---- | ----------- |
| width | The width. If nil, the window uses the content’s width. |

### `height(_:)`

```swift
public func height(_ height: CGFloat? = nil) -> Self
```

Set the window's height when this tab is open without affecting the width.
This is being ignored if there is more than one subtab or if there are settings actions.
- Parameter height: The height. If nil, the window uses the content's height.
- Returns: The settings tab with the new window size.

#### Parameters

| Name | Description |
| ---- | ----------- |
| height | The height. If nil, the window uses the content’s height. |
7 changes: 2 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -73,8 +73,6 @@ struct SuperCoolApp: App {
ComposerSettings()
}
}
// 2.1 - Add a shortcut to the "General" settings tab.
.shortcut()

// 1.3 - An extension for the "General" settings tab.
SettingsTab(.extend(id: "general-tab"), id: "general-tab-extension") {
Expand All @@ -93,7 +91,7 @@ struct SuperCoolApp: App {
}
}
}
// 2.2 - The "+" and "-" buttons in the sidebar's toolbar.
// 2.1 - The "+" and "-" buttons in the sidebar's toolbar.
.standardActions {
themes.append(.init())
} remove: { index in
Expand All @@ -112,8 +110,7 @@ struct SuperCoolApp: App {
- `1.2` - Create a new settings subtab with the initializer for `SettingsSubtab` inside a `SettingsTab` initializer.
- `1.3` - Extend an existing tab by using `.extend(id:)` and providing the ID of the settings tab to extend.
- `1.4` - Create a new settings subtab by using `.noSelection` that is displayed if there is not a selected subtab.
- `2.1` - Use the `.shortcut()` modifier on a settings tab or subtab so that this tab or subtab is opened when the user presses `⌘,`.
- `2.2` - Use the `.standardActions(add:remove:options:)` modifier on a settings tab for adding a „+“, „-„ and optionally „⋯“ toolbar button to the sidebar. You can also add a custom toolbar with `.actions(content:)`.
- `2.1` - Use the `.standardActions(add:remove:options:)` modifier on a settings tab for adding a „+“, „-„ and optionally „⋯“ toolbar button to the sidebar. You can also add a custom toolbar with `.actions(content:)`.

## Thanks

Expand Down
8 changes: 0 additions & 8 deletions Sources/SettingsKit/Model/Data/SettingsSubtab.swift
Original file line number Diff line number Diff line change
Expand Up @@ -55,12 +55,4 @@ public struct SettingsSubtab: Identifiable, View {
self.init(.new(label: label), id: id, content: content)
}

/// Adds the settings shortcut to the subtab and returns the new one.
/// - Returns: The new subtab with the shortcut.
public func shortcut() -> Self {
var newSubtab = self
newSubtab.standard = true
return newSubtab
}

}
38 changes: 30 additions & 8 deletions Sources/SettingsKit/Model/Data/SettingsTab.swift
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,11 @@ public struct SettingsTab: Identifiable, View {
/// The settings window's height.
public var windowHeight: CGFloat? = .settingsHeight

/// The tab's content, but without the subtabs with the ``TabType.noSelection`` type.
private var contentWithoutNoSelectionSubtabs: [SettingsSubtab] {
content.filter { !$0.type.isNoSelection }
}

/// The view containing all the subtabs.
public var body: some View {
if content.count <= 1 && sidebarActions.isEmpty {
Expand Down Expand Up @@ -60,7 +65,7 @@ public struct SettingsTab: Identifiable, View {
/// The list in the tab's sidebar.
private var sidebarList: some View {
List(
content,
contentWithoutNoSelectionSubtabs,
selection: (model.selectedSubtabs[id] ?? .init()).binding { newValue in
model.selectedSubtabs[id] = newValue
}
Expand All @@ -74,12 +79,19 @@ public struct SettingsTab: Identifiable, View {
.tag(subtab.id)
}
}
.onChange(of: model.selectedSubtabs[id]) { newValue in
if !contentWithoutNoSelectionSubtabs.contains(where: { $0.id == newValue }) {
updateSubtabSelection(ids: contentWithoutNoSelectionSubtabs.map { $0.id })
}
}
.onChange(of: contentWithoutNoSelectionSubtabs.map { $0.id }) { updateSubtabSelection(ids: $0) }
.onAppear { updateSubtabSelection(ids: contentWithoutNoSelectionSubtabs.map { $0.id }) }
}

/// The selected subtab's content.
private var contentView: some View {
Form {
if let first = content.first(where: { $0.id == model.selectedSubtabs[id] }) {
if let first = contentWithoutNoSelectionSubtabs.first(where: { $0.id == model.selectedSubtabs[id] }) {
first
} else {
content.first { $0.type.isNoSelection }
Expand Down Expand Up @@ -117,12 +129,22 @@ public struct SettingsTab: Identifiable, View {
self.init(.new(label: label), id: id, content: content)
}

/// Adds the settings shortcut to the first subtab.
/// - Returns: The new tab with the shortcut.
public func shortcut() -> Self {
var newTab = self
newTab.content[safe: 0] = newTab.content.first?.shortcut()
return newTab
/// 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 {
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] {
model.selectedSubtabs[id] = after
} else {
model.selectedSubtabs[id] = ids.last ?? ""
}
}
}

/// Adds actions to the settings sidebar.
Expand Down
3 changes: 1 addition & 2 deletions Tests/TestApp/TestApp/ViewModel/TestAppModel.swift
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,6 @@ class TestAppModel: ObservableObject {
}
noSelection
}
.shortcut()
.standardActions {
self.settings.append(self.newTab)
} remove: { id in
Expand Down Expand Up @@ -63,7 +62,7 @@ class TestAppModel: ObservableObject {
.init(.noSelection, id: "no-selection-settings") {
Text(
.init(
"Select a tab in the sidebar or add a new one with the \"+\" button.",
"Add a new tab with the \"+\" button.",
comment: "TestAppModel (Description of a view without a selection for testing purposes)"
)
)
Expand Down

0 comments on commit 76b7449

Please sign in to comment.