Skip to content

Commit 15d67dd

Browse files
committed
Convert text field to use public API
1 parent b966d80 commit 15d67dd

1 file changed

Lines changed: 13 additions & 51 deletions

File tree

‎Sources/TerminalUI/Views/TextField.swift‎

Lines changed: 13 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -2,68 +2,30 @@ import AttributeGraph
22

33
public struct TextField {
44

5+
@FocusState private var isFocused: Bool
6+
@Environment(\.focusManager) private var focusManager
57
private let text: Binding<String>
68

79
public init(text: Binding<String>) {
810
self.text = text
911
}
1012
}
1113

12-
extension TextField: PrimitiveView {
14+
extension TextField: View {
1315

14-
public static func makeView(
15-
view: Attribute<TextField>,
16-
inputs: ViewInputs
17-
) -> ViewOutputs {
16+
public var body: some View {
17+
HStack {
1818

19-
unowned let graph = inputs.graph
19+
Text(text.wrappedValue)
2020

21-
let geometry = graph.external(of: ViewGeometry.self)
22-
graph.setValue(of: geometry, to: .zero)
23-
24-
let focusManager = graph[inputs.environment].focusManager
25-
let id = focusManager.add { key in
26-
graph[view].text.wrappedValue.append(contentsOf: key.characters)
21+
Text("_")
22+
.hidden(!isFocused)
23+
}
24+
.focusable()
25+
.focused($isFocused)
26+
.onKeyPress { keyPress in
27+
text.wrappedValue.append(keyPress.characters)
2728
return .handled
2829
}
29-
30-
return ViewOutputs(
31-
preferenceValues: graph.constant(.empty),
32-
layoutProxies: graph.rule { _ in
33-
34-
var text = graph[view].text.wrappedValue
35-
if focusManager.isFocused(id) {
36-
text.append("_") // Cursor
37-
}
38-
39-
return [
40-
LayoutProxy(
41-
layoutComputer: LayoutComputer { _ in
42-
Size(width: text.count, height: 1)
43-
},
44-
place: { frame in
45-
graph.setValue(of: geometry, to: ViewGeometry(frame: frame))
46-
}
47-
)
48-
]
49-
},
50-
displayList: graph.rule { _ in
51-
52-
let frame = graph[geometry].frame
53-
let environment = graph[inputs.environment]
54-
55-
var text = graph[view].text.wrappedValue
56-
if focusManager.isFocused(id) {
57-
text.append("_") // Cursor
58-
}
59-
60-
return DisplayList(items: [
61-
DisplayList.Item(
62-
frame: frame,
63-
content: .text(text, environment.style)
64-
)
65-
])
66-
}
67-
)
6830
}
6931
}

0 commit comments

Comments
 (0)