Note
This project is no longer maintained.
ColibriComponents contains some components I often use in my packages and apps. There are views and other code fragments.
Name | Description |
---|---|
Toolbar | A toolbar for inside a window, supporting grouped buttons, toggles, menus and custom views. |
Freeform Toolbar | A toolbar for inside a window that floats over another view. |
Optional Picker | A picker with the option to not choose any of the values presented by the picker by pressing on the selected option. It is mainly used in the window toolbar. |
Selection Item Picker | A picker for elements conforming to SelectionItem . |
Undo Provider | A function for providing undo and redo functionality in a macOS or iOS app. |
Array builder | A result builder for building any array. It supports if , else and switch statements as well as for..in loops. |
Folder | A type for grouping any other type. The folder has an identifier, a title, an icon, and content. |
Easier to use LocalizedStringResource (macOS 13/iOS 16 or later) |
Simplify the initialization of SwiftUI views and other initializers using StringProtocol for LocalizedStringResource . |
Predefined buttons (macOS 13/iOS 16 or later) | Functions for creating cancel buttons, default buttons such as add buttons and confirmation buttons, and delete buttons. |
Color RGBA | Get the RGBA values for a color directly. This enables encoding and decoding of colors. |
Safe subscript | Access elements of an array safely. If an index is out of range, it returns nil , else, it returns the element at the index. It is also possible to set new values using this subscript. |
ID subscript | Access identifiable elements in an array safely with the identifier. |
Editable bounds | The upper and lower bound of a ClosedRange and Range but with a safe setter. |
Map value in range | A function for comparable types that maps the value in a certain ClosedRange or Range . |
Bindable | Adds a method for defining a setter for getting a Binding .Many of the standard types conform to Bindable . |
Geometry | A view modifier for observing the geometry of a view. |
- Open your Swift package in Xcode.
- Navigate to
File > Add Packages
. - Paste this URL into the search field:
https://github.com/david-swift/ColibriComponents
- Click on
Copy Dependency
. - Navigate to the
Package.swift
file. - In the
Package
initializer, underdependencies
, paste the dependency into the array.
- Open your Xcode project in Xcode.
- Navigate to
File > Add Packages
. - Paste this URL into the search field:
https://github.com/david-swift/ColibriComponents
- Click on
Add Package
.
Define any array using DSL.
func string(@ArrayBuilder<String> strings: () -> [String]) -> String {
strings().joined()
}
string {
"Hello, "
if bool {
"world!"
} else {
"colibri!"
}
for x in 0...10 {
"\nIteration Number \(x)"
}
}
Create a Binding
by adding a setter to a value.
TextField(
"Test",
text: text.binding { newValue in
print(newValue)
text = newValue
}
)
Get the RGBA values of a color. Color
conforms to Codable
.
let color = Color(red: 0.5, green: 0.5, blue: 0.5)
print(color.blue)
Initialize SwiftUI components using LocalizedStringResource
.
Button(.init("Hello", comment: "A localized string resource!")) { }
Edit the bounds of a ClosedRange
or Range
.
var range = 0...10
range.editableLowerBound = 2
range.editableUpperBound = 8
Group multiple elements and provide a folder title, icon, and identifier.
Folder("Hello", systemSymbol: .tag) {
"Hello"
"World"
}
A custom toolbar that floats over another view.
Text("Hello, world!")
.freeformToolbar(visibility: visibility, y: 50) {
ToolbarAction("Function", symbol: functionSymbol) {
print("Function!")
}
}
Get the geometry of a view with this modifier.
Text("Hello, world!")
.geometry { geometry in
size = geometry.size
}
Access and set identifiable elements of an array by providing the identifier.
let array: [Element] = [.init(id: "hello"), .init(id: "world)]
print(array[id: "hello"]?.id ?? "error")
Change a comparable type so it is in a certain range.
var value = 10
value.map(in: 0...5)
A picker with the option to not choose any of the values presented by the picker by pressing on the selected option.
OptionalPicker("Value", selection: $selection, defaultValue: "Hello") {
Label("Snow", systemSymbol: .snowflake)
.tagged("Snow")
Label("Swift", systemSymbol: .swift)
.tagged("Swift")
}
Define default buttons and cancel buttons as well as certain types of default buttons the fast way.
Button.addButton {
print("Add")
}
Access and set elements of an array the safe way. If an index is out of range, the getter returns nil and the setter takes no effect.
let array = ["Hello", "World"]
print(array[safe: 2])
A picker view for types with an identifier, title, and icon.
let items = [SomeSelectionItem("Swift", icon: .swift)]
SelectionItemPicker(selection: $selection, items: items)
A custom toolbar for inside a SwiftUI view.
@ArrayBuilder<ToolbarGroup> var toolbar: [ToolbarGroup] {
ToolbarGroup {
ToolbarMenu(.init(), systemSymbol: .plus) {
Button("+1") {
print("+1")
}
Button("+5") {
print("+5")
}
Button("+10") {
print("+10")
}
}
ToolbarAction("Minus", systemSymbol: .minus) {
print("Minus")
}
}
.spacer()
ToolbarGroup {
ProgressView(value: 0.5)
.padding()
}
.spacer()
ToolbarGroup {
ToolbarAction("Restart", systemSymbol: .restart) {
print("Restart")
}
}
}
A function for providing undo and redo functionality in a macOS or iOS app.
class ViewModel: ObservableObject {
@Published var text = "" {
didSet {
UndoProvider.registerUndo(withTarget: self, set: { $0.text = oldValue })
}
}
}
- SFSafeSymbols licensed under the MIT license
- SwiftLintPlugin licensed under the MIT license
- The contributors
- SourceDocs used for generating the docs
- SwiftLint for checking whether code style conventions are violated
- Jaden Geller: GitHub Gist ArrayBuilder.swift
- Matthaus Woolard: Article Handling undo & redo in SwiftUI
- The programming language Swift