Skip to content

Commit 2b38d73

Browse files
committed
Added UIKit Code
1 parent 6496921 commit 2b38d73

File tree

3 files changed

+49
-6
lines changed

3 files changed

+49
-6
lines changed

Examples/Sources/ControlFocusabilityTest/ControlsFocusabilityApp.swift

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,7 @@ struct ControlsFocusabilityApp: App {
8383
.focusable(false)
8484
}
8585
#endif
86-
86+
#if !os(tvOS)
8787
HStack {
8888
VStack {
8989
Text("Slider")
@@ -95,6 +95,7 @@ struct ControlsFocusabilityApp: App {
9595
Toggle("focusable", active: $isSliderFocusable)
9696
.focusable(false)
9797
}
98+
#endif
9899
HStack {
99100
VStack {
100101
Text("Text field")

Examples/Sources/ControlsExample/ControlsApp.swift

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,13 +57,14 @@ struct ControlsApp: App {
5757
Text("Currently enabled: \(exampleCheckboxState)")
5858
}
5959
#endif
60-
60+
#if !os(tvOS)
6161
VStack {
6262
Text("Slider")
6363
Slider($sliderValue, minimum: 0, maximum: 10)
6464
.frame(maxWidth: 200)
6565
Text("Value: \(String(format: "%.02f", sliderValue))")
6666
}
67+
#endif
6768

6869
VStack {
6970
Text("Text field")

Sources/UIKitBackend/UIKitBackend+Control.swift

Lines changed: 45 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,13 @@ import UIKit
33

44
final class ButtonWidget: WrapperWidget<UIButton> {
55
private let event: UIControl.Event
6-
6+
7+
var canBeFocused: Bool = true
8+
9+
override var canBecomeFocused: Bool {
10+
canBeFocused
11+
}
12+
713
var onTap: (() -> Void)? {
814
didSet {
915
if oldValue == nil {
@@ -33,6 +39,12 @@ final class ButtonWidget: WrapperWidget<UIButton> {
3339
final class TextFieldWidget: WrapperWidget<UITextField>, UITextFieldDelegate {
3440
var onChange: ((String) -> Void)?
3541
var onSubmit: (() -> Void)?
42+
43+
var canBeFocused: Bool = true
44+
45+
override var canBecomeFocused: Bool {
46+
canBeFocused
47+
}
3648

3749
@objc
3850
func textChanged() {
@@ -59,6 +71,12 @@ final class TextFieldWidget: WrapperWidget<UITextField>, UITextFieldDelegate {
5971

6072
final class TextEditorWidget: WrapperWidget<UITextView>, UITextViewDelegate {
6173
var onChange: ((String) -> Void)?
74+
75+
var canBeFocused: Bool = true
76+
77+
override var canBecomeFocused: Bool {
78+
canBeFocused
79+
}
6280

6381
init() {
6482
super.init(child: UITextView())
@@ -73,6 +91,12 @@ final class TextEditorWidget: WrapperWidget<UITextView>, UITextViewDelegate {
7391
#if os(tvOS)
7492
final class SwitchWidget: WrapperWidget<UISegmentedControl> {
7593
var onChange: ((Bool) -> Void)?
94+
95+
var canBeFocused: Bool = true
96+
97+
override var canBecomeFocused: Bool {
98+
canBeFocused
99+
}
76100

77101
@objc
78102
func switchFlipped() {
@@ -97,7 +121,13 @@ final class TextEditorWidget: WrapperWidget<UITextView>, UITextViewDelegate {
97121
#else
98122
final class SwitchWidget: WrapperWidget<UISwitch> {
99123
var onChange: ((Bool) -> Void)?
100-
124+
125+
var canBeFocused: Bool = true
126+
127+
override var canBecomeFocused: Bool {
128+
canBeFocused
129+
}
130+
101131
@objc
102132
func switchFlipped() {
103133
onChange?(child.isOn)
@@ -198,9 +228,16 @@ final class HoverableWidget: ContainerWidget {
198228
}
199229
}
200230
}
231+
201232
@available(tvOS, unavailable)
202233
final class SliderWidget: WrapperWidget<UISlider> {
203234
var onChange: ((Double) -> Void)?
235+
236+
var canBeFocused: Bool = true
237+
238+
override var canBecomeFocused: Bool {
239+
canBeFocused
240+
}
204241

205242
private var _decimalPlaces = 17
206243
var decimalPlaces: Int {
@@ -263,11 +300,12 @@ extension UIKitBackend {
263300
action: @escaping () -> Void
264301
) {
265302
let buttonWidget = button as! ButtonWidget
266-
303+
267304
setButtonTitle(buttonWidget, label, environment: environment)
268-
305+
269306
buttonWidget.onTap = action
270307
buttonWidget.child.isEnabled = environment.isEnabled
308+
buttonWidget.canBeFocused = environment.isFocusable
271309
}
272310

273311
public func createTextField() -> Widget {
@@ -293,6 +331,7 @@ extension UIKitBackend {
293331
let (keyboardType, contentType) = splitTextContentType(environment.textContentType)
294332
textFieldWidget.child.keyboardType = keyboardType
295333
textFieldWidget.child.textContentType = contentType
334+
textFieldWidget.canBeFocused = environment.isFocusable
296335

297336
#if os(iOS)
298337
if let updateToolbar = environment.updateToolbar {
@@ -340,6 +379,7 @@ extension UIKitBackend {
340379
let (keyboardType, contentType) = splitTextContentType(environment.textContentType)
341380
textEditorWidget.child.keyboardType = keyboardType
342381
textEditorWidget.child.textContentType = contentType
382+
textEditorWidget.canBeFocused = environment.isFocusable
343383

344384
#if os(iOS)
345385
if let updateToolbar = environment.updateToolbar {
@@ -420,6 +460,7 @@ extension UIKitBackend {
420460
let wrapper = switchWidget as! SwitchWidget
421461
wrapper.onChange = onChange
422462
wrapper.child.isEnabled = environment.isEnabled
463+
wrapper.canBeFocused = environment.isFocusable
423464
}
424465

425466
public func setState(ofSwitch switchWidget: Widget, to state: Bool) {

0 commit comments

Comments
 (0)