Files
Guilherme Rambo 3495fd0449 Improve USB device UI (#754)
* Improve USB device setup UI

* Allow adding multiple host USB devices simultaneously, double-click to instantly add a device from the list

* Use USBIDKit database to retrieve friendly device information when manually declared

* Add note about USB devices with isochronous endpoints
2026-08-27 15:26:39 -03:00

166 lines
5.5 KiB
Swift

//
// GroupedList.swift
// VirtualUI
//
// Created by Guilherme Rambo on 19/07/22.
//
import SwiftUI
struct GroupedList<Content: View, HeaderAccessory: View, FooterAccessory: View, EmptyOverlay: View, AddButton: View, RemoveButton: View>: View {
var content: () -> Content
var headerAccessory: () -> HeaderAccessory
var footerAccessory: () -> FooterAccessory
var emptyOverlay: () -> EmptyOverlay
var addButton: (Label<Text, Image>) -> AddButton?
var removeButton: (Label<Text, Image>) -> RemoveButton?
init(@ViewBuilder _ content: @escaping () -> Content,
headerAccessory: @escaping () -> HeaderAccessory,
footerAccessory: @escaping () -> FooterAccessory,
emptyOverlay: @escaping () -> EmptyOverlay,
addButton: @escaping (Label<Text, Image>) -> AddButton? = { _ in nil },
removeButton: @escaping (Label<Text, Image>) -> RemoveButton? = { _ in nil })
{
self.content = content
self.headerAccessory = headerAccessory
self.footerAccessory = footerAccessory
self.emptyOverlay = emptyOverlay
self.addButton = addButton
self.removeButton = removeButton
}
var body: some View {
VStack(alignment: .leading, spacing: 8) {
header
content()
.listStyle(.plain)
.frame(minHeight: 140)
.overlay { emptyOverlayContents }
.safeAreaInset(edge: .bottom, alignment: .leading, spacing: 0, content: {
listButtons
})
.airMaterialBackground(
visualEffect: .contentBackground,
glassEffect: nil,
in: listShape
)
.controlGroup(level: .secondary)
}
}
@ViewBuilder
private var emptyOverlayContents: some View {
VStack {
emptyOverlay()
}
.buttonStyle(.link)
.frame(maxWidth: .infinity)
.controlSize(.small)
}
private var listRadius: CGFloat { 8 }
private var listShape: some InsettableShape {
RoundedRectangle(cornerRadius: listRadius, style: .continuous)
}
@State private var showTip = false
@ViewBuilder
private var header: some View {
headerAccessory()
.padding(.horizontal, 2)
}
private let addLabel = Label("Add", systemImage: "plus")
private let removeLabel = Label("Remove", systemImage: "minus")
@ViewBuilder
private var listButtons: some View {
if addButton(addLabel) != nil || removeButton(removeLabel) != nil || FooterAccessory.self != EmptyView.self {
HStack {
Group {
if let addButton = addButton(addLabel) {
addButton
}
if let removeButton = removeButton(removeLabel) {
removeButton
.keyboardShortcut(.delete)
}
}
.labelStyle(.iconOnly)
.applyGroupedListCommandButtonStyle()
footerAccessory()
}
.padding(8)
.frame(maxWidth: .infinity, alignment: .leading)
.background(Material.thick, in: Rectangle())
.overlay(alignment: .top) { Divider().opacity(0.5) }
}
}
}
extension View {
/// Ensures that button labels are the same size in a `GroupedList`.
func groupedListButtonLabel() -> some View {
frame(width: 14, height: 14)
}
}
extension GroupedList where HeaderAccessory == EmptyView, FooterAccessory == EmptyView, EmptyOverlay == EmptyView, AddButton == EmptyView, RemoveButton == EmptyView {
init(@ViewBuilder _ content: @escaping () -> Content)
{
self.content = content
self.headerAccessory = { EmptyView() }
self.footerAccessory = { EmptyView() }
self.emptyOverlay = { EmptyView() }
self.addButton = { _ in nil }
self.removeButton = { _ in nil }
}
}
extension GroupedList where FooterAccessory == EmptyView, EmptyOverlay == EmptyView, AddButton == EmptyView, RemoveButton == EmptyView {
init(@ViewBuilder _ content: @escaping () -> Content, headerAccessory: @escaping () -> HeaderAccessory)
{
self.content = content
self.headerAccessory = headerAccessory
self.footerAccessory = { EmptyView() }
self.emptyOverlay = { EmptyView() }
self.addButton = { _ in nil }
self.removeButton = { _ in nil }
}
}
extension GroupedList where HeaderAccessory == EmptyView, FooterAccessory == EmptyView {
init(@ViewBuilder _ content: @escaping () -> Content,
emptyOverlay: @escaping () -> EmptyOverlay,
addButton: @escaping (Label<Text, Image>) -> AddButton? = { _ in nil },
removeButton: @escaping (Label<Text, Image>) -> RemoveButton? = { _ in nil })
{
self.content = content
self.headerAccessory = { EmptyView() }
self.footerAccessory = { EmptyView() }
self.emptyOverlay = emptyOverlay
self.addButton = addButton
self.removeButton = removeButton
}
}
fileprivate extension View {
func applyGroupedListCommandButtonStyle() -> some View {
// the .accessoryBarAction looks nicer, but only available on macOS Sonoma
// on older version of macOS, use the default style (equivalent to .bordered)
if #available(macOS 14.0, *) {
return self.buttonStyle(.accessoryBarAction)
} else {
return self
}
}
}