Skip to content

Commit

Permalink
Add support for setting and observing the selected tab
Browse files Browse the repository at this point in the history
  • Loading branch information
david-swift committed Apr 13, 2024
1 parent eca4865 commit 23fd02d
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 2 deletions.
13 changes: 13 additions & 0 deletions Sources/SettingsKit/Components/SettingsKitScene.swift
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@ struct SettingsKitScene<Content>: Scene where Content: Scene {
var design: SettingsWindowDesign
/// The filter in the sidebar design.
@State private var search = ""
/// The binding controlling the selection.
var selectedTab: Binding<String>?

/// The scene.
var body: some Scene {
Expand All @@ -38,6 +40,17 @@ struct SettingsKitScene<Content>: Scene where Content: Scene {
}
}
.symbolVariant(symbolVariant)
.onChange(of: selectedTab?.wrappedValue) { newValue in
if let newValue {
model.selectedTab = newValue
}
}
.onAppear {
model.selectedTab = selectedTab?.wrappedValue ?? ""
}
.onChange(of: model.selectedTab) { newValue in
selectedTab?.wrappedValue = newValue
}
}
}
}
Expand Down
6 changes: 5 additions & 1 deletion Sources/SettingsKit/Model/Extensions/SwiftUI/Scene.swift
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,9 @@ extension Scene {

/// Adds the settings to a scene.
/// - Parameters:
/// - design: Whether the default or sidebar design is used.
/// - symbolVariant: The way symbols should be displayed.
/// - selectedTab: The currently selected tab.
/// - settings: The settings tabs in the settings window.
/// - Returns: The scene with the settings.
///
Expand All @@ -30,6 +32,7 @@ extension Scene {
public func settings(
design: SettingsWindowDesign = .default,
symbolVariant: SymbolVariants = .none,
selectedTab: Binding<String>? = nil,
@ArrayBuilder<SettingsTab> _ settings: () -> [SettingsTab]
) -> some Scene {
let (settings, standardID) = getSettings(settings())
Expand All @@ -38,7 +41,8 @@ extension Scene {
settings: settings,
standardID: standardID,
symbolVariant: symbolVariant,
design: design
design: design,
selectedTab: selectedTab
)
}

Expand Down
2 changes: 2 additions & 0 deletions Tests/TestApp/TestApp.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -331,6 +331,7 @@
ASSETCATALOG_COMPILER_APPICON_NAME = AppIcon;
ASSETCATALOG_COMPILER_GLOBAL_ACCENT_COLOR_NAME = AccentColor;
CODE_SIGN_ENTITLEMENTS = TestApp/TestApp.entitlements;
"CODE_SIGN_IDENTITY[sdk=macosx*]" = "-";
CODE_SIGN_STYLE = Automatic;
CURRENT_PROJECT_VERSION = 1;
DEVELOPMENT_ASSET_PATHS = "\"TestApp/Preview Content\"";
Expand Down Expand Up @@ -370,6 +371,7 @@
ASSETCATALOG_COMPILER_APPICON_NAME = AppIcon;
ASSETCATALOG_COMPILER_GLOBAL_ACCENT_COLOR_NAME = AccentColor;
CODE_SIGN_ENTITLEMENTS = TestApp/TestApp.entitlements;
"CODE_SIGN_IDENTITY[sdk=macosx*]" = "-";
CODE_SIGN_STYLE = Automatic;
CURRENT_PROJECT_VERSION = 1;
DEVELOPMENT_ASSET_PATHS = "\"TestApp/Preview Content\"";
Expand Down
8 changes: 7 additions & 1 deletion Tests/TestApp/TestApp/TestAppApp.swift
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,19 @@ struct TestAppApp: App {
/// Whether the sidebar design should be used for the settings window.
@AppStorage("sidebar-design")
var sidebarDesign = false
/// The currently selected tab.
@AppStorage("tab")
var selectedTab = ""

/// The main view of the test app.
var body: some Scene {
Window("Window", id: "Window") {
ContentView()
Button("Select Test") {
selectedTab = "test"
}
}
.settings(design: sidebarDesign ? .sidebar : .default) {
.settings(design: sidebarDesign ? .sidebar : .default, selectedTab: $selectedTab) {
for settingsTab in appModel.allSettings {
settingsTab
}
Expand Down

0 comments on commit 23fd02d

Please sign in to comment.