|
| 1 | +import DefaultBackend |
| 2 | +import Foundation |
| 3 | +import SwiftCrossUI |
| 4 | + |
| 5 | +#if canImport(WinUIBackend) |
| 6 | + import WinUI |
| 7 | +#endif |
| 8 | + |
| 9 | +#if canImport(SwiftBundlerRuntime) |
| 10 | + import SwiftBundlerRuntime |
| 11 | +#endif |
| 12 | + |
| 13 | +@main |
| 14 | +@HotReloadable |
| 15 | +struct CounterApp: App { |
| 16 | + @State var count = 0 |
| 17 | + @State var value = 0.0 |
| 18 | + @State var color: String? = nil |
| 19 | + @State var name = "" |
| 20 | + |
| 21 | + var body: some Scene { |
| 22 | + WindowGroup("CounterExample: \(count)") { |
| 23 | + #hotReloadable { |
| 24 | + ScrollView { |
| 25 | + HStack(spacing: 20) { |
| 26 | + Button("-") { |
| 27 | + count -= 1 |
| 28 | + } |
| 29 | + |
| 30 | + Text("Count: \(count)") |
| 31 | + .inspect { text in |
| 32 | + #if canImport(AppKitBackend) |
| 33 | + text.isSelectable = true |
| 34 | + #elseif canImport(UIKitBackend) |
| 35 | + #if targetEnvironment(macCatalyst) |
| 36 | + text.isHighlighted = true |
| 37 | + text.highlightTextColor = .yellow |
| 38 | + #endif |
| 39 | + #elseif canImport(WinUIBackend) |
| 40 | + text.isTextSelectionEnabled = true |
| 41 | + #elseif canImport(GtkBackend) |
| 42 | + text.selectable = true |
| 43 | + #elseif canImport(Gtk3Backend) |
| 44 | + text.selectable = true |
| 45 | + #endif |
| 46 | + } |
| 47 | + |
| 48 | + Button("+") { |
| 49 | + count += 1 |
| 50 | + }.inspect(.afterUpdate) { button in |
| 51 | + #if canImport(AppKitBackend) |
| 52 | + // Button is an NSButton on macOS |
| 53 | + button.bezelColor = .red |
| 54 | + #elseif canImport(UIKitBackend) |
| 55 | + if #available(iOS 15.0, *) { |
| 56 | + button.configuration = .bordered() |
| 57 | + } |
| 58 | + #elseif canImport(WinUIBackend) |
| 59 | + button.cornerRadius.topLeft = 10 |
| 60 | + let brush = WinUI.SolidColorBrush() |
| 61 | + brush.color = .init(a: 255, r: 255, g: 0, b: 0) |
| 62 | + button.background = brush |
| 63 | + #elseif canImport(GtkBackend) |
| 64 | + button.css.set(property: .backgroundColor(.init(1, 0, 0))) |
| 65 | + #elseif canImport(Gtk3Backend) |
| 66 | + button.css.set(property: .backgroundColor(.init(1, 0, 0))) |
| 67 | + #endif |
| 68 | + } |
| 69 | + } |
| 70 | + |
| 71 | + Slider($value, minimum: 0, maximum: 10) |
| 72 | + .inspect { slider in |
| 73 | + #if canImport(AppKitBackend) |
| 74 | + slider.numberOfTickMarks = 10 |
| 75 | + #elseif canImport(UIKitBackend) |
| 76 | + slider.thumbTintColor = .blue |
| 77 | + #elseif canImport(WinUIBackend) |
| 78 | + slider.isThumbToolTipEnabled = true |
| 79 | + #elseif canImport(GtkBackend) |
| 80 | + slider.drawValue = true |
| 81 | + #elseif canImport(Gtk3Backend) |
| 82 | + slider.drawValue = true |
| 83 | + #endif |
| 84 | + } |
| 85 | + |
| 86 | + #if !canImport(Gtk3Backend) |
| 87 | + Picker(of: ["Red", "Green", "Blue"], selection: $color) |
| 88 | + .inspect(.afterUpdate) { picker in |
| 89 | + #if canImport(AppKitBackend) |
| 90 | + picker.preferredEdge = .maxX |
| 91 | + #elseif canImport(UIKitBackend) && os(iOS) |
| 92 | + // Can't think of something to do to the |
| 93 | + // UIPickerView, but the point is that you |
| 94 | + // could do something if you needed to! |
| 95 | + // This would be a UITableView on tvOS. |
| 96 | + // And could be either a UITableView or a |
| 97 | + // UIPickerView on Mac Catalyst depending |
| 98 | + // on Mac Catalyst version and interface |
| 99 | + // idiom. |
| 100 | + #elseif canImport(WinUIBackend) |
| 101 | + let brush = WinUI.SolidColorBrush() |
| 102 | + brush.color = .init(a: 255, r: 255, g: 0, b: 0) |
| 103 | + picker.background = brush |
| 104 | + #elseif canImport(GtkBackend) |
| 105 | + picker.enableSearch = true |
| 106 | + #endif |
| 107 | + } |
| 108 | + #endif |
| 109 | + |
| 110 | + TextField("Name", text: $name) |
| 111 | + .inspect(.afterUpdate) { textField in |
| 112 | + #if canImport(AppKitBackend) |
| 113 | + textField.backgroundColor = .blue |
| 114 | + #elseif canImport(UIKitBackend) |
| 115 | + textField.borderStyle = .bezel |
| 116 | + #elseif canImport(WinUIBackend) |
| 117 | + textField.selectionHighlightColor.color = .init(a: 255, r: 0, g: 255, b: 0) |
| 118 | + let brush = WinUI.SolidColorBrush() |
| 119 | + brush.color = .init(a: 255, r: 0, g: 0, b: 255) |
| 120 | + textField.background = brush |
| 121 | + #elseif canImport(GtkBackend) |
| 122 | + textField.xalign = 1 |
| 123 | + textField.css.set(property: .backgroundColor(.init(0, 0, 1))) |
| 124 | + #elseif canImport(Gtk3Backend) |
| 125 | + textField.hasFrame = false |
| 126 | + textField.css.set(property: .backgroundColor(.init(0, 0, 1))) |
| 127 | + #endif |
| 128 | + } |
| 129 | + |
| 130 | + ScrollView { |
| 131 | + ForEach(Array(1...50)) { number in |
| 132 | + Text("Line \(number)") |
| 133 | + }.padding() |
| 134 | + }.inspect(.afterUpdate) { scrollView in |
| 135 | + #if canImport(AppKitBackend) |
| 136 | + scrollView.borderType = .grooveBorder |
| 137 | + #elseif canImport(UIKitBackend) |
| 138 | + scrollView.alwaysBounceHorizontal = true |
| 139 | + #elseif canImport(WinUIBackend) |
| 140 | + let brush = WinUI.SolidColorBrush() |
| 141 | + brush.color = .init(a: 255, r: 0, g: 255, b: 0) |
| 142 | + scrollView.borderBrush = brush |
| 143 | + scrollView.borderThickness = .init( |
| 144 | + left: 1, top: 1, right: 1, bottom: 1 |
| 145 | + ) |
| 146 | + #elseif canImport(GtkBackend) |
| 147 | + scrollView.css.set(property: .border(color: .init(1, 0, 0), width: 2)) |
| 148 | + #elseif canImport(Gtk3Backend) |
| 149 | + scrollView.css.set(property: .border(color: .init(1, 0, 0), width: 2)) |
| 150 | + #endif |
| 151 | + }.frame(height: 200) |
| 152 | + |
| 153 | + List(["Red", "Green", "Blue"], id: \.self, selection: $color) { color in |
| 154 | + Text(color) |
| 155 | + }.inspect(.afterUpdate) { table in |
| 156 | + #if canImport(AppKitBackend) |
| 157 | + table.usesAlternatingRowBackgroundColors = true |
| 158 | + #elseif canImport(UIKitBackend) |
| 159 | + table.isEditing = true |
| 160 | + #elseif canImport(WinUIBackend) |
| 161 | + let brush = WinUI.SolidColorBrush() |
| 162 | + brush.color = .init(a: 255, r: 255, g: 0, b: 255) |
| 163 | + table.borderBrush = brush |
| 164 | + table.borderThickness = .init( |
| 165 | + left: 1, top: 1, right: 1, bottom: 1 |
| 166 | + ) |
| 167 | + #elseif canImport(GtkBackend) |
| 168 | + table.showSeparators = true |
| 169 | + #elseif canImport(Gtk3Backend) |
| 170 | + table.selectionMode = .multiple |
| 171 | + #endif |
| 172 | + } |
| 173 | + |
| 174 | + Image(Bundle.module.bundleURL.appendingPathComponent("Banner.png")) |
| 175 | + .resizable() |
| 176 | + .inspect(.afterUpdate) { image in |
| 177 | + #if canImport(AppKitBackend) |
| 178 | + image.isEditable = true |
| 179 | + #elseif canImport(UIKitBackend) |
| 180 | + image.layer.borderWidth = 1 |
| 181 | + image.layer.borderColor = .init(red: 0, green: 1, blue: 0, alpha: 1) |
| 182 | + #elseif canImport(WinUIBackend) |
| 183 | + // Couldn't find anything visually interesting |
| 184 | + // to do to the WinUI.Image, but the point is |
| 185 | + // that you could do something if you wanted to. |
| 186 | + #elseif canImport(GtkBackend) |
| 187 | + image.css.set(property: .border(color: .init(0, 1, 0), width: 2)) |
| 188 | + #elseif canImport(Gtk3Backend) |
| 189 | + image.css.set(property: .border(color: .init(0, 1, 0), width: 2)) |
| 190 | + #endif |
| 191 | + } |
| 192 | + .aspectRatio(contentMode: .fit) |
| 193 | + }.padding() |
| 194 | + } |
| 195 | + } |
| 196 | + .defaultSize(width: 400, height: 200) |
| 197 | + } |
| 198 | +} |
0 commit comments