适用于 macOS 应用程序的 Snackbar

Cocoa 的 Snackbar 组件

用于 Cocoa 的轻量级且可定制的 Snackbar 组件,旨在向用户显示简短的信息消息。

演示

特征

  • 向用户显示短信或通知
  • 可定制的外观,包括背景颜色、文本颜色和动画持续时间
  • 支持操作按钮和回调处理程序
  • 轻松集成到现有的 Cocoa 项目中

安装

可可足类

Swift Package 管理器是您的朋友。

用法

初始化

制定一个主题

extension SnackbarTheme where Self == DefaultSnackbarTheme {
    static var info: SnackbarTheme { DefaultSnackbarTheme(withStyle: .info) }
    static var alert: SnackbarTheme { DefaultSnackbarTheme(withStyle: .alert) }
    static var warning: SnackbarTheme { DefaultSnackbarTheme(withStyle: .warning) }
    static var success: SnackbarTheme { DefaultSnackbarTheme(withStyle: .success) }
}

struct DefaultSnackbarTheme: SnackbarTheme {
    var style: SnackbarStyle
    
    init(withStyle style: SnackbarStyle) {
        self.style = style
    }

    var textColor: NSColor { .labelColor }
    var backgroundColor: NSColor {
        switch style {
        case .alert:
            return .systemRed
        case .success:
            return .systemGreen
        case .warning:
            return .systemOrange
        case .info:
            return .systemBlue
        }
    }

    var borderColor: NSColor {
        .secondaryLabelColor
    }
}

显示快捷栏

带有操作按钮和图标的 Snackbar。

let theme: SnackbarTheme = .alert
let actions = [
    SnackbarAction(
        title: NSLocalizedString("Remove", comment: ""),
        icon: nil,
        type: .primary,
        action: {}
    ),
    SnackbarAction(
        title: NSLocalizedString("Later", comment: ""),
        icon: nil,
        type: .secondary,
        action: {}
    ),
]
Snackbar.show(
    theme: theme,
    type: .permanent,
    title: NSLocalizedString("Are you sure you want to remove all spaces?", comment: "").text,
    subtitle: NSLocalizedString("You can not undo this action", comment: "").text,
    actions: actions,
    actionsLayout: .horizontal,
    hasActionsSeparator: false,
    icon: NSImage(named: "your_icon"),
    fromWindow: view.window
)

执照

该项目已获得 MIT 许可证的许可。有关详细信息,请参阅许可证文件。

贡献

欢迎贡献!更多详情请参阅贡献指南。

支持

如果您喜欢 Snackbar,还可以考虑检查我正在使用它的应用程序( Lasso – macOS 的窗口管理器)。

GitHub

查看 Github