Skip to content

Commit 26627bd

Browse files
committed
Remove need for explicit backend typealias (using extensions) and make backend configurable via SCUI_BACKEND env var when running examples
1 parent 415155c commit 26627bd

File tree

17 files changed

+52
-49
lines changed

17 files changed

+52
-49
lines changed

Examples/Package.swift

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
11
// swift-tools-version:5.6
22

3+
import Foundation
34
import PackageDescription
45

5-
let backend = "GtkBackend"
6+
let backend = ProcessInfo.processInfo.environment["SCUI_BACKEND"] ?? "GtkBackend"
67

78
let exampleDependencies: [Target.Dependency] = [
89
"SwiftCrossUI",

Examples/Sources/ControlsExample/ControlsApp.swift

-2
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,6 @@ class ControlsState: Observable {
99

1010
@main
1111
struct ControlsApp: App {
12-
typealias Backend = SelectedBackend
13-
1412
let identifier = "dev.stackotter.Controls"
1513

1614
let state = ControlsState()

Examples/Sources/CounterExample/CounterApp.swift

-2
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,6 @@ class CounterState: Observable {
77

88
@main
99
struct CounterApp: App {
10-
typealias Backend = SelectedBackend
11-
1210
let identifier = "dev.stackotter.CounterApp"
1311

1412
let state = CounterState()

Examples/Sources/GreetingGeneratorExample/GreetingGeneratorApp.swift

-2
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,6 @@ class GreetingGeneratorState: Observable {
88

99
@main
1010
struct GreetingGeneratorApp: App {
11-
typealias Backend = SelectedBackend
12-
1311
let identifier = "dev.stackotter.GreetingGenerator"
1412

1513
let state = GreetingGeneratorState()

Examples/Sources/NavigationExample/NavigationApp.swift

-2
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,6 @@ class NavigationAppState: Observable {
2323

2424
@main
2525
struct NavigationApp: App {
26-
typealias Backend = SelectedBackend
27-
2826
let identifier = "dev.stackotter.NavigationApp"
2927

3028
let state = NavigationAppState()

Examples/Sources/RandomNumberGeneratorExample/RandomNumberGeneratorApp.swift

-2
Original file line numberDiff line numberDiff line change
@@ -27,8 +27,6 @@ enum ColorOption: String, CaseIterable {
2727

2828
@main
2929
struct RandomNumberGeneratorApp: App {
30-
typealias Backend = SelectedBackend
31-
3230
let identifier = "dev.stackotter.RandomNumberGeneratorApp"
3331

3432
let state = RandomNumberGeneratorState()

Examples/Sources/SplitExample/SplitApp.swift

-2
Original file line numberDiff line numberDiff line change
@@ -30,8 +30,6 @@ class SplitAppState: Observable {
3030

3131
@main
3232
struct SplitApp: App {
33-
typealias Backend = SelectedBackend
34-
3533
let identifier = "dev.stackotter.SplitApp"
3634

3735
let state = SplitAppState()

Examples/Sources/SpreadsheetExample/SpreadsheetApp.swift

-2
Original file line numberDiff line numberDiff line change
@@ -25,8 +25,6 @@ class SpreadsheetState: Observable {
2525

2626
@main
2727
struct SpreadsheetApp: App {
28-
typealias Backend = SelectedBackend
29-
3028
let identifier = "dev.stackotter.SpreadsheetApp"
3129

3230
let state = SpreadsheetState()

Examples/Sources/StressTestExample/StressTestApp.swift

-2
Original file line numberDiff line numberDiff line change
@@ -26,8 +26,6 @@ class StressTestState: Observable {
2626

2727
@main
2828
struct StressTestApp: App {
29-
typealias Backend = SelectedBackend
30-
3129
let identifier = "dev.stackotter.StressTestApp"
3230

3331
let state = StressTestState()

Examples/Sources/WindowingExample/WindowingApp.swift

-2
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,6 @@ class WindowingAppState: Observable {
99

1010
@main
1111
struct WindowingApp: App {
12-
typealias Backend = SelectedBackend
13-
1412
let identifier = "dev.stackotter.WindowPropertiesApp"
1513

1614
let state = WindowingAppState()

README.md

+26-18
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@ Here's a simple example app demonstrate how easy it is to get started with Swift
4141

4242
```swift
4343
import SwiftCrossUI
44+
// Import whichever backend you need
4445
import GtkBackend
4546

4647
class CounterState: Observable {
@@ -49,9 +50,9 @@ class CounterState: Observable {
4950

5051
@main
5152
struct CounterApp: App {
52-
// An experimental AppKit backend is also available when on macOS (try AppKitBackend)
53-
// There's also a Qt5 backend (QtBackend) available when qt5 is installed
54-
typealias Backend = GtkBackend
53+
// Optionally, you can explicitly select which imported backend to use (this is done for
54+
// you if only one backend is imported).
55+
// typealias Backend = GtkBackend
5556

5657
let identifier = "dev.stackotter.CounterApp"
5758

@@ -74,26 +75,33 @@ To run this example, run these commands:
7475

7576
```sh
7677
git clone https://github.com/stackotter/swift-cross-ui
77-
cd swift-cross-ui
78+
cd swift-cross-ui/Examples
7879
swift run CounterExample
7980
```
8081

81-
To see all of the examples, run these commands:
82+
### Other examples
8283

83-
```sh
84-
swift run CounterExample
85-
swift run RandomNumberGeneratorExample
86-
swift run WindowingExample
87-
swift run GreetingGeneratorExample
88-
swift run FileViewerExample
89-
swift run NavigationExample
90-
swift run SpitExample
91-
swift run StressTestExample
92-
swift run SpreadsheetExample
93-
swift run ControlsExample
94-
```
84+
A few examples are included with SwiftCrossUI to demonstrate some of its basic features;
85+
86+
- `CounterExample`, a simple app with buttons to increase and decrease a count.
87+
- `RandomNumberGeneratorExample`, a simple app to generate random numbers between a minimum and maximum.
88+
- `WindowingExample`, a simple app showcasing how ``WindowGroup`` is used to make multi-window apps and
89+
control the properties of each window.
90+
- `GreetingGeneratorExample`, a simple app demonstrating dynamic state and the ``ForEach`` view.
91+
- `FileViewerExample`, an app showcasing integration with the system's file chooser.
92+
- `NavigationExample`, an app showcasing ``NavigationStack`` and related concepts.
93+
- `SplitExample`, an app showcasing sidebar-based navigation with multiple levels.
94+
- `StressTestExample`, an app used to test view update performance.
95+
- `SpreadsheetExample`, an app showcasing tables.
96+
- `ControlsExample`, an app showcasing the various types of controls available.
9597

96-
All examples use the `GtkBackend` but can easily be updated to test out other backends.
98+
### Running examples on other backends
99+
100+
All examples use `GtkBackend` by default but you can override this using the `SCUI_BACKEND` environment variable like so;
101+
102+
```
103+
SCUI_BACKEND=QtBackend swift run CounterExample
104+
```
97105

98106
## Documentation
99107

Sources/AppKitBackend/AppKitBackend.swift

+4
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,10 @@
11
import AppKit
22
import SwiftCrossUI
33

4+
extension App {
5+
public typealias Backend = AppKitBackend
6+
}
7+
48
public struct AppKitBackend: AppBackend {
59
public typealias Window = NSWindow
610
public typealias Widget = NSView

Sources/CursesBackend/CursesBackend.swift

+4
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,10 @@ import Foundation
22
import SwiftCrossUI
33
import TermKit
44

5+
extension App {
6+
public typealias Backend = CursesBackend
7+
}
8+
59
public final class CursesBackend: AppBackend {
610
public typealias Window = RootView
711
public typealias Widget = TermKit.View

Sources/GtkBackend/GtkBackend.swift

+4
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,10 @@ import Foundation
33
import Gtk
44
import SwiftCrossUI
55

6+
extension App {
7+
public typealias Backend = GtkBackend
8+
}
9+
610
extension SwiftCrossUI.Color {
711
public var gtkColor: Gtk.Color {
812
return Gtk.Color(red, green, blue, alpha)

Sources/LVGLBackend/LVGLBackend.swift

+4
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,10 @@ import Foundation
33
import LVGL
44
import SwiftCrossUI
55

6+
extension App {
7+
public typealias Backend = LVGLBackend
8+
}
9+
610
public final class LVGLBackend: AppBackend {
711
public class Widget {
812
private var createWithParent: (LVObject) -> LVObject

Sources/QtBackend/QtBackend.swift

+4
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,10 @@ import Foundation
22
import Qlift
33
import SwiftCrossUI
44

5+
extension App {
6+
public typealias Backend = QtBackend
7+
}
8+
59
// TODO: Remove default padding from QBoxLayout related widgets.
610
// TODO: Fix window size code, currently seems to get pretty ignored.
711

Sources/SwiftCrossUI/SwiftCrossUI.docc/Examples.md

+4-12
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ An overview of the examples included with SwiftCrossUI.
44

55
## Overview
66

7-
A few examples are included with SwiftCrossUI to demonstrate some of it's basic features. These examples are:
7+
A few examples are included with SwiftCrossUI to demonstrate some of its basic features;
88

99
- `CounterExample`, a simple app with buttons to increase and decrease a count.
1010
- `RandomNumberGeneratorExample`, a simple app to generate random numbers between a minimum and maximum.
@@ -18,16 +18,8 @@ A few examples are included with SwiftCrossUI to demonstrate some of it's basic
1818
- `SpreadsheetExample`, an app showcasing tables.
1919
- `ControlsExample`, an app showcasing the various types of controls available.
2020

21-
To run an example, either select the example under schemes at the top of the window (in Xcode), or run one of the following commands (if using a terminal):
21+
To run an example, either select the example under schemes at the top of the window (in Xcode), or run it from the command line like so:
22+
2223
```
23-
swift run CounterExample
24-
swift run RandomNumberGeneratorExample
25-
swift run WindowingExample
26-
swift run GreetingGeneratorExample
27-
swift run FileViewerExample
28-
swift run NavigationExample
29-
swift run SpitExample
30-
swift run StressTestExample
31-
swift run SpreadsheetExample
32-
swift run ControlsExample
24+
swift run ExampleToRun
3325
```

0 commit comments

Comments
 (0)