diff --git a/Package.swift b/Package.swift index 9ec5c7e..7a636ca 100644 --- a/Package.swift +++ b/Package.swift @@ -13,8 +13,5 @@ let package = Package( targets: [ .target( name: "NVAlert"), - .testTarget( - name: "NVAlertTests", - dependencies: ["NVAlert"]), ] ) diff --git a/README.md b/README.md index 7b70ed8..f8a1367 100644 --- a/README.md +++ b/README.md @@ -1,7 +1,5 @@ # NVAlert Package -**Important**: 👷‍♂️ This package is currently **under construction**, and may change at any time. - ## What is this? This is a package that helps you present larger alerts (with more text) for macOS, if you dislike the smaller alerts introduced in more recent versions of macOS. @@ -21,3 +19,62 @@ NVAlert().withInformation( .withPrimary(text: NSLocalizedString("generic.ok", nil)) .show() ``` + +### Additional buttons + +The other chainable methods you can call are: + +- `withSecondary(text: String, action: (@MainActor (NVAlertVC) -> Void)?)` +- `withTertiary(text: String, action: (@MainActor (NVAlertVC) -> Void)?)` + +A second button can be added by using `withSecondary` and you can add a third button by using `withTertiary`. + +**Note**: It currently isn't possible to add more than three buttons. It's a bad user experience to have too many buttons for a single alert, so this is rather intentional. + +### Help button + +If you would like to have a "help" button for informative purposes, you can leave the `text` property of `withTertiary` empty, and the button's `.bezelStyle` will be set to `.helpButton`. + +### Setting actions for buttons + +#### Primary button + +You must always manually set the action for each button, except for a `withPrimary` call. + +For a **primary** button interaction, the default callback will simply close the alert without doing anything extra, like an "OK" button which is supposed to be used to acknowledge information without any additional things happening. + +This is because of the method signature's default argument for `action`: + +```swift +public func withPrimary( + text: String, + action: @MainActor @escaping (NVAlertVC) -> Void = { vc in + vc.close(with: .alertFirstButtonReturn) + } +) +``` + +#### Secondary and tertiary buttons + +If an action for a **secondary or tertiary** button is not set, the button will not be displayed. + +An example usage of an action may be something like this: + +```swift +NVAlert().withInformation( + title: "A new component is available", + subtitle: "Would you like to install this component?" +) +.withPrimary( + text: "Install", + action: { vc in + // First, close the alert + vc.close(with: .OK) + + // Run additional code after closing + self.performInstallation() + } +).show() +``` + +If the user now presses the "Install" button, the action callback closure will be executed. \ No newline at end of file diff --git a/Sources/NVAlert/NVAlertVC.swift b/Sources/NVAlert/NVAlertVC.swift index b856e8b..0449fb9 100644 --- a/Sources/NVAlert/NVAlertVC.swift +++ b/Sources/NVAlert/NVAlertVC.swift @@ -26,22 +26,14 @@ open class NVAlertVC: NSViewController { open override func viewWillAppear() { imageView.image = NSApp.applicationIconImage - if actionSecondary == nil { - buttonSecondary.isHidden = true - } - if actionTertiary == nil { - buttonTertiary.isHidden = true - } + buttonSecondary.isHidden = actionSecondary == nil + buttonTertiary.isHidden = actionTertiary == nil } open override func viewDidAppear() { view.window?.makeFirstResponder(buttonPrimary) } - deinit { - // print("deinit: \(String(describing: self)).\(#function)") - } - // MARK: Outlet Actions @IBAction func primaryButtonAction(_ sender: Any) { diff --git a/Sources/NVAlert/NVAlertableError.swift b/Sources/NVAlert/NVAlertableError.swift deleted file mode 100644 index 52af49a..0000000 --- a/Sources/NVAlert/NVAlertableError.swift +++ /dev/null @@ -1,5 +0,0 @@ -import Foundation - -protocol NVAlertableError { - func getErrorMessageKey() -> String -} diff --git a/Sources/NVAlert/Resources/NVAlert.storyboard b/Sources/NVAlert/Resources/NVAlert.storyboard index 53e118a..80da3ed 100644 --- a/Sources/NVAlert/Resources/NVAlert.storyboard +++ b/Sources/NVAlert/Resources/NVAlert.storyboard @@ -73,7 +73,7 @@ Gw - + @@ -84,7 +84,7 @@ Gw - + @@ -108,7 +108,7 @@ Gw - + diff --git a/Tests/NVAlertTests/NVAlertTests.swift b/Tests/NVAlertTests/NVAlertTests.swift deleted file mode 100644 index 792011f..0000000 --- a/Tests/NVAlertTests/NVAlertTests.swift +++ /dev/null @@ -1,12 +0,0 @@ -import XCTest -@testable import NVAlert - -final class NVAlertTests: XCTestCase { - func testExample() throws { - // XCTest Documentation - // https://developer.apple.com/documentation/xctest - - // Defining Test Cases and Test Methods - // https://developer.apple.com/documentation/xctest/defining_test_cases_and_test_methods - } -}