Popover

Swift macOS License: MIT

自定义 macOS popover。

alt text

安装

由于这是一个 Swift Package,安装过程非常简单直接。

手动方式

更新你的 Package.swift 依赖项

dependencies: [
    .package(url: "https://github.com/iSapozhnik/Popover", from: "1.1.1")
]

通过 Xcode

  1. 前往 File -> Swift Packages -> Add Package Dependency
  2. 输入 GitHub URL https://github.com/iSapozhnik/Popover 并点击 Next
  3. 选择最新版本
  4. 点击 Finish

如何使用

  1. 在你的 AppDelegate 中导入 Popover
  2. 创建一个你想放入状态栏的视图。
  3. 创建一个内容视图控制器,它将被嵌入到 Popover 内部
  4. (可选)创建菜单项
  5. 创建一个 Popover 实例。在这里你可以使用标准配置(在这种情况下你不需要传递 windowConfiguration 参数),或者你可以继承 DefaultConfiguration,重写一些属性,然后传递一个新的实例作为参数。如果在第 4 步你创建了菜单项,也在这里传递它。你可以通过右键点击菜单栏图标来查看它们。
  6. 调用 prepare 来设置一切。

下面是一个典型的 AppDelegate 的示例

import Cocoa
import Popover

class MyPopoverConfiguration: DefaultConfiguration {
    override var backgroundColor: NSColor {
        return NSColor.systemRed
    }

    override var borderColor: NSColor? {
        return NSColor.red
    }
}

@NSApplicationMain
class AppDelegate: NSObject, NSApplicationDelegate {
    var popover: Popover!

    func applicationDidFinishLaunching(_ aNotification: Notification) {
        let statusItemView = StatusItemView(frame: NSRect(width: 22.0, height: 20))

        let viewController = MainViewController()

        let menuItems: [Popover.MenuItemType] = [
            .item(Popover.MenuItem(title: "Settings", action: viewController.showSettings)),
            .separator,
            .item(Popover.MenuItem(title: "Quit", key: "q", action: viewController.quit))
        ]

        popover = Popover(with: MyPopoverConfiguration(), menuItems: menuItems)
        popover.prepare(with: statusItemView, contentViewController: viewController)
    }
}

可以自定义什么?

很多东西可以自定义

public protocol PopoverConfiguration {
    /// The distance from Popover's arrow to a status item.
    var popoverToStatusItemMargin:  CGFloat { get }

    /// Popover's background color.
    var backgroundColor:            NSColor { get }

    /// Popover's border color.
    /// - Important:
    ///     If `borderColor` returns `nil`, settings `borderWidth` won't make any effect. See also: `borderWidth`.
    var borderColor:                NSColor? { get }

    /// Popover's border width.
    /// - Important:
    ///      If Popover's border color is set to `nil`, setting `borderWidth` won't make any effect.
    var borderWidth:                CGFloat { get }

    /// Defines Popover arrow height.
    var arrowHeight:                CGFloat { get }

    /// Defines Popover arrow width.
    var arrowWidth:                 CGFloat { get }

    /// Defines Popover corner radius.
    /// - Warning:
    ///     If this value is too big and if the Popover's status item (menu bar view) is too close to the right edge, the appearence of the Popover might be odd.
    var cornerRadius:               CGFloat { get }

    /// Defines Popover content edge insets.
    var contentEdgeInsets:          NSEdgeInsets { get }

    /// The distance from the right side of the Popover to the screen's edge.
    /// - Warning:
    ///     If this value is too big and if the Popover's status item (menu bar view) is too close to the right edge, the appearence of the Popover might be odd.
    var rightEdgeMargin:            CGFloat { get }
}

提及

鸣谢

@iSapozhnik 创建和维护。

许可

在 MIT 许可证下发布。 详细信息请参阅 LICENSE

版权所有 © 2020-present Sapozhnik Ivan。

深受 CCNStatusItem 的启发