Update documentation

This commit is contained in:
david-swift 2024-07-19 23:41:19 +02:00
parent 21e28344c1
commit eadc08d61d
9 changed files with 98 additions and 53 deletions

View File

@ -23,15 +23,10 @@ jobs:
- uses: actions/checkout@v4
- name: Build Docs
run: |
xcrun xcodebuild docbuild \
-scheme TermKitBackend \
-destination 'generic/platform=macOS' \
-derivedDataPath "$PWD/.derivedData" \
-skipPackagePluginValidation
xcrun docc process-archive transform-for-static-hosting \
"$PWD/.derivedData/Build/Products/Debug/TermKitBackend.doccarchive" \
--output-path "docs" \
--hosting-base-path "TermKitBackend"
sed -i '' -n 'H;${x;s/^\n//;s/ .package.*$/ .package(url: "https:\/\/github.com\/swiftlang\/swift-docc-plugin", from: "1.3.0"),\n&/;p;}' Package.swift
swift package --allow-writing-to-directory ./docs \
generate-documentation --target TermKitBackend --output-path ./docs \
--transform-for-static-hosting --hosting-base-path TermKitBackend
- name: Modify Docs
run: |
echo "<script>window.location.href += \"/documentation/termkitbackend\"</script>" > docs/index.html;

View File

