Compare widgets to previous versions

This commit is contained in:
david-swift 2024-07-31 14:33:40 +02:00
parent ef337544bc
commit 8c68676994
6 changed files with 50 additions and 29 deletions

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