Skip to content

Commit c032c6e

Browse files
committed
Reimplement foregroundColor modifier under new layout system (much better now)
1 parent 8c554ca commit c032c6e

File tree

6 files changed

+80
-46
lines changed

6 files changed

+80
-46
lines changed

Examples/Sources/SplitExample/SplitApp.swift

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -127,7 +127,8 @@ struct SplitApp: App {
127127
default:
128128
Text("Select a subject")
129129
}
130-
}.padding(10)
130+
}
131+
.padding(10)
131132
}
132133
}
133134
}

Sources/AppKitBackend/AppKitBackend.swift

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -270,9 +270,16 @@ public struct AppKitBackend: AppBackend {
270270
return .view(textView)
271271
}
272272

273-
public func updateTextView(_ textView: Widget, content: String, shouldWrap: Bool) {
274-
// TODO: Implement text wrap handling
275-
(textView.view as! NSTextField).stringValue = content
273+
public func updateTextView(_ textView: Widget, content: String, environment: Environment) {
274+
let field = textView.view as! NSTextField
275+
field.stringValue = content
276+
let textColor = environment.foregroundColor
277+
field.textColor = NSColor(
278+
calibratedRed: CGFloat(textColor.red),
279+
green: CGFloat(textColor.green),
280+
blue: CGFloat(textColor.blue),
281+
alpha: 1
282+
)
276283
}
277284

278285
public func createButton() -> Widget {

Sources/SwiftCrossUI/Backend/AppBackend.swift

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -174,9 +174,8 @@ public protocol AppBackend {
174174
/// Creates a non-editable text view with optional text wrapping. Predominantly used
175175
/// by ``Text``.`
176176
func createTextView() -> Widget
177-
/// Sets the content and wrapping mode of a non-editable text view. If `shouldWrap`
178-
/// is `false`, text shouldn't be wrapped onto multiple lines.
179-
func updateTextView(_ textView: Widget, content: String, shouldWrap: Bool)
177+
/// Sets the content and wrapping mode of a non-editable text view.
178+
func updateTextView(_ textView: Widget, content: String, environment: Environment)
180179

181180
/// Creates an image view from an image file (specified by path). Predominantly used
182181
/// by ``Image``.
@@ -307,7 +306,7 @@ extension AppBackend {
307306
public func createTextView(content: String, shouldWrap: Bool) -> Widget {
308307
todo()
309308
}
310-
public func updateTextView(_ textView: Widget, content: String, shouldWrap: Bool) {
309+
public func updateTextView(_ textView: Widget, content: String, environment: Environment) {
311310
todo()
312311
}
313312

Lines changed: 58 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -1,38 +1,63 @@
1-
// extension View {
2-
// /// Sets the color of the foreground elements displayed by this view.
3-
// public func foregroundColor(_ color: Color) -> some View {
4-
// return ForegroundView(
5-
// self,
6-
// color: color
7-
// )
8-
// }
9-
// }
1+
extension View {
2+
/// Sets the color of the foreground elements displayed by this view.
3+
public func foregroundColor(_ color: Color) -> some View {
4+
return EnvironmentModifier(self) { environment in
5+
print("Updating fg color")
6+
return environment.with(\.foregroundColor, color)
7+
}
8+
}
9+
}
1010

11-
// /// The implementation for the ``View/foregroundColor(_:)`` view modifier.
12-
// struct ForegroundView<Child: View>: TypeSafeView {
13-
// var body: VariadicView1<Child>
11+
struct EnvironmentModifier<Child: View>: TypeSafeView {
12+
typealias Children = VariadicView1<Child>.Children
1413

15-
// /// The foreground color to use.
16-
// var color: Color
14+
var body: VariadicView1<Child>
15+
var modification: (Environment) -> Environment
1716

18-
// /// Wraps a child view and sets a specific foreground color.
19-
// init(_ child: Child, color: Color) {
20-
// self.body = VariadicView1(child)
21-
// self.color = color
22-
// }
17+
init(_ child: Child, modification: @escaping (Environment) -> Environment) {
18+
self.body = VariadicView1(child)
19+
self.modification = modification
20+
}
2321

24-
// func asWidget<Backend: AppBackend>(
25-
// _ children: ViewGraphNodeChildren1<Child>,
26-
// backend: Backend
27-
// ) -> Backend.Widget {
28-
// return backend.createStyleContainer(for: children.child0.widget.into())
29-
// }
22+
func children<Backend: AppBackend>(
23+
backend: Backend,
24+
snapshots: [ViewGraphSnapshotter.NodeSnapshot]?,
25+
environment: Environment
26+
) -> Children {
27+
body.children(
28+
backend: backend,
29+
snapshots: snapshots,
30+
environment: modification(environment)
31+
)
32+
}
3033

31-
// func update<Backend: AppBackend>(
32-
// _ widget: Backend.Widget,
33-
// children: ViewGraphNodeChildren1<Child>,
34-
// backend: Backend
35-
// ) {
36-
// backend.setForegroundColor(ofStyleContainer: widget, to: color)
37-
// }
38-
// }
34+
func layoutableChildren<Backend: AppBackend>(
35+
backend: Backend,
36+
children: Children
37+
) -> [LayoutSystem.LayoutableChild] {
38+
[]
39+
}
40+
41+
func asWidget<Backend: AppBackend>(
42+
_ children: Children,
43+
backend: Backend
44+
) -> Backend.Widget {
45+
return body.asWidget(children, backend: backend)
46+
}
47+
48+
func update<Backend: AppBackend>(
49+
_ widget: Backend.Widget,
50+
children: Children,
51+
proposedSize: SIMD2<Int>,
52+
environment: Environment,
53+
backend: Backend
54+
) -> ViewUpdateResult {
55+
return body.update(
56+
widget,
57+
children: children,
58+
proposedSize: proposedSize,
59+
environment: modification(environment),
60+
backend: backend
61+
)
62+
}
63+
}

Sources/SwiftCrossUI/ViewGraph/Environment.swift

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,16 @@
11
public struct Environment {
2-
var onResize: (_ newSize: ViewUpdateResult) -> Void
3-
var layoutOrientation: Orientation
4-
var layoutAlignment: StackAlignment
5-
var layoutSpacing: Int
2+
public var onResize: (_ newSize: ViewUpdateResult) -> Void
3+
public var layoutOrientation: Orientation
4+
public var layoutAlignment: StackAlignment
5+
public var layoutSpacing: Int
6+
public var foregroundColor: Color
67

78
init() {
89
onResize = { _ in }
910
layoutOrientation = .vertical
1011
layoutAlignment = .center
1112
layoutSpacing = 10
13+
foregroundColor = .black
1214
}
1315

1416
func with<T>(_ keyPath: WritableKeyPath<Self, T>, _ newValue: T) -> Self {

Sources/SwiftCrossUI/Views/Text.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ public struct Text: ElementaryView, View {
2525
backend: Backend
2626
) -> ViewUpdateResult {
2727
let size = backend.size(of: string, in: proposedSize)
28-
backend.updateTextView(widget, content: string, shouldWrap: true)
28+
backend.updateTextView(widget, content: string, environment: environment)
2929
backend.setSize(of: widget, to: size)
3030
// TODO: Query from backend
3131
let lineHeight = 12

0 commit comments

Comments
 (0)