Simple cleanup

This commit is contained in:
2024-07-09 17:48:14 +02:00
parent 476feaa879
commit 2d64946506
6 changed files with 64 additions and 35 deletions

View File

@ -13,8 +13,5 @@ let package = Package(
targets: [
.target(
name: "NVAlert"),
.testTarget(
name: "NVAlertTests",
dependencies: ["NVAlert"]),
]
)

View File

@ -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.

View File

@ -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) {

View File

@ -1,5 +0,0 @@
import Foundation
protocol NVAlertableError {
func getErrorMessageKey() -> String
}

View File

@ -73,7 +73,7 @@ Gw
<constraint firstAttribute="bottom" secondItem="glZ-s9-p3a" secondAttribute="bottom" constant="20" symbolic="YES" id="y7Q-UK-jiB"/>
</constraints>
</visualEffectView>
<textField focusRingType="none" horizontalHuggingPriority="251" verticalHuggingPriority="750" translatesAutoresizingMaskIntoConstraints="NO" id="W3Q-oe-uSr">
<textField horizontalHuggingPriority="251" verticalHuggingPriority="750" translatesAutoresizingMaskIntoConstraints="NO" id="W3Q-oe-uSr">
<rect key="frame" x="98" y="153" width="384" height="19"/>
<constraints>
<constraint firstAttribute="width" relation="lessThanOrEqual" constant="380" id="Qas-PU-3VH"/>
@ -84,7 +84,7 @@ Gw
<color key="backgroundColor" name="textBackgroundColor" catalog="System" colorSpace="catalog"/>
</textFieldCell>
</textField>
<textField focusRingType="none" horizontalHuggingPriority="251" verticalHuggingPriority="750" translatesAutoresizingMaskIntoConstraints="NO" id="tgD-Mg-YN4">
<textField horizontalHuggingPriority="251" verticalHuggingPriority="750" translatesAutoresizingMaskIntoConstraints="NO" id="tgD-Mg-YN4">
<rect key="frame" x="98" y="127" width="384" height="16"/>
<textFieldCell key="cell" selectable="YES" title="This is a slightly more expanded explanation." id="m3c-P9-gfK">
<font key="font" metaFont="system"/>
@ -108,7 +108,7 @@ Gw
</constraints>
<imageCell key="cell" refusesFirstResponder="YES" alignment="left" imageScaling="proportionallyDown" id="ocb-ea-QMS"/>
</imageView>
<textField focusRingType="none" horizontalHuggingPriority="251" verticalHuggingPriority="750" translatesAutoresizingMaskIntoConstraints="NO" id="0ra-hO-Hjb">
<textField horizontalHuggingPriority="251" verticalHuggingPriority="750" translatesAutoresizingMaskIntoConstraints="NO" id="0ra-hO-Hjb">
<rect key="frame" x="98" y="70" width="384" height="42"/>
<textFieldCell key="cell" selectable="YES" id="dlD-Jr-DXC">
<font key="font" metaFont="smallSystem"/>

View File

@ -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
}
}