|
| 1 | +import DefaultBackend |
| 2 | +import SwiftCrossUI |
| 3 | + |
| 4 | +#if canImport(SwiftBundlerRuntime) |
| 5 | + import SwiftBundlerRuntime |
| 6 | +#endif |
| 7 | + |
| 8 | +@main |
| 9 | +@HotReloadable |
| 10 | +struct ControlsFocusabilityApp: App { |
| 11 | + @State var count = 0 |
| 12 | + @State var exampleButtonState = false |
| 13 | + @State var exampleSwitchState = false |
| 14 | + @State var exampleCheckboxState = false |
| 15 | + @State var sliderValue = 5.0 |
| 16 | + @State var text = "" |
| 17 | + @State var flavor: String? = nil |
| 18 | + @State var enabled = true |
| 19 | + @State var isButtonFocusable = true |
| 20 | + @State var isToggleButtonFocusable = true |
| 21 | + @State var isToggleSwitchFocusable = true |
| 22 | + @State var isCheckboxFocusable = true |
| 23 | + @State var isSliderFocusable = true |
| 24 | + @State var isTextFieldFocusable = true |
| 25 | + @State var isPickerFocusable = true |
| 26 | + |
| 27 | + var body: some Scene { |
| 28 | + WindowGroup("ControlsApp") { |
| 29 | + #hotReloadable { |
| 30 | + ScrollView { |
| 31 | + VStack(spacing: 30) { |
| 32 | + HStack { |
| 33 | + VStack { |
| 34 | + Text("Button") |
| 35 | + Button("Click me!") { |
| 36 | + count += 1 |
| 37 | + } |
| 38 | + .focusable(isButtonFocusable) |
| 39 | + Text("Count: \(count)") |
| 40 | + } |
| 41 | + Toggle("focusable", active: $isButtonFocusable) |
| 42 | + .focusable(false) |
| 43 | + } |
| 44 | + .padding(.bottom, 20) |
| 45 | + |
| 46 | +#if !canImport(UIKitBackend) |
| 47 | + HStack { |
| 48 | + VStack { |
| 49 | + Text("Toggle button") |
| 50 | + Toggle("Toggle me!", active: $exampleButtonState) |
| 51 | + .toggleStyle(.button) |
| 52 | + .focusable(isToggleButtonFocusable) |
| 53 | + Text("Currently enabled: \(exampleButtonState)") |
| 54 | + } |
| 55 | + Toggle("focusable", active: $isToggleButtonFocusable) |
| 56 | + .focusable(false) |
| 57 | + } |
| 58 | + .padding(.bottom, 20) |
| 59 | +#endif |
| 60 | + |
| 61 | + HStack { |
| 62 | + VStack { |
| 63 | + Text("Toggle switch") |
| 64 | + Toggle("Toggle me:", active: $exampleSwitchState) |
| 65 | + .toggleStyle(.switch) |
| 66 | + .focusable(isToggleSwitchFocusable) |
| 67 | + Text("Currently enabled: \(exampleSwitchState)") |
| 68 | + } |
| 69 | + Toggle("focusable", active: $isToggleSwitchFocusable) |
| 70 | + .focusable(false) |
| 71 | + } |
| 72 | + |
| 73 | +#if !canImport(UIKitBackend) |
| 74 | + HStack { |
| 75 | + VStack { |
| 76 | + Text("Checkbox") |
| 77 | + Toggle("Toggle me:", active: $exampleCheckboxState) |
| 78 | + .toggleStyle(.checkbox) |
| 79 | + .focusable(isCheckboxFocusable) |
| 80 | + Text("Currently enabled: \(exampleCheckboxState)") |
| 81 | + } |
| 82 | + Toggle("focusable", active: $isCheckboxFocusable) |
| 83 | + .focusable(false) |
| 84 | + } |
| 85 | +#endif |
| 86 | + |
| 87 | + HStack { |
| 88 | + VStack { |
| 89 | + Text("Slider") |
| 90 | + Slider($sliderValue, minimum: 0, maximum: 10) |
| 91 | + .frame(maxWidth: 200) |
| 92 | + .focusable(isSliderFocusable) |
| 93 | + Text("Value: \(String(format: "%.02f", sliderValue))") |
| 94 | + } |
| 95 | + Toggle("focusable", active: $isSliderFocusable) |
| 96 | + .focusable(false) |
| 97 | + } |
| 98 | + HStack { |
| 99 | + VStack { |
| 100 | + Text("Text field") |
| 101 | + TextField("Text field", text: $text) |
| 102 | + .focusable(isTextFieldFocusable) |
| 103 | + Text("Value: \(text)") |
| 104 | + } |
| 105 | + Toggle("focusable", active: $isTextFieldFocusable) |
| 106 | + .focusable(false) |
| 107 | + } |
| 108 | + |
| 109 | + HStack { |
| 110 | + VStack { |
| 111 | + Text("Drop down") |
| 112 | + HStack { |
| 113 | + Text("Flavor: ") |
| 114 | + Picker(of: ["Vanilla", "Chocolate", "Strawberry"], selection: $flavor) |
| 115 | + .focusable(isPickerFocusable) |
| 116 | + } |
| 117 | + Text("You chose: \(flavor ?? "Nothing yet!")") |
| 118 | + } |
| 119 | + Toggle("focusable", active: $isPickerFocusable) |
| 120 | + .focusable(false) |
| 121 | + } |
| 122 | + } |
| 123 | + .padding() |
| 124 | + .disabled(!enabled) |
| 125 | + |
| 126 | + Toggle(enabled ? "Disable all" : "Enable all", active: $enabled) |
| 127 | + .padding() |
| 128 | + .focusable(false) |
| 129 | + } |
| 130 | + .frame(minHeight: 600) |
| 131 | + } |
| 132 | + |
| 133 | + }.defaultSize(width: 400, height: 600) |
| 134 | + } |
| 135 | +} |
0 commit comments