SPB Git

Repository Public MIT

GlassSplitButton.swift 4.4 KB 128 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-0313//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 SwiftUI4243/// A primary action with a menu of secondary actions.44///45/// ```swift46/// GlassSplitButton("Send", systemImage: "paperplane") {47///     send()48/// } menu: {49///     Button("Send later…") { schedule() }50///     Button("Save draft") { saveDraft() }51/// }52/// ```53///54/// On macOS this is the system split button (`Menu` with a primary action). On iOS and iPadOS the action and55/// the menu are two separate glass buttons in one glass container, so each has its own hit target and its own56/// accessibility element.57public struct GlassSplitButton<MenuContent: View>: View {58    @GlassThemed private var theme59    private let title: Text60    private let systemImage: String?61    private let variant: GlassButtonVariant62    private let size: GlassControlSize63    private let menuLabel: Text64    private let action: @MainActor () -> Void65    private let menu: MenuContent6667    /// - Parameter menuAccessibilityLabel: name of the menu part. Defaults to the localised "More actions".68    public init(69        _ titleKey: LocalizedStringKey,70        systemImage: String? = nil,71        variant: GlassButtonVariant = .regular,72        size: GlassControlSize = .regular,73        menuAccessibilityLabel: LocalizedStringKey? = nil,74        action: @escaping @MainActor () -> Void,75        @ViewBuilder menu: () -> MenuContent76    ) {77        self.title = Text(titleKey)78        self.systemImage = systemImage79        self.variant = variant80        self.size = size81        self.menuLabel = menuAccessibilityLabel.map { Text($0) } ?? Text(GlassL10n.string(.moreActions))82        self.action = action83        self.menu = menu()84    }8586    public var body: some View {87        #if os(macOS)88        Menu {89            menu90        } label: {91            primaryLabel92        } primaryAction: {93            action()94        }95        .glassButtonChrome(variant, size: size)96        .fixedSize()97        #else98        GlassGroup(spacing: theme.spacing.xs) {99            HStack(spacing: theme.spacing.xxs) {100                Button(action: action) { primaryLabel }101                    .glassButtonChrome(variant, size: size)102                Menu {103                    menu104                } label: {105                    Label { menuLabel } icon: { Image(systemName: "chevron.down") }106                        .labelStyle(.iconOnly)107                        .glassButtonLabelContrast(variant)108                }109                .buttonBorderShape(.circle)110                .glassButtonChrome(variant, size: size)111                .glassMinimumHitTarget()112            }113        }114        #endif115    }116117    private var primaryLabel: some View {118        Group {119            if let systemImage {120                Label { title } icon: { Image(systemName: systemImage) }121            } else {122                title123            }124        }125        .glassButtonLabelContrast(variant)126    }127}128
Files main
Menu

Theme