SPB Git

Repository Public MIT

GlassSplitButton.swift 4.4 KB 129 lines swift

1// =============================================================================2//                  SP BOUCHER | SOFTWARE & RESEARCH3//             ARTIFICIAL INTELLIGENCE • FINANCE • ENGINEERING4// =============================================================================5//6// Project          : SP GlassKit7// File             : Sources/SPGlassComponents/Buttons/GlassSplitButton.swift8// Module           : SPGlassComponents9// Repository       : https://git.spboucher.ai/sp-glasskit10// Project Website  : https://glass.spboucher.ai11// Created          : 2026-12-0212// Last Modified    : 2026-12-0213//14// Author           : Simon-Pierre Boucher15// Position         : Professor of Finance16// Institution      : Université du Québec en Outaouais (UQO)17// Organization     : SP Boucher18// Website          : https://spboucher.ai19// Git              : https://git.spboucher.ai20//21// Preferred Contact: contact@spboucher.ai22// Development      : dev@spboucher.ai23// Support          : support@spboucher.ai24// Security/Admin   : admin@spboucher.ai25//26// Copyright (c) 202627// Simon-Pierre Boucher28// All rights reserved where applicable. This file is governed by the29// applicable project license, copyright notices and relevant law.30// Existing third-party rights and notices remain unaffected.31//32// License          : MIT33//34// Do not embed credentials, private keys, access tokens,35// certificates or confidential personal information.36//37// =============================================================================38// END OF SP BOUCHER SOURCE HEADER39// =============================================================================4041import SwiftUI42import SPGlassCore43import SPGlassTokens44import SPGlassThemes45import SPGlassEffects4647/// A primary action with a menu of secondary actions.48///49/// ```swift50/// GlassSplitButton("Send", systemImage: "paperplane") {51///     send()52/// } menu: {53///     Button("Send later…") { schedule() }54///     Button("Save draft") { saveDraft() }55/// }56/// ```57///58/// On macOS this is the system split button (`Menu` with a primary action). On iOS and iPadOS the action and59/// the menu are two separate glass buttons in one glass container, so each has its own hit target and its own60/// accessibility element.61public struct GlassSplitButton<MenuContent: View>: View {62    @GlassThemed private var theme63    private let title: Text64    private let systemImage: String?65    private let variant: GlassButtonVariant66    private let size: GlassControlSize67    private let menuLabel: Text68    private let action: @MainActor () -> Void69    private let menu: MenuContent7071    /// - Parameter menuAccessibilityLabel: name of the menu part. Defaults to the localised "More actions".72    public init(73        _ titleKey: LocalizedStringKey,74        systemImage: String? = nil,75        variant: GlassButtonVariant = .regular,76        size: GlassControlSize = .regular,77        menuAccessibilityLabel: LocalizedStringKey? = nil,78        action: @escaping @MainActor () -> Void,79        @ViewBuilder menu: () -> MenuContent80    ) {81        self.title = Text(titleKey)82        self.systemImage = systemImage83        self.variant = variant84        self.size = size85        self.menuLabel = menuAccessibilityLabel.map { Text($0) } ?? Text(GlassL10n.string(.moreActions))86        self.action = action87        self.menu = menu()88    }8990    public var body: some View {91        #if os(macOS)92        Menu {93            menu94        } label: {95            primaryLabel96        } primaryAction: {97            action()98        }99        .glassButtonChrome(variant, size: size)100        .fixedSize()101        #else102        GlassGroup(spacing: theme.spacing.xs) {103            HStack(spacing: theme.spacing.xxs) {104                Button(action: action) { primaryLabel }105                    .glassButtonChrome(variant, size: size)106                Menu {107                    menu108                } label: {109                    Label { menuLabel } icon: { Image(systemName: "chevron.down") }110                        .labelStyle(.iconOnly)111                }112                .buttonBorderShape(.circle)113                .glassButtonChrome(variant, size: size)114                .glassMinimumHitTarget()115            }116        }117        #endif118    }119120    @ViewBuilder121    private var primaryLabel: some View {122        if let systemImage {123            Label { title } icon: { Image(systemName: systemImage) }124        } else {125            title126        }127    }128}129
Files main
Menu

Theme