Files
VirtualBuddy/VirtualUI/Source/VM Configuration/Components/ConfigurationSection.swift
Guilherme Rambo e19a74866c Liquid glass adoption (#677)
* Raise deployment target to macOS 14, bump version

* Integrate Liquid Glass compatibility components

* Fix onChange(of:) deprecation warnings

* Update project settings

* Improve styling of setup and configuration UI components

* Fix fatal error in VMSavedStatesController.preview

* Tweak session UI

* Do not use prominent button style in VM configuration sheet so it matches overall setup wizard style

* Fix incorrectly sized buttons in grouped lists
2026-05-22 09:47:01 -03:00

96 lines
3.0 KiB
Swift

//
// ConfigurationSection.swift
// VirtualUI
//
// Created by Guilherme Rambo on 18/07/22.
//
import SwiftUI
import VirtualCore
struct ConfigurationSection<Header: View, Content: View>: View {
@Binding private var collapsedStateBinding: Bool
@State private var isCollapsed: Bool
var content: () -> Content
var header: () -> Header
var collapsingDisabled: Bool
init(_ collapsed: Binding<Bool>? = nil, collapsingDisabled: Bool = false, @ViewBuilder _ content: @escaping () -> Content, @ViewBuilder header: @escaping () -> Header) {
if collapsingDisabled {
self._collapsedStateBinding = .constant(false)
self._isCollapsed = .init(wrappedValue: false)
} else {
self._collapsedStateBinding = collapsed ?? .constant(true)
self._isCollapsed = .init(wrappedValue: collapsed?.wrappedValue ?? true)
}
self.collapsingDisabled = collapsingDisabled
self.header = header
self.content = content
}
var body: some View {
VStack(alignment: .leading, spacing: 0) {
styledHeader
if !isCollapsed {
VStack(alignment: .leading, spacing: 6) {
content()
}
.padding()
.transition(.opacity)
}
}
.controlGroup()
.onChange(of: collapsedStateBinding) { _, newValue in
guard newValue != isCollapsed else { return }
isCollapsed = newValue
}
.onChange(of: isCollapsed) { _, newValue in
guard collapsedStateBinding != newValue else { return }
collapsedStateBinding = newValue
}
}
@ViewBuilder
private var styledHeader: some View {
HStack {
header()
.frame(maxWidth: .infinity, alignment: .leading)
Spacer()
if !collapsingDisabled {
Image(systemName: "chevron.down")
.rotationEffect(.degrees(isCollapsed ? -90 : 0))
}
}
.font(.system(size: 14, weight: .medium, design: .rounded))
.padding(.horizontal)
.padding(.vertical, 12)
.frame(maxWidth: .infinity, alignment: .leading)
.background {
if isCollapsed {
ContainerRelativeShape().inset(by: 2).foregroundStyle(Material.ultraThin)
}
}
.overlay(alignment: .bottom) {
Rectangle()
.frame(maxWidth: .infinity, maxHeight: 0.5)
.foregroundColor(.white.opacity(isCollapsed ? 0 : 0.1))
.padding(.horizontal, 1)
.blendMode(.plusLighter)
}
.contentShape(ContainerRelativeShape())
.onTapGesture {
guard !collapsingDisabled else { return }
withAnimation(.default) {
isCollapsed.toggle()
}
}
}
}