// ============================================================================= // SP BOUCHER | SOFTWARE & RESEARCH // ARTIFICIAL INTELLIGENCE • FINANCE • ENGINEERING // ============================================================================= // // Project : SP GlassKit // File : Sources/SPGlassComponents/Buttons/GlassSplitButton.swift // Module : SPGlassComponents // Repository : https://git.spboucher.ai/sp-glasskit // Project Website : https://glass.spboucher.ai // Created : 2026-12-02 // Last Modified : 2026-12-03 // // Author : Simon-Pierre Boucher // Position : Professor of Finance // Institution : Université du Québec en Outaouais (UQO) // Organization : SP Boucher // Website : https://spboucher.ai // Git : https://git.spboucher.ai // // Preferred Contact: contact@spboucher.ai // Development : dev@spboucher.ai // Support : support@spboucher.ai // Security/Admin : admin@spboucher.ai // // Copyright (c) 2026 // Simon-Pierre Boucher // All rights reserved where applicable. This file is governed by the // applicable project license, copyright notices and relevant law. // Existing third-party rights and notices remain unaffected. // // License : MIT // // Do not embed credentials, private keys, access tokens, // certificates or confidential personal information. // // ============================================================================= // END OF SP BOUCHER SOURCE HEADER // ============================================================================= import SwiftUI /// A primary action with a menu of secondary actions. /// /// ```swift /// GlassSplitButton("Send", systemImage: "paperplane") { /// send() /// } menu: { /// Button("Send later…") { schedule() } /// Button("Save draft") { saveDraft() } /// } /// ``` /// /// On macOS this is the system split button (`Menu` with a primary action). On iOS and iPadOS the action and /// the menu are two separate glass buttons in one glass container, so each has its own hit target and its own /// accessibility element. public struct GlassSplitButton: View { @GlassThemed private var theme private let title: Text private let systemImage: String? private let variant: GlassButtonVariant private let size: GlassControlSize private let menuLabel: Text private let action: @MainActor () -> Void private let menu: MenuContent /// - Parameter menuAccessibilityLabel: name of the menu part. Defaults to the localised "More actions". public init( _ titleKey: LocalizedStringKey, systemImage: String? = nil, variant: GlassButtonVariant = .regular, size: GlassControlSize = .regular, menuAccessibilityLabel: LocalizedStringKey? = nil, action: @escaping @MainActor () -> Void, @ViewBuilder menu: () -> MenuContent ) { self.title = Text(titleKey) self.systemImage = systemImage self.variant = variant self.size = size self.menuLabel = menuAccessibilityLabel.map { Text($0) } ?? Text(GlassL10n.string(.moreActions)) self.action = action self.menu = menu() } public var body: some View { #if os(macOS) Menu { menu } label: { primaryLabel } primaryAction: { action() } .glassButtonChrome(variant, size: size) .fixedSize() #else GlassGroup(spacing: theme.spacing.xs) { HStack(spacing: theme.spacing.xxs) { Button(action: action) { primaryLabel } .glassButtonChrome(variant, size: size) Menu { menu } label: { Label { menuLabel } icon: { Image(systemName: "chevron.down") } .labelStyle(.iconOnly) .glassButtonLabelContrast(variant) } .buttonBorderShape(.circle) .glassButtonChrome(variant, size: size) .glassMinimumHitTarget() } } #endif } private var primaryLabel: some View { Group { if let systemImage { Label { title } icon: { Image(systemName: systemImage) } } else { title } } .glassButtonLabelContrast(variant) } }