david-swift e989bea14e
All checks were successful
Deploy Docs / publish (push) Successful in 2m38s
Initial commit
2024-12-02 22:14:13 +01:00

65 lines
1.6 KiB
Swift

//
// Button.swift
// MacBackend
//
// Created by david-swift on 18.09.2024.
//
import SwiftUI
/// A button widget.
public struct Button: SwiftUIWidget {
/// The button's label.
var label: String?
/// The button's icon.
var icon: Icon?
/// The button's action.
var action: () -> Void
/// Initialize a button.
/// - Parameters:
/// - label: The button's label.
/// - icon: The button's icon.
/// - action: The handler.
public init(_ label: String, icon: Icon? = nil, action: @escaping () -> Void) {
self.label = label
self.icon = icon
self.action = action
}
/// Initialize a button.
/// - Parameters:
/// - icon: The button's icon.
/// - action: The handler.
public init(icon: Icon, action: @escaping () -> Void) {
self.icon = icon
self.action = action
}
/// Get the SwiftUI view.
/// - Parameter properties: The widget data.
/// - Returns: The SwiftUI view.
public static func view(properties: Self) -> some SwiftUI.View {
if let icon = properties.icon {
SwiftUI.Button {
properties.action()
} label: {
SwiftUI.Label {
if let label = properties.label {
SwiftUI.Text(label)
}
} icon: {
icon.image
}
}
} else if let label = properties.label {
SwiftUI.Button(label) {
properties.action()
}
}
}
}