@ -7,16 +7,14 @@
import TermKit
/// A menu is an item of a ``MenuBar``.
/// A collection of buttons.
public struct ButtonCollection: ButtonContext.Widget, Wrapper {
/// The content of the menu.
/// The content of the collection.
var content: Body
/// Initialize a menu.
/// - Parameters:
/// - label: The menu's label, displayed in the menu bar.
/// - content: The content of the menu.
/// Initialize a collection.
/// - Parameter content: The content of the collection.
public init(@ViewBuilder content: @escaping () -> Body) {
self.content = content()
}

View File

@ -7,7 +7,7 @@
import TermKit
/// A menu is an item of a ``MenuBar``.
/// A menu is an item of a `MenuBar`.
public struct Menu: MenuContext.Widget {
/// The menu's label, displayed in the menu bar.

View File

@ -7,16 +7,14 @@
import TermKit
/// A menu is an item of a ``MenuBar``.
/// A collection of menus.
public struct MenuCollection: MenuContext.Widget, Wrapper {
/// The content of the menu.
/// The content of the collection.
var content: Body
/// Initialize a menu.
/// - Parameters:
/// - label: The menu's label, displayed in the menu bar.
/// - content: The content of the menu.
/// - Parameter content: The content of the collection.
public init(@ViewBuilder content: @escaping () -> Body) {
self.content = content()
}

View File

@ -22,9 +22,7 @@ public class TermKitApp: AppStorage {
public var storage: StandardAppStorage = .init()
/// Initialize the app storage.
/// - Parameters:
/// - id: The identifier.
/// - app: The application.
/// - Parameter id: The identifier.
public required init(id: String) { }
/// Execute the app.

View File

@ -8,10 +8,10 @@
import TermKit
/// The menu bar scene element adds a menu bar to the top of the app.
public struct MenuBar: TermKitSceneElement {
struct MenuBar: TermKitSceneElement {
/// The identifier of the menu bar.
public var id: String
var id: String
/// The menu bar's content.
var content: Body
@ -19,31 +19,28 @@ public struct MenuBar: TermKitSceneElement {
/// - Parameters:
/// - id: The identifier of the menu bar.
/// - content: The menu bar's content.
public init(id: String = "main-menu", @ViewBuilder content: () -> Body) {
init(id: String = "main-menu", @ViewBuilder content: () -> Body) {
self.id = id
self.content = content()
}
/// Set up the initial scene storages.
/// - Parameter app: The app storage.
public func setupInitialContainers<Storage>(app: Storage) where Storage: AppStorage {
func setupInitialContainers<Storage>(app: Storage) where Storage: AppStorage {
app.storage.sceneStorage.append(container(app: app))
}
/// The scene storage.
/// - Parameter app: The app storage.
public func container<Storage>(app: Storage) -> SceneStorage where Storage: AppStorage {
func container<Storage>(app: Storage) -> SceneStorage where Storage: AppStorage {
let items = MenuCollection { content }.container(modifiers: [], type: MenuContext.self)
let menubar = TermKit.MenuBar(
menus: items.pointer as? [TermKit.MenuBarItem] ?? []
)
Task {
try await Task.sleep(nanoseconds: 1)
for element in Application.top.subviews {
element.y = .bottom(of: menubar)
}
Application.top.addSubview(menubar)
for element in Application.top.subviews {
element.y = .bottom(of: menubar)
}
Application.top.addSubview(menubar)
return .init(id: id, pointer: menubar) {
menubar.ensureFocus()
}
@ -54,7 +51,7 @@ public struct MenuBar: TermKitSceneElement {
/// - storage: The storage to update.
/// - app: The app storage.
/// - updateProperties: Whether to update the view's properties.
public func update<Storage>(
func update<Storage>(
_ storage: SceneStorage,
app: Storage,
updateProperties: Bool

View File

@ -67,4 +67,12 @@ public struct Window: TermKitSceneElement {
Application.refresh()
}
/// Add a menubar to the app.
/// - Parameter content: The mnu bar's content.
@SceneBuilder
public func menuBar(@ViewBuilder content: @escaping () -> Body) -> Scene {
self
MenuBar(content: content)
}
}

View File

@ -0,0 +1,40 @@
# Getting Started
Learn how to use the TermKit backend.
Knowledge about the Meta project is required.
Find more information [here](https://aparokshaui.github.io/meta/).
## The App
Define your app in the following way:
```swift
import TermKitBackend
@main
struct TestApp: App {
let id = "io.github.AparokshaUI.TestApp"
var app: TermKitApp!
var scene: Scene {
Window {
// Views (see list in documentation)
}
.menuBar {
Menu("_File") { // Menus
Button("_New") { // Buttons
print("Hi")
}
}
}
}
}
```
## Widgets
All the available widgets can be found in the documentation.
Take a look at the [sample app](https://github.com/david-swift/TermKitBackend/blob/main/Sources/TestApp/TestApp.swift) for more help.

View File

@ -5,6 +5,7 @@
// Created by david-swift on 01.07.2024.
//
@testable import Meta
import TermKitBackend
@main
@ -17,7 +18,6 @@ struct TestApp: App {
var app: TermKitApp!
var scene: Scene {
menuBar
Window {
VStack {
Demos()
@ -29,10 +29,7 @@ struct TestApp: App {
.vcenter()
.infoBox("About TermKitBackend", message: aboutInfo, signal: about)
}
}
var menuBar: MenuBar {
.init {
.menuBar {
fileMenu
Menu("_Actions") {
Button("_Hello, world!") { }
@ -86,31 +83,45 @@ struct Demos: View {
}
struct ControlsModel: Model {
var isOn = false
var fraction = 0
var text = "Controls"
var model: ModelData?
func increaseFraction() {
Task { @MainActor in
setModel { $0.fraction += 1 }
}
}
}
struct Controls: View {
@State private var isOn = false
@State private var fraction = 0
@State private var text = "Controls"
@State private var model = ControlsModel()
var view: Body {
Frame(text) {
Frame(model.text) {
HStack {
Button("Button (progress)") {
if fraction == 10 {
fraction = 0
if model.fraction == 10 {
model.fraction = 0
} else {
fraction += 1
model.increaseFraction()
}
}
Button("Button (text)") {
text = "Hello"
model.text = "Hello"
}
}
.frame(height: 1)
Checkbox(isOn ? "On" : "Off", isOn: $isOn)
TextField(text: $text)
.secret(isOn)
ProgressBar(value: .init(fraction), max: 10)
Checkbox(model.isOn ? "On" : "Off", isOn: $model.isOn)
TextField(text: $model.text)
.secret(model.isOn)
ProgressBar(value: .init(model.fraction), max: 10)
}
.frame(width: 40, height: 7)
}