Merge remote-tracking branch 'refs/remotes/origin/main'

This commit is contained in:
david-swift 2024-08-25 11:41:21 +02:00
commit ec86bf1324
9 changed files with 53 additions and 32 deletions

View File

@ -30,7 +30,7 @@ jobs:
- name: Modify Docs
run: |
echo "<script>window.location.href += \"/documentation/termkitbackend\"</script>" > docs/index.html;
sed -i '' 's/,2px/,10px/g' docs/css/index.038e887c.css
sed -i '' 's/,2px/,10px/g' docs/css/index.*.css
- name: Upload Artifact
uses: actions/upload-pages-artifact@v3
with:

View File

@ -12,7 +12,7 @@
</a>
</p>
_TermKitBackend_ is a declarative framework allowing the creation of user interface for the terminal. It works on Linux, macOS and Windows thanks to the [TermKit project](https://github.com/migueldeicaza/TermKit).
_TermKitBackend_ is a declarative framework allowing the creation of user interface for the terminal. It works on Linux and macOS thanks to the [TermKit project](https://github.com/migueldeicaza/TermKit).
## Table of Contents

View File

@ -1,6 +1,6 @@
# ``TermKitBackend``
_TermKitBackend_ is a declarative framework allowing the creation of user interface for the terminal. It works on Linux, macOS and Windows thanks to the [TermKit project](https://github.com/migueldeicaza/TermKit).
_TermKitBackend_ is a declarative framework allowing the creation of user interface for the terminal. It works on Linux and macOS thanks to the [TermKit project](https://github.com/migueldeicaza/TermKit).
## Overview

View File

@ -48,7 +48,7 @@ public struct Button: TermKitWidget, ButtonWidget, MenuWidget {
return ViewStorage(self)
}
let button = TermKit.Button(label, clicked: action)
return .init(button)
return .init(button, state: self)
}
/// Update the stored content.
@ -66,12 +66,13 @@ public struct Button: TermKitWidget, ButtonWidget, MenuWidget {
if type == MenuContext.self {
storage.fields[actionID] = action
}
guard let storage = storage.pointer as? TermKit.Button else {
guard let pointer = storage.pointer as? TermKit.Button else {
return
}
storage.clicked = { _ in action() }
if updateProperties {
storage.text = label
pointer.clicked = { _ in action() }
if updateProperties, (storage.previousState as? Self)?.label != label {
pointer.text = label
storage.previousState = self
}
}

View File

@ -37,7 +37,7 @@ public struct Checkbox: TermKitWidget {
button.toggled = { _ in
isOn.wrappedValue = button.checked
}
return .init(button)
return .init(button, state: self)
}
/// Update the stored content.
@ -52,13 +52,20 @@ public struct Checkbox: TermKitWidget {
updateProperties: Bool,
type: Data.Type
) where Data: ViewRenderData {
guard let storage = storage.pointer as? TermKit.Checkbox, updateProperties else {
guard let pointer = storage.pointer as? TermKit.Checkbox else {
return
}
storage.text = label
storage.checked = isOn.wrappedValue
storage.toggled = { _ in
isOn.wrappedValue = storage.checked
if updateProperties {
if (storage.previousState as? Self)?.label != label {
pointer.text = label
}
if (storage.previousState as? Self)?.isOn.wrappedValue != isOn.wrappedValue {
pointer.checked = isOn.wrappedValue
}
storage.previousState = self
}
pointer.toggled = { _ in
isOn.wrappedValue = pointer.checked
}
}

View File

@ -38,7 +38,7 @@ public struct Frame: TermKitWidget {
if let pointer = subview.pointer as? TermKit.View {
frame.addSubview(pointer)
}
return .init(frame, content: [.mainContent: [subview]])
return .init(frame, content: [.mainContent: [subview]], state: self)
}
/// Update the stored content.
@ -56,10 +56,13 @@ public struct Frame: TermKitWidget {
if let storage = storage.content[.mainContent]?.first {
view.updateStorage(storage, modifiers: modifiers, updateProperties: updateProperties, type: type)
}
guard let storage = storage.pointer as? TermKit.Frame, updateProperties else {
guard let pointer = storage.pointer as? TermKit.Frame,
updateProperties,
(storage.previousState as? Self)?.label != label else {
return
}
storage.title = label
pointer.title = label
storage.previousState = self
}
}

View File

@ -29,7 +29,7 @@ public struct Label: TermKitWidget {
type: Data.Type
) -> ViewStorage where Data: ViewRenderData {
let button = TermKit.Label(label)
return .init(button)
return .init(button, state: self)
}
/// Update the stored content.
@ -44,10 +44,13 @@ public struct Label: TermKitWidget {
updateProperties: Bool,
type: Data.Type
) where Data: ViewRenderData {
guard let storage = storage.pointer as? TermKit.Label, updateProperties else {
guard let pointer = storage.pointer as? TermKit.Label,
updateProperties,
(storage.previousState as? Self)?.label != label else {
return
}
storage.text = label
pointer.text = label
storage.previousState = self
}
}

View File

@ -15,6 +15,11 @@ public struct ProgressBar: TermKitWidget {
/// The maximum value.
var max: Double
/// The fraction displayed in the progress bar.
var fraction: Float {
.init(value / max)
}
/// Initialize a progress bar.
/// - Parameters:
/// - value: The current value.
@ -34,8 +39,8 @@ public struct ProgressBar: TermKitWidget {
type: Data.Type
) -> ViewStorage where Data: ViewRenderData {
let bar = TermKit.ProgressBar()
bar.fraction = .init(value / max)
return .init(bar)
bar.fraction = fraction
return .init(bar, state: self)
}
/// Update the stored content.
@ -50,10 +55,13 @@ public struct ProgressBar: TermKitWidget {
updateProperties: Bool,
type: Data.Type
) where Data: ViewRenderData {
guard let storage = storage.pointer as? TermKit.ProgressBar, updateProperties else {
guard let pointer = storage.pointer as? TermKit.ProgressBar,
updateProperties,
(storage.previousState as? Self)?.fraction != fraction else {
return
}
storage.fraction = .init(value / max)
pointer.fraction = fraction
storage.previousState = self
}
}

View File

@ -34,15 +34,13 @@ public struct TextField: TermKitWidget {
type: Data.Type
) -> ViewStorage where Data: ViewRenderData {
let field = TermKit.TextField(text.wrappedValue)
let storage = ViewStorage(field)
let storage = ViewStorage(field, state: self)
field.secret = secret
field.textChanged = { _, _ in
(storage.fields[closureID] as? () -> Void)?()
}
storage.fields[closureID] = {
if field.text != text.wrappedValue {
text.wrappedValue = field.text
}
text.wrappedValue = field.text
}
return storage
}
@ -63,15 +61,16 @@ public struct TextField: TermKitWidget {
return
}
storage.fields[closureID] = {
if field.text != text.wrappedValue {
text.wrappedValue = field.text
}
text.wrappedValue = field.text
}
if updateProperties {
field.secret = secret
if (storage.previousState as? Self)?.secret != secret {
field.secret = secret
}
if field.text != text.wrappedValue {
field.text = text.wrappedValue
}
storage.previousState = self
}
}